unit U_InvoiceOutList;

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, cxPC, RM_e_Xls, Menus, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu, dxDateRanges, U_BaseList,
  Vcl.Clipbrd, cxContainer, dxCore, cxDateUtils, dxSkinsCore,
  dxSkinsDefaultPainters, dxScrollbarAnnotations, cxProgressBar, cxCurrencyEdit,
  dxSkinWXI;

type
  TfrmInvoiceOutList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_Invoice: TDataSource;
    CDS_Invoice: TClientDataSet;
    TADD: TToolButton;
    TUPDATE: TToolButton;
    TBDEL: TToolButton;
    TCHK: TToolButton;
    TNOCHK: TToolButton;
    cxTabControl1: TcxTabControl;
    TSel: TToolButton;
    RMXLSExport1: TRMXLSExport;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ToolButton4: TToolButton;
    ADOQueryPrint: TADOQuery;
    ToolButton3: TToolButton;
    N3: TMenuItem;
    ToolButton5: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column3: TcxGridDBColumn;
    Tv1IVNo: TcxGridDBColumn;
    Tv1BGDNum: TcxGridDBColumn;
    Tv1IVDate: TcxGridDBColumn;
    Tv1SupplierShuiNo: TcxGridDBColumn;
    Tv1Supplier: TcxGridDBColumn;
    Tv1OurShuiNo: TcxGridDBColumn;
    Tv1OurCoName: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    Tv1Amount: TcxGridDBColumn;
    Tv1TaxFee: TcxGridDBColumn;
    Tv1TaxAmount: TcxGridDBColumn;
    Tv1BillCycle: TcxGridDBColumn;
    Tv1PayDate: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Chker: TcxGridDBColumn;
    Tv1Chktime: TcxGridDBColumn;
    Tv1PayStatus: TcxGridDBColumn;
    Tv1PayTime: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label3: TLabel;
    Label5: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label2: TLabel;
    Label4: TLabel;
    F_Name: TcxTextEdit;
    IVNo: TcxTextEdit;
    F_Spec: TcxTextEdit;
    Supplier: TcxTextEdit;
    OurCoName: TcxTextEdit;
    begdate: TcxDateEdit;
    Enddate: TcxDateEdit;
    ToolButton6: TToolButton;
    Tv1FIAbstract: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure IVNoChange(Sender: TObject);
    procedure TADDClick(Sender: TObject);
    procedure TUPDATEClick(Sender: TObject);
    procedure TBDELClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TCHKClick(Sender: TObject);
    procedure TNOCHKClick(Sender: TObject);
    procedure TSelClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure v1Column14PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure SaveFlowInvoice(const IVID, FFFIDs: string);
  private
    { Private declarations }

    procedure InitGrid();
    procedure SetStatus();
  public
    FZKType, canshu1, canshu2: string;
    { Public declarations }
  end;

//var
//  frmInvoiceXX: TfrmInvoiceXX;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_InvoiceOutInPut, U_LabelPrint,
  U_AttachmentUpload, U_GetableSel;

{$R *.dfm}

procedure TfrmInvoiceOutList.SetStatus();
begin
  tchk.Visible := false;
  tnochk.Visible := false;
  TUPDATE.Visible := false;
  tbdel.Visible := false;

  if trim(DParameters1) <> '高权限' then
  begin
    case cxTabControl1.TabIndex of
      0:
        begin
          TUPDATE.Visible := true;
          tbdel.Visible := true;
          TCHK.Visible := true;
        end;
      1:
        begin
          tnochk.Visible := true;

        end;
      2:
        begin
        end;
    end;
  end
  else
  begin
    case cxTabControl1.TabIndex of
      0:
        begin
          tchk.Visible := true;
          TUPDATE.Visible := true;
          tbdel.Visible := true;
          TCHK.Visible := true;
        end;
      1:
        begin
          tnochk.Visible := true;

        end;
      2:
        begin
        end;
    end;
  end;

end;

