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;
    ADOQueryMain: TADOQuery;
    ADOQueryCmd: TADOQuery;
    CDS_FromMJID: TClientDataSet;
    CDS_ToMJID: TClientDataSet;
    DS_FromMJID: TDataSource;
    DS_ToMJID: TDataSource;
    SmNO: TEdit;
    ADOQueryTmp: TADOQuery;
    CheckBox3: TCheckBox;
    ADOQueryPrint: TADOQuery;
    RM2: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Panel4: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1MJID: TcxGridDBColumn;
    v1P_Code: TcxGridDBColumn;
    v1P_Color: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    V2Column1: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    V2Column2: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label32: TLabel;
    Label33: TLabel;
    Edit7: TEdit;
    Edit8: TEdit;
    Label26: TLabel;
    Edit6: TEdit;
    Panel2: TPanel;
    Label2: TLabel;
    Label3: TLabel;
    MJGW: TEdit;
    btnChaiPi: TButton;
    Button4: TButton;
    Label5: TLabel;
    Label9: TLabel;
    MJLen: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure SmNOKeyPress(Sender: TObject; var Key: Char);
    procedure FormShow(Sender: TObject);
    procedure Edit7Click(Sender: TObject);
    procedure Edit8Click(Sender: TObject);
    procedure Edit6Click(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure btnChaiPiClick(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.* from WFB_MJJY where MJID =' + 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.PrintReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\卷标签.rmf'), '提示', 0);
  end;

end;

function TfrmKCRLLTZ.Savedata2(): boolean;
var
  fconNo, FC_CodeName, FC_Color, maxno: string;
  i: Integer;
begin
  result := false;
  if CDS_FromMJID.RecordCount <= 1 then
  begin
    application.MessageBox('被合并的匹数不能小于或者等于一匹，请检查扫描到的匹数！', '提示信息', MB_ICONERROR);
    exit;
  end;
  if GetLSNo(ADOQueryTmp, maxno, 'B', 'WFB_MJJY', 4, 1) = False then
  begin
    Application.MessageBox('生成流水号异常！', '提示', 0);
    exit;
  end;

  with CDS_FromMJID do
  begin
    DisableControls;
    First;
    FC_CodeName := trim(Fieldbyname('C_CodeName').AsString);
    FC_Color := trim(Fieldbyname('C_Color').AsString);
    while not eof do
    begin
      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;
      next;
    end;
    EnableControls;
    First;
  end;

  ADOQueryCmd.Connection.BeginTrans;
  try
    with CDS_FromMJID do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('exec P_Do_JYCBCK ');
          sql.Add(' @MJID=' + quotedstr(trim(ADOQueryCmd.FieldByName('MJID').AsString)));
          sql.Add(',@ToMJID='''' ');
          sql.Add(',@CRType=''并匹'' ');
          sql.Add(',@DName=' + quotedstr(trim(DName)));
          open;
        end;
        next;
      end;
      EnableControls;
      First;
    end;

    with ADOQueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select * from WFB_MJJY ');
      sql.Add('where MJID=' + quotedstr(trim(CDS_FromMJID.FieldByName('MJID').AsString)));
      open;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from WFB_MJJY where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      ADOQueryCmd.Append;
      for i := 0 to ADOQueryCmd.FieldCount - 1 do
      begin
        ADOQueryCmd.fields[i].value := ADOQueryTmp.Fields[i].Value;
      end;
      ADOQueryCmd.FieldByName('MJID').Value := Trim(maxno);
      ADOQueryCmd.FieldByName('MJGW').Value := StrToFloatDef(MJGW.Text, 0);
      ADOQueryCmd.FieldByName('MJNW').Value := StrToFloatDef(MJGW.Text, 0) - ADOQueryTmp.FieldByName('MJTare').AsFloat;
      ADOQueryCmd.FieldByName('MJLen').Value := StrToFloatDef(MJLen.Text, 0);
      ADOQueryCmd.Post;
    end;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('exec P_Do_JYCRK  ' + quotedstr(Trim(maxno)));
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    CDS_FromMJID.EmptyDataSet;
    if Trim(Edit6.Text) = '√' then
    begin
      printdata(maxno);
    end;

    ModalResult := 1;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;

end;

function TfrmKCRLLTZ.Savedata1(): boolean;
var
  FFromMJID, maxno: string;
  i: integer;
begin
  result := false;

  if GetLSNo(ADOQueryTmp, maxno, 'C', 'WFB_MJJY', 4, 1) = False then
  begin
    Application.MessageBox('生成流水号异常！', '提示', 0);
    exit;
  end;

  ADOQueryCmd.Connection.BeginTrans;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('exec P_Do_JYCBCK ');
      sql.Add(' @MJID=' + quotedstr(trim(ADOQueryCmd.FieldByName('MJID').AsString)));
      sql.Add(',@ToMJID='''' ');
      sql.Add(',@CRType=''拆匹'' ');
      sql.Add(',@DName=' + quotedstr(trim(DName)));
      ExecSQL;
    end;
    with ADOQueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select * from WFB_MJJY ');
      sql.Add('where MJID=' + quotedstr(trim(CDS_FromMJID.FieldByName('MJID').AsString)));
      open;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from WFB_MJJY where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      ADOQueryCmd.Append;
      for i := 0 to ADOQueryCmd.FieldCount - 1 do
      begin
        ADOQueryCmd.fields[i].value := ADOQueryTmp.Fields[i].Value;
      end;
      ADOQueryCmd.FieldByName('FromMJID').Value := Trim(ADOQueryTmp.FieldByName('MJID').AsString);
      ADOQueryCmd.FieldByName('MJID').Value := Trim(maxno) + '1';
      ADOQueryCmd.FieldByName('MJGW').Value := StrToFloatDef(MJGW.Text, 0);
      ADOQueryCmd.FieldByName('MJNW').Value := StrToFloatDef(MJGW.Text, 0) - ADOQueryTmp.FieldByName('MJTare').AsFloat;
      ADOQueryCmd.FieldByName('MJLen').Value := StrToFloatDef(MJLen.Text, 0);
      ADOQueryCmd.Post;
    end;

    with ADOQueryCmd do
    begin
      ADOQueryCmd.Append;
      for i := 0 to ADOQueryCmd.FieldCount - 1 do
      begin
        ADOQueryCmd.fields[i].value := ADOQueryTmp.Fields[i].Value;
      end;
      ADOQueryCmd.FieldByName('FromMJID').Value := Trim(ADOQueryTmp.FieldByName('MJID').AsString);
      ADOQueryCmd.FieldByName('MJID').Value := Trim(maxno) + '2';
      ADOQueryCmd.FieldByName('MJGW').Value := ADOQueryTmp.FieldByName('MJGW').AsFloat - StrToFloatDef(MJGW.Text, 0);
      ADOQueryCmd.FieldByName('MJNW').Value := ADOQueryTmp.FieldByName('MJGW').AsFloat - StrToFloatDef(MJGW.Text, 0) - ADOQueryTmp.FieldByName('MJTare').AsFloat;
      ADOQueryCmd.FieldByName('MJLen').Value := ADOQueryTmp.FieldByName('MJGW').AsFloat;
      ADOQueryCmd.Post;
    end;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('exec P_Do_JYCRK  ' + quotedstr(Trim(maxno) + '1'));
      ExecSQL;
    end;

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('exec P_Do_JYCRK  ' + quotedstr(Trim(maxno) + '2'));
      ExecSQL;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    CDS_FromMJID.EmptyDataSet;

    if Trim(Edit6.Text) = '√' then
    begin
      printdata(Trim(maxno) + '1');
      printdata(Trim(maxno) + '2');
    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 WFB_MJJY A');
    SQL.Add('where 1=2 ');
    Open;
  end;
  SCreateCDS20(ADOQueryMain, CDS_FromMJID);
  SCreateCDS20(ADOQueryMain, CDS_ToMJID);
