unit U_CollectImport;

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,
  dxSkinWXI, dxScrollbarAnnotations;

type
  TfrmCollectImport = 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;
    cxGrid1Level1: TcxGridLevel;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TSaveClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
    procedure initGrid();
    procedure SaveDate();
    { Private declarations }
  public
    FFFID, FCTID, FCTName: string;
    fInvoiceFlag, F_ComPany: string;
    { Public declarations }
  end;

var
  frmCollectImport: TfrmCollectImport;

implementation

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

procedure TfrmCollectImport.SaveDate();
var
  MaxNo, MaxNo2: string;
  fDate, fconNO: string;
begin
  ADOQueryCmd.Connection.BeginTrans;
  try
    with CDS_Import do
    begin
      first;
      while not eof do
      begin

        if CDS_Import.fieldbyname('BCIOID').AsString = '' then
        begin
          if GetLSNo(ADOQueryTmp, MaxNo, 'DR', 'Bs_Cloth_IO', 4, 1) = False then
            raise Exception.Create('取最大号失败！');

        end
        else
          MaxNo := CDS_Import.fieldbyname('ConMID').AsString;

        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.add('select * from Bs_Cloth_IO where BCIOID=' + quotedstr(Trim(MaxNo)));
          open;
        end;
        if ADOQueryCmd.IsEmpty then
        begin
          ADOQueryCmd.append;
          ADOQueryCmd.FieldByName('FillId').Value := Trim(DCode);
          ADOQueryCmd.FieldByName('Filler').Value := Trim(DName);
        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, 'Bs_Cloth_IO', 0);
        ADOQueryCmd.fieldbyname('BCIOID').Value := MaxNo;
        ADOQueryCmd.fieldbyname('StkID').Value := MaxNo;

        CDS_Import.Edit;
        CDS_Import.fieldbyname('BCIOID').Value := MaxNo;
        CDS_Import.fieldbyname('StkID').Value := MaxNo;
        CDS_Import.Post;

        ADOQueryCmd.fieldbyname('BCIOID').Value := trim(CDS_Import.fieldbyname('BCIOID').AsString);
        ADOQueryCmd.fieldbyname('StkID').Value := trim(CDS_Import.fieldbyname('BCIOID').AsString);
        ADOQueryCmd.FieldByName('STKName').Value := Trim('贸易成品布');
        ADOQueryCmd.FieldByName('Workshop').Value := Trim('印染一车间');
        ADOQueryCmd.FieldByName('IOFlag').Value := Trim('入库');
        ADOQueryCmd.FieldByName('IOQtyFlag').Value := 1;
        ADOQueryCmd.FieldByName('IOType').Value := Trim('期初入库');
        ADOQueryCmd.FieldByName('StkCoName').Value := Trim('本厂仓库');
        ADOQueryCmd.FieldByName('QtyUnit').Value := Trim('M');

        ADOQueryCmd.fieldbyname('StkPiece').Value := CDS_Import.fieldbyname('Piece').asfloat;
        ADOQueryCmd.fieldbyname('StkQty').Value := CDS_Import.fieldbyname('Qty').asfloat;
        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 TfrmCollectImport.initGrid();
begin
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select * from Bs_Cloth_IO  where 1 <> 1');
    open;
  end;

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

procedure TfrmCollectImport.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmCollectImport.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 TfrmCollectImport.TSaveClick(Sender: TObject);
begin
  if CDS_Import.Locate('Piece', null, []) then
  begin
    application.MessageBox('匹数不能为空', '提示');
    exit;
  end;

  if CDS_Import.Locate('Qty', null, []) then
  begin
    application.MessageBox('数量不能为空', '提示');
    exit;
  end;

  if CDS_Import.Locate('Piece', 0, []) then
  begin
    application.MessageBox('匹数不能为空', '提示');
    exit;
  end;

  if CDS_Import.Locate('Qty', 0, []) then
  begin
    application.MessageBox('数量不能为空', '提示');
    exit;
  end;

  SaveDate();
end;

procedure TfrmCollectImport.FormDestroy(Sender: TObject);
begin
  frmCollectImport := nil;
end;

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

procedure TfrmCollectImport.ToolButton3Click(Sender: TObject);
begin
  WriteCxGrid(Self.Caption, Tv1, '成品汇总入库');
end;

procedure TfrmCollectImport.ToolButton4Click(Sender: TObject);
var
  excelApp, WorkBook: Variant;
  i, j, k, LX, ExcelRowCount, iBuyName: 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;

        if trim(WorkBook.WorkSheets[1].Cells[1, i].value) = '入库时间' then
          iBuyName := i;
      end;
    end;
  except
    application.MessageBox('加载数组错误！', '错误信息', MB_ICONERROR);
    exit;
  end;

  try
    for i := 2 to ExcelRowCount do
    begin

      if trim(WorkBook.WorkSheets[1].Cells[i, iBuyName].value) = '' then
      begin
        Continue;
      end;

      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('ConDate').Value := CDS_Import.fieldbyname('BegDate').AsDateTime;
//
//        with ADOQueryTmp do
//        begin
//          Close;
//          sql.Clear;
//          sql.Add(' select * from BS_Cloth_Info where DCNO =''' + (CDS_Import.fieldbyname('DCNO').AsString) + '''');
//          sql.Add(' and MDNO =''' + (CDS_Import.fieldbyname('MDNO').AsString) + '''');
//          open;
//        end;
//
//        CDS_Import.fieldbyname('C_Code').Value := trim(ADOQueryTmp.fieldbyname('C_Code').AsString);
//        CDS_Import.fieldbyname('KuanNo').Value := trim(ADOQueryTmp.fieldbyname('KuanNo').AsString);
//        CDS_Import.fieldbyname('ISZY').Value := trim(ADOQueryTmp.fieldbyname('ISZY').AsString);
//        CDS_Import.fieldbyname('GPSFactory').Value := trim(ADOQueryTmp.fieldbyname('GPSFactory').AsString);
//        CDS_Import.fieldbyname('GPSNo').Value := trim(ADOQueryTmp.fieldbyname('GPSNo').AsString);
//        CDS_Import.fieldbyname('CarNo').Value := trim(ADOQueryTmp.fieldbyname('CarNo').AsString);
//        CDS_Import.fieldbyname('DJNo').Value := trim(ADOQueryTmp.fieldbyname('DJNo').AsString);
//        CDS_Import.fieldbyname('CLQK').Value := trim(ADOQueryTmp.fieldbyname('CLQK').AsString);
//        CDS_Import.fieldbyname('WZ').Value := trim(ADOQueryTmp.fieldbyname('WZ').AsString);

        Post;
      end;
    end;
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
  except
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
    exit;
  end;

end;

end.

