unit U_SDMANAGEINPUT;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, DB, ADODB, cxControls, cxContainer, cxEdit,
  cxTextEdit, cxCurrencyEdit, BtnEdit, StdCtrls, ExtCtrls, cxStyles,
  cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage, cxDBData,
  cxButtonEdit, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxGridCustomView, cxGrid, cxGridCustomPopupMenu,
  cxGridPopupMenu, DBClient, cxDropDownEdit, Menus, cxLookAndFeelPainters,
  strUtils, cxButtons, cxImage, cxDBEdit, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTCPClient, ShellAPI, IdFTP, cxLookAndFeels, cxNavigator;

type
  TfrmSDMANAGEINPUT = class(TForm)
    ToolBar1: TToolBar;
    TSAVE: TToolButton;
    Tclose: TToolButton;
    ADOQueryTMP: TADOQuery;
    ADOQueryCMD: TADOQuery;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxGridPopupMenu2: TcxGridPopupMenu;
    CDS_HZ: TClientDataSet;
    ADOQueryMain: TADOQuery;
    DS_HZ: TDataSource;
    ADOQueryImage: TADOQuery;
    DataSource1: TDataSource;
    Panel1: TPanel;
    Label1: TLabel;
    Label3: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label9: TLabel;
    Label2: TLabel;
    Label10: TLabel;
    Label12: TLabel;
    Label17: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    ZWBFY: TEdit;
    ZSKP: TEdit;
    ZYJ: TEdit;
    ZGNKF: TEdit;
    ZQITA: TEdit;
    CRTime: TDateTimePicker;
    SDNO: TEdit;
    money: TEdit;
    BZType: TComboBox;
    factoryName: TBtnEditA;
    Label7: TLabel;
    Note: TMemo;
    ToolBar2: TToolBar;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column13: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    v1YSKeZhong: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    wxfpno: TEdit;
    Tv1Column3: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Label4: TLabel;
    DZMONEY: TEdit;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Label8: TLabel;
    ToolButton4: TToolButton;
    Tv1Column9: TcxGridDBColumn;
    ToolButton5: TToolButton;
    ADOQuery1: TADOQuery;
    Tv1Column10: TcxGridDBColumn;
    ToolButton6: TToolButton;
    Label11: TLabel;
    TZmoney: TEdit;
    Tv1Column11: TcxGridDBColumn;
    procedure TcloseClick(Sender: TObject);
    procedure TSAVEClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure QtyKeyPress(Sender: TObject; var Key: Char);
    procedure factoryNameBtnClick(Sender: TObject);
    procedure WXFPNOBtnClick(Sender: TObject);
    procedure WXFPNOBtnUpClick(Sender: TObject);
    procedure WXFPNOBtnDnClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure v1Column13PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column1PropertiesChange(Sender: TObject);
    procedure Tv1Column1PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure moneyChange(Sender: TObject);
    procedure v1Column14PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column11PropertiesEditValueChanged(Sender: TObject);
  private
    procedure savedata();
    procedure initdata();
    procedure HJJE();
    procedure DZJE();
    { Private declarations }
  public
    fkeyNo: string;
    fflag: integer;
    fSYRName: string;
    canshu1: string;
    fFlileFlag: string;
    { Public declarations }
  end;

var
  frmSDMANAGEINPUT: TfrmSDMANAGEINPUT;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_ZDYHelpSel, U_ZdyAttachment, U_ZdyAttachGYS,
  U_CHListsel, U_JHDMAKELISTSEL, U_ProductOrderListSel;

{$R *.dfm}

procedure TfrmSDMANAGEINPUT.HJJE();
var
  FMoney, FHuiLv, FBBMoney: double;
begin
  if TV1.DataController.Summary.FooterSummaryValues[9] <> NULL then
    FMoney := TV1.DataController.Summary.FooterSummaryValues[9]
  else
    FMoney := 0;

  zskp.Text := floattostr(FMoney);

