unit U_InvoiceOutSQInPut;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, cxStyles, cxCustomData, cxGraphics, cxFilter,
  cxData, cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, cxGridCustomPopupMenu, cxGridPopupMenu, ADODB,
  DBClient, ExtCtrls, StdCtrls, BtnEdit, cxButtonEdit, cxTextEdit,
  cxDropDownEdit, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, cxCalendar,
  dxDateRanges, dxBarBuiltInMenu, ComObj, dxScrollbarAnnotations, dxSkinsCore,
  math, dxSkinsDefaultPainters;

type
  TfrmInvoiceOutSQInPut = class(TForm)
    DataSource1: TDataSource;
    CDS_Invoice: TClientDataSet;
    GPM_1: TcxGridPopupMenu;
    ToolBar1: TToolBar;
    TSave: TToolButton;
    TBClose: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1P_CodeName: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ADOQueryTmp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    Tv1Column8: TcxGridDBColumn;
    ToolButton4: TToolButton;
    OpenDialog1: TOpenDialog;
    CDS_LM: TClientDataSet;
    ToolButton5: TToolButton;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TSaveClick(Sender: TObject);
    procedure v1P_CodeNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1P_SpecPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1QtyUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1QtyPropertiesEditValueChanged(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure Tv1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure v1Column5PropertiesEditValueChanged(Sender: TObject);
  private
    procedure initGrid();
    procedure SaveDate();
    { Private declarations }
  public
    canshu1, FIVID: string;
    fInvoiceFlag: string;
    { Public declarations }
  end;

var
  frmInvoiceOutSQInPut: TfrmInvoiceOutSQInPut;

implementation

uses
  U_RTFun, U_ZDYHelp, U_CompanySel, U_dataLink, U_ReceivableValueSel;
{$R *.dfm}

procedure TfrmInvoiceOutSQInPut.SaveDate();
var
  MaxNo: string;
begin
  ADOQueryCmd.Connection.BeginTrans;
  try
    with CDS_Invoice do
    begin
      first;
      while not eof do
      begin
        if fieldbyname('IVID').AsString = '' then
        begin
          if GetLSNo(ADOQueryTmp, MaxNo, 'IJ', 'Finance_Invoice', 4, 1) = False then
            raise Exception.Create('取最大号失败！');
        end
        else
          MaxNo := CDS_Invoice.fieldbyname('IVID').AsString;

        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.add('select * from Finance_Invoice_Apply where IVID=' + quotedstr(Trim(MaxNo)));
          open;
        end;
        if ADOQueryCmd.IsEmpty then
        begin
          ADOQueryCmd.append;
          ADOQueryCmd.FieldByName('FillId').Value := Trim(DCode);
          ADOQueryCmd.FieldByName('Filler').Value := Trim(DName);
          ADOQueryCmd.FieldByName('status').Value := '0';
        end
        else
        begin
          ADOQueryCmd.edit;
          ADOQueryCmd.FieldByName('EditId').Value := Trim(DCode);
          ADOQueryCmd.FieldByName('Editer').Value := Trim(DName);
          ADOQueryCmd.FieldByName('Edittime').Value := SGetServerDateTime(ADOQueryTmp);
        end;
        RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Invoice, 'Finance_Invoice_Apply', 0);
        ADOQueryCmd.fieldbyname('IVID').Value := MaxNo;
        ADOQueryCmd.fieldbyname('IVFlag').Value := '销项';
        ADOQueryCmd.fieldbyname('OppCoNo').Value := trim(CDS_Invoice.FieldByName('OppCoNo').AsString);
        ADOQueryCmd.fieldbyname('OppParentCoNo').Value := trim(CDS_Invoice.FieldByName('OppParentCoNo').AsString);
        ADOQueryCmd.fieldbyname('OurCoNo').Value := trim(CDS_Invoice.FieldByName('OurCoNo').AsString);
        ADOQueryCmd.fieldbyname('FromFFID').Value := trim(CDS_Invoice.FieldByName('FromFFID').AsString);
        ADOQueryCmd.Post;
        next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('保存成功！', '提示');
    Modalresult := 1;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmInvoiceOutSQInPut.initGrid();
