unit U_CollectInput;

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, dxSkinsCore, dxSkinsDefaultPainters, dxDateRanges,
  dxBarBuiltInMenu, U_BaseList, cxContainer, cxCurrencyEdit, dxSkinWXI;

type
  TfrmCollectInput = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ToolButton3: TToolButton;
    ScrollBox1: TScrollBox;
    Label2: TLabel;
    Label7: TLabel;
    Note: TMemo;
    Label8: TLabel;
    Currency: TComboBox;
    PayMent: TBtnEditC;
    OppCoName: TBtnEditC;
    Label5: TLabel;
    OurCoName: TBtnEditC;
    Label9: TLabel;
    OppParentCoName: TBtnEditC;
    Amount: TcxCurrencyEdit;
    Label3: TLabel;
    Label18: TLabel;
    OurBankName: TBtnEditC;
    Label19: TLabel;
    OppBankName: TBtnEditC;
    Label20: TLabel;
    FFTime: TDateTimePicker;
    Label22: TLabel;
    OrderNo: TBtnEditC;
    Label23: TLabel;
    buyConNo: TBtnEditC;
    Label24: TLabel;
    FFAbstract: TBtnEditC;
    Label1: TLabel;
    DKAmount: TcxCurrencyEdit;
    Label4: TLabel;
    ExchangeRate: TcxCurrencyEdit;
    Label6: TLabel;
    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 OppCoNameBtnUpClick(Sender: TObject);
    procedure OppParentCoNameBtnUpClick(Sender: TObject);
    procedure OurCoNameBtnUpClick(Sender: TObject);
    procedure OurBankNameBtnUpClick(Sender: TObject);
    procedure OppBankNameBtnUpClick(Sender: TObject);
    procedure OrderNoBtnUpClick(Sender: TObject);
    procedure DKAmountExit(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    function SaveData(): Boolean;
  public
    { Public declarations }
    FFFID,PState: string;
  end;

var
  frmCollectInput: TfrmCollectInput;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_CompanySel, U_BankSel, U_TradePlanSel;

{$R *.dfm}

procedure TfrmCollectInput.InitGrid();
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, 0);
      OppCoName.TxtCode := Trim(ADOQueryMain.FieldByName('OppCoNo').AsString);
      OurCoName.TxtCode := Trim(ADOQueryMain.FieldByName('OurCoNo').AsString);
      OppParentCoName.TxtCode := Trim(ADOQueryMain.FieldByName('OppParentCoNo').AsString);
      OurBankName.TxtCode := Trim(ADOQueryMain.FieldByName('OurBankID').AsString);

    end
    else
    begin
      FFTime.DateTime := SGetServerDateTime(ADOQueryTemp);
    end;

    if PState='复制' then
    begin
       FFFID:='';

       DKAmount.text:='';
       Amount.text:='';
    end;

  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCollectInput.OppBankNameBtnUpClick(Sender: TObject);
begin

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

