unit U_KuWeiInPutCeng;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls,
  cxGridCustomView, cxGrid, cxMemo, cxRichEdit, ComCtrls, cxContainer,
  cxTextEdit, cxMaskEdit, cxButtonEdit, StdCtrls, ToolWin, DBClient, ADODB,
  ExtCtrls, BtnEdit, cxCalendar,StrUtils, cxDropDownEdit,jpeg,
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdFTP, cxPC,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGroupBox, cxCheckBox,
  RM_Dataset, RM_System, RM_Common, RM_Class, RM_GridReport,IniFiles, Menus;

type
  TfrmKuWeiInPutCeng = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    ADOQuery1: TADOQuery;
    Label26: TLabel;
    CDS_Print: TClientDataSet;
    RM1: TRMGridReport;
    Panel5: TPanel;
    RMDBMain: TRMDBDataSet;
    Panel1: TPanel;
    Label14: TLabel;
    Panel4: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column7: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column1: TcxGridDBColumn;
    ClientDataSet1: TClientDataSet;
    DataSource1: TDataSource;
    ToolButton1: TToolButton;
    Label10: TLabel;
    QuYu: TBtnEditA;
    Label11: TLabel;
    FenQu: TBtnEditA;
    Button1: TButton;
    PrtFlag: TEdit;
    Label9: TLabel;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    ClientDataSet2: TClientDataSet;
    cxPageControl1: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    cxTabSheet2: TcxTabSheet;
    Label1: TLabel;
    Label5: TLabel;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Panel2: TPanel;
    Label3: TLabel;
    Label4: TLabel;
    CengQty: TComboBox;
    Label6: TLabel;
    Pai: TEdit;
    Label12: TLabel;
    Label7: TLabel;
    PaiQty: TEdit;
    Label8: TLabel;
    v1Column5: TcxGridDBColumn;
    Label2: TLabel;
    ComboBox1: TComboBox;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure PrtFlagClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure QuYuBtnClick(Sender: TObject);
    procedure FenQuBtnClick(Sender: TObject);
    procedure QuYuDblClick(Sender: TObject);
    procedure FenQuDblClick(Sender: TObject);
    procedure CengChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
  private
    procedure InitData();
    function  SaveData():Boolean;
    function SaveDataDuoCeng():Boolean;
    procedure PrtData(KWID:String);
    { Private declarations }
  public
    canshu1:String;
    PState:Integer;
    FMainId:String;
    { Public declarations }
  end;

var
  frmKuWeiInPutCeng: TfrmKuWeiInPutCeng;
implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun;

{$R *.dfm}

