unit U_TableInspection;

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, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxSkinsCore,
  dxSkinBlack, dxSkinBlue, dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee,
  dxSkinDarkRoom, dxSkinDarkSide, dxSkinDevExpressDarkStyle,
  dxSkinDevExpressStyle, dxSkinFoggy, dxSkinGlassOceans, dxSkinHighContrast,
  dxSkiniMaginary, dxSkinLilian, dxSkinLiquidSky, dxSkinLondonLiquidSky,
  dxSkinMcSkin, dxSkinMetropolis, dxSkinMetropolisDark, dxSkinMoneyTwins,
  dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
  dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinOffice2010Black,
  dxSkinOffice2010Blue, dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinOffice2013White, dxSkinPumpkin, dxSkinSeven,
  dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus, dxSkinSilver,
  dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008, dxSkinTheAsphaltWorld,
  dxSkinsDefaultPainters, dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint,
  dxSkinXmas2008Blue, dxSkinscxPCPainter;
{function CommOpen(fhandle:hwnd;sCommName:PAnsiChar;
                    IntTime:integer):integer;stdcall;external 'JCYData.DLL';
function CommClose(sCommName:PAnsiChar):integer;stdcall;external 'JCYData.DLL';
  function CommOpen(fhandle:hwnd;sCommName:PAnsiChar;
                    IntTime:integer;IsMessage:integer):integer;stdcall;external 'JCYData.DLL';
  function CommClose(sCommName:PAnsiChar):integer;stdcall;external 'JCYData.DLL';
  function CommOpen(fhandle:hwnd;sCommName:PAnsiChar;
                    IntTime:integer;IsMessage:integer):integer;stdcall;external 'JZCRS323C.DLL';
  function CommClose(sCommName:PAnsiChar):integer;stdcall;external 'JZCRS323C.DLL';

  function GetCommData(fhandle:hwnd;var defnum1,defnum2:double;var fisjz:integer):integer;stdcall;external 'JZCRS323C.DLL';

  function CommOpen1301(fhandle:hwnd;sCommName:PAnsiChar;
                    IntTime:integer;IsMessage:integer):integer;stdcall;external 'JZCRS323C.DLL';
  function CommClose1301(sCommName:PAnsiChar):integer;stdcall;external 'JZCRS323C.DLL';

  function GetCommData1301(fhandle:hwnd;var defnum1,defnum2:double;var fisjz:integer):integer;stdcall;external 'JZCRS323C.DLL';  }