procedure TfrmInvoiceOutList.InitGrid();
begin
  try
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.*   ');
      sql.Add(',FJFlag=CAST((CASE WHEN (SELECT COUNT(X.FileName) FROM FJ_File X where X.TFType=''销项发票'' and X.WBID=A.IVNo)>0 THEN 1 ELSE 0 END) AS BIT)');
      sql.Add(',checkStatus=CASE WHEN isnull(A.Status,''0'')=''0'' THEN ''未审核'' WHEN A.Status=''9'' THEN ''已审核'' ELSE '''' END');
      sql.Add(',PayStatus=CASE WHEN EXISTS(SELECT 1 FROM Finance_Flow F WHERE F.FFType=''收款登记'' AND F.IVID=A.IVID) THEN ''已收款'' ELSE ''未收款'' END');
      sql.Add(',PayTime=(SELECT F.FFTime FROM Finance_Flow F WHERE F.FFType=''收款登记'' AND F.IVID=A.IVID)');
      sql.Add('  from Finance_Invoice A ');
      sql.add(' where A.IVDate>=''' + formatdatetime('yyyy-MM-dd', begdate.Date) + ''' ');
      sql.Add(' and A.IVDate<''' + formatdateTime('yyyy-MM-dd', enddate.Date + 1) + ''' ');
      sql.Add(' and IVFlag=''销项'' ');
      case cxTabControl1.TabIndex of
        0:
          begin
            sql.add(' and isnull(A.Status,''0'')=''0''');
          end;
        1:
          begin
            sql.add(' and A.Status=''9''');
          end;
      end;

      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_Invoice);
    SInitCDSData(ADOQueryMain, CDS_Invoice);
  finally
    TV1.DataController.Filter.Clear;
  end;
end;

procedure TfrmInvoiceOutList.FormDestroy(Sender: TObject);
begin
  inherited;
//  frmInvoiceXX := nil;
end;

procedure TfrmInvoiceOutList.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  Action := caFree;
end;

procedure TfrmInvoiceOutList.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmInvoiceOutList.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(self.Caption, Tv1, '财务管理');
  Enddate.Date := SGetServerDate(ADOQueryTemp);
  begdate.Date := Enddate.Date - 30;

  SetStatus();
  InitGrid();
end;

procedure TfrmInvoiceOutList.TBRafreshClick(Sender: TObject);
begin
  begdate.SetFocus;
  InitGrid();
end;

procedure TfrmInvoiceOutList.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_Invoice);
    SInitCDSData(ADOQueryMain, CDS_Invoice);
  end;
end;

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

procedure TfrmInvoiceOutList.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxgrid1);
end;

procedure TfrmInvoiceOutList.IVNoChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmInvoiceOutList.TADDClick(Sender: TObject);
begin
  frmInvoiceOutInPut := TfrmInvoiceOutInPut.Create(self);
  with frmInvoiceOutInPut do
  begin
    FIVID := '';
    FZKType := Self.FZKType;

    if showmodal = 1 then
    begin
      initGrid();
    end;
    free;
  end;

end;

procedure TfrmInvoiceOutList.TUPDATEClick(Sender: TObject);
begin
  frmInvoiceOutInPut := TfrmInvoiceOutInPut.Create(self);
  with frmInvoiceOutInPut do
  begin
    IvNo.Enabled := False;
    FIVID := trim(Self.CDS_Invoice.fieldbyname('IVID').asstring);

    if showmodal = 1 then
    begin
      Self.initGrid();
    end;
    free;
  end;

end;

procedure TfrmInvoiceOutList.TBDELClick(Sender: TObject);
var
  FIVIDS: string;
begin
  if CDS_Invoice.IsEmpty then
    exit;

  if not CDS_Invoice.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据', '提示');
    Exit;
  end;
  if application.MessageBox('确定要删除发票吗？', '提示', 1) = 2 then
    exit;
  with CDS_Invoice do
  begin
    DisableControls;
    while CDS_Invoice.Locate('SSel', True, []) do
    begin
      FIVIDS := FIVIDS + Trim(CDS_Invoice.fieldbyname('IVID').AsString) + ',';

      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
        sql.Add(' ' + quotedstr(trim(DName)));
        sql.Add(',getdate() ');
        sql.Add(',' + quotedstr(trim(self.Caption)));
        sql.Add(',' + quotedstr(trim('销项发票删除')));
        sql.Add(',' + quotedstr(trim('发票号：' + trim(CDS_Invoice.FieldByName('ivNo').AsString))));
        sql.Add(',' + quotedstr(trim('成功')));
        sql.Add(')');
        execsql;
      end;

      CDS_Invoice.Delete;
    end;
    EnableControls;
  end;

  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.add('delete from Finance_Invoice');
      sql.add('where  IVFlag=''销项''  ');
      sql.add('and exists(select * from [dbo].[F_Tool_SplitString](' + quotedstr(FIVIDS) + ','','') X where X.RTVALUE=Finance_Invoice.IVID  )');
      execsql;
    end;
    application.MessageBox('删除成功', '提示');
  except
    application.MessageBox('删除失败', '提示');
    InitGrid();
  end;