end;

procedure TfrmSDMANAGEINPUT.DZJE();
var
  FMoney, FYJ, FWBFY, FQITA, FGNKF, FSKP, fhjhx: double;
  JL: Integer;
begin
  FMoney := StrToFloatDef(money.TEXT, 0);
  FGNKF := StrToFloatDef(ZGNKF.TEXT, 0);
  FYJ := StrToFloatDef(ZYJ.TEXT, 0);
  FWBFY := StrToFloatDef(ZWBFY.TEXT, 0);
  FQITA := StrToFloatDef(ZQITA.TEXT, 0);
  FSKP := StrToFloatDef(ZSKP.TEXT, 0);
  DZMONEY.TEXT := FLOATTOSTR(FMoney - FYJ - FGNKF - FWBFY - FQITA - FSKP);
  fhjhx := 0;
  if TV1.DataController.Summary.FooterSummaryValues[8] <> null then
  begin
    fhjhx := TV1.DataController.Summary.FooterSummaryValues[8];
  end;
  if CDS_HZ.IsEmpty = FALSE then
  begin
    JL := CDS_HZ.RecordCount;

    with CDS_HZ do
    begin
      First;
      while not Eof do
      begin
        Edit;
        if FGNKF <> 0 then
        begin
          fieldbyname('GNKFMONEY').VALUE := FGNKF / JL;
        end;
        if FWBFY <> 0 then
        begin
          fieldbyname('WBFYMONEY').VALUE := FWBFY / JL;
        end;
        if FQITA <> 0 then
        begin
          fieldbyname('QTMONEY').VALUE := FQITA / JL;
        end;
        if FYJ <> 0 then
        begin
          fieldbyname('YJMONEY').VALUE := FYJ / JL;
//          fieldbyname('YJMONEY').VALUE := StrToFloatDef(fieldbyname('HXNUM').ASSTRING, 0) / 100 * FYJ;
        end;
        if DZMONEY.TEXT <> '' then
        begin

          fieldbyname('ZDZJE').VALUE := StrToFloatDef(fieldbyname('HXNUM').ASSTRING, 0) - StrToFloatDef(fieldbyname('SPK').ASSTRING, 0) - StrToFloatDef(fieldbyname('GNKFMONEY').ASSTRING, 0) - StrToFloatDef(fieldbyname('WBFYMONEY').ASSTRING, 0) - StrToFloatDef(fieldbyname('QTMONEY').ASSTRING, 0) - StrToFloatDef(fieldbyname('YJMONEY').ASSTRING, 0) - StrToFloatDef(fieldbyname('SUBTZMONEY').ASSTRING, 0);
        end;

        Post;
        Next;
      end;
    end;
  end;
//  if fhjhx <> 0 then
//  begin
//    ShowMessage(FloatToStr(fhjhx));
  fhjhx := RoundFloat((StrToFloatdef(money.Text, 0) - fhjhx), 2);
  label8.Caption := '未核销金额：' + Trim(FloatToStr(fhjhx));
//  end;

end;

