unit U_BPDHList;

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, cxCalendar, cxButtonEdit, cxSplitter,
  RM_Common, RM_Class, RM_e_Xls, RM_Dataset, RM_System, RM_GridReport,
  cxTextEdit, cxDropDownEdit, BtnEdit;

type
  TfrmBPDHList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    Label2: TLabel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    BPCode: TEdit;
    Label5: TLabel;
    BPName: TEdit;
    Order_Main: TClientDataSet;
    Label4: TLabel;
    BPSpec: TEdit;
    DataSource2: TDataSource;
    ClientDataSet2: TClientDataSet;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2ComeDate: TcxGridDBColumn;
    v2MxQty: TcxGridDBColumn;
    v2MxNote: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    ToolBar2: TToolBar;
    ToolButton8: TToolButton;
    ToolButton9: TToolButton;
    ToolButton10: TToolButton;
    cxGridPopupMenu2: TcxGridPopupMenu;
    v2Column1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    v1OrderNo: TcxGridDBColumn;
    v1PSName: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1GDPerson: TcxGridDBColumn;
    v1FactoryName: TcxGridDBColumn;
    v1ToFactoryName: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    CheckBox1: TCheckBox;
    v1Column6: TcxGridDBColumn;
    Panel3: TPanel;
    Label6: TLabel;
    TaiTou: TBtnEditA;
    v1Column7: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Label7: TLabel;
    TaiTouCX: TEdit;
    Label8: TLabel;
    FactoryName: TEdit;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    Label9: TLabel;
    JHBatchNo: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure BPCodeChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure BPCodeKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton10Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure ToolButton9Click(Sender: TObject);
    procedure TaiTouBtnClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
  private
    FInt,PFInt:Integer;
    procedure InitGrid();
    procedure InitForm();
    procedure InitSubGrid();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmBPDHList: TfrmBPDHList;

implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun;

{$R *.dfm}

procedure TfrmBPDHList.FormDestroy(Sender: TObject);
begin
  frmBPDHList:=nil;
end;

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

procedure TfrmBPDHList.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('到货汇总GY',Tv1,'跟单管理');
  WriteCxGrid('到货明细GY',Tv2,'跟单管理');
end;

