unit U_PayableSel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxCalendar, cxDropDownEdit, ComCtrls, ToolWin,
  cxGridLevel, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxClasses, cxControls, cxGridCustomView, cxGrid, cxGridCustomPopupMenu,
  cxGridPopupMenu, ADODB, DBClient, cxButtonEdit, cxTextEdit, cxSpinEdit,
  StdCtrls, ExtCtrls, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxDateRanges, dxBarBuiltInMenu, U_BaseInput, System.ImageList, Vcl.ImgList,
  dxScrollbarAnnotations, dxSkinsCore, dxSkinsDefaultPainters, cxImageList,
  dxSkinOffice2013White, dxSkinSharpPlus, dxSkinWXI, cxCheckBox, Vcl.Menus,
  Clipbrd, cxCurrencyEdit, cxPC;

type
  TfrmPayableSel = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    TBSave: TToolButton;
    TBClose: TToolButton;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    GPM_1: TcxGridPopupMenu;
    ToolButton1: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    Tv1Column22: TcxGridDBColumn;
    Tv1FFTime: TcxGridDBColumn;
    TV1OurCoName: TcxGridDBColumn;
    Tv1OppCoName: TcxGridDBColumn;
    Tv1FFAbstract: TcxGridDBColumn;
    Tv1ConNo: TcxGridDBColumn;
    Tv1OrderNo: TcxGridDBColumn;
    Tv1BuyConNo: TcxGridDBColumn;
    Tv1F_Code: TcxGridDBColumn;
    Tv1F_Name: TcxGridDBColumn;
    Tv1F_Width: TcxGridDBColumn;
    Tv1F_GramWeight: TcxGridDBColumn;
    Tv1F_Color: TcxGridDBColumn;
    Tv1Piece: TcxGridDBColumn;
    Tv1Qty: TcxGridDBColumn;
    Tv1QtyUnit: TcxGridDBColumn;
    Tv1Currency: TcxGridDBColumn;
    Tv1NoTaxPrice: TcxGridDBColumn;
    Tv1NoTaxInclude: TcxGridDBColumn;
    Tv1TaxRate: TcxGridDBColumn;
    Tv1Tax: TcxGridDBColumn;
    Tv1TaxInclude: TcxGridDBColumn;
    Tv1Note: TcxGridDBColumn;
    Tv1FromID1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    TbOrdAdd: TToolButton;
    Tv1PurNo: TcxGridDBColumn;
    Tv1FinType: TcxGridDBColumn;
    TbPurSel: TToolButton;
    PM_1: TPopupMenu;
    N1: TMenuItem;
    N3: TMenuItem;
    N2: TMenuItem;
    TbSSel: TToolButton;
    cxTabControl1: TcxTabControl;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TBAddClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TbOrdAddClick(Sender: TObject);
    procedure TbPurSelClick(Sender: TObject);
    procedure Tv1OppCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1FFAbstractPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure N2Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure Tv1PurNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1OrderNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1CurrencyPropertiesChange(Sender: TObject);
    procedure Tv1TaxRatePropertiesEditValueChanged(Sender: TObject);
    procedure TbSSelClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
  private
    { Private declarations }
    function SaveData(): Boolean;
    procedure InitGrid();
  public
    { Public declarations }
    FBCId, FKHName: string;
    FFFID: string;
    FNoTaxInclude, FTax, FTaxInclude: Double; //不含税金额，税额，含税金额 ，选择应付款主键
    FFFIDs: string;
  end;

var
  frmPayableSel: TfrmPayableSel;

implementation

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

{$R *.dfm}

procedure TfrmPayableSel.cxTabControl1Change(Sender: TObject);
begin
//  SetStatus();
  InitGrid();
end;

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

procedure TfrmPayableSel.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      Filtered := False;
      sql.Add(' select  A.*,B.BillCycle ');
      sql.Add(' from Finance_Flow A ');
      SQL.Add(' left join BS_Company B on A.OppCoNo=B.CoCode');
      sql.Add(' where CheckStatus = ''已审核'' ');