begin
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from Finance_Invoice_Apply where IVID=''' + trim(FIVID) + '''');
//    sql.add(' and isnull(status,''0'')=''0''');
    open;
  end;

  SCreateCDS(ADOQueryTmp, CDS_Invoice);
  SInitCDSData(ADOQueryTmp, CDS_Invoice);
end;

procedure TfrmInvoiceOutSQInPut.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmInvoiceOutSQInPut.FormShow(Sender: TObject);
begin
  ReadCxGrid(Self.Caption, Tv1, '财务管理');
  with CDS_LM do
  begin
    FieldDefs.Clear;
    FieldDefs.Add('LXH', ftInteger, 0);
    FieldDefs.Add('lCode', ftString, 40);
    FieldDefs.Add('LName', ftString, 40);
    close;
    CreateDataSet;
  end;
  initGrid();
end;

procedure TfrmInvoiceOutSQInPut.TSaveClick(Sender: TObject);
begin

//  if CDS_Invoice.Locate('IVNo', null, []) then
//  begin
//    application.MessageBox('发票号不能为空', '提示');
//    exit;
//  end;
  if CDS_Invoice.Locate('IVType', null, []) then
  begin
    application.MessageBox('发票类型不能为空', '提示');
    exit;
  end;
  if CDS_Invoice.Locate('OurCoName', null, []) then
  begin
    application.MessageBox('我方单位不能为空', '提示');
    exit;
  end;
  if CDS_Invoice.Locate('OppCoName', null, []) then
  begin
    application.MessageBox('对方单位不能为空', '提示');
    exit;
  end;
  SaveDate();
end;

procedure TfrmInvoiceOutSQInPut.v1Column5PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FQty, FPrice, FAmount, Ftax, FTaxFee: double;
begin
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  with CDS_Invoice do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
    FQty := FieldbyName('Qty').AsFloat;
    FAmount := FieldbyName('Amount').AsFloat;
    FPrice := FieldbyName('Price').AsFloat;
    Ftax := FieldbyName('tax').AsFloat;
    FTaxFee := FieldbyName('TaxFee').AsFloat;
  end;
  if FQty <> 0 then
  begin
    FPrice := strtofloat(format('%.13f', [FAmount / FQty]));
//    FTaxFee := strtofloat(format('%.2f', [FPrice * FQty * (Ftax / 100)]));
//    FTaxFee := RoundTo((FPrice * FQty * (Ftax / 100)), -2);
  end
  else
  begin
    FPrice := 0;
    FTaxFee := strtofloat(format('%.2f', [FPrice * FQty * (Ftax / 100)]));
  end;

  with CDS_Invoice do
  begin
    Edit;
    FieldByName('Price').Value := FPrice;
    FieldByName('TaxFee').Value := FTaxFee;
    FieldByName('taxAmount').Value := FTaxFee + FAmount;
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();

end;

procedure TfrmInvoiceOutSQInPut.v1P_CodeNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'MPRTCodeName';
      flagname := '产品名称';
      if ShowModal = 1 then
      begin
        Self.CDS_Invoice.Edit;
        Self.CDS_Invoice.FieldByName('F_Name').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
//        Self.CDS_Invoice.FieldByName('P_Code').Value := Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  //self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmInvoiceOutSQInPut.v1P_SpecPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'P_Spec';
      flagname := '成品规格';
      if ShowModal = 1 then
      begin
        Self.CDS_Invoice.Edit;
        Self.CDS_Invoice.FieldByName('P_Spec').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmInvoiceOutSQInPut.v1QtyUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'QtyUnit';
      flagname := '数量单位';
      if ShowModal = 1 then
      begin
        Self.CDS_Invoice.Edit;
        Self.CDS_Invoice.FieldByName('QtyUnit').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmInvoiceOutSQInPut.v1QtyPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FQty, FPrice, FAmount, Ftax, FTaxFee: double;