procedure TfrmCollectInput.OppCoNameBtnUpClick(Sender: TObject);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '客户';
      if ShowModal = 1 then
      begin
        OppCoName.Text := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
        OppCoName.TxtCode := Trim(CDS_1.fieldbyname('CoCode').AsString);
        OppParentCoName.Text := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
        OppParentCoName.TxtCode := Trim(CDS_1.fieldbyname('CoCode').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmCollectInput.OppParentCoNameBtnUpClick(Sender: TObject);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '客户';
      if ShowModal = 1 then
      begin
        OppParentCoName.Text := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
        OppParentCoName.TxtCode := Trim(CDS_1.fieldbyname('CoCode').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmCollectInput.OrderNoBtnUpClick(Sender: TObject);
begin
  try
    frmTradePlanSel := TfrmTradePlanSel.Create(Application);
    with frmTradePlanSel do
    begin
//      FMainID := MainID.Text;
      if ShowModal = 1 then
      begin
        Self.OrderNO.Text := CDS_1.fieldbyname('OrderNo').value;
        Self.OurCoName.Text := Trim(CDS_1.fieldbyname('OurCoName').AsString);
        Self.OurCoName.TxtCode := Trim(CDS_1.fieldbyname('OurCoNo').AsString);
        Self.BuyConNo.Text := Trim(CDS_1.fieldbyname('BuyConNo').AsString);
      end;
    end;
  finally
    frmTradePlanSel.Free;
  end;
end;

procedure TfrmCollectInput.OurBankNameBtnUpClick(Sender: TObject);
begin
  try
    frmBankSel := TfrmBankSel.Create(Application);
    with frmBankSel do
    begin
      if ShowModal = 1 then
      begin
        OurBankName.Text := Trim(CDS_1.fieldbyname('BankName').AsString);
        OurBankName.TxtCode := Trim(CDS_1.fieldbyname('BkID').AsString);
      end;
    end;
  finally
    frmBankSel.Free;
  end;
end;

procedure TfrmCollectInput.OurCoNameBtnUpClick(Sender: TObject);
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.TxtCode := Trim(CDS_1.fieldbyname('CoCode').AsString);
        OurBankName.Text := Trim(CDS_1.fieldbyname('BankName').AsString);
        OurBankName.TxtCode := Trim(CDS_1.fieldbyname('BkID').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmCollectInput.FormDestroy(Sender: TObject);
begin
  frmCollectInput := nil;
end;

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

procedure TfrmCollectInput.PayMentBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TEdit(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
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCollectInput.DKAmountExit(Sender: TObject);
var
  MDKAmount, MHL, MAmount: double;
begin
  if Currency.text = '' then
  begin
    Application.MessageBox('币种不能为空!', '提示', 0);
    Exit;
  end;

  MDKAmount := Strtofloatdef(DKAmount.Text, 0);
  MHL := Strtofloatdef(ExchangeRate.Text, 0);

  if Currency.text = 'USD' then
  begin
    if MDKAmount / MHL <> 0 then
    begin
      MAmount := RoundFloat((MDKAmount / MHL), 2);
    end;
  end
  else if Currency.text = 'CNY' then
  begin

    if MDKAmount * MHL <> 0 then
    begin
      MAmount := RoundFloat((MDKAmount * MHL), 2);
    end;
  end;
//  Price.Text := FloatToStr(MPrice);
//  Qty.Text := FloatToStr(MQty);
//  OtherFee.Text := FloatToStr(MOtherFee);
//  Deduction.Text := FloatToStr(MDeduction);
  Amount.Text := FloatToStr(MAmount);
end;

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

  Action := caFree;
end;

procedure TfrmCollectInput.TBCloseClick(Sender: TObject);
begin

  close;
end;

procedure TfrmCollectInput.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=''我司''  order by CoCode ');
//      Open;
//    end;
//    OurCoName.Text := Trim(ADOQueryTemp.fieldbyname('CoAbbrName').AsString);
//    OurCoName.TxtCode := Trim(ADOQueryTemp.fieldbyname('CoCode').AsString);
//  end;
  ExchangeRate.Text := '1';
  InitGrid();
end;

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

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

    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('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('OppCoNo').Value := Trim(OppCoName.TxtCode);
      FieldByName('OurCoNo').Value := Trim(OurCoName.TxtCode);
      FieldByName('OppParentCoNo').Value := Trim(OppParentCoName.TxtCode);
      FieldByName('OurBankID').Value := Trim(OurBankName.TxtCode);
      FieldByName('FFID').Value := Trim(MaxId);
      RTSetsavedata(ADOQueryCmd, 'Finance_Flow', ScrollBox1, 0);
      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 TfrmCollectInput.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 Trim(Currency.Text) = '' then
  begin
    Application.MessageBox('币种不能为空!', '提示', 0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    ModalResult := 1;
  end;
end;

end.

