unit U_MLManage_LRTJ;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, ADODB, DBClient, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxControls, cxGridCustomView, cxGrid, ComCtrls, ToolWin,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxContainer, cxTextEdit,
  cxMaskEdit, cxDropDownEdit, cxCalendar, StdCtrls, ExtCtrls, BtnEdit,
  cxButtonEdit,jpeg,U_SLT,Math, Menus, RM_Common, RM_Class, RM_GridReport,
  RM_System, RM_Dataset,U_SLT_PBSK,U_BPZdy_LRSHK, IdBaseComponent,
  IdComponent, IdTCPConnection, IdTCPClient, IdFTP;

type
  TfrmMLManage_LRTJ = class(TForm)
    CDS_Sub: TClientDataSet;
    ADOQueryMain: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Panel1: TPanel;
    Order_GX: TClientDataSet;
    DataSource1: TDataSource;
    Panel4: TPanel;
    OrdBP_SH: TClientDataSet;
    GSNo: TLabel;
    MLNo: TLabel;
    Ord_HX: TClientDataSet;
    GYName: TLabel;
    Panel9: TPanel;
    Label2: TLabel;
    MLName: TEdit;
    MLMF: TEdit;
    MLKZ: TEdit;
    GYLX: TLabel;
    GYLXName: TLabel;
    Label1: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label8: TLabel;
    Button2: TButton;
    MLCF: TEdit;
    Button1: TButton;
    ScrollBox2: TScrollBox;
    adoqueryPicture: TADOQuery;
    Panel7: TPanel;
    Panel3: TPanel;
    Label11: TLabel;
    BPCode: TLabel;
    BPName: TLabel;
    BPJS: TLabel;
    BPMF: TLabel;
    BPKZ: TLabel;
    BPMiDu: TLabel;
    BPCF: TLabel;
    CPMiDu: TLabel;
    ToolBar3: TToolBar;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    Panel5: TPanel;
    Label5: TLabel;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    V1Ssel: TcxGridDBColumn;
    V1BPCode: TcxGridDBColumn;
    V1BPName: TcxGridDBColumn;
    V1BPMF: TcxGridDBColumn;
    V1BPKZ: TcxGridDBColumn;
    V1BPJS: TcxGridDBColumn;
    v1BPMiDu: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Panel2: TPanel;
    ToolBar2: TToolBar;
    TAdd: TToolButton;
    ToolButton5: TToolButton;
    Panel6: TPanel;
    Label6: TLabel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    V2XHNo: TcxGridDBColumn;
    VGYCode: TcxGridDBColumn;
    V2GYName: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel8: TPanel;
    ScrollBox1: TScrollBox;
    Button4: TButton;
    RMDB_Main: TRMDBDataSet;
    RM1: TRMGridReport;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    Button5: TButton;
    ToolButton1: TToolButton;
    SaveDialog1: TSaveDialog;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    procedure FormShow(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure TAddClick(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure MLNameChange(Sender: TObject);
    procedure MLMFChange(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button4Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure Panel42MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
  private
    lstPat: TStringList;
    Fint,my,mz:Integer;
    procedure InitSubGrid();
    function  SaveData():Boolean;
    procedure BHCZhu();
    procedure SavePrint(var CP,TP:string);
    procedure CreThumb(AJPeg:TJPEGImage;Image1:TImage;Width, Height: Integer);
    { Private declarations }
  public
    FCYID,FCYCode,FCPID,FCPNO,FBPID,FGYName,FML,FGYLX,FGSNo,FHXBPID,FGSName,FSKID,FBPIDSH:String;
    FGYLXName,PatFile:string;
    viewFlag:Boolean;
    procedure initimageSH();
    procedure initimage();
    procedure imagePL(FFF:Integer);
    { Public declarations }
  end;

var
  frmMLManage_LRTJ: TfrmMLManage_LRTJ;
  Mach1: array of TfrmSlt;
  Mach: array of TfrmSLT_PBSK;
implementation
uses
  U_DataLink,U_Fun,U_BPZdy_LR,U_BPZdy_SH,U_MLMange_ADDCX, U_BPZdy_HXK,U_BPZdy_LRPB,U_BPZdy_LRGX,
  U_BPZdy_PanelCX, U_BPZdy_YT,U_BPZdy_Panel,U_BPZdy_LRTJ,U_FileUp_TPSH,U_MLMangeRS_Sel;

{$R *.dfm}
procedure TfrmMLManage_LRTJ.initimageSH();
var
  jpg:TJpegImage;
  myStream:TADOBlobStream;
begin
  
end;

procedure TfrmMLManage_LRTJ.SavePrint(var CP,TP:string);
var maxno,MaxSub:string;
   fPrintFile,Txt,fImagePath:string;
   Porderno:string;
   i,j:Integer;
   Moudle: THandle;
   Makebar:TMakebar;
   Mixtext:TMixtext;
begin
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    SQL.Add('select * from BP_DY ');
    sql.Add('where MLID='''+trim(CP)+'''');
    sql.Add(' and TFID='''+trim(TP)+'''');
    Open;
  end;
  if ADOQueryCmd.IsEmpty then
  begin
    if GetLSNo(ADOQueryTemp,maxno,'DY','BP_DY',3,1)=False then
    begin
      Application.MessageBox('取最大号失败!','提示',0);
      Exit;
    end;
  end
  else
  begin
    maxno:=Trim(ADOQueryCmd.fieldbyname('DYID').AsString);
  end;
  with ADOQueryCmd do
  begin
    if IsEmpty then
    begin
      Append;
      FieldByName('filler').Value:=Trim(DName);
      FieldByName('DYID').Value:=Trim(maxno);
    end
    else
    begin
      edit;
      FieldByName('laster').Value:=Trim(DName);
      FieldByName('lasttime').Value:=SGetServerDate(ADOQueryTemp);
    end;
    FieldByName('MLID').Value:=Trim(CP);
    FieldByName('TFID').Value:=Trim(TP);
    post;
  end;
  //////////记录
  if GetLSNo(ADOQueryTemp,MaxSub,'JL','BP_DY_JL',3,1)=False then
  begin
    Application.MessageBox('取最大号失败!','提示',0);
    Exit;
  end;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    SQL.Add('select * from BP_DY_JL ');
    sql.Add('where 1=2');
    Open;
  end;
  with ADOQueryCmd do
  begin
    Append;
    FieldByName('filler').Value:=Trim(DName);
    FieldByName('JLID').Value:=Trim(MaxSub);
    FieldByName('DYID').Value:=Trim(maxno);
    FieldByName('MLID').Value:=Trim(CP);
    FieldByName('TFID').Value:=Trim(TP);
    post;
  end;
  //打印
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\单面料标签染色.rmf';
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.*,C.DYID from BP_DY C ');
    sql.Add('inner join ML_Manage A on A.MLID=C.MLID ');
    sql.Add('inner join XD_File D on D.XFID=C.TFID ');
    sql.Add('inner join SH_Base B on B.WBID=D.CYID ');
    sql.Add(' Where C.DYID='''+Trim(maxno)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryPrint,CDS_Print);
  SInitCDSData20(ADOQueryPrint,CDS_Print);
  try
    Moudle:=LoadLibrary('MakeQRBarcode.dll');
    @Makebar:=GetProcAddress(Moudle,'Make');
    @Mixtext:=GetProcAddress(Moudle,'MixText');
    Txt:=Trim(CDS_Print.fieldbyname('DYID').AsString);
    fImagePath:=ExtractFilePath(Application.ExeName)+'image\temp.bmp';
    if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName)+'image')) then
      CreateDirectory(pchar(ExtractFilePath(Application.ExeName)+'image'),nil);
    if FileExists(fImagePath) then DeleteFile(fImagePath);
    Makebar(pchar(Txt),Length(Txt),3,3,0,PChar(fImagePath),3);
  except
    application.MessageBox('条形码生成失败！','提示信息',MB_ICONERROR);
    exit;
  end;
  if FileExists(fPrintFile) then
  begin
    RMVariables['QRBARCODE']:=fImagePath;
    RM1.LoadFromFile(fPrintFile);
    //RM1.ShowReport;
    RM1.PrintReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\单面料标签染色.rmf'),'提示',0);
    exit;
  end;
