unit U_HTImport;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin, StdCtrls,
  cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, ShellAPI, IniFiles, cxCheckBox, cxCalendar, cxButtonEdit,
  cxTextEdit, cxDropDownEdit, cxPC, U_BaseHelp, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxDateRanges, dxBarBuiltInMenu, ComObj,
  U_BaseList, System.ImageList, Vcl.ImgList, dxScrollbarAnnotations, dxSkinsCore,
  dxSkinsDefaultPainters, dxSkinWXI;

type
  TfrmHTImport = class(TfrmBaseHelp)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    GPM_1: TcxGridPopupMenu;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    OpenDialog1: TOpenDialog;
    ToolButton5: TToolButton;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    CDS_LM: TClientDataSet;
    ToolButton3: TToolButton;
    cxGrid2: TcxGrid;
    TV1: TcxGridDBTableView;
    v1XHNo: TcxGridDBColumn;
    VC_SCSCode: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    TV1density: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    TV1C_WidthUnit: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    TV1Column1: TcxGridDBColumn;
    TV1Column2: TcxGridDBColumn;
    TV1Column3: TcxGridDBColumn;
    TV1Column4: TcxGridDBColumn;
    TV1CShaZhi: TcxGridDBColumn;
    TV1C_Color: TcxGridDBColumn;
    TV1SampleTime: TcxGridDBColumn;
    TV1ConPrice: TcxGridDBColumn;
    TV1Currency: TcxGridDBColumn;
    TV1ConTotalAmount: TcxGridDBColumn;
    TV1C_GramWeight: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
  public
    { Public declarations }
    FCTID: string;
  end;

var
  frmHTImport: TfrmHTImport;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}

procedure TfrmHTImport.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select * from CP_Contract_dr A   where 2= 1 ');
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmHTImport.FormDestroy(Sender: TObject);
begin
  inherited;
  frmHTImport := nil;
end;

procedure TfrmHTImport.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  Action := cafree;

end;

procedure TfrmHTImport.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmHTImport.FormShow(Sender: TObject);
begin
  inherited;
  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 TfrmHTImport.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmHTImport.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(self.Caption, Tv1, '样品管理');
end;

procedure TfrmHTImport.ToolButton2Click(Sender: TObject);
var
  MaxBCIID, MaxBCPID, maxId2: string;
begin
  if GetLSNo(ADOQueryTemp, MaxBCPID, 'Z', 'CP_Contract_dr', 4, 1) = False then
  begin
//          ADOQueryTemp.Connection.RollbackTrans;
    Application.MessageBox('取最大号失败！', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
//    ADOQueryTemp.Connection.BeginTrans;
    CDS_1.DisableControls;
    with CDS_1 do
    begin
      First;
      while not eof do
      begin
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add(' select * from CP_Contract_dr A   where 2= 1 ');
          Open;
        end;
        if GetLSNo(ADOQueryTemp, MaxBCIID, 'ZD', 'CP_Contract_dr', 4, 1) = False then
        begin
//          ADOQueryTemp.Connection.RollbackTrans;
          Application.MessageBox('取最大号失败！', '提示', 0);
          Exit;
        end;

        with ADOQueryCmd do
        begin
          Append;
          FieldByName('Fillid').Value := Trim(Dcode);
          FieldByName('Filler').Value := Trim(DName);
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_1, 'CP_Contract_dr', 0);
          FieldByName('consid').Value := MaxBCIID;
          FieldByName('ZID').Value := MaxBCPID;
          FieldByName('CONMID').Value := CDS_1.FieldByName('CONNO').AsString;
          Post;
        end;

        Next;
      end;
      with ADOQueryTemp do
      begin
        Close;
        SQL.Clear;
        sql.Add(' exec P_BS_Contract_DR @Filtration=' + quotedstr(MaxBCPID));
        ExecSQL;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    CDS_1.EnableControls;
    application.MessageBox('保存成功！', '提示信息', 0);
  except
    CDS_1.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message + '合同号' + Trim(CDS_1.fieldbyname('CONNO').AsString)), '提示信息', 0);

  end;
end;

procedure TfrmHTImport.ToolButton3Click(Sender: TObject);
begin
  CDS_1.Delete;
end;

procedure TfrmHTImport.ToolButton5Click(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_1 do
      begin
        Append;
        CDS_LM.First;
        while not CDS_LM.Eof do
        begin
          if CDS_LM.FieldByName('LXH').AsInteger > 0 then
            CDS_1.fieldbyname(CDS_LM.FieldByName('LCode').AsString).Value := WorkBook.WorkSheets[1].Cells[i, CDS_LM.FieldByName('LXH').AsInteger].Value;
          CDS_LM.Next;
        end;
        Post;
      end;
    end;
    with CDS_1 do
    begin

      CDS_1.First;
      while not CDS_1.Eof do
      begin
        if CDS_1.FieldByName('conno').AsString = '' then
        begin
          CDS_1.Delete;
        end
        else
        begin
          CDS_1.Next;
        end;

      end;

    end;
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
  except
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
    application.MessageBox('写入数据异常，导入中断！', '错误信息', MB_ICONERROR);
    exit;
  end;

end;

end.

