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;
{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;
    SpeedButton21: TSpeedButton;
    SpeedButton22: TSpeedButton;
    SpeedButton23: TSpeedButton;
    SpeedButton24: TSpeedButton;
    SpeedButton25: TSpeedButton;
    SpeedButton26: TSpeedButton;
    SpeedButton29: TSpeedButton;
    SpeedButton30: TSpeedButton;
    SpeedButton31: TSpeedButton;
    SpeedButton32: TSpeedButton;
    SpeedButton33: TSpeedButton;
    SpeedButton34: TSpeedButton;
    SpeedButton37: TSpeedButton;
    SpeedButton38: TSpeedButton;
    SpeedButton39: TSpeedButton;
    SpeedButton40: TSpeedButton;
    SpeedButton41: TSpeedButton;
    SpeedButton42: TSpeedButton;
    cxStyleRepository2: TcxStyleRepository;
    cxStyle3: TcxStyle;
    cxStyleRepository3: TcxStyleRepository;
    cxStyle4: TcxStyle;
    cxStyleRepository4: TcxStyleRepository;
    cxStyle5: TcxStyle;
    cxStyleRepository5: TcxStyleRepository;
    cxStyle6: TcxStyle;
    cxGridPopupMenu2: TcxGridPopupMenu;
    ADOQueryMainDSC: TADOQuery;
    SpeedButton45: TSpeedButton;
    SpeedButton46: TSpeedButton;
    SpeedButton47: TSpeedButton;
    SpeedButton48: TSpeedButton;
    Panel1: TPanel;
    BTAdd: TButton;
    BTEdit: TButton;
    Label16: TLabel;
    Panel2: TPanel;
    Label4: TLabel;
    MJMaoZ: TEdit;
    Label11: TLabel;
    BTPrint: TButton;
    Edit3: TEdit;
    Label10: TLabel;
    Edit4: TEdit;
    Label12: TLabel;
    Label15: TLabel;
    Panel3: TPanel;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    SpeedButton5: TSpeedButton;
    SpeedButton6: TSpeedButton;
    SpeedButton7: TSpeedButton;
    SpeedButton8: TSpeedButton;
    SpeedButton9: TSpeedButton;
    SpeedButton10: TSpeedButton;
    SpeedButton11: TSpeedButton;
    SpeedButton12: TSpeedButton;
    SpeedButton49: TSpeedButton;
    Label28: TLabel;
    Edit5: TEdit;
    Label36: TLabel;
    Edit12: TEdit;
    Label37: TLabel;
    Edit13: TEdit;
    Label1: TLabel;
    Label13: TLabel;
    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;
    Label22: TLabel;
    Label24: TLabel;
    JBZhuanQty: TEdit;
    Label25: TLabel;
    Edit7: TEdit;
    Panel5: TPanel;
    Label27: TLabel;
    APID: TEdit;
    Button2: TButton;
    ConNo: TEdit;
    CarNo: TEdit;
    Label26: TLabel;
    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;
    JBSCPerson: TEdit;
    Button6: TButton;
    Label5: TLabel;
    JBSCPerson2: TEdit;
    Button7: TButton;
    Label33: TLabel;
    Edit6: TEdit;
    Label9: TLabel;
    JBZhuanQty2: TEdit;
    Label14: TLabel;
    JBStr: TEdit;
    Label19: TLabel;
    houdang: TEdit;
    Button8: TButton;
    YJQtyKg: TEdit;
    YJQtyM: TEdit;
    Label23: TLabel;
    Label34: TLabel;
    Label41: TLabel;
    GCCode: TEdit;
    Label42: TLabel;
    Label43: TLabel;
    Label45: TLabel;
    MJKJZ: TEdit;
    Label29: TLabel;
    v3Column4: TcxGridDBColumn;
    APBatchNo: TEdit;
    Label46: TLabel;
    Label20: TLabel;
    v2Column4: TcxGridDBColumn;
    Label47: TLabel;
    WzZhuanQty: TEdit;
    Label2: TLabel;
    SCPerson: TEdit;
    Button3: TButton;
    Label48: TLabel;
    Edit1: TEdit;
    WorkShift: TComboBox;
    Label49: TLabel;
    QryCheckNet: TADOQuery;
    MovePanel1: TMovePanel;
    Label21: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    Label44: TLabel;
    Button1: TButton;
    Button4: TButton;
    CDQty: TEdit;
    CDGS: TEdit;
    CDKgQty: TEdit;
    ZZPerson: TEdit;
    Button5: TButton;
    chkIsYj: TCheckBox;
    v2Column5: TcxGridDBColumn;
    lbl1: TLabel;
    YJZP: TEdit;
    Panel7: TPanel;
    Label50: TLabel;
    LabQty: TLabel;
    Label51: TLabel;
    LabQQty: TLabel;
    Label52: TLabel;
    LabChouZhen: TLabel;
    lblJYPS: TLabel;
    Panel8: TPanel;
    Label53: TLabel;
    Label54: TLabel;
    edtSCAPID: TEdit;
    Label55: TLabel;
    Label56: TLabel;
    lbl2: TLabel;
    Button9: TButton;
    ADOQuery3: TADOQuery;
    Button10: TButton;
    Panel9: TPanel;
    Tv3Column1: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure Tv2CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    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 YJQtyKgClick(Sender: TObject);
    procedure Edit2Change(Sender: TObject);
    procedure Edit3Click(Sender: TObject);
    procedure Edit4Click(Sender: TObject);
    procedure Edit5Click(Sender: TObject);
    procedure Label9DblClick(Sender: TObject);
    procedure CDQtyClick(Sender: TObject);
    procedure Edit10Click(Sender: TObject);
    procedure Edit12Click(Sender: TObject);
    procedure ConNoChange(Sender: TObject);
    procedure JBStrClick(Sender: TObject);
    procedure Edit7Click(Sender: TObject);
    procedure APIDClick(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 Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure MJKJZChange(Sender: TObject);
    procedure Edit1Click(Sender: TObject);
    procedure ZZPersonKeyPress(Sender: TObject; var Key: Char);
    procedure MJMaoZKeyPress(Sender: TObject; var Key: Char);
    procedure SCPersonKeyPress(Sender: TObject; var Key: Char);
    procedure edtSCAPIDKeyPress(Sender: TObject; var Key: Char);
    procedure Button9Click(Sender: TObject);
    procedure Button10Click(Sender: TObject);
    procedure Panel9Click(Sender: TObject);
    procedure Tv3CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
  private
    { Private declarations }
    FInt, PState, PrintInt, SCInitGrid, FState, FCK: Integer;
    FColumn, FBanZu, FAPID, FMainId, FSubId: string;
    MValue, FCDName, DZCDYDllName, MBDYDllName: string;
    FPiKgQty: Double;
    Flogo: 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;
  public
    { Public declarations }
  end;

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

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_iniParam, U_SCPerson, U_SCPersonAA;

{$R *.dfm}
function TfrmMJManageNewFDJJAP.CheckData(): Boolean;
var
  mvalue: double;
begin
  try
    if Trim(FMainId) = '' then
      raise Exception.Create('请扫描布票！');
//    if Flogo <> 'GY' then
//    begin
//      if Trim(SCPerson.Text) = '' then
//        raise Exception.Create('没有选择挡车工！');
//    end;
    if Length(Trim(SCPerson.Text)) > 10 then
    begin
      raise Exception.Create('请查看挡车工是否正确！');
    end;
    if Trim(ConNo.Text) = '' then
      raise Exception.Create('没有扫描！');
    if Trim(CarNo.Text) = '' then
      raise Exception.Create('没有选择机台！');
    if TryStrToFloat(MJMaoZ.Text, mvalue) = False then
      raise Exception.Create('请输入正确的毛重！');
    if strtofloatdef(Trim(MJMaoZ.Text), 0) < 0.1 then
      raise Exception.Create('毛重不能小于0.1！');
    if strtofloatdef(Trim(MJMaoZ.Text), 0) > 60 then
      raise Exception.Create('毛重不能大于60！');

    if strtofloatdef(Trim(MJMaoZ.Text), 0) + strtofloatdef(Trim(MJKJZ.Text), 0) - FPiKgQty >= 0.399 then
    begin
      Panel9.Visible := true;
      Panel9.Caption := '请注意，这匹布超过匹重0.4千克！';
    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);
//  if FMJMaoZ < 0 then
//  begin
//    Application.MessageBox('生成流水号异常！', '提示', 0);
//    Exit;
//  end;
  FMJJingZ := FMJMaoZ - FMJPiZ;
  MJMaoZ.Text := floattostr(FMJMaoZ);
  MJJingZ.Text := FloatToStr(FMJJingZ);
  //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;
 // newh:HWND;   CDS_MJID
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
  //   FreeLibrary(Th);
    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
  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
      sql.Add('select * from WFB_MJJY_CD_PB where 1=2 ')
    else
      sql.Add('select * from WFB_MJJY_CD_PB where MJID=''' + Trim(CDS_MJID.fieldbyname('MJID').AsString) + '''');
    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 JYPS=count(MJID) from WFB_MJJY_PB where Filler=''' + Trim(DName) + ''' ');
    sql.Add(' and convert(varchar(10),DATEADD(MI,-450,FillTime) ,120)=convert(varchar(10),DATEADD(MI,-450,GETDATE()),120)');
    Open;
  end;

  lblJYPS.Caption := '日检验:' + trim(ADOQueryMain.fieldbyname('JYPS').AsString);

  with ADOQueryMain do
  begin
    Close;
    SQL.Clear;
    sql.Add('select JYPS=count(MJID) from WFB_MJJY_PB where Filler=''' + Trim(DName) + ''' ');
    sql.Add(' and convert(varchar(7),FillTime,120)=convert(varchar(7),GETDATE(),120)');
    Open;
  end;

  lblJYPS.Caption := lblJYPS.Caption + '; 月检验:' + trim(ADOQueryMain.fieldbyname('JYPS').AsString);

  with ADOQueryMain do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.*  ');
    sql.Add(',DuoYu=A.MJMaoZ-isnull((select X.PiKGQty from Cloth_Sub X where X.MainID=A.MainID),0)');
    sql.Add('from WFB_MJJY_PB A where MainId=''' + Trim(FMainId) + ''' ');
    sql.Add(' and convert(varchar(10),A.FillTime,120)=convert(varchar(10),getdate(),120)  order by A.Filltime desc ');
    Open;
  end;
  SCreateCDS20(ADOQueryMain, CDS_MJID);
  SInitCDSData20(ADOQueryMain, CDS_MJID);

end;

procedure TfrmMJManageNewFDJJAP.TBCloseClick(Sender: TObject);
var
  maxno: string;
begin
  if CDS_MJCD.IsEmpty = False then
  begin
    if Trim(CDS_MJCD.FieldByName('MCID').AsString) = '' then
    begin
      //try
        //ADOCmd.Connection.BeginTrans;
        {with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete from WFB_MJJY_CD_Temp where JTType='''+Trim(SCXFlag)+'''');
          Execsql;
        end;
        with CDS_MJCD do
        begin
          First;
          while not Eof do
          begin
            if GetLSNo(ADOCmd,maxno,'LS','WFB_MJJY_CD_Temp',2,1)=False then
            begin
             // ADOCmd.Connection.RollbackTrans;
              Application.MessageBox('取疵点临时最大号失败！','提示',0);
              Exit;
            end;
            with ADOCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('select * from WFB_MJJY_CD_Temp where 1<>1');
              Open;
            end;
            with ADOCmd do
            begin
              Append;
              FieldByName('MCID').Value:=Trim(maxno);
              SSetSaveDataCDSNew(ADOCmd,Tv2,CDS_MJCD,'WFB_MJJY_CD_Temp',0);
              FieldByName('JTType').Value:=Trim(SCXFlag);
              Post;
            end;
            Next;
          end;
        end; }
      close;
        //ADOCmd.Connection.CommitTrans;
        //ModalResult:=1;
      //except
        //ADOCmd.Connection.RollbackTrans;
        //Application.MessageBox('保存临时表失败！','提示',0);
      //end;
    end
    else
    begin
      Close;
    end;
  end
  else
  begin
    Close;
  end;

end;

procedure TfrmMJManageNewFDJJAP.Tv2CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
var
  fsj: string;
begin
  //FInt:=0;
  //Tv1.DataController.FocusedRecordIndex;
  //fsj:=Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName;
end;

procedure TfrmMJManageNewFDJJAP.FormShow(Sender: TObject);
begin
  with ADOTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('select FormPara1 from SY_ModuleSub where ModuleID=''10'' and 	ModuleSubID=''02'' ');

    Open;
  end;
  Flogo := Trim(ADOTmp.FieldByName('FormPara1').AsString);
//  Flogo := 'GY';
//  Edit6.Text := '√';
  MJMaoZ.Font.Color := clRed;

  try
    IniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'FileKPZ.INI');
    DZCDYDllName := IniFile.ReadString('生产车间配置', '电子秤调用Dll文件', '');
    MBDYDllName := IniFile.ReadString('生产车间配置', '码表调用Dll文件', '');
  finally
    IniFile.Free;
  end;

  InitJP();
  InitConNo();
  InitSCPerson();
  BTAdd.Click;
  OpenCom(DZCDYDllName);
//  if Trim(DCode)='ADMIN' then APID.ReadOnly:=False;
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);
begin
  FInt := 1;
  FColumn := '';
  panel3.Visible := True;
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('MJBanZu').Value := Trim(FBanZu);
///////////////////////////保存重量/////////////////////////////


      if Trim(MJKJZ.Text) <> '' then
      begin
        if Trim(MJMaoZ.Text) <> '' then
        begin
          if Flogo = 'GY' then
            FieldByName('MJMaoZ').Value := Ceil((StrToFloat(MJMaoZ.Text) + StrToFloat(MJKJZ.Text)) * 10) / 10
          else
            FieldByName('MJMaoZ').Value := StrToFloat(MJMaoZ.Text) + StrToFloat(MJKJZ.Text);
        end;
        if Trim(MJJingZ.Text) <> '' then
        begin
          if Flogo = 'GY' then
            FieldByName('MJJingZ').Value := Ceil((StrToFloat(MJJingZ.Text) + StrToFloat(MJKJZ.Text)) * 10) / 10
          else
            FieldByName('MJJingZ').Value := StrToFloat(MJJingZ.Text) + StrToFloat(MJKJZ.Text);
        end;
      end
      else
      begin
        if Trim(MJMaoZ.Text) <> '' then
        begin
          if Flogo = 'GY' then
            FieldByName('MJMaoZ').Value := Ceil((StrToFloat(MJMaoZ.Text)) * 10) / 10
          else
            FieldByName('MJMaoZ').Value := StrToFloat(MJMaoZ.Text);
        end;
        if Trim(MJJingZ.Text) <> '' then
        begin
          if Flogo = 'GY' then
            FieldByName('MJJingZ').Value := Ceil((StrToFloat(MJJingZ.Text)) * 10) / 10
          else
            FieldByName('MJJingZ').Value := StrToFloat(MJJingZ.Text);
        end;
      end;
///////////////////////////保存重量/////////////////////////////

      if Trim(MJPiZ.Text) <> '' then
      begin
        FieldByName('MJPiZ').Value := StrToFloat(MJPiZ.Text);
      end;

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

      if Trim(Edit12.Text) <> '' then
      begin
        FieldByName('MJStr3').Value := '深色';
      end;
      if Trim(Edit7.Text) <> '' then
      begin
        FieldByName('MJStr3').Value := '漂白';
      end;
      if Trim(Edit1.Text) <> '' then
      begin
        FieldByName('MJStr3').Value := '浅色';
      end;
      FieldByName('MJTypeOther').Value := Trim(Label9.Caption);
      FieldByName('Filler').Value := Trim(DName);
      FieldByName('ClothType').Value := Trim(clothType.Text);
      FieldByName('WorkShift').Value := Trim(WorkShift.Text);
      FieldByName('JXPerson').Value := Trim(JXPerson.Text);
      FieldByName('houdang').Value := Trim(houdang.Text);

      FieldByName('SCPerson').Value := Trim(SCPerson.Text);
      if Trim(JBSCPerson.Text) <> '' then
      begin
        FieldByName('JBSCPerson').Value := Trim(JBSCPerson.Text);
      end;

      if Trim(SCXFlag) <> '' then
      begin
        FieldByName('JTType').Value := Trim(SCXFlag);
      end;

      if Trim(JBZhuanQty.Text) <> '' then
      begin
        FieldByName('JBZhuanQty').Value := Trim(JBZhuanQty.Text);
      end;
      if Trim(JBSCPerson2.Text) <> '' then
      begin
        FieldByName('JBSCPerson2').Value := Trim(JBSCPerson2.Text);
      end;
      if Trim(JBZhuanQty2.Text) <> '' then
      begin
        FieldByName('JBZhuanQty2').Value := Trim(JBZhuanQty2.Text);
      end;
      if Trim(WzZhuanQty.Text) <> '' then
      begin
        FieldByName('WzZhuanQty').Value := Trim(WzZhuanQty.Text);
      end;

      Post;
    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(', SCPerson=''' + Trim(SCPerson.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.Note  from KH_Zdy X where X.Type=''SCPerson'' and X.ZDYName =WFB_MJJY_PB.SCPerson and WFB_MJJY_PB.MJID=''' + Trim(FAPID) + '''');
      SQL.Add('update WFB_MJJY_PB set JBSCPersonCode=X.Note  from KH_Zdy X where X.Type=''SCPerson'' and X.ZDYName =WFB_MJJY_PB.JBSCPerson 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(',APBatchNo=(select APBatchNo 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 := '新增';
  //MJLen.Text:='';
  MJMaoZ.Text := '';
  //ConNo.Text:='';
  //SCPerson.Text:='';
  //CarNo.Text:='';
  JBStr.Text := '';
  JBSCPerson.Text := '';
  JBZhuanQty.Text := '';
  JBZhuanQty2.Text := '';
  JBSCPerson2.Text := '';
  //MJPiZ.Text:='';
  MJJingZ.Text := '';
  YJQtyKg.Text := '';
  YJQtyM.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
  fPrintFile, FCarNo, FOrderNo, FJBZhuanQty, FJBZhuanQty2, FLBRQ, FLBXS, FLBFZ: string;
  mvalue: Double;
  FLBDate: TDateTime;
begin

  if SelfConnData() = false then
  begin
    Application.MessageBox('网络链接失败，请重新保存', '提示', 0);
    Exit;
  end;

  if Trim(FMainId) = '' then
    Exit;
  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
//    if Application.MessageBox('是否要保存？数据保存后将不能修改！', '提示', 32 + 4) <> IDYES then
//      Exit;
//  end;

  if SaveData() then
  begin
    if Trim(Edit6.Text) <> '' then
    begin
      PrintData(Trim(Label31.Caption));
    end;
    Label31.Caption := '';
    ConNo.Text := '';
    SCPerson.Text := '';
    JBSCPerson.Text := '';
    WzZhuanQty.Text := '';
    CarNo.Text := '';
    LBPiKgQty.Caption := '';
    Label55.Caption := '客户';

    InitCDGridID();
    APID.SetFocus;
  end;
  BTAdd.Click;
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(Label15.Caption) <> '' then
  begin
    //MJLen.Text:='';
    MJMaoZ.Text := '';
    Label15.Caption := '';
  end;
  if Trim(FMainId) = '' then
    Exit;
  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;
//  if CDGS.Text='' then
//  begin
//    if TryStrToFloat(CDGS.Text,mvalue)=False then
//    begin
//      Application.MessageBox('疵点个数不能为空!','提示',0);
//      Exit;
//    end;
//  end;
//  if Trim(ZZPerson.Text) = '' then
//  begin
//    Application.MessageBox('责任人不能为空!', '提示', 0);
//    Exit;
//  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;

    if Trim(YJZP.Text) <> '' then
    begin
      FieldByName('YJZP').Value := StrToFloat(YJZP.Text);
    end
    else
    begin
      FieldByName('YJZP').Value := 0;
    end;

    FieldByName('ZZPerson').Value := Trim(ZZPerson.Text);
    FieldByName('IsYj').Value := chkIsYj.Checked;
    Post;
  end;
  CDQty.Text := '';
  CDGS.Text := '';
  CDKgQty.Text := '';
  YJZP.Text := '';
  chkIsYj.Checked := false;
  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();
  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 := -1;

  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.YJQtyKgClick(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.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 TfrmMJManageNewFDJJAP.Edit3Click(Sender: TObject);
begin
 { if Trim(Edit3.Text)='' then
  begin
    Edit3.Text:='√';
    Edit4.Text:='';
    Edit5.Text:='';
  end else
  begin
    Edit3.Text:='';
    Edit4.Text:='√';
    Edit5.Text:='';
  end;  }
end;

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

procedure TfrmMJManageNewFDJJAP.Edit5Click(Sender: TObject);
begin
 { if Trim(Edit5.Text)='' then
  begin
    Edit5.Text:='√';
    Edit3.Text:='';
    Edit4.Text:='';
  end else
  begin
    Edit5.Text:='';
    Edit3.Text:='√';
    Edit4.Text:='';
  end; }
end;

procedure TfrmMJManageNewFDJJAP.Label9DblClick(Sender: TObject);
begin
  {if Trim(Label9.Caption)='M' then
  begin
    Label9.Caption:='Y';
  end else
  begin
    Label9.Caption:='M';
  end;   }
 // Label19.Caption:=Trim(Label9.Caption);
  //Label20.Caption:=Trim(Label9.Caption);
  //Label7.Caption:=Trim(Label9.Caption);
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.Edit10Click(Sender: TObject);
begin
  {if Trim(Edit10.Text)='' then
  begin
    Edit10.Text:='√';
    Edit12.Text:='';
    Edit7.Text:='';
  end else
  begin
    Edit10.Text:='';
    Edit12.Text:='';
    Edit7.Text:='';
  end; }
end;

procedure TfrmMJManageNewFDJJAP.Edit12Click(Sender: TObject);
begin
  if Trim(Edit12.Text) = '' then
  begin
    Edit1.Text := '';
    Edit12.Text := '√';
    Edit7.Text := '';
  end
  else
  begin
    Edit1.Text := '';
    Edit12.Text := '';
    Edit7.Text := '';
  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.JBStrClick(Sender: TObject);
begin
  if Trim(JBStr.Text) = '' then
  begin
    JBStr.Text := '√';
  end
  else
  begin
    JBStr.Text := '';
    JBSCPerson.Text := '';
    JBZhuanQty.Text := '';
  end;
end;

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

procedure TfrmMJManageNewFDJJAP.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 TfrmMJManageNewFDJJAP.InitDataJM();
begin
  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.KHName,B.GCCode,A.ChouZhen,A.c_codename,AA.APBatchNo');
    sql.Add(',A.C_Qty,AA.APID,AA.SCPerson,AA.DCG,AA.CarNo,AA.MainId,AA.SubId,A.PiZhong,A.PiKgQty,A.BatchNoHZ,AA.JXPerson,A.PiKgQtyFD ,AA.CHEJIAN');
    sql.Add(',QQty=isnull(A.C_Qty,0)-isnull((select sum(MJMaoZ) from WFB_MJJY_PB PB where PB.SubId=A.SubId),0)');
    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) + '''');
    Open;
  end;
  if ADOTmp.IsEmpty = False then
  begin
    Label55.Caption := Trim(ADOTmp.fieldbyname('KHName').AsString);
    Label56.Caption := Trim(ADOTmp.fieldbyname('c_codename').AsString);
    APBatchNo.Text := Trim(ADOTmp.fieldbyname('APBatchNo').AsString);
    ConNo.Text := Trim(ADOTmp.fieldbyname('ConNo').AsString);
    GCCode.Text := Trim(ADOTmp.fieldbyname('GCCode').AsString);
    CarNo.Text := Trim(ADOTmp.fieldbyname('CarNo').AsString);
    JXPerson.Text := Trim(ADOTmp.fieldbyname('JXPerson').AsString);
    SCPerson.Text := Trim(ADOTmp.fieldbyname('DCG').AsString);
    LabChouZhen.Caption := Trim(ADOTmp.fieldbyname('ChouZhen').AsString);
    LabQty.Caption := Trim(ADOTmp.fieldbyname('C_Qty').AsString);
    LabQQty.Caption := Trim(ADOTmp.fieldbyname('QQty').AsString);
//    MJPiZ.Text:=Trim(ADOTmp.fieldbyname('PiZhong').AsString);
    MJKJZ.Text := Trim(ADOTmp.fieldbyname('PiZhong').AsString);
    lbl2.Caption := Trim(ADOTmp.fieldbyname('chejian').AsString);
    FAPID := Trim(APID.Text);
    FPiKgQty := ADOTmp.fieldbyname('PiKgQty').asfloat;
    FMainId := Trim(ADOTmp.fieldbyname('MainId').AsString);
    FSubId := Trim(ADOTmp.fieldbyname('SubId').AsString);
    Label31.Caption := FAPID;
    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;

//    with ADOTmp do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add(' select top 1 SCPerson  from WFB_MJJY_PB');
//      sql.Add(' where CarNo=' + quotedstr(Trim(CarNo.Text)));
//      sql.Add(' and WorkShift=' + quotedstr(Trim(WorkShift.Text)));
//      sql.Add(' order by Filltime desc ');
//      Open;
//    end;
//    SCPerson.Text := Trim(ADOTmp.fieldbyname('SCPerson').AsString);
//    ZZPerson.Text := Trim(ADOTmp.fieldbyname('SCPerson').AsString);
  end
  else
  begin
    Application.MessageBox('条码错误!', '提示', 0);
    Label55.Caption := '客户';
    APID.Text := '';
    FAPID := '';
    FMainId := '';
    FSubId := '';
    Label31.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.Button3Click(Sender: TObject);
begin
  try
    frmSCPerson := TfrmSCPerson.Create(self);
    with frmSCPerson do
    begin
      FlagStr := 'SCPerson';
//      FCHEJIAN := TRIM(LBL2.CAPTION);
      if ShowModal = 1 then
      begin
        SCPerson.Text := frmSCPerson.FSDPerson;
        ZZPerson.Text := frmSCPerson.FSDPerson;

      end;
    end;
  finally
    frmSCPerson.Free;
  end;

end;

procedure TfrmMJManageNewFDJJAP.Button5Click(Sender: TObject);
begin
  try
    frmSCPerson := TfrmSCPerson.Create(self);
    with frmSCPerson do
    begin
      FlagStr := 'SCPerson';
      FCHEJIAN := TRIM(LBL2.CAPTION);
      if ShowModal = 1 then
      begin
        ZZPerson.Text := frmSCPerson.FSDPerson;
      end;
    end;
  finally
    frmSCPerson.Free;
  end;
end;

procedure TfrmMJManageNewFDJJAP.Button6Click(Sender: TObject);
begin
  try
    frmSCPerson := TfrmSCPerson.Create(self);

    with frmSCPerson do
    begin
      FlagStr := 'SCPerson';
      FCHEJIAN := TRIM(LBL2.CAPTION);
      if ShowModal = 1 then
      begin
        JBSCPerson.Text := frmSCPerson.FSDPerson;
      end;
    end;
  finally
    frmSCPerson.Free;
  end;
end;

procedure TfrmMJManageNewFDJJAP.Button7Click(Sender: TObject);
begin
  try
    frmSCPerson := TfrmSCPerson.Create(self);
    with frmSCPerson do
    begin
      FlagStr := 'SCPerson';
      if ShowModal = 1 then
      begin
        JBSCPerson2.Text := frmSCPerson.FSDPerson;
      end;
    end;
  finally
    frmSCPerson.Free;
  end;
end;

procedure TfrmMJManageNewFDJJAP.Button8Click(Sender: TObject);
begin
  try
    frmSCPerson := TfrmSCPerson.Create(self);
    with frmSCPerson do
    begin
      FlagStr := 'houdang';
      if ShowModal = 1 then
      begin
        houdang.Text := frmSCPerson.FSDPerson;
      end;
    end;
  finally
    frmSCPerson.Free;
  end;
end;

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

procedure TfrmMJManageNewFDJJAP.Edit1Click(Sender: TObject);
begin
  if Trim(Edit7.Text) = '' then
  begin
    Edit1.Text := '√';
    Edit12.Text := '';
    Edit7.Text := '';
  end
  else
  begin
    Edit1.Text := '';
    Edit12.Text := '';
    Edit7.Text := '';
  end;
end;

procedure TfrmMJManageNewFDJJAP.ZZPersonKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    with ADOTmp do
    begin
      Close;
      sql.Clear;
      sql.Add('select ZDYName from KH_ZDY  where Type=''SCPerson'' and Note=''' + Trim(SCPerson.Text) + '''');
      Open;
    end;
    ZZPerson.Text := trim(ADOTmp.FieldByName('ZDYName').AsString);
  end;
end;

procedure TfrmMJManageNewFDJJAP.MJMaoZKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
    SCPerson.SetFocus;
end;

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

procedure TfrmMJManageNewFDJJAP.edtSCAPIDKeyPress(Sender: TObject; var Key: Char);
var
  SCAPID: string;
begin
  if Key = #13 then
  begin
    SCAPID := Trim(edtSCAPID.Text);
    edtSCAPID.Text := '';
    if Application.MessageBox('确定要作废数据吗？作废后数据不能恢复。', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOTmp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CK_PBCP_CR where CRFlag=''出库'' and MJID=' + quotedstr(SCAPID));
      Open;
    end;
    if ADOTmp.IsEmpty = False then
    begin
      Application.MessageBox('已产生出库数据不能删除!', '提示', 0);
      Exit;
    end
    else
    begin
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete WFB_MJJY_PB_DelLog where MJID=' + quotedstr(SCAPID));

        SQL.Add('insert into WFB_MJJY_PB_DelLog ');
        sql.Add('select * from WFB_MJJY_PB where MJID=' + quotedstr(SCAPID));
        ExecSQL;
      end;
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update WFB_MJJY_PB_DelLog Set Note=Isnull(Note,'''')+''→删除人:''+''' + Trim(DName) + '''');
        sql.Add('+'' ''+Convert(Varchar(20),getdate(),120)');
        sql.Add(' where MJID=' + quotedstr(SCAPID));
        ExecSQL;
      end;
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete WFB_MJJY_PB where MJID=' + quotedstr(SCAPID));
        sql.Add('delete WFB_MJJY_CD_PB where MJID=' + quotedstr(SCAPID));
        sql.Add('delete CK_PBCP_CR where MJID=' + quotedstr(SCAPID));
        sql.Add('delete CK_PBCP_kc where MJID=' + quotedstr(SCAPID));
        ExecSQL;
      end;
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        SQL.Add('Update JYCon_Sub_AnPai Set JYFlag=0 where APID=' + quotedstr(SCAPID));
        ExecSQL;
      end;
    end;
  end;
end;

procedure TfrmMJManageNewFDJJAP.Button9Click(Sender: TObject);
begin
  if CDS_MJID.IsEmpty = true then
    Exit;
  PrintData(Trim(CDS_MJID.fieldbyname('mjid').AsString));

end;

procedure TfrmMJManageNewFDJJAP.Button10Click(Sender: TObject);
begin
  if CDS_MJID.IsEmpty then
    Exit;
  if Application.MessageBox(PChar('确定要对' + CDS_MJID.FieldByName('APXH').AsString + '号布，重量是' + CDS_MJID.FieldByName('MJJingZ').AsString + 'KG的这匹布进行删除吗？'), '提示', 32 + 4) <> IDYES then
    Exit;

  with ADOQuery3 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_PBCP_CR where CRFlag=''出库'' and MJID=''' + Trim(CDS_MJID.fieldbyname('MJID').AsString) + '''');
//    ShowMessage(sql.text);
    Open;
  end;
  if ADOQuery3.IsEmpty = False then
  begin
    Application.MessageBox('已产生出库数据不能删除!', '提示', 0);
    Exit;
  end
  else
  begin
    with ADOQuery3 do
    begin
      Close;
      sql.Clear;
      sql.Add('delete WFB_MJJY_PB_DelLog where MJID=''' + Trim(CDS_MJID.fieldbyname('MJID').AsString) + '''');
      SQL.Add('insert into WFB_MJJY_PB_DelLog ');
      sql.Add('select * from WFB_MJJY_PB where MJID=''' + Trim(CDS_MJID.fieldbyname('MJID').AsString) + '''');
//      ShowMessage(sql.text);
      ExecSQL;
    end;
    with ADOQuery3 do
    begin
      Close;
      sql.Clear;
      sql.Add('Update WFB_MJJY_PB_DelLog Set Note=Isnull(Note,'''')+''→删除人:''+''' + Trim(DName) + '''');
      sql.Add('+'' ''+Convert(Varchar(20),getdate(),120)');
      sql.Add(' where MJID=''' + Trim(CDS_MJID.fieldbyname('MJID').AsString) + '''');
      ExecSQL;
    end;
    with ADOQuery3 do
    begin
      Close;
      sql.Clear;
      sql.Add('delete WFB_MJJY_PB where MJID=''' + Trim(CDS_MJID.fieldbyname('MJID').AsString) + '''');
      sql.Add('delete WFB_MJJY_CD_PB where MJID=''' + Trim(CDS_MJID.fieldbyname('MJID').AsString) + '''');
      sql.Add('delete CK_PBCP_CR where MJID=''' + Trim(CDS_MJID.fieldbyname('MJID').AsString) + '''');
      sql.Add('delete CK_PBCP_kc where MJID=''' + Trim(CDS_MJID.fieldbyname('MJID').AsString) + '''');
//      ShowMessage(sql.text);
      ExecSQL;
    end;
    with ADOQuery3 do
    begin
      Close;
      sql.Clear;
      SQL.Add('Update JYCon_Sub_AnPai Set JYFlag=0 where APID=''' + Trim(CDS_MJID.fieldbyname('MJID').AsString) + '''');
      ExecSQL;
    end;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
//      sql.Add('select * from WFB_MJJY_PB where MainId=''' + Trim(FMainId) + ''' ');
//      sql.Add(' and convert(varchar(10),FillTime,120)=convert(varchar(10),getdate(),120)');

      sql.Add(' select A.*  ');
      sql.Add(',DuoYu=A.MJMaoZ-isnull((select X.PiKGQty from Cloth_Sub X where X.MainID=A.MainID),0)');
      sql.Add('from WFB_MJJY_PB A where MainId=''' + Trim(FMainId) + ''' ');
      sql.Add(' and convert(varchar(10),A.FillTime,120)=convert(varchar(10),getdate(),120)  order by A.Filltime desc ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_MJID);
    SInitCDSData20(ADOQueryMain, CDS_MJID);

  end;
end;

procedure TfrmMJManageNewFDJJAP.Panel9Click(Sender: TObject);
begin
  Panel9.Visible := false;
end;

procedure TfrmMJManageNewFDJJAP.Tv3CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if AViewInfo.GridRecord.Values[Tv3Column1.Index] >= 0.4 then
    ACanvas.Brush.Color := $A6A6FF;

end;

end.