begin
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  with CDS_Invoice do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
    FQty := FieldbyName('Qty').AsFloat;
    FAmount := FieldbyName('Amount').AsFloat;
    FPrice := FieldbyName('Price').AsFloat;
    Ftax := FieldbyName('tax').AsFloat;
  end;
  if FQty <> 0 then
  begin
    FPrice := strtofloat(format('%.13f', [FAmount / FQty]));
//    FTaxFee := strtofloat(format('%.2f', [FPrice * FQty * (Ftax / 100)]));
    FTaxFee := RoundTo((FPrice * FQty * (Ftax / 100)), -2);
  end
  else
  begin
    FPrice := 0;
    FTaxFee := strtofloat(format('%.2f', [FPrice * FQty * (Ftax / 100)]));
  end;

  with CDS_Invoice do
  begin
    Edit;
    FieldByName('Price').Value := FPrice;
    FieldByName('TaxFee').Value := FTaxFee;
    FieldByName('taxAmount').Value := FTaxFee + FAmount;
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;
//procedure TfrmInvoiceOutInPut.v1QtyPropertiesEditValueChanged(Sender: TObject);
//var
//  mvalue, FFieldName: string;
//  FQty, FPrice, FAmount, Ftax, FtaxAmount: double;
//begin
//  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
//  mvalue := TcxTextEdit(Sender).EditingText;
//  if Trim(mvalue) = '' then
//  begin
//    mvalue := '0';
//  end;
//  with CDS_Invoice do
//  begin
//    Edit;
//    FieldByName(FFieldName).Value := Trim(mvalue);
//    Post;
//    FQty := FieldbyName('Qty').AsFloat;
//    FtaxAmount := FieldbyName('taxAmount').AsFloat;
//    Ftax := FieldbyName('tax').AsFloat;
//  end;
//  if FQty <> 0 then
//  begin
//    FAmount := strtofloat(format('%.2f', [FtaxAmount / (1 + Ftax / 100)]));
//    FPrice := strtofloat(format('%.2f', [FAmount / FQty]));
//  end
//  else
//  begin
//    FPrice := 0;
//    FAmount := strtofloat(format('%.2f', [FtaxAmount / (1 + Ftax / 100)]));
//  end;
//
//  with CDS_Invoice do
//  begin
//    Edit;
//    FieldByName('Price').Value := FPrice;
//    FieldByName('Amount').Value := FAmount;
//    FieldByName('TaxFee').Value := FtaxAmount - FAmount;
//    Post;
//  end;
//  tv1.Controller.EditingController.ShowEdit();
//
//end;

procedure TfrmInvoiceOutSQInPut.FormDestroy(Sender: TObject);
begin
  frmInvoiceOutSQInPut := nil;
end;

