unit U_FKCR;

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, cxPC, cxCurrencyEdit, Menus, RM_e_Xls, cxDropDownEdit,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu;

type
  TfrmFKCR = 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;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    ToolButton3: TToolButton;
    VHL: TcxGridDBColumn;
    VBZ: TcxGridDBColumn;
    VYB: TcxGridDBColumn;
    VBB: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    VOrderNo: TcxGridDBColumn;
    Label4: TLabel;
    conNo: TEdit;
    v2Note: TcxGridDBColumn;
    v2DerateMoney: TcxGridDBColumn;
    v2Ssel: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    Tchk: TToolButton;
    TNochk: TToolButton;
    v2ComTaiTou: TcxGridDBColumn;
    Label5: TLabel;
    ComTaiTou: TEdit;
    Label10: TLabel;
    Label12: TLabel;
    v2Chkstatus: TcxGridDBColumn;
    THX: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    v2FKType: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    Label7: TLabel;
    FKType: TEdit;
    v2BankName: TcxGridDBColumn;
    v2BankNo: TcxGridDBColumn;
    v2FKTaiTou: TcxGridDBColumn;
    Label8: TLabel;
    FKTaiTou: TEdit;
    ADOQuery1: TADOQuery;
    TPrint: TToolButton;
    Order_FK: TClientDataSet;
    v2Price: TcxGridDBColumn;
    v2Qty: TcxGridDBColumn;
    v2SQPerpon: TcxGridDBColumn;
    CheckBox2: TCheckBox;
    v2QPDate: TcxGridDBColumn;
    TSsel: TToolButton;
    RMXLSExport1: TRMXLSExport;
    v2IFKP: TcxGridDBColumn;
    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 Button2Click(Sender: TObject);
    procedure cxGridDBColumn8PropertiesEditValueChanged(Sender: TObject);
    procedure cxGridDBColumn5PropertiesEditValueChanged(Sender: TObject);
    procedure cxGridDBColumn1PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure cxGridDBColumn3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure cxGridDBColumn6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v2Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure v2Column4PropertiesEditValueChanged(Sender: TObject);
    procedure v2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure VBBPropertiesEditValueChanged(Sender: TObject);
    procedure conNoChange(Sender: TObject);
    procedure v2Column1PropertiesEditValueChanged(Sender: TObject);
    procedure v2DerateMoneyPropertiesEditValueChanged(Sender: TObject);
    procedure v2fstr1PropertiesEditValueChanged(Sender: TObject);
    procedure TchkClick(Sender: TObject);
    procedure TNochkClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure v2ComTaiTouPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v2SselPropertiesChange(Sender: TObject);
    procedure Tv2StylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure THXClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure v2FKTypePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure CheckBox1Click(Sender: TObject);
    procedure v2BankNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v2BankNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v2FKTaiTouPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TPrintClick(Sender: TObject);
    procedure v2QtyPropertiesEditValueChanged(Sender: TObject);
    procedure v2PricePropertiesEditValueChanged(Sender: TObject);
    procedure v2SQPerponPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TSselClick(Sender: TObject);
    procedure v2ChkstatusPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    { Private declarations }

    procedure InitGridHZ();
    procedure InitGrid();
    function SaveData(): Boolean;
    procedure SetStatus();
  public
    fflag: integer;
    { Public declarations }
    RKFlag, FCYID, fmanage: string;
  end;

var
  frmFKCR: TfrmFKCR;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_ProductOrderList, U_ProductOrderNewList,
  U_PbProductList, U_ZdyAttachGYS, U_YFFP_HX, U_YSCRPB_ZHYH;

{$R *.dfm}

procedure TfrmFKCR.SetStatus();
var
  i: integer;
begin
  Tchk.Enabled := false;
  TNochk.Enabled := false;
  THX.Visible := False;
  for i := 0 to tv2.ColumnCount - 1 do
  begin
    if uppercase(tv2.Columns[i].DataBinding.FieldName) <> 'SSEL' then
      tv2.Columns[i].Options.Editing := false;
  end;
  if cxTabControl1.TabIndex = 0 then
  begin
    Tchk.Enabled := true;
      //TBDel.Enabled:=true;
    for i := 0 to tv2.ColumnCount - 1 do
    begin
      tv2.Columns[i].Options.Editing := true;
    end;
  end;
  if cxTabControl1.TabIndex = 1 then
  begin
    TNochk.Enabled := true;
  end;
end;

