unit U_PKInPut;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls,
  cxGridCustomView, cxGrid, cxMemo, cxRichEdit, ComCtrls, cxContainer,
  cxTextEdit, cxMaskEdit, cxButtonEdit, StdCtrls, ToolWin, DBClient, ADODB,
  ExtCtrls, BtnEdit, cxDropDownEdit, cxCalendar, cxGridCustomPopupMenu,
  cxGridPopupMenu, cxSplitter;

type
  TfrmPKInPut = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    DataSource2: TDataSource;
    ADOZDY: TADOQuery;
    CDS_ZDY: TClientDataSet;
    ADOQuery1: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ClientDataSet1: TClientDataSet;
    Panel1: TPanel;
    Label1: TLabel;
    PKDate: TDateTimePicker;
    ToolButton1: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column1: TcxGridDBColumn;
    ClientDataSet2: TClientDataSet;
    DataSource3: TDataSource;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    VOrderNo: TcxGridDBColumn;
    vPSName: TcxGridDBColumn;
    VDate: TcxGridDBColumn;
    VFTYName: TcxGridDBColumn;
    VYFName: TcxGridDBColumn;
    VPayMent: TcxGridDBColumn;
    VPS: TcxGridDBColumn;
    VQty: TcxGridDBColumn;
    VQtyUnit: TcxGridDBColumn;
    VPrice: TcxGridDBColumn;
    VYB: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    VNote: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxSplitter1: TcxSplitter;
    Panel2: TPanel;
    RadioGroup1: TRadioGroup;
    cxGridPopupMenu2: TcxGridPopupMenu;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    ToolButton2: TToolButton;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure RadioGroup1Click(Sender: TObject);
    procedure v1Column11PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column7PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton2Click(Sender: TObject);
    procedure v1Column13PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    FXS:Integer;
    procedure InitData();
    procedure ZDYHelp(FButn:TcxButtonEdit;LType:string);
    function  SaveData():Boolean;
    procedure GetMaxConNO();
    { Private declarations }
  public
    PState,PCopyInt:Integer;
    FMainId,FConNo,FConType,FYLType:String;

    { Public declarations }
  end;

var
  frmPKInPut: TfrmPKInPut;

implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_GYSList,U_ZDYHelpSel, U_ModuleNote;

{$R *.dfm}

procedure TfrmPKInPut.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('排款录入',Tv1,'财务管理');
  WriteCxGrid('应付列表1',Tv2,'财务管理');
end;

