unit U_KuWeiInPut;

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
  TfrmKuWeiInPut = 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;
    HuoJia: TBtnEditA;
    KuWeiQty: TEdit;
    Button1: TButton;
    PrtFlag: TEdit;
    Label9: TLabel;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    ClientDataSet2: TClientDataSet;
    cxPageControl1: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    cxTabSheet2: TcxTabSheet;
    Label12: TLabel;
    Ceng: TComboBox;
    Label2: TLabel;
    CengQty: TComboBox;
    Label3: TLabel;
    Label4: TLabel;
    Label1: TLabel;
    Label5: TLabel;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Panel2: TPanel;
    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 HuoJiaBtnClick(Sender: TObject);
    procedure QuYuDblClick(Sender: TObject);
    procedure HuoJiaDblClick(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
  frmKuWeiInPut: TfrmKuWeiInPut;
implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun;

{$R *.dfm}

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

procedure TfrmKuWeiInPut.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(HuoJia.Text)<>'' then
    begin
      sql.Add(' and isnull(HuoJia,'''')='''+Trim(HuoJia.Text)+'''');
    end;
    if cxPageControl1.ActivePageIndex=0 then
    begin
      if Trim(Ceng.Text)<>'' then
      begin
        sql.Add(' and isnull(Ceng,'''')='''+Trim(Ceng.Text)+'''');
      end;
    end;
    sql.Add(' and Valid=''Y'' ');
    Open;
  end;
  SCreateCDS20(ADOQuery1,ClientDataSet1);
  SInitCDSData20(ADOQuery1,ClientDataSet1);
end;



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

function TfrmKuWeiInPut.SaveData():Boolean;
var
  maxno: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(KuWeiQty.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('HuoJia').Value:=Trim(HuoJia.Text);
        FieldByName('Ceng').Value:=Trim(Ceng.Text);
        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(HuoJia,'''')='''+Trim(HuoJia.Text)+'''');
        sql.Add(' and isnull(Ceng,'''')='''+Trim(Ceng.Text)+'''');
        sql.Add('),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 KWName=RTrim(isnull(QuYu,''''))+''-''+RTrim(isnull(HuoJia,''''))+''-'' ');
        sql.Add('+RTrim(isnull(Ceng,''''))+''-''+RTrim(isnull(KWNo,''''))');
        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 TfrmKuWeiInPut.SaveDataDuoCeng():Boolean;
var
  maxno:String;
  i,c: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 c:=1 to StrToInt(CengQty.Text) do
    begin
      for i:=1 to StrToInt(KuWeiQty.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('HuoJia').Value:=Trim(HuoJia.Text);
          FieldByName('Ceng').Value:=IntToStr(c);
          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(HuoJia,'''')='''+Trim(HuoJia.Text)+'''');
          sql.Add(' and isnull(Ceng,'''')='''+Trim(IntToStr(c))+'''');
          sql.Add('),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 KWName=RTrim(isnull(QuYu,''''))+''-''+RTrim(isnull(HuoJia,''''))+''-'' ');
          sql.Add('+RTrim(isnull(Ceng,''''))+''-''+RTrim(isnull(KWNo,''''))');
          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 TfrmKuWeiInPut.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;
  RM1.LoadFromFile(fPrintFile);
  //RM1.ShowReport;
  RM1.PrintReport;
end;
Procedure TfrmKuWeiInPut.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  ModalResult:=1;
end;


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

procedure TfrmKuWeiInPut.Button1Click(Sender: TObject);
var
  FInt:Integer;
begin
  if Trim(QuYu.Text)='' then
  begin
    Application.MessageBox('区域不能为空!','提示',0);
    Exit;
  end;
  if Trim(HuoJia.Text)='' then
  begin
    Application.MessageBox('货架不能为空!','提示',0);
    Exit;
  end;
  if cxPageControl1.ActivePageIndex=0 then
  begin
    if Trim(ceng.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 cxPageControl1.ActivePageIndex=0 then
  begin
    if Trim(KuWeiQty.Text)='' then
    begin
      Application.MessageBox('增加库位数量不能为空!','提示',0);
      Exit;
    end;
    if Trim(KuWeiQty.Text)='' then
    begin
      Application.MessageBox('增加库位数量不能为空!','提示',0);
      Exit;
    end;
    if TryStrToInt(KuWeiQty.Text,FInt)=False then
    begin
      Application.MessageBox('增加库位数量非法数字!','提示',0);
      Exit;
    end;
    if StrToInt(KuWeiQty.Text)>99 then
    begin
      Application.MessageBox('增加库位数量不能大于100！','提示',0);
      Exit;
    end;
  end else
  if cxPageControl1.ActivePageIndex=1 then
  begin
    if Trim(KuWeiQty.Text)='' then
    begin
      Application.MessageBox('每层库位数量不能为空!','提示',0);
      Exit;
    end;
    if Trim(KuWeiQty.Text)='' then
    begin
      Application.MessageBox('每层库位数量不能为空!','提示',0);
      Exit;
    end;
    if TryStrToInt(KuWeiQty.Text,FInt)=False then
    begin
      Application.MessageBox('每层库位数量非法数字!','提示',0);
      Exit;
    end;
    if StrToInt(KuWeiQty.Text)>99 then
    begin
      Application.MessageBox('每层库位数量不能大于100！','提示',0);
      Exit;
    end;
  end;
  Panel2.Visible:=True;
  Panel2.Refresh;
  if cxPageControl1.ActivePageIndex=0 then
  begin
    if SaveData() then
    begin
      InitData();
      Application.MessageBox('操作成功~','提示',0);
      Ceng.ItemIndex:=Ceng.ItemIndex+1;
    end;
  end else
  if cxPageControl1.ActivePageIndex=1 then
  begin
    if SaveDataDuoCeng() then
    begin
      InitData();
      Application.MessageBox('操作成功~','提示',0);
      Ceng.ItemIndex:=Ceng.ItemIndex+1;
    end;
  end;
  Panel2.Visible:=False;
  
end;

procedure TfrmKuWeiInPut.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 TfrmKuWeiInPut.HuoJiaBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='GGHuoJia';
      flagname:='货架';
      if ShowModal=1 then
      begin
        HuoJia.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  Ceng.ItemIndex:=0;
  InitData();
end;

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

procedure TfrmKuWeiInPut.HuoJiaDblClick(Sender: TObject);
begin
  HuoJia.Text:='';
  InitData();
end;

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

procedure TfrmKuWeiInPut.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 TfrmKuWeiInPut.N1Click(Sender: TObject);
begin
  SelOKNo(ClientDataSet1,True);
end;

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

end.