end;

procedure TfrmMLManage_LRTJ.InitSubGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from ML_Manage where MLID='''+trim(FCYID)+'''');
      Open;
    end;
    if ADOQueryMain.IsEmpty=false then
    begin
      GSNo.Caption:=Trim(ADOQueryMain.FieldByName('GSNo').asstring);
      GYName.Caption:=Trim(ADOQueryMain.FieldByName('GYName').asstring);
      MLNo.Caption:=Trim(ADOQueryMain.FieldByName('MLNo').asstring);
      GYLX.Caption:=Trim(ADOQueryMain.FieldByName('GYLX').asstring);
      GYLXName.Caption:=Trim(ADOQueryMain.FieldByName('GYLXName').asstring);
      //MLDLNo.Caption:=Trim(ADOQueryMain.FieldByName('MLDLNo').asstring);
      SCSHDataNew(ADOQueryMain,Panel9,2);
      FBPID:=Trim(ADOQueryMain.FieldByName('MBPID').Value);
      FSKID:=Trim(ADOQueryMain.FieldByName('MLDLNo').AsString);
    end;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.*,isnull(B.BPCode,C.MLNo) BPCode,isnull(B.BPName,C.MLName) BPName');
      sql.Add(' ,isnull(B.BPMF,C.MLMF) BPMF,isnull(B.BPKZ,C.MLKZ) BPKZ');
      sql.Add(',B.BPJS,B.BPWS,B.BPMiDu,B.BPUnit,B.BPCF,B.CPMiDu,B.SKID');
      SQL.Add(' from ML_Manage_PB A left join BP_ZDY B on B.BPID=A.BPID ');
      sql.Add(' left join ML_Manage C on C.MLID=A.BPID');
      sql.Add('where A.MLID='''+Trim(FCYID)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Sub);
    SInitCDSData20(ADOQueryMain,CDS_Sub);
    BPCode.caption:=Trim(CDS_Sub.fieldbyname('BPCode').AsString);
    BPName.caption:=Trim(CDS_Sub.fieldbyname('BPName').AsString);
    BPMF.Caption:=Trim(CDS_Sub.fieldbyname('BPMF').AsString);
    BPKZ.Caption:=Trim(CDS_Sub.fieldbyname('BPKZ').AsString);
    BPCF.Caption:=Trim(CDS_Sub.fieldbyname('BPCF').AsString);
    BPJS.Caption:=Trim(CDS_Sub.fieldbyname('BPJS').AsString);
    BPMiDu.Caption:=Trim(CDS_Sub.fieldbyname('BPMiDu').AsString);
    CPMiDu.Caption:=Trim(CDS_Sub.fieldbyname('CPMiDu').AsString);
    if CDS_Sub.fieldbyname('SKID').AsString='' then
    FBPIDSH:=Trim(CDS_Sub.fieldbyname('BPCode').AsString)
    else
    FBPIDSH:=Trim(CDS_Sub.fieldbyname('SKID').AsString);
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.*,B.* from ML_Manage_GX A ');
      SQL.Add('inner join GY_ZDY B on B.GYID=A.GYID ');
      sql.Add('where A.MLID='''+Trim(FCYID)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_GX);
    SInitCDSData20(ADOQueryMain,Order_GX);
    if FCYID='' then
    begin
      with ADOQueryMain do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from GY_ZDY ');
        sql.Add('where GYName='''+Trim(FGYName)+''' and GYflag=''GXName''');
        Open;
      end;
      MLNo.Caption:=ADOQueryMain.fieldbyname('ZGYCode').AsString;
      FML:=ADOQueryMain.fieldbyname('ZGYCode').AsString;
    end else
    begin
      with ADOQueryMain do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from GY_ZDY ');
        sql.Add('where GYName='''+Trim(FGYName)+''' and GYflag=''GXName''');
        Open;
      end;
      FML:=ADOQueryMain.fieldbyname('ZGYCode').AsString;
    end;
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmMLManage_LRTJ.FormShow(Sender: TObject);
var
  fsj:String;
