unit U_ReceivableInput;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin, StdCtrls,
  cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, ShellAPI, IniFiles, cxCheckBox, cxCalendar, cxButtonEdit,
  cxTextEdit, cxDropDownEdit, BtnEdit, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, dxDateRanges, dxBarBuiltInMenu, U_BaseList, cxContainer,
  cxCurrencyEdit, dxCore, cxDateUtils, cxMemo, dxSkinsCore,
  dxSkinsDefaultPainters, dxScrollbarAnnotations, cxGroupBox, dxCoreGraphics,
  cxDBLookupComboBox;

type
  TfrmReceivableInput = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ToolButton3: TToolButton;
    ADOQueryMain: TADOQuery;
    cxGroupBox1: TcxGroupBox;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    Tv1Column17: TcxGridDBColumn;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    CDS_1: TClientDataSet;
    DS_1: TDataSource;
    Tv1Column21: TcxGridDBColumn;
    Tv1Column22: TcxGridDBColumn;
    Tv1Column23: TcxGridDBColumn;
    Tv1Column24: TcxGridDBColumn;
    Tv1Column25: TcxGridDBColumn;
    Tv1Column26: TcxGridDBColumn;
    Tv1Column27: TcxGridDBColumn;
    Tv1Column28: TcxGridDBColumn;
    ScrollBox1: TScrollBox;
    Label2: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label5: TLabel;
    Label9: TLabel;
    Label20: TLabel;
    Label22: TLabel;
    Label23: TLabel;
    Label24: TLabel;
    Label18: TLabel;
    Label26: TLabel;
    Label27: TLabel;
    OurCoName: TcxButtonEdit;
    OppCoName: TcxButtonEdit;
    OppParentCoName: TcxButtonEdit;
    FFTime: TcxDateEdit;
    ConNo: TcxButtonEdit;
    OrderNo: TcxButtonEdit;
    FFAbstract: TcxButtonEdit;
    PayMent: TcxButtonEdit;
    IsInvoice: TcxComboBox;
    Note: TcxMemo;
    ReconciliationDate: TcxDateEdit;
    ExchangeRate: TcxCurrencyEdit;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    ADO2: TADOQuery;
    Tv1Column4: TcxGridDBColumn;
    DS_YS: TDataSource;
    DS_PM: TDataSource;
    ADO_PM: TADOQuery;
    ADO_YS: TADOQuery;
    DS_DanWei: TDataSource;
    ADO_DanWei: TADOQuery;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure PayMentBtnDnClick(Sender: TObject);
    procedure PayMentBtnUpClick(Sender: TObject);
    //procedure QtyExit(Sender: TObject);
    //procedure ScrollBox1Click(Sender: TObject);
    procedure OrderNoBtnUpClick(Sender: TObject);
    procedure ConNoBtnUpClick(Sender: TObject);
    procedure OurCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure OppCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure OurCoNameDblClick(Sender: TObject);
    procedure OppParentCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ConNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure OrderNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure FFAbstractPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure v1QtyPropertiesEditValueChanged(Sender: TObject);
    procedure Tv1CustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
    procedure ConNoPropertiesChange(Sender: TObject);
    procedure Tv1Column1PropertiesEditValueChanged(Sender: TObject);
    procedure Tv1Column2PropertiesEditValueChanged(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
//    procedure HJJE();
    function SaveData(): Boolean;
  public
    { Public declarations }
    FFFID, FAuthority, canshu4: string;
  end;

var
  frmReceivableInput: TfrmReceivableInput;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_CompanySel, U_FinTradePlanSel,
  U_FinTradeSalesContractSel, U_ClothInfoSel;

{$R *.dfm}

//procedure TfrmReceivableInput.HJJE();
//var
//  MPrice, MQty, MOtherFee, MOtherFee1, MDeduction, MAmount: double;
//begin
//  MPrice := Strtofloatdef(price.Text, 0);
//  MQty := Strtofloatdef(Qty.Text, 0);
//  MOtherFee := Strtofloatdef(OtherFee.Text, 0);
//  MOtherFee1 := Strtofloatdef(OtherFee1.Text, 0);
//  MDeduction := Strtofloatdef(Deduction.Text, 0);
//  MAmount := Strtofloatdef(Amount.Text, 0);
//
////  if MQty * MPrice >= 0 then
//  begin
//    MAmount := RoundFloat((MQty * MPrice + MOtherFee + MOtherFee1 - MDeduction), 2);
//  end;
////  Price.Text := FloatToStr(MPrice);
////  Qty.Text := FloatToStr(MQty);
////  OtherFee.Text := FloatToStr(MOtherFee);
////  Deduction.Text := FloatToStr(MDeduction);
//  Amount.Text := FloatToStr(MAmount);
//end;

procedure TfrmReceivableInput.InitGrid();
begin
  try

    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select * from Finance_Flow where ISNULL(FROMID1,FFID)=''' + Trim(FFFID) + '''');
//      sql.Add(' select * from Finance_Flow where FromID1=''' + Trim(FFFID) + '''');
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);

    if not ADOQueryMain.IsEmpty then
    begin
      SCSHData(ADOQueryMain, ScrollBox1, 0);
      OppCoName.Properties.LookupItems.Text := Trim(ADOQueryMain.FieldByName('OppCoNo').AsString);
      OurCoName.Properties.LookupItems.Text := Trim(ADOQueryMain.FieldByName('OurCoNo').AsString);
      OppParentCoName.Properties.LookupItems.Text := Trim(ADOQueryMain.FieldByName('OppParentCoNo').AsString);
    end
    else
    begin
      FFTime.Date := SGetServerDateTime(ADOQueryTemp);
      ReconciliationDate.Date := SGetServerDateTime(ADOQueryTemp);
      ExchangeRate.Text := '1';
    end;

  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmReceivableInput.OppCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '客户';
      CANSHU4 := Self.canshu4;
      if self.FAuthority = '低权限' then
        FAuthority := '理单业务';

      if ShowModal = 1 then
      begin
        OppCoName.Text := Trim(CDS_1.fieldbyname('CoName').AsString);
        OppCoName.Properties.LookupItems.Text := Trim(CDS_1.fieldbyname('CoCode').AsString);
        OppParentCoName.Text := Trim(CDS_1.fieldbyname('CoName').AsString);
        OppParentCoName.Properties.LookupItems.Text := Trim(CDS_1.fieldbyname('CoCode').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmReceivableInput.OppParentCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '客户';
      if ShowModal = 1 then
      begin
        OppParentCoName.Text := Trim(CDS_1.fieldbyname('CoName').AsString);
        OppParentCoName.Properties.LookupItems.Text := Trim(CDS_1.fieldbyname('CoCode').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmReceivableInput.OrderNoBtnUpClick(Sender: TObject);
begin
  try
    frmFinTradePlanSel := TfrmFinTradePlanSel.Create(Application);
    with frmFinTradePlanSel do
    begin
      if ShowModal = 1 then
      begin
        with frmFinTradePlanSel.CDS_1 do
        begin
          self.ConNo.Text := Trim(frmFinTradePlanSel.CDS_1.fieldbyname('ConNo').AsString);
          self.OrderNo.Text := Trim(frmFinTradePlanSel.CDS_1.fieldbyname('OrderNo').AsString);
        end;

      end;
    end;
  finally
    frmFinTradePlanSel.Free;
  end;
end;

procedure TfrmReceivableInput.OrderNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmFinTradePlanSel := TfrmFinTradePlanSel.Create(Application);
    with frmFinTradePlanSel do
    begin
      if Self.FAuthority = '低权限' then
        FAuthority := '拥有者';
      if ShowModal = 1 then
      begin
        with frmFinTradePlanSel.CDS_1 do
        begin

          self.ConNo.Text := Trim(frmFinTradePlanSel.CDS_1.fieldbyname('ConNo').AsString);
          self.OrderNo.Text := Trim(frmFinTradePlanSel.CDS_1.fieldbyname('OrderNo').AsString);
//          self.F_Code.Text := Trim(frmFinTradePlanSel.CDS_1.fieldbyname('C_Code').AsString);
          self.OppCoName.Text := Trim(frmFinTradePlanSel.CDS_1.fieldbyname('CustName').AsString);
           //  多选指示单信息
          First;
          while Locate('SSel', True, []) do
          begin
            with Self.CDS_1 do
            begin
              Append;
              Self.CDS_1.FieldByName('QtyUnit').AsString := frmFinTradePlanSel.CDS_1.FieldByName('OrdUnit').AsString;
              Self.CDS_1.FieldByName('F_Name').AsString := frmFinTradePlanSel.CDS_1.FieldByName('C_Name').AsString;

              Self.CDS_1.FieldByName('F_code').AsString := frmFinTradePlanSel.CDS_1.FieldByName('C_code').AsString;
              Self.CDS_1.FieldByName('F_ColorNo').AsString := frmFinTradePlanSel.CDS_1.FieldByName('C_ColorNo').AsString;
              Self.CDS_1.FieldByName('F_Color').AsString := frmFinTradePlanSel.CDS_1.FieldByName('C_Color').AsString;
              Self.CDS_1.FieldByName('Price').AsString := frmFinTradePlanSel.CDS_1.FieldByName('ProcessingPrice').AsString;
              Self.CDS_1.FieldByName('Currency').AsString := frmFinTradePlanSel.CDS_1.FieldByName('OrdUnit').AsString;
              Self.CDS_1.FieldByName('Qty').AsString := frmFinTradePlanSel.CDS_1.FieldByName('OrdQty').AsString;
              Self.CDS_1.FieldByName('QtyUnit').AsString := frmFinTradePlanSel.CDS_1.FieldByName('OrdUnit').AsString;
              Self.CDS_1.post;
            end;
            frmFinTradePlanSel.CDS_1.Delete;
          end;
           //  多选指示单信息
          with ADO2 do
          begin
            Filtered := False;
            Close;
            SQL.Clear;
            sql.Add(' select * from Finance_Flow where OrderNo=''' + Trim(OrderNo.Text) + '''');
            Open;
          end;
          SCreateCDS(ADO2, CDS_2);
          SInitCDSData(ADO2, CDS_2);
          if CDS_2.IsEmpty then
          begin
            with CDS_2 do
            begin
              while not eof do
              begin
                CDS_1.Append;
                CDS_1.FieldByName('F_Name').AsString := FieldByName('F_Name').AsString;
                CDS_1.FieldByName('F_ColorNo').AsString := FieldByName('F_ColorNo').AsString;
                CDS_1.FieldByName('F_Color').AsString := FieldByName('F_Color').AsString;
                CDS_1.FieldByName('Currency').AsString := FieldByName('Currency').AsString;
                CDS_1.FieldByName('Qty').AsString := FieldByName('Qty').AsString;
                CDS_1.FieldByName('QtyUnit').AsString := FieldByName('QtyUnit').AsString;
                CDS_1.post;
                Next;
              end;
            end;
          end;

        end;

      end;
    end;
  finally
    frmFinTradePlanSel.Free;
  end;
end;

procedure TfrmReceivableInput.OurCoNameDblClick(Sender: TObject);
begin
  TcxButtonEdit(Sender).Text := '';
  TcxButtonEdit(Sender).Properties.LookupItems.Text := '';
end;

procedure TfrmReceivableInput.OurCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '我司';

      if ShowModal = 1 then
      begin
        OurCoName.Text := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
        OurCoName.Properties.LookupItems.Text := Trim(CDS_1.fieldbyname('CoCode').AsString);
        OppCoName.Properties.LookupItems.Text := Trim(CDS_1.fieldbyname('CoName').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmReceivableInput.FormDestroy(Sender: TObject);
begin
  frmReceivableInput := nil;
end;

procedure TfrmReceivableInput.PayMentBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

procedure TfrmReceivableInput.PayMentBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TcxTextEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      MainType := '应收款';
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        TcxTextEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

//procedure TfrmReceivableInput.QtyExit(Sender: TObject);
//begin
//  HJJE();
//end;

procedure TfrmReceivableInput.ConNoBtnUpClick(Sender: TObject);
begin
  try
    frmFinTradeSalesContractSel := TfrmFinTradeSalesContractSel.Create(Application);
    with frmFinTradeSalesContractSel do
    begin
      if ShowModal = 1 then
      begin

        self.ConNo.Text := Trim(frmFinTradeSalesContractSel.CDS_1.fieldbyname('ConNo').AsString);

      end;
    end;
  finally
    frmFinTradeSalesContractSel.Free;
  end;
end;

procedure TfrmReceivableInput.ConNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmFinTradeSalesContractSel := TfrmFinTradeSalesContractSel.Create(Application);
    with frmFinTradeSalesContractSel do
    begin
      if Self.FAuthority = '低权限' then
        FAuthority := '拥有者';
      if ShowModal = 1 then
      begin
        with frmFinTradePlanSel.CDS_1 do
        begin
          self.ConNo.Text := Trim(frmFinTradeSalesContractSel.CDS_1.fieldbyname('ConNo').AsString);
//          self.F_Code.Text := Trim(frmFinTradeSalesContractSel.CDS_1.fieldbyname('C_Code').AsString);
          Fieldbyname('F_code').value := trim(frmFinTradePlanSel.CDS_1.Fieldbyname('F_code').AsString);
          Fieldbyname('F_Name').value := trim(frmFinTradePlanSel.CDS_1.Fieldbyname('F_Name').AsString);
          Fieldbyname('F_Pattern').value := trim(frmFinTradePlanSel.CDS_1.Fieldbyname('F_Pattern').AsString);
          Fieldbyname('F_ColorNo').value := trim(frmFinTradePlanSel.CDS_1.Fieldbyname('F_ColorNo').AsString);
          Fieldbyname('F_Width').value := trim(frmFinTradePlanSel.CDS_1.Fieldbyname('F_Width').AsString);
          Fieldbyname('F_GramWeight').value := trim(frmFinTradePlanSel.CDS_1.Fieldbyname('F_GramWeight').AsString);
          Fieldbyname('F_Color').value := trim(frmFinTradePlanSel.CDS_1.Fieldbyname('F_Color').AsString);
        end;

      end;
    end;
  finally
    frmFinTradeSalesContractSel.Free;
  end;
end;

procedure TfrmReceivableInput.ConNoPropertiesChange(Sender: TObject);
begin
  with ADO2 do
  begin
    Filtered := False;
    Close;
    SQL.Clear;
    sql.Add(' select * from Finance_Flow where ConNo=''' + Trim(ConNo.Text) + '''');
    Open;
  end;
  SCreateCDS(ADO2, CDS_2);
  SInitCDSData(ADO2, CDS_2);
  if CDS_2.IsEmpty then
  begin
    with CDS_2 do
    begin
      while not eof do
      begin
        CDS_1.Append;
        CDS_1.FieldByName('F_Name').AsString := FieldByName('F_Name').AsString;
        CDS_1.FieldByName('F_ColorNo').AsString := FieldByName('F_ColorNo').AsString;
        CDS_1.FieldByName('F_Color').AsString := FieldByName('F_Color').AsString;
        CDS_1.FieldByName('Currency').AsString := FieldByName('Currency').AsString;
        CDS_1.FieldByName('Qty').AsString := FieldByName('Qty').AsString;
        CDS_1.FieldByName('QtyUnit').AsString := FieldByName('QtyUnit').AsString;
        CDS_1.post;
        Next;
      end;
    end;
  end;

end;

procedure TfrmReceivableInput.FFAbstractPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TcxTextEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      MainType := '应收款';
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        TcxTextEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmReceivableInput.FormClose(Sender: TObject; var Action: TCloseAction);
begin

  Action := caFree;
end;

procedure TfrmReceivableInput.TBCloseClick(Sender: TObject);
begin

  close;
end;

procedure TfrmReceivableInput.FormShow(Sender: TObject);
begin
  if Trim(FFFID) = '' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select top 1 * from BS_Company where CoType=''我司'' AND VALID=''Y''  order by CoCode ');
      Open;
    end;
    OurCoName.Text := Trim(ADOQueryTemp.fieldbyname('CoAbbrName').AsString);
    OurCoName.Properties.LookupItems.Text := Trim(ADOQueryTemp.fieldbyname('CoCode').AsString);
  end;
  InitGrid();

  with ADO_PM do
  begin
    Close;
    sql.Clear;
    sql.Add(' SELECT distinct A.C_Code,A.C_Name,A.qtyunit,A.HotQty,StkQty = SUM(B.StkQty) ');
    SQL.ADD(' FROM Bs_Cloth_Info A  LEFT JOIN Bs_Cloth_Io B on A.C_Code=B.C_Code');
    sql.add(' where B.ioflag=''入库'' ');
    if Trim(canshu4) = '新余' then
      sql.Add(' and isnull(STKNAME,'''')=''新余'' ');
    SQL.add('group by A.C_Code,A.C_Name,A.qtyunit,A.HotQty');
    SQL.ADD('order by  A.C_Code ');
//    showmessage(SQL.text);
    Open;
  end;
  with ADO_YS do
  begin
    Close;
    sql.Clear;
    sql.Add(' SELECT distinct A.C_Code,B.C_Color,B.C_ColorNo,B.referencePrice AS Price  FROM  Bs_Cloth_Info A inner join BS_Cloth_Info_Sub B on A.BCIID=B.BCIID ');
    sql.Add(' where  1=2  ');
    Open;
  end;

  with ADO_DanWei do
  begin
    Close;
    sql.Clear;
    sql.Add(' SELECT distinct A.C_Code,B.CICUnit  FROM  Bs_Cloth_Info A inner join Bs_Cloth_Info_Coefficient B on A.BCIID=B.BCIID ');
    sql.Add(' where 2=1  ');
    Open;
  end;
end;

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

function TfrmReceivableInput.SaveData(): Boolean;
var
  MaxId, MaxSubId, FCoCode, FCCID: string;
begin

  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_1 do
    begin
      First;
      while not Eof do
      begin
  //        if Trim(FFFID) = '' then
        if Trim(CDS_1.fieldbyname('FFID').AsString) = '' then
        begin
          if not GetLSNo(ADOQueryCmd, MaxId, 'YS', 'Finance_Flow', 4, 1) then
            raise Exception.Create('取最大号失败！');
        end
        else
        begin
          MaxId := Trim(CDS_1.fieldbyname('FFID').AsString);//Trim(FFFID);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
//            sql.Add('select * from Finance_Flow where FFID=''' + Trim(FFFID) + '''');
          sql.Add('select * from Finance_Flow where FFID=''' + Trim(CDS_1.fieldbyname('FFID').AsString) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(CDS_1.fieldbyname('FFID').AsString) = '' then     //Trim(FFFID) = ''
          begin
            Append;
            FieldByName('FillId').Value := Trim(DCode);
            FieldByName('Filler').Value := Trim(DName);
            FieldByName('FFFlag').Value := '应收收';
            FieldByName('FFType').Value := '应收款登记';
            FieldByName('status').Value := '0';
            FieldByName('FFQtyFlag').Value := 1;
          end
          else
          begin
            Edit;
            FieldByName('EditId').Value := Trim(DCode);
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('Edittime').Value := SGetServerDateTime(ADOQueryTemp);
          end;
          FieldByName('Store').Value := Trim(canshu4);
          FieldByName('OppCoNo').Value := Trim(OppCoName.Properties.LookupItems.Text);
          FieldByName('OurCoNo').Value := Trim(OurCoName.Properties.LookupItems.Text);
          FieldByName('OppParentCoNo').Value := Trim(OppParentCoName.Properties.LookupItems.Text);
          FieldByName('FFID').Value := Trim(MaxId);
          RTSetsavedata(ADOQueryCmd, 'Finance_Flow', ScrollBox1, 0);

          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_1, 'Finance_Flow', 0);
          Post;

        end;
        Next;
      end;

    end;

    ADOQueryCmd.Connection.CommitTrans;
    FCCID := Trim(MaxSubId);
    Result := True;
  except
    Result := false;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

//procedure TfrmReceivableInput.ScrollBox1Click(Sender: TObject);
//begin
//  HJJE();
//end;

procedure TfrmReceivableInput.ToolButton1Click(Sender: TObject);
begin
  with CDS_1 do
  begin
    Append;

    FieldByName('QtyUnit').Value := 'Kg';
    FieldByName('Amount').Value := 0;
    FieldByName('Qty').Value := 0;
    FieldByName('Price').Value := 0;
    FieldByName('Currency').Value := 'CNY';
    Post;
  end;

end;

procedure TfrmReceivableInput.ToolButton2Click(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    exit;
  if application.MessageBox('确定要删除吗', '提示', 1) = 2 then
    exit;
  with ADOQueryTemp do
  begin
    close;
    sql.clear;
    sql.add('delete from Finance_Flow where FFID=''' + trim(CDS_1.fieldbyname('FFID').asstring) + ''' ');
    execsql;
  end;
  CDS_1.delete;
end;

procedure TfrmReceivableInput.ToolButton3Click(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if Trim(OppCoName.Text) = '' then
  begin
    Application.MessageBox('对方单位不能为空!', '提示', 0);
    Exit;
  end;
  if Trim(OurCoName.Text) = '' then
  begin
    Application.MessageBox('我方单位不能为空!', '提示', 0);
    Exit;
  end;

  if CDS_1.Locate('Currency', null, []) then
  begin
    application.MessageBox('币种不能为空', '提示');
    exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    ModalResult := 1;
  end;
end;

procedure TfrmReceivableInput.Tv1Column1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  mvalue := TcxTextEdit(Sender).EditingText;

  with CDS_1 do
  begin

    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
    ADO_PM.Locate('C_Name', mvalue, []);
    Edit;
    FieldByName('F_Code').Value := ADO_PM.FieldByName('C_Code').Value;
    FieldByName('QtyUnit').Value := ADO_PM.FieldByName('qtyunit').Value;

    Post;
  end;
  with ADO_YS do
  begin
    Close;
    sql.Clear;
    sql.Add(' SELECT DISTINCT A.C_Code,B.C_Color,B.C_ColorNo,B.referencePrice AS Price FROM  Bs_Cloth_Info A inner join BS_Cloth_Info_Sub B on A.BCIID=B.BCIID ');
    sql.Add(' where A.C_Code= ' + QuotedStr(CDS_1.FieldByName('F_Code').AsString));
    SQL.ADD(' order by B.C_ColorNo');
         //ShowMessage(sql.text);
    Open;
  end;
end;

procedure TfrmReceivableInput.Tv1Column2PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FIncludeTax: Double;
begin
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add(' select isnull(IncludeTax,0)IncludeTax from Bs_Company a    ');
    sql.Add('where CoCode=' + QuotedStr(OppCoName.Properties.LookupItems.Text));
    open;
  end;

  FIncludeTax := ADOQueryTemp.Fieldbyname('IncludeTax').asfloat;

  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  mvalue := TcxTextEdit(Sender).EditingText;

  with CDS_1 do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
    ADO_YS.Locate('C_ColorNo', mvalue, []);
    Edit;
    FieldByName('F_Color').Value := ADO_YS.FieldByName('C_Color').Value;
    FieldByName('price').Value := ADO_YS.FieldByName('price').Value * (1 + (FIncludeTax / 100));
    FieldByName('referencePrice').Value := ADO_YS.FieldByName('Price').Value;
    Post;
  end;

end;

procedure TfrmReceivableInput.Tv1CustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
var
  FValue: string;
  FBounds: TRect;
begin
  if not (AViewInfo is TcxGridIndicatorRowItemViewInfo) then
    Exit;

  // 计算行号（从 1 开始）
  FValue := IntToStr(TcxGridIndicatorRowItemViewInfo(AViewInfo).GridRecord.Index + 1);

  // 绘制背景和边框
  FBounds := AViewInfo.ContentBounds;
  ACanvas.FillRect(FBounds);
  ACanvas.DrawComplexFrame(FBounds, clBtnHighlight, clBtnShadow, [bLeft, bBottom, bRight], 1);

  // 绘制文本
  InflateRect(FBounds, -3, -2);
  ACanvas.Font.Color := clWhite;
  ACanvas.Brush.Style := bsClear;
  ACanvas.DrawText(FValue, FBounds, cxAlignCenter or cxAlignTop);

  ADone := True;
end;

procedure TfrmReceivableInput.v1QtyPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FQty, FPrice, FAmount: double;
begin
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  with CDS_1 do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
    FQty := FieldbyName('Qty').AsFloat;
    FPrice := FieldByName('price').AsFloat;
  end;

  FAmount := strtofloat(format('%.2f', [FQty * FPrice]));

  with CDS_1 do
  begin
    Edit;

    FieldByName('Amount').Value := FAmount;

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

end;

end.