procedure TfrmBPDHList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select A.*,TaiTouCX=A.TaiTou from XH_Cloth_JH A where BegFlag=1 and EndFlag=0 ');
      if CheckBox1.Checked=True then
      begin
        SQL.Add(' and JHDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
        SQL.Add(' and JHDate<'''+Trim(FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1))+'''');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmBPDHList.InitForm();
begin
  ReadCxGrid('到货汇总GY',Tv1,'跟单管理');
  ReadCxGrid('到货明细GY',Tv2,'跟单管理');
  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp)-7;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  InitGrid();
end;

procedure TfrmBPDHList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  TaiTou.Text:=Trim(Order_Main.fieldbyname('TaiTou').AsString);
  InitSubGrid();
end;

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

procedure TfrmBPDHList.BPCodeChange(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  TaiTou.Text:=Trim(Order_Main.fieldbyname('TaiTou').AsString);
  InitSubGrid();
end;

procedure TfrmBPDHList.FormShow(Sender: TObject);
begin
  InitForm();
end;
procedure TfrmBPDHList.BPCodeKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin

    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add('select * from XH_Cloth_JH where BegFlag=1 and EndFlag=0 ');
        if CheckBox1.Checked=True then
        begin
          SQL.Add(' and JHDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
          SQL.Add(' and JHDate>='''+Trim(FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1))+'''');
        end;
        sql.Add(' and BPCode like '''+'%'+Trim(BPCode.Text)+'%'+''' ' );
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
    TaiTou.Text:=Trim(Order_Main.fieldbyname('TaiTou').AsString);
    InitSubGrid();
  end;

end;
procedure TfrmBPDHList.InitSubGrid();
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.*');
    sql.Add('from XH_Cloth_DH A');
    sql.Add(' where A.JHId='''+Trim(Order_Main.fieldbyname('JHId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet2);
  SInitCDSData20(ADOQueryTemp,ClientDataSet2);
end;
procedure TfrmBPDHList.ToolButton10Click(Sender: TObject);
var
  maxno,MXMaxno,FMXID:string;
begin
  if ClientDataSet2.IsEmpty then Exit;
  if Trim(TaiTou.Text)='' then
  begin
    Application.MessageBox('抬头不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('DHDate',null,[]) then
  begin
    Application.MessageBox('到货日期不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('DHPS',null,[]) then
  begin
    Application.MessageBox('到货匹数不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('DHQty',null,[]) then
  begin
    Application.MessageBox('到货数量不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('DHType',null,[]) then
  begin
    Application.MessageBox('类型不能为空！','提示',0);
    Exit;
  end;
  BegDate.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
    //保存明细表
    ClientDataSet2.DisableControls;
    with ClientDataSet2 do
    begin
      First;
      while not eof do
      begin
        with ADOQueryTemp do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from XH_Cloth_DH where DHID='''+Trim(ClientDataSet2.fieldbyname('DHID').AsString)+'''');
          Open;
        end;
        FMXID:=Trim(ADOQueryTemp.fieldbyname('DHID').AsString);
        if Trim(FMXID)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxno,'DH','XH_Cloth_DH',4,1)=False then
          begin
            ClientDataSet2.EnableControls;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取明细流水号失败！','提示',0);
            Exit;
          end;
        end  else
        begin
          maxno:=Trim(FMXID);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from XH_Cloth_DH where DHID='''+Trim(FMXID)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(FMXID)='' then
          begin
            Append;
            FieldByName('Filler').Value:=Trim(DName);
            FieldByName('FillerCode').Value:=Trim(DCode);
          end
          else
          begin
            Edit;
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditerCode').Value:=Trim(DCode);
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
          end;
          FieldByName('DHID').Value:=Trim(maxno);
          FieldByName('JHId').Value:=Trim(Order_Main.fieldbyname('JHId').AsString);
          RTSetSaveDataCDS(ADOQueryCmd,Tv2,ClientDataSet2,'XH_Cloth_DH',2);
          FieldByName('FromFactoryNo').Value:=Trim(Order_Main.fieldbyname('FactoryNo').AsString);
          FieldByName('FromFactoryName').Value:=Trim(Order_Main.fieldbyname('FactoryName').AsString);
          FieldByName('DQFactoryNo').Value:=Trim(Order_Main.fieldbyname('ToFactoryNo').AsString);
          FieldByName('DQFactoryName').Value:=Trim(Order_Main.fieldbyname('ToFactoryName').AsString);
          FieldByName('GDCode').Value:=Trim(ClientDataSet2.fieldbyname('GDCode').AsString);
          FieldByName('GDName').Value:=Trim(ClientDataSet2.fieldbyname('GDName').AsString);
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('exec P_Update_JHDH :JHID');
          Parameters.ParamByName('JHID').Value:=Trim(Order_Main.fieldbyname('JHID').AsString);
          ExecSQL;
        end;
        with ClientDataSet2 do
        begin
          Edit;
          FieldByName('DHID').Value:=Trim(maxno);
          Post;
        end;
        Next;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update XH_Cloth_JH Set TaiTou='''+Trim(TaiTou.Text)+'''');
      sql.Add(' where JHID='''+Trim(Order_Main.fieldbyname('JHID').AsString)+'''');
      ExecSQL;
    end;
    with Order_Main do
    begin
      Edit;
      FieldByName('TaiTou').Value:=Trim(TaiTou.Text);
      Post;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    ClientDataSet2.EnableControls;
    Application.MessageBox('操作成功！','提示',0);
  except
    ClientDataSet2.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常！','提示',0);
  end;
end;

procedure TfrmBPDHList.ToolButton8Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  ClientDataSet2.Append;
  ClientDataSet2.FieldByName('DHUnit').Value:=Trim(Order_Main.fieldbyname('JHUnit').AsString);
  ClientDataSet2.FieldByName('DHDate').Value:=SGetServerDate(ADOQueryTemp);
  ClientDataSet2.FieldByName('DHType').Value:='收货';
  ClientDataSet2.Post;
  if ClientDataSet2.RecordCount=1 then
  begin
    with ClientDataSet2 do
    begin
      Edit;
      FieldByName('DHPS').Value:=Order_Main.fieldbyname('JHPS').Value;
      FieldByName('DHQty').Value:=Order_Main.fieldbyname('JHQty').Value;
      Post;
    end;
  end;
end;

procedure TfrmBPDHList.ToolButton9Click(Sender: TObject);
begin
  if ClientDataSet2.IsEmpty then Exit;
  if Trim(ClientDataSet2.fieldbyname('DHID').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete XH_Cloth_DH where DHId='''+Trim(ClientDataSet2.fieldbyname('DHId').AsString)+'''');
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('exec P_Update_JHDH :JHID');
        Parameters.ParamByName('JHID').Value:=Trim(Order_Main.fieldbyname('JHID').AsString);
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('exec P_Update_JHTP :JHID');
        Parameters.ParamByName('JHID').Value:=Trim(Order_Main.fieldbyname('JHID').AsString);
        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('删除异常!','提示',0);
    end;
  end;
  ClientDataSet2.Delete;
end;

procedure TfrmBPDHList.TaiTouBtnClick(Sender: TObject);
begin
  try
     frmZDYHelp:=TfrmZDYHelp.Create(Application);
     with frmZDYHelp do
     begin
       flag:='GDGSTT';
       flagname:='抬头';
       if ShowModal=1 then
       begin
         TaiTou.Text:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
       end;
     end;
   finally
     frmZDYHelp.Free;
   end;
end;

procedure TfrmBPDHList.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  TcxGridToExcel(Self.Caption,cxGrid1);
end;

procedure TfrmBPDHList.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  TaiTou.Text:=Trim(Order_Main.fieldbyname('TaiTou').AsString);
  InitSubGrid();

end;

end.
