unit U_MLManage_LRRS_Col;

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;

type
  TfrmMLManage_LRRS_Col = class(TForm)
    CDS_Sub: TClientDataSet;
    ADOQueryMain: TADOQuery;
    DataSource2: TDataSource;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Panel1: TPanel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Order_GX: TClientDataSet;
    DataSource1: TDataSource;
    Panel4: TPanel;
    OrdBP_SH: TClientDataSet;
    DataSource3: TDataSource;
    cxGridPopupMenu3: TcxGridPopupMenu;
    GSNo: TLabel;
    Ord_HX: TClientDataSet;
    DataSource4: TDataSource;
    cxGridPopupMenu4: TcxGridPopupMenu;
    GYName: TLabel;
    Panel9: TPanel;
    Label1: TLabel;
    Label3: TLabel;
    ScrollBox2: TScrollBox;
    adoqueryPicture: TADOQuery;
    RMDB_Main: TRMDBDataSet;
    RM1: TRMGridReport;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    Panel42: TPanel;
    Label79: TLabel;
    Label80: TLabel;
    Panel43: TPanel;
    Label81: TLabel;
    Label82: TLabel;
    Panel44: TPanel;
    Label83: TLabel;
    Label84: TLabel;
    Panel45: TPanel;
    Label85: TLabel;
    Label86: TLabel;
    Panel46: TPanel;
    Label87: TLabel;
    Label88: TLabel;
    Panel47: TPanel;
    Label89: TLabel;
    Label90: TLabel;
    Panel48: TPanel;
    Label91: TLabel;
    Label92: TLabel;
    Panel49: TPanel;
    Label93: TLabel;
    Label94: TLabel;
    Panel50: TPanel;
    Label95: TLabel;
    Label96: TLabel;
    Panel51: TPanel;
    Label97: TLabel;
    Label98: TLabel;
    Panel52: TPanel;
    Label99: TLabel;
    Label100: TLabel;
    Panel53: TPanel;
    Label101: TLabel;
    Label102: TLabel;
    Panel54: TPanel;
    Label103: TLabel;
    Label104: TLabel;
    Panel55: TPanel;
    Label105: TLabel;
    Label106: TLabel;
    Panel56: TPanel;
    Label107: TLabel;
    Label108: TLabel;
    Panel57: TPanel;
    Label109: TLabel;
    Label110: TLabel;
    Panel58: TPanel;
    Label111: TLabel;
    Label112: TLabel;
    Panel59: TPanel;
    Label113: TLabel;
    Label114: TLabel;
    Panel60: TPanel;
    Label115: TLabel;
    Label116: TLabel;
    Panel61: TPanel;
    Label117: TLabel;
    Label118: TLabel;
    Panel62: TPanel;
    Label119: TLabel;
    Label120: TLabel;
    Panel63: TPanel;
    Label121: TLabel;
    Label122: TLabel;
    Panel64: TPanel;
    Label123: TLabel;
    Label124: TLabel;
    Panel65: TPanel;
    Label125: TLabel;
    Label126: TLabel;
    Panel66: TPanel;
    Label127: TLabel;
    Label128: TLabel;
    Panel67: TPanel;
    Label129: TLabel;
    Label130: TLabel;
    Panel68: TPanel;
    Label131: TLabel;
    Label132: TLabel;
    Panel69: TPanel;
    Label133: TLabel;
    Label134: TLabel;
    Panel70: TPanel;
    Label135: TLabel;
    Label136: TLabel;
    Panel71: TPanel;
    Label137: TLabel;
    Label138: TLabel;
    Panel72: TPanel;
    Label139: TLabel;
    Label140: TLabel;
    Panel73: TPanel;
    Label141: TLabel;
    Label142: TLabel;
    Panel74: TPanel;
    Label143: TLabel;
    Label144: TLabel;
    Panel75: TPanel;
    Label145: TLabel;
    Label146: TLabel;
    Panel76: TPanel;
    Label147: TLabel;
    Label148: TLabel;
    Panel77: TPanel;
    Label149: TLabel;
    Label150: TLabel;
    Panel78: TPanel;
    Label151: TLabel;
    Label152: TLabel;
    Panel79: TPanel;
    Label153: TLabel;
    Label154: TLabel;
    Panel80: TPanel;
    Label155: TLabel;
    Label156: TLabel;
    Panel81: TPanel;
    Label157: TLabel;
    Label158: TLabel;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    Panel10: TPanel;
    Label7: TLabel;
    Label12: TLabel;
    Panel11: TPanel;
    Label13: TLabel;
    Label14: TLabel;
    Panel12: TPanel;
    Label15: TLabel;
    Label16: TLabel;
    Panel13: TPanel;
    Label17: TLabel;
    Label18: TLabel;
    Panel14: TPanel;
    Label19: TLabel;
    Label20: TLabel;
    Panel15: TPanel;
    Label21: TLabel;
    Label22: TLabel;
    Panel16: TPanel;
    Label23: TLabel;
    Label24: TLabel;
    Panel17: TPanel;
    Label25: TLabel;
    Label26: TLabel;
    Panel18: TPanel;
    Label27: TLabel;
    Label28: TLabel;
    Panel19: TPanel;
    Label29: TLabel;
    Label30: TLabel;
    Label8: TLabel;
    MLName: TLabel;
    GYLXName: TLabel;
    Label10: TLabel;
    Button2: TButton;
    Button4: TButton;
    Label2: TLabel;
    MLMF: TLabel;
    MLKZ: TLabel;
    MLCF: TLabel;
    SY: TButton;
    XY: TButton;
    label554: TLabel;
    procedure FormShow(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure Button2Click(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 XYClick(Sender: TObject);
    procedure SYClick(Sender: TObject);
    procedure Panel42DblClick(Sender: TObject);
  private
    Fint,my,mz:Integer;
    procedure InitSubGrid();
    procedure SavePrint(var CP,TP:string);
    { Private declarations }
  public
    FCYID,FCYCode,FCPID,FCPNO,FBPID,FGYName,FML,FGYLX,FGSNo,FHXBPID,FGSName,FSKID,FBPIDSH:String;
    FGYLXName,FZ:string;
    viewFlag:Boolean;
    procedure initimageSH();
    { Public declarations }
  end;

var
  frmMLManage_LRRS_Col: TfrmMLManage_LRRS_Col;
  Mach1: array of TfrmSlt;
  Mach: array of TfrmSLT_PBSK;
implementation
uses
  U_DataLink,U_Fun,U_BPZdy_PanelCX,U_BPZdy_Panel,U_ZDYHelp,
  U_MLMange_ADDCX, U_BPZdy_YT;

{$R *.dfm}
procedure TfrmMLManage_LRRS_Col.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_LRRS_Col.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);
      MLMF.Caption:=Trim(ADOQueryMain.FieldByName('MLMF').asstring);
      MLKZ.Caption:=Trim(ADOQueryMain.FieldByName('MLKZ').asstring);
      MLCF.Caption:=Trim(ADOQueryMain.FieldByName('MLCF').asstring);
      MLName.Caption:=Trim(ADOQueryMain.FieldByName('MLName').asstring)+'  '+Trim(ADOQueryMain.FieldByName('MLNo').asstring);
      GYLXName.Caption:=Trim(ADOQueryMain.FieldByName('GYLXName').asstring);
      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);
    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 ');
      if FZ<>'1' then
      begin
        sql.Add('where A.MLID='''+Trim(FCYID)+'''');
      end
      else
      begin
        sql.Add(' where 1=2');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_GX);
    SInitCDSData20(ADOQueryMain,Order_GX);
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*,B.* ');
      SQL.Add(',MXGS=(select count(C.PTID) from BP_ColZDY C where C.SKID=B.SKID and isnull(C.SKID,'''')<>'''') ');
      sql.Add('from RT_PTColor A ');
      sql.Add('inner join BP_ColZDY B on B.PTID=A.PTID ');
      sql.Add('where isnull(B.SKID,'''')='''+trim(FBPIDSH)+''' and isnull(B.SKID,'''')<>''''');
      sql.Add(' order by cast(B.ColNo as int)');
      Open;
    end;
    my:=1;
    mz:=ceil(ADOQueryMain.fieldbyname('MXGS').AsInteger/50);
    label554.Caption:=inttostr(my)+'/'+inttostr(mz);
    SCreateCDS20(ADOQueryMain,OrdBP_SH);
    SInitCDSData20(ADOQueryMain,OrdBP_SH);
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from GY_ZDY ');
      sql.Add('where ZGYName='''+Trim(FGYName)+''' and GYflag=''GXName''');
      Open;
    end;
    FML:=ADOQueryMain.fieldbyname('ZGYCode').AsString;
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmMLManage_LRRS_Col.FormShow(Sender: TObject);
var
  fsj:String;
