unit uSetupDuct;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Common,
  Dialogs, uWorkPlaceBase, dxExEdtr, ADODB, DB, ExtCtrls, dxBar,MyDataStruct,
  dxBarExtItems, ImgList, StdCtrls, dxTL, dxCntner, RzPanel, RzSplit,uFunMesClient,ufuncom,
  ComCtrls, dxEditor, dxEdLib,uTECPrintBarCode;

type
  TfrmSetupDuct = class(TfrmWorkPlaceBase)
    PageControl1: TPageControl;
    CheAutoputup: TCheckBox;
    ADOPutup: TADOStoredProc;
    Label3: TLabel;
    PutupNum: TdxSpinEdit;
    tlSetWrkGrp: TdxTreeList;
    dxTreeListColumn4: TdxTreeListColumn;
    dxTreeListColumn5: TdxTreeListColumn;
    dxTreeListColumn6: TdxTreeListColumn;
    procedure edtCodeKeyPress(Sender: TObject; var Key: Char);
    procedure LbtnOKClick(Sender: TObject);
    procedure LbtnNewClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure tolTreeListGetFooterCellText(Sender: TObject;
      ANode: TdxTreeListNode; AColumn, AFooterIndex: Integer;
      var AText: String);
    procedure tlSetWrkGrpGetFooterCellText(Sender: TObject;
      ANode: TdxTreeListNode; AColumn, AFooterIndex: Integer;
      var AText: String);
    procedure tlSetWrkGrpIsExistFooterCell(Sender: TObject;
      AColumn: Integer; var AExist: Boolean);
    procedure tolTreeListIsExistFooterCell(Sender: TObject;
      AColumn: Integer; var AExist: Boolean);
  private
    TmpCodeList: TList;
    procedure AddtoTmptab(Code, DuctId, DuctNO, pName: String);
    function  CDuctBarCodeIn(Barcode: String): Boolean;
    function  SameCode(Code: String): Boolean;
    procedure AddTreeList;
    procedure FillTree;
    procedure AddListByCode(barCode: String); // 参看包装
    procedure FillUptlSetWrkGrp;
    function isDuctBarCode(CBarCode: String): integer;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmSetupDuct: TfrmSetupDuct;

implementation

uses PublicDBUnit;

{$R *.dfm}

procedure TfrmSetupDuct.edtCodeKeyPress(Sender: TObject; var Key: Char);
begin
 // inherited;
  if Key = #13 then
    CDuctBarCodeIn(edtCode.Text) ;
end;

function TfrmSetupDuct.CDuctBarCodeIn(Barcode: String): Boolean;
var
  isOK: Boolean;
  C_barCode: String;
  CodeiFlag: integer;
Const
  Selsql = 'SELECT tabProduct.ProductNO, tabProduct.ProductName, tabCrtCode.ID,tabproduct.id FROM tabCrtCode '
    + 'INNER JOIN tabProduct ON tabCrtCode.DuctID = tabProduct.ID WHERE tabCrtCode.TmpCode=''%s''';
begin
  C_barCode := trim(Barcode);
  if C_barCode = '' then Exit ;
  if isSysCode(C_barCode) then    Exit;
  if  not SameCode(Barcode)  then
  begin
    edtCode.Text := '';
    Exit;
  end;
  CodeiFlag := isDuctBarCode(C_barCode);
  if  CodeiFlag = 1  then  //条码 是否登记
  begin
    MyDuct.Barcode := C_barCode ;
    CurBringID := GetBringID(C_BarCode); // 得到随工单主表ID
    if DuctBWorkIsOK(CurBringID) then   //工位是否 正确
    begin
      isOK := false;
      dmPublic.acnDB.BeginTrans ;
      isOK :=  SaveAutoWork(ManworkList);  //保存排班
      if isOK then isOk := SaveCodeBringSub(CurBringID,OkNo,'-1','无',MyWork.WorkName+' 完成',MyWork,0,1) ;
      if isOK then
        isOK := SaveMductIDbyList(MDuctAddNumList,inttoStr(CurLineID),MyDuct.Barcode); // 保存自动安装料

      AddtoTmptab(MyDuct.Barcode,MyDuct.DuctID,MyDuct.DuctNo,MyDuct.DuctName);
      if isOK then dmPublic.acnDB.CommitTrans
      else dmPublic.acnDB.RollbackTrans ;
      AddTreeList;
    end ;
    edtCode.SelectAll ;
  end
  else if CodeiFlag = 2 then
  begin
    AddListByCode(C_BarCode) ;
  end else
  begin
    lblSpeaker.Caption := ' 不是产品条码！';
    imgSpeaker.Visible := True;
    lblSpeaker.Visible := True;
    //edtman.Text := '';
    edtCode.SetFocus;
    edtCode.SelectAll;
   // moCurRout.Enabled := false;
    ShowNODuct.Enabled := True;
  end;
end;

procedure TfrmSetupDuct.AddTreeList;
var
  i: integer;
  myCodeRec: pDuctRec;
  sql: String;
begin
  Clearlist(TotList);
  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;
      TotList.Add(myCodeRec);
      qryCom1.Next ;
    end;
  end;
  FillTree;
end;

function TfrmSetupDuct.SameCode(Code: String): Boolean;
var
  S: String ;
  i: integer;
  myCodeRec: pDuctRec;
begin
  Result := false;
  For i := 0 to TotList.Count -1 do
  begin
     myCodeRec := TotList.Items[i];
     if myCodeRec.Barcode = 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 TfrmSetupDuct.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;  