procedure TfrmSDMANAGEINPUT.initdata();
begin
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR A where A.YFID=' + quotedstr(trim(fkeyNo)));
//    ShowMessage(sql.Text);
    open;
  end;
  if ADOQueryTmp.isempty = False then
  begin
    SCSHDataNew(ADOQueryTmp, Panel1, 1);
    SCSHDataNew(ADOQueryTmp, Panel1, 2);
  end
  else
  begin
    ZGNKF.Text := '0';
    ZWBFY.Text := '0';
    ZQITA.Text := '0';

    ZYJ.Text := '0';
    money.Text := '0';
    ZSKP.Text := '0';
    CRTime.DateTime := SGetServerDate(ADOQueryTmp);
  end;

  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select *      ');
    SQL.Add(',DHXMONEY=MXMONEY-(SELECT SUM(HXNUM) FROM YF_MONEY_CR_SUB D WHERE D.SUBWXFPNO=A.SUBWXFPNO AND D.MXORDERNO=A.MXORDERNO)');
    SQL.Add(',YHXMONEY=(SELECT SUM(HXNUM) FROM YF_MONEY_CR_SUB D WHERE D.SUBWXFPNO=A.SUBWXFPNO AND D.MXORDERNO=A.MXORDERNO)');
    SQL.Add(' from YF_Money_CR_Sub A  where YFID=''' + trim(fkeyNo) + '''');
    open;
  end;
  SCreateCDS20(ADOQueryTmp, CDS_HZ);
  SInitCDSData20(ADOQueryTmp, CDS_HZ);
end;

procedure TfrmSDMANAGEINPUT.savedata();
var
  MaxYFSID, MaxYFID: string;
begin
  ADOQueryCmd.Connection.BeginTrans;
  try
    if trim(fkeyNo) = '' then
    begin
      if GetLSNo(ADOQueryTmp, MaxYFID, 'SK', 'YF_Money_CR', 4, 1) = False then
      begin
        Application.MessageBox('取最大号失败！', '提示', 0);
        Exit;
      end;

    end
    else
    begin
      MaxYFID := trim(fkeyNo);

    end;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.add('select * from YF_Money_CR where YFID=' + quotedstr(fkeyNo));
      open;
    end;
    with ADOQueryCmd do
    begin
      if trim(fkeyNo) = '' then
      begin
        append;
        Fieldbyname('YFID').value := trim(MaxYFID);
        FieldByName('CRType').Value := '收款登记';
        FieldByName('CRFlag').Value := '应收收';
        FieldByName('QtyFlag').Value := -1;
        FieldByName('BBMONEY').Value := money.Text;
        FieldByName('status').Value := '0';
        FieldByName('Filler').Value := Trim(DName);
        Fieldbyname('SKtype').value := '水单';

      end
      else
      begin
        edit;
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOQueryTmp);
      end;
      Fieldbyname('wxfpno').value := Trim(wxfpno.Text);
      SSetsaveSqlnew(ADOQueryCmd, 'YF_Money_CR', Self.panel1, 1);
      post;
    end;
    ////保存子表
    with ADOQueryTMP do
    begin
      Close;
      sql.Clear;
      sql.add('select count(*) as AA from YF_Money_CR where sdno=' + quotedstr(Trim(SDNO.Text)));
      Open;
      if FieldByName('AA').AsInteger > 1 then
      begin
        ADOQueryCmd.Connection.RollbackTrans;

        Application.MessageBox('水单编号重复！', '提示', 0);
        Exit;
      end;
    end;

    with CDS_HZ do
    begin
      first;
      while not eof do
      begin
        if fieldbyname('YFSID').AsString = '' then
        begin
          if GetLSNo(ADOQueryTmp, MaxYFSID, 'SKS', 'YF_Money_CR_Sub', 4, 1) = False then
          begin
            Application.MessageBox('取最大号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          MaxYFSID := CDS_HZ.fieldbyname('YFSID').AsString;
        end;
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.add('select * from YF_Money_CR_Sub where YFSID=' + Quotedstr(trim(MaxYFSID)));
          open;
        end;
        if ADOQueryCmd.IsEmpty then
        begin
          ADOQueryCmd.append;
          ADOQueryCmd.Fieldbyname('YFSID').value := trim(MaxYFSID);
          ADOQueryCmd.Fieldbyname('YFID').value := trim(MaxYFID);
        end
        else
        begin
          ADOQueryCmd.edit;
        end;
        ADOQueryCmd.Fieldbyname('qty').value := 0;
        ADOQueryCmd.Fieldbyname('price').value := 0;
        ADOQueryCmd.Fieldbyname('kxtype').value := '水单';
        SSetSaveDataCDSNew(ADOQueryCmd, Tv1, CDS_HZ, 'YF_Money_CR_Sub', 0);
        ADOQueryCmd.Post;
        next;
      end;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('保存成功！', '提示');
    fkeyNo := MaxYFID;
    Modalresult := 1;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('保存失败！', '提示', MB_ICONERROR);
  end;
end;

procedure TfrmSDMANAGEINPUT.TcloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmSDMANAGEINPUT.TSAVEClick(Sender: TObject);
var
  FREAL: Double;
  FMONEY1: Double;
begin
  if CDS_HZ.IsEmpty then
  begin
    application.MessageBox('明细不能为空', '提示');
    exit;
  end;
  if trim(SDNO.Text) = '' then
  begin
    application.MessageBox('水单编号不能为空', '提示');
    exit;
  end;
//  if Trim(wxfpno.Text) = '' then
//  begin
//    application.MessageBox('发票号不能为空', '提示');
//    exit;
//  end;
  if Trim(factoryName.Text) = '' then
  begin
    application.MessageBox('客户不能为空', '提示');
    exit;
  end;
  FMONEY1 := StrToFloatDef(money.Text, 0) - StrToFloatDef(tzmoney.Text, 0);
//  if TV1.DataController.Summary.FooterSummaryValues[7] <> null then
//  begin
//    if TV1.DataController.Summary.FooterSummaryValues[7] < FMONEY1 then
//    begin
//      application.MessageBox('收汇金额不能大于发票金额', '提示');
//      exit;
//    end;
//
//  end;
//  if TV1.DataController.Summary.FooterSummaryValues[8] <> null then
//  begin
//    if TV1.DataController.Summary.FooterSummaryValues[8] < FMONEY1 then
//    begin
//      application.MessageBox('收汇金额不能大于核销金额', '提示');
//      exit;
//    end;
//
//  end;

  savedata();
end;

procedure TfrmSDMANAGEINPUT.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := cahide;
end;

procedure TfrmSDMANAGEINPUT.FormDestroy(Sender: TObject);
begin
  frmSDMANAGEINPUT := nil;
end;

procedure TfrmSDMANAGEINPUT.FormShow(Sender: TObject);
begin
  ToolBar1.SetFocus();
  SClearData(Panel1, 0);
  CRTime.DateTime := SGetServerDate10(adoqueryTmp);

  initData();
  DZJE();
end;

procedure TfrmSDMANAGEINPUT.QtyKeyPress(Sender: TObject; var Key: Char);
begin
  if not (Key in ['0'..'9', '.', '-', #8]) then
    Key := #0;
  if (Key = '.') and (Pos('.', Tedit(Sender).Text) > 0) then
    Key := #0;
end;

procedure TfrmSDMANAGEINPUT.factoryNameBtnClick(Sender: TObject);
begin
  try
    frmZdyAttachment := TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
      if ShowModal = 1 then
      begin
        FactoryName.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);

      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
end;

procedure TfrmSDMANAGEINPUT.WXFPNOBtnClick(Sender: TObject);
begin
  try
    frmCHListsel := TfrmCHListsel.Create(Application);
    with frmCHListsel do
    begin
      if ShowModal = 1 then
      begin
        WXFPNO.Text := Trim(CDS_Main.fieldbyname('A4FPNO').AsString);

      end;
    end;
  finally
    frmCHListsel.Free;
  end;
end;

procedure TfrmSDMANAGEINPUT.WXFPNOBtnUpClick(Sender: TObject);
begin
  try
    frmCHListsel := TfrmCHListsel.Create(Application);
    with frmCHListsel do
    begin
      if ShowModal = 1 then
      begin
        while CDS_Main.Locate('SSel', true, []) do
        begin
          if WXFPNO.Text = '' then
          begin
            WXFPNO.Text := Trim(CDS_Main.fieldbyname('A4FPNO').AsString) + ';';
          end
          else
          begin
            WXFPNO.Text := Trim(WXFPNO.Text) + Trim(CDS_Main.fieldbyname('A4FPNO').AsString) + ';';
          end;

          CDS_Main.Delete;
        end;
      end;
    end;
  finally
    frmCHListsel.Free;
  end;
end;

procedure TfrmSDMANAGEINPUT.WXFPNOBtnDnClick(Sender: TObject);
begin
  WXFPNO.Text := '';
end;

procedure TfrmSDMANAGEINPUT.ToolButton2Click(Sender: TObject);
var
  AA: array[0..100] of string;
  i, J, K, Count: Integer;
begin

  try
    frmJHDMAKEListsel := TfrmJHDMAKEListsel.Create(Application);
    with frmJHDMAKEListsel do
    begin
      if ShowModal = 1 then
      begin
        while CDS_Main.Locate('SSel', true, []) do
        begin

          WXFPNO.Text := WXFPNO.Text + CDS_Main.fieldbyname('A4FPNO').AsString + ';';
          if factoryName.Text = '' then
          begin
            factoryName.Text := CDS_Main.fieldbyname('KHName').AsString;
          end;

          with ADOQueryTMP do
          begin
            Close;
            SQL.Clear;
            sql.Add('SELECT ORDERNO,DDHXNUM,(C7BGMoney+(CASE WHEN DDGS>0 THEN  ROUND(ADDMONEY/DDGS,2) ELSE 0 END ))C7BGMoney FROM (   ');
            SQL.Add('select (sum(C7BGMoney)+sum(isnull(CYFEE,0))) C7BGMoney,orderno ');
            SQL.Add(',DDGS=(select count(distinct OrderNo) from JHD_SUB b  where  B.SVALID=''Y'' AND  b.jhid=' + QuotedStr(Trim(CDS_Main.fieldbyname('jhid').AsString)));
            SQL.Add(')');
            sql.Add(',DDHXNUM=ISNULL((SELECT SUM(HXNUM) FROM YF_MONEY_CR_SUB D WHERE D.SUBWXFPNO=B.A4FPNO AND D.MXORDERNO=A.ORDERNO),0)');
            SQL.Add(',ADDMONEY=(SELECT isnull(jsmoney,0)-isnull(jmmoney,0) FROM JHD_Main B where b.VALID=''Y'' and  b.jhid=' + QuotedStr(Trim(CDS_Main.fieldbyname('jhid').AsString)));
            SQL.Add(')from jhd_SUB  a INNER JOIN JHD_MAIN B ON A.JHID=B.JHID');
            SQL.Add('where A.JHID=' + QuotedStr(Trim(CDS_Main.fieldbyname('jhid').AsString)));
            SQL.Add('AND B.VALID=''Y'' AND A.SVALID=''Y''');
            sql.Add('GROUP BY ORDERNO,A4FPNO )AA ORDER BY ORDERNO');
