unit U_MainWFBCKList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, DBClient, cxCheckBox, cxCalendar, cxSplitter,
  RM_Dataset, RM_System, RM_Common, RM_Class, RM_GridReport, RM_e_Xls,
  Menus, cxButtonEdit, cxDropDownEdit, cxPC, Buttons, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxSkinsCore,  
     dxSkinDarkRoom,
    
     
     
    
    
    
    
   dxSkinOffice2013White, 
     dxSkinSharpPlus,
   dxSkinSpringTime,  
   dxSkinsDefaultPainters, 
     dxSkinscxPCPainter;

type
  TfrmMainWFBCKList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    Panel1: TPanel;
    Label2: TLabel;
    MLName: TEdit;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    V2MYName: TcxGridDBColumn;
    V2MYCode: TcxGridDBColumn;
    V2Qty: TcxGridDBColumn;
    V2KgQty: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    V2MYMF: TcxGridDBColumn;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    V2Column1: TcxGridDBColumn;
    V2Column2: TcxGridDBColumn;
    V2Column3: TcxGridDBColumn;
    V2Column4: TcxGridDBColumn;
    V2Column5: TcxGridDBColumn;
    Label4: TLabel;
    MLCodeName: TEdit;
    TV2Column1: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ZJStatusChange(Sender: TObject);
    procedure MYCodeChange(Sender: TObject);
    procedure DataTypeChange(Sender: TObject);
  private
    canshu1:string;
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    { Private declarations }
  public
    FFInt,FCloth:Integer;

    { Public declarations }
  end;

var
  frmMainWFBCKList: TfrmMainWFBCKList;

implementation
uses
  U_DataLink,U_RTFun, U_QCRKInPut,U_ZDYHelp,U_LLRKInPut,U_GYSList,U_RCCKInPut,
  U_LLRKInPutMH,U_LLRKInPutX,U_LLRKInPut_CG,U_LLRKInPutMH_CG,U_LLRKInPutMH_PBCG,
  U_LLRKInPutMH_PBKCCG,U_BPZdy_LRPB,U_LLRKInPutMH_MCG,U_BPZdy_LRM,U_MCGRKList_MKC_CX;

{$R *.dfm}

procedure TfrmMainWFBCKList.FormDestroy(Sender: TObject);
begin
  frmMainWFBCKList:=nil;
end;

procedure TfrmMainWFBCKList.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TfrmMainWFBCKList.FormCreate(Sender: TObject);
begin
  canshu1:=Trim(DParameters1);
end;

procedure TfrmMainWFBCKList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('棉WFBCK',Tv2,'贸易生产管理');
  Close;
end;

procedure TfrmMainWFBCKList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' exec P_Viwe_MainWFBCK :begdate,:enddate');
      Parameters.ParamByName('begdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date));
      Parameters.ParamByName('enddate').Value:=Trim(FormatDateTime('yyyy-MM-dd',enddate.Date));
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmMainWFBCKList.InitForm();
begin
  BegDate.Date:=SGetServerDateMBeg(ADOQueryTemp);
  EndDate.Date:=SGetServerDate(ADOQueryTemp);
  ReadCxGrid('棉WFBCK',Tv2,'贸易生产管理');
  InitGrid();
end;

procedure TfrmMainWFBCKList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

function TfrmMainWFBCKList.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      //First;
     // while Order_Main.Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' insert into CK_MYSC_CR_Log  ');
          sql.Add(' select * from CK_MYSC_CR where MYID='''+Trim(Order_Main.fieldbyname('MYID').AsString)+'''');
          sql.Add(' UPdate CK_MYSC_CR_Log Set DelTime=getdate(),DelerCode='''+Trim(DCode)+''',Deler='''+Trim(DName)+'''');
          sql.Add(' where MYID='''+Trim(Order_Main.fieldbyname('MYID').AsString)+'''');
          sql.Add(' Delete CK_MYSC_CR  where MYId='''+Trim(Order_Main.fieldbyname('MYId').AsString)+'''');
          sql.Add(' delete from TP_File where WBID='''+trim(Order_Main.fieldbyname('MYId').AsString)+''' ');
          sql.Add(' and TFType=''采购坯布''');
          ExecSQL;
        end;
        Order_Main.Delete;
      end;
    end;
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmMainWFBCKList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel(Self.Caption,cxGrid2);
end;

procedure TfrmMainWFBCKList.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmMainWFBCKList.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmMainWFBCKList.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmMainWFBCKList.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmMainWFBCKList.ToolButton1Click(Sender: TObject);
begin
  try
    frmMCGRKList_MKC_CX:=TfrmMCGRKList_MKC_CX.Create(Application);
    with frmMCGRKList_MKC_CX do
    begin
      FMYCode:=Trim(Self.Order_Main.fieldbyname('MYCode').AsString);
      FMYName:=Trim(Self.Order_Main.fieldbyname('MYName').AsString);
      if ShowModal=1 then
      begin
    
      end;
    end;
  finally
    frmMCGRKList_MKC_CX.Free;
  end;
end;

procedure TfrmMainWFBCKList.CustomerNoNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmMainWFBCKList.N2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_MYSC_CR_MX where MYID='''+Trim(Order_Main.fieldbyname('MYID').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('已产生明细数据，不能删除!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if DelData() then
  begin
    //Order_Main.Delete;
  end;
end;

procedure TfrmMainWFBCKList.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmMainWFBCKList.ZJStatusChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmMainWFBCKList.MYCodeChange(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmMainWFBCKList.DataTypeChange(Sender: TObject);
begin
  TBFind.Click;
end;

end.