end;

procedure TfrmInvoiceOutList.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  initGrid();
end;

procedure TfrmInvoiceOutList.TCHKClick(Sender: TObject);
var
  FIVIDS: string;
begin
  if CDS_Invoice.IsEmpty then
    exit;

  if not CDS_Invoice.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据', '提示');
    Exit;
  end;

  with CDS_Invoice do
  begin
    DisableControls;
    while CDS_Invoice.Locate('SSel', True, []) do
    begin
      FIVIDS := FIVIDS + Trim(CDS_Invoice.fieldbyname('IVID').AsString) + ',';
      CDS_Invoice.Delete;
    end;
    EnableControls;
  end;

  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.add('update Finance_Invoice set Chker=''' + trim(DName) + ''',Chktime=getdate(),status=''9'' ');
      sql.add('where  IVFlag=''销项''  ');
      sql.add('and exists(select * from [dbo].[F_Tool_SplitString](' + quotedstr(FIVIDS) + ','','') X where X.RTVALUE=Finance_Invoice.IVID  )');
      execsql;
    end;
    application.MessageBox('审核成功', '提示');
  except
    application.MessageBox('审核失败', '提示');
    InitGrid();
  end;
end;

procedure TfrmInvoiceOutList.TNOCHKClick(Sender: TObject);
var
  FIVIDS: string;
begin
  if CDS_Invoice.IsEmpty then
    exit;

  if not CDS_Invoice.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据', '提示');
    Exit;
  end;

  with CDS_Invoice do
  begin
    DisableControls;
    while CDS_Invoice.Locate('SSel', True, []) do
    begin
      FIVIDS := FIVIDS + Trim(CDS_Invoice.fieldbyname('IVID').AsString) + ',';
      CDS_Invoice.Delete;
    end;
    EnableControls;
  end;

  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.add('update Finance_Invoice set Chker=null,Chktime=null,status=''0'' ');

      sql.add('where  IVFlag=''销项''  ');
      sql.add('and exists(select * from [dbo].[F_Tool_SplitString](' + quotedstr(FIVIDS) + ','','') X where X.RTVALUE=Finance_Invoice.IVID  )');
      execsql;
    end;
    application.MessageBox('撤销成功', '提示');
  except
    application.MessageBox('撤销失败', '提示');
    InitGrid();
  end;
end;

procedure TfrmInvoiceOutList.TSelClick(Sender: TObject);
begin
  frmInvoiceOutInPut := TfrmInvoiceOutInPut.Create(self);
  with frmInvoiceOutInPut do
  begin
    TSave.Visible := False;
    FIvID := trim(Self.CDS_Invoice.fieldbyname('IvID').asstring);
    if showmodal = 1 then
    begin

    end;
  end;
end;

procedure TfrmInvoiceOutList.FormCreate(Sender: TObject);
begin
  inherited;
  canshu1 := trim(DParameters1);
end;

procedure TfrmInvoiceOutList.N1Click(Sender: TObject);
begin

  SelOKNo(CDS_Invoice, True);

end;

procedure TfrmInvoiceOutList.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Invoice, false);
end;

procedure TfrmInvoiceOutList.N3Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_Invoice.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));

end;

procedure TfrmInvoiceOutList.ToolButton4Click(Sender: TObject);
var
  MIvID: string;
begin
  if CDS_Invoice.IsEmpty then
    Exit;
  if CDS_Invoice.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  MIvID := '';
  CDS_Invoice.DisableControls;
  with CDS_Invoice do
  begin
    First;
    while CDS_Invoice.Locate('SSel', True, []) do
    begin

      MIvID := MIvID + ',' + (Trim(CDS_Invoice.fieldbyname('IvID').AsString));

      Edit;
      FieldByName('SSel').Value := False;
      Post;
    end;
  end;
  CDS_Invoice.EnableControls;

  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'InvoiceOut';
      FFiltration1 := MIvID;
      if ShowModal = 1 then
      begin
//        Self.InitGrid();
      end;
    end;
  finally
    frmLabelPrint.Free;
  end;
end;

procedure TfrmInvoiceOutList.ToolButton5Click(Sender: TObject);
begin
  if CDS_Invoice.IsEmpty then
    Exit;
  try
    frmAttachmentUpload := TfrmAttachmentUpload.Create(Application);
    with frmAttachmentUpload do
    begin
      fkeyNO := Trim(Self.CDS_Invoice.fieldbyname('IVNo').AsString);
      fType := '销项发票';
      FEditAuthority := True;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmAttachmentUpload.Free;
  end;
end;

procedure TfrmInvoiceOutList.ToolButton6Click(Sender: TObject);
var
  mIVId, mffIDS: string;
begin
  if CDS_Invoice.Locate('ssel', True, []) = false then
  begin
    Application.MessageBox('请先选择数据', '提示信息', MB_OK);
    Exit;
  end;
  try

    CDS_Invoice.Locate('ssel', True, []);

    mIVId := CDS_Invoice.fieldByName('IVId').AsString;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.add(' select * from Finance_Invoice where IVId = ' + QuotedStr(Trim(mIVId)));
      Open;
    end;

    frmGetableSel := TfrmGetableSel.Create(Application);
    with frmGetableSel do
    begin
      if ShowModal = 1 then
      begin
        with frmGetableSel.CDS_1 do
        begin
          self.ADOQueryCmd.Edit;
//          self.ADOQueryCmd.FieldByName('Supplier').AsString := frmPayableSel.CDS_1.FieldByName('OppCoName').AsString;
//          self.ADOQueryCmd.FieldByName('SupplierNo').AsString := frmPayableSel.CDS_1.FieldByName('OppCoNo').AsString;
//          self.ADOQueryCmd.FieldByName('SupplierShuiNo').AsString := frmPayableSel.CDS_1.FieldByName('oppShuiNo').AsString;
//          self.ADOQueryCmd.FieldByName('OurCoName').AsString := frmPayableSel.CDS_1.FieldByName('OurCoName').AsString;
//          self.ADOQueryCmd.FieldByName('OurCoNo').AsString := frmPayableSel.CDS_1.FieldByName('OurCoNo').AsString;
//          self.ADOQueryCmd.FieldByName('OurShuiNo').AsString := frmPayableSel.CDS_1.FieldByName('ourShuiNo').AsString;

//          self.ADOQueryCmd.FieldByName('Currency').AsString := frmPayableSel.CDS_1.FieldByName('Currency').AsString;
//          self.ADOQueryCmd.FieldByName('BillCycle').AsString := frmPayableSel.CDS_1.FieldByName('BillCycle').AsString;

//          if frmPayableSel.CDS_1.FieldByName('NoTaxPrice').AsString <> '' then
//          begin
//            Self.ADOQueryCmd.FieldByName('Price').value := frmPayableSel.CDS_1.FieldByName('NoTaxPrice').AsString;
//          end
//          else
//          begin
//            Self.ADOQueryCmd.FieldByName('Price').value := 0;
//          end;
//          Self.ADOQueryCmd.FieldByName('Amount').value := frmPayableSel.FNoTaxInclude;
//          Self.ADOQueryCmd.FieldByName('TaxFee').value := frmPayableSel.FTax;
//          Self.ADOQueryCmd.FieldByName('TaxAmount').value := frmPayableSel.FTaxInclude;
          self.ADOQueryCmd.FieldByName('fromFlow').Value := frmGetableSel.FFFIDs;
          mffIDS := frmGetableSel.FFFIDs;

          with ADOQueryTemp do
          begin
            close;
            sql.Clear;
            sql.add('UPDATE Finance_FLOW SET  NoTaxPrice =' + (Trim(CDS_Invoice.FieldByName('PRICE').AsString)));
            SQL.Add(',Currency=' + QuotedStr(Trim(CDS_Invoice.FieldByName('Currency').AsString)));
            sql.Add(' where EXISTS (select SP.RTValue from [dbo].[F_Tool_SplitString](' + quotedstr(mffIDS) + ','','') SP where SP.RTValue=Finance_FLOW.FFID )');
          //  SQL.ADD('AND ISNULL(STATUS,''0'')=''0''');
            ExecSQL;
          end;
          with ADOQueryTemp do
          begin
            close;
            sql.Clear;
            sql.add('UPDATE Finance_FLOW SET NoTaxInclude=NoTaxPrice*QTY ');

            sql.Add(' where EXISTS (select SP.RTValue from [dbo].[F_Tool_SplitString](' + quotedstr(mffIDS) + ','','') SP where SP.RTValue=Finance_FLOW.FFID )');
           // SQL.ADD('AND ISNULL(STATUS,''0'')=''0''');
            ExecSQL;
          end;

          with ADOQueryTemp do
          begin
            close;
            sql.Clear;
            sql.add('UPDATE Finance_FLOW SET  Tax=NoTaxInclude*TaxRate/100 ');

            sql.Add(' where EXISTS (select SP.RTValue from [dbo].[F_Tool_SplitString](' + quotedstr(mffIDS) + ','','') SP where SP.RTValue=Finance_FLOW.FFID )');
           // SQL.ADD('AND ISNULL(STATUS,''0'')=''0''');
            ExecSQL;
          end;
          with ADOQueryTemp do
          begin
            close;
            sql.Clear;
            sql.add('UPDATE Finance_FLOW SET   TaxInclude=NoTaxInclude+Tax ');

            sql.Add(' where EXISTS (select SP.RTValue from [dbo].[F_Tool_SplitString](' + quotedstr(mffIDS) + ','','') SP where SP.RTValue=Finance_FLOW.FFID )');
           // SQL.ADD('AND ISNULL(STATUS,''0'')=''0''');
            ExecSQL;
          end;

          self.ADOQueryCmd.Post;
        end;
      end;
    end;
  finally
    frmGetableSel.Free;
    SaveFlowInvoice(mIVId, mffIDS);
  end;