//            ShowMessage(SQL.Text);
            Open;
          end;

          with ADOQueryTMP do
          begin
            First;
            while not Eof do
            begin
              with self.CDS_HZ do
              begin
                append;
                FieldByName('SUBWXFPNO').Value := CDS_Main.fieldbyname('A4FPNO').AsString;
                FieldByName('MXORDERNO').Value := ADOQueryTMP.fieldbyname('orderno').AsString;

                FieldByName('YHXMONEY').Value := ADOQueryTMP.fieldbyname('DDHXNUM').AsString;
                FieldByName('mxmoney').Value := ADOQueryTMP.fieldbyname('C7BGMoney').AsString;
                FieldByName('DHXMONEY').Value := StrToFloatDEF(ADOQueryTMP.fieldbyname('C7BGMoney').AsString, 0) - StrToFloatDEF(ADOQueryTMP.fieldbyname('DDHXNUM').AsString, 0);
                FieldByName('kxtype').Value := '货款';
                Post;
              end;
              Next;
            end;
          end;

          CDS_Main.Delete;
        end;
      end;
    end;
  finally
    frmJHDMAKEListsel.Free;
  end;
  DZJE();

//  with CDS_HZ do
//  begin
//    First;
//    i := 0;
//    while not Eof do
//    begin
//      AA[i] := Trim(fieldbyname('SUBWXFPNO').AsString);
//      i := i + 1;
//      Next;
//    end;
//  end;
//  Count := Length(N);
//  for i := 0 to Count - 1 do
//  begin
//    for J := i + 1 to Count - 1 do
//    begin
//      if N[i] = N[J] then
//      begin
//        K := J + 1;
//        while K < Count do
//        begin
//          N[K - 1] := N[K];
//          Inc(K);
//          Dec(Count);
//        end;
//      end;
//      if J >= Count then
//        Break;
//    end;
//    if i >= Count then
//      Break;
//
//  end;