end;

procedure TfrmSetupDuct.FillTree;
var
  i: integer;
  myCodeRec: pDuctRec;
begin
  tolTreeList.BeginUpdate;
  tolTreeList.ClearNodes;
  for i := 0 to TotList.Count -1 do
  begin
    MycodeRec := TotList.Items[i];
    with tolTreeList.Add do
    begin
      Strings[0] := MyCodeRec^.ID;
      Strings[1] := MyCodeRec^.DuctNo;
      Strings[2] := MyCodeRec^.DuctName;
      Strings[3] := MyCodeRec^.barCode; //IntToStr(Mycoderec^.number) ;
    end;
  end;
  tolTreeList.EndUpdate;
  if tolTreeList.Count >=  PutupNum.Value then
    if CheAutoputup.checked then
       LbtnOK.Click ;

end;

procedure TfrmSetupDuct.LbtnOKClick(Sender: TObject);
var
  PBarCode,S: String ;
  i: integer;
  myCodeRec: pDuctRec;
begin
  //inherited;
  if tolTreeList.Count <= 0 then
  begin
    MessageBox(Handle,'没有产品可以入库！','系统提示',MB_OKCANCEL + MB_ICONWARNING);
    Exit;
  end;
  PBarCode := MES42CreateBarCode('42',MyDuct.DuctbarCode,now,2);
  S := 'Insert Into tabCrtCode(CrtDate,DuctID,TmpCode,iflag,ClientNO,PrintDate) Values(Getdate(),'+MyDuct.Ductid+ ','''+PBarCode+''',2,'''',Getdate())';
  if dmPublic.ExcuteSQLCommand(S,0)= false then Exit ;

  for i := 0 to TotList.Count -1 do
  begin
    MycodeRec := TotList.Items[i];
    AddToTask(MycodeRec.DuctId,'DoNumber',CurLineID);
  end;
  with ADOPutup do
  begin
    Close;
    Parameters.ParamByName('@CCode').value:= PBarCode ;//查询参数
    ExecProc;
  end;
  TECPrintBarCode(2,PBarCode,MyDuct.DuctName,1);
  MessageBox(Handle,'数据保存保存完毕！','系统提示',MB_OKCANCEL + MB_ICONWARNING);
  LbtnNew.Click ;
end;

procedure TfrmSetupDuct.LbtnNewClick(Sender: TObject);
begin
  inherited;
  ClearList(TotList) ;
  AddTreeList ;
  ClearList(TmpCodeList);
  FillUptlSetWrkGrp;
  edtCode.text := '' ;
end;

procedure TfrmSetupDuct.FormCreate(Sender: TObject);
begin
  inherited;
  AddTreeList;
  TmpCodeList := TList.Create;
end;

procedure TfrmSetupDuct.AddListByCode(barCode: String);
var
  S: String;
  xCodeRec: pDuctRec ;
begin
  barCode := trim(barCode);
  ClearList(TmpCodeList);
  S := 'Select tabProduct.ID,tabProduct.ProductNo,tabProduct.ProductName,tabCodeRelation2.cldCode from '
    + ' tabCodeRelation2 inner join tabCrtCode on tabCodeRelation2.cldCode=  tabCrtCode.tmpCode '
    + ' inner join tabProduct on  tabProduct.id = tabCrtCode.DuctID where  tabCodeRelation2.ptCode=''' + barCode+ '''';
  with dmPublic do
  begin
    ExcuteSQLCommand(S,1);
    if not qryCom1.IsEmpty then
    begin
      while not qryCom1.Eof do
      begin
        new(xCodeRec);
        xCodeRec.barCode := qryCom1.Fields[3].AsString ;
        xCodeRec.DuctID := qryCom1.Fields[0].AsString;
        xCodeRec.DuctName:= qryCom1.Fields[2].AsString ;
        xCodeRec.DuctNo := qryCom1.Fields[1].AsString;
        TmpCodeList.add(xCodeRec);
        qryCom1.Next ;
      end;
    end;
  end;
  FillUptlSetWrkGrp;
end;

procedure TfrmSetupDuct.FillUptlSetWrkGrp;
var
  i: integer;
  myCodeRec: pDuctRec;
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^.BarCode;
      Strings[1] := myCodeRec^.DuctNO;
      Strings[2] := myCodeRec^.DuctName;
    end;
  end;
  tlSetWrkGrp.EndUpdate;
end;

procedure TfrmSetupDuct.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
  inherited;
  ClearList(TmpCodeList);
  TmpCodeList.Free;
end;

function TfrmSetupDuct.isDuctBarCode(CBarCode: String): integer;
var
  Selsql: String ;
begin
  Result := -1;
  Selsql := 'SELECT iflag  FROM tabCrtCode WHERE tabCrtCode.TmpCode =''' + CBarCode + '''';
  with dmPublic do
  begin
    if ExcuteSQLCommand(Selsql, 1) then
    begin
      if Not qryCom1.IsEmpty then
      begin 
        Result := qryCom1.fields[0].AsInteger ;
      end ;
    end;
  end;

end;

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

procedure TfrmSetupDuct.tlSetWrkGrpGetFooterCellText(Sender: TObject;
  ANode: TdxTreeListNode; AColumn, AFooterIndex: Integer;
  var AText: String);
begin
  inherited;
  Case AColumn of
    0: AText := '合计: ' + IntToStr(tlSetWrkGrp.Count) + ' 项';
  end;
end;

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

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

end.