procedure TfrmPKInPut.InitData();
var
  fsj:String;
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
     sql.Add(' select A.*,MDQingKuang=Cast('''' as varchar(50)) ');
     SQL.Add(' from YF_Money_PaiKuan A ');
    sql.Add(' where A.PKId='''+Trim(FMainId)+'''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_Sub);
  SInitCDSData20(ADOQuery1,Order_Sub);
  if Order_Sub.IsEmpty then
  begin
    PKDate.DateTime:=Now;
    fsj:=Trim(FormatDateTime('yyyy-MM-dd',PKDate.DateTime));
    PKDate.DateTime:=StrToDate(fsj);
  end else
  begin
    fsj:=Trim(FormatDateTime('yyyy-MM-dd',Order_Sub.fieldbyname('PKDate').AsDateTime));
    PKDate.Date:=StrToDate(fsj);
  end;

end;

procedure TfrmPKInPut.ZDYHelp(FButn:TcxButtonEdit;LType:string);
begin
end;

procedure TfrmPKInPut.FormShow(Sender: TObject);
begin
  ReadCxGrid('排款录入',Tv1,'财务管理');
  ReadCxGrid('应付列表1',Tv2,'财务管理');
  InitData();
end;
procedure TfrmPKInPut.GetMaxConNO();
begin

end;
function TfrmPKInPut.SaveData():Boolean;
var
  maxno:String;
begin
  try
    Result:=False;
    ADOCmd.Connection.BeginTrans;
    //保存主表
    Order_Sub.DisableControls;
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('PKMoney').AsString)<>'' then
        begin
          if Trim(Order_Sub.fieldbyname('PKID').AsString)='' then
          begin
            if GetLSNo(ADOCmd,maxno,'PK','YF_Money_PaiKuan',4,1)=False then
            begin
              Order_Sub.EnableControls;
              ADOCmd.Connection.RollbackTrans;
              Application.MessageBox('取规则流水号失败!','提示',0);
              Exit;
            end;
          end else
          begin
            maxno:=Trim(Order_Sub.fieldbyname('PKID').AsString);
          end;
          with ADOCmd do
          begin
            Close;
            sql.Clear;
            SQL.Add('select * from YF_Money_PaiKuan where PKID='''+Trim(Order_Sub.fieldbyname('PKID').AsString)+'''');
            Open;
          end;
          with ADOCmd do
          begin
            if Trim(Order_Sub.fieldbyname('PKID').AsString)='' then
            begin
              Append;
            end
            else begin
              Edit;
            end;
            FieldByName('PKID').Value:=Trim(maxno);
            RTSetSaveDataCDS(ADOCmd,Tv1,Order_Sub,'YF_Money_PaiKuan',0) ;
            FieldByName('PKDate').Value:=Trim(FormatDateTime('yyyy-MM-dd',PKDate.Date));
            FieldByName('FactoryNo').Value:=Order_Sub.fieldbyname('FactoryNo').Value;
            if PState=1 then
            begin
              FieldByName('Chker').Value:=Null;
              FieldByName('ChkStatus').Value:=Null;
              FieldByName('ChkNote').Value:=Null;
              FieldByName('ChkTime').Value:=Null;
            end;

            if Trim(FMainId)='' then
            begin
              FieldByName('Filler').Value:=Trim(DName);
            end else
            begin
              FieldByName('Editer').Value:=Trim(DName);
              FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
            end;
            Post;
          end;
          with ADOTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from YF_Money_PaiKuan where ');
            sql.Add(' FactoryName='''+Trim(Order_Sub.fieldbyname('FactoryName').AsString)+'''');
            sql.Add(' and isnull(ComTaiTou,'''')='''+Trim(Order_Sub.fieldbyname('ComTaiTou').AsString)+'''');
            sql.Add(' and isnull(FactoryNameOther,'''')='''+Trim(Order_Sub.fieldbyname('FactoryNameOther').AsString)+'''');
            sql.Add(' and isnull(PayMent,'''')='''+Trim(Order_Sub.fieldbyname('PayMent').AsString)+'''');
            sql.Add(' and  PKDate='''+Trim(Order_Sub.fieldbyname('PKDate').AsString)+'''');
            Open;
          end;
          if ADOTemp.RecordCount>1 then
          begin
            Result:=False;
            ADOCmd.Connection.RollbackTrans;
            Order_Sub.EnableControls;
            Application.MessageBox(PChar('供应商:'+
                                         Trim(Order_Sub.fieldbyname('FactoryName').AsString)+
                                         Trim(FormatDateTime('yyyy-MM-dd',PKDate.Date))+
                                         '日期的排款数据不能重复录入!'),'提示',0);
            Exit;
          end;
          Edit;
          FieldByName('PKID').Value:=Trim(maxno);
        end;

        Next;
      end;
    end;
    Order_Sub.EnableControls;
    ADOCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmPKInPut.TBSaveClick(Sender: TObject);
var
  FPPrice,PRTPrice,BCMoney,FPQty,ChaMoney:string;
begin
  ToolBar1.SetFocus;

  if Order_Sub.IsEmpty then
  begin
    Application.MessageBox('明细不能为空！','提示',0);
    exit;
  end;
  {if Order_Sub.Locate('PKMoney',null,[]) then
  begin
    Application.MessageBox('排款金额不能为空!','提示',0);
    Exit;
  end;}
  Order_Sub.DisableControls;
  with Order_Sub do
  begin
    First;
    while not Eof do
    begin
      if Trim(Order_Sub.fieldbyname('PKMoney').AsString)<>'' then
      begin
        {if Trim(Order_Sub.fieldbyname('ComTaiTou').AsString)='' then
        begin
          Order_Sub.EnableControls;
          Application.MessageBox('付款单位不能为空!','提示',0);
          Exit;
        end;}
        if Trim(Order_Sub.fieldbyname('PayMent').AsString)='' then
        begin
          Order_Sub.EnableControls;
          Application.MessageBox('支付方式不能为空!','提示',0);
          Exit;
        end;
        if Trim(Order_Sub.fieldbyname('PSName').AsString)='' then
        begin
          Order_Sub.EnableControls;
          Application.MessageBox('工序名称不能为空!','提示',0);
          Exit;
        end;
        if Trim(Order_Sub.fieldbyname('PKYongTu').AsString)='' then
        begin
          Order_Sub.EnableControls;
          Application.MessageBox('资金用途不能为空!','提示',0);
          Exit;
        end;

      end;
      Next;
    end;
  end;
  Order_Sub.EnableControls;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    ModalResult:=1;
  end;

end;

procedure TfrmPKInPut.ToolButton3Click(Sender: TObject);
var
  FKQDate:TDateTime;
  FKQWorkDays,FStr:string;
begin
  Fstr:='';
  try
    frmGYSList:=TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      if ShowModal=1 then
      begin
        {with ADOTemp do
        begin
          close;
          SQL.Clear;
          sql.Add('select * from YF_Money_KC where FactoryName='''+Order_Main.fieldbyname('KHNameJC').AsString+'''');
          Open;
        end;
        if ADOTemp.IsEmpty then
        begin
          Fstr:='99';
        end else
        if ADOTemp.FieldByName('KCMoney').Value>0 then
        begin
          FStr:='';
        end else
        begin
          FStr:='99';
        end;}
        with ADOTemp do
        begin
          Close;
          sql.Clear;
          SQL.Add(' select AAA.*,MaxHZYFMoney=isnull(HZYFMoney,0)+isnull(DDuiZhangMoney,0) ');
          sql.Add(' ,BQYFMoney=isnull(BQYFMoney10,0)+isnull(HZKKMoney,0)');
          sql.Add(',Case when GYS.LockFlag=1 and isnull(GYS.LockDays,0)+isnull(AAA.MinMHDate,Getdate())<=getdate() then ''有未处理的码单不能排款'' ');
          SQL.Add(' When isnull((select Top 1 PK.FactoryName from YF_Money_PaiKuan PK ');
          SQL.Add(' where isnull(PK.FKMoney,0)=0 and PK.FactoryName='''+Trim(Order_Main.fieldbyname('KHNameJC').AsString)+'''');
          sql.Add(' and isnull(PK.ChkStatus,'''')<>''审核不通过'' and PK.PKDate>''2014-11-10''), '''')<>''''  then ''有未付款的排款记录不能排款'' ');
          SQL.Add(' else '''' end as MDQingKuang');
          sql.Add(' from(');
          sql.Add(' select AA.* ');
          SQL.Add(',MinMHDate=(select min(MHDate) from JYOrder_Main_MD_HZ B ');
          sql.Add('           where B.FactoryName=AA.FactoryName and isnull(B.DuiZhangStatus,'''')='''' )');
          sql.Add(' ,HZYFMoney=(select Sum(Money-YiFuMoney) from YF_Money_CR B ');
          sql.Add('            where B.FactoryName=AA.FactoryName  and isnull(B.YiFuMoney,0)<>isnull(B.Money,0)');//and B.PSName=AA.PSName
          sql.Add('            and B.CRType=''应付款登记'' )');
          sql.Add(' ,BQKKMoney=(select Sum(Money-YiFuMoney) from YF_Money_CR B ');
          sql.Add('            where B.FactoryName=AA.FactoryName  and isnull(B.YiFuMoney,0)<>isnull(B.Money,0)');//and B.PSName=AA.PSName
          sql.Add('            and isnull(B.Money,0)<0 and B.CRTime<='''+Trim(FormatDateTime('yyyy-MM-dd',PKDate.DateTime))+'''');
          sql.Add('            and B.CRType=''应付款登记'' )');
          sql.Add(' ,HZKKMoney=(select Sum(Money-YiFuMoney) from YF_Money_CR B ');
          sql.Add('            where B.FactoryName=AA.FactoryName  and isnull(B.YiFuMoney,0)<>isnull(B.Money,0)');//and B.PSName=AA.PSName
          sql.Add('            and isnull(B.Money,0)<0 ');
          sql.Add('            and B.CRType=''应付款登记'' )');
          sql.Add(' ,DDuiZhangMoney=(select Sum(Money) from JYOrder_Main_MD_HZ JMMH where JMMH.FactoryName=AA.FactoryName ');//and JMMH.PSName=AA.PSName
          sql.Add('                 and isnull(JMMH.Status,'''')<>'''' and isnull(JMMH.DuiZhangStatus,'''')='''')');
          sql.Add(' from (');
          sql.Add(' select FactoryName,BQYFMoney10=sum(Money-YiFuMoney) '); //PSName,FactoryNo,
          sql.Add(' from YF_Money_CR A');
          sql.Add(' where CRTime<='''+Trim(FormatDateTime('yyyy-MM-dd',PKDate.DateTime))+'''');
          sql.Add(' and isnull(YiFuMoney,0)<>isnull(Money,0)');
          sql.Add(' and isnull(Money,0)>0');
          sql.Add(' and not exists(select * from YF_Money_PaiKuan PK where PK.FactoryName=A.FactoryName ');
          sql.Add('                and PK.PKDate='''+Trim(FormatDateTime('yyyy-MM-dd',PKDate.DateTime))+''')');
          sql.Add(' and FactoryName='''+Trim(Order_Main.fieldbyname('KHNameJC').AsString)+'''');
          sql.Add(' and A.CRType=''应付款登记'' ');
          sql.Add(' Group by FactoryName)AA)AAA');//,PSName  ,FactoryNo
          sql.Add(' inner join ZH_KH_Info GYS on Rtrim(GYS.KHNameJC)=RTrim(AAA.FactoryName)');
          //sql.Add(' where (GYS.LockFlag=0 or (GYS.LockFlag=1 and isnull(GYS.LockDays,0)+isnull(AAA.MinMHDate,Getdate())>getdate()  ))');
          Open;
        end;
        //if (ADOTemp.IsEmpty=False) or (FStr='99') then
        begin
          with Self.Order_Sub do
          begin
            Append;
           // FieldByName('FactoryNo').Value:=Trim(Order_Main.fieldbyname('ZKID').AsString);
            FieldByName('FactoryName').Value:=Trim(Order_Main.fieldbyname('KHNameJC').AsString);
            FieldByName('PKDate').Value:=PKDate.Date;
           // FieldByName('PSName').Value:=ADOTemp.fieldbyname('PSName').Value;
            FieldByName('MDQingKuang').Value:=ADOTemp.fieldbyname('MDQingKuang').Value;
            FieldByName('BQYFMoney').Value:=ADOTemp.fieldbyname('BQYFMoney').Value;
            FieldByName('HZYFMoney').Value:=ADOTemp.fieldbyname('HZYFMoney').Value;
            FieldByName('BQKKMoney').Value:=ADOTemp.fieldbyname('BQKKMoney').Value;
            FieldByName('HZKKMoney').Value:=ADOTemp.fieldbyname('HZKKMoney').Value;
            FieldByName('DDuiZhangMoney').Value:=ADOTemp.fieldbyname('DDuiZhangMoney').Value;
            FieldByName('MaxHZYFMoney').Value:=ADOTemp.fieldbyname('MaxHZYFMoney').Value;
            Post;
          end;
        end;

      end;
    end;
  finally
    frmGYSList.Free;
  end;

end;

procedure TfrmPKInPut.ToolButton4Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then Exit;
  if Trim(Order_Sub.fieldbyname('PKId').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete YF_Money_PaiKuan where PKId='''+Trim(Order_Sub.fieldbyname('PKId').AsString)+'''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;
end;

procedure TfrmPKInPut.ToolButton1Click(Sender: TObject);
var
  FReal:Double;
begin
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select AAA.*,MaxHZYFMoney=isnull(HZYFMoney,0)+isnull(DDuiZhangMoney,0) ');
    sql.Add(' ,BQYFMoney=isnull(BQYFMoney10,0)+isnull(HZKKMoney,0)');
    sql.Add(',Case when GYS.LockFlag=1 and isnull(GYS.LockDays,0)+isnull(AAA.MinMHDate,Getdate())<=getdate() then ''有未处理的码单不能排款'' ');
    SQL.Add(' When isnull((select Top 1 PK.FactoryName from YF_Money_PaiKuan PK ');
    SQL.Add(' where isnull(FKMoney,0)=0 and PK.FactoryName=AAA.FactoryName and PK.PKDate<='''+Trim(FormatDateTime('yyyy-MM-dd',PKDate.Date))+'''');
    sql.Add(' and isnull(PK.ChkStatus,'''')<>''审核不通过'' and PKDate>''2014-11-10''), '''')<>''''  then ''有未付款的排款记录不能排款'' ');
    sql.Add(' else '''' end as MDQingKuang');
    sql.Add(' from(');
    sql.Add(' select AA.* ');
    SQL.Add(',MinMHDate=isnull((select min(MHDate) from JYOrder_Main_MD_HZ B ');
    sql.Add('           where B.FactoryName=AA.FactoryName and isnull(B.DuiZhangStatus,'''')='''' ),getdate()+1)'); // and isnull(B.Status,'''')<>''''
    sql.Add(' ,HZYFMoney=(select Sum(Money-YiFuMoney) from YF_Money_CR B ');
    sql.Add('            where B.FactoryName=AA.FactoryName  and isnull(B.YiFuMoney,0)<>isnull(B.Money,0)');//and B.PSName=AA.PSName
    sql.Add('            and B.CRType=''应付款登记'' )');
   // sql.Add('            and not exists(select * from JYOrder_Main_MD_HZ C on )');
    sql.Add(' ,BQKKMoney=(select Sum(Money-YiFuMoney) from YF_Money_CR B ');
    sql.Add('            where B.FactoryName=AA.FactoryName  and isnull(B.YiFuMoney,0)<>isnull(B.Money,0)');//and B.PSName=AA.PSName
    sql.Add('            and isnull(B.Money,0)<0 and B.CRTime<='''+Trim(FormatDateTime('yyyy-MM-dd',PKDate.DateTime))+'''');
    sql.Add('            and B.CRType=''应付款登记'' )');
    sql.Add(' ,HZKKMoney=(select Sum(Money-YiFuMoney) from YF_Money_CR B ');
    sql.Add('            where B.FactoryName=AA.FactoryName  and isnull(B.YiFuMoney,0)<>isnull(B.Money,0)');//and B.PSName=AA.PSName
    sql.Add('            and isnull(B.Money,0)<0 ');
    sql.Add('            and B.CRType=''应付款登记'' )');
    sql.Add(' ,DDuiZhangMoney=(select Sum(Money) from JYOrder_Main_MD_HZ JMMH where JMMH.FactoryName=AA.FactoryName ');//and JMMH.PSName=AA.PSName
    sql.Add('                 and isnull(JMMH.Status,'''')<>'''' and isnull(JMMH.DuiZhangStatus,'''')='''')');
    sql.Add(' from (');
    sql.Add(' select FactoryName,BQYFMoney10=sum(Money-YiFuMoney) ');//PSName,FactoryNo,
    sql.Add(' from YF_Money_CR A');
    sql.Add(' where CRTime<='''+Trim(FormatDateTime('yyyy-MM-dd',PKDate.DateTime))+'''');
    sql.Add(' and isnull(YiFuMoney,0)<>isnull(Money,0)');
    sql.Add(' and isnull(Money,0)>0');
    sql.Add(' and A.CRType=''应付款登记'' ');
    sql.Add(' and not exists(select * from YF_Money_PaiKuan PK where PK.FactoryName=A.FactoryName ');
    sql.Add('                and PK.PKDate='''+Trim(FormatDateTime('yyyy-MM-dd',PKDate.DateTime))+''')');
    sql.Add(' Group by FactoryName)AA)AAA');//PSName ,FactoryNo
    sql.Add(' inner join ZH_KH_Info GYS on Rtrim(GYS.KHNameJC)=RTrim(AAA.FactoryName)');
    //sql.Add(' where (GYS.LockFlag=0 or (GYS.LockFlag=1 and isnull(GYS.LockDays,0)+isnull(AAA.MinMHDate,Getdate())>getdate()  ))');
    //sql.Add(' )AAA');
    //ShowMessage(sql.Text);
    Open;
  end;
  with ADOTemp do
  begin
    First;
    while not Eof do
    begin
      with Order_Sub do
      begin
        Append;
        //FieldByName('FactoryNo').Value:=ADOTemp.fieldbyname('FactoryNo').Value;
        FieldByName('FactoryName').Value:=ADOTemp.fieldbyname('FactoryName').Value;
        //FieldByName('PSName').Value:=ADOTemp.fieldbyname('PSName').Value;
        FieldByName('MDQingKuang').Value:=ADOTemp.fieldbyname('MDQingKuang').Value;
        FieldByName('BQYFMoney').Value:=ADOTemp.fieldbyname('BQYFMoney').Value;
        FieldByName('HZYFMoney').Value:=ADOTemp.fieldbyname('HZYFMoney').Value;
        FieldByName('BQKKMoney').Value:=ADOTemp.fieldbyname('BQKKMoney').Value;
        FieldByName('HZKKMoney').Value:=ADOTemp.fieldbyname('HZKKMoney').Value;
        FieldByName('PKDate').Value:=PKDate.Date;
        FieldByName('DDuiZhangMoney').Value:=ADOTemp.fieldbyname('DDuiZhangMoney').Value;
        FieldByName('MaxHZYFMoney').Value:=ADOTemp.fieldbyname('MaxHZYFMoney').Value;
        Post;
      end;
      Next;
    end;
  end;

end;

procedure TfrmPKInPut.Tv1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
 { if Trim(Order_Sub.fieldbyname('MDQingKuang').AsString)<>'' then
  begin
    Tv1.OptionsData.Editing:=False;
  end else
  begin
    Tv1.OptionsData.Editing:=True;
  end;
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,OrderNo=(select OrderNo from JYOrder_Main where MainId=A.MainId) ');
    SQL.Add(',FKDate=(select Top 1 CRTime from YF_Money_CR YR inner join YF_Money_CR_HX YH on YR.YFID=YH.FKID');
    sql.Add('         where YH.YFID=A.YFID order by YR.CRTime )');
    sql.Add(',YFYMoney=isnull(Money,0)-isnull(YiFuMoney,0)');
    sql.Add('  from YF_Money_CR A where CRType=''应付款登记''');
    SQL.Add(' and FactoryName='''+Trim(Order_Sub.fieldbyname('FactoryName').AsString)+'''');
    //SQL.Add(' and PSName='''+Trim(Order_Sub.fieldbyname('PSName').AsString)+'''');
    if RadioGroup1.ItemIndex=0 then
    begin
      sql.Add(' and CRTime<='''+Trim(Order_Sub.fieldbyname('PKDate').AsString)+'''');
    end else
    if RadioGroup1.ItemIndex=1 then
    begin
      sql.Add(' and CRTime>'''+Trim(Order_Sub.fieldbyname('PKDate').AsString)+'''');
    end;
    Open;
  end;
  SCreateCDS20(ADOTemp,ClientDataSet2);
  SInitCDSData20(ADOTemp,ClientDataSet2);
  if Trim(Order_Sub.fieldbyname('PSName').AsString)='' then
  begin
    if ClientDataSet2.IsEmpty then Exit;
    with Order_Sub do
    begin
      Edit;
      FieldByName('PSName').Value:=Trim(ClientDataSet2.fieldbyname('PSName').AsString);
      //Post;
    end;
  end;   }
end;

procedure TfrmPKInPut.RadioGroup1Click(Sender: TObject);
begin
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,OrderNo=(select OrderNo from JYOrder_Main where MainId=A.MainId) ');
    SQL.Add(',FKDate=(select Top 1 CRTime from YF_Money_CR YR inner join YF_Money_CR_HX YH on YR.YFID=YH.FKID');
    sql.Add('         where YH.YFID=A.YFID order by YR.CRTime )');
    sql.Add(',YFYMoney=isnull(Money,0)-isnull(YiFuMoney,0)');
    sql.Add('  from YF_Money_CR A where CRType=''应付款登记''');
    SQL.Add(' and FactoryName='''+Trim(Order_Sub.fieldbyname('FactoryName').AsString)+'''');
    //SQL.Add(' and PSName='''+Trim(Order_Sub.fieldbyname('PSName').AsString)+'''');
    if RadioGroup1.ItemIndex=0 then
    begin
      sql.Add(' and CRTime<='''+Trim(Order_Sub.fieldbyname('PKDate').AsString)+'''');
    end else
    if RadioGroup1.ItemIndex=1 then
    begin
      sql.Add(' and CRTime>'''+Trim(Order_Sub.fieldbyname('PKDate').AsString)+'''');
    end;
    Open;
  end;
  SCreateCDS20(ADOTemp,ClientDataSet2);
  SInitCDSData20(ADOTemp,ClientDataSet2);
end;

procedure TfrmPKInPut.v1Column11PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='GSTT';
      flagname:='公司抬头';
      V1ZdyStr1.Visible:=True;
      V1ZdyStr2.Visible:=True;
      V1ZdyStr3.Visible:=True;
      V1ZdyStr4.Visible:=True;
      V1ZdyStr1.Caption:='简称';
      V1ZdyStr2.Caption:='电话';
      V1ZdyStr3.Caption:='传真';
      V1ZdyStr4.Caption:='地址';
      frmZDYHelp.Align:=alClient;
      if ShowModal=1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('ComTaiTou').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmPKInPut.v1Column7PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='PKPayMent';
      flagname:='付款方式';
      if ShowModal=1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('PayMent').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmPKInPut.ToolButton2Click(Sender: TObject);
begin
  try
    frmModuleNote:=TfrmModuleNote.Create(Application);
    with frmModuleNote do
    begin
      flag:='排款';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmModuleNote.Free;
  end;
end;

procedure TfrmPKInPut.v1Column13PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='PKYongTu';
      flagname:='资金用途';
      if ShowModal=1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('PKYongTu').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmPKInPut.Tv1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if Trim(Order_Sub.fieldbyname('MDQingKuang').AsString)<>'' then
  begin
    Tv1.OptionsData.Editing:=False;
  end else
  begin
    Tv1.OptionsData.Editing:=True;
  end;
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,OrderNo=(select OrderNo from JYOrder_Main where MainId=A.MainId) ');
    SQL.Add(',FKDate=(select Top 1 CRTime from YF_Money_CR YR inner join YF_Money_CR_HX YH on YR.YFID=YH.FKID');
    sql.Add('         where YH.YFID=A.YFID order by YR.CRTime )');
    sql.Add(',YFYMoney=isnull(Money,0)-isnull(YiFuMoney,0)');
    sql.Add('  from YF_Money_CR A where CRType=''应付款登记''');
    SQL.Add(' and FactoryName='''+Trim(Order_Sub.fieldbyname('FactoryName').AsString)+'''');
    //SQL.Add(' and PSName='''+Trim(Order_Sub.fieldbyname('PSName').AsString)+'''');
    if RadioGroup1.ItemIndex=0 then
    begin
      sql.Add(' and CRTime<='''+Trim(Order_Sub.fieldbyname('PKDate').AsString)+'''');
    end else
    if RadioGroup1.ItemIndex=1 then
    begin
      sql.Add(' and CRTime>'''+Trim(Order_Sub.fieldbyname('PKDate').AsString)+'''');
    end;
    Open;
  end;
  SCreateCDS20(ADOTemp,ClientDataSet2);
  SInitCDSData20(ADOTemp,ClientDataSet2);
  if Trim(Order_Sub.fieldbyname('PSName').AsString)='' then
  begin
    if ClientDataSet2.IsEmpty then Exit;
    with Order_Sub do
    begin
      Edit;
      FieldByName('PSName').Value:=Trim(ClientDataSet2.fieldbyname('PSName').AsString);
      //Post;
    end;
  end;
end;

end.
