unit U_KCRLLTZ;

interface

uses
  Windows, Messages, strutils,SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls,
  cxGridCustomView, cxGrid, StdCtrls, cxContainer, cxTextEdit,
  cxCurrencyEdit, BtnEdit, ExtCtrls, ComCtrls, ToolWin, DBClient, ADODB,
  MovePanel, RM_Dataset, RM_System, RM_Common, RM_Class, RM_GridReport;
  function CommOpen(fhandle:hwnd;sCommName:PAnsiChar;
                    IntTime:integer;IsMessage:integer):integer;stdcall;external 'JCYData.DLL';
  function CommClose(sCommName:PAnsiChar):integer;stdcall;external 'JCYData.DLL';

type
  TfrmKCRLLTZ = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1MJID: TcxGridDBColumn;
    v1P_Code: TcxGridDBColumn;
    v1P_Color: TcxGridDBColumn;
    v1RollNum: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ADOQueryMain: TADOQuery;
    ADOQueryCmd: TADOQuery;
    CDSOrder: TClientDataSet;
    CDSMJID: TClientDataSet;
    DataSource1: TDataSource;
    DataSource2: TDataSource;
    SmNO: TEdit;
    ADOQueryTmp: TADOQuery;
    MovePanel1: TMovePanel;
    Label2: TLabel;
    Edit2: TEdit;
    Button4: TButton;
    V2Column1: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    cxStyle_gridRow: TcxStyle;
    cxStyle_gridFoot: TcxStyle;
    cxStyle_gridHead: TcxStyle;
    cxStyle_gridGroupBox: TcxStyle;
    cxStyle_yellow: TcxStyle;
    cxStyle_Red: TcxStyle;
    cxStyle_fontBlack: TcxStyle;
    cxStyle_fontclFuchsia: TcxStyle;
    cxStyle_fontclPurple: TcxStyle;
    cxStyle_fontclGreen: TcxStyle;
    cxStyle_fontclBlue: TcxStyle;
    cxStyle_fontclTeal: TcxStyle;
    cxStyle_fontclOlive: TcxStyle;
    CheckBox3: TCheckBox;
    Panel3: TPanel;
    MJLen: TEdit;
    Label5: TLabel;
    Label9: TLabel;
    Edit7: TEdit;
    Label32: TLabel;
    Label33: TLabel;
    Edit8: TEdit;
    Edit6: TEdit;
    Label26: TLabel;
    BTPrint: TButton;
    Button5: TButton;
    ADOQueryPrint: TADOQuery;
    RM2: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);

    procedure FormCreate(Sender: TObject);
    procedure SmNOKeyPress(Sender: TObject; var Key: Char);
    procedure FormShow(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Edit7Click(Sender: TObject);
    procedure Edit8Click(Sender: TObject);
    procedure Edit6Click(Sender: TObject);
    procedure BTPrintClick(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure Button5Click(Sender: TObject);
  private
    procedure InitGrid();
    procedure InitMJID();
    function  Savedata1():boolean;
    function  Savedata2():boolean;
    procedure Printdata(fCRID:string);
    procedure On1201(Var Message:Tmessage);Message 1201;
    { Private declarations }
  public
      iscom:boolean;
    { Public declarations }
  end;

var
  frmKCRLLTZ: TfrmKCRLLTZ;

implementation
uses
  U_DataLink,U_Fun, U_UserHelp, U_ZDYHelp;
{$R *.dfm}
procedure TfrmKCRLLTZ.On1201(Var Message:Tmessage);
var
  i1,i2:integer;
  unitname:string;
  fdata:double;
begin
  i1:=message.WParam;
  i2:=message.LParam;

  IF not CheckBox3.Checked then exit;

  IF trim(DParameters10)='含小数'then
  MJLen.Text:= format('%.2f',[i1/100000])
  else
  MJLen.Text:= format('%.0f',[i1/100000]);
  IF i2=0 then Label9.Caption:='M' else  Label9.Caption:='Y';
end;

procedure TfrmKCRLLTZ.Printdata(fCRID:string);
var
  fPrintFile:String;
  mvalue:Double;
  Txt,fImagePath:string;
  Moudle: THandle;
  Makebar:TMakebar;
  Mixtext:TMixtext;
begin
    with ADOQueryPrint do
    begin
      Close;
      SQL.Clear;
      sql.Add('select A.*,kcQty as mjlen,kcQtyUnit as MJTypeother,C_CodeNameZW=(select Top 1 defnote3 from KH_Zdy_Attachment X where X.ZdyName=A.C_CodeName and Type=''CP'') ');
      sql.Add(',C_colorName=(select Top 1 DEFstr6 from KH_Zdy_Attachment X where X.ZdyName=A.C_CodeName and X.DEFstr3=A.C_Color and Type=''CP'') ');
      sql.Add(' from CK_BanCP_KC A') ;
      //SQL.Add(' where A.MJID='''+Trim(CDS_Main.fieldbyname('MJID').AsString)+'''');
      SQL.Add(' where A.CRID='+quotedstr(trim(fCRID)));
      Open;
    end;
    try
      Moudle:=LoadLibrary('MakeQRBarcode.dll');
      @Makebar:=GetProcAddress(Moudle,'Make');
      @Mixtext:=GetProcAddress(Moudle,'MixText');
      Txt:=Trim(ADOQueryPrint.fieldbyname('MJID').AsString);
      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;


    fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\通用标签.rmf' ;
    if FileExists(fPrintFile) then
    begin
      RMVariables['QRBARCODE']:=fImagePath;
      RM2.LoadFromFile(fPrintFile);
   //   RM2.ShowReport;
      RM2.PrintReport;
     // MJLen.SetFocus;
    end else
    begin
      Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\卷标签.rmf'),'提示',0);
    end;

end;
function  TfrmKCRLLTZ.Savedata2():boolean;
var
  i,FCRID:integer;
  fconNo,FC_CodeName,FC_Color:string;
  fRollNum,FQty:double;
begin
  result:=false;
  IF CDSOrder.RecordCount<=1 then
  begin
    application.MessageBox('被合并的匹数不能小于或者等于一匹，请检查扫描到的匹数！','提示信息',MB_ICONERROR);
    exit;
  end;

  i:=0;
  with  CDSOrder do
  begin
    DisableControls;
    First;
    while not eof do
    begin
        if i=0 then
        begin
//          fconNo:= trim(Fieldbyname('ConNo').AsString);
          FC_CodeName:= trim(Fieldbyname('C_CodeName').AsString);
          FC_Color:= trim(Fieldbyname('C_Color').AsString);
        end
        else
        begin
         { if trim(fconNo)<>trim(Fieldbyname('ConNo').AsString) then
          begin
            EnableControls;
            application.MessageBox('任务单号不同，不能合并！','提示信息',0);
            exit;
          end;  }
          if trim(FC_CodeName)<>trim(Fieldbyname('C_CodeName').AsString) then
          begin
            EnableControls;
            application.MessageBox('产品名称不同，不能合并！','提示信息',0);
            exit;
          end;
          if trim(FC_Color)<>trim(Fieldbyname('C_Color').AsString) then
          begin
            EnableControls;
            application.MessageBox('颜色不同，不能合并！','提示信息',0);
            exit;
          end;
        end;
        i:=i+1;
      next;
    end;
    EnableControls;
    First;
  end;

  if i<2 then exit;

  ADOQueryCmd.Connection.BeginTrans;
  try

    fRollNum:=0;
    FQty:=0;
    i:=0;
    with  CDSOrder do
    begin
      DisableControls;
      First;
      while not eof do
      begin
      //  if  Fieldbyname('Ssel').AsBoolean then
      //  begin
          IF i=0 then
          fCRID:=Fieldbyname('CRID').AsInteger;

          fRollNum:=1;
          FQty:=strtofloatdef(trim(MJLen.Text),0);

          with adoqueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('select * from CK_BanCP_KC ');
            sql.Add('where CRID='+quotedstr(trim(CDSOrder.Fieldbyname('CRID').AsString)));
            open;
            edit;
            fieldbyname('ZDYstr1').Value:='检验库存合并';
            fieldbyname('ZDYFlote1').Value:=fieldbyname('kcQty').AsFloat;
            FieldByName('oldCRID').Value:=fCRID;
            fieldbyname('KCROllNum').Value:=0;
            fieldbyname('kcQty').Value:=0;
            post;
          end;
          i:=i+1;
        //end;
        next;
      end;
      EnableControls;
      First;
    end;

    with adoqueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from CK_BanCP_KC ');
      sql.Add('where CRID='+quotedstr(inttostr(fCRID)));
      open;
      edit;
      fieldbyname('KCROllNum').Value:=1;
      fieldbyname('kcQty').Value:=FQty;
      post;
    end;
    with adoqueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from CK_BanCP_KC ');
      sql.Add('where CRID='+quotedstr(inttostr(fCRID)));
      open;
      SInitCDSData20(adoqueryCmd,CDSMJID);
    end;

    ADOQueryCmd.Connection.CommitTrans;
    CDSOrder.EmptyDataSet;
    if Trim(Edit6.Text)='√' then
    begin

      printdata(inttostr(fCRID));
    end;
    
    ModalResult:=1;
  //  application.MessageBox('数据合并成功！','提示信息');
 //   TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  //  application.MessageBox('数据合并失败！','提示信息',0);
  end;



end;

function  TfrmKCRLLTZ.Savedata1():boolean;
var
  fbcID,FCRID,fAPTM:string;
  CRID:integer;
  i:integer;
begin
  result:=false;

  IF CDSOrder.RecordCount>1 then
  begin
    application.MessageBox('被拆分的匹数不能大于一匹，请检查扫描到的匹数！','提示信息',MB_ICONERROR);
    exit;
  end;
  IF strtofloatdef(trim(mjlen.Text),0)>CDSOrder.FieldByName('KCqty').AsFloat then
  begin
    application.MessageBox('拆分数量不能大于原数量！','提示信息',MB_ICONERROR);
    exit;
  end;

  if GetLSNo(ADOQueryCmd,fAPTM,'C','APTM',4,1)=False then
  begin
    Application.MessageBox('取子流水号失败！','提示',0);
    Exit;
  end;

  fbcID:=CDSOrder.fieldbyname('BCID').AsString;
  fcrID:=CDSOrder.fieldbyname('CRID').AsString;
  ADOQueryCmd.Connection.BeginTrans;
  try
    with ADOQueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select * from CK_BanCP_KC A');
      sql.Add('where BCID='+quotedstr(trim(fBCID)));
      sql.Add('and CRID='+quotedstr(trim(fCRID)));
      open;
    end;
    with ADOQueryCmd  do
    begin
      Close;
      sql.Clear;
      SQL.add('Update CK_BanCP_CRID Set CRID=CRID+1');
      sql.Add('select * from CK_BanCP_CRID');
      Open;
    end;
    CRID:=ADOQueryCmd.fieldbyname('CRID').AsInteger;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from CK_BanCP_KC A');
      sql.Add('where 1=2 ');
      open;
    end;
    ADOQueryCmd.Append;
    for i:=0 to ADOQueryCmd.FieldCount-1 do
    begin
      ADOQueryCmd.fields[i].value:=ADOQueryTmp.Fields[i].Value ;
    end;
    ADOQueryCmd.FieldByName('CRID').Value:=CRID;
    ADOQueryCmd.FieldByName('KCROllNum').Value:=1;
    ADOQueryCmd.FieldByName('KCQty').Value:=strtofloatdef(MJLen.Text,0);
    ADOQueryCmd.FieldByName('oldCRID').Value:=fcrID;
    ADOQueryCmd.FieldByName('KCStatus').Value:=NULL;
    ADOQueryCmd.FieldByName('MJID').Value:=trim(fAPTM);
    ADOQueryCmd.fieldbyname('ZDYstr1').Value:='检验库存拆分';
    ADOQueryCmd.fieldbyname('ZDYFlote1').Value:= 0;
    ADOQueryCmd.Post;


    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from CK_BanCP_KC A');
      sql.Add('where CRID='''+trim(fCRID)+'''');
      open;
    end;
    ADOQueryCmd.Edit;
    ADOQueryCmd.FieldByName('KCROllNum').Value:=ADOQueryCmd.FieldByName('KCROllNum').AsFloat - 1;
    ADOQueryCmd.FieldByName('KCQty').Value:=ADOQueryCmd.FieldByName('KCQty').AsFloat - strtofloatdef(MJLen.Text,0);
    ADOQueryCmd.fieldbyname('ZDYstr1').Value:='检验库存拆分';
    ADOQueryCmd.fieldbyname('ZDYFlote1').Value:= ADOQueryCmd.FieldByName('KCQty').AsFloat;
    ADOQueryCmd.Post;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPDATE CK_BanCp_KC SET KCRollNum =1 ');
      sql.Add('WHERE (KCQty<>0 or KCKgQty<>0)and KcRollNum=0  ');
      ExecSQL;
    end;

    with adoqueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from CK_BanCP_KC ');
      sql.Add('where CRID='+quotedstr(inttostr(CRID)));
      open;
      SInitCDSData20(adoqueryCmd,CDSMJID);
    end;

    with adoqueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from CK_BanCP_KC ');
      sql.Add('where CRID='+quotedstr(fCRID));
      open;
      SInitCDSData20(adoqueryCmd,CDSMJID);
    end;
    ADOQueryCmd.Connection.CommitTrans;
  //  application.MessageBox('数据保存成功！','提示信息');
    CDSOrder.EmptyDataSet;

    if Trim(Edit6.Text)='√' then
    begin
      printdata(inttostr(CRID));
      printdata(fCRID);
    end;
    ModalResult:=1;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('数据保存失败！','提示信息',0);
  end;

end;

procedure TfrmKCRLLTZ.InitGrid();
begin
  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from CK_BanCP_KC A');
    SQL.Add('where 1=2 ');
    Open;
  end;
  SCreateCDS20(ADOQueryMain,CDSOrder);
  SInitCDSData20(ADOQueryMain,CDSOrder);
  SCreateCDS20(ADOQueryMain,CDSMJID);
  SInitCDSData20(ADOQueryMain,CDSMJID);
end;

procedure TfrmKCRLLTZ.InitMJID();
var
  fP_No:string;
begin
  MovePanel1.Visible:=false;
  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.* from CK_BanCP_KC A');
   // sql.add('left join  CK_BanCP_CR B on B.BCID=A.BCID and B.CRFlag=''入库'' ');
    SQL.Add('where A.MJID='+quotedstr(trim(SmNO.text)));
    sql.Add('and isnull(A.KCStatus,''0'')=''0''  ');
    sql.Add('and KCQty>0  ');
    Open;
    if not IsEmpty  then
    begin
      IF Fieldbyname('KCQty').AsFloat<=0 then
      begin
        MovePanel1.Visible:=true;
        Edit2.Text:=SmNO.Text;
        Label2.Caption:='此条码已出库';
      end
      else
      begin
         SInitCDSData20(ADOQueryMain,CDSOrder);
      end;
    end
    else
    begin
        MovePanel1.Visible:=true;
        Edit2.Text:=SmNO.Text;
        Label2.Caption:='此条码不存在';
    end;
  end;
end;

procedure TfrmKCRLLTZ.FormDestroy(Sender: TObject);
begin
  wRITECxGrid(self.Caption+tV1.Name,Tv1,'成品仓库');
  wRITECxGrid(self.Caption+TV2.Name,Tv2,'成品仓库');
  frmKCRLLTZ:=nil;
end;

procedure TfrmKCRLLTZ.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  if iscom then
  CommClose(pchar('com2'));
  Action:=caFree;
end;

procedure TfrmKCRLLTZ.TBCloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmKCRLLTZ.FormCreate(Sender: TObject);
begin
  Panel2.Align:=alClient;
end;

procedure TfrmKCRLLTZ.SmNOKeyPress(Sender: TObject; var Key: Char);
begin
  IF key=#13 then
  begin
    MovePanel1.Visible:=false;
    if not CDSOrder.Locate('MJID',trim(smNo.Text),[]) then
    initMjid();
    smno.Text:='';
    smno.SetFocus;

    IF trim(Edit8.Text)='√' then
    begin
      IF not CDSOrder.IsEmpty then
      MJLen.Text:=floattostr(tv1.DataController.Summary.FooterSummaryValues[1]);
    end;
  end;
end;

procedure TfrmKCRLLTZ.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption+tV1.Name,Tv1,'成品仓库');
  ReadCxGrid(self.Caption+TV2.Name,Tv2,'成品仓库');
  MovePanel1.Left:=(Width-MovePanel1.Width) div 2;
  MovePanel1.top:=(Height-MovePanel1.Height-200) div 2;
  InitGrid();
  if iscom then
  begin
    if CommOpen(frmKCRLLTZ.Handle,'',500,1)<1 then
    begin
      showmessage('打开串口错误');
    end;
  end;
  SmNO.SetFocus;
end;

procedure TfrmKCRLLTZ.Button4Click(Sender: TObject);
begin
  MovePanel1.Visible:=false;  
end;

procedure TfrmKCRLLTZ.Edit7Click(Sender: TObject);
begin
  if Trim(Edit7.Text)='' then
  begin
    Edit7.Text:='√';
    Edit8.Text:='';
   // Edit9.Text:='';
  end else
  begin
    Edit7.Text:='';
    Edit8.Text:='';
   // Edit9.Text:='';
  end;
end;

procedure TfrmKCRLLTZ.Edit8Click(Sender: TObject);
begin
  if Trim(Edit8.Text)='' then
  begin
    Edit7.Text:='';
    Edit8.Text:='√';
  //  Edit9.Text:='';
  end else
  begin
    Edit7.Text:='';
    Edit8.Text:='';
 //   Edit9.Text:='';
  end;
  IF trim(Edit8.Text)='√' then
  begin
    IF not CDSOrder.IsEmpty then
    MJLen.Text:=floattostr(tv1.DataController.Summary.FooterSummaryValues[1]);
  end;
end;

procedure TfrmKCRLLTZ.Edit6Click(Sender: TObject);
begin
  if Trim(Edit6.Text)='' then
  begin
    Edit6.Text:='√';
  end else
  begin
    Edit6.Text:='';
  end;
end;

procedure TfrmKCRLLTZ.BTPrintClick(Sender: TObject);
begin
  if Trim(Edit7.Text)='√' then
  begin
    savedata1();
  end;
  if Trim(Edit8.Text)='√' then
  begin
    savedata2();
  end;
  SmNO.SetFocus;
end;

procedure TfrmKCRLLTZ.Tv1DblClick(Sender: TObject);
begin
  IF CDSOrder.IsEmpty then exit;
  CDSOrder.Delete;
end;

procedure TfrmKCRLLTZ.Button5Click(Sender: TObject);
begin
  If CDSMJID.IsEmpty then exit;
  printdata(CDSMJID.fieldbyname('CRID').AsString);
  SmNO.SetFocus;
end;

end.
