unit U_CollectInput2;

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, dxSkinsCore, dxSkinsDefaultPainters, dxCore, cxDateUtils,
  dxScrollbarAnnotations, Vcl.Menus, Clipbrd, dxCoreGraphics, dxSkinWXI, cxLabel;

type
  TfrmCollectInput2 = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ToolButton3: TToolButton;
    ScrollBox1: TScrollBox;
    Label7: TLabel;
    Note: TMemo;
    Currency: TcxComboBox;
    Label2: TLabel;
    Label3: TLabel;
    Label5: TLabel;
    Amount: TcxTextEdit;
    Label6: TLabel;
    Label8: TLabel;
    OurBankCardNo: TcxTextEdit;
    OurBankName: TcxButtonEdit;
    OppCoName: TcxButtonEdit;
    FFTime: TcxDateEdit;
    Label13: TLabel;
    Panel1: TPanel;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    Tv1CoName: TcxGridDBColumn;
    Tv1BankName: TcxGridDBColumn;
    Tv1BankNo: TcxGridDBColumn;
    Tv1ShuiNo: TcxGridDBColumn;
    PM_1: TPopupMenu;
    GPM_1: TcxGridPopupMenu;
    N1: TMenuItem;
    Label15: TLabel;
    IVNo: TcxButtonEdit;
    oppShuiNo: TcxTextEdit;
    FFID: TEdit;
    cxLookAndFeelController1: TcxLookAndFeelController;
    cxLabel1: TcxLabel;
    OurCoName: TcxButtonEdit;
    OppParentCoName: TcxButtonEdit;
    cxLabel2: TcxLabel;
    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 OppCoNameBtnUpClick(Sender: TObject);
    procedure OurBankNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure AmountPropertiesEditValueChanged(Sender: TObject);
    procedure TaxNumberPropertiesChange(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure ScrollBox1Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure cxButtonEdit1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure OppCoNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    { Private declarations }
    procedure InitGrid();
    function SaveData(): Boolean;
    procedure InitGrid2();
  public
    { Public declarations }
    FFFID: string;
    FoppShuiNoChange: string;
  end;

var
  frmCollectInput2: TfrmCollectInput2;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_CompanySel, U_BankSel, U_FinTradePlanSel,
  U_FinTradeSalesContractSel, U_ZDYBankSel, U_GetableSel;

{$R *.dfm}

procedure TfrmCollectInput2.InitGrid();
var
  nAmount: Double;
begin
  try

    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select * from Finance_Flow where FFID=''' + Trim(FFFID) + '''');
      Open;
    end;
    if not ADOQueryMain.IsEmpty then
    begin
      SCSHData(ADOQueryMain, ScrollBox1, 2);
      if ADOQueryMain.FieldByName('Amount').AsString <> '' then
      begin
        nAmount := ADOQueryMain.FieldByName('Amount').AsFloat;
        Amount.Text := FormatFloat('#,##0.00', nAmount);
      end;
    end
    else
    begin
      FFTime.Date := SGetServerDateTime(ADOQueryTemp);
    end;

  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCollectInput2.InitGrid2();
begin

  try
    ADOQueryCmd.DisableControls;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.* from BS_Company A where 1=1 ');
      sql.Add(' and A.CoType=''供应商'' ');
      SQL.Add(' and ShuiNo like ''%' + oppShuiNo.Text + '%'' ');
      Open;
    end;

    SCreateCDS(ADOQueryCmd, CDS_1);
    SInitCDSData(ADOQueryCmd, CDS_1);
  finally
    ADOQueryCmd.EnableControls;
    TV1.DataController.Filter.Clear;
  end;
end;

procedure TfrmCollectInput2.N1Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_1.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmCollectInput2.OppCoNameBtnUpClick(Sender: TObject);
begin

  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'OppCoName';
      flagname := '对方单位';
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

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

procedure TfrmCollectInput2.OurBankNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TcxButtonEdit(Sender).Hint);
  FWZ := Pos('/', fsj); //先英文再中文
  try
    frmZDYBankSel := TfrmZDYBankSel.Create(Application);
    with frmZDYBankSel do
    begin
      MainType := '财务银行';
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        OurBankName.Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        OurBankCardNo.Text := Trim(ClientDataSet1.fieldbyname('note').AsString);
      end;
    end;
  finally
    frmZDYBankSel.Free;
  end;
end;

procedure TfrmCollectInput2.FormDestroy(Sender: TObject);
begin
  frmCollectInput2 := nil;
end;

procedure TfrmCollectInput2.AmountPropertiesEditValueChanged(Sender: TObject);
var
  n: Double;
  CleanText: string;
  DecimalPos: Integer;
  DecimalDigits: Integer;
  FormatStr: string;
  CaretPos: Integer;
  TextBeforeCaret: string;