begin
  ReadCxGrid('烫金面料管理1',Tv1,'样品管理');
  ReadCxGrid('烫金面料管理2',Tv2,'样品管理');
  InitSubGrid();
  initimageSH();
  initimage();
  Button1.Caption:='保存';
  if viewFlag=True then
  begin
    ToolButton2.Visible:=False;
    ToolButton3.Visible:=False;
    TAdd.Visible:=False;
    ToolButton5.Visible:=False;
    Button1.Visible:=False;
  end;
end;

function TfrmMLManage_LRTJ.SaveData():Boolean;
var
  maxId,maxno,FXDID:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if FCYID='' then
    begin
      if GetLSNo(ADOQueryCmd,maxno,'ML','ML_Manage',3,1)=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!','提示',0);
        Exit;
      end;
      if PatFile<>'' then
      begin
        if GetLSNo(ADOQueryCmd,FXDID,'YT','XD_File',4,1)=False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取图片最大号失败！','提示',0);
          Exit;
        end;
      end;
    end
    else
    begin
      maxno:=Trim(FCYID);
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from ML_Manage where MLID='''+Trim(FCYID)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if isempty then
      begin
        Append;
        fieldbyname('GYType').Value:=Trim(FGYName);
        FieldByName('GYTypeJC').Value:=Trim(FML);
      end
      else
      begin
        Edit;
      end;
      //ShowMessage(FBPID);
      FieldByName('MLID').Value:=Trim(maxno);
      FieldByName('MBPID').Value:=Trim(FBPID);
      FieldByName('MLType').Value:='烫金面料';
      FieldByName('GSNo').Value:=Trim(GSNo.Caption);
      FieldByName('GYName').Value:=Trim(GYName.Caption);
      FieldByName('MLNo').Value:=Trim(MLNo.Caption);
      FieldByName('GYLX').Value:=Trim(GYLX.Caption);
      FieldByName('GYLXName').Value:=Trim(GYLXName.Caption);
      //SSetsaveSqlNew(ADOQueryCmd,'ML_Manage',panel9,2);
      RTSetsavedata(ADOQueryCmd,'ML_Manage',panel9,2);
      FieldByName('MLJWMD').Value:=Trim(CPMiDu.Caption);
      FieldByName('MLDLNo').Value:=Trim(FSKID);
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from ML_Manage where GSNo='''+Trim(GSNo.Caption)+'''');
      Open;
    end;
    if ADOQueryCmd.RecordCount>1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('工艺编号重复!','提示',0);
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from ML_Manage where MLNo='''+Trim(MLNo.Caption)+'''');
      sql.add(' and GYLX='''+trim(GYLX.Caption)+'''');
      Open;
    end;
    if ADOQueryCmd.RecordCount>1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('产品编号+工艺路线重复!','提示',0);
      Exit;
    end;
    FCYID:=Trim(maxno);
    CDS_Sub.DisableControls;
    with CDS_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_Sub.fieldbyname('MPID').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxId,'MP','ML_Manage_PB',4,1)=False then
          begin
            Result:=False;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('区最大号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxId:=Trim(CDS_Sub.fieldbyname('MPID').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from ML_Manage_PB where MPID='''+Trim(maxId)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if isempty then
          begin
            Append;
          end else
          begin
            Edit;
          end;
          FieldByName('MPID').Value:=Trim(maxId);
          FieldByName('MLID').Value:=Trim(FCYID);
          FieldByName('BPID').Value:=Trim(CDS_Sub.fieldbyname('BPID').AsString);
          //FieldByName('SKID').Value:=Trim(FBPIDSH);
          Post;
        end;
        Next;
      end;
    end;
    CDS_Sub.EnableControls;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete ML_Manage_GX where MLID='''+Trim(FCYID)+'''');
      ExecSQL;
    end;
    Order_GX.DisableControls;
    with Order_GX do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_GX.fieldbyname('GXID').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxId,'GX','ML_Manage_GX',4,1)=False then
          begin
            Result:=False;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('区最大号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxId:=Trim(Order_GX.fieldbyname('GXID').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from ML_Manage_GX where GXID='''+Trim(maxId)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if isempty then
          begin
            Append;
          end else
          begin
            Edit;
          end;
          FieldByName('GXID').Value:=Trim(maxId);
          FieldByName('MLID').Value:=Trim(FCYID);
          fieldbyname('XHNo').Value:=Order_GX.fieldbyname('XHNo').Value;
          FieldByName('GYID').Value:=Trim(Order_GX.fieldbyname('GYID').AsString);
          Post;
        end;
        Next;
      end;
    end;
    Order_GX.EnableControls;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update BP_ZDY Set BPCF='''+Trim(MLCF.Text)+'''');
      sql.Add(' where isnull(BPCode,'''')='''+Trim(BPCode.Caption)+'''');
      sql.Add(' and isnull(BPType,'''')=''坯布'' ');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except;
    Result:=True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!','提示',0);
  end;
end;

procedure TfrmMLManage_LRTJ.BHCZhu();
var FBH,FCP,FLX,FLL:string;
    FBHName,FLXName,FLLName:string;
begin
  CDS_Sub.DisableControls;
  with CDS_Sub do
  begin
    First;
    while not eof do
    begin
      FBH:=FBH+trim(CDS_Sub.fieldbyname('BPCode').AsString);
      FBHName:=FBHName+trim(CDS_Sub.fieldbyname('BPName').AsString);
      Next;
    end;
  end;
  CDS_Sub.EnableControls;
  FCP:=FML+FBH;
  Order_GX.DisableControls;
  with Order_GX do
  begin
    First;
    while not eof do
    begin
      FLL:=FLL+trim(Order_GX.fieldbyname('GYCode').AsString);
      FLLName:=FLLName+trim(Order_GX.fieldbyname('GYName').AsString);
      if FLX='' then
      FLX:=trim(Order_GX.fieldbyname('GYCode').AsString)
      else
      FLX:=FLX+'+'+trim(Order_GX.fieldbyname('GYCode').AsString);
      if FLXName='' then
      FLXName:=trim(Order_GX.fieldbyname('GYName').AsString)
      else
      FLXName:=FLXName+'+'+trim(Order_GX.fieldbyname('GYName').AsString);
      Next;
    end;
  end;
  Order_GX.EnableControls;
  GSNo.Caption:=FBH+Trim(FLL);
  GYName.Caption:=FBHName+Trim(FLLName);
  MLNo.Caption:=Trim(FCP);
  if FLX<>'' then
  begin
    if FGYLX='' then
    GYLX.Caption:=Trim(FLX)
    else
    GYLX.Caption:=FGYLX+'+'+Trim(FLX);
  end
  else
  begin
    GYLX.Caption:=FGYLX;
  end;
  if FLXName<>'' then
  begin
    if FGYLXName='' then
    GYLXName.Caption:=Trim(FLXName)
    else
    GYLXName.Caption:=FGYLXName+'+'+Trim(FLXName);
  end
  else
  begin
    GYLXName.Caption:=FGYLXName;
  end;
end;

procedure TfrmMLManage_LRTJ.imagePL(FFF:Integer);
var
  i,j,x,p,x1,p1:Integer;
begin
  x:=0;
  p:=0;
  x1:=0;
  p1:=0;
  ScrollBox1.Refresh;
  ScrollBox2.Refresh;
  if FFF=1 then
  begin
    with ScrollBox1 do
    begin
      for i:=0 to ScrollBox1.ControlCount-1 do
      begin
        if Controls[i].Visible=True then
        begin
          if Controls[i] is TfrmSLT then
          begin
            if x<4*(p+1) then
            begin

            end
            else
            begin
              p:=p+1;
            end;
            TfrmSLT(Controls[i]).Left:=0+(x-4*p)*185;
            TfrmSLT(Controls[i]).Top:=p*143;
            x:=x+1;
          end;
        end else
        begin
          with ScrollBox2 do
          begin
            Controls[i].Visible:=True;
            if Controls[i] is TfrmSLT then
            begin
              if x1<3*(p1+1) then
              begin

              end
              else
              begin
                p1:=p1+1;
              end;
              TfrmSLT(Controls[i]).Left:=0+(x1-3*p1)*185;
              TfrmSLT(Controls[i]).Top:=p1*143;
              x1:=x1+1;
            end;
          end;
        end;
      end;
    end;
  end;
  if FFF=2 then
  BEGIN
    x:=0;
    p:=0;
    x1:=0;
    p1:=0;
    with ScrollBox2 do
    begin
      for i:=0 to ScrollBox2.ControlCount-1 do
      begin
        if Controls[i].Visible=True then
        begin
          if Controls[i] is TfrmSLT then
          begin
            if x1<3*(p1+1) then
              begin

              end
              else
              begin
                p1:=p1+1;
              end;
              TfrmSLT(Controls[i]).Left:=0+(x1-3*p1)*185;
              TfrmSLT(Controls[i]).Top:=p1*143;
              x1:=x1+1;

          end;
        end else
        begin
          with ScrollBox1 do
          begin
            Controls[i].Visible:=True;
            if Controls[i] is TfrmSLT then
            begin
              if x<4*(p+1) then
              begin

              end
              else
              begin
                p:=p+1;
              end;
              TfrmSLT(Controls[i]).Left:=0+(x-4*p)*185;
              TfrmSLT(Controls[i]).Top:=p*143;
              x:=x+1;
            end;
          end;
        end;
      end;
    end;
  end;

end;

procedure TfrmMLManage_LRTJ.initimage();
var
  i,j:integer;
  jpg:TJpegImage;
  myStream: TADOBlobStream;
begin
  j:=length(Mach);
  if j>0 then
  begin
    for i:=0 to j-1 do
    begin
      Mach[i].free;
    end;
    //Finalize(Mach);
  end;
  SetLength(Mach, 0);
  try
    with adoqueryPicture do
    begin
      close;
      sql.Clear;
      sql.Add(' select A.TFID,A.WBID,B.XFID,A.FilesOther,B.FileName,C.* from BP_ZDY C ');
      sql.Add(' inner join XD_File_TP B on C.SKID =B.SKID');
      SQL.Add(' inner join TP_File_TP A on B.CYID=A.WBID ');
      sql.Add(' where C.BPID='+quotedstr(trim(FBPID)));
      //ShowMessage(sql.Text);
      open;
    end;
    j:=adoqueryPicture.RecordCount;
    if j<1 then exit;
    adoqueryPicture.DisableControls;
    adoqueryPicture.First;
    SetLength(Mach, j);
    with adoqueryPicture do
    begin
      First;
      i:=0;
      while not eof do
      begin
        if triM(adoqueryPicture.fieldbyname('TFID').AsString)<>'' then
        begin
          jpg:=TJpegImage.Create();
          myStream:=tadoblobstream.Create(tblobfield(adoqueryPicture.fieldbyname('FilesOther')),bmread);
          jpg.LoadFromStream(myStream);
        end;
        Mach[i] := TfrmSLT_PBSK.Create(Self);
        Mach[i].Name:=trim(adoqueryPicture.fieldbyname('XFID').AsString);
        Mach[i].Parent := ScrollBox1;
        Mach[I].Left:=0+410*i;
        if triM(adoqueryPicture.fieldbyname('TFID').AsString)<>'' then
        begin
          Mach[i].Init(adoqueryPicture.fieldbyname('TFID').AsString,adoqueryPicture.fieldbyname('FileName').AsString,jpg);
        end;
        i:=i+1;
        Next;
      end;
    end;
    adoqueryPicture.EnableControls;
  finally
    jpg.free;
    //application.ProcessMessages;
  end;
end;

procedure TfrmMLManage_LRTJ.ToolButton2Click(Sender: TObject);
begin
  if CDS_Sub.IsEmpty=false then
  begin
    CDS_Sub.Delete;
    //Application.MessageBox('已存在坯布信息','提示');
   // exit;
  end;
  frmBPZDY_LRPB:=TfrmBPZDY_LRPB.create(self);
  with frmBPZDY_LRPB do
  begin
    flag:='PBName';
    flagName:='坯布';
    ViewFlag:=true;
    Panel4.Align:=alClient;
    ToolBar3.Visible:=false;
    Button1.Visible:=False;
    if ShowModal=1 then
    begin
      with CDS_Sub do
      begin
        append;
        FieldByName('BPCode').Value:=Trim(ClientDataSet1.fieldbyname('BPCode').AsString);
        FieldByName('BPID').Value:=Trim(ClientDataSet1.fieldbyname('BPID').AsString);
        FieldByName('BPName').Value:=Trim(ClientDataSet1.fieldbyname('BPName').AsString);
        FieldByName('BPMF').Value:=Trim(ClientDataSet1.fieldbyname('BPMF').AsString);
        FieldByName('BPKZ').Value:=Trim(ClientDataSet1.fieldbyname('BPKZ').AsString);
        FieldByName('BPCF').Value:=Trim(ClientDataSet1.fieldbyname('BPCF').AsString);
        FieldByName('BPJS').Value:=Trim(ClientDataSet1.fieldbyname('BPJS').AsString);
        FieldByName('BPWS').Value:=Trim(ClientDataSet1.fieldbyname('BPWS').AsString);
        FieldByName('BPMiDu').Value:=Trim(ClientDataSet1.fieldbyname('BPMiDu').AsString);
        FieldByName('CPMiDu').Value:=Trim(ClientDataSet1.fieldbyname('CPMiDu').AsString);
        FieldByName('SKID').Value:=trim(ClientDataSet1.fieldbyname('SKID').AsString);
        post;
      end;
      self.BPCode.caption:=Trim(ClientDataSet1.fieldbyname('BPCode').AsString);
      Self.BPName.caption:=Trim(ClientDataSet1.fieldbyname('BPName').AsString);
      Self.MLName.Text:=Trim(ClientDataSet1.fieldbyname('BPName').AsString);
      Self.MLCF.Text:=Trim(ClientDataSet1.fieldbyname('BPCF').AsString);
      Self.BPMF.Caption:=Trim(ClientDataSet1.fieldbyname('BPMF').AsString);
      Self.BPKZ.Caption:=Trim(ClientDataSet1.fieldbyname('BPKZ').AsString);
      Self.BPCF.Caption:=Trim(ClientDataSet1.fieldbyname('BPCF').AsString);
      Self.BPJS.Caption:=Trim(ClientDataSet1.fieldbyname('BPJS').AsString);
      Self.BPMiDu.Caption:=Trim(ClientDataSet1.fieldbyname('BPMiDu').AsString);
      Self.CPMiDu.Caption:=Trim(ClientDataSet1.fieldbyname('CPMiDu').AsString);
      Self.FBPID:=Trim(ClientDataSet1.fieldbyname('BPID').AsString);
      if ClientDataSet1.fieldbyname('SKID').AsString='' then
        self.FBPIDSH:=Trim(ClientDataSet1.fieldbyname('BPCode').AsString)
      else
        self.FBPIDSH:=Trim(ClientDataSet1.fieldbyname('SKID').AsString);
      BHCZhu();
      with ADOQueryMain do
      begin
        Close;
        sql.Clear;
        sql.Add('select A.*,B.* from RT_PTColor A ');
        sql.Add('inner join BP_ColZDY B on B.PTID=A.PTID ');
        sql.Add('where isnull(B.SKID,'''')='''+trim(self.FBPIDSH)+''' and isnull(B.SKID,'''')<>''''');
        sql.Add(' order by B.ColNo');
        Open;
      end;
      SCreateCDS20(ADOQueryMain,OrdBP_SH);
      SInitCDSData20(ADOQueryMain,OrdBP_SH);
      initimageSH();
      initimage();
    end;
    free;
  end;
end;

procedure TfrmMLManage_LRTJ.ToolButton3Click(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then exit;
  if CDS_Sub.fieldbyname('MPID').AsString<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗','提示',1)=2 then exit;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete from ML_Manage_PB where MPID='''+trim(CDS_Sub.fieldbyname('MPID').AsString)+'''');
      ExecSQL;
    end;
  end;
  CDS_Sub.Delete;
  BHCZhu();
end;

procedure TfrmMLManage_LRTJ.ToolButton5Click(Sender: TObject);
var FGYName:string;
begin
  if Order_GX.IsEmpty then exit;
  if Order_GX.fieldbyname('GXID').AsString<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗','提示',1)=2 then exit;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete from ML_Manage_GX where GXID='''+trim(Order_GX.fieldbyname('GXID').AsString)+'''');
      ExecSQL;
    end;
  end;
  Order_GX.Delete;
  BHCZhu();
end;

procedure TfrmMLManage_LRTJ.TAddClick(Sender: TObject);
var FGYLX,FGSNo:string;
begin
  frmBPZDY_LRTJ:=TfrmBPZDY_LRTJ.create(self);
  with frmBPZDY_LRTJ do
  begin
    flag:='TJName';
    flagName:='烫金';
    ViewFlag:=true;
    FGYName:=Self.FGYName;
    Label12.Visible:=true;
    DASH.Visible:=true;
    cxGrid3.Align:=alClient;
    Panel2.Visible:=false;
    Panel4.Visible:=false;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.* from GY_ZDY A where 1=2');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,ML_GX);
    Order_GX.DisableControls;
    with Order_GX do
    begin
      First;
      while not Eof do
      begin
        with ML_GX do
        begin
          Append;
          FieldByName('XHNo').Value:=Order_GX.fieldbyname('XHNo').Value;
          FieldByName('ZGYCode').Value:=Trim(Order_GX.fieldbyname('ZGYCode').AsString);
          FieldByName('ZGYName').Value:=Trim(Order_GX.fieldbyname('ZGYName').AsString);
          FieldByName('GYCode').Value:=Trim(Order_GX.fieldbyname('GYCode').AsString);
          FieldByName('GYName').Value:=Trim(Order_GX.fieldbyname('GYName').AsString);
          FieldByName('GYID').Value:=Trim(Order_GX.fieldbyname('GYID').AsString);
          Post;
        end;
        Next;
      end;
    end;
    Order_GX.EnableControls;
    if ShowModal=1 then
    begin
      with ADOQueryMain do
      begin
        Close;
        SQL.Clear;
        sql.Add(' select A.*,B.* from ML_Manage_GX A ');
        SQL.Add('inner join GY_ZDY B on B.GYID=A.GYID ');
        sql.Add('where 1=2 ');
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_GX);
      SInitCDSData20(ADOQueryMain,Order_GX);
      ML_GX.DisableControls;
      with ML_GX do
      begin
        first;
        while not eof do
        begin
          with Order_GX do
          begin
            append;
            FieldByName('XHNo').Value:=ML_GX.fieldbyname('XHNo').Value;
            FieldByName('ZGYCode').Value:=Trim(ML_GX.fieldbyname('ZGYCode').AsString);
            FieldByName('ZGYName').Value:=Trim(ML_GX.fieldbyname('ZGYName').AsString);
            FieldByName('GYCode').Value:=Trim(ML_GX.fieldbyname('GYCode').AsString);
            FieldByName('GYName').Value:=Trim(ML_GX.fieldbyname('GYName').AsString);
            FieldByName('GYID').Value:=Trim(ML_GX.fieldbyname('GYID').AsString);
            post;
          end;
          Next;
        end;
      end;
      ML_GX.EnableControls;
      BHCZhu();
    end;
    free;
  end;
end;

procedure TfrmMLManage_LRTJ.ToolButton4Click(Sender: TObject);
begin
  if CDS_Sub.IsEmpty=false then
  begin
    Application.MessageBox('已存在面料信息','提示');
    exit;
  end;
  frmMLMange_ADDCX:=TfrmMLMange_ADDCX.Create(self);
  with frmMLMange_ADDCX do
  begin
    if ShowModal=1 then
    begin
      with CDS_Sub do
      begin
        Append;
        FieldByName('BPCode').Value:=CDS_Main.fieldbyname('MLNo').AsString;
        FieldByName('BPID').Value:=Trim(CDS_Main.fieldbyname('MLID').AsString);
        FieldByName('BPName').Value:=CDS_Main.fieldbyname('MLName').AsString;
        FieldByName('BPMF').Value:=CDS_Main.fieldbyname('MLMF').AsString;
        FieldByName('BPKZ').Value:=CDS_Main.fieldbyname('MLKZ').AsString;
        post;
      end;
      GYLX.Caption:=CDS_Main.fieldbyname('GYLX').AsString;
      FGYLX:=CDS_Main.fieldbyname('GYLX').AsString;
      GSNo.Caption:=CDS_Main.fieldbyname('GSNo').AsString;
      FGSNo:=CDS_Main.fieldbyname('GSNo').AsString;
      FGYLXName:=CDS_Main.fieldbyname('GYLXName').AsString;
      FGSName:=CDS_Main.fieldbyname('GYName').AsString;
      BHCZhu();
    end;
    free;
  end;
end;

procedure TfrmMLManage_LRTJ.Button1Click(Sender: TObject);
var FBH:string;
begin
  if CDS_Sub.IsEmpty then
  begin
    application.MessageBox('请选择坯布信息','提示');
    exit;
  end;
  if Trim(MLName.Text)='' then
  begin
    Application.MessageBox('品名不能为空!','提示',0);
    Exit;
  end;
  if Trim(MLKZ.Text)='' then
  begin
    Application.MessageBox('克重不能为空!','提示',0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    Finalize(Mach1);
    Finalize(Mach);
    ModalResult:=1;
  end;
end;

procedure TfrmMLManage_LRTJ.CreThumb(AJPeg:TJPEGImage;Image1:TImage;Width, Height: Integer);
var
  Bitmap: TBitmap;
  Ratio: Double;
  ARect: TRect;
  AHeight, AHeightOffset: Integer;
  AWidth, AWidthOffset: Integer;
begin
  Bitmap := TBitmap.Create;
  try
    Ratio := AJPeg.Width /AJPeg.Height;
    if Ratio > 1.333 then
    begin
      AHeight := Round(Width / Ratio);
      AHeightOffset := (Height - AHeight) div 2;
      AWidth := Width;
      AWidthOffset := 0;
    end
    else
    begin
      AWidth := Round(Height * Ratio);
      AWidthOffset := (Width - AWidth) div 2;
      AHeight := Height;
      AHeightOffset := 0;
    end;
    Bitmap.Width := Width;
    Bitmap.Height := Height;
    Bitmap.Canvas.Brush.Color := clBtnFace;
    Bitmap.Canvas.FillRect(Rect(0, 0, Width, Height));
    ARect := Rect(AWidthOffset, AHeightOffset, AWidth + AWidthOffset, AHeight + AHeightOffset);
    Bitmap.Canvas.StretchDraw(ARect, AJPeg);
    Image1.Picture.Assign(BitMap);
  finally
    Bitmap.Free;
  end;
end;

procedure TfrmMLManage_LRTJ.Button2Click(Sender: TObject);
begin
  WriteCxGrid('烫金面料管理1',Tv1,'样品管理');
  WriteCxGrid('烫金面料管理2',Tv2,'样品管理');
  close;
end;

procedure TfrmMLManage_LRTJ.MLNameChange(Sender: TObject);
begin
  if Trim(FCYID)<>'' then
  begin
    Button1.Caption:='保存改'
  end;
end;

procedure TfrmMLManage_LRTJ.MLMFChange(Sender: TObject);
begin
  if Trim(FCYID)<>'' then
  begin
    Button1.Caption:='保存改'
  end;
end;

procedure TfrmMLManage_LRTJ.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Finalize(Mach1);
  Finalize(Mach);
end;

procedure TfrmMLManage_LRTJ.Button4Click(Sender: TObject);
var i,j,x:Integer;
begin
  i:=0;
  j:=0;
  frmBPZDY_PanelCX:=TfrmBPZDY_PanelCX.create(self);
  with frmBPZDY_PanelCX do
  begin
    FColorFlag:=Trim(self.FBPIDSH);
    FBPID:=Trim(self.FBPID);
    FMLID:=Trim(self.FCYID);
    if ShowModal=1 then
    begin

    end;
    free;
  end;
end;

procedure TfrmMLManage_LRTJ.N1Click(Sender: TObject);
var
  i:Integer;
begin
  //ShowMessage(FPT);
  FrmBPZdy_YT:=TFrmBPZdy_YT.create(Self);
  with FrmBPZdy_YT do
  begin
    Panel1.Color:=StrToInt('$'+OrdBP_SH.fieldbyname('ColDaiMa').Value);
    Label1.Caption:=Trim(OrdBP_SH.fieldbyname('PTID').AsString);
    Label2.Caption:=Trim(OrdBP_SH.fieldbyname('ColNameEng').AsString);
    Label3.Caption:=Trim(OrdBP_SH.fieldbyname('ColName').AsString);
    Label4.Caption:=Trim(OrdBP_SH.fieldbyname('ColNameKH').AsString);
    Label5.Visible:=true;
    Label5.Caption:=Trim(OrdBP_SH.fieldbyname('ColNo').AsString);
    if showmodal=1 then
    begin

    end;
    free;
  end;
end;


procedure TfrmMLManage_LRTJ.Panel42MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  OrdBP_SH.Locate('PTID',TPanel(Sender).Hint,[]);
end;

procedure TfrmMLManage_LRTJ.FormCreate(Sender: TObject);
begin
  lstPat := TStringList.Create;
end;

procedure TfrmMLManage_LRTJ.ToolButton1Click(Sender: TObject);
begin
  frmMLMangeRS_Sel:=TfrmMLMangeRS_Sel.Create(self);
  with frmMLMangeRS_Sel do
  begin
    if ShowModal=1 then
    begin
      {with CDS_Sub do
      begin
        append;
        FieldByName('BPCode').Value:=Trim(ClientDataSet1.fieldbyname('BPCode').AsString);
        FieldByName('BPID').Value:=Trim(ClientDataSet1.fieldbyname('BPID').AsString);
        FieldByName('BPName').Value:=Trim(ClientDataSet1.fieldbyname('BPName').AsString);
        FieldByName('BPMF').Value:=Trim(ClientDataSet1.fieldbyname('BPMF').AsString);
        FieldByName('BPKZ').Value:=Trim(ClientDataSet1.fieldbyname('BPKZ').AsString);
        FieldByName('BPCF').Value:=Trim(ClientDataSet1.fieldbyname('BPCF').AsString);
        FieldByName('BPJS').Value:=Trim(ClientDataSet1.fieldbyname('BPJS').AsString);
        FieldByName('BPWS').Value:=Trim(ClientDataSet1.fieldbyname('BPWS').AsString);
        FieldByName('BPMiDu').Value:=Trim(ClientDataSet1.fieldbyname('BPMiDu').AsString);
        FieldByName('CPMiDu').Value:=Trim(ClientDataSet1.fieldbyname('CPMiDu').AsString);
        FieldByName('SKID').Value:=trim(ClientDataSet1.fieldbyname('SKID').AsString);
        post;
      end;
      self.BPCode.caption:=Trim(ClientDataSet1.fieldbyname('BPCode').AsString);
      Self.BPName.caption:=Trim(ClientDataSet1.fieldbyname('BPName').AsString);
      Self.MLName.Text:=Trim(ClientDataSet1.fieldbyname('BPName').AsString);
      Self.MLCF.Text:=Trim(ClientDataSet1.fieldbyname('BPCF').AsString);
      Self.BPMF.Caption:=Trim(ClientDataSet1.fieldbyname('BPMF').AsString);
      Self.BPKZ.Caption:=Trim(ClientDataSet1.fieldbyname('BPKZ').AsString);
      Self.BPCF.Caption:=Trim(ClientDataSet1.fieldbyname('BPCF').AsString);
      Self.BPJS.Caption:=Trim(ClientDataSet1.fieldbyname('BPJS').AsString);
      Self.BPMiDu.Caption:=Trim(ClientDataSet1.fieldbyname('BPMiDu').AsString);
      Self.CPMiDu.Caption:=Trim(ClientDataSet1.fieldbyname('CPMiDu').AsString);
      Self.FBPID:=Trim(ClientDataSet1.fieldbyname('BPID').AsString);
      if ClientDataSet1.fieldbyname('SKID').AsString='' then
        self.FBPIDSH:=Trim(ClientDataSet1.fieldbyname('BPCode').AsString)
      else
        self.FBPIDSH:=Trim(ClientDataSet1.fieldbyname('SKID').AsString);
      BHCZhu();
      initimageSH();
      initimage(); }
    end;
    free;
  end;
end;

end.