//      sql.Add(' where 1=1 ');
      SQL.Add(' and FFFlag = ''应付付'' ');
      case cxTabControl1.TabIndex of
        0:
          begin
            sql.Add(' and not EXISTS ( select 1 from Finance_FlowInvoice X where X.FFID=A.FFID ) ');
          end;
        1:
          begin
            sql.Add(' and EXISTS ( select 1 from Finance_FlowInvoice X where X.FFID=A.FFID ) ');
          end;
      end;
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  finally
    ADOQueryMain.EnableControls;
    TV1.DataController.Filter.Clear;
  end;
end;

procedure TfrmPayableSel.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_1, True);

end;

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

procedure TfrmPayableSel.N3Click(Sender: TObject);
begin
  SelOKNo(CDS_1, false);

end;

function TfrmPayableSel.SaveData(): Boolean;
var
  Maxno, MFFID: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_1.DisableControls;
    with CDS_1 do
    begin
      First;
      while not eof do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Finance_Flow where FFID=''' + Trim(CDS_1.fieldbyname('FFID').AsString) + '''');
          open;
        end;
        MFFID := Trim(ADOQueryTemp.fieldbyname('FFID').AsString);
        if Trim(MFFID) = '' then
        begin
          if not GetLSNo(ADOQueryCmd, Maxno, 'YFK', 'Finance_Flow', 3, 1) then
            raise Exception.Create('取应收款编号失败！');
        end
        else
        begin
          Maxno := Trim(MFFID);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Finance_Flow where FFID=''' + Trim(Maxno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(MFFID) = '' then
          begin
            Append;
            FieldByName('Fillid').Value := Trim(Dcode);
            FieldByName('Filler').Value := Trim(DName);
          end
          else
          begin
            Edit;
            FieldByName('Editid').Value := Trim(Dcode);
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDate(ADOQueryTemp);
          end;
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_1, 'Finance_Flow', 0);

          FieldByName('FFID').Value := Maxno;
          FieldByName('FFFlag').Value := '应付付';
          FieldByName('FFType').Value := '应付款登记';
          FieldByName('CheckStatus').Value := '未审核';


          //织造单号
          FieldByName('OrdMainId').Value := CDS_1.fieldbyname('OrdMainId').Value;
          FieldByName('OrdSubId').Value := CDS_1.fieldbyname('OrdSubId').Value;
          //织造单号
          FieldByName('PurMID').Value := CDS_1.fieldbyname('PurMID').Value;
          FieldByName('PurSID').Value := CDS_1.fieldbyname('PurSID').Value;
          //坯布合同
          FieldByName('ConMId').Value := CDS_1.fieldbyname('ConMId').value;
          FieldByName('ConSId').Value := CDS_1.fieldbyname('ConSId').value;
          //面料合同
          FieldByName('BuyConMID').Value := CDS_1.fieldbyname('BuyConMID').value;
          FieldByName('BuyConSID').Value := CDS_1.fieldbyname('BuyConSID').Value;
          //对方单位
          FieldByName('OppCoNo').Value := CDS_1.fieldbyname('OppCoNo').Value;
          //我方单位
          FieldByName('OurCoNo').Value := CDS_1.fieldbyname('OurCoNo').Value;

          Post;
        end;

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('insert into Finance_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails)');
          sql.Add('values(''应付款批量登记'' ');
          sql.Add(',' + quotedstr(Maxno));
          sql.Add(',''保存''   ');
          sql.Add(',' + quotedstr(DName));
          sql.Add(',' + quotedstr(trim(self.Caption)));
          sql.Add(',' + quotedstr(Maxno));
          sql.Add(')  ');
          ExecSQL;
        end;
        Next;
      end;
    end;
    CDS_1.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    CDS_1.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmPayableSel.TBAddClick(Sender: TObject);
begin
  CDS_1.Append;
  CDS_1.FieldByName('FFTime').Value := SGetServerDateTime(ADOQueryCmd);
  CDS_1.FieldByName('ReconciliationDate').Value := SGetServerDate(ADOQueryCmd);
  CDS_1.FieldByName('OurCoName').Value := 'DESHINE VIETNAM CO., LTD';
  CDS_1.FieldByName('SSel').Value := False;
  CDS_1.Post;
end;

procedure TfrmPayableSel.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmPayableSel.TBDelClick(Sender: TObject);
begin
  CDS_1.Delete;
end;

procedure TfrmPayableSel.TbOrdAddClick(Sender: TObject);
begin
  try
    frmFinTradePlanSel := TfrmFinTradePlanSel.Create(Application);
    with frmFinTradePlanSel do
    begin
      if ShowModal = 1 then
      begin
        with frmFinTradePlanSel.CDS_1 do
        begin
          //织造单
          self.CDS_1.FieldByName('OrderNo').AsString := Trim(frmFinTradePlanSel.CDS_1.fieldbyname('OrderNo').AsString);
          self.CDS_1.FieldByName('OrdMainId').AsString := Trim(frmFinTradePlanSel.CDS_1.fieldbyname('MainId').AsString);
          self.CDS_1.FieldByName('OrdSubId').AsString := Trim(frmFinTradePlanSel.CDS_1.fieldbyname('SubId').AsString);
          //坯布合同
          self.CDS_1.FieldByName('ConNo').AsString := Trim(frmFinTradePlanSel.CDS_1.fieldbyname('ConNo').AsString);
          self.CDS_1.FieldByName('ConMId').AsString := Trim(frmFinTradePlanSel.CDS_1.fieldbyname('ConMId').AsString);
          self.CDS_1.FieldByName('ConSId').AsString := Trim(frmFinTradePlanSel.CDS_1.fieldbyname('ConSId').AsString);
          //面料合同
          self.CDS_1.FieldByName('BuyConNo').AsString := Trim(frmFinTradePlanSel.CDS_1.fieldbyname('BuyConNo').AsString);
          self.CDS_1.FieldByName('BuyConMID').AsString := Trim(frmFinTradePlanSel.CDS_1.fieldbyname('FROMCPSID').AsString);
          self.CDS_1.FieldByName('BuyConSID').AsString := Trim(frmFinTradePlanSel.CDS_1.fieldbyname('FROMCPMID').AsString);

          self.CDS_1.FieldByName('F_Code').AsString := Trim(frmFinTradePlanSel.CDS_1.fieldbyname('OrderNo').AsString);
          self.CDS_1.FieldByName('F_Name').AsString := Trim(frmFinTradePlanSel.CDS_1.fieldbyname('MainId').AsString);
          self.CDS_1.FieldByName('F_Width').AsString := Trim(frmFinTradePlanSel.CDS_1.fieldbyname('SubId').AsString);
          self.CDS_1.FieldByName('F_GramWeight').AsString := Trim(frmFinTradePlanSel.CDS_1.fieldbyname('OrderNo').AsString);
          self.CDS_1.FieldByName('F_Color').AsString := Trim(frmFinTradePlanSel.CDS_1.fieldbyname('MainId').AsString);

        end;

      end;
    end;
  finally
    frmFinTradePlanSel.Free;
  end;
end;

procedure TfrmPayableSel.FormShow(Sender: TObject);
var
  fsj: string;
begin
  inherited;
  ReadCxGrid('应付款选择', Tv1, '应付款选择');
  InitGrid();
end;

procedure TfrmPayableSel.TBSaveClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  if CDS_1.Locate('FFTime', null, []) = True then
  begin
    Application.MessageBox('登记日期不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_1.Locate('ReconciliationDate', null, []) = True then
  begin
    Application.MessageBox('对账日期不能为空!', '提示', 0);
    Exit;
  end;

  if CDS_1.Locate('FinType', null, []) = True then
  begin
    Application.MessageBox('请选择类型!', '提示', 0);
    Exit;
  end;

  if CDS_1.Locate('Qty', null, []) = True then
  begin
    Application.MessageBox('数量不能为空!', '提示', 0);
    Exit;
  end;

//  CDS_1.First;
//  while not CDS_1.Eof do
//  begin
//  // 同时判断两个条件
//    if (CDS_1.FieldByName('IOType').AsString = '车间领用') and (CDS_1.FieldByName('ToOrdNo').IsNull or (Trim(CDS_1.FieldByName('ToOrdNo').AsString) = '')) then
//    begin
//      Application.MessageBox('请选择车间领用 出到织造单!', '提示', 0);
//      Exit;
//    end;
//    CDS_1.Next;
//  end;

  if SaveData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    //ModalResult:=1;
    Exit;
  end;
end;

procedure TfrmPayableSel.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid('应付款选择', Tv1, '应付款选择');
end;

procedure TfrmPayableSel.TbPurSelClick(Sender: TObject);
begin
  try
    frmFinPurchasePlanSel := TfrmFinPurchasePlanSel.Create(Application);
    with frmFinPurchasePlanSel do
    begin
      if ShowModal = 1 then
      begin
        with frmFinPurchasePlanSel.CDS_1 do
        begin
          first;
          while not Eof do
          begin
            if CDS_1.fieldbyname('SSel').AsBoolean then
            begin
              with Self.CDS_1 do
              begin
                append;
          //对方单位
                self.CDS_1.FieldByName('OppCoName').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('SellName').Value);
                self.CDS_1.FieldByName('OppCoNo').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('SellNo').Value);
          //我方单位
                self.CDS_1.FieldByName('FFTime').Value := SGetServerDateTime(ADOQueryCmd);
                self.CDS_1.FieldByName('ReconciliationDate').Value := SGetServerDate(ADOQueryCmd);
                self.CDS_1.FieldByName('OurCoName').Value := 'DESHINE VIETNAM CO., LTD';
                self.CDS_1.FieldByName('FinType').Value := '纱线';
                self.CDS_1.FieldByName('Currency').Value := 'CNY';
                self.CDS_1.FieldByName('SSel').Value := False;
                self.CDS_1.FieldByName('FFAbstract').Value := '纱线采购';
          //采购单
                self.CDS_1.FieldByName('PurNo').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('PurNo').AsString);
                self.CDS_1.FieldByName('PurMID').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('PurMId').AsString);
                self.CDS_1.FieldByName('PurSID').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('PurSId').AsString);
          //坯布合同
                self.CDS_1.FieldByName('ConNo').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('ConNo').AsString);
                self.CDS_1.FieldByName('ConMId').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('fromConMId').AsString);
                self.CDS_1.FieldByName('ConSId').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('fromConSId').AsString);
          //面料合同
                self.CDS_1.FieldByName('BuyConNo').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('CPConNo').AsString);
                self.CDS_1.FieldByName('BuyConMID').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('FROMCPSID').AsString);
                self.CDS_1.FieldByName('BuyConSID').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('FROMCPMID').AsString);

                self.CDS_1.FieldByName('F_Code').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('Y_Code').Value);
                self.CDS_1.FieldByName('F_Name').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('Y_Name').Value);
//          self.CDS_1.FieldByName('F_Width').AsString := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('SubId').AsString);
//                self.CDS_1.FieldByName('F_GramWeight').AsString := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('C_GramWeight').AsString);
                self.CDS_1.FieldByName('F_Color').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('Y_Color').Value);
                if not frmFinPurchasePlanSel.CDS_1.FieldByName('Piece').IsNull then
                begin
                  self.CDS_1.FieldByName('Piece').AsString := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('Piece').AsString);
                end;
                if not frmFinPurchasePlanSel.CDS_1.FieldByName('Qty').IsNull then
                begin
                  self.CDS_1.FieldByName('Qty').AsString := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('Qty').AsString);
                end;
                self.CDS_1.FieldByName('QtyUnit').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('QtyUnit').AsString);
                self.CDS_1.FieldByName('currency').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('currency').AsString);
                if not frmFinPurchasePlanSel.CDS_1.FieldByName('Price').IsNull then
                begin
                  self.CDS_1.FieldByName('NoTaxPrice').AsString := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('Price').AsString);
                end;
                if not frmFinPurchasePlanSel.CDS_1.FieldByName('Amount').IsNull then
                begin
                  self.CDS_1.FieldByName('NoTaxInclude').AsString := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('Amount').AsString);
                end;
                if not frmFinPurchasePlanSel.CDS_1.FieldByName('TaxRate').IsNull then
                begin
                  self.CDS_1.FieldByName('TaxRate').AsString := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('TaxRate').AsString);
                end;
                if not frmFinPurchasePlanSel.CDS_1.FieldByName('Tax').IsNull then
                begin
                  self.CDS_1.FieldByName('Tax').AsString := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('Tax').AsString);
                end;
                if not frmFinPurchasePlanSel.CDS_1.FieldByName('TaxInclude').IsNull then
                begin
                  self.CDS_1.FieldByName('TaxInclude').AsString := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('TaxInclude').AsString);
                end;
                Post;
              end;
            end;
            frmFinPurchasePlanSel.CDS_1.Next;
          end;

        end;

      end;
    end;
  finally
    frmFinPurchasePlanSel.Free;
  end;
end;

procedure TfrmPayableSel.Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        with Self.CDS_1 do
        begin
          Edit;
          FieldByName('ToCoNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
          FieldByName('ToCoName').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
          post;
        end;
      end;
    end;
  finally
    frmCompanySel.Free;
  end;

end;

procedure TfrmPayableSel.Tv1CurrencyPropertiesChange(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_1 do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;
//  if not Assigned(Sender) or not (Sender is TComboBox) then
//    Exit;

  if TComboBox(Sender).Text = 'VND' then
  begin
    with CDS_1 do
    begin
      // 检查记录是否处于可编辑状态
      if not (State in [dsEdit, dsInsert]) then
        Edit;

      // 检查字段是否存在且不为空
      if not FieldByName('NoTaxPrice').IsNull and (FieldByName('NoTaxPrice').AsString <> '') then
      begin
        FieldByName('NoTaxPrice').Value := RoundFloat(FieldByName('NoTaxPrice').AsFloat, 0);
      end;


      // 检查Qty和NoTaxPrice字段是否存在且不为空
      if not FieldByName('Qty').IsNull and not FieldByName('NoTaxPrice').IsNull and (FieldByName('Qty').AsString <> '') and (FieldByName('NoTaxPrice').AsString <> '') then
      begin
        FieldByName('NoTaxInclude').Value := RoundFloat(FieldByName('Qty').AsFloat * FieldByName('NoTaxPrice').AsFloat, 0);
      end;

      if not FieldByName('TaxRate').IsNull and (FieldByName('TaxRate').AsString <> '') then
        FieldByName('TaxRate').Value := RoundFloat(FieldByName('TaxRate').AsFloat, 0);

      if not FieldByName('Tax').IsNull and (FieldByName('Tax').AsString <> '') then
        FieldByName('Tax').Value := RoundFloat(FieldByName('Tax').AsFloat, 0);

      Post;
    end;
  end
  else
  begin
    with CDS_1 do
    begin
      // 检查记录是否处于可编辑状态
      if not (State in [dsEdit, dsInsert]) then
        Edit;
      // 使用一致的空值检查方式
      if (not FieldByName('NoTaxPrice').IsNull) and (FieldByName('NoTaxPrice').AsString <> '') then
      begin
        FieldByName('NoTaxPrice').Value := FormatFloat('0.00', FieldByName('NoTaxPrice').AsFloat);
      end;

      if (not FieldByName('Qty').IsNull) and (not FieldByName('NoTaxPrice').IsNull) and (FieldByName('Qty').AsString <> '') and (FieldByName('NoTaxPrice').AsString <> '') then
      begin
        FieldByName('NoTaxInclude').Value := FormatFloat('0.00', FieldByName('NoTaxInclude').AsFloat);
      end;

      if not FieldByName('TaxRate').IsNull and (FieldByName('TaxRate').AsString <> '') then
      begin
        FieldByName('TaxRate').Value := FormatFloat('0.00', FieldByName('TaxRate').AsFloat);
      end;

      if not FieldByName('Tax').IsNull and (FieldByName('Tax').AsString <> '') then
      begin
        FieldByName('Tax').Value := FormatFloat('0.00', FieldByName('Tax').AsFloat);
      end;

      Post;
    end;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmPayableSel.Tv1FFAbstractPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'FFAbstract';
      flagname := '摘要';
      if ShowModal = 1 then
      begin
        Self.CDS_1.Edit;
        Self.CDS_1.FieldByName('FFAbstract').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Self.CDS_1.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

procedure TfrmPayableSel.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  FFSPID: string;
begin
//  if CDS_Sub.IsEmpty then
//    Exit;
//  if Trim(CDS_Sub.fieldbyname('BYIOID').AsString) <> '' then
//  begin
//    with ADOQueryTemp do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add('select * from BS_Yarn_IO where BYIOID=''' + Trim(CDS_Sub.fieldbyname('BYIOID').AsString) + '''');
//      Open;
//    end;
//    FFSPID := Trim(ADOQueryTemp.fieldbyname('BYIOID').AsString);
//    if Trim(FFSPID) <> '' then
//    begin
//      v1CRType.Options.Editing := False;
//    end
//    else
//    begin
//      v1CRType.Options.Editing := True;
//    end;
//  end
//  else
//  begin
//    v1CRType.Options.Editing := True;
//  end;
end;

procedure TfrmPayableSel.Tv1OppCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        Self.CDS_1.Edit;
        Self.CDS_1.FieldByName('OppCoName').asstring := Trim(CDS_1.fieldbyname('CoName').AsString);
        Self.CDS_1.FieldByName('OurCoNo').asstring := Trim(CDS_1.fieldbyname('CoCode').AsString);
        Self.CDS_1.Post;
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmPayableSel.Tv1OrderNoPropertiesButtonClick(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.CDS_1.FieldByName('PurNo').value := null;
          self.CDS_1.FieldByName('PurMID').value := null;
          self.CDS_1.FieldByName('PurSID').value := null;

          //织造单
          self.CDS_1.FieldByName('OrderNo').AsString := Trim(frmFinTradePlanSel.CDS_1.fieldbyname('OrderNo').AsString);
          self.CDS_1.FieldByName('OrdMainId').AsString := Trim(frmFinTradePlanSel.CDS_1.fieldbyname('MainId').AsString);
          self.CDS_1.FieldByName('OrdSubId').AsString := Trim(frmFinTradePlanSel.CDS_1.fieldbyname('SubId').AsString);
          //坯布合同
          self.CDS_1.FieldByName('ConNo').AsString := Trim(frmFinTradePlanSel.CDS_1.fieldbyname('ConNo').AsString);
          self.CDS_1.FieldByName('ConMId').AsString := Trim(frmFinTradePlanSel.CDS_1.fieldbyname('ConMId').AsString);
          self.CDS_1.FieldByName('ConSId').AsString := Trim(frmFinTradePlanSel.CDS_1.fieldbyname('ConSId').AsString);
          //面料合同
          self.CDS_1.FieldByName('BuyConNo').AsString := Trim(frmFinTradePlanSel.CDS_1.fieldbyname('BuyConNo').AsString);
          self.CDS_1.FieldByName('BuyConMID').AsString := Trim(frmFinTradePlanSel.CDS_1.fieldbyname('FROMCPMID').AsString);
          self.CDS_1.FieldByName('BuyConSID').AsString := Trim(frmFinTradePlanSel.CDS_1.fieldbyname('FROMCPSID').AsString);

//          self.CDS_1.FieldByName('F_Code').AsString := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('Y_Code').AsString);
//          self.CDS_1.FieldByName('F_Name').AsString := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('Y_Name').AsString);
//          self.CDS_1.FieldByName('F_Color').AsString := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('Y_Color').AsString);
//          self.CDS_1.FieldByName('Piece').AsString := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('Piece').AsString);
//          self.CDS_1.FieldByName('Qty').AsString := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('Qty').AsString);
//          self.CDS_1.FieldByName('QtyUnit').AsString := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('QtyUnit').AsString);
//          self.CDS_1.FieldByName('currency').AsString := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('currency').AsString);
//          self.CDS_1.FieldByName('NoTaxPrice').AsString := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('Piece').AsString);
//          self.CDS_1.FieldByName('NoTaxInclude').AsString := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('Amount').AsString);
//          self.CDS_1.FieldByName('TaxRate').AsString := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('TaxRate').AsString);
//          self.CDS_1.FieldByName('Tax').AsString := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('Tax').AsString);
//          self.CDS_1.FieldByName('TaxInclude').AsString := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('TaxInclude').AsString);
          self.CDS_1.Post;

        end;

      end;
    end;
  finally
    frmFinTradePlanSel.Free;
  end;
end;

procedure TfrmPayableSel.Tv1PurNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmFinPurchasePlanSel := TfrmFinPurchasePlanSel.Create(Application);
    with frmFinPurchasePlanSel do
    begin
      if ShowModal = 1 then
      begin
        with frmFinPurchasePlanSel.CDS_1 do
        begin
          self.CDS_1.Edit;
          //对方单位
          self.CDS_1.FieldByName('OppCoName').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('SellName').AsString);
          self.CDS_1.FieldByName('OppCoNo').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('SellNo').AsString);
          //织造单号
          self.CDS_1.FieldByName('OrderNo').value := null;
          self.CDS_1.FieldByName('OrdMainId').value := null;
          self.CDS_1.FieldByName('OrdSubId').value := null;

          //类型
          self.CDS_1.FieldByName('FinType').Value := '纱线';
          self.CDS_1.FieldByName('FFAbstract').Value := '纱线采购';
          //采购单
          self.CDS_1.FieldByName('PurNo').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('PurNo').AsString);
          self.CDS_1.FieldByName('PurMID').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('PurMId').AsString);
          self.CDS_1.FieldByName('PurSID').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('PurSId').AsString);
          //坯布合同
          self.CDS_1.FieldByName('ConNo').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('ConNo').AsString);
          self.CDS_1.FieldByName('ConMId').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('fromConMId').AsString);
          self.CDS_1.FieldByName('ConSId').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('fromConSId').AsString);
          //面料合同
          self.CDS_1.FieldByName('BuyConNo').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('CPConNo').AsString);
          self.CDS_1.FieldByName('BuyConMID').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('FROMCPSID').AsString);
          self.CDS_1.FieldByName('BuyConSID').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('FROMCPMID').AsString);

          self.CDS_1.FieldByName('F_Code').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('Y_Code').AsString);
          self.CDS_1.FieldByName('F_Name').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('Y_Name').AsString);
//        self.CDS_1.FieldByName('F_Width').AsString := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('SubId').AsString);
//        self.CDS_1.FieldByName('F_GramWeight').AsString := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('C_GramWeight').AsString);
          self.CDS_1.FieldByName('F_Color').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('Y_Color').AsString);

          self.CDS_1.FieldByName('Piece').asstring := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('Piece').AsString);
          self.CDS_1.FieldByName('Qty').asstring := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('Qty').AsString);
          self.CDS_1.FieldByName('QtyUnit').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('QtyUnit').AsString);
          self.CDS_1.FieldByName('currency').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('currency').AsString);
          if not frmFinPurchasePlanSel.CDS_1.FieldByName('PRICE').IsNull then
          begin
            self.CDS_1.FieldByName('NoTaxPrice').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('PRICE').Value);
          end;
          if not frmFinPurchasePlanSel.CDS_1.FieldByName('Amount').IsNull then
          begin
            self.CDS_1.FieldByName('NoTaxInclude').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('Amount').Value);
          end;
          if not frmFinPurchasePlanSel.CDS_1.FieldByName('TaxRate').IsNull then
          begin
            self.CDS_1.FieldByName('TaxRate').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('TaxRate').Value);
          end;
          if not frmFinPurchasePlanSel.CDS_1.FieldByName('Tax').IsNull then
          begin
            self.CDS_1.FieldByName('Tax').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('Tax').Value);
          end;

          if not frmFinPurchasePlanSel.CDS_1.FieldByName('TaxInclude').IsNull then
          begin
            self.CDS_1.FieldByName('TaxInclude').Value := Trim(frmFinPurchasePlanSel.CDS_1.fieldbyname('TaxInclude').Value);
          end;

          self.CDS_1.Post;
        end;
      end;
    end;
  finally
    frmFinPurchasePlanSel.Free;
  end;
end;

procedure TfrmPayableSel.Tv1TaxRatePropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FQty, FNoTaxPrice, FNoTaxInclude: Double;   //数量，不含税单价，不含税金额
  mTaxRate, mTax, mTaxInclude: Double;        //税率，税额，含税金额

//  mCP_QTY, mCP_TPXS: Double;                //成品数量，投坯系数
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);

  with CDS_1 do
  begin
    Edit;
    FieldByName(FFieldName).Value := StrToFloat(Trim(mvalue));
    Post;
//    ShowMessage(Fieldbyname('TaxRate').AsString);
    if Fieldbyname('Qty').AsString <> '' then
    begin
      FQty := Fieldbyname('Qty').AsFloat;
    end
    else
    begin
      FQty := 0;
    end;
    if Fieldbyname('NoTaxPrice').AsString <> '' then
    begin
      FNoTaxPrice := Fieldbyname('NoTaxPrice').AsFloat;
    end
    else
    begin
      FNoTaxPrice := 0;
    end;
    if Fieldbyname('TaxRate').AsString <> '' then
    begin
      mTaxRate := Fieldbyname('TaxRate').AsFloat / 100;
    end
    else
    begin
      mTaxRate := 0;
    end;
    if Fieldbyname('Tax').AsString <> '' then
    begin
      mTax := Fieldbyname('Tax').AsFloat;
    end
    else
    begin
      mTax := 0;
    end;
    if Fieldbyname('TaxInclude').AsString <> '' then
    begin
      mTaxInclude := Fieldbyname('TaxInclude').AsFloat;
    end
    else
    begin
      mTaxInclude := 0;
    end;

  end;

  FNoTaxInclude := RoundFloat(FQty * FNoTaxPrice, 2);
  mTax := RoundFloat(FNoTaxInclude * mTaxRate, 2);
  mTaxInclude := RoundFloat(FNoTaxInclude + mTax, 2);

  with CDS_1 do
  begin
    Edit;
    FieldByName('Qty').Value := FQty;
//    FieldByName('ConPrice').Value := FConPrice;
    FieldByName('NoTaxPrice').Value := FloatToStr(FNoTaxPrice);
    FieldByName('NoTaxInclude').Value := FloatToStr(FNoTaxInclude);
    FieldByName('TaxRate').Value := FloatToStr(mTaxRate * 100);
    FieldByName('Tax').Value := FloatToStr(mTax);
    FieldByName('TaxInclude').Value := FloatToStr(mTaxInclude);
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmPayableSel.TbSSelClick(Sender: TObject);
var
  FirstOppCoName: string;
  bAllSame: Boolean;
begin
  if not CDS_1.Locate('ssel', True, []) then
  begin
    Application.MessageBox('请先选择数据！', '提示信息', MB_OK);
    Exit;
  end;
//判断所选供应商是否是同一个
  FNoTaxInclude := 0;
  FTax := 0;
  FTaxInclude := 0;
  FFFID := '';

  CDS_1.First;
  FirstOppCoName := '';
  bAllSame := True;

  with CDS_1 do
  begin
    while not Eof do
    begin
      if FieldByName('Ssel').AsBoolean then
      begin
        if FirstOppCoName = '' then
          FirstOppCoName := FieldByName('OppCoName').AsString
        else if FirstOppCoName <> FieldByName('OppCoName').AsString then
        begin
          bAllSame := False;
          Break;
        end;

        FNoTaxInclude := FNoTaxInclude + CDS_1.FieldByName('NoTaxInclude').AsFloat;
        FTax := FTax + CDS_1.FieldByName('Tax').AsFloat;
        FTaxInclude := FTaxInclude + CDS_1.FieldByName('TaxInclude').AsFloat;
        if FFFIDs = '' then
        begin
          FFFIDs := CDS_1.FieldByName('FFID').AsString;
        end
        else
        begin
          FFFIDs := FFFIDs + ',' + CDS_1.FieldByName('FFID').AsString;
        end;

      end;
      Next;
    end;
  end;

  if not bAllSame then
  begin
    ShowMessage('选择的对方单位不相同');
    exit;
  end;
  if FFFIDs <> '' then
  begin
    ModalResult := 1;
  end;

end;

end.