begin
  CaretPos := Amount.SelStart;
  TextBeforeCaret := Copy(Amount.Text, 1, CaretPos);

  CleanText := StringReplace(Amount.Text, ',', '', [rfReplaceAll]);

  if TryStrToFloat(CleanText, n) then
  begin
    Amount.Properties.OnEditValueChanged := nil;
    try
      DecimalPos := Pos('.', CleanText);
      if DecimalPos > 0 then
        DecimalDigits := Length(CleanText) - DecimalPos
      else
        DecimalDigits := 0;

      if DecimalDigits = 0 then
        FormatStr := '#,##0'
      else
        FormatStr := '#,##0.' + StringOfChar('0', DecimalDigits);

      Amount.Text := FormatFloat(FormatStr, n);

      if CaretPos > 0 then
      begin
        CaretPos := Length(TextBeforeCaret) + (StringReplace(Copy(Amount.Text, 1, Length(TextBeforeCaret)), ',', '', [rfReplaceAll]).Length - TextBeforeCaret.Length);
        if CaretPos <= Length(Amount.Text) then
          Amount.SelStart := CaretPos;
      end;
    finally
      Amount.Properties.OnEditValueChanged := AmountPropertiesEditValueChanged;
    end;
  end;
end;

procedure TfrmCollectInput2.cxButtonEdit1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  TotalTaxInclude: Double;
begin
  try
    frmGetableSel := TfrmGetableSel.Create(Application);
    with frmGetableSel do
    begin
      cxTabControl1.TabIndex := 0;
      if ShowModal <> 1 then Exit;

      if FFFIDs = '' then
      begin
        Application.MessageBox('未选择任何应收款！', '提示', MB_OK + MB_ICONWARNING);
        Exit;
      end;

      FoppShuiNoChange := '0';

      Self.IVNo.Properties.LookupItems.Text := FFFIDs;
      Self.IVNo.Text := FFFIDs;

      TotalTaxInclude := FTaxInclude;
      Self.Amount.Text := FormatFloat('#,##0.00', TotalTaxInclude);

      if not frmGetableSel.CDS_1.IsEmpty then
      begin
        frmGetableSel.CDS_1.First;
        Self.OppCoName.Text := frmGetableSel.CDS_1.FieldByName('OppCoName').AsString;
        Self.OppCoName.Properties.LookupItems.Text := frmGetableSel.CDS_1.FieldByName('OppCoNo').AsString;
        Self.oppShuiNo.Text := frmGetableSel.CDS_1.FieldByName('oppShuiNo').AsString;
        Self.Currency.Text := frmGetableSel.CDS_1.FieldByName('Currency').AsString;
      end;

      FoppShuiNoChange := '1';
    end;
  finally
    frmGetableSel.Free;
  end;
end;

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

  Action := caFree;
end;

procedure TfrmCollectInput2.TaxNumberPropertiesChange(Sender: TObject);
begin
  if FoppShuiNoChange <> '0' then
  begin
    InitGrid2();
    Panel1.Visible := True;
  end;

end;

procedure TfrmCollectInput2.TBCloseClick(Sender: TObject);
begin

  close;
end;

procedure TfrmCollectInput2.FormShow(Sender: TObject);
begin
  InitGrid();
  Panel1.Visible := False;
end;

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

function TfrmCollectInput2.SaveData(): Boolean;
var
  MaxId, MaxSubId, FCoCode, FCCID: string;
  CleanAmount: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(FFFID) = '' then
    begin
      if not GetLSNo(ADOQueryCmd, MaxId, 'SK', 'Finance_Flow', 4, 1) then
        raise Exception.Create('取最大号失败！');
    end
    else
    begin
      MaxId := Trim(FFFID);
    end;

    FFID.Text := MaxId;
    CleanAmount := StringReplace(Amount.Text, ',', '', [rfReplaceAll]);

    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from Finance_Flow where FFID=''' + Trim(FFFID) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FFFID) = '' then
      begin
        Append;
        FieldByName('FillId').Value := Trim(DCode);
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('Filltime').Value := SGetServerDateTime(ADOQueryTemp);
        FieldByName('FFFlag').Value := '应收收';
        FieldByName('FFType').Value := '收款登记';
        FieldByName('ourShuiNo').Value := '1102083046';
//        FieldByName('OurCoName').Value := 'DESHINE VIETNAM CO., LTD';
        FieldByName('OurCoNo').Value := 'W0001';
        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('OppCoNo').Value := trim(OppCoName.Properties.LookupItems.Text);
      FieldByName('IVID').Value := trim(IVNO.Properties.LookupItems.Text);
      if CleanAmount <> '' then
        FieldByName('Amount').AsFloat := StrToFloatDef(CleanAmount, 0);

      RTSetsavedata(ADOQueryCmd, 'Finance_Flow', ScrollBox1, 2);
      Post;
    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 TfrmCollectInput2.ScrollBox1Click(Sender: TObject);
begin
  Panel1.Visible := False;
end;

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

  if Trim(Currency.Text) = '' then
  begin
    Application.MessageBox('币种不能为空!', '提示', 0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    ModalResult := 1;
  end;
end;

procedure TfrmCollectInput2.Tv1DblClick(Sender: TObject);
begin
  OppCoName.Text := Trim(CDS_1.fieldbyname('CoName').AsString);
  OppCoName.Properties.LookupItems.Text := Trim(CDS_1.fieldbyname('CoCode').AsString);
  oppShuiNo.Text := Trim(CDS_1.fieldbyname('ShuiNo').AsString);

  Panel1.Visible := false;
end;

end.

