unit U_BangAdd;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, ADODB,OleCtrls, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport;
  function CommOpen(fhandle:hwnd;sCommName:PAnsiChar;
                    IntTime:integer;IsMessage:integer):integer;stdcall;external 'ELERS323C.DLL';
  function CommClose(sCommName:PAnsiChar):integer;stdcall;external 'ELERS323C.DLL';

type
  TfrmBangAdd = class(TForm)
    Label1: TLabel;
    MJID: TEdit;
    Label2: TLabel;
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    ADOCmd: TADOQuery;
    ADOTmp: TADOQuery;
    Label3: TLabel;
    Label4: TLabel;
    CheckBox1: TCheckBox;
    RM2: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    ADOQueryPrint: TADOQuery;
    Label5: TLabel;
    Label6: TLabel;
    Edit2: TEdit;
    Label7: TLabel;
    Label8: TLabel;
    Edit3: TEdit;
    procedure MJIDKeyPress(Sender: TObject; var Key: Char);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure Button1KeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
    procedure On1201(Var Message:Tmessage);Message 1201;
    procedure PrintData();
  public
    { Public declarations }
  end;

var
  frmBangAdd: TfrmBangAdd;

implementation

{$R *.dfm}
procedure TfrmBangAdd.On1201(Var Message:Tmessage);
var
  i1,i2:integer;
  unitname:string;
  fdata:double;
begin
  i1:=message.WParam;
  i2:=message.LParam;

  Edit1.Text:= floattostr(i1 *i2 /100000 );
end;

procedure TfrmBangAdd.MJIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    with ADOTmp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from WFB_MJJY where MJID='''+Trim(MJID.Text)+'''');
      Open;
    end;
    if ADOTmp.IsEmpty=False then
    begin
      Label2.Visible:=True;
      Label2.Caption:=Trim(ADOTmp.fieldbyname('MJID').AsString);
    end else
    begin
      MJID.Text:='';
      Label2.Visible:=False;
      Label2.Caption:='';
      Application.MessageBox('条码错误!','提示',0);
      Exit;
    end;
    MJID.Text:='';
    Button1.SetFocus;
  end;
end;

procedure TfrmBangAdd.FormDestroy(Sender: TObject);
begin
  frmBangAdd:=nil;
end;

procedure TfrmBangAdd.Button1Click(Sender: TObject);
var
  FZG,FJD:string;
  FFreal,FMJMaoZ:Double;
