unit U_PayableInput;

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;

type
  TfrmPayableInput = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ToolButton3: TToolButton;
    ScrollBox1: TScrollBox;
    Label2: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label1: TLabel;
    Label8: TLabel;
    Label5: TLabel;
    Label9: TLabel;
    Label3: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Label4: TLabel;
    Label20: TLabel;
    Label21: TLabel;
    Label22: TLabel;
    Label23: TLabel;
    Label24: TLabel;
    Label18: TLabel;
    F_Code: TcxTextEdit;
    F_Name: TcxTextEdit;
    Amount: TcxCurrencyEdit;
    Price: TcxCurrencyEdit;
    Qty: TcxCurrencyEdit;
    Piece: TcxCurrencyEdit;
    F_Width: TcxTextEdit;
    F_GramWeight: TcxTextEdit;
    F_Pattern: TcxTextEdit;
    F_Color: TcxTextEdit;
    F_ColorNo: TcxTextEdit;
    OtherFee: TcxCurrencyEdit;
    Deduction: TcxCurrencyEdit;
    OurCoName: TcxButtonEdit;
    OppCoName: TcxButtonEdit;
    OppParentCoName: TcxButtonEdit;
    FFTime: TcxDateEdit;
    ConNo: TcxButtonEdit;
    OrderNo: TcxButtonEdit;
    FFAbstract: TcxButtonEdit;
    PayMent: TcxButtonEdit;
    QtyUnit: TcxComboBox;
    Currency: TcxComboBox;
    IsInvoice: TcxComboBox;
    Note: TcxMemo;
    Label19: TLabel;
    OtherFee1: TcxCurrencyEdit;
    Label25: TLabel;
    ReconciliationDate: TcxDateEdit;
    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 QtyExit(Sender: TObject);
    procedure ScrollBox1Click(Sender: TObject);
    procedure ConNoBtnUpClick(Sender: TObject);
    procedure OrderNoBtnUpClick(Sender: TObject);
    procedure OurCoNameDblClick(Sender: TObject);
    procedure OurCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure OppCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure OppParentCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ConNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure OrderNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure FFAbstractPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    { Private declarations }
    procedure InitGrid();
    procedure HJJE();
    function SaveData(): Boolean;
  public
    { Public declarations }
    FFFID: string;
  end;

var
  frmPayableInput: TfrmPayableInput;

implementation

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

{$R *.dfm}

procedure TfrmPayableInput.HJJE();
var
  MPrice, MQty, MOtherFee, MOtherFee1, MDeduction, MAmount: double;
begin
  MPrice := Strtofloatdef(Price.Text, 0);
  MQty := Strtofloatdef(Qty.Text, 0);
  MOtherFee1 := Strtofloatdef(OtherFee1.Text, 0);
  MOtherFee := Strtofloatdef(OtherFee.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 TfrmPayableInput.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.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);
    end;

  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmPayableInput.OppCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      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 TfrmPayableInput.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 TfrmPayableInput.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 TfrmPayableInput.OrderNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
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 TfrmPayableInput.OurCoNameDblClick(Sender: TObject);
begin
  TcxButtonEdit(Sender).Text := '';
  TcxButtonEdit(Sender).Properties.LookupItems.Text := '';
end;

procedure TfrmPayableInput.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);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmPayableInput.FormDestroy(Sender: TObject);
begin
  frmPayableInput := nil;
end;

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

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

procedure TfrmPayableInput.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 TfrmPayableInput.ConNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
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 TfrmPayableInput.FFAbstractPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
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 TfrmPayableInput.FormClose(Sender: TObject; var Action: TCloseAction);
begin

  Action := caFree;
end;

procedure TfrmPayableInput.TBCloseClick(Sender: TObject);
begin

  close;
end;

procedure TfrmPayableInput.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.Properties.LookupItems.Text := Trim(ADOQueryTemp.fieldbyname('CoCode').AsString);
  end;
  InitGrid();
end;

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

function TfrmPayableInput.SaveData(): Boolean;
var
  MaxId, MaxSubId, FCoCode, FCCID: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(FFFID) = '' then
    begin
      if not GetLSNo(ADOQueryCmd, MaxId, 'YF', '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.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);
      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 TfrmPayableInput.ScrollBox1Click(Sender: TObject);
begin
  HJJE();
end;

procedure TfrmPayableInput.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.

