unit U_MJManageNewFDJJAP;

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, Math, 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 'JZCRS232D.dll';

function CommClose(sCommName: PAnsiChar): integer; stdcall; external 'JZCRS232D.dll';

type
  TfrmMJManageNewFDJJAP = class(TForm)
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    cxGridPopupMenu1: TcxGridPopupMenu;                   
    DataSource1: TDataSource;
    Order_MJ: TClientDataSet;
    ADOQueryMain: TADOQuery;
    ADOCmd: TADOQuery;
    ADOTmp: TADOQuery;
    RMDB_Main: TRMDBDataSet;
    RM2: TRMGridReport;
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    DataSource2: TDataSource;
    CDS_MJCD: TClientDataSet;
    DataSource3: TDataSource;
    CDS_MJID: TClientDataSet;
    Timer1: TTimer;
    ADOQueryPrint: TADOQuery;
    ToolButton1: TToolButton;
    ScrollBox1: TScrollBox;
    SpeedButton13: TSpeedButton;
    SpeedButton14: TSpeedButton;
    SpeedButton15: TSpeedButton;
    SpeedButton16: TSpeedButton;
    SpeedButton17: TSpeedButton;
    SpeedButton18: TSpeedButton;
    cxGridPopupMenu2: TcxGridPopupMenu;
    ADOQueryMainDSC: TADOQuery;
    Panel1: TPanel;
    BTAdd: TButton;
    BTEdit: TButton;
    Label16: TLabel;
    Panel2: TPanel;
    MJMaoZ: TEdit;
    Label11: TLabel;
    BTPrint: 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;
    Panel4: TPanel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    tv2CDType: TcxGridDBColumn;
    tv2CDWZ: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    Tv2CDQty: TcxGridDBColumn;
    Tv2CDReason: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    v3Column1: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Panel5: TPanel;
    Label27: TLabel;
    APID: TEdit;
    Button2: TButton;
    MJPiZ: TEdit;
    Label30: TLabel;
    www: TLabel;
    MJJingZ: TEdit;
    v3Column3: TcxGridDBColumn;
    Label31: TLabel;
    Label3: TLabel;
    Label6: TLabel;
    ClothType: TComboBox;
    Panel6: TPanel;
    Label32: TLabel;
    LBPiKgQty: TLabel;
    Label35: TLabel;
    CDS_Print: TClientDataSet;
    v2Column3: TcxGridDBColumn;
    JXPerson: TEdit;
    Label41: TLabel;
    GCCode: TEdit;
    Label43: TLabel;
    Label45: TLabel;
    MJKJZ: TEdit;
    Label29: TLabel;
    v3Column4: TcxGridDBColumn;
    APBatchNo: TEdit;
    Label46: TLabel;
    WorkShift: TComboBox;
    Label49: TLabel;
    MovePanel1: TMovePanel;
    Label21: TLabel;
    Label8: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    Label44: TLabel;
    Button1: TButton;
    Button4: TButton;
    CDQty: TEdit;
    CDGS: TEdit;
    CDKgQty: TEdit;
    StaffName: TComboBox;
    CarNo: TEdit;
    Button3: TButton;
    Label2: TLabel;
    QryCheckNet: TADOQuery;
    v3Column5: TcxGridDBColumn;
    lblCodeName: TLabel;
    v3Column6: TcxGridDBColumn;
    Label37: TLabel;
    Label33: TLabel;
    Edit13: TEdit;
    Edit6: TEdit;
    Label1: TLabel;
    ConNo: TEdit;
    Panel7: TPanel;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    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;
    SpeedButton34: TSpeedButton;
    SpeedButton35: TSpeedButton;
    SpeedButton36: TSpeedButton;
    SpeedButton37: TSpeedButton;
    SpeedButton38: TSpeedButton;
    SpeedButton39: TSpeedButton;
    SpeedButton40: TSpeedButton;
    SpeedButton41: TSpeedButton;
    SpeedButton42: TSpeedButton;
    SpeedButton43: TSpeedButton;
    SpeedButton44: TSpeedButton;
    SpeedButton45: TSpeedButton;
    SpeedButton46: TSpeedButton;
    SpeedButton47: TSpeedButton;
    SpeedButton48: TSpeedButton;
    SpeedButton50: TSpeedButton;
    SpeedButton51: TSpeedButton;
    SpeedButton52: TSpeedButton;
    SpeedButton53: TSpeedButton;
    SpeedButton54: TSpeedButton;
    SpeedButton55: TSpeedButton;
    SpeedButton56: TSpeedButton;
    SpeedButton57: TSpeedButton;
    SpeedButton58: TSpeedButton;
    SpeedButton59: TSpeedButton;
    SpeedButton60: TSpeedButton;
    SpeedButton61: TSpeedButton;
    SpeedButton62: TSpeedButton;
    SpeedButton63: TSpeedButton;
    SpeedButton64: TSpeedButton;
    SpeedButton65: TSpeedButton;
    SpeedButton66: TSpeedButton;
    SpeedButton67: TSpeedButton;
    SpeedButton68: TSpeedButton;
    SpeedButton69: TSpeedButton;
    SpeedButton70: TSpeedButton;
    SpeedButton71: TSpeedButton;
    SpeedButton72: TSpeedButton;
    SpeedButton73: TSpeedButton;
    SpeedButton74: TSpeedButton;
    SpeedButton75: TSpeedButton;
    SpeedButton76: TSpeedButton;
    SpeedButton77: TSpeedButton;
    SpeedButton78: TSpeedButton;
    SpeedButton79: TSpeedButton;
    SpeedButton80: TSpeedButton;
    SpeedButton81: TSpeedButton;
    SpeedButton82: TSpeedButton;
    SpeedButton83: TSpeedButton;
    SpeedButton84: TSpeedButton;
    SpeedButton85: TSpeedButton;
    SpeedButton86: TSpeedButton;
    SpeedButton87: TSpeedButton;
    SpeedButton88: TSpeedButton;
    SpeedButton89: TSpeedButton;
    SpeedButton90: TSpeedButton;
    SpeedButton91: TSpeedButton;
    SpeedButton92: TSpeedButton;
    SpeedButton93: TSpeedButton;
    SpeedButton94: TSpeedButton;
    SpeedButton95: TSpeedButton;
    SpeedButton96: TSpeedButton;
    SpeedButton97: TSpeedButton;
    SpeedButton98: TSpeedButton;
    SpeedButton99: TSpeedButton;
    SpeedButton100: TSpeedButton;
    SpeedButton101: TSpeedButton;
    SpeedButton102: TSpeedButton;
    SpeedButton103: TSpeedButton;
    Button8: TButton;
    Button9: TButton;
    Button10: TButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(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 MJFKClick(Sender: TObject);
    procedure MJBanZuClick(Sender: TObject);
    procedure MJSJKZClick(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 BTAddClick(Sender: TObject);
    procedure BTEditClick(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 Button4Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure SpeedButton49Click(Sender: TObject);
    procedure CDQtyClick(Sender: TObject);
    procedure ConNoChange(Sender: TObject);
    procedure APIDKeyPress(Sender: TObject; var Key: Char);
    procedure Button2Click(Sender: TObject);
    procedure Edit13Click(Sender: TObject);
    procedure MJPiZChange(Sender: TObject);
    procedure MJMaoZChange(Sender: TObject);
    procedure Edit6Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Tv3CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure Button10Click(Sender: TObject);
  private
    { Private declarations }
    FInt, PState, PrintInt, SCInitGrid, FState, FCK: Integer;
    CurrentPage: Integer;
    FMaxWet, FMinWet: Double;
    FColumn, FBanZu, FAPID, FMainId, FSubId, FIsOutRange, FJTZB: string;
    MValue, FCDName, DZCDYDllName, MBDYDllName: string;
    procedure InitConNo();
    procedure InitSCPerson();
    procedure InitJP();
    procedure InitCDGrid();
    procedure InitCDGridID();
    procedure AddCD(Fbtn: TButton);
    procedure AddSL(Fbtn: TButton);
    function SaveData(): Boolean;
    procedure BtnStatus(BSInt: Boolean);
    procedure AddSLNew(Fbtn: TSpeedButton);
    procedure AddCDNew(Fbtn: TSpeedButton);
    procedure On1201(var Message: Tmessage); message 1201;
    procedure OpenCom(DllName: string);
    procedure CloseCom(DllName: string);
    procedure InitDataJM();
    procedure PrintData(FFFMJID: string);
    function SelfConnData(): Boolean;
    function CheckData(): Boolean;
    procedure InitCarNo();
    procedure CSHKJ();
  public
    { Public declarations }
  end;

var
  frmMJManageNewFDJJAP: TfrmMJManageNewFDJJAP;
  newh, newh1: hwnd;
  IniFile: TIniFile;

implementation

uses
  U_DataLink, U_RTFun, U_Fun, U_ZDYHelp, U_iniParam, U_SCPerson, U_SCPersonAA,
  U_TSOKNO, U_JTView;

{$R *.dfm}
function TfrmMJManageNewFDJJAP.CheckData(): Boolean;
var
  mvalue: double;
begin
  FIsOutRange := '正常';
  try
    if Trim(FMainId) = '' then
      raise Exception.Create('请扫描布票！');

    if Trim(ConNo.Text) = '' then
      raise Exception.Create('没有扫描！');
    if Trim(CarNo.Text) = '' then
      raise Exception.Create('没有选择机台！');
   //判断重量是否合法
    if Trim(MJMaoZ.Text) <> '' then
    begin
      if TryStrToFloat(MJMaoZ.Text, mvalue) = False then
      begin
        raise Exception.Create('重量录入错误!');
      end
      else
      begin
        if FMaxWet = 0 then
          FMaxWet := 35;
        if FMinWet = 0 then
          FMinWet := 5;

        if strtofloatdef(trim(MJMaoZ.Text), 0) > FMaxWet then
        begin
          FIsOutRange := '重量超范围';
          if Application.MessageBox(PChar('数据有异常,重量大于' + floattostr(FMaxWet) + ',请确认无误!'), '提示', 32 + 4) <> IDYES then
          begin
            raise Exception.Create('重量大于' + floattostr(FMaxWet) + '!');
          end;
        end;

        if strtofloatdef(trim(MJMaoZ.Text), 0) < FMinWet then
        begin
          FIsOutRange := '重量超范围';
          if Application.MessageBox(PChar('数据有异常,重量小于' + floattostr(FMinWet) + ',请确认无误!'), '提示', 32 + 4) <> IDYES then
          begin
            raise Exception.Create('重量小于' + floattostr(FMinWet) + '!');
          end;
        end;

      end;
    end;

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

function TfrmMJManageNewFDJJAP.SelfConnData(): Boolean;
begin
  try
    with QryCheckNet do
    begin
      Close;
      sql.Clear;
      //查询一个小表检测网络
      SQL.Add('select MainType from TbForBatch where 1=2');
      Open;
    end;
    Result := true;
  except
//    if Application.MessageBox('是否重新连接网络！','提示',32+4)<>IDYES then  Exit;
    if not Assigned(DataLink_WeavingSchedule) then
      DataLink_WeavingSchedule := TDataLink_WeavingSchedule.Create(Application);
    try
      with DataLink_WeavingSchedule.ADOLink do
      begin
        //if not Connected then
        begin
          Connected := false;
          ConnectionString := DConString;
          LoginPrompt := false;
          Connected := true;
        end;
      end;
      Result := true;
    except
      Result := false;
    end;
  end;
end;

procedure TfrmMJManageNewFDJJAP.FormDestroy(Sender: TObject);
begin
  frmMJManageNewFDJJAP := nil;
end;

procedure TfrmMJManageNewFDJJAP.On1201(var Message: Tmessage);
var
  i1, i2: integer;
  unitname: string;
  fdata: double;
  FMJKJZ, FMJMaoZ, FMJPiZ, FMJJingZ: Double;
begin
  i1 := Message.WParam;
  i2 := Message.LParam;
                       //RoundFloat(StrToFloat(floattostr(i1 *i2 /100000 )),1);
  if TryStrToFloat(MJPiZ.Text, FMJPiZ) = False then
  begin
    FMJPiZ := 0;
  end;
  if TryStrToFloat(MJKJZ.Text, FMJKJZ) = False then
  begin
    FMJKJZ := 0;
  end;
  FMJMaoZ := RoundFloat(i1 * i2 / 100000, 1);
  FMJJingZ := FMJMaoZ - FMJPiZ;
  MJMaoZ.Text := floattostr(Floor(FMJMaoZ * 10) / 10 + 0.1);
  MJJingZ.Text := FloatToStr(Floor(FMJJingZ * 10) / 10 + 0.1);
  //MJMaoZ.Text:= floattostr(i1 *i2 /100000 );
//  if Trim(MJKJZ.Text)=''then
//  begin
//    MJMaoZ.Text:=FloatToStr(RoundFloat(StrToFloat(floattostr(i1 *i2 /100000 )),1));
//  end
//  else
//  begin
//    MJMaoZ.Text:=FloatToStr(RoundFloat(StrToFloat(floattostr(i1 *i2 /100000 ))+StrToFloat(MJKJZ.Text),1));
//  end;
//
//  if Trim(MJPiZ.Text)='' then
//  begin
//    MJJingZ.Text:=FloatToStr(RoundFloat(StrToFloat(floattostr(i1 *i2 /100000 )),1));
//  end
//  else
//  begin
//    if TryStrToFloat(MJPiZ.Text,fdata) then
//    begin
//      MJJingZ.Text:=FloatToStr(RoundFloat(StrToFloat(floattostr(i1 *i2 /100000 )),1)+StrToFloat(MJPiZ.Text));
//    end
//    else
//    begin
//      MJJingZ.Text:='';
//    end;
//  end;
end;

procedure TfrmMJManageNewFDJJAP.OpenCom(DllName: string);
type
  TMyFunc = function(fhandle: hwnd; sCommName: PAnsiChar; IntTime: integer; IsMessage: integer): HWND; stdcall;
var
  Tf: TMyFunc;
  Tp: TFarProc;
  Th: Thandle;
begin
  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
          FCK := 1;
          showmessage('串口打开失败!');
          Exit;
        end
        else
        begin
          FCK := 9;
        end;
      end
      else
      begin
      end;
    finally
    end;
  end
  else
  begin
    application.MessageBox(Pchar('找不到 ' + trim(DllName) + '串口文件！'), '提示');
  end;
end;

procedure TfrmMJManageNewFDJJAP.CloseCom(DllName: string);
type
  TMyFunc = function(sCommName: PAnsiChar): HWND; stdcall;
var
  Tf1: TMyFunc;
  Tp1: TFarProc;
  Th1: Thandle;
  //newh1:hwnd;
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 TfrmMJManageNewFDJJAP.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if FCK = 9 then
  begin
    CloseCom(DZCDYDllName)
  end;
  Action := caFree;
end;

procedure TfrmMJManageNewFDJJAP.PrintData(FFFMJID: string);
var
  fPrintFile: string;
  i: Integer;
  FDate: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  ExportFtErpFile('坯布标签.rmf', ADOTmp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\坯布标签.rmf';
  if FileExists(fPrintFile) then
  begin
    try
      Moudle := LoadLibrary('MakeQRBarcode.dll');
      @Makebar := GetProcAddress(Moudle, 'Make');
      @Mixtext := GetProcAddress(Moudle, 'MixText');
      Txt := Trim(FFFMJID);
      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;
    with ADOQueryPrint do
    begin
      Close;
      SQL.Clear;
      SQL.Add('exec P_Print_PBLab :MJID');
      Parameters.ParamByName('MJID').Value := Trim(FFFMJID);
      Open;
    end;
    SCreateCDS20(ADOQueryPrint, CDS_Print);
    SInitCDSData20(ADOQueryPrint, CDS_Print);
    RM2.LoadFromFile(fPrintFile);
    RMVariables['QRBARCODE'] := fImagePath;
    //RM2.ShowReport;
    RM2.PrintReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\坯布标签.rmf'), '提示', 0);
    Exit;
  end;
end;

procedure TfrmMJManageNewFDJJAP.InitCDGrid();
begin
  with ADOQueryMain do
  begin
    Close;
    SQL.Clear;
    if PState = 1 then
    begin
      sql.Add('select A.*   from WFB_MJJY_CD_PB A where 1=2 ')
    end
    else
    begin
      sql.Add('select A.*   from WFB_MJJY_CD_PB A where MJID=''' + Trim(CDS_MJID.fieldbyname('MJID').AsString) + '''');
    end;
    Open;
  end;
  SCreateCDS20(ADOQueryMain, CDS_MJCD);
  SInitCDSData20(ADOQueryMain, CDS_MJCD);
end;

procedure TfrmMJManageNewFDJJAP.InitCDGridID();
begin
  with ADOQueryMain do
  begin
    Close;
    SQL.Clear;
    sql.Add('select TOP 20 A.*');
    sql.Add(' ,C_CodeName=(select C_CodeName from Cloth_Sub X where X.SubId=A.SubId)');
    sql.Add(' from WFB_MJJY_PB A where A.Filler=''' + Trim(DName) + ''' ');
    sql.Add(' and convert(varchar(10),A.FillTime,120)=convert(varchar(10),getdate(),120)');
    sql.Add(' order by A.FillTime desc');
    Open;
  end;
  SCreateCDS20(ADOQueryMain, CDS_MJID);
  SInitCDSData20(ADOQueryMain, CDS_MJID);
end;

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

procedure TfrmMJManageNewFDJJAP.FormShow(Sender: TObject);
begin

  try
    IniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'FileKPZ.INI');
    DZCDYDllName := IniFile.ReadString('生产车间配置', '电子秤调用Dll文件', '');
    MBDYDllName := IniFile.ReadString('生产车间配置', '码表调用Dll文件', '');
  finally
    IniFile.Free;
  end;
  CurrentPage := 1;
//  InitJP();
  //InitCarNo();
  InitConNo();
  InitSCPerson();
  BTAdd.Click;
  if DZCDYDllName <> '' then
  begin
    OpenCom(DZCDYDllName);
  end;

end;

procedure TfrmMJManageNewFDJJAP.InitCarNo();
var
  AA: array[0..100] of string;
  i, j: Integer;
  FCriteria, fsj: string;
begin
//  FCriteria := ' where MCClass=' + quotedstr(Trim(FJTZB));
  FCriteria := '';
  with ADOTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from SY_User where userId=''' + Trim(DCode) + '''');
    Open;
  end;
  fsj := Trim(ADOTmp.fieldbyname('UType').AsString);
  with ADOTmp do
  begin
    Close;
    sql.Clear;
    if CurrentPage < 3 then
    begin
      sql.Add('exec P_Page_CarNoSel @pageIndex=' + inttostr(CurrentPage) + ',@pageSize=60  ');
      sql.Add(',@criteria=' + Quotedstr(FCriteria));
    end
    else if CurrentPage = 3 then
    begin
      sql.Add('exec P_Page_CarNoSel201   ');
    end;
    Open;
  end;
  if fsj = '新车间' then
  begin
    with ADOTmp do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Page_CarNoSel_X @pageIndex=' + inttostr(CurrentPage) + ',@pageSize=60  ');
      sql.Add(',@criteria=' + Quotedstr(FCriteria));
      Open;
    end;
  end
  else if fsj = '老车间' then
  begin
    with ADOTmp do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Page_CarNoSel_L @pageIndex=' + inttostr(CurrentPage) + ',@pageSize=60  ');
      sql.Add(',@criteria=' + Quotedstr(FCriteria));
      Open;
    end;
  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('MCNO').AsString);
      i := i + 1;
      Next;
    end;
  end;
  i := i - 1;
//  if i > 63 then
//  begin
//    i := 63;
//  end;
  i := 59;
  for j := 0 to i do
  begin
    with ScrollBox1 do
    begin
      if AA[j] <> '' then
      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
      else
      begin
        TSpeedButton(Controls[j]).Visible := false;
      end;

    end;
  end;
end;

procedure TfrmMJManageNewFDJJAP.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=''WFBCPBZZ'' 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 > 63 then
  begin
    i := 63;
  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;

procedure TfrmMJManageNewFDJJAP.AddCD(Fbtn: TButton);
begin
  if PState < 1 then
    Exit;
  if Order_MJ.IsEmpty then
    Exit;
  with CDS_MJCD do
  begin
    Append;
    FieldByName('cdname').Value := Trim(TButton(Fbtn).Caption);
    Post;
  end;
end;

procedure TfrmMJManageNewFDJJAP.AddCDNew(Fbtn: TSpeedButton);
begin
  with CDS_MJCD do
  begin
    Append;
    FieldByName('cdname').Value := Trim(TSpeedButton(Fbtn).Hint);
    Post;
  end;
end;

procedure TfrmMJManageNewFDJJAP.AddSL(Fbtn: TButton);
begin
  if PState < 1 then
    Exit;
  if Order_MJ.IsEmpty then
    Exit;

  //if MJMaoZ.Focused then
  if FInt = 4 then
    Exit;
  if CDS_MJCD.IsEmpty = False then
  begin
    FColumn := Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName;
    if Trim(FColumn) <> '' then
    begin
      with CDS_MJCD do
      begin
        Edit;
        FieldByName(FColumn).Value := Trim(FieldByName(FColumn).AsString) + Trim(TButton(Fbtn).Caption);
        if Trim(fieldbyname('CDQtyS').AsString) <> '' then
          Fieldbyname('CDQty').Value := Trim(fieldbyname('CDQtyS').AsString);
        Post;
      end;
    end;
  end;
end;

procedure TfrmMJManageNewFDJJAP.AddSLNew(Fbtn: TSpeedButton);
begin
  if PState < 1 then
    Exit;
  if Trim(FAPID) = '' then
    Exit;

  //if MJMaoZ.Focused then
  if FInt = 4 then
    Exit;
  if FInt = 11 then
  begin
    if TSpeedButton(Fbtn).Tag = 9 then
      Exit;
   // Edit1.Text:=Trim(Edit1.Text)+Trim(TSpeedButton(Fbtn).Caption);
    //Edit1.SelectAll;
  end
  else if FInt = 12 then
  begin
    if TSpeedButton(Fbtn).Tag = 9 then
      Exit;
    //Edit2.Text:=Trim(Edit2.Text)+Trim(TSpeedButton(Fbtn).Caption);
    //Edit2.SelectAll;
  end; { else
  if CDS_MJCD.IsEmpty=False then
  begin
    FColumn:=Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName;
    if Trim(FColumn)<>'' then
    begin
      with CDS_MJCD do
      begin
        Edit;
        FieldByName(FColumn).Value:=Trim(FieldByName(FColumn).AsString)+Trim(TButton(Fbtn).Caption);
        if Trim(fieldbyname('CDQtyS').AsString)<>'' then
          Fieldbyname('CDQty').Value:=Trim(fieldbyname('CDQtyS').AsString);
        Post;
      end;
    end;
  end; }
end;

procedure TfrmMJManageNewFDJJAP.Tv2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  //if Tv2.OptionsSelection.CellSelect=True then
  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 TfrmMJManageNewFDJJAP.MJMaoZClick(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 TfrmMJManageNewFDJJAP.MJLenClick(Sender: TObject);
begin
  FInt := 2;
  FColumn := '';
  panel3.Visible := True;
end;

procedure TfrmMJManageNewFDJJAP.MJFKClick(Sender: TObject);
begin
  FInt := 3;
  FColumn := '';
  panel3.Visible := True;
end;

procedure TfrmMJManageNewFDJJAP.MJBanZuClick(Sender: TObject);
begin
  FInt := 4;
end;

procedure TfrmMJManageNewFDJJAP.MJSJKZClick(Sender: TObject);
begin
  FInt := 4;
end;

procedure TfrmMJManageNewFDJJAP.Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  //DataLink_WFBProducttion.ADOLink.Connected:=False;
  FInt := 4;
 // if PState=1 then Exit;
  InitCDGridID();
  //InitCDGrid();
  {with ADOTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from WFB_MJJY where MJID='''+Trim(CDS_MJID.fieldbyname('MJID').AsString)+'''');
    Open;
  end;
  if ADOTmp.IsEmpty then
  begin
    MJMaoZ.Text:='';
    MJLen.Text:='';
    MJFK.Text:='';
    MJSJKZ.Text:='';
  end else
    SSetWinData(ADOTmp,Panel5); }
end;

procedure TfrmMJManageNewFDJJAP.Tv3CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  {if CDS_MJCD.IsEmpty=False then
  begin
    if Trim(CDS_MJCD.fieldbyname('MJID').AsString)='' then
    begin
      Application.MessageBox('数据未保存,不能浏览数据!','提示',0);
      Exit;
    end;
  end;
  PState:=0;
  InitCDGrid();
  with ADOTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from WFB_MJJY_PB where MJID='''+Trim(CDS_MJID.fieldbyname('MJID').AsString)+'''');
    Open;
  end;
  MJLen.Text:=Trim(ADOTmp.fieldbyname('MJLen').AsString);
  MJMaoZ.Text:=Trim(ADOTmp.fieldbyname('MJMaoZ').AsString);
  Label15.Caption:=Trim(CDS_MJID.fieldbyname('MJID').AsString);
  if Trim(ADOTmp.fieldbyname('MJType').AsString)='正品' then
  begin
    Edit3.Text:='√';
    Edit4.Text:='';
    Edit5.Text:='';
  end else
  if Trim(ADOTmp.fieldbyname('MJType').AsString)='次品' then
  begin
    Edit4.Text:='√';
    Edit3.Text:='';
    Edit5.Text:='';
  end else
  if Trim(ADOTmp.fieldbyname('MJType').AsString)='留样' then
  begin
    Edit4.Text:='';
    Edit3.Text:='';
    Edit5.Text:='√';
  end;
  BtnStatus(False);
  Label16.Caption:=''; }
end;

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

procedure TfrmMJManageNewFDJJAP.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 TfrmMJManageNewFDJJAP.SaveData(): Boolean;
var
  maxno, JFMaxNo, FMJID, BZID, str1, maxsubno: string;
  FMJLen, FYJQtyKg, KKMoney: Double;
  FOrder: Integer;
begin
  Result := False;
  if PState < 1 then
    Exit;
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from WFB_MJJY_PB where MJId=''' + Trim(FAPID) + '''');
      Open;
    end;

    with ADOCmd do
    begin
      Append;
      FieldByName('MainId').value := Trim(FMainId);
      FieldByName('SubId').value := Trim(FSubId);
      FieldByName('APId').value := Trim(FAPID);
      FieldByName('MJID').Value := Trim(FAPID);
      FieldByName('MJStr2').Value := '未入库';
      FieldByName('MJStr4').Value := '未出库';
      FieldByName('IsOutRange').Value := Trim(FIsOutRange);
      FieldByName('MJBanZu').Value := Trim(FBanZu);

      if Trim(MJKJZ.Text) <> '' then
      begin
        if Trim(MJMaoZ.Text) <> '' then
        begin
          FieldByName('MJMaoZ').Value := StrToFloat(MJMaoZ.Text) + StrToFloat(MJKJZ.Text);
        end;
        if Trim(MJJingZ.Text) <> '' then
        begin
          FieldByName('MJJingZ').Value := StrToFloat(MJJingZ.Text) + StrToFloat(MJKJZ.Text);
        end;
      end
      else
      begin
        if Trim(MJMaoZ.Text) <> '' then
        begin
          FieldByName('MJMaoZ').Value := StrToFloat(MJMaoZ.Text);
        end;
        if Trim(MJJingZ.Text) <> '' then
        begin
          FieldByName('MJJingZ').Value := StrToFloat(MJJingZ.Text);
        end;
      end;
      if Trim(MJPiZ.Text) <> '' then
      begin
        FieldByName('MJPiZ').Value := StrToFloat(MJPiZ.Text);
      end;

      FieldByName('Filler').Value := Trim(DName);
      FieldByName('ClothType').Value := Trim(clothType.Text);
      FieldByName('WorkShift').Value := Trim(WorkShift.Text);
      FieldByName('JXPerson').Value := Trim(JXPerson.Text);
      FieldByName('APBatchNo').Value := Trim(APBatchNo.Text);
      FieldByName('CarNo').Value := Trim(CarNo.Text);
      if Trim(SCXFlag) <> '' then
      begin
        FieldByName('JTType').Value := Trim(SCXFlag);
      end;

      Post;
    end;
    if strtofloatdef(Trim(MJMaoZ.Text), 0) <= 5 then
    begin
      ADOCmd.Connection.RollbackTrans;
      Application.MessageBox('毛重不能小于5！', '提示', 0);
      Exit;
    end;
    if strtofloatdef(Trim(MJMaoZ.Text), 0) >= 35 then
    begin
      ADOCmd.Connection.RollbackTrans;
      Application.MessageBox('毛重不能大于35！', '提示', 0);
      Exit;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('UPdate WFB_MJJY_PB Set MJXH=(select isnull(max(MJXH),0)+1 from  WFB_MJJY_PB A where A.MainID=WFB_MJJY_PB.MainID)');
      sql.Add(',FOrderNo=''' + Trim(ConNo.Text) + '''');

      sql.Add(',CarNO=''' + Trim(CarNo.Text) + '''');
      sql.Add(' where MJID=''' + Trim(FAPID) + '''');
      ExecSQL;
    end;

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('update WFB_MJJY_PB set ZZPersonCode=X.UserID from SY_User X where X.Udept=''挡车工'' and X.UserName=WFB_MJJY_PB.SCPerson and WFB_MJJY_PB.MJID=''' + Trim(FAPID) + '''');
      SQL.Add('update WFB_MJJY_PB set JBSCPersonCode=X.UserID from SY_User X where X.Udept=''挡车工'' and X.UserName=WFB_MJJY_PB.JBSCPerson and WFB_MJJY_PB.MJID=''' + Trim(FAPID) + '''');
      SQL.Add('update WFB_MJJY_PB set JBSCPerson2Code=X.UserID from SY_User X where X.Udept=''挡车工'' and X.UserName=WFB_MJJY_PB.JBSCPerson2 and WFB_MJJY_PB.MJID=''' + Trim(FAPID) + '''');
      ExecSQL;
    end;

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('Update JYCon_Sub_AnPai Set JYFlag=1 where APID=''' + Trim(FAPID) + '''');
      ExecSQL;
    end;
    
    ///保存母卷疵点表
    with CDS_MJCD do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_MJCD.fieldbyname('MCID').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, maxno, 'MC', 'WFB_MJJY_CD_PB', 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_PB  ');
          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(FAPID);
          FieldByName('MCID').Value := Trim(maxno);
          SSetSaveDataCDSNew(ADOCmd, Tv2, CDS_MJCD, 'WFB_MJJY_CD_PB', 0);
          Post;
        end;
        CDS_MJCD.Edit;
        CDS_MJCD.FieldByName('MCID').Value := Trim(maxno);
        Next;
      end;
    end;
    with CDS_MJID do
    begin
      Append;
      FieldByName('MJID').Value := Trim(FAPID);
      //FieldByName('MJType').Value:=
      Post;
    end;

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('UPdate WFB_MJJY_PB Set ');
      sql.Add(' CDQK=dbo.F_Get_Order_SubStr_PB(WFB_MJJY_PB.MJID,''MJCDHZFM'') ');
      sql.Add(' ,ZZPerson=dbo.F_Get_Order_SubStr_PB(WFB_MJJY_PB.MJID,''ZZPerson'') ');
      sql.Add(',HZYJZP=(select sum(YJZP) from WFB_MJJY_CD_PB A where A.MJID=WFB_MJJY_PB.MJID)');
      sql.Add(',CDGS=(select Count(*) from WFB_MJJY_CD_PB A where A.MJID=WFB_MJJY_PB.MJID)');
      sql.Add(',CDHZQty=(select sum(CDQty) from WFB_MJJY_CD_PB A where A.MJID=WFB_MJJY_PB.MJID)');
      sql.Add(',CDHZKgQty=(select sum(CDKgQty) from WFB_MJJY_CD_PB A where A.MJID=WFB_MJJY_PB.MJID)');
      sql.Add(',CheJian=(select max(MCCJ) from Machine A where A.MCNO=WFB_MJJY_PB.CarNO)');
      sql.Add(',APXH=(select XHInt from JYCon_Sub_AnPai A where A.APID=WFB_MJJY_PB.MJID)');
      sql.Add(' where MJID=''' + Trim(FAPID) + '''');
      ExecSQL;
    end;

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update WFB_MJJY_PB Set ChuLiFlag=1 where MJID=''' + Trim(FAPID) + '''');
      sql.Add(' and not exists(select MJID from WFB_MJJY_CD_PB A where A.MJID=WFB_MJJY_PB.MJID )');
      ExecSQL;
    end;
        //保存出库
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Do_PBJYCRK  @MJID=' + quotedstr(trim(Trim(FAPID))));
      ExecSQL;
    end;

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

procedure TfrmMJManageNewFDJJAP.BTAddClick(Sender: TObject);
begin
  if Trim(FMainId) = '' then
    Exit;
  PState := 1;
  InitCDGridID();
  InitCDGrid();
  BtnStatus(True);
  Label16.Caption := '新增';
  MJMaoZ.Text := '';

  MJJingZ.Text := '';

end;

procedure TfrmMJManageNewFDJJAP.BTEditClick(Sender: TObject);
begin
  if CDS_MJID.IsEmpty then
    Exit;
  PState := 2;
  Label16.Caption := '修改';
end;

procedure TfrmMJManageNewFDJJAP.BTPrintClick(Sender: TObject);
var
  FName1, FName2, FStr: string;
begin
  Edit13.Text := '';
  if SelfConnData() = false then
  begin
    Application.MessageBox('网络链接失败，请重新保存', '提示', 0);
    Exit;
  end;

  if Trim(FMainId) = '' then
  begin
    Application.MessageBox('未扫描无法保存', '提示', 0);
    Exit;
  end;

  if CheckData() = false then
    exit;

  if PState = 0 then
  begin
    if CDS_MJID.IsEmpty = False then
    begin
      Application.MessageBox('已打过条码？请尝试重打。', '提示', 0);
      Exit;
    end;
  end;

  if PState = 1 then
  begin
    FStr := '1';
    with ADOTmp do
    begin
      Close;
      sql.Clear;
      SQL.Add('select Top 1(A.APBatchNo+B.C_CodeName) As AA from WFB_MJJY_PB A inner join Cloth_Sub B on A.SubId=B.SubId where A.CarNo=''' + Trim(CarNo.Text) + '''');
      sql.Add('order by A.FillTime desc ');
      Open;
    end;
    FName1 := Trim(ADOTmp.fieldbyname('AA').AsString);
    with ADOTmp do
    begin
      Close;
      sql.Clear;
      SQL.Add('select Top 1(A.APBatchNo+B.C_CodeName) As AA from JYCon_Sub_AnPai A inner join Cloth_Sub B on A.SubId=B.SubId where A.APID=''' + Trim(Label31.Caption) + '''');
      Open;
    end;
    FName2 := Trim(ADOTmp.fieldbyname('AA').AsString);
    if Trim(FName1) <> Trim(FName2) then
    begin
      FStr := '';
      try
        frmTSOKNO := TfrmTSOKNO.Create(Application);
        with frmTSOKNO do
        begin
          TSStr := '此机台号上一卷产品和本次不一致，' + #13 + #13 + '确定要继续操作吗？';
          OKNOFlag := '选择';
          if ShowModal = 1 then
          begin
            FStr := '1';
          end;
        end;
      finally
        frmTSOKNO.Free;
      end;
      //if Application.MessageBox('此机台号上一卷产品和本次不一致，确定要继续操作吗？','提示',32+4)<>IDYES then Exit;
    end;
    if Trim(FStr) = '1' then
    begin
      if Application.MessageBox('是否要保存？数据保存后将不能修改！', '提示', 32 + 4) <> IDYES then
        Exit;
    end
    else
    begin
      Exit;
    end;

  end;

  if SaveData() then
  begin
    if Trim(Edit6.Text) <> '' then
    begin
      PrintData(Trim(Label31.Caption));
    end;
    Label31.Caption := '';
    lblCodeName.Caption := '';
    ConNo.Text := '';

    CarNo.Text := '';
    LBPiKgQty.Caption := '';

    InitCDGridID();
    APID.SetFocus;
  end;
  BTAdd.Click;
  APID.SetFocus;
end;

procedure TfrmMJManageNewFDJJAP.BtnStatus(BSInt: Boolean);
begin

end;

procedure TfrmMJManageNewFDJJAP.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 TfrmMJManageNewFDJJAP.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 TfrmMJManageNewFDJJAP.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 TfrmMJManageNewFDJJAP.SpeedButton13Click(Sender: TObject);
var
  i: Integer;
begin
  if Label31.Caption = '' then
  begin
    Application.MessageBox('没有扫描!', '提示', 0);
    Exit;
  end;
  if Trim(CDS_MJCD.fieldbyname('MJID').AsString) <> '' then
  begin
    BTAdd.Click;
  end;

  if Trim(FMainId) = '' then
    Exit;
  CarNo.Text := Trim(TSpeedButton(Sender).Hint);
 { FCDName := Trim(TSpeedButton(Sender).Hint);

  MovePanel1.Visible := True;
  Label21.Caption := Trim(FCDName);
  FInt := 11;
 // 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(CDGS.Name);
      end;
    end;
  end;
  CDGS.SetFocus;     }
end;

procedure TfrmMJManageNewFDJJAP.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 TfrmMJManageNewFDJJAP.Button1Click(Sender: TObject);
var
  mvalue: Double;
begin

  if CDQty.Text <> '' then
  begin
    if TryStrToFloat(CDQty.Text, mvalue) = False then
    begin
      Application.MessageBox('疵点长度非法数字!', '提示', 0);
      Exit;
    end;
  end;
  if CDGS.Text <> '' then
  begin
    if TryStrToFloat(CDGS.Text, mvalue) = False then
    begin
      Application.MessageBox('疵点个数非法数字!', '提示', 0);
      Exit;
    end;
    if mvalue = 0 then
    begin
      Application.MessageBox('疵点个数不能为哦0!', '提示', 0);
      Exit;
    end;
  end;

  with CDS_MJCD do
  begin
    Append;
    FieldByName('cdname').Value := Trim(FCDName);
    if Trim(CDQty.Text) <> '' then
    begin
      FieldByName('CDQty').Value := StrToFloat(CDQty.Text);
    end
    else
    begin
      FieldByName('CDQty').Value := 0;
    end;
    if Trim(CDGS.Text) <> '' then
    begin
      FieldByName('CDGS').Value := StrToFloat(CDGS.Text);
    end
    else
    begin
      FieldByName('CDGS').Value := 0;
    end;
    if Trim(CDKgQty.Text) <> '' then
    begin
      FieldByName('CDKgQty').Value := StrToFloat(CDKgQty.Text);
    end
    else
    begin
      FieldByName('CDKgQty').Value := 0;
    end;
    FieldByName('ZZPerson').Value := StaffName.text;
    Post;
  end;
  CDQty.Text := '';
  CDGS.Text := '';
  CDKgQty.Text := '';

  MovePanel1.Visible := False;
  Panel3.Visible := False;
end;

procedure TfrmMJManageNewFDJJAP.Button4Click(Sender: TObject);
begin
  CDQty.Text := '';
  CDGS.Text := '';
  MovePanel1.Visible := False;
  Panel3.Visible := False;
end;

procedure TfrmMJManageNewFDJJAP.ToolButton1Click(Sender: TObject);
begin
//  InitJP();
 // InitCarNo();
  InitConNo();
  InitSCPerson();
  BTAdd.Click;
end;

procedure TfrmMJManageNewFDJJAP.InitConNo();
begin
end;

procedure TfrmMJManageNewFDJJAP.InitSCPerson();
var
  fsj: string;
begin
  fsj := 'select Name=ZdyName from KH_Zdy where Type=''ClothType'' ';
  SInitComBoxBySql(ADOTmp, ClothType, True, fsj);
  ClothType.ItemIndex := 0;

  fsj := 'select Name=ZdyName from KH_Zdy where Type=''WorkShift'' ';
  SInitComBoxBySql(ADOTmp, WorkShift, True, fsj);
  WorkShift.ItemIndex := -1;

end;

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

procedure TfrmMJManageNewFDJJAP.CDQtyClick(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 TfrmMJManageNewFDJJAP.ConNoChange(Sender: TObject);
begin
  with ADOTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Cloth_Main where ConNo=''' + Trim(ConNo.Text) + '''');
    Open;
  end;
  FMainId := Trim(ADOTmp.fieldbyname('MainId').AsString);
  PState := 1;
  InitCDGridID();
  InitCDGrid();

end;

procedure TfrmMJManageNewFDJJAP.InitDataJM();
var
  fsj: string;
begin

  fsj := ' select distinct StaffName as Name  from Worker_WorkDetail where Valid=''Y'' and APID=' + quotedstr(Trim(APID.Text));
  SInitComBoxBySql(ADOTmp, StaffName, False, fsj);
  StaffName.ItemIndex := 0;
  with ADOTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from WFB_MJJY_PB');
    sql.Add('where MJId=''' + Trim(APID.Text) + '''');
    Open;
  end;
  if ADOTmp.IsEmpty = False then
  begin
    APID.Text := '';
    Application.MessageBox('此卷已经检验!', '提示', 0);
    APID.SetFocus;
    Exit;
  end;
  with ADOTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('select B.ConNo,B.GCCode');
    sql.Add(',DCBatchNo=(select top 1  BatchNo   from Worker_WorkDetail X where Valid=''Y'' and X.APID=AA.APID)');
    sql.Add(',DCCarNo=(select top 1  CarNo   from Worker_WorkDetail X where Valid=''Y'' and X.APID=AA.APID)');
    sql.Add(',AA.APID,AA.BuPiaoType,AA.SCPerson,AA.CarNo,AA.MainId,AA.SubId,A.PiZhong,A.PiKgQty,AA.APBatchNo,AA.JXPerson ');
    sql.Add(',A.MaxWet,A.MinWet,A.PiKgQtyFD,A.C_CodeName,A.JTZB ');
    sql.Add(' from JYCon_Sub_AnPai AA');
    sql.Add(' inner join Cloth_Sub A on AA.SubId=A.SubId and AA.MainId=A.MainId ');
    sql.Add(' inner join Cloth_Main B on AA.MainId=B.MainId ');
    sql.Add('where AA.APId=''' + Trim(APID.Text) + '''');
//    ShowMessage(SQL.Text);
    Open;
  end;
  if ADOTmp.IsEmpty = False then
  begin
    FJTZB := Trim(ADOTmp.fieldbyname('JTZB').AsString);
    lblCodeName.Caption := Trim(ADOTmp.fieldbyname('C_CodeName').AsString);
    APBatchNo.Text := Trim(ADOTmp.fieldbyname('APBatchNo').AsString);
    Panel7.Caption := Trim(ADOTmp.fieldbyname('APBatchNo').AsString);
    ConNo.Text := Trim(ADOTmp.fieldbyname('ConNo').AsString);
    GCCode.Text := Trim(ADOTmp.fieldbyname('GCCode').AsString);
    if Trim(ADOTmp.fieldbyname('BuPiaoType').AsString) = '外发加工' then
      CarNo.Text := '外发加工'
    else
      CarNo.Text := Trim(ADOTmp.fieldbyname('DCCarNo').AsString);
    JXPerson.Text := Trim(ADOTmp.fieldbyname('JXPerson').AsString);
    MJKJZ.Text := Trim(ADOTmp.fieldbyname('PiZhong').AsString);
    FAPID := Trim(APID.Text);
    FMainId := Trim(ADOTmp.fieldbyname('MainId').AsString);
    FSubId := Trim(ADOTmp.fieldbyname('SubId').AsString);
    Label31.Caption := FAPID;
    FMaxWet := ADOTmp.fieldbyname('MaxWet').AsFloat;
    FMinWet := ADOTmp.fieldbyname('MinWet').AsFloat;
//    LBPiKgQty.Caption := Trim(ADOTmp.fieldbyname('MinWet').AsString) + '至' + Trim(ADOTmp.fieldbyname('MaxWet').AsString);

    if Trim(ADOTmp.fieldbyname('PiKgQtyFD').AsString) = '' then
    begin
      LBPiKgQty.Caption := Trim(ADOTmp.fieldbyname('PiKgQty').AsString);
    end
    else
    begin
      LBPiKgQty.Caption := Trim(ADOTmp.fieldbyname('PiKgQty').AsString) + '±' + Trim(ADOTmp.fieldbyname('PiKgQtyFD').AsString);
    end;
    ToolButton1.Click;
  end
  else
  begin
    Application.MessageBox('条码错误!', '提示', 0);
    APID.Text := '';
    FAPID := '';
    FMainId := '';
    FSubId := '';
    Label31.Caption := '';
    lblCodeName.Caption := '';
    LBPiKgQty.Caption := '';
    Exit;
  end;
  APID.Text := '';
  BTAdd.Click;
//  APID.SetFocus;
//  MJMaoZ.SetFocus;
end;

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

procedure TfrmMJManageNewFDJJAP.Button2Click(Sender: TObject);
begin
  if Trim(APID.Text) = '' then
    Exit;
  InitDataJM();
end;

procedure TfrmMJManageNewFDJJAP.Edit13Click(Sender: TObject);
begin
  if Trim(Edit13.Text) = '' then
  begin
    Edit13.Text := '√';
    OpenCom(DZCDYDllName);
  end
  else
  begin
    Edit13.Text := '';
    if FCK = 9 then
      CloseCom(DZCDYDllName)
    else
    begin
      Exit;
    end;
  end;
end;

procedure TfrmMJManageNewFDJJAP.MJPiZChange(Sender: TObject);
var
  FMJKJZ, FMJMaoZ, FMJPiZ, FMJJingZ: Double;
begin
  if TryStrToFloat(MJMaoZ.Text, FMJMaoZ) then
  begin
    if TryStrToFloat(MJPiZ.Text, FMJPiZ) = False then
    begin
      FMJPiZ := 0;
    end;
    FMJJingZ := FMJMaoZ - FMJPiZ;
    MJJingZ.Text := FloatToStr(FMJJingZ);
  end;
end;

procedure TfrmMJManageNewFDJJAP.MJMaoZChange(Sender: TObject);
var
  FMJKJZ, FMJMaoZ, FMJPiZ, FMJJingZ: Double;
begin
  if TryStrToFloat(MJMaoZ.Text, FMJMaoZ) then
  begin
    if TryStrToFloat(MJPiZ.Text, FMJPiZ) = False then
    begin
      FMJPiZ := 0;
    end;
    if TryStrToFloat(MJKJZ.Text, FMJKJZ) = False then
    begin
      FMJKJZ := 0;
    end;
    FMJJingZ := FMJMaoZ - FMJPiZ;
    MJJingZ.Text := FloatToStr(FMJJingZ);
  end;
end;

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

procedure TfrmMJManageNewFDJJAP.CSHKJ();
var
  j: Integer;
begin
  for j := 0 to 89 do
  begin
    with ScrollBox1 do
    begin
      TSpeedButton(Controls[j]).Visible := False;
      TSpeedButton(Controls[j]).Hint := '';
      TSpeedButton(Controls[j]).Caption := '';
    end;
  end;

end;

procedure TfrmMJManageNewFDJJAP.Button3Click(Sender: TObject);
var
  j: Integer;
begin
  CSHKJ();
  for j := 0 to 85 do
  begin
    with ScrollBox1 do
    begin
      TSpeedButton(Controls[j]).Visible := True;
      if j < 9 then
        TSpeedButton(Controls[j]).Hint := '00' + inttostr(j + 1)
      else
        TSpeedButton(Controls[j]).Hint := '0' + inttostr(j + 1);
      TSpeedButton(Controls[j]).Caption := TSpeedButton(Controls[j]).Hint;
    end;
  end;
  for j := 86 to 89 do
  begin
    with ScrollBox1 do
    begin
      TSpeedButton(Controls[j]).Visible := True;
      if j < 13 then
      begin
        TSpeedButton(Controls[j]).Hint := '0' + inttostr(j + 35);
      end
      else
      begin
        TSpeedButton(Controls[j]).Hint := inttostr(j + 35);
      end;

      TSpeedButton(Controls[j]).Caption := TSpeedButton(Controls[j]).Hint;

    end;
  end;
end;

procedure TfrmMJManageNewFDJJAP.Tv3CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
//  if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('IsOutRange').Index] <> '正常' then
//    ACanvas.Brush.Color := $B5E4FF;
end;

procedure TfrmMJManageNewFDJJAP.Button5Click(Sender: TObject);
var
  j: Integer;
begin
  CSHKJ();
  for j := 0 to 25 do
  begin
    with ScrollBox1 do
    begin
      TSpeedButton(Controls[j]).Visible := True;
      if j < 13 then
      begin
        TSpeedButton(Controls[j]).Hint := '0' + inttostr(j + 87);
      end
      else
      begin
        TSpeedButton(Controls[j]).Hint := inttostr(j + 87);
      end;

      TSpeedButton(Controls[j]).Caption := TSpeedButton(Controls[j]).Hint;

    end;
  end;

end;

procedure TfrmMJManageNewFDJJAP.Button6Click(Sender: TObject);
var
  j: Integer;
begin
  CSHKJ();
  for j := 0 to 73 do
  begin
    with ScrollBox1 do
    begin
      TSpeedButton(Controls[j]).Visible := True;
      TSpeedButton(Controls[j]).Hint := inttostr(j + 201);
      TSpeedButton(Controls[j]).Caption := inttostr(j + 201);
    end;
  end;

end;

procedure TfrmMJManageNewFDJJAP.Button7Click(Sender: TObject);
var
  j: Integer;
begin
  CSHKJ();
  for j := 0 to 27 do
  begin
    with ScrollBox1 do
    begin
      TSpeedButton(Controls[j]).Visible := True;
      TSpeedButton(Controls[j]).Hint := inttostr(j + 301);
      TSpeedButton(Controls[j]).Caption := inttostr(j + 301);
    end;
  end;

end;

procedure TfrmMJManageNewFDJJAP.Button8Click(Sender: TObject);
var
  j: Integer;
begin
  CSHKJ();
  for j := 0 to 38 do
  begin
    with ScrollBox1 do
    begin
      TSpeedButton(Controls[j]).Visible := True;
      TSpeedButton(Controls[j]).Hint := inttostr(j + 401);
      TSpeedButton(Controls[j]).Caption := inttostr(j + 401);
    end;
  end;

end;

procedure TfrmMJManageNewFDJJAP.Button9Click(Sender: TObject);
var
  j: Integer;
begin
  CSHKJ();
  for j := 0 to 11 do
  begin                                      
    with ScrollBox1 do                  
    begin
      TSpeedButton(Controls[j]).Visible := True;
      TSpeedButton(Controls[j]).Hint := inttostr(j + 275);
      TSpeedButton(Controls[j]).Caption := inttostr(j + 275);             
    end;
  end;

                                            
end;                           

procedure TfrmMJManageNewFDJJAP.Button10Click(Sender: TObject);
var
  j: Integer;
begin
  CSHKJ();
  for j := 0 to 40 do
  begin
    with ScrollBox1 do               
    begin
      TSpeedButton(Controls[j]).Visible := True;
      TSpeedButton(Controls[j]).Hint := inttostr(j + 501);
      TSpeedButton(Controls[j]).Caption := inttostr(j + 501);
    end;
  end;

end;

end.