begin
  InitSubGrid();
  initimageSH();
end;

procedure TfrmMLManage_LRRS_Col.initimageSH();
var
  i,j,z,x,q:Integer;
begin
  with Self.ScrollBox2 do
  begin
    for i:=0 to Self.ScrollBox2.ControlCount-1 do
    begin
      if Controls[i] is TPanel then
      begin
        TPanel(Controls[i]).Visible:=false;
      end;
    end;
  end;
  OrdBP_SH.DisableControls;
  with OrdBP_SH do
  begin
    First;
    i:=0;
    q:=1;
    while not Eof do
    begin
      if (q>(my-1)*50) and (q<=my*50) then
      begin
        with ScrollBox2 do
        begin
          TPanel(Controls[i]).Visible:=True;
          TPanel(Controls[i]).Hint:=Trim(OrdBP_SH.Fieldbyname('PTID').AsString);
          TPanel(Controls[i]).Color:=StrToInt('$'+Trim(OrdBP_SH.Fieldbyname('ColDaiMa').AsString));
          with TPanel(Controls[i]) do
          begin
            TLabel(Controls[0]).Caption:=Trim(OrdBP_SH.Fieldbyname('ColNo').AsString);
            TLabel(Controls[1]).Caption:=Trim(OrdBP_SH.Fieldbyname('ColNameKH').AsString);
            TLabel(Controls[0]).Visible:=true;
            TLabel(Controls[1]).Visible:=true;
            TLabel(Controls[0]).Color:=clWhite;
            TLabel(Controls[1]).Color:=clWhite;
          end;
        end;
        i:=i+1;
      end;
      q:=q+1;
      Next;
    end;
  end;
  OrdBP_SH.EnableControls;
