unit U_KcRollCaiFen;

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;


type
  TfrmKcRollCaiFen = 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;
    MJQty4: TEdit;
    Label3: TLabel;
    Label4: TLabel;
    MJQty3: TEdit;
    Label6: TLabel;
    Label7: TLabel;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    V2Column2: TcxGridDBColumn;
    V2Column3: TcxGridDBColumn;
    V2Column4: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    V2Column5: TcxGridDBColumn;
    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(FMJID:string);
    procedure On1301(Var Message:Tmessage);Message 1301;
    procedure CloseCom(DllName:string);
    procedure OpenCom(DllName:string);
    { Private declarations }
  public
      iscom:boolean;
      isCommopen:boolean;
    { Public declarations }
  end;

var
  frmKcRollCaiFen: TfrmKcRollCaiFen;
    newh,newh1:hwnd;
implementation
uses
  U_DataLink,U_Fun,U_iniParam, U_ZDYHelp;
{$R *.dfm}
procedure TfrmKcRollCaiFen.CloseCom(DllName:string);
type
  TMyFunc = function(sCommName:PAnsiChar): HWND; stdcall;
var
  Tf1: TMyFunc;
  Tp1: TFarProc;
  Th1:Thandle;
begin
  Th1 := LoadLibrary(Pchar(trim(DllName)));
  if Th1 > 0 then
  begin
    try
      Tp1 := GetProcAddress(Th1, 'CommClose');
      if Tp1 <> nil then
      begin
        Tf1 := TMyFunc(Tp1);
        newh1:=Tf1('Comm1');
      end
      else
      begin

      end;
    finally
    // FreeLibrary(Th1);
    end;
  end
  else
  begin
    application.MessageBox(Pchar('找不到 '+trim(DllName)+' 文件！'),'提示');
  end;
end;

procedure TfrmKcRollCaiFen.OpenCom(DllName:string);
type
  TMyFunc = function(fhandle:hwnd;sCommName:PAnsiChar;
                    IntTime:integer;IsMessage:integer): HWND; stdcall;
var
  Tf:TMyFunc;
  Tp:TFarProc;
  Th:Thandle;
begin
 //  closeCom(FComFile);
  Th := LoadLibrary(Pchar(trim(DllName)));
  if Th > 0 then
  begin
    try
      Tp := GetProcAddress(Th, 'CommOpen');
      if Tp <> nil then
      begin
        Tf := TMyFunc(Tp);
        newh:=Tf(self.Handle,'Comm1',500,1);
        if newh<1 then
        begin
          application.MessageBox(Pchar('打开串口失败！'),'提示');
        end
        else
          IsCommOpen:=true;
      end
      else
      begin
        IsCommOpen:=false;
      end;
    finally
    // FreeLibrary(Th);
    end;
  end
  else
  begin
    IsCommOpen:=false;
    application.MessageBox(Pchar('找不到 '+trim(DllName)+' 文件！'),'提示');
  end;
end;

procedure TfrmKcRollCaiFen.On1301(Var Message:Tmessage);
var
  i1,i2:integer;
  unitname:string;
  fdata:double;
begin
  i1:=message.WParam;
  i2:=message.LParam;

  IF not CheckBox3.Checked then exit;

  MJLen.Text:= format('%.1f',[i1/100000]);

  IF i2=0 then Label9.Caption:='M' else  Label9.Caption:='Y';
end;

procedure TfrmKcRollCaiFen.Printdata(FMJID:string);
var
   fPrintFile:String;
   Txt,fImagePath:string;
   Moudle: THandle;
   Makebar:TMakebar;
   Mixtext:TMixtext;
begin
  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    SQL.Add(' P_Do_PrintJyBq'+quotedstr(Trim(FMJID)));
    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;

  if Trim(ADOQueryPrint.fieldbyname('LBName').AsString)<>'' then
    fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\'+Trim(ADOQueryPrint.fieldbyname('LBName').AsString)+'.rmf'
  else
    fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\通用英文标签.rmf' ;
  if FileExists(fPrintFile) then
  begin
    RMVariables['QRBARCODE']:=fImagePath;
    RM2.LoadFromFile(fPrintFile);
    Rm2.DefaultCopies:=1;
    RM2.ShowReport;
   // RM2.PrintReport;

  end
  else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\'+Trim(ADOQueryPrint.fieldbyname('LBName').AsString)),'提示',0);
  end;

