unit uPutup;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, cxControls, cxContainer, cxEdit, cxTextEdit, ComCtrls,
  dxBar, ImgList, ExtCtrls, dxExEdtr, dxTL, dxCntner, PublicDBUnit,uFunMesClient,
  MobileCard, MyDataStruct, cxGroupBox, cxRadioGroup, Buttons, DB, ADODB,uFuncom,Common,
  dxBarExtItems,uFunMesClient;
Const
  FristWrk = '初调';
type
  TfrmPutup = class(TForm)
    ImageList: TImageList;
    dxBarManager1: TdxBarManager;
    tbSumbit: TdxBarButton;
    tbDelSub: TdxBarButton;
    tbClose: TdxBarButton;
    tbLeadIn: TdxBarButton;
    tbLeadOut: TdxBarButton;
    Timer1: TTimer;
    Panel2: TPanel;
    Panel1: TPanel;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    Panel4: TPanel;
    Label1: TLabel;
    imgSpeaker: TImage;
    lblSpeaker: TLabel;
    edtCode: TcxTextEdit;
    GroupBox1: TGroupBox;
    lblProductNo: TLabel;
    lblProductName: TLabel;
    btnOk: TSpeedButton;
    btnEnter: TdxBarButton;
    grp1: TcxRadioGroup;
    Splitter1: TSplitter;
    tlSetWrkGrp: TdxTreeList;
    dxTreeListColumn1: TdxTreeListColumn;
    dxTreeListColumn2: TdxTreeListColumn;
    dxTreeListColumn3: TdxTreeListColumn;
    PageControl2: TPageControl;
    dxBarButton1: TdxBarButton;
    dxBarButton2: TdxBarButton;
    qryCom1: TADOQuery;
    ADOStoredProc1: TADOStoredProc ;
    MytreeList: TdxTreeList;
    MytreeListColumn1: TdxTreeListColumn;
    MytreeListColumn2: TdxTreeListColumn;
    MytreeListColumn3: TdxTreeListColumn;
    MytreeListColumn4: TdxTreeListColumn;
    tishiNum: TdxBarSpinEdit;
    dxBarStatic1: TdxBarStatic;
    procedure edtCodeKeyPress(Sender: TObject; var Key: Char);
    procedure tbSumbitClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure tbDelSubClick(Sender: TObject);
    procedure tbLeadInClick(Sender: TObject);
    procedure grp1Click(Sender: TObject);
    procedure btnEnterClick(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure FormShow(Sender: TObject);
    procedure tbCloseClick(Sender: TObject);
    procedure tlSetWrkGrpGetFooterCellText(Sender: TObject;
      ANode: TdxTreeListNode; AColumn, AFooterIndex: Integer;
      var AText: String);
    procedure tlSetWrkGrpCustomDrawFooter(Sender: TObject;
      ACanvas: TCanvas; ARect: TRect; ANode: TdxTreeListNode;
      AColumn: TdxTreeListColumn; var AText: String; var AColor: TColor;
      AFont: TFont; var AAlignment: TAlignment; var ADone: Boolean);
    procedure tlSetWrkGrpIsExistFooterCell(Sender: TObject;
      AColumn: Integer; var AExist: Boolean);
    procedure MytreeListGetFooterCellText(Sender: TObject;
      ANode: TdxTreeListNode; AColumn, AFooterIndex: Integer;
      var AText: String);
    procedure MytreeListIsExistFooterCell(Sender: TObject;
      AColumn: Integer; var AExist: Boolean);
  private
    TmpCodeList,RecordList: TList;
    TreeList: TList;
    CodeThreeRec: array[0..3] of String;
    procedure ClearObj(lst: TList);
    function GetTmpCodeLst: String;
    procedure InitCodeBringFace;
    procedure AddTreeList;
    procedure AddtoTmptab(Code,DuctId,DuctNO,pName: String);
    Procedure FillTree;    
    function  SameCode(Code: String): Boolean;
    //function NotExistCode(myCrtCode: String): Boolean;
    function SaveBaozhang : boolean;

  public
    procedure AddCodeRec(xCodeRec: TCodeRec);
    procedure ModiCodeRec(xNo: integer; xCodeRec: TCodeRec);
    procedure DeleteCodeRec(xNo: integer);

    procedure FillUptlSetWrkGrp;
    procedure SaveCodeBring;
    function  CodeIsEixst(Code: String): Boolean;
  end;

var
  frmPutup: TfrmPutup;

implementation

uses uMain;


{$R *.dfm}

{function TfrmPutup.NotExistCode(myCrtCode: String): Boolean;
Const
  GetWrkSt = 'Select PtCode From tabCodeRelation Where PtCode =''%s''';
begin
  Result := False;
  With dmPublic Do
    if ExcuteSQLCommand(Format(GetWrkSt, [myCrtCode]), 1) then
      Result := qryCom1.IsEmpty;
end;}

procedure TfrmPutup.InitCodeBringFace;
begin
  edtCode.Text := '';
  edtcode.SetFocus;
  CodeThreeRec[0] := '';
  CodeThreeRec[1] := '';
  CodeThreeRec[2] := '';
  CodeThreeRec[3] := '';
  lblProductNo.Caption := '商品型号：';
  lblProductName.Caption := '商品名称：';
  ClearObj(TmpCodeList);
  FillUptlSetWrkGrp;
end;

procedure TfrmPutup.SaveCodeBring;
begin
end;

function TfrmPutup.GetTmpCodeLst: String;
var
  i: integer;
  myCodeRec: pCodeRec;
begin
  Result := '';
  for i := 0 to TmpCodeList.Count -1 do
  begin
    myCodeRec := TmpCodeList[i];
    Result := Result + myCodeRec^.myCode;
    if i < TmpCodeList.Count -1 then
      Result := Result +  ',';
  end;
end;

procedure TfrmPutup.DeleteCodeRec(xNo: integer);
begin
  if xNo < TmpCodeList.Count then
  begin
    TmpCodeList.Delete(xNo);
    FillUptlSetWrkGrp
  end;

end;

procedure TfrmPutup.ModiCodeRec(xNo: integer; xCodeRec: TCodeRec);
var
  myCodeRec: pCodeRec;
begin
  if xNo < TmpCodeList.Count then
  begin
    myCodeRec := TmpCodeList[xNo];
    myCodeRec^.myCode := xCodeRec.myCode;
    myCodeRec^.DuctID := xCodeRec.DuctID;
    myCodeRec^.DuctName := xCodeRec.DuctName;
  end;
end;

procedure TfrmPutup.AddCodeRec(xCodeRec: TCodeRec);
var
  myCodeRec: pCodeRec;
begin
  New(myCodeRec);
  myCodeRec^.myCode := xCodeRec.myCode;
  myCodeRec^.DuctID := xCodeRec.DuctID;
  myCodeRec^.DuctNo := xCodeRec.DuctNO;
  myCodeRec^.DuctName := xCodeRec.DuctName;
  TmpCodeList.Add(myCodeRec)
end;

procedure TfrmPutup.ClearObj(lst: TList);
var
  i: integer;
begin
  for i := 0 to lst.Count -1 do
    Dispose(lst[i]);
  lst.Clear;
end;

procedure TfrmPutup.FillUptlSetWrkGrp;
var
  i: integer;
  myCodeRec: pCodeRec;
begin
  tlSetWrkGrp.BeginUpdate;
  tlSetWrkGrp.ClearNodes;
  for i := 0 to TmpCodeList.Count -1 do
  begin
    myCodeRec := TmpCodeList[i];
    With tlSetWrkGrp.Add Do
    begin
      Strings[0] := myCodeRec^.myCode;
      Strings[1] := myCodeRec^.Ductno;
      Strings[2] := myCodeRec^.DuctName;
    end;
  end;
  tlSetWrkGrp.EndUpdate;
end;

procedure TfrmPutup.edtCodeKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key = #13 then
    tbSumbitClick(Self);
end;

procedure TfrmPutup.tbSumbitClick(Sender: TObject);
Const
   Selsql = 'SELECT  tabProduct.ID , tabProduct.ProductNO,tabProduct.ProductName,tabCrtCode.iFlag FROM tabCrtCode INNER JOIN '
     + 'tabProduct ON tabCrtCode.DuctID = tabProduct.ID WHERE  tabCrtCode.TmpCode =''%s''';
var
  Command,S,S1,Barcode: String;
  iflag: integer;
  myCodeRec: pCodeRec;
begin
  Barcode := trim(edtCode.Text);
  if  not SameCode(Barcode)  then
  begin
    edtCode.Text := '';
    Exit;
  end;
  With dmPublic Do
    if ExcuteSQLCommand(Format(Selsql, [trim(Barcode)]), 1) then
    begin
      if Not qryCom1.IsEmpty then
      begin
        iflag := qryCom1.Fields[3].AsInteger;
        CodeThreeRec[0] := Barcode;
        CodeThreeRec[1] := qryCom1.Fields[0].AsString;
        CodeThreeRec[2] := qryCom1.Fields[1].AsString;
        CodeThreeRec[3] := qryCom1.Fields[2].AsString;
        lblProductNo.Caption := '商品型号：' + qryCom1.Fields[0].AsString;
        lblProductName.Caption := '商品名称：' + qryCom1.Fields[1].AsString;
        grp1.ItemIndex := iFlag -1;
        if (iFlag = 2) then
        begin
          S := 'Select tabCodeRelation2.ptCode,tabCodeRelation2.cldCode,tabproduct.Id,tabproduct.ProductNO,tabproduct.productName from tabCodeRelation2  inner join tabproduct on tabProduct.Id = ' 
           +'(select ductid from tabCrtCode where tabCrtCode.tmpCode = tabCodeRelation2.ptCode) where ptCode = ''' +  Barcode + '''';
          if ExcuteSQLCommand(S, 1) then
          begin
            TmpCodeList.Clear;
            if not qryCom1.IsEmpty then
              while not qryCom1.Eof do
              begin
                new(myCodeRec);
                myCodeRec^.myCode := qryCom1.Fields[1].AsString;
                myCodeRec^.DuctID := qryCom1.Fields[2].AsString;
                MyCodeRec^.DuctNo := qryCom1.Fields[3].AsString;
                myCodeRec^.DuctName := qryCom1.Fields[4].AsString;
                AddcodeRec(MycodeRec^);
                qryCom1.Next;
              end;
          end;
          With TfrmCard.Create(Application) Do
          begin
            GetCardNoData(2, GetTmpCodeLst, False);
            ShowModal;
            Command := Save;
            Free;
          end;
          if Command = 'Save' then SaveCodeBring
          else if Command = 'Clear' then InitCodeBringFace;
        end
        else if  iFlag = 1 then
        begin
          AddtoTmptab(CodeThreeRec[0],CodeThreeRec[1],CodeThreeRec[2], CodeThreeRec[3]);
        end;
        AddTreeList;
        InitCodeBringFace;
      end
      else
      begin
        imgSpeaker.Visible := True;
        lblSpeaker.Visible := True;
        lblSpeaker.Caption := '  没有此商品条码！';
        CodeThreeRec[0] := '';
        CodeThreeRec[1] := '';
        CodeThreeRec[2] := '';
        lblProductNo.Caption := '商品型号：';
        lblProductName.Caption := '商品名称：';
        edtCode.SelectAll;
        Timer1.Enabled := True;
      end;
    end;

end;

procedure TfrmPutup.Timer1Timer(Sender: TObject);
begin
  imgSpeaker.Visible := False;
  lblSpeaker.Visible := False;
  Timer1.Enabled := False;
end;

procedure TfrmPutup.FormCreate(Sender: TObject);
begin
  RecordList := Tlist.Create;
  TmpCodeList := TList.Create;
  TreeList := TList.Create;
  MytreeList.ClearNodes;
  tlSetWrkGrp.ClearNodes ;
  AddTreeList;
end;

procedure TfrmPutup.FormDestroy(Sender: TObject);
begin
  ClearObj(RecordList) ;
  RecordList.Free;
  ClearObj(TmpCodeList);
  TmpCodeList.Free;
  ClearObj(TreeList) ;
  TreeList.Free;
end;

procedure TfrmPutup.tbDelSubClick(Sender: TObject);
Var
  S,CurID: String;
begin
  CurID := MytreeList.FocusedNode.Strings[0];
  if  MessageBox(Handle,'确定要删除吗？','系统提示',MB_OKCANCEL + MB_ICONWARNING)= mrOK then
  begin
    S := 'Delete from tempEnter where id =' +CurID;
    if dmPublic.ExcuteSQLCommand(S,0) then
      AddTreeList ;
  end;


  //InitCodeBringFace;
end;

procedure TfrmPutup.tbLeadInClick(Sender: TObject);
begin
  SaveCodeBring;
end;

procedure TfrmPutup.grp1Click(Sender: TObject);
begin
  btnOk.Visible := grp1.ItemIndex = 1;
end;


procedure TfrmPutup.AddTreeList;
var
  i: integer;
  myCodeRec: pDuctRec;
  sql: String;
begin
  ClearObj(TreeList);
  sql := 'Select tempEnter.ID,tempEnter.DuctID,tempEnter.DuctNO,tempEnter.Ductname,tempEnter.barcode '
   + ' From tempEnter Order by id ';
  dmPublic.RunSQL(qryCom1,SQL,1) ;
  if not qryCom1.IsEmpty then
  begin
    while not qryCom1.Eof do
    begin
      new(MycodeRec);
      MyCodeRec^.ID := qryCom1.Fields[0].AsString;
      MyCodeRec^.DuctID := qryCom1.Fields[1].AsString;
      MycodeRec^.ductnO := qryCom1.Fields[2].AsString;
      MycodeRec^.DuctName := qryCom1.Fields[3].AsString;
      MyCodeRec^.BarCode := qryCom1.Fields[4].AsString;
      TreeList.Add(myCodeRec);
      qryCom1.Next ;
    end;
  end;
  FillTree;
end;




procedure TfrmPutup.FillTree;
var
  i: integer;
  myCodeRec: pDuctRec;
begin
  MytreeList.BeginUpdate;
  MytreeList.ClearNodes;
  for i := 0 to TreeList.Count -1 do
  begin
    MycodeRec := TreeList.Items[i];
    with MytreeList.Add do
    begin
      Strings[0] := MyCodeRec^.ID;
      Strings[1] := MyCodeRec^.DuctNo;
      Strings[2] := MyCodeRec^.DuctName;
      Strings[3] := MyCodeRec^.barCode; //IntToStr(Mycoderec^.number) ;
    end;
  end;
  Mytreelist.EndUpdate;
  if MytreeList.Count = tishiNum.value then
    MessageBox(Handle,pchar('数量够' + tishiNum.Text + '请入库！'),'系统提示',MB_OKCANCEL + MB_ICONWARNING);
end;

procedure TfrmPutup.btnEnterClick(Sender: TObject);
var
  ccode,S: String;
  i: integer ;
  myCodeRec: pCodeRec;
begin
  if treelist.Count <= 0 then
  begin
    MessageBox(Handle,'没有产品可以入库！','系统提示',MB_OKCANCEL + MB_ICONWARNING);
    Exit;
  end;


  for i := 0 to TreeList.Count -1 do
  begin
    MycodeRec := TreeList.Items[i];
    AddToTask(MycodeRec.DuctId,'DoNumber',1);
    S := ' update tabCurTask set finish = 1 where finish <> 1 and  Donumber >= sNumber  ';
        dmPublic.ExcuteSQLCommand(S,0);
  end;

  with ADOStoredProc1 do
  begin
    Close;
    Parameters.ParamByName('@CCode').value:= ccode ;//查询参数
    ExecProc;
  end;
  MessageBox(Handle,'数据保存保存完毕！','系统提示',MB_OKCANCEL + MB_ICONWARNING);
  ClearObj(TreeList) ;
  Filltree;
  ClearObj(RecordList) ; 
  InitCodeBringFace;
end;

procedure TfrmPutup.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  if treelist.Count > 0 then
  begin
    if  MessageBox(Handle,'有产品未入库，是否入库！','系统提示',MB_OKCANCEL + MB_ICONWARNING)=  IDOk  then
      btnEnterClick(Nil)
    else CanClose := true;
   end
   else CanClose := True;
end;

procedure TfrmPutup.FormShow(Sender: TObject);
begin
  edtCode.SetFocus;
end;

function TfrmPutup.SameCode(Code: String): Boolean;
var
  S: String ;
  i: integer;
  myCodeRec: pCodeRec;
begin
  Result := false;
  For i := 0 to TreeList.Count -1 do
  begin
     myCodeRec := Treelist.Items[i];
     if myCodeRec.myCode = Code then
       Exit;
  end;
  S := 'Select BarCode from tabProEnter BarCode where BarCode ='''+Code+'''';
  with dmPublic do
  begin
    ExcuteSQLCommand(S,1);
    if not qryCom1.IsEmpty then
    begin
      MessageBox(Handle,'该产品已经入库！','系统提示',MB_OKCANCEL + MB_ICONWARNING);
      Exit;
    end;
  end;
  Result := true ;
end;

procedure TfrmPutup.tbCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmPutup.tlSetWrkGrpGetFooterCellText(Sender: TObject;
  ANode: TdxTreeListNode; AColumn, AFooterIndex: Integer;
  var AText: String);
begin
  if  AColumn in [0] then   AText := '合计: ' + IntToStr(tlSetWrkGrp.Count) + ' 项';

end;

procedure TfrmPutup.tlSetWrkGrpCustomDrawFooter(Sender: TObject;
  ACanvas: TCanvas; ARect: TRect; ANode: TdxTreeListNode;
  AColumn: TdxTreeListColumn; var AText: String; var AColor: TColor;
  AFont: TFont; var AAlignment: TAlignment; var ADone: Boolean);
begin
  AFont.Color := clBlue;
end;

procedure TfrmPutup.tlSetWrkGrpIsExistFooterCell(Sender: TObject;
  AColumn: Integer; var AExist: Boolean);
begin
   if AColumn in [0] then  AExist := true;
end;

function TfrmPutup.CodeIsEixst(Code: String): Boolean;
var
  i: integer;
  myCodeRec: pCodeRec;
begin
  Result := False;
  for i := 0 to  TreeList.Count - 1 do
  begin
    MyCodeRec := TreeList.Items[i];
     if MyCodeRec.myCode = Code then
     begin
       Result :=TRue;
       break;
     end;
  end;
end;


procedure TfrmPutup.MytreeListGetFooterCellText(Sender: TObject;
  ANode: TdxTreeListNode; AColumn, AFooterIndex: Integer;
  var AText: String);
begin
  Case AColumn of
  1: AText := '合计: ' + IntToStr(MytreeList.Count) + ' 项';
  end;
end;

procedure TfrmPutup.MytreeListIsExistFooterCell(Sender: TObject;
  AColumn: Integer; var AExist: Boolean);
begin
  if AColumn in [1] then  AExist := true;
end;

procedure TfrmPutup.AddtoTmptab(Code, DuctId, DuctNO, pName: String);
var
  S: String;
const
  InSQL = 'Insert Into tempEnter(EnterDate,DuctID,DuctNO,Ductname,barcode,'
   +'finFlag) '
   + 'Values(convert(char(10),getdate(),21),''%s'',''%s'',''%s'',''%s'',0)';
begin  
  with dmPublic do
  begin
    S := ' Select Id from tempEnter where Barcode = ''' + Code + ''' and  convert(char(10),EnterDate,21)=''' + FormatDateTime('yyyy-mm-dd', Now)+ '''' ;
    if ExcuteSQLCommand(S,1) then
      if qryCom1.isempty then
      begin
        S :=  Format(InSQL,[DuctId,DuctNO,pName,Code]);
        ExcuteSQLCommand(S,0);
      end;  
  end;
  if MytreeList.Count =  tishiNum.Value then  btnEnter.Click ;

end;

function TfrmPutup.SaveBaozhang: boolean;
var
  i: integer;
  S,PBarcode : String;
  myCodeRec: pCodeRec;
Const
  InsRelTab = 'Insert Into tabCodeRelation2 (ptCode, cldCode) Values(''%s'', ''%s'')';
begin
  PBarcode := CreateTmpCode('');
  With dmPublic Do
  begin
    S := 'delete from tabCodeRelation2 where ptCode =''' + edtCode.text + '''';
    ExcuteSQLCommand(S,0);
    for i := 0 to MytreeList.Count -1 do  //
    begin
      myCodeRec := MytreeList[i];
      if not SameCode(myCodeRec^.myCode) then Continue;
      ExcuteSQLCommand(Format(InsRelTab, [CodeThreeRec[0], myCodeRec^.myCode]), 0);
      AddtoTmptab(Mycoderec^.myCode,mycodeRec^.DuctID,MycodeRec^.DuctNO, MycodeRec^.DuctName);
    end;
  end;

end;

end.
