unit U_JWLInList;

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, cxCalendar, cxButtonEdit, cxDropDownEdit,
  DBClient;

type
  TfrmJWLInList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    CDS_Main: TClientDataSet;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure YCLNameChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmJWLInList: TfrmJWLInList;

implementation
uses
  U_DataLink,U_Fun,U_JWLIn;

{$R *.dfm}

procedure TfrmJWLInList.FormDestroy(Sender: TObject);
begin
  frmJWLInList:=nil;
end;

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

procedure TfrmJWLInList.FormCreate(Sender: TObject);
begin
  cxgrid1.Align:=alClient;
end;

procedure TfrmJWLInList.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('入库列表',Tv1,'贝斯仓库');
end;

procedure TfrmJWLInList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select A.*,B.*,GYSName=C.ZDYName from CK_YCL_Main A inner join CK_YCL_Sub B on A.MainId=B.MainId ');
      sql.Add(' left join KH_ZDY C on A.GYS=C.ZDYNO');
      SQL.Add(' where CRTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
      SQL.Add(' and CRTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.DateTime+1))+'''');
      sql.Add(' and CKName='''+Trim(DParameters1)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmJWLInList.InitForm();
begin
  ReadCxGrid('入库列表',Tv1,'原材料仓库');

  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime:=EndDate.DateTime-7;
  InitGrid();
end;

procedure TfrmJWLInList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,CDS_Main);
  SInitCDSData20(ADOQueryMain,CDS_Main);
end;

procedure TfrmJWLInList.TBEditClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from CK_YCL_Sub where CRID='+Trim(ADOQueryMain.fieldbyname('CRID').AsString));
    sql.Add(' and MainId like ''MC%'' ');
    open;
    if ADOQueryTemp.RecordCount>0 then
    begin
      Application.MessageBox('此物品已经有出库记录，不能进行操作！','提示',0);
      Exit;
    end;
  end;

  try
    frmJWLIn:=TfrmJWLIn.Create(Application);
    with frmJWLIn do
    begin
      FMainId:=Trim(self.CDS_Main.fieldbyname('MainId').AsString);
      ToolButton1.Visible:=False;
      ToolButton2.Visible:=False;
      ToolButton3.Visible:=False;
      ToolButton4.Visible:=False;
      if ShowModal=1 then
      begin
        Self.InitGrid();
        Self.TBFind.Click;
        Self.CDS_Main.Locate('MainId',FMainID,[]);
      end;
    end;
  finally
    frmJWLIn.Free;
  end;
end;

procedure TfrmJWLInList.TBDelClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from CK_YCL_Sub where CRID='+Trim(ADOQueryMain.fieldbyname('CRID').AsString));
    sql.Add(' and MainId like ''MC%'' ');
    open;
    if ADOQueryTemp.RecordCount>0 then
    begin
      Application.MessageBox('此物品已经有出库记录，不能进行操作！','提示',0);
      Exit;
    end;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if DelData() then
  begin
    CDS_Main.Delete;
  end;
end;

function TfrmJWLInList.DelData():Boolean;
var
  FInt:Integer;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete CK_YCL_Main where MainId='''+Trim(CDS_Main.fieldbyname('MainId').AsString)+'''');
      sql.Add(' delete CK_YCL_Sub where MainId='''+Trim(CDS_Main.fieldbyname('MainId').AsString)+'''');
      sql.Add(' delete CK_YCL_KC where CRID='+Trim(CDS_Main.fieldbyname('CRID').AsString));
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmJWLInList.TBExportClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  TcxGridToExcel('入库列表',cxGrid1);
end;

procedure TfrmJWLInList.TBPrintClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
end;

procedure TfrmJWLInList.TBAddClick(Sender: TObject);
begin
  try
    frmJWLIn:=TfrmJWLIn.Create(Application);
    with frmJWLIn do
    begin
      if ShowModal=1 then
      begin
        Self.InitGrid();
        Self.TBFind.Click;
        Self.CDS_Main.Locate('MainId',FMainID,[]);
      end;
    end;
  finally
    frmJWLIn.Free;
  end;
end;

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

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

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

end.
