unit U_YFCRLZ;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin,
  StdCtrls, cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP,ShellAPI,IniFiles, cxCheckBox, cxCalendar,
  cxButtonEdit, cxTextEdit, cxDropDownEdit;

type
  TfrmYFCRLZ = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    Label1: TLabel;
    YFName: TEdit;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    FactoryName: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    VDate: TcxGridDBColumn;
    VYFName: TcxGridDBColumn;
    VQty: TcxGridDBColumn;
    VQtyUnit: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    ToolButton3: TToolButton;
    VPrice: TcxGridDBColumn;
    VYB: TcxGridDBColumn;
    VFTYName: TcxGridDBColumn;
    VOrderNo: TcxGridDBColumn;
    VNote: TcxGridDBColumn;
    Label4: TLabel;
    OrderNo: TEdit;
    ToolButton5: TToolButton;
    VPS: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2SSel: TcxGridDBColumn;
    Label5: TLabel;
    CPName: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure FactoryNameChange(Sender: TObject);
    procedure cxGridDBColumn5PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure cxGridDBColumn3PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure cxGridDBColumn6PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton5Click(Sender: TObject);
    procedure Tv2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure VHZFTYNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure PriceChange(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
  public
    { Public declarations }

  end;

var
  frmYFCRLZ: TfrmYFCRLZ;

implementation
uses
  U_DataLink,U_RTFun,U_ZDYHelp,U_GYSSelList;

{$R *.dfm}



procedure TfrmYFCRLZ.InitGrid();
begin
  begdate.SetFocus;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select * from YF_Money_CR where CRTime>=:begdate and CRTime<:Enddate ');
      sql.Add(' and isnull(CRType,'''')=''应付款登记'' ');
      Parameters.ParamByName('begdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',begdate.DateTime));
      Parameters.ParamByName('Enddate').Value:=Trim(FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1));
      if Trim(FactoryName.Text)<>'' then
      begin
        SQL.Add(' and isnull(FactoryName,'''') like '''+'%'+Trim(FactoryName.Text)+'%'+'''');
      end;
      if Trim(YFName.Text)<>'' then
      begin
        SQL.Add(' and isnull(YFName,'''') like '''+'%'+Trim(YFName.Text)+'%'+'''');
      end;
      if Trim(OrderNo.Text)<>'' then
      begin
        SQL.Add(' and isnull(OrderNo,'''') like '''+'%'+Trim(OrderNo.Text)+'%'+'''');
      end;
      if Trim(CPName.Text)<>'' then
      begin
        SQL.Add(' and isnull(CPName,'''') like '''+'%'+Trim(CPName.Text)+'%'+'''');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmYFCRLZ.FormDestroy(Sender: TObject);
begin
  frmYFCRLZ:=nil;
end;

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

procedure TfrmYFCRLZ.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('应付款列表',Tv2,'财务管理');
  Close;
end;
procedure TfrmYFCRLZ.TBDelClick(Sender: TObject);
var
  FBBMoney,CRID:string;
begin
  if CDS_HZ.IsEmpty then Exit;
  if CDS_HZ.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_HZ.DisableControls;
    with CDS_HZ do
    begin
      First;
      while Locate('SSel',True,[]) do
      begin
        if Trim(CDS_HZ.fieldbyname('YFID').AsString)<>'' then
        begin
          with ADOQueryCmd do
          begin
            Close;
            SQL.Clear;
            sql.Add('delete YF_Money_CR where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
            sql.Add('UPdate YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
            sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
            sql.Add(' where CRID='+CDS_HZ.Fieldbyname('CRID').AsString);
            ExecSQL;
          end;
          CRID:=Trim(CDS_HZ.fieldbyname('CRID').AsString);
          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;
        CDS_HZ.Delete;
      end;
    end;
    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('删除成功!','提示',0);
  except
    CDS_HZ.EnableConstraints;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('删除异常!','提示',0);
  end;
end;

procedure TfrmYFCRLZ.FormShow(Sender: TObject);
var
  fsj:String;
begin
  ReadCxGrid('应付款列表',Tv2,'财务管理');
  Enddate.DateTime:=SGetServerDate(ADOQueryTemp);
  begdate.DateTime:=Enddate.DateTime-30;
end;

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

procedure TfrmYFCRLZ.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  end;
end;

procedure TfrmYFCRLZ.FactoryNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmYFCRLZ.cxGridDBColumn5PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FQty,FPrice,FMoney,FBBMoney,FFieldName:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  FFieldName:=Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value:=Trim(mvalue);
    //Post;
  end;
  FQty:=Trim(CDS_HZ.fieldbyname('Qty').AsString);
  if Trim(FQty)='' then FQty:='0';
  FPrice:=Trim(CDS_HZ.fieldbyname('Price').AsString);
  if Trim(FPrice)='' then FPrice:='0';
  FMoney:=FloatToStr(StrToFloat(FQty)*StrToFloat(FPrice));
  with CDS_HZ do
  begin
    Edit;
    FieldByName('Money').Value:=StrToFloat(FMoney);
    FieldByName('BBMoney').Value:=StrToFloat(FMoney);
    Post;
  end;
end;

procedure TfrmYFCRLZ.ToolButton3Click(Sender: TObject);
begin
  try
    frmGYSSelList:=TfrmGYSSelList.Create(Application);
    with frmGYSSelList do
    begin
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Append;
          FieldByName('FactoryNo').Value:=Trim(Order_Main.fieldbyname('ZKID').AsString);
          FieldByName('FactoryName').Value:=Trim(Order_Main.fieldbyname('KHName').AsString);
          FieldByName('CRTime').Value:=Now;
          Post;
        end;
      end;
    end;
  finally
    frmGYSSelList.Free;
  end;
end;

procedure TfrmYFCRLZ.cxGridDBColumn3PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='YFName';
      flagname:='项目及摘要';
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('YFCode').Value:=Trim(ClientDataSet1.fieldbyname('ZdyNo').AsString);
          FieldByName('YFName').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYFCRLZ.cxGridDBColumn6PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='OrderUnit';
      flagname:='数量单位';
      if ShowModal=1 then
      begin
        Self.CDS_HZ.Edit;
        Self.CDS_HZ.FieldByName('QtyUnit').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYFCRLZ.ToolButton1Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  TcxGridToExcel('入账列表',cxGrid2);

end;

procedure TfrmYFCRLZ.OrderNoChange(Sender: TObject);
begin
  if Length(Trim(OrderNo.Text))<3 then Exit;
  ToolButton2.Click;
end;

procedure TfrmYFCRLZ.OrderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(OrderNo.Text)<3 then Exit;
    begdate.SetFocus;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add(' select * from YF_Money_CR where OrderNo like :OrderNo ');
        sql.Add(' and isnull(CRType,'''')=''应付款登记'' ');
        Parameters.ParamByName('orderno').Value:='%'+Trim(OrderNo.Text)+'%';
        Open;
      end;
      SCreateCDS20(ADOQueryMain,CDS_HZ);
      SInitCDSData20(ADOQueryMain,CDS_HZ);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmYFCRLZ.ToolButton5Click(Sender: TObject);
var
  CRID,maxId,FYFID:String;
begin
  if CDS_HZ.IsEmpty then Exit;
  if CDS_HZ.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;
  ToolBar1.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_HZ.DisableControls;
    with CDS_HZ do
    begin
      First;
      while Locate('SSel',True,[])  do
      begin
        with ADOQueryTemp do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from YF_Money_CR where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
          Open;
        end;
        FYFID:=Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
        if Trim(FYFID)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxId,'YF','YF_Money_CR',4,1)=False then
          begin
            CDS_HZ.EnableControls;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取应付款最大号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxId:=Trim(FYFID);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from YF_Money_KC where FactoryNo='''+Trim(CDS_HZ.fieldbyname('FactoryNo').AsString)+'''');
          Open;
        end;
        if ADOQueryCmd.IsEmpty=False then
        begin
          CRID:=ADOQueryCmd.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('FactoryNo').Value:=Trim(CDS_HZ.fieldbyname('FactoryNo').AsString);
            FieldByName('FactoryName').Value:=Trim(CDS_HZ.fieldbyname('FactoryName').AsString);
            FieldByName('ZdyStr1').Value:='应付付';
            Post;
          end;
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from YF_Money_CR where YFID='''+Trim(maxId)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(FYFID)='' then
          begin
            Append;
            FieldByName('Filler').Value:=Trim(DName);
          end else
          begin
            Edit;
            FieldByName('Editer').Value:=Trim(DName);
          end;
          FieldByName('YFID').Value:=Trim(maxId);
          FieldByName('CRID').Value:=StrToInt(CRID);
          RTSetSaveDataCDS(ADOQueryCmd,Tv2,CDS_HZ,'YF_Money_CR',3);
          FieldByName('YFType').Value:='手工录入';
          FieldByName('CRType').Value:='应付款登记';
          FieldByName('CRFlag').Value:='应付付';
          FieldByName('QtyFlag').Value:=1;
          FieldByName('HuiLv').Value:=1;
          FieldByName('BZType').Value:='人民币';
          FieldByName('FactoryNo').Value:=Trim(CDS_HZ.fieldbyname('FactoryNo').AsString);
          FieldByName('BBMoney').Value:=CDS_HZ.fieldbyname('Money').Value;
          Post;
        end;
        with CDS_HZ do
        begin
          Edit;
          FieldByName('YFId').Value:=Trim(maxId);
          FieldByName('CRID').Value:=StrToInt(CRID);
          FieldByName('SSel').Value:=False;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
          sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
          sql.Add(' where CRID='+CDS_HZ.Fieldbyname('CRID').AsString);
          ExecSQL;
        end;
      end;
    end;
    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功!','提示',0);
  except
    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!','提示',0);
  end;
end;

procedure TfrmYFCRLZ.Tv2MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if CDS_HZ.IsEmpty then Exit;
  If Trim(CDS_HZ.fieldbyname('YFType').AsString)='自动生成' then
  begin
    TV2.OptionsSelection.CellSelect:=False;
  end else
  begin
    TV2.OptionsSelection.CellSelect:=True;
  end;
end;

procedure TfrmYFCRLZ.VHZFTYNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmGYSSelList:=TfrmGYSSelList.Create(Application);
    with frmGYSSelList do
    begin
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('HZFactoryNo').Value:=Trim(Order_Main.fieldbyname('ZKID').AsString);
          FieldByName('HZFactoryName').Value:=Trim(Order_Main.fieldbyname('KHNameJC').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmGYSSelList.Free;
  end;
end;

procedure TfrmYFCRLZ.PriceChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  end;
end;

end.
