unit U_FDINVOICE_Sub;

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;

type
  TFrmInvoice_Sub = class(TForm)
    DataSource1: TDataSource;
    FP_Sub: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ToolBar1: TToolBar;
    TSave: TToolButton;
    TBClose: TToolButton;
    ToolBar2: TToolBar;
    TADD: TToolButton;
    TDEl: TToolButton;
    Panel1: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1P_CodeName: TcxGridDBColumn;
    v1P_Spec: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1Price: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Money: TcxGridDBColumn;
    v1note: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    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;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    Label14: TLabel;
    YFGSType: TComboBox;
    v1Column5: TcxGridDBColumn;
    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 v1QtyPropertiesEditValueChanged(Sender: TObject);
    procedure SYRNameBtnClick(Sender: TObject);
  private
    procedure initGrid();
    procedure SaveDate();
    { Private declarations }
  public
    FInvoiceNO: string;
    fInvoiceFlag: string;
    { Public declarations }
  end;

var
  FrmInvoice_Sub: TFrmInvoice_Sub;

implementation

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

procedure TFrmInvoice_Sub.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('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.fieldbyname('tax').Value := FP_Sub.fieldbyname('tax').AsFloat;
        ADOQueryCmd.fieldbyname('TaxMoney').Value := FP_Sub.fieldbyname('TaxMoney').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.initGrid();
begin
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from FD_Invoice where 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 *,shuie=taxMoney-Money from FD_InvoiceSub where InvoiceNo=''' + trim(FInvoiceNo) + '''');
    open;
  end;
  SCreateCDS20(ADOQueryTmp, FP_Sub);
  SInitCDSData20(ADOQueryTmp, FP_Sub);
end;

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

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

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

procedure TFrmInvoice_Sub.TSaveClick(Sender: TObject);
begin
  if trim(YFGSType.Text) = '' then
  begin
    application.MessageBox('应付类别不能为空', '提示');
    exit;
  end;
  if trim(InvoiceNo.Text) = '' then
  begin
    application.MessageBox('发票号不能为空', '提示');
    exit;
  end;

  if trim(FactoryName.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.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 ShowModal = 1 then
        begin
          FactoryName.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
        end;
      end;
    finally
      frmZdyAttachment.Free;
    end;
  end;
end;

procedure TFrmInvoice_Sub.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.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.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.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.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.TADDClick(Sender: TObject);
begin
  FP_Sub.Append;
  FP_Sub.FieldByName('QtyUnit').Value := 'Kg';
  FP_Sub.FieldByName('priceUnit').Value := 'RMB';
  FP_Sub.FieldByName('tax').Value := 13;
  FP_Sub.Post;

end;

procedure TFrmInvoice_Sub.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;
end;

procedure TFrmInvoice_Sub.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();

end;

procedure TFrmInvoice_Sub.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;

end.