end;

procedure TfrmSDMANAGEINPUT.v1Column13PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmJHDMAKEListsel := TfrmJHDMAKEListsel.Create(Application);
    with frmJHDMAKEListsel do
    begin
      if ShowModal = 1 then
      begin
        with self.CDS_HZ do
        begin
          Edit;
          FieldByName('SUBWXFPNO').Value := CDS_Main.fieldbyname('A4FPNO').AsString;
//          FieldByName('MXORDERNO').Value := CDS_Main.fieldbyname('hZORDERNO').AsString;
        end;
      end;
    end;
  finally
    frmJHDMAKEListsel.Free;
  end;
end;

procedure TfrmSDMANAGEINPUT.Tv1Column1PropertiesChange(Sender: TObject);
begin
  HJJE();
end;

procedure TfrmSDMANAGEINPUT.Tv1Column1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FQty, FPrice, FMxMoney: double;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
  end;

  tv1.Controller.EditingController.ShowEdit();
  HJJE();
end;

procedure TfrmSDMANAGEINPUT.ToolButton3Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  if application.MessageBox('确定要删除吗', '提示', 1) = 2 then
    exit;
  if trim(CDS_HZ.fieldbyname('YFSID').asstring) = '' then
  begin
    CDS_HZ.delete;
    HJJE();
  end
  else
  begin
    with ADOQueryCmd do
    begin
      close;
      sql.clear;
      sql.add('delete from YF_Money_CR_Sub where YFSID=''' + trim(CDS_HZ.fieldbyname('YFSID').asstring) + ''' ');
      execsql;
    end;
    CDS_HZ.delete;
    HJJE();

  end;
  with CDS_HZ do
  begin
    WXFPNO.TEXT := '';
    First;
    while not Eof do
    begin
      WXFPNO.Text := WXFPNO.Text + CDS_HZ.fieldbyname('SUBWXFPNO').AsString + ';';
      Next;
    end;
  end;
  DZJE();
end;

procedure TfrmSDMANAGEINPUT.ToolButton1Click(Sender: TObject);
begin
  with self.CDS_HZ do
  begin
    append;
//    FieldByName('SUBWXFPNO').Value := CDS_Main.fieldbyname('A4FPNO').AsString;
//    FieldByName('MXORDERNO').Value := ADOQueryTMP.fieldbyname('orderno').AsString;
    FieldByName('mxmoney').Value := 0;
    FieldByName('kxtype').Value := '定金';
    Post;
  end;
  DZJE();
end;

procedure TfrmSDMANAGEINPUT.moneyChange(Sender: TObject);
begin
  DZJE();
end;

procedure TfrmSDMANAGEINPUT.v1Column14PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FQty, FPrice, FMxMoney: double;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
  end;
  FQty := StrToFloatDef(ZYJ.Text, 0);
  with CDS_HZ do
  begin
    Edit;

    FPrice := FieldByName('HXNUM').AsFloat;
    Post;
  end;
  FMxMoney := strtofloat(format('%.2f', [FPrice * FQty / 100]));
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    FieldbyName('YJMONEY').Value := FMxMoney;
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();

  dzje();
end;

procedure TfrmSDMANAGEINPUT.ToolButton4Click(Sender: TObject);
var
  FMONEY: Double;
begin

  if money.Text <> '' then
  begin
    FMONEY := StrToFloatDEF(money.Text, 0) ;                   //- StrToFloatDEF(TZmoney.Text, 0)
    with CDS_HZ do
    begin
      First;
      while not Eof do
      begin
        Edit;
        CDS_HZ.FieldByName('HXNUM').value := 0;
        CDS_HZ.FieldByName('DHXMONEY').value := CDS_HZ.FieldByName('MXMONEY').value - CDS_HZ.FieldByName('yhxmoney').value;
        Next;
      end;
    end;
    with CDS_HZ do
    begin
      First;
      while not Eof do
      begin
        FMONEY := FMONEY - CDS_HZ.FieldByName('DHXMONEY').AsFloat;
        if FMONEY > 0 then
        begin
          Edit;
          CDS_HZ.FieldByName('HXNUM').value := StrToFloatdef(CDS_HZ.FieldByName('HXNUM').AsString, 0) + CDS_HZ.FieldByName('DHXMONEY').AsFloat;
          CDS_HZ.FieldByName('DHXMONEY').value := 0;
          Post;
        end
        else
        begin
          Edit;
          CDS_HZ.FieldByName('HXNUM').value := StrToFloatdef(CDS_HZ.FieldByName('HXNUM').AsString, 0) + FMONEY + CDS_HZ.FieldByName('DHXMONEY').AsFloat;
          CDS_HZ.FieldByName('DHXMONEY').value := CDS_HZ.FieldByName('DHXMONEY').value - CDS_HZ.FieldByName('HXNUM').value;
          Post;
          Break;
        end;

        Next;
      end;
    end;
  end;
  DZJE();
end;

procedure TfrmSDMANAGEINPUT.ToolButton5Click(Sender: TObject);
begin
  with CDS_HZ do
  begin
    First;
    while not Eof do
    begin
      Edit;
      FieldByName('HXNUM').AsString := '';
      FieldByName('DHXMONEY').AsString := FieldByName('MXMONEY').AsString;
      Post;
      Next;
    end;
  end;

end;

procedure TfrmSDMANAGEINPUT.ToolButton6Click(Sender: TObject);
begin
  with self.CDS_HZ do
  begin
    append;
//    FieldByName('SUBWXFPNO').Value := CDS_Main.fieldbyname('A4FPNO').AsString;
//    FieldByName('MXORDERNO').Value := ADOQueryTMP.fieldbyname('orderno').AsString;
    FieldByName('mxmoney').Value := 0;
    FieldByName('kxtype').Value := '其他收入';
    Post;
  end;
  DZJE();
end;

procedure TfrmSDMANAGEINPUT.v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmProductOrderListSel := TfrmProductOrderListSel.Create(Application);
    with frmProductOrderListSel do
    begin
      if ShowModal = 1 then
      begin
        with CDS_HZ do
        begin
          Edit;
          fieldbyname('mxorderno').AsString := Trim(frmProductOrderListSel.Order_Main.fieldbyname('orderno').AsString);
        end;

      end;
    end;
  finally
    frmProductOrderListSel.Free;
  end;
end;

procedure TfrmSDMANAGEINPUT.Tv1Column11PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FQty, FPrice, FMxMoney, FMoney: double;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
  end;

  tv1.Controller.EditingController.ShowEdit();
  if TV1.DataController.Summary.FooterSummaryValues[16] <> NULL then
    FMoney := TV1.DataController.Summary.FooterSummaryValues[16]
  else
    FMoney := 0;

  TZmoney.Text := floattostr(FMoney);
end;

end.

