unit U_PositionInPut;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxCalendar, cxDropDownEdit, ComCtrls, ToolWin,
  cxGridLevel, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxClasses, cxControls, cxGridCustomView, cxGrid, cxGridCustomPopupMenu,
  cxGridPopupMenu, ADODB, DBClient, cxButtonEdit, cxTextEdit, StdCtrls, ExtCtrls,
  StrUtils, U_BaseInput, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxDateRanges, dxScrollbarAnnotations, cxContainer, dxBarBuiltInMenu,
  System.ImageList, Vcl.ImgList, cxImageList, System.Win.ComObj, dxSkinsCore,
  dxSkinsDefaultPainters;

type
  TfrmPositionInPut = class(TfrmBaseInput)
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column2: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    ToolBar1: TToolBar;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    TBSave: TToolButton;
    TBClose: TToolButton;
    DataSource3: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    ToolButton1: TToolButton;
    Tv1Column1: TcxGridDBColumn;
    Panel1: TPanel;
    cxtxtdtQS: TcxTextEdit;
    cxtxtdtJS: TcxTextEdit;
    Label4: TLabel;
    Label1: TLabel;
    QZ: TcxTextEdit;
    Label2: TLabel;
    Label3: TLabel;
    POSName: TcxTextEdit;
    CDS_LM: TClientDataSet;
    OpenDialog1: TOpenDialog;
    ToolButton2: TToolButton;
    Tv1Column2: TcxGridDBColumn;
    ADOQuery1: TADOQuery;
    ADOQuery2: TADOQuery;
    Tv1Column3: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBAddClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
  private
    { Private declarations }
    function SaveCKData(): Boolean;
  public
    { Public declarations }
    FBCId, FSTKName: string;
  end;

var
  frmPositionInPut: TfrmPositionInPut;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmPositionInPut.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  Action := caFree;
end;

procedure TfrmPositionInPut.TBAddClick(Sender: TObject);
var
  I: Integer;
begin
  for I := strtointdef(cxtxtdtQS.Text, 0) to strtointdef(cxtxtdtJS.Text, 0) do
  begin
    with CDS_Sub do
    begin
      Append;

      CDS_Sub.fieldbyname('POSNO').Value := Trim(QZ.Text) + RightStr(('0000' + inttostr(I)), 5);

      CDS_Sub.fieldbyname('POSName').Value := Trim(POSName.Text);
      Post;
    end;
  end;

end;

function TfrmPositionInPut.SaveCKData(): Boolean;
var
  MaxNo, MWBID: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Sub.DisableControls;
    with CDS_Sub do
    begin
      First;
      while not eof do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from BS_Position where POSID=''' + Trim(CDS_Sub.fieldbyname('POSID').AsString) + '''');
          open;
        end;
        MWBID := Trim(ADOQueryTemp.fieldbyname('POSID').AsString);
        if Trim(MWBID) = '' then
        begin
          if GetLSNo(ADOQueryCmd, MaxNo, 'M', 'BS_Position', 4, 1) = False then
            raise Exception.Create('取最大号失败！');
          with ADOQuery1 do
          begin
            Close;
            sql.Clear;
            sql.Add('select count(*) as AA from BS_Position where POSNO=''' + Trim(CDS_Sub.FieldByName('POSNO').ASString) + '''');
            Open;
            if FieldByName('AA').AsInteger > 0 then
            begin
              ADOQueryCmd.Connection.RollbackTrans;
              Result := False;
              Application.MessageBox('编号重复！', '提示', 0);
              CDS_Sub.EnableControls;
              Exit;
            end;
          end;

          with ADOQuery2 do
          begin
            Close;
            sql.Clear;
            sql.Add('select count(*) as KW from BS_Position where POSName=''' + Trim(CDS_Sub.FieldByName('POSName').ASString) + '''');
            Open;
            if FieldByName('KW').AsInteger > 0 then
            begin
              ADOQueryCmd.Connection.RollbackTrans;
              Result := False;
              Application.MessageBox('库位重复！', '提示', 0);
              CDS_Sub.EnableControls;
              Exit;
            end;
          end;
        end
        else
        begin
          MaxNo := Trim(MWBID);
        end;

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from BS_Position where POSID=''' + Trim(MaxNo) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(MWBID) = '' then
          begin
            Append;
            FieldByName('Fillid').Value := Trim(Dcode);
            FieldByName('Filler').Value := Trim(DName)
          end
          else
          begin
            Edit;
            FieldByName('Editid').Value := Trim(Dcode);
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDate(ADOQueryTemp);
          end;
          FieldByName('POSID').Value := Trim(MaxNo);
//          FieldByName('STKName').Value := Trim(FSTKName);
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Sub, 'BS_Position', 0);
          Post;
        end;

        Edit;
        FieldByName('POSID').Value := Trim(MaxNo);
        Post;
        Next;
      end;
    end;
    CDS_Sub.EnableControls;

    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end;

procedure TfrmPositionInPut.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmPositionInPut.FormShow(Sender: TObject);
var
  fsj: string;
begin
  inherited;
  ReadCxGrid(trim(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;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(' from BS_Position A');
    sql.Add(' where POSID=''' + Trim(FBCId) + '''');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_Sub);
  SInitCDSData(ADOQueryTemp, CDS_Sub);
end;

procedure TfrmPositionInPut.TBDelClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if Trim(CDS_Sub.fieldbyname('POSID').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;

    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' delete BS_Position where POSID=''' + Trim(CDS_Sub.fieldbyname('POSID').AsString) + '''');
        ExecSQL;
      end;

      ADOQueryCmd.Connection.CommitTrans;
      CDS_Sub.Delete;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('删除异常!', '提示', 0);
    end;

  end
  else
  begin
    CDS_Sub.Delete;
  end;

end;

procedure TfrmPositionInPut.TBSaveClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;

  if CDS_Sub.Locate('POSNO', null, []) = True then
  begin
    Application.MessageBox('编号不能为空!', '提示', 0);
    Exit;
  end;

  if SaveCKData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    ModalResult := 1;
    Exit;
  end;
end;

procedure TfrmPositionInPut.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption), Tv1, '染色坯布仓库');
end;

procedure TfrmPositionInPut.ToolButton2Click(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_Sub do
      begin
        Append;
        CDS_LM.First;
        while not CDS_LM.Eof do
        begin
          if CDS_LM.FieldByName('LXH').AsInteger > 0 then
            CDS_Sub.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;
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
  except
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
    exit;
  end;

end;

end.