end;
function  TfrmKcRollCaiFen.Savedata2():boolean;
var
  i,FCRID:integer;
  fconNo,FC_CodeName,FC_Color,fMJID:string;
  fRollNum,FQty,fMJQty2,FMJQty3,fMJQty4,FMJQty5,fMJMaoZ: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('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;
    fMJQty2:=0;
    fMJQty3:=0;
    fMJQty4:=0;
    fMJQty5:=0;
    fmjMaoZ:=0;
    with  CDSOrder do
    begin
      DisableControls;
      First;
      while not eof do
      begin
      //  if  Fieldbyname('Ssel').AsBoolean then
      //  begin
          IF i=0 then
          begin
            fCRID:=Fieldbyname('CRID').AsInteger;
            fMJID:=Fieldbyname('MJID').asstring;
          end;

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

          fMJQty2:=fMJQty2+Fieldbyname('MJQty2').AsFloat;
          fMJQty3:=strtofloatdef(trim(mjQty3.Text),0);
          fMJQty4:=strtofloatdef(trim(mjQty4.Text),0);
          fMJQty5:=fMJQty5+Fieldbyname('MJQty5').AsFloat;
          fmjMaoZ:=fMJQty3+fMJQty4;
          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('KCROllNum').Value:=0;
            fieldbyname('kcQty').Value:=0;
            fieldbyname('kcKGQty').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;
      fieldbyname('kckgQty').Value:=fmjMaoZ;
      post;
    end;

    with adoqueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from WFB_MJJY ');
      sql.Add('where MJID='+quotedstr(trim(fMJID)));
      open;
      edit;
      fieldbyname('MJLen').Value:=FQty;
      fieldbyname('MJQty2').Value:=fMJQty2;
      fieldbyname('MJQty3').Value:=fMJQty3;
      fieldbyname('MJQty4').Value:=fMJQty4;
      fieldbyname('MJQty5').Value:=fMJQty5;
      fieldbyname('MJMaoZ').Value:=fMJMaoZ;
      post;
    end;

    with adoqueryCmd do
    begin
      close;
      sql.Clear;
      SQL.Add('select A.*,D.PRTCodeName,D.PrtColor,C.MJQty2,C.MJQty3,C.MJQty4,C.MJQty5,C.MJMaoZ from CK_BanCP_KC A');
      sql.Add('inner join WFB_MJJY C on C.MJID=A.MJID ');
      sql.Add('inner join JYOrder_sub D on D.MainID=C.MainID and D.subID=C.subID ');
      sql.Add('where CRID='+quotedstr(inttostr(fCRID)));
      open;
      SInitCDSData20(adoqueryCmd,CDSMJID);
    end;

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

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



end;

function  TfrmKcRollCaiFen.Savedata1():boolean;
var
  fbcID,FCRID,fAPTM,FMJID,fNewMJID,fnewbcid:string;
  CRID:integer;
  i:integer;
  fKGQty1,fKGQty2:double;
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;
  fMJID:=CDSOrder.fieldbyname('MJID').AsString;
  ADOQueryCmd.Connection.BeginTrans;
  try
    //保存新卷
    with ADOQueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select * from WFB_MJJY A');
      sql.Add('where MJID='+quotedstr(trim(CDSOrder.fieldbyname('MJID').asstring)));
      open;
    end;
    if GetLSNo(ADOQueryCmd,fNewMJID,'C','WFB_MJJY',4,1)=False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('生成流水号异常！','提示',0);
      exit;
    end;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from WFB_MJJY 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('MJID').Value:=fNewMJID;
    ADOQueryCmd.FieldByName('MJLen').Value:=strtofloatdef(MJLen.Text,0);
    ADOQueryCmd.FieldByName('MJQty2').Value:=0;
    ADOQueryCmd.FieldByName('MJQty5').Value:=0;
    ADOQueryCmd.FieldByName('MJStr2').Value:='已入库';
    ADOQueryCmd.FieldByName('MJQty3').Value:=strtofloatdef(MJQty3.Text,0);
    ADOQueryCmd.FieldByName('MJQty4').Value:=strtofloatdef(MJQty4.Text,0);
    ADOQueryCmd.FieldByName('MJmaoz').Value:=strtofloatdef(MJQty4.Text,0)+strtofloatdef(MJQty3.Text,0);
    ADOQueryCmd.FieldByName('ZDMJID').Value:=fMJID;
    ADOQueryCmd.Post;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update WFB_MJJY SET mjxh=1+isnull((select cast (max(mjxh) as int) from WFB_MJJY X where X.subID=WFB_MJJY.subID and X.MJstr4=WFB_MJJY.Mjstr4 and X.MJID<>WFB_MJJY.MJID and X.MJType<>''零碎''),0)');
      sql.Add('where MJID='''+Trim(fNewMJID)+'''');
      execsql;
    end;
  //修改原匹数量
    with adoqueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from WFB_MJJY ');
      sql.Add('where MJID='+quotedstr(trim(fMJID)));
      open;
      edit;
      fieldbyname('MJLen').Value:=fieldbyname('MJLen').AsFloat-strtofloatdef(MJLen.Text,0);
      fieldbyname('MJQty4').Value:=fieldbyname('MJQty4').AsFloat-strtofloatdef(MJQty4.Text,0);
      fieldbyname('MJMaoZ').Value:=fieldbyname('MJMaoZ').AsFloat-strtofloatdef(MJQty4.Text,0)-strtofloatdef(MJQty3.Text,0);
      post;
    end;
    //更新出入库数据
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Do_CFCRK @MJID='
          +quotedstr(trim(fNewMJID))
          +',@OldMJID='+quotedstr(trim(fMJID)));
      execsql;
    end;

    //更新检验进度
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Update_Order_JD_ByOther @MainType=''检验信息编辑'', @MJId= '+quotedstr(Trim(fNewMJID)));
      execsql;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    
    with adoqueryCmd do
    begin
      close;
      sql.Clear;
      SQL.Add('select A.*,D.PRTCodeName,D.PrtColor,C.MJQty2,C.MJQty3,C.MJQty4,C.MJQty5,C.MJMaoZ from CK_BanCP_KC A');
      sql.Add('inner join WFB_MJJY C on C.MJID=A.MJID ');
      sql.Add('inner join JYOrder_sub D on D.MainID=C.MainID and D.subID=C.subID ');
      sql.Add('where C.MJID in ('+quotedstr(trim(fNewMJID))+','+quotedstr(trim(fMJID))+')');
      open;
      SInitCDSData20(adoqueryCmd,CDSMJID);
    end;

    CDSOrder.EmptyDataSet;

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

end;

procedure TfrmKcRollCaiFen.InitGrid();
begin
  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.*,D.PRTCodeName,D.PrtColor,C.MJQty2,C.MJQty3,C.MJQty4,C.MJQty5,C.MJMaoZ from CK_BanCP_KC A');
    sql.Add('inner join WFB_MJJY C on C.MJID=A.MJID ');
    sql.Add('inner join JYOrder_sub D on D.MainID=C.MainID and D.subID=C.subID ');
    SQL.Add('where 1=2 ');
    Open;
  end;
  SCreateCDS20(ADOQueryMain,CDSOrder);
  SInitCDSData20(ADOQueryMain,CDSOrder);
  SCreateCDS20(ADOQueryMain,CDSMJID);
  SInitCDSData20(ADOQueryMain,CDSMJID);
end;

procedure TfrmKcRollCaiFen.InitMJID();
var
  fP_No:string;
begin
  MovePanel1.Visible:=false;
//  with ADOQueryTmp do
//  begin
//    Close;
//    sql.Clear;
//    SQL.Add('select B.MJID from CK_BanCP_CR B where B.CRFlag=''入库'' ');
//    SQL.Add('and B.MJID='+quotedstr(trim(SmNO.text)));
//    Open;
//  end;
//  if  ADOQueryTmp.IsEmpty  then
//  begin
//      MovePanel1.Visible:=true;
//      Edit2.Text:=SmNO.Text;
//      Label2.Caption:='此条码未入库';
//      exit;
//  end;
  with ADOQueryTmp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select B.MJID from CK_BanCP_CR B where B.CRFlag=''出库'' ');
    SQL.Add('and B.MJID='+quotedstr(trim(SmNO.text)));
    Open;
  end;
  if  ADOQueryTmp.IsEmpty=false  then
  begin
      MovePanel1.Visible:=true;
      Edit2.Text:=SmNO.Text;
      Label2.Caption:='此条码已出库';
      exit;
  end;
  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.*,D.PRTCodeName,D.PrtColor,C.MJQty2,C.MJQty3,C.MJQty4,C.MJQty5,C.MJMaoZ from CK_BanCP_KC A');
    sql.Add('inner join WFB_MJJY C on C.MJID=A.MJID ');
    sql.Add('inner join JYOrder_sub D on D.MainID=C.MainID and D.SubId=C.SubId ');
   // 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 KCQty>0  ');
    Open;
    if not IsEmpty  then
    begin
      IF ((Fieldbyname('KCQty').AsFloat<=0) and (Fieldbyname('KCKGQty').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 TfrmKcRollCaiFen.FormDestroy(Sender: TObject);
begin
  wRITECxGrid(self.Caption+tV1.Name,Tv1,'成品仓库');
  wRITECxGrid(self.Caption+TV2.Name,Tv2,'成品仓库');
  frmKcRollCaiFen:=nil;
end;

procedure TfrmKcRollCaiFen.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  if  isCommopen then
  begin
    closeCom(JCYDLL);
  end;
  Action:=caFree;
end;

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

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

procedure TfrmKcRollCaiFen.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 TfrmKcRollCaiFen.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 TfrmKcRollCaiFen.Button4Click(Sender: TObject);
begin
  MovePanel1.Visible:=false;  
end;

procedure TfrmKcRollCaiFen.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 TfrmKcRollCaiFen.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 TfrmKcRollCaiFen.Edit6Click(Sender: TObject);
begin
  if Trim(Edit6.Text)='' then
  begin
    Edit6.Text:='√';
  end else
  begin
    Edit6.Text:='';
  end;
end;

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

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

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

end.