begin
  if Label2.Caption='' then
  begin
    Application.MessageBox('未扫条码!','提示',0);
    Exit;
  end;
  if Trim(Edit1.Text)='' then
  begin
    Application.MessageBox('重量不能为空!','提示',0);
    Exit;
  end;
  with ADOTmp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from WFB_MJJY where MJID='''+Trim(Label2.Caption)+'''');
    Open;
  end;
  if ADOTmp.FieldByName('MJMaoZ').AsFloat>0 then
  begin
    if Application.MessageBox('已称重，确定要重新称重吗？','提示',32+4)<>IDYES then Exit;
  end;
  if Trim(Edit2.Text)<>'' then
  begin
    if TryStrToFloat(Edit2.Text,FFreal)=False then
    begin
      Application.MessageBox('非法数字!','提示',0);
      Exit;
    end else
    begin
      FZG:=Edit2.Text;
    end;
  end else
  begin
    FZG:='0';
  end;
  if Trim(Edit3.Text)<>'' then
  begin
    if TryStrToFloat(Edit3.Text,FFreal)=False then
    begin
      Application.MessageBox('非法数字!','提示',0);
      Exit;
    end else
    begin
      FJD:=Edit3.Text;
    end;
  end else
  begin
    FJD:='0';
  end;
  FMJMaoZ:=StrToFloat(Edit1.Text)-StrToFloat(FZG);
  try
    ADOCmd.Connection.BeginTrans;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update WFB_MJJY Set MJMaoZ='+Trim(Floattostr(FMJMaoZ)));
      sql.add(',MJQty1='+Trim(Edit1.Text));
      sql.add(',MJQty2='+Trim(FZG));
      sql.add(',MJQty3='+Trim(FJD));
      SQL.Add(' where MJID='''+Trim(Label2.Caption)+'''');
      ExecSQL;
    end;
    ADOCmd.Connection.CommitTrans;
    PrintData();
    Label2.Caption:='';
    Label2.Visible:=False;
    MJID.SetFocus;
    //Application.MessageBox('操作成功!','提示',0);
  except
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('操作失败!','提示',0);
  end;
end;
procedure TfrmBangAdd.PrintData();
var
   fPrintFile,LabInt,LabName:String;
begin
  with ADOTmp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select C.SLbInt,C.SLbName from WFB_MJJY A');
    sql.Add(' inner join JYOrder_Sub_AnPai B on A.APID=B.APID');
    sql.Add(' inner join JYOrder_Sub C on B.SubId=C.SubId');
    sql.Add(' where A.MJID='''+Trim(Label2.Caption)+'''');
    Open;
  end;
  if ADOTmp.IsEmpty=False  then
  begin
    LabInt:=ADOTmp.fieldbyname('SLbInt').AsString;
    LabName:=ADOTmp.fieldbyname('SLbName').AsString;
  end ;
  if Trim(LabName)='' then 
  begin
    Application.MessageBox('卷标签未设置！','提示',0);
    Exit;
  end;
 { try
    frmLabelPrint:=TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      fLabelId:=LabInt;
      FFCDFlag:=Trim(CDFlag);
      fKeyNo:=Trim(FXJID);
      fIsPreviewPrint:=True;
      frmLabelPrint.Button1.Click;
     // if ShowModal=1 then
      //begin

     // end;
    end;
  finally
    frmLabelPrint.Free;
  end;  }
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    SQL.Add('select D.OrderNo,C.PRTColor,C.PRTKZ,C.PRTType,D.OrdDefStr2,D.OrdDefStr3,D.OrdDefStr7,B.AOrdDefNote30,A.MJXH,B.GangNo');
    SQL.Add(',C.PRTMF,C.SOrddefstr3,C.SOrddefstr5,D.DlyDate,D.DLyPlace,A.MJMaoZ,B.AOrdDefNote31,C.SOrddefstr4,');
    SQL.Add('ColorEngName=(select top 1 Note from KH_Zdy E where E.ZdyName=C.PRTColor and E.Type=''OrdColor''  ),');
    {SQL.Add('MJBang=Cast((A.MJMaoZ*2.2046) as decimal(18,2)),');
    SQL.Add('MJMaoZBang=Cast(((A.MJQty1+A.MJQty3)*2.2046) as decimal(18,2)),');
    SQL.Add('MAQty=Cast((A.MJMaoZ*100*1000/(A.MJSJKZ*(A.MJFK*2.54))*0.9144) as decimal(18,2) ),');
    SQL.Add('MQty=Cast((A.MJMaoZ*100*1000/(A.MJSJKZ*(A.MJFK*2.54))) as decimal(18,2) ),');
    SQL.Add('MaoZ=A.MJQty1+A.MJQty3,');
    SQL.Add('JingZ=A.MJQty1-A.MJQty2'); }
    SQL.Add('MJBang=A.MJMaoZ,');
    SQL.Add('MJMaoZBang=A.MJQty1+A.MJQty3,');
    SQL.Add('MAQty=Cast((A.MJMaoZ*0.4536*100*1000/(A.MJSJKZ*(A.MJFK*2.54))*0.9144) as decimal(18,2) ),');
    SQL.Add('MQty=Cast((A.MJMaoZ*0.4536*100*1000/(A.MJSJKZ*(A.MJFK*2.54))) as decimal(18,2) ),');
    SQL.Add('MaoZ=Cast((A.MJQty1+A.MJQty3)*0.4536 as decimal(18,2)),');
    SQL.Add('JingZ=Cast((A.MJQty1-A.MJQty2)*0.4536 as decimal(18,2))');
    SQL.Add('from WFB_MJJY A inner join JYOrder_Sub_AnPai B on A.APID=B.APID');
    SQL.Add('inner join JYOrder_Sub C on B.SubId=C.SubId');
    SQL.Add('inner join JYOrder_Main D on C.MainId=D.Mainid');
    SQL.Add('where A.MJID='''+Trim(Label2.Caption)+'''');
    Open;
  end;
  fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\'+Trim(LabName)+'.rmf' ;
  if FileExists(fPrintFile) then
  begin
    RM2.LoadFromFile(fPrintFile);
    //RM2.ShowReport;
    Rm2.PrintReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\'+Trim(LabName)+'.rmf'),'提示',0);
  end;
 
end;

procedure TfrmBangAdd.Button2Click(Sender: TObject);
begin
  Close;
end;

procedure TfrmBangAdd.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action:=caFree;
  if CheckBox1.Checked=False then
    CommClose(pchar('com1'));
end;

procedure TfrmBangAdd.FormShow(Sender: TObject);
begin
  if CommOpen(frmBangAdd.Handle,pchar('com1'),500,1)<1 then
  begin
    showmessage('串口打开失败!');
  end
  else
  begin
  end;
end;

procedure TfrmBangAdd.CheckBox1Click(Sender: TObject);
begin
  if CheckBox1.Checked=True then
  begin
    if CommOpen(frmBangAdd.Handle,pchar('com1'),500,1)<1 then
    begin
      showmessage('串口打开失败!');
    end
    else
    begin
    end;
  end else
  begin
    CommClose(pchar('com1'));
  end;
end;

procedure TfrmBangAdd.Button1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    Button1.Click;
  end;
end;

end.