end;

procedure TfrmMLManage_LRRS_Col.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;
      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;
    end;
    free;
  end;
end;

procedure TfrmMLManage_LRRS_Col.Button2Click(Sender: TObject);
begin
  close;
end;

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

procedure TfrmMLManage_LRRS_Col.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_LRRS_Col.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);
    Label6.Visible:=true;
    Label6.Caption:=Trim(OrdBP_SH.fieldbyname('GYSColNo').AsString);
    Edit2.Visible:=true;
    Button2.Visible:=true;
    FSKID:=Trim(OrdBP_SH.fieldbyname('SKID').AsString);
    if showmodal=1 then
    begin
      with OrdBP_SH do
      begin
        edit;
        FieldByName('GYSColNo').AsString:=Trim(FrmBPZdy_YT.Label6.Caption);
      end;
    end;
    free;
  end;
end;


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

procedure TfrmMLManage_LRRS_Col.XYClick(Sender: TObject);
var x:integer;
begin
  if my=mz then exit;
  XY.Enabled:=False;
  my:=my+1;
  if my>0 then
  begin
    SY.Enabled:=True;
  end
  else
  begin
    SY.Enabled:=False;
  end;
  label554.Caption:=inttostr(my)+'/'+inttostr(mz);
  initimageSH();
  XY.Enabled:=true;
end;

procedure TfrmMLManage_LRRS_Col.SYClick(Sender: TObject);
var x:integer;
begin
  if my=1 then exit;
  SY.Enabled:=False;
  my:=my-1;
  if my<mz then
  begin
    XY.Enabled:=True;
  end
  else
  begin
    XY.Enabled:=False;
  end;
  label554.Caption:=inttostr(my)+'/'+inttostr(mz);
  initimageSH();
  SY.Enabled:=true;
end;

procedure TfrmMLManage_LRRS_Col.Panel42DblClick(Sender: TObject);
begin
  ModalResult:=1;
end;

end.