end;

procedure TfrmInvoiceOutList.SaveFlowInvoice(const IVID, fffIDS: string);
begin
  if CDS_Invoice.IsEmpty then
    Exit;
  if fffIDS = '' then
    Exit;

  // 开始事务（假设使用ADO事务）
  ADOQueryCmd.Connection.BeginTrans;
  try
    CDS_Invoice.DisableControls;

        // 使用参数化查询
    with ADOQueryCmd do
    begin
      Close;
      SQL.Text := 'delete from Finance_FlowInvoice where IVID = :IVID';
      Parameters.ParamByName('IVID').Value := Trim(IVID);
      ExecSQL;

      Close;
      SQL.Text := 'exec P_InsertFinanceFlowInvoice @IVID = :IVID, @fffIDs = :fffIDs';
      Parameters.ParamByName('IVID').Value := Trim(IVID);
      Parameters.ParamByName('fffIDs').Value := Trim(fffIDS);
//          ShowMessage(sql.Text);
      ExecSQL;
    end;

    CDS_Invoice.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;  // 提交事务
//    ShowMessage('保存成功！');
  except
    ADOQueryCmd.Connection.RollbackTrans;  // 出错回滚
    raise;  // 重新抛出异常

  end;
end;

procedure TfrmInvoiceOutList.v1Column14PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FQty, FPrice, FMoney, Ftax, FtaxMoney: 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;
    FtaxMoney := FieldbyName('taxMoney').AsFloat;
    Ftax := FieldbyName('tax').AsFloat;
  end;
  if FQty <> 0 then
  begin

    FMoney := strtofloat(format('%.2f', [FtaxMoney / (1 + Ftax / 100)]));
    FPrice := strtofloat(format('%.2f', [FMoney / FQty]));
  end
  else
  begin
    FPrice := 0;
    FMoney := strtofloat(format('%.2f', [FtaxMoney / (1 + Ftax / 100)]));
  end;

  with CDS_Invoice do
  begin
    Edit;
    FieldByName('Price').Value := FPrice;
    FieldByName('Money').Value := FMoney;
    FieldByName('TaxFee').Value := FtaxMoney - FMoney;
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();

end;

end.

