unit U_MarkingInspection;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxCalendar, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxControls, cxGridCustomView, cxGrid, RM_Common, RM_Class, RM_GridReport,
  RM_System, RM_Dataset, ADODB, DBClient, cxGridCustomPopupMenu, cxGridPopupMenu,
  ExtCtrls, ComCtrls, ToolWin, cxTextEdit, Buttons, cxSplitter, cxCheckBox,
  MovePanel, IniFiles, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator;

type
  TfrmMarkingInspection = class(TForm)
    cxGridPopupMenu1: TcxGridPopupMenu;
    DataSource1: TDataSource;
    Order_MJ: TClientDataSet;
    ADOCmd: TADOQuery;
    RMDB_Main: TRMDBDataSet;
    RM2: TRMGridReport;
    DS_MJCD: TDataSource;
    CDS_MJCD: TClientDataSet;
    DS_MJID: TDataSource;
    CDS_MJID: TClientDataSet;
    ADOQueryPrint: TADOQuery;
    ScrollBox1: TScrollBox;
    SpeedButton13: TSpeedButton;
    SpeedButton14: TSpeedButton;
    SpeedButton15: TSpeedButton;
    SpeedButton16: TSpeedButton;
    SpeedButton17: TSpeedButton;
    SpeedButton18: TSpeedButton;
    SpeedButton21: TSpeedButton;
    SpeedButton22: TSpeedButton;
    SpeedButton23: TSpeedButton;
    SpeedButton24: TSpeedButton;
    SpeedButton25: TSpeedButton;
    SpeedButton26: TSpeedButton;
    SpeedButton29: TSpeedButton;
    SpeedButton30: TSpeedButton;
    SpeedButton31: TSpeedButton;
    SpeedButton32: TSpeedButton;
    SpeedButton33: TSpeedButton;
    SpeedButton34: TSpeedButton;
    SpeedButton37: TSpeedButton;
    SpeedButton38: TSpeedButton;
    SpeedButton39: TSpeedButton;
    SpeedButton40: TSpeedButton;
    SpeedButton41: TSpeedButton;
    SpeedButton42: TSpeedButton;
    cxGridPopupMenu2: TcxGridPopupMenu;
    ADOQueryMainDSC: TADOQuery;
    SpeedButton45: TSpeedButton;
    SpeedButton46: TSpeedButton;
    SpeedButton47: TSpeedButton;
    SpeedButton48: TSpeedButton;
    SpeedButton50: TSpeedButton;
    SpeedButton51: TSpeedButton;
    SpeedButton54: TSpeedButton;
    SpeedButton55: TSpeedButton;
    SpeedButton56: TSpeedButton;
    SpeedButton57: TSpeedButton;
    SpeedButton58: TSpeedButton;
    SpeedButton59: TSpeedButton;
    SpeedButton62: TSpeedButton;
    SpeedButton63: TSpeedButton;
    SpeedButton64: TSpeedButton;
    SpeedButton65: TSpeedButton;
    SpeedButton66: TSpeedButton;
    SpeedButton67: TSpeedButton;
    SpeedButton70: TSpeedButton;
    SpeedButton71: TSpeedButton;
    SpeedButton72: TSpeedButton;
    SpeedButton73: TSpeedButton;
    SpeedButton74: TSpeedButton;
    SpeedButton75: TSpeedButton;
    Panel2: TPanel;
    Label9: TLabel;
    Label4: TLabel;
    MJGW: TEdit;
    Label5: TLabel;
    Label11: TLabel;
    BTPrint: TButton;
    Button5: TButton;
    Edit3: TEdit;
    Label10: TLabel;
    Edit4: TEdit;
    Label12: TLabel;
    Panel3: TPanel;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    SpeedButton5: TSpeedButton;
    SpeedButton6: TSpeedButton;
    SpeedButton7: TSpeedButton;
    SpeedButton8: TSpeedButton;
    SpeedButton9: TSpeedButton;
    SpeedButton10: TSpeedButton;
    SpeedButton11: TSpeedButton;
    SpeedButton12: TSpeedButton;
    SpeedButton49: TSpeedButton;
    Label26: TLabel;
    Edit6: TEdit;
    Label30: TLabel;
    MJTare: TEdit;
    Label31: TLabel;
    Label32: TLabel;
    MJNW: TEdit;
    Label33: TLabel;
    MovePanel1: TMovePanel;
    Label17: TLabel;
    Label18: TLabel;
    Label20: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Edit1: TEdit;
    Button1: TButton;
    Button4: TButton;
    Edit2: TEdit;
    CDQty: TEdit;
    RadioGroup1: TRadioGroup;
    Button2: TButton;
    Panel7: TPanel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    tv2CDType: TcxGridDBColumn;
    tv2CDWZ: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    Tv2CDQty: TcxGridDBColumn;
    Tv2CDReason: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel8: TPanel;
    OrdNote: TRichEdit;
    Label34: TLabel;
    ComboBox1: TComboBox;
    SpeedButton19: TSpeedButton;
    SpeedButton20: TSpeedButton;
    SpeedButton27: TSpeedButton;
    SpeedButton28: TSpeedButton;
    SpeedButton35: TSpeedButton;
    SpeedButton36: TSpeedButton;
    SpeedButton43: TSpeedButton;
    SpeedButton44: TSpeedButton;
    SpeedButton52: TSpeedButton;
    SpeedButton53: TSpeedButton;
    SpeedButton60: TSpeedButton;
    SpeedButton61: TSpeedButton;
    SpeedButton68: TSpeedButton;
    SpeedButton69: TSpeedButton;
    SpeedButton76: TSpeedButton;
    SpeedButton77: TSpeedButton;
    SpeedButton78: TSpeedButton;
    SpeedButton79: TSpeedButton;
    SpeedButton80: TSpeedButton;
    SpeedButton81: TSpeedButton;
    SpeedButton82: TSpeedButton;
    SpeedButton83: TSpeedButton;
    Label36: TLabel;
    Label38: TLabel;
    RM3: TRMGridReport;
    mjxh: TEdit;
    ADOTmp: TADOQuery;
    Panel5: TPanel;
    Panel6: TPanel;
    Label1: TLabel;
    Label35: TLabel;
    Label39: TLabel;
    Scan: TEdit;
    Edit7: TEdit;
    Edit8: TEdit;
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    TBCD: TToolButton;
    TBClose: TToolButton;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    v3Column1: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    v3Column4: TcxGridDBColumn;
    v3Column5: TcxGridDBColumn;
    v3Column3: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Label2: TLabel;
    GangNo: TEdit;
    MJXS: TEdit;
    MJLen: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBCDClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure BTPrintClick(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton12Click(Sender: TObject);
    procedure SpeedButton13Click(Sender: TObject);
    procedure cxGridDBColumn2PropertiesChange(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure SpeedButton49Click(Sender: TObject);
    procedure ScanKeyPress(Sender: TObject; var Key: Char);
    procedure Edit1Click(Sender: TObject);
    procedure Edit2Change(Sender: TObject);
    procedure Edit3Click(Sender: TObject);
    procedure Edit4Click(Sender: TObject);
    procedure Edit6Click(Sender: TObject);
    procedure MJstr3KeyPress(Sender: TObject; var Key: Char);
    procedure Button2Click(Sender: TObject);
    procedure MJGWChange(Sender: TObject);
    procedure ScanClick(Sender: TObject);
    procedure Edit7Click(Sender: TObject);
    procedure MJNWKeyPress(Sender: TObject; var Key: Char);
    procedure FormCreate(Sender: TObject);
    procedure Edit8Click(Sender: TObject);
    procedure MJLenChange(Sender: TObject);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
  private
    { Private declarations }
    FColumn, FFFMJID, FOperationStatus: string;
    FAPID, FSPID, FMainId, FSubId, FMJCode, FMJName, FMJSpec, FMJMF, FMJKZ, FMJColor, FAPBatchNo, FCarNo: string;   // 产品信息
    FCDName: string; //疵点名称
    isCommopen, IsJsMessage: boolean;
    fxsType, fRollType: string;
    procedure InitDefectOptions(); //刷新疵点选项
    procedure InitCDGrid(); //刷新疵点信息
    procedure InitInspectionRecord(); //刷新检验信息

    function CheckData(CKType: string): Boolean; //校验数据
    function SaveData(): Boolean; //保存数据
    procedure ScanOrder();  //扫描订单

    procedure ClearGlobal(MStatus: string); //清除全局ID
    procedure PrintCKMDG(MRKOrdID: string); //打印入库单
    procedure PrtData(FMJID: string);  //打印卷标签
    procedure QuantityConversion();  //数量换算
    procedure OpenCom(DllName: string);
    procedure CloseCom(DllName: string);
    procedure On1201(var Message: Tmessage); message 1201;
    procedure On1301(var Message: Tmessage); message 1301;
  public
    fmanage: string;
    { Public declarations }

  end;

var
  frmMarkingInspection: TfrmMarkingInspection;
  newh, newh1: hwnd;
  IniFile: TIniFile;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_iniParam;

{$R *.dfm}

procedure TfrmMarkingInspection.QuantityConversion();
var
  MLen, MNWet, MGWet, MPWet, MCC, XiShu, MGreyLen: Double;
begin
  MLen := StrToFloatdef(trim(MJLen.Text), 0);
  MNWet := StrToFloatdef(trim(MJNW.Text), 0);
  MPWet := StrToFloatdef(trim(MJTare.Text), 0);
  MGWet := StrToFloatdef(trim(MJGW.Text), 0);
  MCC := StrToFloatdef(trim(MJXS.Text), 0);
  if MCC > 0 then
  begin
    MGWet := StrToFloat(format('%.1f', [MLen * MCC]));
    MNWet := MGWet - MPWet;
    MJGW.Text := FloatToStr(MGWet);
    MJNW.Text := FloatToStr(MNWet);
  end;

  MNWet := MGWet - MPWet;
  MJNW.Text := FloatToStr(MNWet);

end;

function TfrmMarkingInspection.CheckData(CKType: string): Boolean;
var
  mvalue: Double;
begin
  try
    if Trim(FSPID) = '' then
      raise Exception.Create('请扫描检验指示单！');

    if Trim(CKType) = '提交' then
    begin
      if Trim(GangNo.Text) = '' then
        raise Exception.Create('缸号不能为空！');

      if Trim(MJGW.Text) <> '' then
      begin
        if TryStrToFloat(MJGW.Text, mvalue) = False then
          raise Exception.Create('重量录入错误!');

        if strtofloatdef(trim(MJGW.Text), 0) > 80 then
          raise Exception.Create('重量不能大于80!');
      end;

      if strtofloatdef(trim(MJNW.Text), 0) < 0.00 then
        raise Exception.Create('重量不能小于0!');

      if strtofloatdef(trim(MJLen.Text), 0) < 0.00 then
        raise Exception.Create('长度不能小于0!');

      if Trim(MJLen.Text) <> '' then
      begin
        if strtofloatdef(trim(MJLen.Text), 0) > 400 then
          raise Exception.Create('长度不能大于400!');
      end;

      if (strtofloatdef(trim(MJLen.Text), 0) = 0) and (strtofloatdef(trim(MJNW.Text), 0) = 0) then
        raise Exception.Create('长度和重量不能都为0!!');

    end;
    Result := True;
  except
    Result := false;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmMarkingInspection.ClearGlobal(MStatus: string);
begin
  if MStatus = '扫描流程卡' then
  begin
    FAPID := '';
    FSPID := '';
    FMainId := '';
    FSubId := '';
    FFFMJID := '';
    FMJCode := '';
    FMJName := '';
    FMJSpec := '';
    FMJMF := '';
    FMJKZ := '';
    FMJColor := '';
    FAPBatchNo := '';
    FCarNo := '';
    MJXH.Text := '';
    MJXs.Text := '';
    MJGW.Text := '';
    MJNW.Text := '';
    MJLen.Text := '';

    GangNo.Text := '';

  end;

  if MStatus = '提交' then
  begin
    MJLen.Text := '';

    MJGW.Text := '';
    MJNW.Text := '';
    FOperationStatus := '';
    FFFMJID := '';
    FAPID := '';

  end;

end;

procedure TfrmMarkingInspection.PrintCKMDG(MRKOrdID: string);
var
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin

  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add(' exec P_Print_RKMDGangNo ');
    SQL.Add(' @RKOrdID=''' + Trim(MRKOrdID) + '''');
    Open;
  end;
  try
    Moudle := LoadLibrary('MakeQRBarcode.dll');
    @Makebar := GetProcAddress(Moudle, 'Make');
    @Mixtext := GetProcAddress(Moudle, 'MixText');
    Txt := Trim(ADOQueryPrint.fieldbyname('RKOrdID').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;
  ExportFtErpFile('验布工仓库码单.rmf', ADOTmp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\验布工仓库码单.rmf';
  if FileExists(fPrintFile) then
  begin
    RMVariables['QRBARCODE'] := fImagePath;
    RM2.LoadFromFile(fPrintFile);
    Rm2.DefaultCopies := 1;
    RM2.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
    Exit;
  end;
end;

procedure TfrmMarkingInspection.ScanOrder();
begin

  with ADOTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.*,C.* ');
    sql.Add(',maxRoll=1+isnull((select cast (max(mjxh) as int) from FinishedCloth_Inspect X    ');
    sql.Add(' where X.SPID=C.CRID and X.MJType =''正品'' ),0)');
    sql.Add('from Cloth_Main A ');
    sql.Add('inner join Cloth_Sub B on A.MainId=B.MainId');
    sql.Add('inner join CK_Cloth_CR  C on C.ToSubId=B.SubId');
    sql.Add('where  C.CRFlag=''出库'' and C.Valid=''Y'' and C.CRType=''检验出库'' and C.CKName=''待检布仓库'' ');
    sql.Add('and C.CRID=''' + Trim(Scan.Text) + '''');
    //ShowMessage(SQL.Text);
    Open;
  end;

  if not ADOTmp.IsEmpty then
  begin
    FAPID := '';
    FSPID := Trim(ADOTmp.fieldbyname('CRID').AsString);
    FMainId := Trim(ADOTmp.fieldbyname('MainId').AsString);
    FSubId := Trim(ADOTmp.fieldbyname('SubId').AsString);
    FMJCode := Trim(ADOTmp.fieldbyname('C_Code').AsString);
    FMJName := Trim(ADOTmp.fieldbyname('C_CodeName').AsString);
    FMJSpec := Trim(ADOTmp.fieldbyname('C_Spec').AsString);
    FMJMF := Trim(ADOTmp.fieldbyname('MF').AsString);
    FMJKZ := Trim(ADOTmp.fieldbyname('KZ').AsString);
    FMJColor := trim(ADOTmp.fieldbyname('C_Color').AsString);
    MJXH.Text := Trim(ADOTmp.fieldbyname('maxRoll').AsString);
    GangNo.Text := Trim(ADOTmp.fieldbyname('GangNo').AsString);

    OrdNote.Text := '订单号:' + Trim(ADOTmp.fieldbyname('ConNo').AsString) + #13;
    OrdNote.Text := OrdNote.Text + '编号:' + FMJCode + #13;
    OrdNote.Text := OrdNote.Text + '品名:' + FMJName + #13;
    OrdNote.Text := OrdNote.Text + '门幅:' + FMJMF + #13;
    OrdNote.Text := OrdNote.Text + '克重:' + FMJKZ + #13;
    OrdNote.Text := OrdNote.Text + '颜色:' + FMJColor + #13;
    OrdNote.Text := OrdNote.Text + '特别提示:' + Trim(ADOTmp.fieldbyname('Note').AsString);

  end
  else
  begin
    ClearGlobal('扫描');
  end;

  InitInspectionRecord();
  InitCDGrid();

  Scan.Text := '';

end;

procedure TfrmMarkingInspection.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 TfrmMarkingInspection.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 TfrmMarkingInspection.FormDestroy(Sender: TObject);
begin
  frmMarkingInspection := nil;
end;

procedure TfrmMarkingInspection.On1301(var Message: Tmessage);
var
  i1, i2: integer;
  unitname: string;
  fdata: double;
begin
  i1 := Message.WParam;
  i2 := Message.LParam;
  if IsJsMessage then
  begin
    if (trim(Edit7.Text) = '√') then
    begin
      MJLen.Text := format('%.f', [i1 / 100000]);
      if i2 = 0 then
        Label9.Caption := 'M'
      else
        Label9.Caption := 'Y';
      edit1.Text := format('%.1f', [i1 / 100000]);
    end;
  end;

end;

procedure TfrmMarkingInspection.On1201(var Message: Tmessage);
var
  i1, i2: integer;
  unitname: string;
  fdata: double;
begin
  i1 := Message.WParam;
  i2 := Message.LParam;
  if IsJsMessage then
  begin
    if trim(Edit7.Text) = '√' then
    begin
      MJGW.Text := format('%.1f', [i1 / 100000]);
    end;
  end;
end;

procedure TfrmMarkingInspection.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  //DataLink_WFBProducttion.ADOLink.Connected:=False;

  if isCommopen then
  begin
    closeCom(DZCDYDllName);
    closeCom(MBDYDllName);
  end;
  Action := caFree;
end;

procedure TfrmMarkingInspection.InitCDGrid();
begin
  with ADOTmp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from FinishedCloth_Inspect_CD where MJID=' + quotedstr(Trim(FFFMJID)));
    Open;
  end;
  SCreateCDS20(ADOTmp, CDS_MJCD);
  SInitCDSData20(ADOTmp, CDS_MJCD);
end;

procedure TfrmMarkingInspection.InitInspectionRecord();
begin
  with ADOTmp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.*');
    sql.Add('from FinishedCloth_Inspect A where MJCRType=''未入库'' ');
    sql.Add(' and Filler=''' + Trim(DName) + '''');
    sql.Add(' and DATEDIFF(HH,Filltime,GETDATE())<5');
    Open;
  end;
  SCreateCDS20(ADOTmp, CDS_MJID);
  SInitCDSData20(ADOTmp, CDS_MJID);

end;

procedure TfrmMarkingInspection.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmMarkingInspection.TBCDClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'WFBCD';
      flagname := '疵点';
      fnote := True;
      V1Note.Caption := '英文名称';
      if ShowModal = 1 then
      begin
        Self.InitDefectOptions();
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmMarkingInspection.FormShow(Sender: TObject);
begin
  InitDefectOptions();
  InitCDGrid();

  if IsINIFile() then
    ReadINIFile()
  else
    WriteINIFile;

  if trim(Edit7.Text) = '√' then
    IsJsMessage := true;

  if trim(fmanage) <> '高权限' then
  begin
    if trim(DZCDYDllName) <> '' then
      OpenCom(DZCDYDllName);
    if trim(MBDYDllName) <> '' then
      OpenCom(MBDYDllName);
    Edit7.Visible := true;
    Label35.Visible := true;
  end;
end;

procedure TfrmMarkingInspection.InitDefectOptions();
var
  AA: array[0..100] of string;
  i, j: Integer;
begin
  with ADOTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('select ZDYName from KH_Zdy where Type=''WFBCD'' order by ZDYNO ');
    Open;
  end;
  if ADOTmp.IsEmpty then
  begin
    Application.MessageBox('没有定义疵点！', '提示', 0);
    Exit;
  end;
  with ADOTmp do
  begin
    First;
    i := 0;
    while not Eof do
    begin
      AA[i] := Trim(fieldbyname('ZDYName').AsString);
      i := i + 1;
      Next;
    end;
  end;
  i := i - 1;
  if i > 69 then
  begin
    i := 69;
  end;
  for j := 0 to i do
  begin
    with ScrollBox1 do
    begin
      TSpeedButton(Controls[j]).Visible := True;
      TSpeedButton(Controls[j]).Hint := AA[j];
      if Length(AA[j]) > 4 then
      begin
        TSpeedButton(Controls[j]).Caption := Copy(Trim(AA[j]), 1, 4) + #13 + Copy(Trim(AA[j]), 5, Length(AA[j]) - 4);
      end
      else
        TSpeedButton(Controls[j]).Caption := AA[j];
    end;
  end;
end;

function TfrmMarkingInspection.SaveData(): Boolean;
var
  maxno, FMJID, BZID, strsql: string;
  FMJLen: Double;
begin

  FMJID := '';

  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from FinishedCloth_Inspect where MJId=''' + Trim(FMJID) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMJID) = '' then
      begin
        Append;
        if GetLSNo(ADOTmp, maxno, Trim(SCXFlag), 'FinishedCloth_Inspect', 4, 1) = False then
        begin
          ADOCmd.Connection.RollbackTrans;
          Application.MessageBox('生成流水号异常！', '提示', 0);
          exit;
        end;
      end
      else
      begin
        maxno := Trim(FMJID);
        Edit;
      end;
      FieldByName('MainId').value := Trim(FMainId);
      FieldByName('SubId').value := Trim(FSubId);
      FieldByName('APId').value := Trim(FAPID);
      FieldByName('SPID').value := Trim(FSPID);
      FieldByName('APBatchNo').value := Trim(FAPBatchNo);
      FieldByName('GangNo').value := Trim(GangNo.Text);

      FieldByName('CarNo').value := Trim(FCarNo);
      FieldByName('MJID').Value := Trim(maxno);
      FieldByName('MJCRType').Value := '未入库';

      FieldByName('MJXH').Value := strtointdef(trim(MJXH.Text), 0);

    
      //长度
      FieldByName('MJLen').Value := StrToFloatdef(trim(MJLen.Text), 0);
      //皮重
      FieldByName('MJTare').Value := StrToFloatdef(trim(MJTare.Text), 0);
      //毛重
      FieldByName('MJGW').Value := StrToFloatdef(trim(MJGW.Text), 0);
      //净重
      FieldByName('MJNW').Value := StrToFloatdef(trim(MJGW.Text), 0) - StrToFloatdef(trim(MJTare.Text), 0);
      //系数
      FieldByName('MJXS').Value := StrToFloatdef(trim(MJXS.Text), 0);

      if Trim(Edit3.Text) <> '' then
      begin
        FieldByName('MJType').Value := '正品';
      end;
      if Trim(Edit4.Text) <> '' then
      begin
        FieldByName('MJType').Value := '次品';
      end;

      if Trim(FMJID) = '' then
      begin
        FieldByName('Filler').Value := Trim(DName);
      end
      else
      begin
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOTmp);
      end;
      FieldByName('JTType').Value := Trim(SCXFlag);
      Post;
    end;
//    if FJHGZ = '按缸排序' then
    begin
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('update FinishedCloth_Inspect SET mjxh=1+isnull((select cast (max(mjxh) as int) from FinishedCloth_Inspect X   ');
        sql.Add('where X.subID=FinishedCloth_Inspect.subID and X.GangNo=FinishedCloth_Inspect.GangNo  ');
        sql.Add('and X.SPID=FinishedCloth_Inspect.SPID and X.MJID<>FinishedCloth_Inspect.MJID and  X.MJType =''正品''),0)');
        sql.Add('where MJID=''' + Trim(maxno) + '''');
//        ShowMessage(SQL.Text);
        execsql;
      end;
    end;

    //查询卷号修改界面
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select MJXH from FinishedCloth_Inspect');
      sql.Add('where MJID=''' + Trim(maxno) + '''');
      open;
      if not IsEmpty then
      begin
        MJXH.Text := fieldbyname('mjxh').AsString;
      end;
    end;

    FMJID := Trim(maxno);
    FFFMJID := Trim(maxno);

////////////////////////保存母卷疵点表//////////////////////////////////////
    with CDS_MJCD do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_MJCD.fieldbyname('MCID').AsString) = '' then
        begin
          if GetLSNo(ADOTmp, maxno, 'MC', 'FinishedCloth_Inspect_CD', 5, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxno := Trim(CDS_MJCD.fieldbyname('MCID').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from FinishedCloth_Inspect_CD  ');
          sql.Add(' where MCID=''' + Trim(maxno) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(CDS_MJCD.fieldbyname('MCID').AsString) = '' then
            Append
          else
            Edit;
          FieldByName('MJId').Value := Trim(FMJID);
          FieldByName('MCID').Value := Trim(maxno);
          SSetSaveDataCDSNew(ADOCmd, Tv2, CDS_MJCD, 'FinishedCloth_Inspect_CD', 0);
          FieldByName('KouFenType').Value := CDS_MJCD.fieldbyname('KouFenType').Value;
          Post;
        end;
        CDS_MJCD.Edit;
        CDS_MJCD.FieldByName('MCID').Value := Trim(maxno);
        Next;
      end;
    end;

    with ADOCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update FinishedCloth_Inspect SET ');
      sql.Add('CDList=cast((select CDName+CAST(CDQty as varchar)+''M;'' from FinishedCloth_Inspect_CD where FinishedCloth_Inspect_CD.MJID=FinishedCloth_Inspect.MJID for xml path('''')) as varchar(200))');
      sql.Add('where MJID=''' + Trim(FMJID) + ''' ');
      execsql;
    end;

////////////////////////保存母卷疵点表//////////////////////////////////////

        //出入库及库存
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_FinishedCloth_CRK @MJID=''' + trim(FMJID) + '''');
      execsql;
    end;

    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
end;

procedure TfrmMarkingInspection.BTPrintClick(Sender: TObject);
var
  fPrintFile: string;
  mvalue: Double;
  i: Integer;
begin
  Scan.SetFocus;
  BTPrint.Enabled := False;
  if CheckData('提交') = False then
  begin
    BTPrint.Enabled := True;
    exit;
  end;

  if SaveData() then
  begin

    MJXH.Text := inttostr(strTointdef(trim(MJXH.Text), 0) + 1);
    if Trim(Edit6.Text) = '√' then
    begin
      PrtData(FFFMJID);
    end;
  end
  else
  begin
    BTPrint.Enabled := True;
  end;

  InitInspectionRecord();
  InitCDGrid();
  ClearGlobal('提交');
  MJLen.SetFocus;
  Edit1Click(MJLen);
  BTPrint.Enabled := True;
end;

procedure TfrmMarkingInspection.PrtData(FMJID: string);
var
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add(' EXEC P_Print_FinishedLab ');
    SQL.Add(' @MJID=''' + Trim(FMJID) + '''');
    Open;
  end;
  if ADOQueryPrint.IsEmpty then
  begin
    application.MessageBox('标签内容未找到！', '提示信息', MB_ICONERROR);
    exit;
  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;
  ExportFtErpFile(Trim(ADOQueryPrint.fieldbyname('LabName').AsString), ADOTmp);
  if Trim(ADOQueryPrint.fieldbyname('LabName').AsString) <> '' then
  begin
    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(ADOQueryPrint.fieldbyname('LabName').AsString)
  end
  else
  begin
    ExportFtErpFile('通用成品标签.rmf', ADOTmp);
    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\通用成品标签.rmf';
  end;

  if FileExists(fPrintFile) then
  begin
    RMVariables['QRBARCODE'] := fImagePath;
    RM2.LoadFromFile(fPrintFile);
    Rm2.DefaultCopies := strtointdef(trim(ComboBox1.Text), 1);
    RM2.PrintReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

procedure TfrmMarkingInspection.SpeedButton1Click(Sender: TObject);
var
  fsj: string;
begin
  fsj := Trim(TSpeedButton(Sender).Hint);
  if Trim(fsj) = '' then
    Exit;
  fsj := Trim(TEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).Text);
  TEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).Text := fsj + Trim(TSpeedButton(Sender).Caption);
  TEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).SelectAll;
end;

procedure TfrmMarkingInspection.SpeedButton12Click(Sender: TObject);
var
  fsj: string;
begin
  fsj := Trim(TEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).Text);
  if Trim(fsj) = '' then
    Exit;
  TEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).Text := Copy(fsj, 1, Length(fsj) - 1);
  TEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).SelectAll;
end;

procedure TfrmMarkingInspection.SpeedButton13Click(Sender: TObject);
var
  i: Integer;
begin
  if CheckData('疵点') = False then
    exit;

  FCDName := Trim(TSpeedButton(Sender).Hint);
  MovePanel1.Visible := True;
  
  //显示数字键盘
  Edit1.SetFocus;
  Panel3.Visible := True;
  with Panel3 do
  begin
    for i := 0 to ControlCount - 1 do
    begin
      if Controls[i] is TSpeedButton then
      begin
        TSpeedButton(Controls[i]).Hint := Trim(Edit1.Name);
      end;
    end;
  end;

end;

procedure TfrmMarkingInspection.cxGridDBColumn2PropertiesChange(Sender: TObject);
var
  fsj: string;
begin

  fsj := Trim(Order_MJ.fieldbyname('SubId').AsString);
  Order_MJ.DisableControls;
  with Order_MJ do
  begin
    First;
    while not Eof do
    begin
      if Trim(Order_MJ.fieldbyname('SubId').AsString) <> fsj then
      begin
        Edit;
        FieldByName('SSel').Value := False;
      end;
      Next;
    end;
  end;
  Order_MJ.EnableControls;
  Order_MJ.Locate('SubId', fsj, []);
end;

procedure TfrmMarkingInspection.Button1Click(Sender: TObject);
var
  mvalue: Double;
begin
 { if Trim(Edit1.Text)='' then
  begin
    Application.MessageBox('位置不能为空！','提示',0);
    exit;
  end;  }
  if Trim(Edit1.Text) <> '' then
  begin
    if Trim(Edit2.Text) <> '' then
    begin
      if StrToFloat(Edit2.Text) < StrToFloat(Edit1.Text) then
      begin
        Application.MessageBox('疵点位置录入错误!', '提示', 0);
        Exit;
      end;
    end;
  end;
  if CDQty.Text <> '' then
  begin
    if TryStrToFloat(CDQty.Text, mvalue) = False then
    begin
      Application.MessageBox('非法数字!', '提示', 0);
      Exit;
    end;
  end;
  with CDS_MJCD do
  begin
    Append;
    FieldByName('cdname').Value := Trim(FCDName);
    FieldByName('CDbeg').Value := Trim(Edit1.Text);
    FieldByName('CDEnd').Value := Trim(Edit2.Text);
    FieldByName('KouFenQty').Value := 0;
    if RadioGroup1.ItemIndex > -1 then
      FieldByName('KouFenType').Value := RadioGroup1.Items.Strings[RadioGroup1.ItemIndex];
    if RadioGroup1.ItemIndex > -1 then
    begin
      if RadioGroup1.ItemIndex < 3 then
      begin
        FieldByName('KouFenQty').Value := RadioGroup1.ItemIndex + 1;
      end
      else
      begin
        FieldByName('KouFenQty').Value := 4;
      end;
    end;
    if Trim(FCDName) = '测幅宽' then
      FieldByName('KouFenQty').Value := 0;
    FieldByName('CDQty').Value := StrToFloatdef(trim(CDQty.Text), 0);
    {if Trim(Edit2.Text)<>'' then
    begin
      FieldByName('CDQty').Value:=StrToFloat(Edit2.Text)-StrToFloat(Edit1.Text);
    end else
    begin
      FieldByName('CDQty').Value:=0;
    end; }

    Post;
  end;
  Edit1.Text := '';
  Edit2.Text := '';
  CDQty.Text := '';
  MovePanel1.Visible := False;
  Panel3.Visible := False;
end;

procedure TfrmMarkingInspection.Button5Click(Sender: TObject);
var
  fPrintFile: string;
  i: Integer;
  mvalue: Double;
begin
  if CDS_MJID.IsEmpty then
    Exit;
  PrtData(CDS_MJID.fieldbyname('MJID').AsString);
end;

procedure TfrmMarkingInspection.SpeedButton49Click(Sender: TObject);
begin
  Panel3.Visible := False;
end;

procedure TfrmMarkingInspection.ScanKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if Pos('DC', Trim(Scan.Text)) > 0 then
    begin
      ClearGlobal('扫描检验指示单');
      FOperationStatus := '流程卡';
      ScanOrder();
    end
    else
    begin
      application.MessageBox('请扫描检验指示单！', '提示信息', 0);
    end;

    MJLen.SetFocus;
    Edit1Click(MJLen);
    Scan.Text := '';
  end;
end;

procedure TfrmMarkingInspection.Edit1Click(Sender: TObject);
var
  i: Integer;
begin
  Panel3.Visible := True;
  with Panel3 do
  begin
    for i := 0 to ControlCount - 1 do
    begin
      if Controls[i] is TSpeedButton then
      begin
        TSpeedButton(Controls[i]).Hint := Trim(TEdit(Sender).Name);
      end;
    end;
  end;
end;

procedure TfrmMarkingInspection.Edit2Change(Sender: TObject);
var
  mvalue: Double;
begin
  if Trim(Edit1.Text) <> '' then
  begin
    if TryStrToFloat(Edit1.Text, mvalue) = False then
    begin
      Application.MessageBox('非法数字!', '提示', 0);
      Exit;
    end;
  end
  else
  begin
    Exit;
  end;
  if Trim(Edit2.Text) <> '' then
  begin
    if TryStrToFloat(Edit2.Text, mvalue) = False then
    begin
      Application.MessageBox('非法数字!', '提示', 0);
      Exit;
    end;
  end
  else
  begin
    Exit;
  end;
  CDQty.Text := FloatToStr(StrToFloat(Edit2.Text) - StrToFloat(Edit1.Text));
end;

procedure TfrmMarkingInspection.Edit3Click(Sender: TObject);
begin
  if Trim(Edit3.Text) = '' then
  begin
    Edit3.Text := '√';
    Edit4.Text := '';
  end
  else
  begin
    Edit3.Text := '';
    Edit4.Text := '√';
  end;
end;

procedure TfrmMarkingInspection.Edit4Click(Sender: TObject);
begin
  if Trim(Edit4.Text) = '' then
  begin
    Edit4.Text := '√';
    Edit3.Text := '';
  end
  else
  begin
    Edit4.Text := '';
    Edit3.Text := '√';
  end;
end;

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

procedure TfrmMarkingInspection.MJstr3KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    Key := #0;
    SelectNext(ActiveControl as TWinControl, True, True);
  end;
end;

procedure TfrmMarkingInspection.Button2Click(Sender: TObject);
begin
  RadioGroup1.ItemIndex := -1;
end;

procedure TfrmMarkingInspection.MJGWChange(Sender: TObject);
begin
  QuantityConversion();
end;

procedure TfrmMarkingInspection.ScanClick(Sender: TObject);
var
  i: Integer;
begin
  Panel3.Visible := True;
  with Panel3 do
  begin
    for i := 0 to ControlCount - 1 do
    begin
      if Controls[i] is TSpeedButton then
      begin
        TSpeedButton(Controls[i]).Hint := Trim(TEdit(Sender).Name);
      end;
    end;
  end;
end;

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

procedure TfrmMarkingInspection.MJNWKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    BTPrint.Click;
  end;
end;

procedure TfrmMarkingInspection.FormCreate(Sender: TObject);
begin
  IsCommopen := false;
  IsJsMessage := false;
end;

procedure TfrmMarkingInspection.Edit8Click(Sender: TObject);
begin
  if Trim(Edit8.Text) = '' then
  begin
    Edit8.Text := '√';
    Button5.Visible := true;
  end
  else
  begin
    Edit8.Text := '';
    Button5.Visible := false;
  end;
end;

procedure TfrmMarkingInspection.MJLenChange(Sender: TObject);
begin
  QuantityConversion();

end;

procedure TfrmMarkingInspection.Tv2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  if CDS_MJCD.IsEmpty then
    Exit;

  if Trim(CDS_MJCD.fieldbyname('MCID').AsString) = '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    CDS_MJCD.Delete;
  end;
end;

end.