procedure TfrmInvoiceOutSQInPut.Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '我司';
      if ShowModal = 1 then
      begin

        Self.CDS_Invoice.Edit;
        Self.CDS_Invoice.FieldByName('OurCoNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
        Self.CDS_Invoice.FieldByName('OurCoName').Value := Trim(CDS_1.fieldbyname('CoName').AsString);

      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmInvoiceOutSQInPut.Tv1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'InvoiceType';
      flagname := '发票类型';
      if ShowModal = 1 then
      begin
        Self.CDS_Invoice.Edit;
        Self.CDS_Invoice.FieldByName('IVType').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmInvoiceOutSQInPut.Tv1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      if Self.canshu1 = '录入' then
        FAuthority := '理单业务';
      FCoType := '客户';
      if ShowModal = 1 then
      begin
        Self.CDS_Invoice.Edit;
        Self.CDS_Invoice.FieldByName('OppCoNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
        Self.CDS_Invoice.FieldByName('OppCoName').Value := Trim(CDS_1.fieldbyname('CoName').AsString);
        Self.CDS_Invoice.FieldByName('OppCoNameZ').Value := Trim(CDS_1.fieldbyname('CoName').AsString);
        Self.CDS_Invoice.FieldByName('OppParentCoNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
        Self.CDS_Invoice.FieldByName('OppParentCoName').Value := Trim(CDS_1.fieldbyname('CoName').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmInvoiceOutSQInPut.Tv1Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '客户';
      if ShowModal = 1 then
      begin
        Self.CDS_Invoice.Edit;
        Self.CDS_Invoice.FieldByName('OppParentCoNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
        Self.CDS_Invoice.FieldByName('OppParentCoName').Value := Trim(CDS_1.fieldbyname('CoName').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmInvoiceOutSQInPut.ToolButton1Click(Sender: TObject);
var
  maxno: string;
begin
  CopyAddRow(Tv1, CDS_Invoice);
  with CDS_Invoice do
  begin
    CDS_Invoice.Edit;
    CDS_Invoice.FieldByName('QtyUnit').Value := 'Kg';
    CDS_Invoice.FieldByName('Amount').Value := 0;
    CDS_Invoice.FieldByName('Qty').Value := 0;
    CDS_Invoice.FieldByName('tax').Value := 0;
    CDS_Invoice.FieldByName('TaxFee').Value := 0;
    CDS_Invoice.FieldByName('taxAmount').Value := 0;
    CDS_Invoice.FieldByName('Price').Value := 0;
    CDS_Invoice.FieldByName('tax').Value := 13;
    CDS_Invoice.FieldByName('Currency').Value := 'CNY';

    CDS_Invoice.FieldByName('IVDate').Value := SGetServerDate(ADOQueryTmp);
    CDS_Invoice.Post;
  end;

end;
//begin
//  CDS_Invoice.Append;
//
//  CDS_Invoice.FieldByName('QtyUnit').Value := 'Kg';
//  CDS_Invoice.FieldByName('Amount').Value := 0;
//  CDS_Invoice.FieldByName('Qty').Value := 0;
//  CDS_Invoice.FieldByName('tax').Value := 0;
//  CDS_Invoice.FieldByName('TaxFee').Value := 0;
//  CDS_Invoice.FieldByName('taxAmount').Value := 0;
//  CDS_Invoice.FieldByName('Price').Value := 0;
//  CDS_Invoice.FieldByName('tax').Value := 13;
//  CDS_Invoice.FieldByName('Currency').Value := 'CNY';
//  CDS_Invoice.Post;
//
//end;

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

procedure TfrmInvoiceOutSQInPut.ToolButton3Click(Sender: TObject);
begin
  WriteCxGrid(Self.Caption, Tv1, '财务管理');
end;

procedure TfrmInvoiceOutSQInPut.ToolButton4Click(Sender: TObject);
var
  excelApp, WorkBook: Variant;
  i, j, k, LX, ExcelRowCount: integer;
  maxId, FCPID, FCPName, t1, t2, t3, FFID: string;
begin

  try
    excelApp := CreateOleObject('Excel.Application');
    openDialog1.Filter := '*.CSV;*.xls';
    if opendialog1.Execute then
    begin
      WorkBook := excelApp.WorkBooks.Open(OpenDialog1.FileName);
    end
    else
      exit;
    excelApp.Visible := false;
    ExcelRowCount := WorkBook.WorkSheets[1].UsedRange.Rows.Count;
  except
    application.MessageBox('加载EXCEL错误！', '错误信息', MB_ICONERROR);
    exit;
  end;

  CDS_LM.EmptyDataSet;

  for j := 0 to Tv1.ColumnCount - 1 do
  begin
    with CDS_LM do
    begin
      Append;
      FieldByName('LCode').Value := trim(Tv1.Columns[j].DataBinding.FieldName);
      FieldByName('LName').Value := trim(Tv1.Columns[j].Caption);
      Post;
    end;
  end;

  try
    for i := 1 to 50 do
    begin
      if trim(WorkBook.WorkSheets[1].Cells[1, i].value) = '' then
        continue;
      if CDS_LM.Locate('LName', trim(WorkBook.WorkSheets[1].Cells[1, i].value), []) then
      begin
        with CDS_LM do
        begin
          Edit;
          FieldByName('LXH').Value := i;
          Post;
        end;
      end;
    end;
  except
    application.MessageBox('加载数组错误！', '错误信息', MB_ICONERROR);
    exit;
  end;

  try
    for i := 2 to ExcelRowCount do
    begin
      with CDS_Invoice do
      begin
        Append;
        CDS_LM.First;
        while not CDS_LM.Eof do
        begin
          if CDS_LM.FieldByName('LXH').AsInteger > 0 then
            CDS_Invoice.fieldbyname(CDS_LM.FieldByName('LCode').AsString).Value := WorkBook.WorkSheets[1].Cells[i, CDS_LM.FieldByName('LXH').AsInteger].Value;
          CDS_LM.Next;
        end;
//        CDS_Invoice.fieldbyname('OurCoNo').Value := '91330602MA2JTEXX4B';
//        CDS_Invoice.fieldbyname('OurCoName').Value := '圆中圆';
        CDS_Invoice.FieldByName('Currency').Value := 'CNY';
        Post;
      end;
    end;
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
  except
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
    exit;
  end;

end;

procedure TfrmInvoiceOutSQInPut.ToolButton5Click(Sender: TObject);
var
  flag: Boolean;
begin
//  application.MessageBox('1111', '提示信息');
  frmReceivableValueSel := TfrmReceivableValueSel.Create(Application);
  with frmReceivableValueSel do
  begin
    if Self.canshu1 = '录入' then
      FAuthority := '低权限';
    if showmodal = 1 then
    begin
      flag := False;
      while CDS_1.locate('SSel', true, []) do
      begin
        with self.CDS_Invoice do
        begin
          with ADOQueryTmp do
          begin
            close;
            sql.Clear;
            sql.Add('select count(*) as RecordCount from Finance_Invoice_Apply where FromFFID=''' + trim(frmReceivableValueSel.CDS_1.Fieldbyname('FFID').AsString) + '''');
            open;
          end;
          if (ADOQueryTmp.FieldByName('RecordCount').AsInteger < 1) and not (CDS_Invoice.Locate('FromFFID', trim(frmReceivableValueSel.CDS_1.Fieldbyname('FFID').AsString), [])) then
          begin
            Append;
            FieldByName('SSel').Value := false;
            Fieldbyname('OurCoName').value := trim(frmReceivableValueSel.CDS_1.Fieldbyname('OurCoName').AsString);
            Fieldbyname('OppCoName').value := trim(frmReceivableValueSel.CDS_1.Fieldbyname('OppCoName').AsString);
            Fieldbyname('OppParentCoName').value := trim(frmReceivableValueSel.CDS_1.Fieldbyname('OppParentCoName').AsString);
            Fieldbyname('F_Code').value := trim(frmReceivableValueSel.CDS_1.Fieldbyname('F_Code').AsString);
            Fieldbyname('F_Name').value := trim(frmReceivableValueSel.CDS_1.Fieldbyname('F_Name').AsString);
            Fieldbyname('Qty').value := trim(frmReceivableValueSel.CDS_1.Fieldbyname('Qty').AsString);
            Fieldbyname('QtyUnit').value := trim(frmReceivableValueSel.CDS_1.Fieldbyname('QtyUnit').AsString);
            Fieldbyname('Price').value := trim(frmReceivableValueSel.CDS_1.Fieldbyname('Price').AsString);

            Fieldbyname('Amount').value := frmReceivableValueSel.CDS_1.Fieldbyname('Amount').AsCurrency;
            Fieldbyname('TaxAmount').value := frmReceivableValueSel.CDS_1.Fieldbyname('Amount').AsCurrency * 1.13;
            FieldByName('tax').Value := 13;
            FieldByName('TaxFee').Value := Fieldbyname('TaxAmount').value - Fieldbyname('Amount').value;

            Fieldbyname('Currency').value := trim(frmReceivableValueSel.CDS_1.Fieldbyname('Currency').AsString);
            Fieldbyname('IVDate').value := SGetServerDate(ADOQueryTemp);
            Fieldbyname('FromFFID').value := trim(frmReceivableValueSel.CDS_1.Fieldbyname('FFID').AsString);
            Post;
          end
          else
          begin
            flag := True;
          end;
        end;
        CDS_1.Edit;
        CDS_1.fieldbyname('SSel').value := false;
      end;
      if flag then
      begin
        application.MessageBox('选取内容中存在重复应收款记录，重复的记录程序没有录入', '提示信息');
      end;
    end;
    free;
  end;
end;

end.