procedure TfrmFKCR.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select A.* ');
      sql.add(',WKFPMoney=(isnull(A.Money,0)-isnull(A.YKFPMoney,0))');
      sql.Add(',KHZJM=dbo.getPinYin(A.FactoryName)');
      sql.Add('  from YF_Money_CR A');
      sql.Add(' inner join YF_Money_KC B on A.CRID=B.CRID');
      sql.Add('where A.CRTime>=''' + FormatDateTime('yyyy-MM-dd', begdate.DateTime) + ''' ');
      sql.Add(' and CRTime<''' + FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1) + ''' ');
      sql.Add(' and A.CRType=''付款登记'' and A.YFDefFlag1=''' + inttostr(fflag) + ''' ');
      if cxTabControl1.TabIndex = 0 then
        sql.Add(' and A.status=''0'' ');
      if cxTabControl1.TabIndex = 1 then
        sql.Add(' and A.status=''1'' ');
      if cxTabControl1.TabIndex = 2 then
        sql.Add(' and A.status=''2'' ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
    Label12.Caption := '0';
  finally
    ADOQueryMain.EnableControls;
    //ToolButton2.Click;
  end;
end;

procedure TfrmFKCR.InitGridHZ();
begin

end;

procedure TfrmFKCR.FormDestroy(Sender: TObject);
begin
  frmFKCR := nil;
end;

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

procedure TfrmFKCR.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('付款登记21', Tv2, '财务管理');
  {with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('delete CK_YP_CR where isnull(CRTime,'''')='''' and CYID='''+Trim(FCYID)+''' and CRType='''+Trim(RKFlag)+'''');
    ExecSQL;
  end;}
  Close;
end;

procedure TfrmFKCR.TBDelClick(Sender: TObject);
var
  FBBMoney, CRID: string;
begin
  if CDS_HZ.IsEmpty then
    Exit;
  if cxtabcontrol1.TabIndex <> 0 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;
    with CDS_HZ do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        if Fieldbyname('ssel').AsBoolean = true then
        begin

          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('delete YF_Money_CR where YFID=''' + Trim(CDS_HZ.fieldbyname('YFID').AsString) + '''');
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    InitGrid();
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmFKCR.FormShow(Sender: TObject);
begin
  fflag := 0;
  ReadCxGrid('付款登记21', Tv2, '财务管理');
  Enddate.DateTime := SGetServerDate(ADOQueryTemp);
  begdate.DateTime := SGetServerDate(ADOQueryTemp) - 7;
  THX.Visible := False;
  VBZ.Visible := true;
  if Trim(DParameters1) = '无订单号' then
  begin
    VOrderNo.Hidden := True;
    VOrderNo.Visible := False;
    label4.Visible := False;
    conNo.Visible := False;
  end;
  if Trim(DParameters2) = '人民币' then
  begin
    VHL.Hidden := True;
    VHL.Visible := False;
    VBZ.Hidden := True;
    VBZ.Visible := False;
    VYB.Hidden := True;
    VYB.Visible := False;
    VBB.Caption := '实付金额';
  end;
  SetStatus();
  InitGrid();
end;

procedure TfrmFKCR.TBRafreshClick(Sender: TObject);
begin
  toolbar1.SetFocus;
  InitGrid();
end;

procedure TfrmFKCR.ToolButton2Click(Sender: TObject);
var
  fsj: string;
begin
  if ADOQueryMain.Active = False then
    Exit;
  Label12.Caption := '0';
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryMain, CDS_HZ);
  SInitCDSData20(ADOQueryMain, CDS_HZ);
end;

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

procedure TfrmFKCR.Button2Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  if Trim(CDS_HZ.fieldbyname('SubID').AsString) <> '' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('delete YCL_SG_HZ where SubID=''' + Trim(CDS_HZ.fieldbyname('SubID').AsString) + '''');
      ExecSQL;
    end;
  end;
  CDS_HZ.Delete;
end;

function TfrmFKCR.SaveData(): Boolean;
var
  maxId, CRID: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd, maxId, 'FK', 'YF_Money_CR', 4, 1) = False then
    begin
      Result := False;
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取最大号失败！', '提示', 0);
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YF_Money_KC where FactoryName=''' + Trim(CDS_HZ.fieldbyname('FactoryName').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=2');
        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 1=2');
      Open;
    end;

    with ADOQueryCmd do
    begin
      Append;
      FieldByName('YFID').Value := Trim(maxId);
      FieldByName('CRID').Value := StrToInt(CRID);
      //SSetSaveDataCDSNew(ADOQueryCmd,Tv2,CDS_HZ,'YF_Money_CR',3);
      FieldByName('Filler').Value := Trim(DName);
      FieldByName('CRType').Value := '付款登记';
      FieldByName('BZType').Value := Trim(CDS_HZ.fieldbyname('BZType').AsString);
      FieldByName('CRFlag').Value := '应付付';
      FieldByName('QtyFlag').Value := -1;
      FieldByName('FactoryNo').Value := Trim(CDS_HZ.fieldbyname('FactoryNo').AsString);
      FieldByName('YFName').Value := Trim(CDS_HZ.fieldbyname('YFName').AsString);
      FieldByName('FactoryName').Value := Trim(CDS_HZ.fieldbyname('FactoryName').AsString);
      FieldByName('BankName').Value := Trim(CDS_HZ.fieldbyname('BankName').AsString);
      FieldByName('BankNo').Value := Trim(CDS_HZ.fieldbyname('BankNo').AsString);
      FieldByName('FKTaiTou').Value := Trim(CDS_HZ.fieldbyname('FKTaiTou').AsString);
      fieldbyname('Note').Value := Trim(CDS_HZ.fieldbyname('Note').AsString);
      fieldbyname('ComTaiTou').Value := Trim(CDS_HZ.fieldbyname('ComTaiTou').AsString);
      FieldByName('Money').Value := CDS_HZ.fieldbyname('Money').AsFloat;
      FieldByName('CRTime').Value := formatdateTIme('yyyy-MM-dd', CDS_HZ.fieldbyname('CRTime').AsDateTime);
      FieldByName('YFDefFlag1').Value := fFlag;
      FieldByName('status').Value := '0';
      Post;
    end;
    with CDS_HZ do
    begin
      Edit;
      FieldByName('YFId').Value := Trim(maxId);
      FieldByName('CRID').Value := StrToInt(CRID);
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('增行失败!', '提示', 0);
  end;
end;

procedure TfrmFKCR.cxGridDBColumn8PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YCL_SG_HZ ');
    if Trim(mvalue) <> '' then
    begin
      sql.Add(' Set ' + FFieldName + '=''' + Trim(mvalue) + '''');
    end
    else
    begin
      sql.Add(' Set ' + FFieldName + '=NULL');
    end;
    SQL.Add(',Editer=''' + Trim(DName) + '''');
    SQL.Add(',Edittime=getdate() ');
    SQL.Add(' where SubId=''' + Trim(CDS_HZ.fieldbyname('SubId').AsString) + '''');
    ExecSQL;
  end;
end;

procedure TfrmFKCR.cxGridDBColumn5PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FQty, FPrice, FMoney, FHuiLv, FBBMoney, FKCBB, 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;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select isnull(BBMoney,0) BBMoney from YF_Money_CR where YFID=''' + cds_hz.fieldbyname('YFID').AsString + '''');
    Open;
  end;
  FKCBB := ADOQueryTemp.fieldbyname('BBMoney').AsString;

  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';
  FQty := Trim(CDS_HZ.fieldbyname('Qty').AsString);
  if Trim(FQty) = '' then
    FQty := '0';

  FMoney := Trim(CDS_HZ.fieldbyname('Money').AsString);
  if Trim(FMoney) = '' then
    FMoney := '0';

  FHuiLv := Trim(CDS_HZ.fieldbyname('HuiLv').AsString);
  if Trim(FHuiLv) = '' then
    FHuiLv := '0';

  {FBBMoney:=Trim(CDS_HZ.fieldbyname('BBMoney').AsString);
  if Trim(FBBMoney)='' then FBBMoney:='0'; }

  if Trim(FFieldName) <> 'Money' then
  begin
    if Trim(FFieldName) <> 'HuiLv' then
    begin
      FBBMoney := FloatToStr(StrToFloat(FQty) * StrToFloat(FPrice) * StrToFloat(FHuiLv));
      FMoney := FloatToStr(StrToFloat(FQty) * StrToFloat(FPrice));
    end
    else
    begin
      FBBMoney := FloatToStr(StrToFloat(FMoney) * StrToFloat(FHuiLv));
    end;

  end
  else
  begin
    FBBMoney := FloatToStr(StrToFloat(FMoney) * StrToFloat(FHuiLv));
  end;
  with CDS_HZ do
  begin
    Edit;
    FieldByName('Money').Value := StrToFloat(FMoney);
    FieldByName('BBMoney').Value := StrToFloat(FBBMoney);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' Set ' + FFieldName + '=' + Trim(mvalue));
    if Trim(FFieldName) <> 'Money' then
    begin
      sql.Add(',Money=' + FMoney);
    end;

    sql.Add(',BBMoney=' + FBBMoney);
    SQL.Add(',Editer=''' + Trim(DName) + '''');
    SQL.Add(',Edittime=getdate() ');
    SQL.Add(' where YFId=''' + Trim(CDS_HZ.fieldbyname('YFId').AsString) + '''');

    SQL.Add('Update YF_Money_KC Set KCBBMoney=KCBBMoney-' + FKCBB + '+' + FBBMoney);
    sql.Add(' where CRID=''' + Trim(CDS_HZ.fieldbyname('CRID').AsString) + '''');
    ExecSQL;
  end;
end;

procedure TfrmFKCR.cxGridDBColumn1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    if Trim(mvalue) <> '' then
    begin
      sql.Add(' Set ' + FFieldName + '=''' + Trim(mvalue) + '''');
    end
    else
    begin
      sql.Add(' Set ' + FFieldName + '=NULL');
    end;
    SQL.Add(',Editer=''' + Trim(DName) + '''');
    SQL.Add(',Edittime=getdate() ');
    SQL.Add(' where YFId=''' + Trim(CDS_HZ.fieldbyname('YFId').AsString) + '''');
    ExecSQL;
  end;
end;

procedure TfrmFKCR.ToolButton3Click(Sender: TObject);
var
  SQNo: string;
  i: integer;
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal = 1 then
      begin
        with Self.CDS_HZ do
        begin
          Append;
          FieldByName('FactoryNo').Value := Trim(CDS_HZ.fieldbyname('ZdyCode').AsString);
          FieldByName('FactoryName').Value := Trim(CDS_HZ.fieldbyname('ZdyName').AsString);
          fieldbyname('Note').Value := '';
          FieldByName('Money').Value := 0;
          FieldByName('CRTime').Value := SGetServerDate(ADOQueryTemp);
          FieldByName('HuiLv').Value := 1;
          FieldByName('BZType').Value := '￥';
          fieldbyname('filler').Value := trim(DName);
          Post;
        end;
        Self.SaveData();
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmFKCR.cxGridDBColumn3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  if CDS_HZ.FieldByName('Filler').AsString <> trim(DName) then
  begin
    application.MessageBox('不能操作他人数据', '提示');
    exit;
  end;
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'YFName';
      flagname := '项目及摘要';
      MainType := '付款';
      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;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR ');
          sql.Add(' Set YFCode=''' + Trim(ClientDataSet1.fieldbyname('ZdyNo').AsString) + '''');
          sql.Add(' ,YFName=''' + Trim(ClientDataSet1.fieldbyname('ZdyName').AsString) + '''');
          SQL.Add(',Editer=''' + Trim(DName) + '''');
          SQL.Add(',Edittime=getdate() ');
          SQL.Add(' where YFId=''' + Trim(CDS_HZ.fieldbyname('YFId').AsString) + '''');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmFKCR.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);
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR ');
          sql.Add(' Set QtyUnit=''' + Trim(ClientDataSet1.fieldbyname('ZdyName').AsString) + '''');
          SQL.Add(',Editer=''' + Trim(DName) + '''');
          SQL.Add(',Edittime=getdate() ');
          SQL.Add(' where YFId=''' + Trim(CDS_HZ.fieldbyname('YFId').AsString) + '''');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmFKCR.v2Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'BZType';
      flagname := '币种';
      if ShowModal = 1 then
      begin
        Self.CDS_HZ.Edit;
        Self.CDS_HZ.FieldByName('BZType').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR ');
          sql.Add(' Set BZType=''' + Trim(ClientDataSet1.fieldbyname('ZdyName').AsString) + '''');
          SQL.Add(',Editer=''' + Trim(DName) + '''');
          SQL.Add(',Edittime=getdate() ');
          SQL.Add(' where YFId=''' + Trim(CDS_HZ.fieldbyname('YFId').AsString) + '''');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmFKCR.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  SelExportData(Tv2, ADOQueryMain, '付款列表');
end;

procedure TfrmFKCR.v2Column4PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FMoney, FHuiLv, FBBMoney, FKCBB, FFieldName: string;
begin
  if CDS_HZ.FieldByName('Filler').AsString <> trim(DName) then
  begin
    application.MessageBox('不能操作他人数据', '提示');
    exit;
  end;
  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;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select isnull(BBMoney,0) BBMoney from YF_Money_CR where YFID=''' + cds_hz.fieldbyname('YFID').AsString + '''');
    Open;
  end;
  FKCBB := ADOQueryTemp.fieldbyname('BBMoney').AsString;

  FMoney := Trim(CDS_HZ.fieldbyname('Money').AsString);
  if Trim(FMoney) = '' then
    FMoney := '0';

  FHuiLv := '1';

  FBBMoney := FloatToStr(StrToFloat(FMoney) * StrToFloat(FHuiLv));
  with CDS_HZ do
  begin
    Edit;
    FieldByName('Money').Value := StrToFloat(FMoney);
    FieldByName('BBMoney').Value := StrToFloat(FBBMoney);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' Set ' + FFieldName + '=' + Trim(mvalue));
    sql.Add(',BBMoney=' + FBBMoney);
    SQL.Add(',Editer=''' + Trim(DName) + '''');
    SQL.Add(',Edittime=getdate() ');
    SQL.Add(' where YFId=''' + Trim(CDS_HZ.fieldbyname('YFId').AsString) + '''');

    SQL.Add('Update YF_Money_KC Set KCBBMoney=KCBBMoney+' + FKCBB + '-' + FBBMoney);
    sql.Add(' where CRID=''' + Trim(CDS_HZ.fieldbyname('CRID').AsString) + '''');
    ExecSQL;
  end;
end;

procedure TfrmFKCR.v2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmPbProductList := TfrmPbProductList.Create(Application);
    with frmPbProductList do
    begin
     // DParameters1:='高权限';
      //frmProductOrderList.v1Column4.Visible:=True;
      if ShowModal = 1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('MainId').Value := Trim(frmPbProductList.Order_Main.fieldbyname('MainId').AsString);
          FieldByName('subID').Value := Trim(frmPbProductList.Order_Main.fieldbyname('subID').AsString);
          FieldByName('conNo').Value := Trim(frmPbProductList.Order_Main.fieldbyname('conNo').AsString);
          post;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('UPdate YF_Money_CR ');
            sql.Add(' Set MainId=''' + Trim(frmPbProductList.Order_Main.fieldbyname('MainId').AsString) + '''');
            sql.Add(' ,SubId=''' + Trim(frmPbProductList.Order_Main.fieldbyname('SubId').AsString) + '''');
            SQL.Add(',Editer=''' + Trim(DName) + '''');
            SQL.Add(',Edittime=getdate() ');
            SQL.Add(' where YFId=''' + Trim(CDS_HZ.fieldbyname('YFId').AsString) + '''');
            ExecSQL;
          end;
        end;
      end;
    end;
  finally
    frmPbProductList.Free;
    self.Tv2.Controller.EditingController.ShowEdit();
  end;
end;

procedure TfrmFKCR.VBBPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FKCBB: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select isnull(BBMoney,0) BBMoney from YF_Money_CR where YFID=''' + cds_hz.fieldbyname('YFID').AsString + '''');
    Open;
  end;
  FKCBB := ADOQueryTemp.fieldbyname('BBMoney').AsString;
  if Trim(mvalue) = '' then
    mvalue := '0';
  with CDS_HZ do
  begin
    Edit;
    FieldByName('BBMoney').Value := StrToFloat(mvalue);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' set BBMoney=' + mvalue);
    SQL.Add(',Editer=''' + Trim(DName) + '''');
    SQL.Add(',Edittime=getdate() ');
    SQL.Add(' where YFId=''' + Trim(CDS_HZ.fieldbyname('YFId').AsString) + '''');

    SQL.Add('Update YF_Money_KC Set KCBBMoney=KCBBMoney+' + FKCBB + '-' + mvalue);
    sql.Add(' where CRID=''' + Trim(CDS_HZ.fieldbyname('CRID').AsString) + '''');
    ExecSQL;
  end;
end;

procedure TfrmFKCR.conNoChange(Sender: TObject);
begin
  if Length(Trim(conNo.Text)) < 3 then
    Exit;
  ToolButton2.Click;
end;

procedure TfrmFKCR.v2Column1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  if CDS_HZ.FieldByName('Filler').AsString <> trim(DName) then
  begin
    application.MessageBox('不能操作他人数据', '提示');
    exit;
  end;
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' Set ' + FFieldName + '=''' + Trim(mvalue) + '''');
    SQL.Add(',Editer=''' + Trim(DName) + '''');
    SQL.Add(',Edittime=getdate() ');
    SQL.Add(' where YFId=''' + Trim(CDS_HZ.fieldbyname('YFId').AsString) + '''');
    ExecSQL;
  end;
end;

procedure TfrmFKCR.v2DerateMoneyPropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;

  if Trim(mvalue) = '' then
    mvalue := '0';
  with CDS_HZ do
  begin
    Edit;
    FieldByName('DerateMoney').Value := StrToFloatdef(mvalue, 0);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' set DerateMoney=' + mvalue);
    SQL.Add(' where YFId=''' + Trim(CDS_HZ.fieldbyname('YFId').AsString) + '''');
    ExecSQL;
  end;

end;

procedure TfrmFKCR.v2fstr1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' set fstr1=' + quotedstr(trim(mvalue)));
    SQL.Add(' where YFId=''' + Trim(CDS_HZ.fieldbyname('YFId').AsString) + '''');
    ExecSQL;
  end;
end;

procedure TfrmFKCR.TchkClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;

  if CDS_HZ.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        if Fieldbyname('ssel').AsBoolean = true then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update YF_Money_CR SET status=''1'' ');
            sql.Add('where YFID =' + quotedstr(trim(CDS_HZ.fieldbyname('YFID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    InitGrid();
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmFKCR.TNochkClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;

  if CDS_HZ.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        if Fieldbyname('ssel').AsBoolean = true then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update YF_Money_CR SET status=''0'' ');
            sql.Add('where YFID =' + quotedstr(trim(CDS_HZ.fieldbyname('YFID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;

    InitGrid();
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

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

procedure TfrmFKCR.v2ComTaiTouPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  if CDS_HZ.FieldByName('Filler').AsString <> trim(DName) then
  begin
    application.MessageBox('不能操作他人数据', '提示');
    exit;
  end;
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'ComTaiTou';
      flagname := '公司抬头';
      if ShowModal = 1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('ComTaiTou').Value := Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR ');
          sql.Add(' Set ComTaiTou=''' + Trim(ClientDataSet1.fieldbyname('ZdyName').AsString) + '''');
          SQL.Add(',Editer=''' + Trim(DName) + '''');
          SQL.Add(',Edittime=getdate() ');
          SQL.Add(' where YFId=''' + Trim(CDS_HZ.fieldbyname('YFId').AsString) + '''');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmFKCR.v2SselPropertiesChange(Sender: TObject);
begin
  HJ(CDS_HZ, Label12, CDS_HZ.fieldbyname('Money').AsCurrency);
end;

procedure TfrmFKCR.Tv2StylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
var
  id, id10: Integer;
begin
  id := Tv2.GetColumnByFieldName('ChkStatus').Index;
  if ARecord.Values[id] = '不通过' then
  begin
    AStyle := DataLink_YSYF1.Red;
  end;
end;

procedure TfrmFKCR.THXClick(Sender: TObject);
begin
  frmYFFP_HX := TfrmYFFP_HX.Create(self);
  with frmYFFP_HX do
  begin
    FFPYFID := Trim(self.CDS_HZ.fieldbyname('YFID').AsString);
    FWKFPMoney := Trim(self.CDS_HZ.fieldbyname('WKFPMoney').AsString);
    if showmodal = 1 then
    begin
      with self.ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('update YF_Money_CR set YKFPMoney=(select Sum(KPMoney) from ');
        sql.add('YF_Money_CR where FPYFID=''' + trim(FFPYFID) + '''');
        sql.add(' and CRType=''应付款发票登记'') ');
        sql.Add('where YFID=''' + trim(self.CDS_HZ.fieldbyname('YFID').AsString) + '''');
        execsql;
      end;
    end;
    free;
  end;
  initgrid();
end;

procedure TfrmFKCR.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_HZ, True);
end;

procedure TfrmFKCR.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_HZ, false);
end;

procedure TfrmFKCR.v2FKTypePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  if CDS_HZ.FieldByName('Filler').AsString <> trim(DName) then
  begin
    application.MessageBox('不能操作他人数据', '提示');
    exit;
  end;
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'FKType';
      flagname := '付款类型';
      if ShowModal = 1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('FKType').Value := Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR ');
          sql.Add(' Set FKType=''' + Trim(ClientDataSet1.fieldbyname('ZdyName').AsString) + '''');
          SQL.Add(' where YFId=''' + Trim(CDS_HZ.fieldbyname('YFId').AsString) + '''');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmFKCR.CheckBox1Click(Sender: TObject);
begin
  Label12.Caption := '0';
  if CDS_HZ.IsEmpty then
    exit;
  with CDS_HZ do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value := checkbox1.Checked;
      if checkbox1.Checked = true then
      begin
        Label12.Caption := floattostr(strtofloat(Label12.Caption) + fieldbyname('Money').AsCurrency);
      end;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

procedure TfrmFKCR.v2BankNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  if CDS_HZ.FieldByName('Filler').AsString <> trim(DName) then
  begin
    application.MessageBox('不能操作他人数据', '提示');
    exit;
  end;
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'BankNameFK';
      flagname := '开户行';
      fnote := true;
      v1note.Caption := '银行账号';
      MainType := trim(CDS_HZ.FieldByName('FKTaiTou').AsString);
      if ShowModal = 1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('BankName').Value := Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
          FieldByName('BankNo').Value := Trim(ClientDataSet1.fieldbyname('note').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR ');
          sql.Add(' Set BankName=''' + Trim(ClientDataSet1.fieldbyname('ZdyName').AsString) + '''');
          sql.Add(',BankNo=''' + Trim(ClientDataSet1.fieldbyname('note').AsString) + '''');
          SQL.Add(' where YFId=''' + Trim(CDS_HZ.fieldbyname('YFId').AsString) + '''');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmFKCR.v2BankNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  if CDS_HZ.FieldByName('Filler').AsString <> trim(DName) then
  begin
    application.MessageBox('不能操作他人数据', '提示');
    exit;
  end;
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'BankNoFK';
      flagname := '银行账号';
      MainType := trim(CDS_HZ.FieldByName('FKTaiTou').AsString);
      if ShowModal = 1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('BankNo').Value := Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR ');
          sql.Add(' Set BankNo=''' + Trim(ClientDataSet1.fieldbyname('ZdyName').AsString) + '''');
          SQL.Add(' where YFId=''' + Trim(CDS_HZ.fieldbyname('YFId').AsString) + '''');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmFKCR.v2FKTaiTouPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  if CDS_HZ.FieldByName('Filler').AsString <> trim(DName) then
  begin
    application.MessageBox('不能操作他人数据', '提示');
    exit;
  end;
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'FKTaitou';
      flagname := '付款抬头';
      if ShowModal = 1 then
      begin
        with ADOQuery1 do
        begin
          Close;
          SQL.Clear;
          sql.Add('select Top 1 * from YF_Money_CR ');
          sql.add('where FKTaiTou=''' + trim(ClientDataSet1.fieldbyname('ZdyName').AsString) + '''');
          sql.add(' and CRType=''付款登记''');
          sql.add(' Order by CRTime desc');
          Open;
        end;
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('FKTaitou').Value := Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
          FieldByName('BankName').Value := Trim(ADOQuery1.fieldbyname('BankName').AsString);
          FieldByName('BankNo').Value := Trim(ADOQuery1.fieldbyname('BankNo').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR ');
          sql.Add(' Set FKTaitou=''' + Trim(ClientDataSet1.fieldbyname('ZdyName').AsString) + '''');
          sql.add(' ,BankName=''' + trim(ADOQuery1.fieldbyname('BankName').AsString) + '''');
          sql.add(' ,BankNo=''' + trim(ADOQuery1.fieldbyname('BankNo').AsString) + '''');
          SQL.Add(' where YFId=''' + Trim(CDS_HZ.fieldbyname('YFId').AsString) + '''');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmFKCR.TPrintClick(Sender: TObject);
var
  fPrintFile, FFactoryName, FComTaiTou, FSQNo: string;
  FSQMoney: double;
begin
  if CDS_HZ.IsEmpty then
    exit;
  if CDs_HZ.Locate('Ssel', true, []) = False then
  begin
    application.MessageBox('没有选择数据', '提示');
    exit;
  end;
  with CDS_HZ do
  begin
    first;
    while not eof do
    begin
      if fieldbyname('Ssel').AsBoolean = true then
      begin
        if FFactoryName = '' then
          FFactoryName := fieldbyname('FKTaiTou').AsString
        else
        begin
          if FFactoryName <> fieldbyname('FKTaiTou').AsString then
          begin
            application.MessageBox('付款抬头不一致,不能打印', '提示');
            exit;
          end;
        end;
        if FSQNo = '' then
          FSQNo := fieldbyname('SQNo').AsString
        else
        begin
          if FSQNo <> fieldbyname('SQNo').AsString then
          begin
            application.MessageBox('申请单号不一致,不能打印', '提示');
            exit;
          end;
        end;
        if FComTaiTou = '' then
          FComTaiTou := fieldbyname('ComTaiTou').AsString
        else
        begin
          if FComTaiTou <> fieldbyname('ComTaiTou').AsString then
          begin
            application.MessageBox('公司抬头不一致,不能打印', '提示');
            exit;
          end;
        end;
      end;
      next;
    end;
  end;
  FSQMoney := 0;
  with adoQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where 1=2 ');
    open;
  end;
  SCreateCDS20(adoQueryCmd, Order_FK);
  SInitCDSData20(adoQueryCmd, Order_FK);
  with CDS_HZ do
  begin
    first;
    while not eof do
    begin
      if fieldbyname('Ssel').AsBoolean = true then
      begin
        FSQMoney := FSQMoney + CDS_HZ.fieldbyname('Money').AsFloat;
        with Order_FK do
        begin
          append;
          fieldbyname('FKTaiTou').AsString := trim(CDS_HZ.fieldbyname('FKTaitou').AsString);
          fieldbyname('ComTaiTou').AsString := trim(CDS_HZ.fieldbyname('ComTaiTou').AsString);
          fieldbyname('BankName').AsString := trim(CDS_HZ.fieldbyname('BankName').AsString);
          fieldbyname('BankNo').AsString := trim(CDS_HZ.fieldbyname('BankNo').AsString);
          fieldbyname('BZType').AsString := trim(CDS_HZ.fieldbyname('BZType').AsString);
          fieldbyname('SQNo').AsString := trim(CDS_HZ.fieldbyname('SQNo').AsString);
          fieldbyname('YFName').AsString := trim(CDS_HZ.fieldbyname('YFName').AsString);
          fieldbyname('FKType').AsString := trim(CDS_HZ.fieldbyname('FKType').AsString);
          fieldbyname('Note').AsString := trim(CDS_HZ.fieldbyname('Note').AsString);
          fieldbyname('SQPerPon').AsString := trim(CDS_HZ.fieldbyname('SQPerPon').AsString);
          fieldbyname('QPDate').Value := CDS_HZ.fieldbyname('QPDate').AsDateTime;
          fieldbyname('FactoryName').AsString := trim(CDS_HZ.fieldbyname('FactoryName').AsString);
          fieldbyname('Money').Value := CDS_HZ.fieldbyname('Money').AsFloat;
          fieldbyname('Qty').Value := CDS_HZ.fieldbyname('Qty').AsFloat;
          fieldbyname('Price').Value := CDS_HZ.fieldbyname('Price').AsFloat;
          fieldbyname('ChkStatus').Value := trim(CDS_HZ.fieldbyname('ChkStatus').AsString);
          post;
        end;
      end;
      next;
    end;
  end;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\资金使用申请单.rmf';
  if FileExists(fPrintFile) then
  begin
    if CheckBox2.Checked = true then
    begin
      RMVariables['FFactory'] := trim(Order_FK.fieldbyname('FactoryName').AsString);
    end
    else
    begin
      RMVariables['FFactory'] := '';
    end;
    RMVariables['FSQMoney'] := FSQMoney;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\资金使用申请单.rmf'), '提示', 0);
  end;
end;

procedure TfrmFKCR.v2QtyPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  if trim(mvalue) = '' then
    mvalue := '0';
  with CDS_HZ do
  begin
    Edit;
    FieldByName('Qty').Value := Trim(mvalue);
    FieldByName('Money').Value := FieldByName('Qty').AsFloat * FieldByName('Price').AsFloat;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' Set Qty=''' + trim(mvalue) + '''');
    SQL.Add(',Money=''' + Trim(CDS_HZ.fieldbyname('Money').AsString) + '''');
    SQL.Add(',Edittime=getdate() ');
    SQL.Add(' where YFId=''' + Trim(CDS_HZ.fieldbyname('YFId').AsString) + '''');
    ExecSQL;
  end;
end;

procedure TfrmFKCR.v2PricePropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  if trim(mvalue) = '' then
    mvalue := '0';
  with CDS_HZ do
  begin
    Edit;
    FieldByName('Price').Value := Trim(mvalue);
    FieldByName('Money').Value := FieldByName('Qty').AsFloat * FieldByName('Price').AsFloat;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YF_Money_CR ');
    sql.Add(' Set Price=''' + trim(mvalue) + '''');
    SQL.Add(',Money=''' + Trim(CDS_HZ.fieldbyname('Money').AsString) + '''');
    SQL.Add(',Edittime=getdate() ');
    SQL.Add(' where YFId=''' + Trim(CDS_HZ.fieldbyname('YFId').AsString) + '''');
    ExecSQL;
  end;
end;

procedure TfrmFKCR.v2SQPerponPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SQPerpon';
      flagname := '申请人';
      if ShowModal = 1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('SQPerpon').Value := Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR ');
          sql.Add(' Set SQPerpon=''' + Trim(ClientDataSet1.fieldbyname('ZdyName').AsString) + '''');
          SQL.Add(' where YFId=''' + Trim(CDS_HZ.fieldbyname('YFId').AsString) + '''');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmFKCR.TSselClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  ModalResult := 1;
end;

procedure TfrmFKCR.v2ChkstatusPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'Chkstatus';
      flagname := '审核状态';
      if ShowModal = 1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('Chkstatus').Value := Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate YF_Money_CR ');
          sql.Add(' Set Chkstatus=''' + Trim(ClientDataSet1.fieldbyname('ZdyName').AsString) + '''');
          SQL.Add(' where YFId=''' + Trim(CDS_HZ.fieldbyname('YFId').AsString) + '''');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.

