unit U_ReceivableImport;

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
  TfrmReceivableImport = class(TForm)
    DS_1: TDataSource;
    CDS_Import: TClientDataSet;
    GPM_1: TcxGridPopupMenu;
    ToolBar1: TToolBar;
    TSave: TToolButton;
    TBClose: TToolButton;
    ADOQueryTmp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    OpenDialog1: TOpenDialog;
    CDS_LM: TClientDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v2CRTime: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2FactoryName: TcxGridDBColumn;
    v2OrderNo: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column19: TcxGridDBColumn;
    Tv1Column24: TcxGridDBColumn;
    Tv1Column20: TcxGridDBColumn;
    Tv1Column21: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    Tv1Column17: TcxGridDBColumn;
    Tv1Column18: TcxGridDBColumn;
    Tv1Column26: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    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 FormDestroy(Sender: TObject);
    procedure Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    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
    FFFID: string;
    fInvoiceFlag: string;
    { Public declarations }
  end;

var
  frmReceivableImport: TfrmReceivableImport;

implementation

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

procedure TfrmReceivableImport.SaveDate();
var
  MaxNo: string;
begin
  ADOQueryCmd.Connection.BeginTrans;
  try
    with CDS_Import do
    begin
      first;
      while not eof do
      begin
        if fieldbyname('FFID').AsString = '' then
        begin
          if GetLSNo(ADOQueryTmp, MaxNo, 'YS', 'Finance_Flow', 4, 1) = False then
            raise Exception.Create('取最大号失败！');
        end
        else
          MaxNo := CDS_Import.fieldbyname('FFID').AsString;

        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.add('select * from Finance_Flow where FFID=' + 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_Import, 'Finance_Flow', 0);
        ADOQueryCmd.fieldbyname('FFID').Value := MaxNo;
        ADOQueryCmd.FieldByName('FFFlag').Value := '应收收';
        ADOQueryCmd.FieldByName('FFType').Value := '应收款登记';
        ADOQueryCmd.FieldByName('FFQtyFlag').Value := 1;
        ADOQueryCmd.fieldbyname('OppCoNo').Value := trim(CDS_Import.FieldByName('OppCoNo').AsString);
        ADOQueryCmd.fieldbyname('OppParentCoNo').Value := trim(CDS_Import.FieldByName('OppParentCoNo').AsString);
        ADOQueryCmd.fieldbyname('OurCoNo').Value := trim(CDS_Import.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 TfrmReceivableImport.initGrid();
begin
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from Finance_Flow where FFID=''' + trim(FFFID) + '''');
    sql.add(' and isnull(status,''0'')=''0''');
    open;
  end;

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

procedure TfrmReceivableImport.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmReceivableImport.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 TfrmReceivableImport.TSaveClick(Sender: TObject);
begin
  if CDS_Import.Locate('OurCoName', null, []) then
  begin
    application.MessageBox('我方单位不能为空', '提示');
    exit;
  end;
  if CDS_Import.Locate('OppCoName', null, []) then
  begin
    application.MessageBox('对方单位不能为空', '提示');
    exit;
  end;
  if CDS_Import.Locate('Currency', null, []) then
  begin
    application.MessageBox('币种不能为空', '提示');
    exit;
  end;
  if CDS_Import.Locate('Amount', null, []) then
  begin
    application.MessageBox('金额不能为空', '提示');
    exit;
  end;
  if CDS_Import.Locate('Price', null, []) then
  begin
    application.MessageBox('金额不能为空', '提示');
    exit;
  end;
  if CDS_Import.Locate('Qty', null, []) then
  begin
    application.MessageBox('数量不能为空', '提示');
    exit;
  end;
  if CDS_Import.Locate('IsInvoice', null, []) then
  begin
    application.MessageBox('是否含税不能为空', '提示');
    exit;
  end;
  SaveDate();
end;

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

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

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

procedure TfrmReceivableImport.FormDestroy(Sender: TObject);
begin
  frmReceivableImport := nil;
end;

procedure TfrmReceivableImport.Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '我司';
      if ShowModal = 1 then
      begin

        Self.CDS_Import.Edit;
        Self.CDS_Import.FieldByName('OurCoNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
        Self.CDS_Import.FieldByName('OurCoName').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);

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

procedure TfrmReceivableImport.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_Import.Edit;
        Self.CDS_Import.FieldByName('IVType').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmReceivableImport.Tv1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '客户';
      if ShowModal = 1 then
      begin
        Self.CDS_Import.Edit;
        Self.CDS_Import.FieldByName('OppCoNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
        Self.CDS_Import.FieldByName('OppCoName').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
        Self.CDS_Import.FieldByName('OppParentCoNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
        Self.CDS_Import.FieldByName('OppParentCoName').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmReceivableImport.Tv1Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '客户';
      if ShowModal = 1 then
      begin
        Self.CDS_Import.Edit;
        Self.CDS_Import.FieldByName('OppParentCoNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
        Self.CDS_Import.FieldByName('OppParentCoName').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmReceivableImport.ToolButton2Click(Sender: TObject);
begin
  if CDS_Import.IsEmpty then
    exit;
  CDS_Import.delete;
end;

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

procedure TfrmReceivableImport.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_Import do
      begin
        Append;
        CDS_LM.First;
        while not CDS_LM.Eof do
        begin
          if CDS_LM.FieldByName('LXH').AsInteger > 0 then
            CDS_Import.fieldbyname(CDS_LM.FieldByName('LCode').AsString).Value := WorkBook.WorkSheets[1].Cells[i, CDS_LM.FieldByName('LXH').AsInteger].Value;
          CDS_LM.Next;
        end;
        CDS_Import.fieldbyname('OurCoNo').Value := 'W0002';
        CDS_Import.fieldbyname('OurCoName').Value := '恒燕';
        Post;
      end;
    end;
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
  except
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
    exit;
  end;

end;

end.

