unit U_MJManageNewFDNew_New;

interface

uses
  Windows, Messages, SysUtils,strutils, 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;

type
  TfrmMJManageNewFDNewSF_New = class(TForm)
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    cxGridPopupMenu1: TcxGridPopupMenu;
    DataSource1: TDataSource;
    Order_MJ: TClientDataSet;
    ADOQueryMain: TADOQuery;
    ADOCmd: TADOQuery;
    RMDB_Main: TRMDBDataSet;
    RM2: TRMGridReport;
    DataSource2: TDataSource;
    CDS_MJCD: TClientDataSet;
    DataSource3: TDataSource;
    CDS_MJID: TClientDataSet;
    Timer1: TTimer;
    ADOQueryPrint: TADOQuery;
    cxStyleRepository2: TcxStyleRepository;
    cxStyle3: TcxStyle;
    cxStyleRepository3: TcxStyleRepository;
    cxStyle4: TcxStyle;
    cxStyleRepository4: TcxStyleRepository;
    cxStyle5: TcxStyle;
    cxStyleRepository5: TcxStyleRepository;
    cxStyle6: TcxStyle;
    cxGridPopupMenu2: TcxGridPopupMenu;
    ADOQueryMainDSC: TADOQuery;
    RM3: TRMGridReport;
    ADOTmp: TADOQuery;
    Panel6: TPanel;
    Label16: TLabel;
    Label35: TLabel;
    Label24: TLabel;
    Label25: TLabel;
    Label3: TLabel;
    BTAdd: TButton;
    BTEdit: TButton;
    Edit7: TEdit;
    SMRK: TEdit;
    Label2: TLabel;
    Panel2: TPanel;
    SpeedButton13: TSpeedButton;
    SpeedButton14: TSpeedButton;
    SpeedButton15: TSpeedButton;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    Panel7: TPanel;
    Label5: TLabel;
    Label37: TLabel;
    MJLen: TEdit;
    BTPrint: TButton;
    GangNo: TEdit;
    Button5: TButton;
    Panel3: TPanel;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    SpeedButton5: TSpeedButton;
    SpeedButton6: TSpeedButton;
    SpeedButton7: TSpeedButton;
    SpeedButton8: TSpeedButton;
    SpeedButton9: TSpeedButton;
    SpeedButton10: TSpeedButton;
    SpeedButton11: TSpeedButton;
    SpeedButton12: TSpeedButton;
    TabSheet2: TTabSheet;
    TabSheet3: TTabSheet;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGangNo: TcxGridDBColumn;
    v3JYNo: TcxGridDBColumn;
    v3JYLen: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ComboBox1: TComboBox;
    Panel1: TPanel;
    Label9: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label11: TLabel;
    Label15: TLabel;
    Label30: TLabel;
    Label31: TLabel;
    Label32: TLabel;
    Label33: TLabel;
    JYLen: TEdit;
    JYKG: TEdit;
    JYPZ: TEdit;
    JYJZ: TEdit;
    Panel4: TPanel;
    SpeedButton16: TSpeedButton;
    SpeedButton17: TSpeedButton;
    SpeedButton18: TSpeedButton;
    SpeedButton19: TSpeedButton;
    SpeedButton20: TSpeedButton;
    SpeedButton21: TSpeedButton;
    SpeedButton22: TSpeedButton;
    SpeedButton23: TSpeedButton;
    SpeedButton24: TSpeedButton;
    SpeedButton25: TSpeedButton;
    SpeedButton26: TSpeedButton;
    SpeedButton27: TSpeedButton;
    cxGrid1: TcxGrid;
    TV2: TcxGridDBTableView;
    vJYNo: TcxGridDBColumn;
    cxGridDBJYLen: TcxGridDBColumn;
    v3JYKG: TcxGridDBColumn;
    v3JYJZ: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel8: TPanel;
    SpeedButton29: TSpeedButton;
    SpeedButton30: TSpeedButton;
    SpeedButton31: TSpeedButton;
    SpeedButton32: TSpeedButton;
    SpeedButton33: TSpeedButton;
    SpeedButton34: TSpeedButton;
    cxGrid2: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridDBGangNo: TcxGridDBColumn;
    cxGridDBSJZQty: TcxGridDBColumn;
    cxGridDBMQQty: TcxGridDBColumn;
    cxGridDBCQty: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    Label4: TLabel;
    Button1: TButton;
    Button2: TButton;
    ComboBox2: TComboBox;
    ADOQuery1: TADOQuery;
    Button3: TButton;
    DataSource4: TDataSource;
    DataSource5: TDataSource;
    JY_CP: TClientDataSet;
    JY_HZ: TClientDataSet;
    Edit1: TEdit;
    Panel5: TPanel;
    SpeedButton28: TSpeedButton;
    SpeedButton35: TSpeedButton;
    SpeedButton36: TSpeedButton;
    SpeedButton37: TSpeedButton;
    SpeedButton38: TSpeedButton;
    SpeedButton39: TSpeedButton;
    Label1: TLabel;
    cxGrid4: TcxGrid;
    TV4: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cSJZQty: TcxGridDBColumn;
    cxGridLevel4: TcxGridLevel;
    DataSource6: TDataSource;
    DB_HZ: TClientDataSet;
    Panel9: TPanel;
    Label8: TLabel;
    ComboBox3: TComboBox;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure Tv3CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure FormClick(Sender: TObject);
    procedure BTPrintClick(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton12Click(Sender: TObject);
    procedure SpeedButton13Click(Sender: TObject);
    procedure cxGridDBColumn2PropertiesChange(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure SMRKKeyPress(Sender: TObject; var Key: Char);
    procedure MJstr3KeyPress(Sender: TObject; var Key: Char);
    procedure Label9Click(Sender: TObject);
    procedure JYJZExit(Sender: TObject);
    procedure JYJZChange(Sender: TObject);
    procedure JYKGChange(Sender: TObject);
    procedure Edit7Click(Sender: TObject);
    procedure JYJZKeyPress(Sender: TObject; var Key: Char);
    procedure FormCreate(Sender: TObject);
    procedure SpeedButton14Click(Sender: TObject);
    procedure SpeedButton15Click(Sender: TObject);
    procedure PageControl1Change(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure GangNoClick(Sender: TObject);
    procedure SpeedButton29Click(Sender: TObject);
    procedure JYLenClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure TV2CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure SpeedButton28Click(Sender: TObject);
  private
    { Private declarations }
    FMainId,FSubId,FInt,MD:String;
    FOrdQty:Double;
    isCommopen,IsJsMessage,IsCzMessage:boolean;
    procedure InitCDGridID();
    function  SaveData():Boolean;
    procedure PrtData(FMJID:String);
    procedure PrtDataMD(FMJID:String);
    procedure PrtDataJY(FMJID:String);
    function SaveDataMD():Boolean;
    procedure OpenCom(DllName:string);
    procedure CloseCom(DllName:string);
    function CLnum(fMjlen:string):double;
    procedure On1201(Var Message:Tmessage);Message 1201;
    procedure On1301(Var Message:Tmessage);Message 1301;
    procedure RKData(ZJ:string);
    procedure SSetEditDataADONew(yClientDataset:TADOQuery; mClientDataset:TADOQuery );
  public
    fmanage:string;
    { Public declarations }

  end;

var
  frmMJManageNewFDNewSF_New: TfrmMJManageNewFDNewSF_New;
  newh,newh1:hwnd;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp,U_iniParam;

{$R *.dfm}
procedure TfrmMJManageNewFDNewSF_New.SSetEditDataADONew(yClientDataset:TADOQuery; mClientDataset:TADOQuery );
var
  i:integer;
  k:integer;
  mfieldName:string;
begin
  if yClientDataset.IsEmpty then exit;
  K:=1;
  try
    mClientDataset.DisableControls;
    mClientDataset.Filtered:=false;
    with mClientDataset do
    begin
      Append;
      for i:=0 to yClientDataset.FieldCount-1 do
      begin
        mfieldName:=trim(yClientDataset.fields[i].FieldName);
        fieldbyname(mfieldName).Value:=yClientDataset.Fields[i].Value;
      end;
      post;
      inc(k);
    end;
    if not mClientDataset.IsEmpty then
    begin
      mClientDataset.First;
    end;
  finally
    mClientDataset.First;
    mClientDataset.EnableControls;
  end;
end;

procedure TfrmMJManageNewFDNewSF_New.RKData(ZJ:string);
var maxno:string;
begin
  with ADOQueryPrint do
  begin
    close;
    sql.Clear;
    sql.Add('select * from JTJY_New where JYID='''+trim(ZJ)+'''');
    open;
  end;
  with ADOCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select * from CP_MX_New where JYID='''+trim(ZJ)+'''');
    open;
  end;
  with ADOCmd do
  begin
    if IsEmpty then
    begin
      Append;
      if GetLSNo(ADOTmp,maxno,'CP','CP_MX_New',4,1)=False then
      begin
        Application.MessageBox('生成流水号异常！','提示',0);
        Button1.Enabled:=true;
        Button2.Enabled:=true;
        exit;
      end;
      FieldByName('Filler').Value:=Trim(DName);
    end
    else
    begin
      maxno:=Trim(ADOCmd.fieldbyname('CPID').AsString);
      Edit;
      FieldByName('Editer').Value:=Trim(DName);
      FieldByName('EditTime').Value:=SGetServerDateTime(ADOTmp);
    end;
    FieldByName('CPID').Value:=Trim(maxno);
    SSetEditDataADONew(ADOQueryPrint,ADOCmd);
    Post;
  end;
end;

function TfrmMJManageNewFDNewSF_New.CLnum(fMjlen:string):double;
var
  i,m:integer;
  num1:double;
begin
  i:=strtoint(rightstr(fMjlen,1));
  m:=strtoint(leftstr(fMjlen,length(fMjlen)-2));
  IF (i>=3) and (i<8) then
    num1:=strtofloat(inttostr(m)+'.5')
  else IF i>=8 then
    num1:=m+1
  else
    num1:=m;
  result:= num1;
end;

procedure TfrmMJManageNewFDNewSF_New.CloseCom(DllName:string);
type
  TMyFunc = function(sCommName:PAnsiChar): HWND; stdcall;
var
  Tf1: TMyFunc;
  Tp1: TFarProc;
  Th1:Thandle;
begin
  Th1 := LoadLibrary(Pchar(trim(DllName)));
  if Th1 > 0 then
  begin
    try
      Tp1 := GetProcAddress(Th1, 'CommClose');
      if Tp1 <> nil then
      begin
        Tf1 := TMyFunc(Tp1);
        newh1:=Tf1('Comm1');
      end
      else
      begin

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

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

procedure TfrmMJManageNewFDNewSF_New.FormDestroy(Sender: TObject);
begin
  frmMJManageNewFDNewSF_New:=nil;
end;

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

procedure TfrmMJManageNewFDNewSF_New.On1301(Var Message:Tmessage);
var
  i1,i2:integer;
  unitname:string;
  fdata:double;
begin
  i1:=message.WParam;
  i2:=message.LParam;
  IF IsJsMessage then
  begin
    IF trim(Edit7.Text)='√' then
    begin
      if PageControl1.TabIndex=0 then
      begin
        MJLen.Text:=floattostr(CLnum(format('%.1f',[i1 /100000])));
        IF i2=0 then Label9.Caption:='M' else  Label9.Caption:='Y';
      end;
      if PageControl1.TabIndex=2 then
      begin
        JYLen.Text:=floattostr(CLnum(format('%.1f',[i1 /100000])));
        IF i2=0 then Label9.Caption:='M' else  Label9.Caption:='Y';
      end;
    end;
  end;
end;

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

procedure TfrmMJManageNewFDNewSF_New.InitCDGridID();
begin
  with ADOQueryMain do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.*');
    sql.Add(' from JTJY_New A where NewSubid='''+Trim(FSubid)+'''');
    sql.Add(' and JTType='''+Trim(label2.Caption)+'''');
    sql.Add(' and Filler='''+Trim(DName)+'''');
    sql.Add(' and RKType=''未入库'' ');
    Open;
  end;
  SCreateCDS20(ADOQueryMain,CDS_MJID);
  SInitCDSData20(ADOQueryMain,CDS_MJID);
end;

procedure TfrmMJManageNewFDNewSF_New.FormShow(Sender: TObject);
begin
  IF trim(Edit7.Text)='√'then
  IsJsMessage:=true;
  //IF trim(fmanage)<>'高权限' then
  begin
    OpenCom(JCYDLL);
    OpenCom(DllName);
  end;
  SMRK.SetFocus;
end;

procedure TfrmMJManageNewFDNewSF_New.Tv3CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  with ADOTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from JTJY_New where JYID='''+Trim(Order_MJ.fieldbyname('JYID').AsString)+'''');
    Open;
  end;
  MJLen.Text:=Trim(ADOTmp.fieldbyname('JYLen').AsString);
  GangNo.Text:=Trim(ADOTmp.fieldbyname('GangNo').AsString);
end;

procedure TfrmMJManageNewFDNewSF_New.FormClick(Sender: TObject);
begin
  FInt:='0';
end;

function TfrmMJManageNewFDNewSF_New.SaveData():Boolean;
var
  maxno,FJYID,BZID,strsql:String;
  FMJLen:Double;
  FOrder,FWZ:Integer;
begin
  if FInt='1' then
  begin
    FJYID:='';
  end;
  if FInt='2' then
  begin
    FJYID:=Trim(JY_CP.fieldbyname('JYID').AsString);
  end;
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from JTJY_New where JYID='''+Trim(FJYID)+'''');
      Open;
    end;
    with ADOCmd do
    begin
      if IsEmpty then
      begin
        Append;
        if GetLSNo(ADOTmp,maxno,Trim(MD),'JTJY_New',4,1)=False then
        begin
          ADOCmd.Connection.RollbackTrans;
          Application.MessageBox('生成流水号异常！','提示',0);
          Button1.Enabled:=true;
          Button2.Enabled:=true;
          exit;
        end;
        FieldByName('Filler').Value:=Trim(DName);
      end
      else
      begin
        maxno:=Trim(FJYID);
        Edit;
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOTmp);
      end;
      FieldByName('NewMLID').value:=Trim(FMainId);
      FieldByName('NewSubid').value:=Trim(FSubId);
      FieldByName('JYID').Value:=Trim(maxno);
      FieldByName('RKType').Value:='未入库';
      FieldByName('gangNo').Value:=Trim(edit1.Text);
      FieldByName('JYLen').Value:=Trim(JYLen.Text);
      FieldByName('JYKG').Value:=StrToFloatDef(JYKG.Text,0);
      FieldByName('JYPZ').Value:=StrToFloatDef(JYPZ.Text,0);
      FieldByName('JYJZ').Value:=StrToFloatDef(JYJZ.Text,0);
      FieldByName('JYType').Value:=Trim(Label2.Caption);
      FieldByName('JYOrderNo').Value:=Trim(label24.Caption);
      FieldByName('JYColHX').Value:=Trim(label3.Caption);
      FieldByName('JYColor').Value:=Trim(label25.Caption);
      FieldByName('JYUnit').Value:=Trim(Label9.Caption);
      Post;
    end;
    if FInt='1' then
    begin
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('update JTJY_New SET JYNo=1+isnull((select cast(max(X.JYNo) as int) from JTJY_New X ');
        sql.Add('where X.NewSubid=JTJY_New.NewSubid and X.JYType=JTJY_New.JYType ');
        SQL.Add('and X.GangNo=JTJY_New.GangNo and X.JYID<>JTJY_New.JYID),0) ');
        sql.Add('where JYID='''+Trim(maxno)+'''');
        execsql;
      end;
      if JY_CP.IsEmpty then
      begin
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JTJY_New where NewSubid='''+trim(FSubId)+''' and JYType='''+trim(Label2.Caption)+'''');
          SQL.Add(' and GangNo='''+trim(edit1.Text)+'''');
          open;
        end;
        SCreateCDS20(ADOCmd,JY_CP);
        SInitCDSData20(ADOCmd,JY_CP);
      end
      else
      begin
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JTJY_New where JYID='''+trim(maxno)+'''');
          open;
        end;
        SInitCDSData20(ADOCmd,JY_CP);
      end;
      JY_CP.Locate('JYID',Trim(maxno),[]);
    end;
    if FInt='2' then
    begin
      with JY_CP do
      begin
        edit;
        FieldByName('JYJZ').Value:=Trim(JYJZ.Text);
        FieldByName('JYKG').Value:=Trim(JYKG.Text);
        FieldByName('JYLen').Value:=Trim(JYLen.Text);
      end;
    end;
    FJYID:=Trim(maxno);
    //成品入库
    RKData(FJYID);
    ADOCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

function TfrmMJManageNewFDNewSF_New.SaveDataMD():Boolean;
var
  maxno,FJYID,BZID,strsql:String;
  FMJLen:Double;
  FOrder,FWZ:Integer;
begin
  if FInt='1' then
  begin
    FJYID:='';
  end;
  if FInt='2' then
  begin
    FJYID:=Trim(Order_MJ.fieldbyname('JYID').AsString);
  end;
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from JTJY_New where JYID='''+Trim(FJYID)+'''');
      Open;
    end;
    with ADOCmd do
    begin
      if IsEmpty then
      begin
        Append;
        if GetLSNo(ADOTmp,maxno,Trim(MD),'JTJY_New',4,1)=False then
        begin
          ADOCmd.Connection.RollbackTrans;
          Application.MessageBox('生成流水号异常！','提示',0);
          BTPrint.Enabled:=true;
          Button5.Enabled:=true;
          exit;
        end;
        FieldByName('Filler').Value:=Trim(DName);
      end
      else
      begin
        maxno:=Trim(FJYID);
        Edit;
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOTmp);
      end;
      FieldByName('NewMLID').value:=Trim(FMainId);
      FieldByName('NewSubid').value:=Trim(FSubId);
      FieldByName('JYID').Value:=Trim(maxno);
      FieldByName('RKType').Value:='未入库';
      FieldByName('gangNo').Value:=Trim(GangNo.Text);
      FieldByName('JYLen').Value:=Trim(MJLen.Text);
      FieldByName('JYKG').Value:=0;
      FieldByName('JYPZ').Value:=0;
      FieldByName('JYJZ').Value:=0;
      FieldByName('JYType').Value:=Trim(Label2.Caption);
      FieldByName('JYOrderNo').Value:=Trim(label24.Caption);
      FieldByName('JYColHX').Value:=Trim(label3.Caption);
      FieldByName('JYColor').Value:=Trim(label25.Caption);
      Post;
    end;
    if FInt='1' then
    begin
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('update JTJY_New SET JYNo=1+isnull((select cast(max(X.JYNo) as int) from JTJY_New X ');
        sql.Add('where X.NewSubid=JTJY_New.NewSubid and X.JYType=JTJY_New.JYType ');
        SQL.Add('and X.GangNo=JTJY_New.GangNo and X.JYID<>JTJY_New.JYID),0) ');
        sql.Add('where JYID='''+Trim(maxno)+'''');
        execsql;
      end;
      if Order_MJ.IsEmpty then
      begin
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JTJY_New where NewSubid='''+trim(FSubId)+''' and JYType='''+trim(Label2.Caption)+'''');
          SQL.Add(' and GangNo='''+trim(GangNo.Text)+'''');
          open;
        end;
        SCreateCDS20(ADOCmd,Order_MJ);
        SInitCDSData20(ADOCmd,Order_MJ);
      end
      else
      begin
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JTJY_New where JYID='''+trim(maxno)+'''');
          open;
        end;
        SInitCDSData20(ADOCmd,Order_MJ);
      end;
      Order_MJ.Locate('JYID',Trim(maxno),[]);
    end;
    if FInt='2' then
    begin
      with Order_MJ do
      begin
        edit;
        FieldByName('GangNo').Value:=Trim(GangNo.Text);
        FieldByName('JYLen').Value:=Trim(MJLen.Text);
      end;
    end;
    FJYID:=Trim(maxno);
    ADOCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmMJManageNewFDNewSF_New.BTPrintClick(Sender: TObject);
var
  fPrintFile:String;
  mvalue:Double;
  i:Integer;
begin
  BTPrint.Enabled:=False;
  if (FMainId='') or (FSubId='') then
  begin
    BTPrint.Enabled:=true;
    application.MessageBox('请扫描指示单','提示');
    exit;
  end;
  with ADOQueryPrint do
  begin
    close;
    sql.Clear;
    sql.Add('select Sum(JYLen) MJLen ');
    sql.Add('from JTJY_New A where NewSubid='''+Trim(FSubid)+''' ');
    sql.Add('and JYType='''+trim(Label2.Caption)+'''');
    open;
  end;
  if FOrdQty*1.5<ADOQueryPrint.FieldByName('MJLen').AsFloat+strtofloat(MJLen.Text) then
  begin
    if Application.MessageBox('超过指示单数量的1.5倍,确定要提交吗','提示',1)=2 then
    begin
      BTPrint.Enabled:=true;
      Exit;
    end;
  end;
  if MJLen.Text='' then
  begin
    application.MessageBox('长度不能为空','提示信息',0);
    BTPrint.Enabled:=true;
    exit;
  end;
  if strtofloatdef(trim(MJLen.Text),0)<0.0 then
  begin
    application.MessageBox('长度不能小于0！','提示信息',0);
    BTPrint.Enabled:=true;
    exit;
  end;
  FInt:='1';
  if SaveDataMD() then
  begin
    MJLen.Text:='';
    PrtDataMD(Trim(Order_MJ.fieldbyname('JYID').AsString));
    if Label2.Caption='底布' then
    begin
      SpeedButton13.Click;
    end;
    if Label2.Caption='面布' then
    begin
      SpeedButton14.Click;
    end;
  end;
  BTPrint.Enabled:=true;
  MJLen.SetFocus;
end;

procedure TfrmMJManageNewFDNewSF_New.PrtDataMD(FMJID:String);
var
   fPrintFile:String;
   Txt,fImagePath:string;
   Moudle: THandle;
   Makebar:TMakebar;
   Mixtext:TMixtext;
begin
  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.*,B.*,C.* ');
    SQL.Add('from JTJY_New A ');
    SQL.Add('inner join ML_OrderMainNew B on B.NewMLID=A.NewMLID ');
    SQL.Add('inner join ML_OrderSubidNew C on C.NewSubid=A.NewSubid ');
    sql.Add('where A.JYID='''+trim(FMJID)+'''');
    Open;
  end;
  try
    Moudle:=LoadLibrary('MakeQRBarcode.dll');
    @Makebar:=GetProcAddress(Moudle,'Make');
    @Mixtext:=GetProcAddress(Moudle,'MixText');
    Txt:=Trim(ADOQueryPrint.fieldbyname('JYID').AsString);
    fImagePath:=ExtractFilePath(Application.ExeName)+'image\temp.bmp' ;
    if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName)+'image')) then
        CreateDirectory(pchar(ExtractFilePath(Application.ExeName)+'image'),nil);
    if FileExists(fImagePath) then DeleteFile(fImagePath);
    Makebar(pchar(Txt),Length(Txt),3,3,0,PChar(fImagePath),3);
  except
    application.MessageBox('条形码生成失败！','提示信息',MB_ICONERROR);
    BTPrint.Enabled:=true;
    Button5.Enabled:=true;
    exit;
  end;
  fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\'+trim(Label2.Caption)+'标签.rmf';
  if FileExists(fPrintFile) then
  begin
    RMVariables['QRBARCODE']:=fImagePath;
    RM2.LoadFromFile(fPrintFile);
    Rm2.DefaultCopies:=strtointdef(trim(ComboBox1.Text),1);
    //RM2.ShowReport;
    RM2.PrintReport;
  end
  else
  begin
    Application.MessageBox('没有找标签格式','提示',0);
    BTPrint.Enabled:=true;
    Button5.Enabled:=true;
    exit;
  end;
end;

procedure TfrmMJManageNewFDNewSF_New.PrtData(FMJID:String);
var
   fPrintFile:String;
   Txt,fImagePath:string;
   Moudle: THandle;
   Makebar:TMakebar;
   Mixtext:TMixtext;
begin
  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.*,B.*,C.* ');
    SQL.Add('from JTJY_New A ');
    SQL.Add('inner join ML_OrderMainNew B on B.NewMLID=A.NewMLID ');
    SQL.Add('inner join ML_OrderSubidNew C on C.NewSubid=A.NewSubid ');
    sql.Add('where A.JYID='''+trim(FMJID)+'''');
    Open;
  end;
  try
    Moudle:=LoadLibrary('MakeQRBarcode.dll');
    @Makebar:=GetProcAddress(Moudle,'Make');
    @Mixtext:=GetProcAddress(Moudle,'MixText');
    Txt:=Trim(ADOQueryPrint.fieldbyname('JYID').AsString);
    fImagePath:=ExtractFilePath(Application.ExeName)+'image\temp.bmp' ;
    if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName)+'image')) then
        CreateDirectory(pchar(ExtractFilePath(Application.ExeName)+'image'),nil);
    if FileExists(fImagePath) then DeleteFile(fImagePath);
    Makebar(pchar(Txt),Length(Txt),3,3,0,PChar(fImagePath),3);
  except
    application.MessageBox('条形码生成失败！','提示信息',MB_ICONERROR);
    Button1.Enabled:=true;
    Button2.Enabled:=true;
    exit;
  end;
  fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\'+trim(ADOQueryPrint.fieldbyname('CPBQName').AsString)+'.rmf';
  if FileExists(fPrintFile) then
  begin
    RMVariables['QRBARCODE']:=fImagePath;
    RM2.LoadFromFile(fPrintFile);
    Rm2.DefaultCopies:=strtointdef(trim(ComboBox1.Text),1);
    //RM2.ShowReport;
    RM2.PrintReport;
  end
  else
  begin
    Application.MessageBox('没有找标签格式','提示',0);
    Button1.Enabled:=true;
    Button2.Enabled:=true;
    exit;
  end;
end;

procedure TfrmMJManageNewFDNewSF_New.PrtDataJY(FMJID:String);
var
   fPrintFile:String;
   Txt,fImagePath:string;
   Moudle: THandle;
   Makebar:TMakebar;
   Mixtext:TMixtext;
begin
  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec P_Print_JY_Label ');
    sql.Add('@MJID='+quotedstr(trim(FMJID)) );
    Open;
  end;
  try
    Moudle:=LoadLibrary('MakeQRBarcode.dll');
    @Makebar:=GetProcAddress(Moudle,'Make');
    @Mixtext:=GetProcAddress(Moudle,'MixText');
    Txt:=Trim(ADOQueryPrint.fieldbyname('MJID').AsString);
    fImagePath:=ExtractFilePath(Application.ExeName)+'image\temp.bmp' ;
    if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName)+'image')) then
        CreateDirectory(pchar(ExtractFilePath(Application.ExeName)+'image'),nil);
    if FileExists(fImagePath) then DeleteFile(fImagePath);
    Makebar(pchar(Txt),Length(Txt),3,3,0,PChar(fImagePath),3);
  except
    application.MessageBox('条形码生成失败！','提示信息',MB_ICONERROR);
    BTPrint.Enabled:=true;
    exit;
  end;
  fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\剪样标签.rmf';
  if FileExists(fPrintFile) then
  begin
    RMVariables['QRBARCODE']:=fImagePath;
    RM2.LoadFromFile(fPrintFile);
    //Rm2.DefaultCopies:=strtointdef(trim(ComboBox1.Text),1);
    //RM2.ShowReport;
    RM2.PrintReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\'+Trim(ADOQueryPrint.fieldbyname('LBName').AsString)),'提示',0);
  end;
end;

procedure TfrmMJManageNewFDNewSF_New.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 TfrmMJManageNewFDNewSF_New.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 TfrmMJManageNewFDNewSF_New.SpeedButton13Click(Sender: TObject);
var
  AA:array[0..100] of string;
  i,j:Integer;
begin
  Label2.Caption:='底布';
  MD:='DB';
  PageControl1.TabIndex:=0;
  with ADOQueryPrint do
  begin
    close;
    sql.Clear;
    sql.Add('select A.GangNo,A.NewSubid,Sum(A.JYLen) SJZQty ');
    sql.Add('from JTJY_New A where NewSubid='''+Trim(FSubid)+''' ');
    sql.Add('and JYType=''底布'' ');
    SQL.Add('group by A.GangNo,A.NewSubid');
    //ShowMessage(sql.Text);
    open;
  end;
  SCreateCDS20(ADOQueryPrint,DB_HZ);
  SInitCDSData20(ADOQueryPrint,DB_HZ);
  with DB_HZ do
  begin
    First;
    i:=0;
    while not Eof do
    begin
      AA[i]:=Trim(fieldbyname('GangNo').AsString);
      i:=i+1;
      Next;
    end;
  end;
  with Panel5 do
  begin
    for j:=0 to i-1 do
    begin
      if Controls[j] is TSpeedButton then
      begin
        TSpeedButton(Controls[j]).Visible:=true;
        TSpeedButton(Controls[j]).Hint:=AA[j];
        TSpeedButton(Controls[j]).Caption:=AA[j];
      end;
    end;
  end;
  MJLen.SetFocus;
  panel2.Visible:=false;
end;

procedure TfrmMJManageNewFDNewSF_New.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 TfrmMJManageNewFDNewSF_New.Button5Click(Sender: TObject);
var
   fPrintFile:String;
   i:Integer;
     mvalue:Double;
begin
  Button5.Enabled:=False;
  if (FMainId='') or (FSubId='') then
  begin
    Button5.Enabled:=true;
    application.MessageBox('请扫描指示单','提示');
    exit;
  end;
  with ADOQueryPrint do
  begin
    close;
    sql.Clear;
    sql.Add('select Sum(JYLen) MJLen ');
    sql.Add('from JTJY_New A where NewSubid='''+Trim(FSubid)+''' ');
    sql.Add('and JYType='''+trim(Label2.Caption)+''' and A.JYID<>'''+trim(Order_MJ.fieldbyname('JYID').AsString)+'''');
    open;
  end;
  if FOrdQty*1.5<ADOQueryPrint.FieldByName('MJLen').AsFloat+strtofloat(MJLen.Text) then
  begin
    if Application.MessageBox('超过指示单数量的1.5倍,确定要提交吗','提示',1)=2 then
    begin
      Button5.Enabled:=true;
      Exit;
    end;
  end;
  if MJLen.Text='' then
  begin
    application.MessageBox('长度不能为空','提示信息',0);
    Button5.Enabled:=true;
    exit;
  end;
  if strtofloatdef(trim(MJLen.Text),0)<0.0 then
  begin
    application.MessageBox('长度不能小于0！','提示信息',0);
    Button5.Enabled:=true;
    exit;
  end;
  FInt:='2';
  if SaveDataMD() then
  begin
    MJLen.Text:='';
    PrtDataMD(Trim(Order_MJ.fieldbyname('JYID').AsString));
  end;
  Button5.Enabled:=true;
  MJLen.SetFocus;
end;

procedure TfrmMJManageNewFDNewSF_New.SMRKKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      sql.add('select A.*,B.* ');
      sql.Add('from ML_OrderMainNew A ');
      sql.Add('inner join ML_OrderSubidNew B on B.NewMLID=A.NewMLID ');
      sql.Add('where B.NewSubid='''+trim(SMRK.Text)+'''');
      Open;
    end;
    if ADOQuery1.IsEmpty then
    begin
      SMRK.Text:='';
      Application.MessageBox('没有该条码','提示');
      exit;
    end;
    FMainId:=Trim(ADOQuery1.fieldbyname('NewMLID').AsString);
    FSubId:=Trim(ADOQuery1.fieldbyname('NewSubid').AsString);
    FOrdQty:=ADOQuery1.fieldbyname('Qty').AsFloat;
    Label24.Caption:=Trim(ADOQuery1.fieldbyname('MLOrderNo').AsString);
    Label25.Caption:=Trim(ADOQuery1.fieldbyname('MLColor').AsString);
    Label3.Caption:=Trim(ADOQuery1.fieldbyname('MLHX').AsString);
    Label9.Caption:=Trim(ADOQuery1.fieldbyname('QtyUnit').AsString);
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JTJY_New where 1=2');
      open;
    end;
    SCreateCDS20(ADOCmd,Order_MJ);
    Panel2.Visible:=True;
    SMRK.Text:='';
  end;
end;

procedure TfrmMJManageNewFDNewSF_New.MJstr3KeyPress(Sender: TObject;
  var Key: Char);
begin
	if Key = #13 then
	begin
		Button1.SetFocus;
	end;
end;

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

procedure TfrmMJManageNewFDNewSF_New.JYJZExit(Sender: TObject);
begin
  if (trim(JYKG.Text)='') and (trim(JYJZ.Text)='') then exit;
  if (trim(JYKG.Text)='') and (trim(JYPZ.Text)='') then exit;
  if (trim(JYJZ.Text)='') and (trim(JYPZ.Text)='') then exit;

  if (trim(JYPZ.Text)='') then exit;

  if (trim(JYJZ.Text)<>'') then
  begin
    JYKG.Text:=floattostr(strtofloatdef(trim(JYPZ.Text),0)+strtofloatdef(trim(JYJZ.Text),0) );
    exit;
  end;
end;

procedure TfrmMJManageNewFDNewSF_New.JYJZChange(Sender: TObject);
var
  FReal:Double;
begin
  if Trim(JYJZ.Text)='' then
  begin
    JYKG.Text:='';
    Exit;
  end;
  if TryStrToFloat(JYJZ.Text,FReal)=False then Exit;
  if Trim(JYPZ.Text)<>'' then
  begin
    if TryStrToFloat(JYPZ.Text,FReal)=True then
      JYKG.Text:=FloatToStr(StrToFloat(JYJZ.Text)+StrToFloat(JYPZ.Text));
  end else
  begin
    JYKG.Text:=JYJZ.Text;
  end;
end;

procedure TfrmMJManageNewFDNewSF_New.JYKGChange(Sender: TObject);
var
  FReal:Double;
begin
  if Trim(JYKG.Text)='' then
  begin
    JYJZ.Text:='';
    Exit;
  end;
  if TryStrToFloat(JYKG.Text,FReal)=False then Exit;
  if Trim(JYPZ.Text)<>'' then
  begin
    if TryStrToFloat(JYPZ.Text,FReal)=False then Exit;
    JYJZ.Text:=FloatToStr(StrToFloat(JYKG.Text)-StrToFloat(JYPZ.Text));
  end else
  begin
    JYJZ.Text:=JYKG.Text;
  end;
end;

procedure TfrmMJManageNewFDNewSF_New.Edit7Click(Sender: TObject);
begin
  if Trim(Edit7.Text)='' then
  begin
    Edit7.Text:='√';
    if not IsCommOpen then OpenCom(JCYDLL);
    IsJsMessage:=true;
  end
  else
  begin
    Edit7.Text:='';
    IsJsMessage:=true;
  end;
end;

procedure TfrmMJManageNewFDNewSF_New.JYJZKeyPress(Sender: TObject;
  var Key: Char);
begin
  IF key=#13 then
  begin
    BTPrint.Click;
  end;
end;

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

procedure TfrmMJManageNewFDNewSF_New.SpeedButton14Click(Sender: TObject);
var
  AA:array[0..100] of string;
  i,j:Integer;
begin
  Label2.Caption:='面布';
  MD:='MB';
  PageControl1.TabIndex:=0;
  with ADOQueryPrint do
  begin
    close;
    sql.Clear;
    sql.Add('select A.GangNo,A.NewSubid,Sum(A.JYLen) SJZQty ');
    sql.Add('from JTJY_New A where NewSubid='''+Trim(FSubid)+''' ');
    sql.Add('and JYType=''面布'' ');
    SQL.Add('group by A.GangNo,A.NewSubid');
    //ShowMessage(sql.Text);
    open;
  end;
  SCreateCDS20(ADOQueryPrint,DB_HZ);
  SInitCDSData20(ADOQueryPrint,DB_HZ);
  with DB_HZ do
  begin
    First;
    i:=0;
    while not Eof do
    begin
      AA[i]:=Trim(fieldbyname('GangNo').AsString);
      i:=i+1;
      Next;
    end;
  end;
  with Panel5 do
  begin
    for j:=0 to i-1 do
    begin
      if Controls[j] is TSpeedButton then
      begin
        TSpeedButton(Controls[j]).Visible:=true;
        TSpeedButton(Controls[j]).Hint:=AA[j];
        TSpeedButton(Controls[j]).Caption:=AA[j];
      end;
    end;
  end;
  MJLen.SetFocus;
  panel2.Visible:=false;
end;

procedure TfrmMJManageNewFDNewSF_New.SpeedButton15Click(Sender: TObject);
var
  AA:array[0..100] of string;
  i,j:Integer;
begin
  Label2.Caption:='成品';
  MD:='CP';
  PageControl1.TabIndex:=2;
  with ADOQueryPrint do
  begin
    close;
    sql.Clear;
    sql.Add('select AA.*,CQty=SJZQty-MQQty from (select A.GangNo,A.NewSubid,Sum(A.JYLen) SJZQty ');
    sql.Add(',MQQty=(select Sum(B.JYLen) from JTJY_New B where B.NewSubid=A.NewSubid ');
    sql.Add('and B.JYType=''成品'' and B.GangNo=A.GangNo)');
    sql.Add('from JTJY_New A where NewSubid='''+Trim(FSubid)+''' ');
    sql.Add('and JYType=''面布'' ');
    SQL.Add('group by A.GangNo,A.NewSubid) AA');
    //ShowMessage(sql.Text);
    open;
  end;
  SCreateCDS20(ADOQueryPrint,JY_HZ);
  SInitCDSData20(ADOQueryPrint,JY_HZ);
  with JY_HZ do
  begin
    First;
    i:=0;
    while not Eof do
    begin
      AA[i]:=Trim(fieldbyname('GangNo').AsString);
      i:=i+1;
      Next;
    end;
  end;
  with Panel8 do
  begin
    for j:=0 to i-1 do
    begin
      if Controls[j] is TSpeedButton then
      begin
        TSpeedButton(Controls[j]).Visible:=true;
        TSpeedButton(Controls[j]).Hint:=AA[j];
        TSpeedButton(Controls[j]).Caption:=AA[j];
      end;
    end;
  end;
  JYLen.SetFocus;
  panel2.Visible:=false;
end;

procedure TfrmMJManageNewFDNewSF_New.PageControl1Change(Sender: TObject);
begin
  if not IsCommOpen then OpenCom(JCYDLL);
  IsJsMessage:=true;
end;

procedure TfrmMJManageNewFDNewSF_New.Button3Click(Sender: TObject);
begin
  close;
end;

procedure TfrmMJManageNewFDNewSF_New.GangNoClick(Sender: TObject);
var
  i:Integer;
begin
  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 TfrmMJManageNewFDNewSF_New.SpeedButton29Click(Sender: TObject);
var
  i:Integer;
begin
  with Panel8 do
  begin
    for i:=0 to ControlCount-1 do
    begin
      if Controls[i] is TSpeedButton then
      begin
        TSpeedButton(Controls[i]).Font.Color:=clWindowText;
      end;
    end;
  end;
  TSpeedButton(Sender).Font.Color:=clRed;
  Edit1.Text:=TSpeedButton(Sender).Caption;
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from JTJY_New where NewSubid='''+Trim(FSubid)+'''');
    sql.Add(' and GangNo='''+Trim(Edit1.Text)+'''');
    SQL.Add(' and JYType='''+trim(Label2.Caption)+'''');
    Open;
  end;
  SCreateCDS20(ADOCmd,JY_CP);
  SInitCDSData20(ADOCmd,JY_CP);
end;

procedure TfrmMJManageNewFDNewSF_New.JYLenClick(Sender: TObject);
var
  i:Integer;
begin
  with Panel4 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 TfrmMJManageNewFDNewSF_New.Button1Click(Sender: TObject);
var
  fPrintFile:String;
  mvalue:Double;
  i:Integer;
begin
  button1.Enabled:=False;
  if (FMainId='') or (FSubId='') then
  begin
    button1.Enabled:=true;
    application.MessageBox('请扫描指示单','提示');
    exit;
  end;
  if trim(Edit1.Text)='' then
  begin
    button1.Enabled:=true;
    application.MessageBox('缸号不能为空','提示');
    exit;
  end;
  with ADOQueryPrint do
  begin
    close;
    sql.Clear;
    sql.Add('select Sum(JYLen) JYLen ');
    sql.Add('from JTJY_New A where NewSubid='''+Trim(FSubid)+''' ');
    sql.Add('and JYType='''+trim(Label2.Caption)+'''');
    open;
  end;
  if FOrdQty*1.5<ADOQueryPrint.FieldByName('JYLen').AsFloat+strtofloat(JYLen.Text) then
  begin
    if Application.MessageBox('超过指示单数量的1.5倍,确定要提交吗','提示',1)=2 then
    begin
      button1.Enabled:=true;
      Exit;
    end;
  end;
  if JYLen.Text='' then
  begin
    application.MessageBox('长度不能为空','提示信息',0);
    button1.Enabled:=true;
    exit;
  end;
  if strtofloatdef(trim(JYLen.Text),0)<0.0 then
  begin
    application.MessageBox('长度不能小于0！','提示信息',0);
    button1.Enabled:=true;
    exit;
  end;
  FInt:='1';
  if SaveData() then
  begin
    JYLen.Text:='';
    JYPZ.Text:='';
    JYJZ.Text:='';
    JYKG.Text:='';
    PrtData(Trim(JY_CP.fieldbyname('JYID').AsString));
  end;
  with ADOQueryPrint do
  begin
    close;
    sql.Clear;
    sql.Add('select AA.*,CQty=SJZQty-MQQty from (select A.GangNo,A.NewSubid,Sum(A.JYLen) SJZQty ');
    sql.Add(',MQQty=(select Sum(B.JYLen) from JTJY_New B where B.NewSubid=A.NewSubid ');
    sql.Add('and B.JYType=''成品'' and B.GangNo=A.GangNo) ');
    sql.Add('from JTJY_New A where NewSubid='''+Trim(FSubid)+''' ');
    sql.Add('and JYType=''面布'' ');
    SQL.Add('group by A.GangNo,A.NewSubid) AA');
    //ShowMessage(sql.Text);
    open;
  end;
  SCreateCDS20(ADOQueryPrint,JY_HZ);
  SInitCDSData20(ADOQueryPrint,JY_HZ);
  button1.Enabled:=true;
  JYLen.SetFocus;
end;

procedure TfrmMJManageNewFDNewSF_New.Button2Click(Sender: TObject);
var
   fPrintFile:String;
   i:Integer;
     mvalue:Double;
begin
  button2.Enabled:=False;
  if (FMainId='') or (FSubId='') then
  begin
    Button2.Enabled:=true;
    application.MessageBox('请扫描指示单','提示');
    exit;
  end;
  with ADOQueryPrint do
  begin
    close;
    sql.Clear;
    sql.Add('select Sum(JYLen) MJLen ');
    sql.Add('from JTJY_New A where NewSubid='''+Trim(FSubid)+''' ');
    sql.Add('and JYType='''+trim(Label2.Caption)+''' and A.JYID<>'''+trim(JY_CP.fieldbyname('JYID').AsString)+'''');
    open;
  end;
  if FOrdQty*1.5<ADOQueryPrint.FieldByName('MJLen').AsFloat+strtofloat(JYLen.Text) then
  begin
    if Application.MessageBox('超过指示单数量的1.5倍,确定要提交吗','提示',1)=2 then
    begin
      Button2.Enabled:=true;
      Exit;
    end;
  end;
  if JYLen.Text='' then
  begin
    application.MessageBox('长度不能为空','提示信息',0);
    Button2.Enabled:=true;
    exit;
  end;
  if strtofloatdef(trim(JYLen.Text),0)<0.0 then
  begin
    application.MessageBox('长度不能小于0！','提示信息',0);
    Button2.Enabled:=true;
    exit;
  end;
  FInt:='2';
  if SaveData() then
  begin
    JYLen.Text:='';
    JYPZ.Text:='';
    JYJZ.Text:='';
    JYKG.Text:='';
    PrtData(Trim(JY_CP.fieldbyname('JYID').AsString));
  end;
  with ADOQueryPrint do
  begin
    close;
    sql.Clear;
    sql.Add('select AA.*,CQty=SJZQty-MQQty from (select A.GangNo,A.NewSubid,Sum(A.JYLen) SJZQty ');
    sql.Add(',MQQty=(select Sum(B.JYLen) from JTJY_New B where B.NewSubid=A.NewSubid ');
    sql.Add('and B.JYType=''成品'' and B.GangNo=A.GangNo)');
    sql.Add('from JTJY_New A where NewSubid='''+Trim(FSubid)+''' ');
    sql.Add('and JYType=''面布'' ');
    SQL.Add('group by A.GangNo,A.NewSubid) AA');
    //ShowMessage(sql.Text);
    open;
  end;
  SCreateCDS20(ADOQueryPrint,JY_HZ);
  SInitCDSData20(ADOQueryPrint,JY_HZ);
  Button2.Enabled:=true;
  JYLen.SetFocus;
end;

procedure TfrmMJManageNewFDNewSF_New.TV2CellClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
var i:Integer;
begin
  with ADOTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from JTJY_New where JYID='''+Trim(JY_CP.fieldbyname('JYID').AsString)+'''');
    Open;
  end;
  JYLen.Text:=FloatToStr(ADOTmp.fieldbyname('JYLen').asfloat);
  JYPZ.Text:=FloatToStr(ADOTmp.fieldbyname('JYPZ').asfloat);
  JYJZ.Text:=FloatToStr(ADOTmp.fieldbyname('JYJZ').asfloat);
  JYKG.Text:=FloatToStr(ADOTmp.fieldbyname('JYKG').asfloat);
  edit1.Text:=Trim(ADOTmp.fieldbyname('GangNo').AsString);
  with Panel8 do
  begin
    for i:=0 to ControlCount-1 do
    begin
      if Controls[i] is TSpeedButton then
      begin
        if TSpeedButton(Controls[i]).Caption=Trim(Edit1.Text) then
        begin
          TSpeedButton(Controls[i]).Font.Color:=clRed;
        end
        else
        TSpeedButton(Controls[i]).Font.Color:=clWindowText;
      end;
    end;
  end;
end;

procedure TfrmMJManageNewFDNewSF_New.SpeedButton28Click(Sender: TObject);
var
  i:Integer;
begin
  with Panel5 do
  begin
    for i:=0 to ControlCount-1 do
    begin
      if Controls[i] is TSpeedButton then
      begin
        TSpeedButton(Controls[i]).Font.Color:=clWindowText;
      end;
    end;
  end;
  TSpeedButton(Sender).Font.Color:=clRed;
  GangNo.Text:=TSpeedButton(Sender).Caption;
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from JTJY_New where NewSubid='''+Trim(FSubid)+'''');
    sql.Add(' and GangNo='''+Trim(GangNo.Text)+'''');
    SQL.Add(' and JYType='''+trim(Label2.Caption)+'''');
    Open;
  end;
  SCreateCDS20(ADOCmd,Order_MJ);
  SInitCDSData20(ADOCmd,Order_MJ);
end;

end.