type
  TfrmTableInspection = class(TForm)
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    cxGridPopupMenu1: TcxGridPopupMenu;
    DataSource1: TDataSource;
    Order_MJ: TClientDataSet;
    ADOQueryMain: TADOQuery;
    ADOCmd: TADOQuery;
    RMDB_Main: TRMDBDataSet;
    RM2: TRMGridReport;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    DataSource2: TDataSource;
    CDS_MJCD: TClientDataSet;
    DataSource3: TDataSource;
    CDS_MJID: TClientDataSet;
    Timer1: TTimer;
    ADOQueryPrint: TADOQuery;
    ScrollBox1: TScrollBox;
    SpeedButton13: TSpeedButton;
    cxGridPopupMenu2: TcxGridPopupMenu;
    ADOQueryMainDSC: TADOQuery;
    Panel2: TPanel;
    Label9: TLabel;
    MJLen: TEdit;
    Label4: TLabel;
    MJMaoZ: TEdit;
    Label5: TLabel;
    Label11: TLabel;
    BTPrint: TButton;
    Button5: TButton;
    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;
    v3Column1: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    v3Column3: TcxGridDBColumn;
    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;
    v3Column4: TcxGridDBColumn;
    v3Column5: TcxGridDBColumn;
    RM3: TRMGridReport;
    ADOTmp: TADOQuery;
    Panel5: TPanel;
    Panel6: TPanel;
    Label1: TLabel;
    lblOrderNo: TLabel;
    Label35: TLabel;
    Label39: TLabel;
    APID: TEdit;
    Edit7: TEdit;
    Edit8: TEdit;
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    TBCD: TToolButton;
    TBClose: TToolButton;
    lblMPRTCodeName: TLabel;
    lblcolor: TLabel;
    SpeedButton14: TSpeedButton;
    SpeedButton15: TSpeedButton;
    SpeedButton16: TSpeedButton;
    SpeedButton17: TSpeedButton;
    SpeedButton18: TSpeedButton;
    SpeedButton19: TSpeedButton;
    SpeedButton20: TSpeedButton;
    SpeedButton21: TSpeedButton;
    SpeedButton22: TSpeedButton;
    SpeedButton23: TSpeedButton;
    SpeedButton24: TSpeedButton;
    SpeedButton25: TSpeedButton;
    SpeedButton26: TSpeedButton;
    SpeedButton27: TSpeedButton;
    SpeedButton28: TSpeedButton;
    SpeedButton29: TSpeedButton;
    SpeedButton30: TSpeedButton;
    SpeedButton31: TSpeedButton;
    SpeedButton32: TSpeedButton;
    SpeedButton33: TSpeedButton;
    lblrcgangNo: TLabel;
    lblPiQty: TLabel;
    lblPBFactoryName: TLabel;
    lblFactoryName: TLabel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    tv2CDType: TcxGridDBColumn;
    tv2CDWZ: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    Tv2CDQty: TcxGridDBColumn;
    Tv2CDReason: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label21: TLabel;
    Label2: TLabel;
    JYY1: TEdit;
    SpeedButton34: TSpeedButton;
    SpeedButton35: TSpeedButton;
    SpeedButton36: TSpeedButton;
    Button3: TButton;
    Button6: TButton;
    Label3: TLabel;
    edtDuanMa: TEdit;
    Label6: TLabel;
    edtPinPi: TEdit;
    SpeedButton37: TSpeedButton;
    SpeedButton38: TSpeedButton;
    SpeedButton39: TSpeedButton;
    Button7: TButton;
    Button8: TButton;
    CDS_WXTS: TClientDataSet;
    Panel1: TPanel;
    SpeedButton40: TSpeedButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCDClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure MJMaoZClick(Sender: TObject);
    procedure MJLenClick(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure Tv3CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure FormClick(Sender: TObject);
    procedure Button12Click(Sender: TObject);
    procedure BTPrintClick(Sender: TObject);
    procedure Tv2CDQtyPropertiesEditValueChanged(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton12Click(Sender: TObject);
    procedure SpeedButton13Click(Sender: TObject);
    procedure cxGridDBColumn2PropertiesChange(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure SpeedButton49Click(Sender: TObject);
    procedure APIDKeyPress(Sender: TObject; var Key: Char);
    procedure Edit1Click(Sender: TObject);
    procedure Edit2Change(Sender: TObject);
    procedure MJstr3KeyPress(Sender: TObject; var Key: Char);
    procedure Label9Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure APIDClick(Sender: TObject);
    procedure Edit7Click(Sender: TObject);
    procedure MJQty4KeyPress(Sender: TObject; var Key: Char);
    procedure FormCreate(Sender: TObject);
    procedure MJQty2Change(Sender: TObject);
    procedure Edit8Click(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure edtDuanMaClick(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
    FInt, PState, PrintInt, SCInitGrid, FState, FCK: Integer;
    FColumn, FAPID, FMainId, FSubId, FFFMJID, FYXJL: string;
    MValue, FCDName: string;
    isCommopen, IsJsMessage: boolean;
    fxsType, fRollType, FOrderNo, FGangNo: string;
    procedure InitJP();
    procedure InitCDGrid();
    procedure InitCDGridID();
    function SaveData(MMJType: string): Boolean;
    procedure InitLCK();  //加载流程卡
    procedure ClearGlobal();  //清除全局ID
    function CheckData(CKType: string): Boolean;
    procedure PrintCKMDG(MRKOrdID: string);
    procedure PrtData(FMJID: string);
    procedure PrtDataG(FSPID: string);
    procedure OpenCom(DllName: string);
    procedure CloseCom(DllName: string);
    procedure On1201(var Message: Tmessage); message 1201;
    procedure On1301(var Message: Tmessage); message 1301;
    procedure SendMessage(Mfsid, MFactory, MorderNo, key2str: string);
  public
    fmanage: string;
    { Public declarations }

  end;

var
  frmTableInspection: TfrmTableInspection;
  newh, newh1: hwnd;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_iniParam, U_ProductOrderListSel;

{$R *.dfm}

procedure TfrmTableInspection.ClearGlobal();
begin
  FAPID := '';
  FMainId := '';
  FSubId := '';
  lblOrderNo.Caption := '订单号';
  lblMPRTCodeName.Caption := '品名';
  lblcolor.Caption := '颜色';
  lblrcgangNo.Caption := '缸号';
  lblPBFactoryName.Caption := '坯布厂';
  lblFactoryName.Caption := '染厂';
  lblPiQty.Caption := '匹数';
  APID.SetFocus;
end;

function TfrmTableInspection.CheckData(CKType: string): Boolean;
var
  mvalue: Double;
begin
  try
    if Trim(FAPID) = '' then
      raise Exception.Create('请扫描流程卡！');
    if Trim(FMainId) = '' then
      raise Exception.Create('请扫描流程卡！');
    if Trim(FSubId) = '' then
      raise Exception.Create('请扫描流程卡！');

    if Trim(CKType) = '次品' then
    begin
      if CDS_MJCD.IsEmpty then
        raise Exception.Create('请输入疵点!');
    end;

    if ((Trim(CKType) = '次品') or (Trim(CKType) = '零头布') or (Trim(CKType) = '剪样品')) then
    begin
      if trim(JYY1.Text) = '' then
        raise Exception.Create('请输入桌号!');

      if trim(Label9.Caption) = '' then
        raise Exception.Create('长度单位不能为空!');

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

        if Length(trim(MJMaoZ.Text)) <> 4 then
          raise Exception.Create('重量格式错误 XX.X!');
        if pos('.', trim(MJMaoZ.Text)) <> 3 then
          raise Exception.Create('重量格式错误 XX.X!');

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

        if strtofloatdef(trim(MJMaoZ.Text), 0) < 0 then
          raise Exception.Create('重量不能小于0!');
      end;
      if Trim(MJLen.Text) <> '' then
      begin
        if TryStrToFloat(MJLen.Text, mvalue) = False then
          raise Exception.Create('长度录入错误!');

        if Length(trim(MJLen.Text)) <> 5 then
          raise Exception.Create('长度格式错误 XXX.X!');

        if pos('.', trim(MJLen.Text)) <> 4 then
          raise Exception.Create('长度格式错误 XXX.X!');

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

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

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

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

procedure TfrmTableInspection.PrintCKMDG(MRKOrdID: string);
begin

end;

procedure TfrmTableInspection.InitLCK();
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 WFB_MJJY X where X.APID=A.SPID),0)');
    sql.Add('from CK_SXPB_CR A');
    sql.Add('inner join JYOrder_Main B on A.ORDMainIdRK=B.MainId');
    sql.Add('inner join JYOrder_Sub C on A.ORDSubIdRK=C.SubId');
    sql.Add('where SPID=''' + Trim(APID.Text) + '''');
    Open;
  end;
  if ADOTmp.FieldByName('DJLockFlag').AsBoolean = True then
  begin
    Application.MessageBox('此卡已锁定，禁止操作!', '提示', 0);
    APID.Text := '';

    Exit;
  end;
  FOrderNo := Trim(ADOTmp.fieldbyname('ConNo').AsString);
  FGangNo := Trim(ADOTmp.fieldbyname('RCGangNo').AsString);
  if ADOTmp.IsEmpty = False then
  begin
    FAPID := Trim(APID.Text);
    APID.Text := '';
    FMainId := Trim(ADOTmp.fieldbyname('ORDMainIdRK').AsString);
    FSubId := Trim(ADOTmp.fieldbyname('ORDSubIdRK').AsString);
    FYXJL := Trim(ADOTmp.fieldbyname('YXJL').AsString);

    lblOrderNo.Caption := '订单号：' + Trim(ADOTmp.fieldbyname('conno').AsString);
    lblMPRTCodeName.Caption := '品名：' + Trim(ADOTmp.fieldbyname('MPRTCodeName').AsString);
    lblcolor.Caption := '颜色：' + trim(ADOTmp.fieldbyname('PRTColor').AsString);
    lblrcgangNo.Caption := '缸号：' + Trim(ADOTmp.fieldbyname('rcgangNo').AsString);
    lblPBFactoryName.Caption := '坯布厂：' + Trim(ADOTmp.fieldbyname('PBFactoryName').AsString);
    lblFactoryName.Caption := '染厂：' + Trim(ADOTmp.fieldbyname('FactoryName').AsString);
    lblPiQty.Caption := '匹数：' + Trim(ADOTmp.fieldbyname('PiQty').AsString);
    if Trim(ADOTmp.fieldbyname('OrderUnit').AsString) <> '' then
    begin
      Label20.Caption := Label9.Caption;
      Label7.Caption := Label9.Caption;
    end;

    if FYXJL = '米数' then
    begin
      MJLen.SetFocus;
      Edit1Click(MJLen);
    end
    else
    begin
      MJMaoZ.SetFocus;
      Edit1Click(MJMaoZ);
    end;
    Timer1.Enabled := True;
  end
  else
  begin
    ClearGlobal();
  end;
  InitCDGridID();
  InitCDGrid();
end;

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

procedure TfrmTableInspection.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 TfrmTableInspection.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
      MJMaoZ.Text := format('%.1f', [i1 / 100000]);
    end;
  end;
end;

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

  if isCommopen then
  begin
    closeCom(DllName);
    closeCom(JCYDLL);
  end;
  Action := caFree;
end;

procedure TfrmTableInspection.InitCDGrid();
begin
  with ADOQueryMain do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from WFB_MJJY_CD where MJID='''' ');
    Open;
  end;
  SCreateCDS20(ADOQueryMain, CDS_MJCD);
  SInitCDSData20(ADOQueryMain, CDS_MJCD);
end;

procedure TfrmTableInspection.InitCDGridID();
begin
  with ADOQueryMain do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.*');
    sql.Add('from WFB_MJJY A where APID=''' + Trim(FAPID) + '''');
    if Trim(SCXFlag) <> '' then
      sql.Add(' and JTType=''' + Trim(SCXFlag) + '''');
    sql.Add(' and Filler=''' + Trim(DName) + '''');
    sql.Add(' and MJstr2=''未入库'' ');
    Open;
  end;
  SCreateCDS20(ADOQueryMain, CDS_MJID);
  SInitCDSData20(ADOQueryMain, CDS_MJID);
end;

procedure TfrmTableInspection.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.InitJP();
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTableInspection.FormShow(Sender: TObject);
begin
  InitJP();

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

  if trim(fmanage) <> '高权限' then
  begin
//        SHOWMESSAGE(dllName);
//         SHOWMESSAGE(JCYDLL);
    if trim(DllName) <> '' then
      OpenCom(DllName);
    if trim(JCYDLL) <> '' then
      OpenCom(JCYDLL);
    Edit7.Visible := true;
    Label35.Visible := true;
  end;
end;

procedure TfrmTableInspection.InitJP();
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 > 24 then
//  begin
//    i := 24;
//  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;

  SpeedButton13.Caption := '勾丝';
  SpeedButton14.Caption := '断氨' + #13 + '纶';
  SpeedButton15.Caption := '破洞';
  SpeedButton16.Caption := '坯布风' + #13 + '格不好';
  SpeedButton17.Caption := '邹条';
  SpeedButton18.Caption := '风格' + #13 + '不对';
  SpeedButton19.Caption := '粗细' + #13 + '纱';
  SpeedButton20.Caption := '断经' + #13 + '/断纬';
  SpeedButton21.Caption := '边中' + #13 + '差';
  SpeedButton22.Caption := '页差';
  SpeedButton23.Caption := '门幅' + #13 + '问题';
  SpeedButton24.Caption := '色花';
  SpeedButton25.Caption := '擦伤';
  SpeedButton26.Caption := '定型' + #13 + '不好';
  SpeedButton27.Caption := '手感' + #13 + '不好';
  SpeedButton28.Caption := '脏污';
  SpeedButton29.Caption := '克重' + #13 + '问题';         
  SpeedButton30.Caption := '颜料' + #13 + '点';
  SpeedButton31.Caption := '中央' + #13 + '痕';
  SpeedButton32.Caption := '颜色' + #13 + '不对';
  SpeedButton33.Caption := '反面' + #13 + '横档';
  SpeedButton34.Caption := '异纤';
  SpeedButton35.Caption := '横档';
  SpeedButton36.Caption := '针路';
  SpeedButton37.Caption := '静电';
  SpeedButton38.Caption := '霉点';
  SpeedButton39.Caption := '老油' + #13 + '丝';
  SpeedButton40.Caption := '零头' + #13 + '布';

  SpeedButton13.hint := '勾丝';
  SpeedButton14.hint := '断氨纶';
  SpeedButton15.hint := '破洞';
  SpeedButton16.hint := '坯布风格不好';
  SpeedButton17.hint := '邹条';
  SpeedButton18.hint := '风格不对';
  SpeedButton19.hint := '粗细纱';
  SpeedButton20.hint := '断经/断纬';
  SpeedButton21.hint := '边中差';
  SpeedButton22.hint := '页差';
  SpeedButton23.hint := '门幅问题';
  SpeedButton24.hint := '色花';
  SpeedButton25.hint := '擦伤';
  SpeedButton26.hint := '定型不好';
  SpeedButton27.hint := '手感不好';
  SpeedButton28.hint := '脏污';
  SpeedButton29.hint := '克重问题';
  SpeedButton30.hint := '颜料点';
  SpeedButton31.hint := '中央痕';
  SpeedButton32.hint := '颜色不对';
  SpeedButton33.hint := '反面横档';                          
  SpeedButton34.hint := '异纤';                            
  SpeedButton35.hint := '横档';
  SpeedButton36.hint := '针路';
  SpeedButton37.hint := '静电';
  SpeedButton38.hint := '霉点';
  SpeedButton39.hint := '老油丝'; //其他待定
  SpeedButton40.hint := '零头布';
end;

procedure TfrmTableInspection.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;

procedure TfrmTableInspection.MJMaoZClick(Sender: TObject);
begin
  FInt := 1;
  FColumn := '';
  panel3.Visible := True;
end;

procedure TfrmTableInspection.MJLenClick(Sender: TObject);
begin
  FInt := 2;
  FColumn := '';
  panel3.Visible := True;
end;

procedure TfrmTableInspection.Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  InitCDGridID();
end;

procedure TfrmTableInspection.Tv3CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  exit;
  if CDS_MJCD.IsEmpty = False then
  begin
    if Trim(CDS_MJCD.fieldbyname('MJID').AsString) = '' then
    begin
      Application.MessageBox('数据未保存,不能浏览数据!', '提示', 0);
      Exit;
    end;
  end;

  InitCDGrid();
  with ADOTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from WFB_MJJY where MJID=''' + Trim(CDS_MJID.fieldbyname('MJID').AsString) + '''');
    Open;
  end;

  MJMaoZ.Text := Trim(ADOTmp.fieldbyname('MJMaoZ').AsString);
  MJLen.Text := Trim(ADOTmp.fieldbyname('MJLen').AsString);

end;

procedure TfrmTableInspection.FormClick(Sender: TObject);
begin
  FInt := 4;
end;

procedure TfrmTableInspection.Button12Click(Sender: TObject);
var
  fsj: string;
begin
  if PState < 1 then
    Exit;
  if Trim(FAPID) = '' then
    Exit;
  if FInt = 4 then
    Exit;
  begin
    FColumn := Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName;
    if Trim(FColumn) <> '' then
    begin
      if Trim(CDS_MJCD.FieldByName(FColumn).AsString) <> '' then
      begin
        with CDS_MJCD do
        begin
          Edit;
          if Length(CDS_MJCD.FieldByName(FColumn).AsString) = 1 then
          begin
            FieldByName(FColumn).Value := null;
            FieldByName('CDQty').Value := 0;
          end
          else
          begin
            FieldByName(FColumn).Value := Copy(Trim(FieldByName(FColumn).AsString), 1, Length(Trim(FieldByName(FColumn).AsString)) - 1);
            FieldByName('CDQty').Value := FieldByName(FColumn).Value;
          end;
          Post;
        end;
      end;

    end;
  end;
end;

function TfrmTableInspection.SaveData(MMJType: string): Boolean;
var
  maxno, FMJID, BZID, strsql: string;
  FMJLen: Double;
  FOrder: Integer;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from WFB_MJJY where 1=2');
      Open;
    end;
    with ADOCmd do
    begin
      Append;
      if GetLSNo(ADOTmp, maxno, Trim(SCXFlag), 'WFB_MJJY', 4, 1) = False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('生成流水号异常！', '提示', 0);
        exit;
      end;
      FieldByName('MainId').value := Trim(FMainId);
      FieldByName('SubId').value := Trim(FSubId);
      FieldByName('FromMainId').value := Trim(FMainId);
      FieldByName('FromSubId').value := Trim(FSubId);

      if Trim(edtDuanMa.Text) <> '' then
        FieldByName('DuanMa').value := '染厂短码';
      if Trim(edtPinPi.Text) <> '' then
        FieldByName('PinPi').value := '有拼匹';

      FieldByName('APID').value := Trim(FAPID);
      FieldByName('MJID').Value := Trim(maxno);
      FieldByName('MJStr2').Value := '未入库';
      FieldByName('MJType').Value := MMJType;
      FieldByName('MJXH').Value := 0;
      FieldByName('MJTypeOther').Value := Trim(Label9.Caption);
      FieldByName('JYY1').Value := Trim(JYY1.Text) + '号桌';
      FieldByName('MJLen').Value := StrToFloatdef(trim(MJLen.Text), 0);
      //毛重
      FieldByName('MJMaoZ').Value := StrToFloatdef(trim(MJMaoZ.Text), 0);
       //净重
      FieldByName('MJQty4').Value := StrToFloatdef(trim(MJMaoZ.Text), 0);

      if Trim(FMJID) = '' then
      begin
        FieldByName('Filler').Value := Trim(DName);
      end
      else
      begin
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOTmp);
      end;
      if Trim(SCXFlag) <> '' then
      begin
        FieldByName('JTType').Value := Trim(SCXFlag);
      end;
      FieldByName('MJTypeOther').Value := Trim(Label9.Caption);
      Post;
    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', 'WFB_MJJY_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 WFB_MJJY_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, 'WFB_MJJY_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 WFB_MJJY SET CDList= dbo.F_Get_Order_SubStr(MJID,''MJCDHZSL'')');
      sql.Add('where MJID=''' + Trim(FMJID) + ''' ');
      execsql;
    end;
    //出入库及库存
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Do_JYCRK @MJID=''' + trim(FMJID) + '''');
      execsql;
    end;
    //更新开卡
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_UPdate_LCKJY @APID=''' + trim(FAPID) + '''');
      execsql;
    end;
    ADOCmd.Connection.CommitTrans;
    Result := True;
    PState := 0;
  except
    Result := False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
end;

procedure TfrmTableInspection.BTPrintClick(Sender: TObject);
var
  fPrintFile: string;
  mvalue: Double;
  i: Integer;
begin
  APID.SetFocus;
  Timer1.Enabled := False;
  if CheckData('次品') = False then
    exit;
  if SaveData('次品') then
  begin
    MJLen.Text := '';
    MJMaoZ.Text := '';
    JYY1.Text := '';
    edtDuanMa.Text := '';
    edtPinPi.Text := '';
  end;
  PrtData(FFFMJID);
  InitCDGridID();
  InitCDGrid();

  if FYXJL = '米数' then
  begin
    MJLen.SetFocus;
    Edit1Click(MJLen);
  end
  else
  begin
    MJMaoZ.SetFocus;
    Edit1Click(MJMaoZ);
  end;
end;

procedure TfrmTableInspection.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_JYLab ');
    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('次品标签.rmf', ADOTmp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\次品标签.rmf';

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

procedure TfrmTableInspection.PrtDataG(FSPID: string);
var
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.*,B.*,C.*,PBFactoryName1=A.PBFactoryName ');
    sql.Add('from CK_SXPB_CR A');
    sql.Add('inner join JYOrder_Main B on A.ORDMainIdRK=B.MainId');
    sql.Add('inner join JYOrder_Sub C on A.ORDSubIdRK=C.SubId');
    sql.Add('where SPID=''' + Trim(FAPID) + '''');
    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(FAPID);
    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 := 2;
    RM2.PrintReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

procedure TfrmTableInspection.Tv2CDQtyPropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  try
    mvalue := TcxTextEdit(Sender).EditingText;
    if Trim(mvalue) <> '' then
    begin
      CDS_MJCD.Edit;
      CDS_MJCD.FieldByName('CDQty').Value := mvalue;
      CDS_MJCD.Post;
    end
    else
    begin
      CDS_MJCD.Edit;
      CDS_MJCD.FieldByName('CDQty').Value := 0;
      CDS_MJCD.Post;
    end;
    CDS_MJCD.Edit;
    CDS_MJCD.FieldByName('CDQtyS').Value := mvalue;
  except
    Application.MessageBox('非法数字！', '提示', 0);
  end;
end;

procedure TfrmTableInspection.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 TfrmTableInspection.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 TfrmTableInspection.SpeedButton13Click(Sender: TObject);
var
  i: Integer;
begin

  if CheckData('疵点') = False then
    exit;
  Timer1.Enabled := False;
  Timer1.Enabled := True;
  //显示疵点录入界面
  FCDName := Trim(TSpeedButton(Sender).Hint);
//  MovePanel1.Visible := True;
  Label21.Caption := Trim(FCDName);
  Button1.Click;
  //显示数字键盘
//  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 TfrmTableInspection.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 TfrmTableInspection.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);
    Post;
  end;
  Edit1.Text := '';
  Edit2.Text := '';
  CDQty.Text := '';
  MovePanel1.Visible := False;
  Panel3.Visible := False;
end;

procedure TfrmTableInspection.ToolButton1Click(Sender: TObject);
begin
  InitJP();
end;

procedure TfrmTableInspection.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 TfrmTableInspection.SpeedButton49Click(Sender: TObject);
begin
  Panel3.Visible := False;
end;

procedure TfrmTableInspection.APIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    InitLCK();
  end;
end;

procedure TfrmTableInspection.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 TfrmTableInspection.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 TfrmTableInspection.MJstr3KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    Key := #0;
    SelectNext(ActiveControl as TWinControl, True, True);
  end;
end;

procedure TfrmTableInspection.Label9Click(Sender: TObject);
begin
  if trim(Label9.Caption) = 'M' then
  begin
    Label9.Caption := 'Y';
    Label20.Caption := 'Y';
    Label7.Caption := 'Y';
  end
  else
  begin
    Label9.Caption := 'M';
    Label20.Caption := 'M';
    Label7.Caption := 'M';
  end;
end;

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

procedure TfrmTableInspection.APIDClick(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 TfrmTableInspection.Edit7Click(Sender: TObject);
begin
  if Trim(Edit7.Text) = '' then
  begin
    Edit7.Text := '√';
  end
  else
  begin
    Edit7.Text := '';
  end;
end;

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

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

procedure TfrmTableInspection.MJQty2Change(Sender: TObject);
var
  FReal: double;
begin
//  MJLen.Text:=format('%.1f',[StrToFloatdef(trim(MJLen.Text),0)- StrToFloatdef(trim(MJQty2.Text),0)]);
 { IF trim(MKXS.Text)<>'' then
  begin
    if TryStrToFloat(MKXS.Text,FReal)=true then
    begin
    //  MJLen.Text:=format('%.1f',[StrToFloatdef(trim(MJQty4.Text),0)* StrToFloatdef(trim(MKXS.Text),0)]);
      MJLen.Text:=format('%.1f',[StrToFloatdef(trim(MJLen.Text),0)- StrToFloatdef(trim(MJQty2.Text),0)]);
    end;
  end
  else
  begin
    if Trim(MJSJKZ.Text)='' then Exit;
    if TryStrToFloat(MJSJKZ.Text,FReal)=False then
    begin
    Application.MessageBox('克重录入错误!','提示',0);
    Exit;
    end;
    if Trim(MJFK.Text)='' then Exit;
    if TryStrToFloat(MJFK.Text,FReal)=False then
    begin
    Application.MessageBox('幅宽录入错误!','提示',0);
    Exit;
    end;
    if StrToFloat(MJSJKZ.Text)*StrToFloat(MJFK.Text)=0 then Exit;
   if Trim(Label9.Caption)='M' then
   begin
    //MJLen.Text:=FloatToStr(StrToFloat(MJQty4.Text)*1000*100/StrToFloat(MJSJKZ.Text)/StrToFloat(MJFK.Text));
    MJLen.Text:=format('%.1f',[StrToFloat(MJQty4.Text)*1000*100/StrToFloat(MJSJKZ.Text)/StrToFloat(MJFK.Text)]);
     MJLen.Text:=format('%.1f',[StrToFloatdef(trim(MJLen.Text),0)- StrToFloatdef(trim(MJQty2.Text),0)]);
   end else
   begin
     MJLen.Text:=format('%.1f',[StrToFloat(MJQty4.Text)*1000*100/StrToFloat(MJSJKZ.Text)/StrToFloat(MJFK.Text)*1.0936]);
      MJLen.Text:=format('%.1f',[StrToFloatdef(trim(MJLen.Text),0)- StrToFloatdef(trim(MJQty2.Text),0)]);
    //MJLen.Text:=FloatToStr(StrToFloat(MJQty4.Text)*1000*100/StrToFloat(MJSJKZ.Text)/StrToFloat(MJFK.Text)*1.0936);
  //  MJLen.Text:=floatToStr(Round(StrToFloat(MJQty4.Text)*1000*100/StrToFloat(MJSJKZ.Text)/StrToFloat(MJFK.Text)*1.0936));
   end;    
  end;  }
end;

procedure TfrmTableInspection.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 TfrmTableInspection.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmTableInspection.Button3Click(Sender: TObject);
var
  fPrintFile: string;
  mvalue: Double;
  i: Integer;
begin
  APID.SetFocus;
  Timer1.Enabled := False;
  if CheckData('零头布') = False then
    exit;
  if SaveData('零头布') then
  begin
    MJLen.Text := '';
    MJMaoZ.Text := '';
    JYY1.Text := '';
    edtDuanMa.Text := '';
    edtPinPi.Text := '';
  end;
  PrtData(FFFMJID);
  InitCDGridID();
  InitCDGrid();

  if FYXJL = '米数' then
  begin
    MJLen.SetFocus;
    Edit1Click(MJLen);
  end
  else
  begin
    MJMaoZ.SetFocus;
    Edit1Click(MJMaoZ);
  end;
end;

procedure TfrmTableInspection.Button6Click(Sender: TObject);
var
  fPrintFile: string;
  mvalue: Double;
  i: Integer;
begin
  APID.SetFocus;
  Timer1.Enabled := False;
  if CheckData('剪样品') = False then
    exit;
  if SaveData('剪样品') then
  begin
    MJLen.Text := '';
    MJMaoZ.Text := '';
    JYY1.Text := '';
    edtDuanMa.Text := '';
    edtPinPi.Text := '';
  end;
  PrtData(FFFMJID);
  InitCDGridID();
  InitCDGrid();

  if FYXJL = '米数' then
  begin
    MJLen.SetFocus;
    Edit1Click(MJLen);
  end
  else
  begin
    MJMaoZ.SetFocus;
    Edit1Click(MJMaoZ);
  end;
end;

procedure TfrmTableInspection.edtDuanMaClick(Sender: TObject);
begin
  if Trim(TEdit(Sender).Text) = '' then
  begin
    TEdit(Sender).Text := '√';
  end
  else
  begin
    TEdit(Sender).Text := '';
  end;
end;

procedure TfrmTableInspection.Button7Click(Sender: TObject);
var
  strsql: string;
  FCDHZ: string;
begin
  if Trim(FAPID) = '' then
    Exit;
  if CDS_MJCD.IsEmpty then
  begin
    Application.MessageBox('没有选择疵点，不能操作!', '提示', 0);
    Exit;
  end;
  Timer1.Enabled := False;
  with ADOTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_SXPB_CR where SPID=''' + Trim(FAPID) + ''' and isnull(CiPinNote,'''')=''' + Trim(TButton(Sender).Caption) + '''');
    Open;
  end;
  if ADOTmp.IsEmpty = False then
  begin
    Application.MessageBox('已执行过此操作，不允许再次操作！', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要将整缸布设为次品吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  Panel1.Visible := True;
  Panel1.Refresh;
  strsql := '流程卡号：' + FAPID;
  TButton(Sender).Enabled := False;
  with CDS_MJCD do
  begin
    First;
    while not Eof do
    begin
      if Trim(FCDHZ) = '' then
      begin
        FCDHZ := Trim(fieldbyname('CDName').AsString);
      end
      else
      begin
        FCDHZ := FCDHZ + ',' + Trim(fieldbyname('CDName').AsString);
      end;
      Next;
    end;
  end;
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('update CK_SXPB_CR set SPType=''次品'',CiPinNote=''' + Trim(TButton(Sender).Caption) + ''',GangCD=''' + FCDHZ + ''' where SPID=' + quotedstr(Trim(FAPID)));
    sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
    sql.Add(' ' + quotedstr(trim(DName)));
    sql.Add(',getdate() ');
    sql.Add(',' + quotedstr(trim(self.Caption)));
    sql.Add(',' + quotedstr(trim(TButton(Sender).Caption)));
    sql.Add(',' + quotedstr(trim(strsql)));
    sql.Add(',' + quotedstr(trim('成功')));
    sql.Add(')');
    execsql;
  end;
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    SQL.Add('Update JYorder_Sub Set CPNoteS1=Null,CPNoteS2=Null where Subid=''' + Trim(FSubId) + '''');
    SQL.Add('Update JYorder_Main Set CPNoteM1=Null,CPNoteM2=Null where Mainid=''' + Trim(FMainId) + '''');
    ExecSQL;
  end;
  if Trim(TButton(Sender).Caption) = '整缸次品,不能发货' then
  begin

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('Update JYorder_Sub Set CPNoteS1=dbo.F_Get_Order_SubStr(''' + Trim(FSubId) + ''',''CPNoteS1'')');
      sql.Add(' where subid=''' + Trim(FSubId) + '''');
      ExecSQL;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('Update JYorder_Main Set CPNoteM1=dbo.F_Get_Order_SubStr(''' + Trim(FMainId) + ''',''CPNoteM1'')');
      sql.Add(' where Mainid=''' + Trim(FMainId) + '''');
      ExecSQL;
    end;
  end
  else if Trim(TButton(Sender).Caption) = '整缸布大部分很差，将就发货' then
  begin
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('Update JYorder_Sub Set CPNoteS2=dbo.F_Get_Order_SubStr(''' + Trim(FSubId) + ''',''CPNoteS2'')');
      sql.Add(' where subid=''' + Trim(FSubId) + '''');
      ExecSQL;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('Update JYorder_Main Set CPNoteM2=dbo.F_Get_Order_SubStr(''' + Trim(FMainId) + ''',''CPNoteM2'')');
      sql.Add(' where Mainid=''' + Trim(FMainId) + '''');
      ExecSQL;
    end;
  end;
  SendMessage(FAPID, Trim(lblFactoryName.Caption), Trim(FOrderNo), trim(TButton(Sender).Caption) + ' 疵点：' + FCDHZ);
  PrtDataG(FAPID);
  TButton(Sender).Enabled := True;
  Panel1.Visible := False;
end;

procedure TfrmTableInspection.SendMessage(Mfsid, MFactory, MorderNo, key2str: string);
begin
  with ADOTmp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' exec P_View_ZJCPTS  ' + quotedstr(Trim(Mfsid)));
//    ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS20(ADOTmp, CDS_WXTS);
  SInitCDSData20(ADOTmp, CDS_WXTS);

  CDS_WXTS.First;
  while not CDS_WXTS.eof do
  begin
    with ADOCmd do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from WeixinMessage where 1<>1');
      Open;
      Append;
      FieldByName('MessageType').Value := 'OrderProgress';
      FieldByName('MessageName').Value := '整缸布很差';
      FieldByName('Title').Value := '您的订单整缸布很差'; //first data
      //FieldByName('URL').Value := 'pages/grayfabric-repairOut/gdConfirm?spid=' + Trim(Mfsid);
      FieldByName('OpenId').Value := Trim(CDS_WXTS.FieldByName('wxid').AsString);
      FieldByName('Status').Value := '0';
      FieldByName('CreateDate').Value := FormatDateTime('yyyymmdd', SGetServerDateTime(ADOTmp));
      FieldByName('CreateTime').Value := FormatDateTime('HH:mm:ss', SGetServerDateTime(ADOTmp));
      FieldByName('BusinessId').Value := Trim(Mfsid); //
      FieldByName('KeyWord1').Value := Trim(MorderNo);   // 订单号
      FieldByName('KeyWord2').Value := Trim(key2str); //
      FieldByName('remark').Value := Trim(MFactory + ',缸号：' + FGangNo); //remark
      Post;
    end;
    CDS_WXTS.Next;
  end;

end;

procedure TfrmTableInspection.Timer1Timer(Sender: TObject);
begin
  Close;
end;

end.