procedure TfrmKuWeiInPutCeng.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmKuWeiInPutCeng.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * ');
    sql.Add(' from KuWei   ');
    if Trim(QuYu.Text)='' then
    begin
      sql.Add(' where 1=2');
    end else
    begin
      sql.Add(' where isnull(QuYu,'''')='''+Trim(QuYu.Text)+'''');
    end;
    if Trim(FenQu.Text)<>'' then
    begin
      sql.Add(' and isnull(FenQu,'''')='''+Trim(FenQu.Text)+'''');
    end;
    sql.Add(' and Valid=''Y'' ');
    Open;
  end;
  SCreateCDS20(ADOQuery1,ClientDataSet1);
  SInitCDSData20(ADOQuery1,ClientDataSet1);
end;



procedure TfrmKuWeiInPutCeng.FormShow(Sender: TObject);
begin
  InitData();
end;

function TfrmKuWeiInPutCeng.SaveData():Boolean;
var
  maxno,PaiStr:String;
  i:Integer;
begin
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select KWID from KuWei where 1=2');
    open;
  end;
  SCreateCDS20(ADOTemp,ClientDataSet2);
  SInitCDSData20(ADOTemp,ClientDataSet2);
  Result:=False;
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    for i:=1 to StrToInt(CengQty.Text) do
    begin
      if GetLSNoHZ(ADOCmd,maxno,'KW','KuWei',4,0,0)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取库位最大号失败!','提示',0);
        Exit;
      end;
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        SQL.Add('select * from KuWei where KWId='''+Trim(maxno)+'''');
        Open;
      end;
      with ADOCmd do
      begin
        Append;
        FieldByName('KWId').Value:=Trim(maxno);
        FieldByName('KWType').Value:='GG';//挂钩
        FieldByName('Filler').Value:=Trim(DName);
        FieldByName('FillerCode').Value:=Trim(DCode);
        FieldByName('QuYu').Value:=Trim(QuYu.Text);
        FieldByName('FenQu').Value:=Trim(FenQu.Text);
        if StrToInt(Pai.Text)<10 then
        begin
          FieldByName('Pai').Value:='0'+Trim(IntToStr(StrToInt(Pai.Text)));
        end else
        begin
          FieldByName('Pai').Value:=Trim(Pai.Text);
        end;
        FieldByName('Valid').Value:='Y';
        Post;
      end;
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update KuWei Set KWNoInt=isnull((select max(KWNoInt) from KuWei ');
        sql.Add(' where KWType=''GG'' and  isnull(QuYu,'''')='''+Trim(QuYu.Text)+'''');
        sql.Add(' and isnull(FenQu,'''')='''+Trim(FenQu.Text)+'''');
        if StrToInt(Pai.Text)<10 then
        begin
          PaiStr:='0'+Trim(IntToStr(strtoint(Pai.Text)));
        end else
        begin
          PaiStr:=Trim(Pai.Text)
        end;
        sql.Add(' and isnull(Pai,'''')='''+PaiStr+'''');
        sql.Add(' and Valid=''Y''),0)+1');
        sql.Add(' where KWID='''+Trim(maxno)+'''');
        ExecSQL;
      end;
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update KuWei Set KWNo=''0''+RTrim(Cast(KWNoInt As varchar(10))) ');
        sql.Add(' where KWID='''+Trim(maxno)+''' and KWNoInt<10');
        ExecSQL;
      end;
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update KuWei Set KWNo=RTrim(Cast(KWNoInt As varchar(10))) ');
        sql.Add(' where KWID='''+Trim(maxno)+''' and KWNoInt>9');
        ExecSQL;
      end;
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update KuWei Set Ceng=KWNo ');
        sql.Add(' where KWID='''+Trim(maxno)+'''');
        ExecSQL;
      end;
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update KuWei Set KWName=RTrim(isnull(QuYu,''''))+''-''+RTrim(isnull(FenQu,''''))+''区-'' ');
        sql.Add('+RTrim(isnull(Pai,''''))+''排-''+RTrim(isnull(Ceng,''''))+''层'' ');
        sql.Add(' where KWID='''+Trim(maxno)+'''');
        ExecSQL;
      end;
      with ClientDataSet2 do
      begin
        Append;
        FieldByName('KWID').Value:=Trim(maxno);
        Post;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    if Trim(PrtFlag.Text)<>'' then
    begin
      with ClientDataSet2 do
      begin
        First;
        while not eof do
        begin
          PrtData(Trim(ClientDataSet2.fieldbyname('KWID').AsString));
          Next;
        end;
      end;
    end;
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;
function TfrmKuWeiInPutCeng.SaveDataDuoCeng():Boolean;
var
  maxno,PaiStr:String;
  p,c,i,PInt:Integer;
begin
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select KWID from KuWei where 1=2');
    open;
  end;
  SCreateCDS20(ADOTemp,ClientDataSet2);
  SInitCDSData20(ADOTemp,ClientDataSet2);
  Result:=False;
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    for P:=1 to StrToInt(PaiQty.Text) do
    begin
      for C:=1 to StrToInt(CengQty.Text) do
      begin
        if GetLSNoHZ(ADOCmd,maxno,'KW','KuWei',4,0,0)=False then
        begin
          ADOCmd.Connection.RollbackTrans;
          Application.MessageBox('取库位最大号失败!','提示',0);
          Exit;
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add('select * from KuWei where KWId='''+Trim(maxno)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          Append;
          FieldByName('KWId').Value:=Trim(maxno);
          FieldByName('KWType').Value:='GG';//挂钩
          FieldByName('Filler').Value:=Trim(DName);
          FieldByName('FillerCode').Value:=Trim(DCode);
          FieldByName('QuYu').Value:=Trim(QuYu.Text);
          FieldByName('FenQu').Value:=Trim(FenQu.Text);
          if c<10 then
          begin
            FieldByName('Ceng').Value:='0'+Trim(IntToStr(c));
          end else
          begin
            FieldByName('Ceng').Value:=Trim(IntToStr(c));
          end;
          FieldByName('Valid').Value:='Y';
          Post;
        end;
        //更新排
        if C=1 then
        begin
          with ADOCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select AA=isnull(Cast(max(Pai) as int),0)+1 from KuWei where KWType=''GG'' ');
            sql.Add(' and isnull(QuYu,'''')='''+Trim(QuYu.Text)+'''');
            sql.Add(' and isnull(FenQu,'''')='''+Trim(FenQu.Text)+'''');
            sql.Add(' and valid=''Y'' ');
            Open;
          end;
          PInt:=ADOCmd.fieldbyname('AA').Value;
          if PInt<10 then
          begin
            PaiStr:='0'+Trim(IntToStr(PInt));
          end else
          begin
            PaiStr:=Trim(IntToStr(PInt));
          end;
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update KuWei Set Pai='''+Trim(PaiStr)+'''');
          sql.Add(' where KWID='''+Trim(maxno)+'''');
          ExecSQL;
        end;
        //更新排
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update KuWei Set KWNo=Ceng ');
          sql.Add(' where KWID='''+Trim(maxno)+'''');
          ExecSQL;
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update KuWei Set KWNoInt=Cast(Ceng as int) ');
          sql.Add(' where KWID='''+Trim(maxno)+'''');
          ExecSQL;
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update KuWei Set KWName=RTrim(isnull(QuYu,''''))+''-''+RTrim(isnull(FenQu,''''))+''区-'' ');
          sql.Add('+RTrim(isnull(Pai,''''))+''排-''+RTrim(isnull(Ceng,''''))+''层'' ');
          sql.Add(' where KWID='''+Trim(maxno)+'''');
          ExecSQL;
        end;
        with ClientDataSet2 do
        begin
          Append;
          FieldByName('KWID').Value:=Trim(maxno);
          Post;
        end;
      end;
    end;

    ADOCmd.Connection.CommitTrans;
    if Trim(PrtFlag.Text)<>'' then
    begin
      with ClientDataSet2 do
      begin
        First;
        while not eof do
        begin
          PrtData(Trim(ClientDataSet2.fieldbyname('KWID').AsString));
          Next;
        end;
      end;
    end;
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;
procedure TfrmKuWeiInPutCeng.PrtData(KWID:String);
var
   fPrintFile,Txt,fImagePath:string;
   i,j:Integer;
   Moudle: THandle;
   Makebar:TMakebar;
   Mixtext:TMixtext;
begin
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\库位标签.rmf';
  if FileExists(fPrintFile)=False then
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\库位标签.rmf'),'提示',0);
    exit;
  end;
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from KuWei where KWID='''+Trim(KWID)+'''');
    Open;
  end;
  SCreateCDS20(ADOTemp,CDS_Print);
  SInitCDSData20(ADOTemp,CDS_Print);
  try
    Moudle:=LoadLibrary('MakeQRBarcode.dll');
    @Makebar:=GetProcAddress(Moudle,'Make');
    @Mixtext:=GetProcAddress(Moudle,'MixText');
    Txt:=Trim(KWID);
    fImagePath:=ExtractFilePath(Application.ExeName)+'image\temp.bmp';
    if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName)+'image')) then
      CreateDirectory(pchar(ExtractFilePath(Application.ExeName)+'image'),nil);
    if FileExists(fImagePath) then DeleteFile(fImagePath);
    Makebar(pchar(Txt),Length(Txt),3,3,0,PChar(fImagePath),3);
  except
    application.MessageBox('二维码生成失败！','提示信息',MB_ICONERROR);
    exit;
  end;
  RMVariables['QRBARCODE']:=fImagePath;
  if Trim(ComboBox1.Text)='→' then
  begin
    RMVariables['FangXiang']:='→→→→';
  end else
  if Trim(ComboBox1.Text)='←' then
  begin
    RMVariables['FangXiang']:='←←←←';
  end;
  RM1.LoadFromFile(fPrintFile);
  //RM1.ShowReport;
  RM1.PrintReport;
end;
Procedure TfrmKuWeiInPutCeng.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  ModalResult:=1;
end;


procedure TfrmKuWeiInPutCeng.PrtFlagClick(Sender: TObject);
begin
  if Trim(PrtFlag.Text)='' then
  begin
    PrtFlag.Text:='√';
  end else
  begin
    PrtFlag.Text:='';
  end;
end;

procedure TfrmKuWeiInPutCeng.Button1Click(Sender: TObject);
var
  FInt:Integer;
begin
  if Trim(QuYu.Text)='' then
  begin
    Application.MessageBox('区域不能为空!','提示',0);
    Exit;
  end;
  if Trim(FenQu.Text)='' then
  begin
    Application.MessageBox('分区不能为空!','提示',0);
    Exit;
  end;
  if cxPageControl1.ActivePageIndex=0 then
  begin
    if Trim(pai.Text)='' then
    begin
      Application.MessageBox('第几排不能为空!','提示',0);
      Exit;
    end;
    if TryStrToInt(pai.Text,FInt)=False then
    begin
      Application.MessageBox('第几排非法数字!','提示',0);
      Exit;
    end;
    if StrToInt(pai.Text)>99 then
    begin
      Application.MessageBox('第几排不能大于100！','提示',0);
      Exit;
    end;
  end else
  if cxPageControl1.ActivePageIndex=1 then
  begin
    if Trim(paiQty.Text)='' then
    begin
      Application.MessageBox('增加排数不能为空!','提示',0);
      Exit;
    end;
    if TryStrToInt(paiQty.Text,FInt)=False then
    begin
      Application.MessageBox('增加排数非法数字!','提示',0);
      Exit;
    end;
    if StrToInt(paiQty.Text)>99 then
    begin
      Application.MessageBox('增加排数不能大于100！','提示',0);
      Exit;
    end;
  end;
  if cxPageControl1.ActivePageIndex=0 then
  begin
    if Trim(CengQty.Text)='' then
    begin
      Application.MessageBox('增加层数不能为空!','提示',0);
      Exit;
    end;
  end else
  if cxPageControl1.ActivePageIndex=1 then
  begin
    if Trim(CengQty.Text)='' then
    begin
      Application.MessageBox('每排层数不能为空!','提示',0);
      Exit;
    end;
  end;
  if Trim(PrtFlag.Text)<>'' then
  begin
    if Trim(ComboBox1.Text)='' then
    begin
      Application.MessageBox('方向不能为空!','提示',0);
      Exit;
    end;
  end;
  Panel2.Visible:=True;
  Panel2.Refresh;
  if cxPageControl1.ActivePageIndex=0 then
  begin
    if SaveData() then
    begin
      InitData();
      Application.MessageBox('操作成功~','提示',0);
    end;
  end else
  if cxPageControl1.ActivePageIndex=1 then
  begin
    if SaveDataDuoCeng() then
    begin
      InitData();
      Application.MessageBox('操作成功~','提示',0);
    end;
  end;
  Panel2.Visible:=False;
  
end;

procedure TfrmKuWeiInPutCeng.QuYuBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='GGQuYu';
      flagname:='区域';
      if ShowModal=1 then
      begin
        QuYu.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  InitData();
end;

procedure TfrmKuWeiInPutCeng.FenQuBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='GGFenQu';
      flagname:='分区';
      if ShowModal=1 then
      begin
        FenQu.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  InitData();
end;

procedure TfrmKuWeiInPutCeng.QuYuDblClick(Sender: TObject);
begin
  QuYu.Text:='';
  InitData();
end;

procedure TfrmKuWeiInPutCeng.FenQuDblClick(Sender: TObject);
begin
  FenQu.Text:='';
  InitData();
end;

procedure TfrmKuWeiInPutCeng.CengChange(Sender: TObject);
begin
  InitData();
end;

procedure TfrmKuWeiInPutCeng.ToolButton1Click(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then Exit;
  if ClientDataSet1.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  ClientDataSet1.DisableControls;
  with ClientDataSet1 do
  begin
    First;
    while Locate('SSel',True,[]) do
    begin
      PrtData(Trim(ClientDataSet1.fieldbyname('KWID').AsString));
      Edit;
      FieldByName('SSel').Value:=False;
    end;
  end;
  ClientDataSet1.EnableControls;
end;

procedure TfrmKuWeiInPutCeng.N1Click(Sender: TObject);
begin
  SelOKNo(ClientDataSet1,True);
end;

procedure TfrmKuWeiInPutCeng.N2Click(Sender: TObject);
begin
  SelOKNo(ClientDataSet1,False);
end;

end.
