unit U_FDINVOICE_Sub_X;

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;

type
  TFrmInvoice_Sub_X = class(TForm)
    DataSource1: TDataSource;
    FP_Sub: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ToolBar1: TToolBar;
    TSave: TToolButton;
    TBClose: TToolButton;
    ToolBar2: TToolBar;
    TADD: TToolButton;
    TDEl: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    InvoiceNo: TEdit;
    Label2: TLabel;
    Label3: TLabel;
    InvoiceDate: TDateTimePicker;
    Note: TMemo;
    InvoiceType: TBtnEditA;
    ToFactoryName: TBtnEditA;
    FactoryName: TBtnEditA;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Filler: TEdit;
    ADOQueryTmp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Label8: TLabel;
    SYRName: TBtnEditA;
    Label9: TLabel;
    YFGSType: TComboBox;
    ToolButton1: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    v1P_Spec: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1Price: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Money: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1note: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label10: TLabel;
    ishs: TComboBox;
    Label11: TLabel;
    ZHSMONEY: TEdit;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToFactoryNameBtnClick(Sender: TObject);
    procedure TSaveClick(Sender: TObject);
    procedure FactoryNameBtnClick(Sender: TObject);
    procedure v1P_CodeNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure InvoiceTypeBtnClick(Sender: TObject);
    procedure v1P_SpecPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1QtyUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1PriceUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TADDClick(Sender: TObject);
    procedure TDElClick(Sender: TObject);
    procedure SYRNameBtnClick(Sender: TObject);
    procedure v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1QtyPropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
  private
    procedure initGrid();
    procedure SaveDate();
    { Private declarations }
  public
    FInvoiceNO: string;
    fInvoiceFlag: string;
    canshu1: string;
    { Public declarations }
  end;

var
  FrmInvoice_Sub_X: TFrmInvoice_Sub_X;

implementation

uses
  U_Fun10, U_ZDYHelp, U_ZdyAttachment, U_dataLink, U_ZdyAttachGYS, U_CPManageSel,
  U_ProductOrderNewList, U_YSCRSEL2;
{$R *.dfm}

procedure TFrmInvoice_Sub_X.SaveDate();
var
  FInvoiceID: string;
