unit U_BPCGDZList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridCustomTableView,
  cxGridTableView, cxGridBandedTableView, cxGridDBBandedTableView,
  cxGridLevel, cxClasses, cxControls, cxGridCustomView, cxGridDBTableView,
  cxGrid, StdCtrls, ComCtrls, ExtCtrls, ToolWin, cxGridCustomPopupMenu,
  cxGridPopupMenu, ADODB, DBClient, cxDropDownEdit, cxCheckBox, RM_Common,
  RM_Class, RM_e_Xls, RM_Dataset, RM_System, RM_GridReport, Menus,
  cxCalendar, cxButtonEdit, cxTextEdit, cxPC;

type
  TfrmBPCGDZList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    TBRKCX: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    Label3: TLabel;
    Label4: TLabel;
    MPRTCodeName: TEdit;
    FactoryName: TEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column2: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    cxTabControl1: TcxTabControl;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    DZType: TComboBox;
    Label5: TLabel;
    Label6: TLabel;
    OrderNo: TEdit;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBRKCXClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure FactoryNameChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
    procedure InitGrid();
    function  YFHZData(FDZID:string):Boolean;
    function DelYFHZData():Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmBPCGDZList: TfrmBPCGDZList;

implementation
uses
  U_DataLink,U_RTFun,U_BPCGDZInPut;

{$R *.dfm}

procedure TfrmBPCGDZList.FormDestroy(Sender: TObject);
begin
  frmBPCGDZList:=nil;
end;

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

procedure TfrmBPCGDZList.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime:=EndDate.DateTime-15;
end;

