unit U_BaseInList;

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;

type
  TfrmBaseInList = 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;
    TBView: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label7: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    PlanNo: TEdit;
    P_ChnName20: TEdit;
    ShorName: TEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    v1ShortName: TcxGridDBColumn;
    v1Quantity: TcxGridDBColumn;
    v1UnitName: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1batchnum: TcxGridDBColumn;
    v1DepotClass: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    ToolButton1: TToolButton;
    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 TBViewClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure PlanNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
  private
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmBaseInList: TfrmBaseInList;

implementation
uses
  U_DataLink,U_Fun,U_BaseIn;

{$R *.dfm}

procedure TfrmBaseInList.FormDestroy(Sender: TObject);
begin
  frmBaseInList:=nil;
end;

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

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

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

procedure TfrmBaseInList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('exec P_Get_ClothInOutList :begdate,:enddate,:DepotNo,:DepotCode,:PState,:CRK');
      Parameters.ParamByName('DepotCode').Value:=Trim(gDef1);
      Parameters.ParamByName('begdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime));
      Parameters.ParamByName('enddate').Value:=Trim(FormatDateTime('yyyy-MM-dd',enddate.DateTime+1));
      Parameters.ParamByName('PState').Value:=1;
      Parameters.ParamByName('CRK').Value:=1;
      Open;
    end;
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmBaseInList.InitForm();
begin
  ReadCxGrid('入库列表',Tv1,'贝斯仓库');
  BegDate.DateTime:=GetServerTime10(ADOQueryTemp);//SGetServerDate10(ADOQueryTemp);
  EndDate.DateTime:=GetServerTime10(ADOQueryTemp);
  InitGrid();
end;

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

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

  try
    frmBaseIn:=TfrmBaseIn.Create(Application);
    with frmBaseIn do
    begin
      DepotNo:=Trim(self.ADOQueryMain.fieldbyname('DepotNo').AsString);
      ToolButton1.Visible:=False;
      ToolButton2.Visible:=False;
      ToolButton3.Visible:=False;
      ToolButton4.Visible:=False;
      if ShowModal=1 then
      begin
        Self.InitGrid();
        Self.TBFind.Click;
        Self.ADOQueryMain.Locate('DepotNo',DepotNo,[]);
      end;
    end;
  finally
    frmBaseIn.Free;
  end;
end;

procedure TfrmBaseInList.TBDelClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from WC_ClothInout_Info where KCID='+Trim(ADOQueryMain.fieldbyname('KCID').AsString));
    sql.Add(' and DepotNO like ''BC%'' ');
    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
    TBRafresh.Click;
    TBFind.Click;
  end;
end;

function TfrmBaseInList.DelData():Boolean;
var
  FInt:Integer;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from WC_ClothInout_Info where DefFlt2>0 ');
      SQL.Add(' and DepotNo<>'''+Trim(ADOQueryMain.fieldbyname('DepotNo').AsString)+'''');
      if Trim(ADOQueryMain.fieldbyname('PlanNo').AsString)<>'' then
        SQL.Add(' and PlanNo='''+ADOQueryMain.fieldbyname('PlanNo').AsString+'''');
      if Trim(ADOQueryMain.fieldbyname('P_Code').AsString)<>'' then
        SQL.Add(' and P_Code='''+ADOQueryMain.fieldbyname('P_Code').AsString+'''');
      if Trim(ADOQueryMain.fieldbyname('DefStr1').AsString)<>'' then
        SQL.Add(' and DefStr1='''+ADOQueryMain.fieldbyname('DefStr1').AsString+'''');
      if Trim(ADOQueryMain.fieldbyname('DefStr2').AsString)<>'' then
        SQL.Add(' and DefStr2='''+ADOQueryMain.fieldbyname('DefStr2').AsString+'''');
      if Trim(ADOQueryMain.fieldbyname('DefStr3').AsString)<>'' then
        SQL.Add(' and DefStr3='''+ADOQueryMain.fieldbyname('DefStr3').AsString+'''');
      if Trim(ADOQueryMain.fieldbyname('DefStr4').AsString)<>'' then
        SQL.Add(' and DefStr4='''+ADOQueryMain.fieldbyname('DefStr4').AsString+'''');
      if Trim(ADOQueryMain.fieldbyname('Defflt1').AsString)<>'' then
        SQL.Add(' and Defflt1='+ADOQueryMain.fieldbyname('Defflt1').AsString);
      Open;
    end;
    with ADOQueryTemp do
    begin
      if not IsEmpty then
      begin
        First;
        FInt:=1;
        while not Eof do
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update WC_ClothInout_Info Set DefFlt2='+inttostr(FInt));
            sql.Add(',DefFlt3='+inttostr((FInt-1) div 10));
            sql.Add(' where DepotNo='''+Trim(ADOQueryTemp.fieldbyname('DepotNo').AsString)+'''');
            ExecSQL;
          end;
          FInt:=FInt+1;
          Next;
        end;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete WC_ClothInout_Info where depotno='''+Trim(ADOQueryMain.fieldbyname('depotno').AsString)+'''');
      sql.Add(' delete WC_ClothInout where depotno='''+Trim(ADOQueryMain.fieldbyname('depotno').AsString)+'''');
      sql.Add(' delete WC_ClothClothing where KCID='+Trim(ADOQueryMain.fieldbyname('KCID').AsString));
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmBaseInList.TBViewClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
end;

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

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

procedure TfrmBaseInList.TBAddClick(Sender: TObject);
begin
  try
    frmBaseIn:=TfrmBaseIn.Create(Application);
    with frmBaseIn do
    begin
      if ShowModal=1 then
      begin
        Self.InitGrid();
        Self.TBFind.Click;
        Self.ADOQueryMain.Locate('DepotNo',DepotNo,[]);
      end;
    end;
  finally
    frmBaseIn.Free;
  end;
end;

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

procedure TfrmBaseInList.PlanNoChange(Sender: TObject);
begin
  if ADOQueryMain.Active then 
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
end;

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

procedure TfrmBaseInList.ToolButton1Click(Sender: TObject);
begin
 // if ADOQueryMain.IsEmpty then Exit;
  SeniorFilter(Tv1,ADOQueryMain);
end;

end.