begin
  ADOQueryCmd.Connection.BeginTrans;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.add('select * from FD_Invoice where InvoiceNo=''' + trim(InvoiceNo.Text) + '''');
      open;
      if isempty then
      begin
        append;
        fieldbyname('filltime').AsDateTime := SGetServerDate(ADOQueryTmp);
      end
      else
        edit;
      SSetsaveSqlnew(ADOQueryCmd, 'FD_Invoice', panel1, 0);
      fieldbyname('Invoiceflag').Value := strtoint(FInvoiceflag);
      fieldbyname('chkstatus').AsString := '0';
      post;
    end;

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.add('delete from FD_InvoiceSub where InvoiceNo=''' + trim(InvoiceNo.Text) + ''' ');
      execsql;
    end;
    with FP_Sub do
    begin
      first;
      while not eof do
      begin
        if fieldbyname('invoiceID').AsString = '' then
        begin
          if GetLSNo(ADOQueryTmp, FInvoiceID, 'FP', 'FD_InvoiceSub', 4, 1) = False then
          begin
            Application.MessageBox('取最大号失败！', '提示', 0);
            Exit;
          end;
        end
        else
          FInvoiceID := FP_Sub.fieldbyname('invoiceID').AsString;
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.add('select * from FD_InvoiceSub where InvoiceNo=''' + trim(InvoiceNo.Text) + ''' ');
          sql.add('and InvoiceID=''' + trim(FInvoiceID) + '''');
          open;
        end;
        if ADOQueryCmd.IsEmpty then
        begin
          ADOQueryCmd.append;
        end
        else
        begin
          ADOQueryCmd.edit;
        end;
        ADOQueryCmd.fieldbyname('InvoiceNo').Value := trim(InvoiceNo.Text);
        ADOQueryCmd.fieldbyname('InvoiceID').Value := FInvoiceID;
        SSetSaveDataCDSNew(ADOQueryCmd, Tv1, FP_Sub, 'FD_InvoiceSub', 0);
        ADOQueryCmd.fieldbyname('MainID').Value := FP_Sub.fieldbyname('MainID').asstring;
        ADOQueryCmd.fieldbyname('Qty').Value := FP_Sub.fieldbyname('Qty').AsFloat;
        ADOQueryCmd.fieldbyname('Price').Value := FP_Sub.fieldbyname('Price').AsFloat;
        ADOQueryCmd.fieldbyname('Money').Value := FP_Sub.fieldbyname('Money').AsFloat;
        ADOQueryCmd.Post;
        next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('保存成功！', '提示');
    Modalresult := 1;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('保存失败！', '提示', MB_ICONERROR);
  end;
end;

procedure TFrmInvoice_Sub_X.initGrid();
begin
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select A.*');
    sql.Add('from FD_Invoice A');
    sql.Add('where A.InvoiceNo=''' + trim(FInvoiceNo) + '''');
    open;
  end;
  if ADOQueryTmp.isempty = False then
  begin
    SCSHDataNew(ADOQueryTmp, Panel1, 0);
  end
  else
  begin
    Filler.Text := trim(DName);
    InvoiceDate.DateTime := SGetServerDate(ADOQueryCmd);
  end;
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select A.*,shuie=taxMoney-Money');
    sql.Add('from FD_InvoiceSub A ');
    sql.Add('where A.InvoiceNo=''' + trim(FInvoiceNo) + '''');
    open;
  end;
  SCreateCDS20(ADOQueryTmp, FP_Sub);
  SInitCDSData20(ADOQueryTmp, FP_Sub);
end;

procedure TFrmInvoice_Sub_X.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('销项发票录入', Tv1, '财务管理');
  Close;
end;

procedure TFrmInvoice_Sub_X.FormShow(Sender: TObject);
begin
  ReadCxGrid('销项发票录入', Tv1, '财务管理');
  initGrid();
end;

procedure TFrmInvoice_Sub_X.ToFactoryNameBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'YSFtySYRName';
      flagname := '对方抬头';
      if ShowModal = 1 then
      begin
        ToFactoryName.Text := Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TFrmInvoice_Sub_X.TSaveClick(Sender: TObject);
begin
  Note.SetFocus;
  if trim(InvoiceNo.Text) = '' then
  begin
    application.MessageBox('发票号不能为空', '提示');
    exit;
  end;

  if trim(YFGSType.Text) = '' then
  begin
    application.MessageBox('应收类别不能为空', '提示');
    exit;
  end;
  if trim(FInvoiceNo) = '' then
  begin
    with ADOQueryTmp do
    begin
      close;
      sql.clear;
      sql.add('select * from FD_Invoice where InvoiceNo=''' + trim(InvoiceNo.Text) + ''' ');
      open;
    end;
    if ADOQueryTmp.IsEmpty = False then
    begin
      application.MessageBox('发票号不能重复', '提示');
      exit;
    end;
  end;
  SaveDate();
end;

procedure TFrmInvoice_Sub_X.FactoryNameBtnClick(Sender: TObject);
begin
  if fInvoiceflag = '0' then
  begin
    try
      frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
      with frmZdyAttachGYS do
      begin
        if ShowModal = 1 then
        begin
          FactoryName.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
        end;
      end;
    finally
      frmZdyAttachGYS.Free;
    end;
  end
  else
  begin
    try
      frmZdyAttachment := TfrmZdyAttachment.Create(Application);
      with frmZdyAttachment do
      begin
        if trim(canshu1) = '业务员' then
          fywy := trim(DName);
        if ShowModal = 1 then
        begin
          FactoryName.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
        end;
      end;
    finally
      frmZdyAttachment.Free;
    end;
  end;
end;

procedure TFrmInvoice_Sub_X.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.FP_Sub.Edit;
        Self.FP_Sub.FieldByName('P_CodeName').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Self.FP_Sub.FieldByName('P_Code').Value := Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  //self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TFrmInvoice_Sub_X.InvoiceTypeBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'InvoiceType';
      flagname := '发票类型';
      if ShowModal = 1 then
      begin
        InvoiceType.Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TFrmInvoice_Sub_X.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.FP_Sub.Edit;
        Self.FP_Sub.FieldByName('P_Spec').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

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

procedure TFrmInvoice_Sub_X.v1PriceUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'PriceUnit';
      flagname := '币种';
      if ShowModal = 1 then
      begin
        Self.FP_Sub.Edit;
        Self.FP_Sub.FieldByName('PriceUnit').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TFrmInvoice_Sub_X.TADDClick(Sender: TObject);
begin
  FP_Sub.Append;
  FP_Sub.FieldByName('QtyUnit').Value := 'M';
  FP_Sub.FieldByName('priceUnit').Value := 'RMB';
  FP_Sub.FieldByName('tax').Value := 13;
  FP_Sub.Post;

end;

procedure TFrmInvoice_Sub_X.TDElClick(Sender: TObject);
begin
  if FP_Sub.IsEmpty then
    exit;
  if application.MessageBox('确定要删除吗', '提示', 1) = 2 then
    exit;
  with ADOQueryTmp do
  begin
    close;
    sql.clear;
    sql.add('delete from FD_Invoicesub where InvoiceID=''' + trim(FP_Sub.fieldbyname('InvoiceID').asstring) + ''' ');
    execsql;
  end;
  FP_Sub.delete;
  ZHSMONEY.Text := TV1.DataController.Summary.FooterSummaryValues[4];
end;

procedure TFrmInvoice_Sub_X.SYRNameBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SYRName';
      flagname := '公司抬头';
      if Trim(flag) = 'SYRName' then
      begin
        flag := 'OrdDefStr2';
        V1Name.Caption := '中文名称';
        V1Note.Caption := '英文名称';
        fnote := True;
      end;
      if ShowModal = 1 then
      begin
        SYRName.Text := Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TFrmInvoice_Sub_X.v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmProductOrderNewList := TfrmProductOrderNewList.Create(Application);
    with frmProductOrderNewList do
    begin
      if ShowModal = 1 then
      begin
        with Self.FP_Sub do
        begin
          Edit;
          FieldByName('MainId').Value := Trim(frmProductOrderNewList.Order_Main.fieldbyname('MainId').AsString);
      //    FieldByName('subID').Value:=Trim(frmProductOrderNewList.Order_Main.fieldbyname('subID').AsString);
          FieldByName('orderNo').Value := Trim(frmProductOrderNewList.Order_Main.fieldbyname('orderNo').AsString);
          FieldByName('khconNO').Value := Trim(frmProductOrderNewList.Order_Main.fieldbyname('khconNO').AsString);
          FieldByName('P_Code').Value := Trim(frmProductOrderNewList.Order_Main.fieldbyname('MPRTCode').AsString);
          FieldByName('P_CodeName').Value := Trim(frmProductOrderNewList.Order_Main.fieldbyname('MPRTCodeName').AsString);
          FieldByName('P_spec').Value := Trim(frmProductOrderNewList.Order_Main.fieldbyname('Mprtspec').AsString);
          post;
        end;
      end;
    end;
  finally
    frmProductOrderNewList.Free;
    self.Tv1.Controller.EditingController.ShowEdit();
  end;
end;

procedure TFrmInvoice_Sub_X.v1QtyPropertiesEditValueChanged(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 FP_Sub 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 FP_Sub do
  begin
    Edit;
    FieldByName('Price').Value := FPrice;
    FieldByName('Money').Value := FMoney;
    FieldByName('shuie').Value := FtaxMoney - FMoney;
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();

  ZHSMONEY.Text := TV1.DataController.Summary.FooterSummaryValues[4];
end;

procedure TFrmInvoice_Sub_X.ToolButton1Click(Sender: TObject);
var
  CFactoryName, CKXType: string;
  CQtyFlag: integer;
begin
  if ishs.Text = '' then
  begin
    Application.MessageBox('请先选择是否含税!', '提示', 0);
    Exit;
  end;
  try
    frmYSCRSEL2 := TfrmYSCRSEL2.create(self);
    with frmYSCRSEL2 do
    begin

      if showmodal = 1 then
      begin
        CFactoryName := Trim(CDS_HZ.fieldbyname('FACTORYNAME').AsString);
//        FTT := '1';
//        FtySYRName.Text := CFactoryName;
        while CDS_HZ.Locate('SSel', true, []) do
        begin
          if trim(CFactoryName) <> Trim(CDS_HZ.fieldbyname('FactoryName').AsString) then
          begin
            Application.MessageBox('不能选择多个开票单位!', '提示', 0);
            Exit;
          end;

          self.FP_Sub.Append;
          self.FP_Sub.FieldByName('DZDNO').Value := CDS_HZ.FieldByName('YFSID').Value;
          self.FP_Sub.FieldByName('ORDERNO').Value := CDS_HZ.FieldByName('MXOrderNo').Value;
          self.FP_Sub.FieldByName('P_CODENAME').Value := CDS_HZ.FieldByName('CPName').AsString;
          self.FP_Sub.FieldByName('P_CODE').Value := CDS_HZ.FieldByName('CPCODE').AsString;
          FP_Sub.FieldByName('priceUnit').Value := 'RMB';
          FP_Sub.FieldByName('tax').Value := 13;
          self.FP_Sub.FieldByName('Qty').Value := CDS_HZ.FieldByName('Qty').AsFloat;
          self.FP_Sub.FieldByName('Price').Value := CDS_HZ.FieldByName('Price').AsFloat;
          self.FP_Sub.FieldByName('QtyUnit').Value := CDS_HZ.FieldByName('QtyUnit').AsString;
//          self.FP_Sub.FieldByName('Money').Value := CDS_HZ.FieldByName('MXMONEY').AsFloat;
          self.FP_Sub.FieldByName('XGFEE').Value := CDS_HZ.FieldByName('xiaogangfei').AsFloat;
          self.FP_Sub.FieldByName('SJFEE').Value := CDS_HZ.FieldByName('shangjifei').AsFloat;
          if ishs.Text = '是' then
          begin
            self.FP_Sub.FieldByName('Money').Value := CDS_HZ.FieldByName('MXMONEY').AsFloat / 1.13;
            self.FP_Sub.FieldByName('taxMoney').Value := CDS_HZ.FieldByName('MXMONEY').AsFloat;
            self.FP_Sub.FieldByName('shuie').Value := CDS_HZ.FieldByName('MXMONEY').AsFloat / 1.13 * 0.13;
          end
          else
          begin
            self.FP_Sub.FieldByName('Money').Value := CDS_HZ.FieldByName('MXMONEY').AsFloat;
            self.FP_Sub.FieldByName('taxMoney').Value := 1.13 * CDS_HZ.FieldByName('MXMONEY').AsFloat;

            self.FP_Sub.FieldByName('shuie').Value := CDS_HZ.FieldByName('MXMONEY').AsFloat * 0.13;
          end;
          self.FP_Sub.post;
          CDS_HZ.Delete;
        end;
        self.FactoryName.Text := Trim(CFactoryName);
//        HJJE();
      end;
    end;

  finally
    frmYSCRSEL2.free;
  end;
  ZHSMONEY.Text := TV1.DataController.Summary.FooterSummaryValues[4];
end;

end.