procedure TfrmBPCGDZList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select A.* ');
      sql.Add(' from JYOrder_Main_MD_DuiZhang A');
      sql.Add(' where isnull(DZType,'''')in(''坯布'',''成品'')');
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' and isnull(Status,'''')=''待对账'' ');
      end else
      begin
        sql.Add(' and isnull(Status,'''')=''已对账'' ');
      end;
      if cxTabControl1.TabIndex<>0 then
      begin
        sql.add(' and A.DZDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
        sql.Add(' and A.DZDate<='''+Trim(FormatDateTime('yyyy-MM-dd',enddate.DateTime+1))+'''');
      end;
      if Trim(FactoryName.Text)<>'' then
      begin
        SQL.Add(' and isnull(A.FactoryName,'''') like '''+'%'+Trim(FactoryName.Text)+'%'+'''');
      end;
      if Trim(MPRTCodeName.Text)<>'' then
      begin
        SQL.Add(' and isnull(A.MPRTCodeName,'''') like '''+'%'+Trim(MPRTCodeName.Text)+'%'+'''');
      end;
      if Trim(DZType.Text)<>'' then
      begin
        SQL.Add(' and isnull(A.DZType,'''')='''+Trim(DZType.Text)+'''');
      end;
      if Trim(OrderNo.Text)<>'' then
      begin
        SQL.Add(' and isnull(A.OrderNo,'''') like '''+'%'+Trim(OrderNo.Text)+'%'+'''');
      end;
      Open;
      //ShowMessage(SQL.Text);
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmBPCGDZList.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;
  InitGrid();
end;

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

procedure TfrmBPCGDZList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('布匹采购对账列表',Tv1,'财务对账');
  Close;
end;

procedure TfrmBPCGDZList.FormShow(Sender: TObject);
begin
  ReadCxGrid('布匹采购对账列表',Tv1,'财务对账');
  //InitGrid();
end;

procedure TfrmBPCGDZList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then exit;
  TcxGridToExcel('布匹采购对账列表',cxGrid1);
end;

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

procedure TfrmBPCGDZList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,True);
end;

procedure TfrmBPCGDZList.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,False);
end;

procedure TfrmBPCGDZList.TBRKCXClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then exit;
  if cxTabControl1.TabIndex<>0 then Exit;
  if CDS_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete from JYOrder_Main_MD_DuiZhang where DZID='''+Trim(CDS_Main.fieldbyname('DZID').AsString)+'''');
          ExecSQL;
        end;
        CDS_Main.Delete;
      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    CDS_Main.EnableControls;
    Application.MessageBox('删除异常!','提示',0);
  end;
end;

procedure TfrmBPCGDZList.ToolButton1Click(Sender: TObject);
begin
  try
    frmBPCGDZInPut:=TfrmBPCGDZInPut.Create(Application);
    with frmBPCGDZInPut do
    begin
      FBCId:='';
      if ShowModal=1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmBPCGDZInPut.Free;
  end;
end;

procedure TfrmBPCGDZList.ToolButton2Click(Sender: TObject);
begin
  if cxTabControl1.TabIndex<>0 then Exit;
  try
    frmBPCGDZInPut:=TfrmBPCGDZInPut.Create(Application);
    with frmBPCGDZInPut do
    begin
      FBCId:=Trim(CDS_Main.fieldbyname('DZID').AsString);
      TBDel.Visible:=False;
      TBAdd.Visible:=False;
      if ShowModal=1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmBPCGDZInPut.Free;
  end;
end;

procedure TfrmBPCGDZList.FactoryNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

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

procedure TfrmBPCGDZList.ToolButton3Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then exit;
  if cxTabControl1.TabIndex<>0 then Exit;
  if CDS_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要执行对账操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JYOrder_Main_MD_DuiZhang where DZID='''+Trim(CDS_Main.fieldbyname('DZID').AsString)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Edit;
          FieldByName('DuiZhangPerson').Value:=Trim(DName);
          FieldByName('DuiZhangTime').Value:=SGetServerDateTime(ADOQueryTemp);
          FieldByName('Status').Value:='已对账';
          Post;
        end;
        YFHZData(Trim(CDS_Main.fieldbyname('DZID').AsString));
        CDS_Main.Delete;
      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    CDS_Main.EnableControls;
    Application.MessageBox('对账异常!','提示',0);
  end;
end;
function TfrmBPCGDZList.YFHZData(FDZID:string):Boolean;
var
  CRID,OrdMainId,FYFID,MaxNo,FComTaiTou,FCRID,FFactoryName,CWFactoryNo,CWFactoryName:String;
begin
  Result:=False;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main_MD_DuiZhang where DZID='''+Trim(FDZID)+'''');
    Open;
  end;
  CWFactoryNo:=Trim(ADOQueryTemp.fieldbyname('FactoryNo').AsString);
  CWFactoryName:=Trim(ADOQueryTemp.fieldbyname('FactoryName').AsString);
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YF_Money_KC where FactoryNo='''+Trim(CWFactoryNo)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    CRID:=ADOQueryTemp.fieldbyname('CRID').AsString;
  end else
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YF_Money_CRID set CRID=CRID+1');
      sql.Add('select * from YF_Money_CRID ');
      Open;
    end;
    CRID:=ADOQueryCmd.fieldbyname('CRID').AsString;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YF_Money_KC where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('CRID').Value:=StrToInt(CRID);
      FieldByName('FactoryName').Value:=Trim(CWFactoryName);
      FieldByName('FactoryNo').Value:=Trim(CWFactoryNo);
      FieldByName('ZdyStr1').Value:='应付付';
      Post;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where FactoryNo='''+Trim(CWFactoryNo)+'''');
    sql.Add(' and MHID='''+Trim(CDS_Main.fieldbyname('DZID').AsString)+'''');
    Open;
  end;
  FYFID:=Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
  if Trim(FYFID)='' then
  begin
    if GetLSNo(ADOQueryCmd,MaxNo,'PC','YF_Money_CR',3,1)=False then
    begin
      Application.MessageBox('取纱线应付最大号失败!','提示',0);
      Exit;
    end;
  end else
  begin
    MaxNo:=Trim(FYFID);
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where YFID='''+Trim(MaxNo)+'''');
    Open;
  end;
  with ADOQueryCmd do
  begin
    if Trim(FYFID)='' then
      Append
    else
      Edit;
    FieldByName('YFID').Value:=Trim(MaxNo);
    FieldByName('CRID').Value:=StrToInt(CRID);
    FieldByName('Filler').Value:=Trim(DName);
    FieldByName('CRType').Value:='应付款登记';
    FieldByName('CRFlag').Value:='应付付';
    FieldByName('QtyFlag').Value:=1;
    FieldByName('FactoryNo').Value:=Trim(CWFactoryNo);
    FieldByName('FactoryName').Value:=Trim(CWFactoryName);
    FieldByName('HZFactoryNo').Value:=Trim(CWFactoryNo);
    FieldByName('HZFactoryName').Value:=Trim(CWFactoryName);
    FieldByName('YFType').Value:='自动生成';
    FieldByName('HuiLv').Value:=1;
    FieldByName('BZType').Value:='人民币';

    //FieldByName('PSName').Value:='纱线';
    FieldByName('MHID').Value:=Trim(CDS_Main.fieldbyname('DZID').AsString);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR Set PS=(select PS from JYOrder_Main_MD_DuiZhang where DZID='''+Trim(FDZID)+''')');
    sql.Add(', Qty=(select Qty from JYOrder_Main_MD_DuiZhang where DZID='''+Trim(FDZID)+''')');
    sql.Add(', Price=(select Price from JYOrder_Main_MD_DuiZhang where DZID='''+Trim(FDZID)+''')');
    sql.Add(',Money=(select Money from JYOrder_Main_MD_DuiZhang where DZID='''+Trim(FDZID)+''')');
    sql.Add(',BBMoney=(select Money from JYOrder_Main_MD_DuiZhang where DZID='''+Trim(FDZID)+''')');
    sql.Add(',CPName=(select MPRTCodeName from JYOrder_Main_MD_DuiZhang where DZID='''+Trim(FDZID)+''')');
    sql.Add(',YFName=(select DZType from JYOrder_Main_MD_DuiZhang where DZID='''+Trim(FDZID)+''')');
    sql.Add(',QtyUnit=(select QtyUnit from JYOrder_Main_MD_DuiZhang where DZID='''+Trim(FDZID)+''')');
    sql.Add(',CRTime=(select DZDate from JYOrder_Main_MD_DuiZhang where DZID='''+Trim(FDZID)+''')');
    sql.Add(',OrderNo=(select OrderNo from JYOrder_Main_MD_DuiZhang where DZID='''+Trim(FDZID)+''')');
    sql.Add(',Note=(select Note from JYOrder_Main_MD_DuiZhang where DZID='''+Trim(FDZID)+''')');
    sql.Add(' where YFID='''+Trim(MaxNo)+'''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Main_MD_DuiZhang Set YFID='''+Trim(MaxNo)+'''');
    sql.Add(' where DZID='''+Trim(CDS_Main.fieldbyname('DZID').AsString)+'''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_KC Set ');
    sql.Add(' KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID and A.Valid=''Y'')');
    sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID and A.Valid=''Y'' )');
    sql.Add(' where CRID='+CRID);
    ExecSQL;
  end;

  Result:=True;
end;
function TfrmBPCGDZList.DelYFHZData():Boolean;
var
  CRID:String;
begin
  Result:=False;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where MHID='''+Trim(CDS_Main.fieldbyname('DZID').AsString)+'''');
    Open;
  end;
  CRID:=Trim(ADOQueryTemp.fieldbyname('CRID').AsString);
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add(' delete YF_Money_CR where  ');
    sql.Add(' MHID='''+Trim(CDS_Main.fieldbyname('DZID').AsString)+'''');
    sql.Add(' Update JYOrder_Main_MD_DuiZhang Set YFID=Null where  ');
    sql.Add(' DZID='''+Trim(CDS_Main.fieldbyname('DZID').AsString)+'''');
    ExecSQL;
  end;
  if Trim(CRID)<>'' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YF_Money_KC Set ');
      sql.Add(' KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID and A.Valid=''Y'')');
      sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID and A.Valid=''Y'' )');
      sql.Add(' where CRID='+CRID);
      ExecSQL;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YF_Money_CR where CRID='+CRID);
      Open;
    end;
    if ADOQueryTemp.IsEmpty then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' delete YF_Money_KC where CRID='+CRID);
        ExecSQL;
      end;
    end;
  end;

  Result:=True;
end;
procedure TfrmBPCGDZList.ToolButton4Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then exit;
  if cxTabControl1.TabIndex<>1 then Exit;
  if CDS_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要执行撤销操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JYOrder_Main_MD_DuiZhang where DZID='''+Trim(CDS_Main.fieldbyname('DZID').AsString)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Edit;
          FieldByName('DuiZhangPerson').Value:=Null;
          FieldByName('DuiZhangTime').Value:=Null;
          FieldByName('Status').Value:='待对账';
          Post;
        end;
        DelYFHZData();
        CDS_Main.Delete;
      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    CDS_Main.EnableControls;
    Application.MessageBox('撤销异常!','提示',0);
  end;
end;

end.
