unit U_InvoiceOutInPut;

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,
  dxSkinsCore, dxSkinsDefaultPainters, dxDateRanges, dxBarBuiltInMenu, ComObj,
  dxScrollbarAnnotations;

type
  TfrmInvoiceOutInPut = class(TForm)
    DataSource1: TDataSource;
    CDS_Invoice: TClientDataSet;
    GPM_1: TcxGridPopupMenu;
    ToolBar1: TToolBar;
    TSave: TToolButton;
    TBClose: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1P_CodeName: TcxGridDBColumn;
    v1P_Spec: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1Price: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ADOQueryTmp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    ToolButton4: TToolButton;
    OpenDialog1: TOpenDialog;
    CDS_LM: TClientDataSet;
    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);
  private
    procedure initGrid();
    procedure SaveDate();
    { Private declarations }
  public
    FIVID: string;
    fInvoiceFlag: string;
    { Public declarations }
  end;

var
  frmInvoiceOutInPut: TfrmInvoiceOutInPut;

implementation

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

procedure TfrmInvoiceOutInPut.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 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', 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.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 TfrmInvoiceOutInPut.initGrid();
begin
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from Finance_Invoice where IVID=''' + trim(FIVID) + '''');
//    sql.add(' and isnull(status,''0'')=''0''');
    open;
  end;

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

procedure TfrmInvoiceOutInPut.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmInvoiceOutInPut.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 TfrmInvoiceOutInPut.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 TfrmInvoiceOutInPut.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('P_CodeName').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 TfrmInvoiceOutInPut.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 TfrmInvoiceOutInPut.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 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 TfrmInvoiceOutInPut.FormDestroy(Sender: TObject);
begin
  frmInvoiceOutInPut := nil;
end;

procedure TfrmInvoiceOutInPut.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('CoAbbrName').AsString);

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

procedure TfrmInvoiceOutInPut.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 TfrmInvoiceOutInPut.Tv1Column4PropertiesButtonClick(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('OppCoNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
        Self.CDS_Invoice.FieldByName('OppCoName').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
        Self.CDS_Invoice.FieldByName('OppParentCoNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
        Self.CDS_Invoice.FieldByName('OppParentCoName').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmInvoiceOutInPut.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('CoAbbrName').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmInvoiceOutInPut.ToolButton1Click(Sender: TObject);
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 TfrmInvoiceOutInPut.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 where IvID=''' + trim(CDS_Invoice.fieldbyname('IvID').asstring) + ''' ');
    execsql;
  end;
  CDS_Invoice.delete;
end;

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

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

end.