end;

procedure TfrmKCRLLTZ.InitMJID();
var
  fP_No: string;
begin
  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.* from WFB_MJJY A');
    SQL.Add('where A.MJID=' + quotedstr(trim(SmNO.text)));
    Open;
    if not IsEmpty then
    begin
      if Fieldbyname('MJCRType').AsString = '已出库' then
      begin
        application.MessageBox('此条码已出库！', '提示信息', MB_ICONERROR);
      end
      else
      begin
        SInitCDSData20(ADOQueryMain, CDS_FromMJID);
      end;
    end
    else
    begin
      application.MessageBox('此条码不存在！', '提示信息', MB_ICONERROR);
    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.SmNOKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin

    if not CDS_FromMJID.Locate('MJID', trim(smNo.Text), []) then
      initMjid();

    smno.Text := '';
    smno.SetFocus;

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

    end;
  end;
end;

procedure TfrmKCRLLTZ.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption + tV1.Name, Tv1, '成品仓库');
  ReadCxGrid(self.Caption + TV2.Name, Tv2, '成品仓库');
  InitGrid();
  if iscom then
  begin
//    if CommOpen(frmKCRLLTZ.Handle, '', 500, 1) < 1 then
//    begin
//      showmessage('打开串口错误');
//    end;
  end;
  SmNO.SetFocus;
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 CDS_FromMJID.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.Tv1DblClick(Sender: TObject);
begin
  if CDS_FromMJID.IsEmpty then
    exit;
  CDS_FromMJID.Delete;
end;

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

procedure TfrmKCRLLTZ.btnChaiPiClick(Sender: TObject);
begin

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

  SmNO.SetFocus;
end;

end.

