unit U_KHGetCPManage;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin,
  StdCtrls, cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP,ShellAPI,IniFiles, cxCheckBox, RM_e_Xls,jpeg;

type
  TfrmKHGetCPManage = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    DataSource1: TDataSource;
    ADOQueryPrint: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    Panel1: TPanel;
    Label2: TLabel;
    CYNo: TEdit;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    Label1: TLabel;
    CYName: TEdit;
    Label4: TLabel;
    CYEName: TEdit;
    Label5: TLabel;
    CYSpec: TEdit;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    DataSource2: TDataSource;
    ADOQueryMain: TADOQuery;
    CDS_Main: TClientDataSet;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ToolButton4: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    SaveDialog1: TSaveDialog;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    Label3: TLabel;
    KHName: TEdit;
    Label6: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    Label7: TLabel;
    CKNO: TEdit;
    ToolButton7: TToolButton;
    RM2: TRMGridReport;
    RMDBDataSet1: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    ToolButton8: TToolButton;
    Image2: TImage;
    ToolButton9: TToolButton;
    ToolButton10: TToolButton;
    v1Column21: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxDBTreeList1DblClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure CYNoKeyPress(Sender: TObject; var Key: Char);
    procedure CYNoChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure CKNOKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton9Click(Sender: TObject);
    procedure ToolButton10Click(Sender: TObject);
  private
    { Private declarations }
    PState:Integer;
    FCPID,FTopID:String;
    procedure InitTree();
    procedure InitGrid();
    procedure ReadINIFile();

  public
    { Public declarations }
  end;

var
  frmKHGetCPManage: TfrmKHGetCPManage;

implementation
uses
  U_DataLink,U_Fun,U_CPAdd,U_FileUp,U_KHGetCPAdd;

{$R *.dfm}

procedure TfrmKHGetCPManage.InitTree();
var
  i:Integer;
begin
  {with ADOQueryTree do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from CP_Type order by CPlevel,CPName');
    Open;
  end;
  cxDBTreeList1.Items[0].Expand(false);  }
  //cxDBTreeList1.Items[1].Expand(False);
end;

procedure TfrmKHGetCPManage.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      SQL.Add('exec P_Select_Get_CP_YDang :CKID,:Begdate,:EndDate,:PState');
      Parameters.ParamByName('CKID').Value:='';
      Parameters.ParamByName('Begdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime));
      Parameters.ParamByName('EndDate').Value:=Trim(FormatDateTime('yyyy-MM-dd',EndDate.DateTime+1));
      Parameters.ParamByName('PState').Value:=0;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmKHGetCPManage.FormDestroy(Sender: TObject);
begin
  frmKHGetCPManage:=nil;
end;

procedure TfrmKHGetCPManage.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TfrmKHGetCPManage.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('客户拿样',Tv1,'样品管理');
  if  DirectoryExists(ExtractFileDir('E:\Right1209')) then
    winexec('cmd /c rd /s /q D:\Right1209',sw_hide);
  Close;
end;
procedure TfrmKHGetCPManage.TBDelClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('delete CP_YDang_KHGet where CKID='''+Trim(CDS_Main.fieldbyname('CKID').AsString)+'''');
    ExecSQL;
  end;
  CDS_Main.Delete;
end;

procedure TfrmKHGetCPManage.FormShow(Sender: TObject);
begin
  //InitTree();
  ReadCxGrid('样品列表',Tv1,'样品管理');

  EndDate.DateTime:=SGetServerDate(ADOQueryTemp);
  BegDate.DateTime:=EndDate.DateTime-7;
end;

procedure TfrmKHGetCPManage.cxDBTreeList1DblClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmKHGetCPManage.TBAddClick(Sender: TObject);
begin
  try
    frmKHGetCPAdd:=TfrmKHGetCPAdd.Create(Application);
    with frmKHGetCPAdd do
    begin
      FCYID:='';
      //FCPID:=Trim(Self.ADOQueryTree.fieldbyname('CPID').AsString);
      //FCPNo:=Trim(Self.ADOQueryTree.fieldbyname('CPNo').AsString);
      if ShowModal=1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmKHGetCPAdd.Free;
  end;
end;

procedure TfrmKHGetCPManage.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmKHGetCPManage.ToolButton3Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  try
    frmKHGetCPAdd:=TfrmKHGetCPAdd.Create(Application);
    with frmKHGetCPAdd do
    begin
      FCYID:=Trim(Self.CDS_Main.fieldbyname('CKNO').AsString);
      //FCPID:=Trim(Self.ADOQueryTree.fieldbyname('CPID').AsString);
      //FCPNo:=Trim(Self.ADOQueryTree.fieldbyname('CPNo').AsString);
      if ShowModal=1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmKHGetCPAdd.Free;
  end;
end;

procedure TfrmKHGetCPManage.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  end;
end;

procedure TfrmKHGetCPManage.CYNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CP_YDang where CYID='''+Trim(CYNo.Text)+'''');
      Open;
      CYNo.Text:=Trim(fieldbyname('CYNo').AsString);
    end;
  end;
end;

procedure TfrmKHGetCPManage.CYNoChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmKHGetCPManage.ToolButton1Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  TcxGridToExcel('客户拿样',cxGrid1);
end;

procedure TfrmKHGetCPManage.ToolButton4Click(Sender: TObject);
var
   fPrintFile:string;
   Txt,fImagePath:string;
   Moudle: THandle;
   Makebar:TMakebar;
   Mixtext:TMixtext;
begin
  if CDS_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\样品标签.rmf' ;
  if FileExists(fPrintFile) then
  begin
        try
          Moudle:=LoadLibrary('MakeQRBarcode.dll');
          @Makebar:=GetProcAddress(Moudle,'Make');
          @Mixtext:=GetProcAddress(Moudle,'MixText');
          Txt:=Trim(CDS_Main.fieldbyname('CYID').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;
        RMVariables['QRBARCODE']:=fImagePath;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\样品标签.rmf'),'提示',0);
  end;
end;

procedure TfrmKHGetCPManage.ToolButton5Click(Sender: TObject);
begin
  try
    frmFileUp:=TfrmFileUp.Create(Application);
    with frmFileUp do
    begin
      Code.Text:=Trim(Self.CDS_Main.fieldbyname('CYNO').AsString);
      CYID:=Trim(Self.CDS_Main.fieldbyname('CYID').AsString);
      if ShowModal=1 then
      begin
        Self.InitGrid();
        Self.CDS_Main.Locate('CYID',CYID,[]);
      end;
    end;
  finally
    frmFileUp.Free;
  end;
end;

procedure TfrmKHGetCPManage.ToolButton6Click(Sender: TObject);
var
  fHandle:THandle;
  FInt:Integer;
  FFName,FPath:String;
begin
  {FPath:='C:\HTTP1209\';
  if  DirectoryExists(ExtractFileDir(FPath)) then
    winexec('cmd /c rd /s /q C:\HTTP1209',sw_hide);}
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from XD_File where CYNO='''+Trim(CDS_Main.fieldbyname('CYNO').AsString)+'''');
    Open;
    if IsEmpty then
    begin
      Application.MessageBox('样品图片未上传！','提示',0);
      Exit;
    end;
  end;
  try
    ReadINIFile();
    server:=ReadINIFileStr('SYSTEMSET.INI','SERVER','服务器地址','127.0.0.1');
    if Length(server)<6 then
    begin
      server:='127.0.0.1';
    end;
    IdFTP1.Host :=server;//PicSvr;
    IdFTP1.Username := 'three';
    IdFTP1.Password := '641010';
    IdFTP1.Connect();
  except
    IdFTP1.Quit;
    Application.MessageBox('无法连接到文件服务器，请检查！', '提示', MB_ICONWARNING);
    Exit;
  end;
  FPath:='D:\Right1209\';
  if not DirectoryExists(ExtractFileDir(FPath)) then
    CreateDir(ExtractFileDir(FPath));
  FFName:=Trim(ADOQueryTemp.fieldbyname('FileName').AsString);
  FFName:=FPath+FFName;
  if FileExists(FFName) then
  begin
    FInt:=1;
  end;
  if FInt<>1 then
    IdFTP1.Get('YP\'+Trim(ADOQueryTemp.fieldbyname('FileName').AsString),
                   FPath+Trim(ADOQueryTemp.fieldbyname('FileName').AsString)
                    );
  if IdFTP1.Connected then IdFTP1.Quit;
  ShellExecute(Handle, 'open',PChar(FPath+Trim(ADOQueryTemp.fieldbyname('FileName').AsString)),'', '', SW_SHOWNORMAL);

end;
procedure TfrmKHGetCPManage.ReadINIFile();
var
  programIni:Tinifile;  //配置文件名
  FileName:string;
begin
  FileName:=ExtractFilePath(Paramstr(0))+'SYSTEMSET.INI';
  programIni:=Tinifile.create(FileName);
  server:=programIni.ReadString('SERVER','服务器地址','127.0.0.1');
  programIni.Free;
end;

procedure TfrmKHGetCPManage.Tv1CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
 // ToolButton6.Click;
end;

procedure TfrmKHGetCPManage.CKNOKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    if Trim(CKNO.Text)='' then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Close;
        SQL.Clear;
        SQL.Add('exec P_Select_Get_CP_YDang :CKID,:Begdate,:EndDate,:PState');
        Parameters.ParamByName('CKID').Value:=Trim(CKNO.Text);
        Parameters.ParamByName('Begdate').Value:='';
        Parameters.ParamByName('EndDate').Value:='';
        Parameters.ParamByName('PState').Value:=3;
        Open;
      end;
      SCreateCDS20(ADOQueryMain,CDS_Main);
      SInitCDSData20(ADOQueryMain,CDS_Main);
      CKNO.Text:='';
    finally
      ADOQueryMain.EnableControls;
    end;
  end;

end;

procedure TfrmKHGetCPManage.ToolButton7Click(Sender: TObject);
var
   fPrintFile:string;
begin
  if CDS_Main.IsEmpty then Exit;
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    SQL.Add('exec P_Select_Get_CP_YDang :CKID,:Begdate,:EndDate,:PState');
    Parameters.ParamByName('CKID').Value:=Trim(CDS_Main.fieldbyname('CKNO').AsString);
    Parameters.ParamByName('Begdate').Value:='';
    Parameters.ParamByName('EndDate').Value:='';
    Parameters.ParamByName('PState').Value:=3;
    Open;
  end;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\拿样清单.rmf' ;
  if FileExists(fPrintFile) then
  begin
    RM2.LoadFromFile(fPrintFile);
    RMVariables['printer']:=Trim(DName);
    RM2.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\拿样清单.rmf'),'提示',0);
  end;
end;

procedure TfrmKHGetCPManage.ToolButton8Click(Sender: TObject);
var
   fPrintFile:string;
begin
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\空白信笺纸.rmf' ;
  if FileExists(fPrintFile) then
  begin
    RM2.LoadFromFile(fPrintFile);
    RM2.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\空白信笺纸.rmf'),'提示',0);
  end;
end;

procedure TfrmKHGetCPManage.Tv1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
var
  jpg:TJpegImage;
  myStream:TADOBlobStream;
begin
  if CDS_Main.IsEmpty then Exit;
  if Trim(CDS_Main.fieldbyname('CYID').AsString)='' then Exit;
 // if cxPageControl1.ActivePageIndex=6 then
  begin
    Image2.Picture.Assign(nil);
    try
      with ADOQueryTemp do
      begin
        close;
        sql.Clear;
        sql.Add(' select * from TP_File A where A.WBID='''+Trim(CDS_Main.fieldbyname('CYID').AsString)+'''');
        sql.Add(' and TFType=''样品'' ');
        open;
        if RecordCount>0 then
        begin
          if trim(ADOQueryTemp.fieldbyname('FilesOther').AsString)<>'' then
          begin
            myStream:=tadoblobstream.Create(tblobfield(ADOQueryTemp.fieldbyname('FilesOther')),bmread);
            if myStream=nil then exit;
            jpg:=TJPEGImage.Create;
            jpg.LoadFromStream(myStream);
            Image2.Picture.Assign(jpg);
          end;
        end;
      end;
    finally
      jpg.Free;
      myStream.Free;
    end;
  end;
end;

procedure TfrmKHGetCPManage.ToolButton9Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  try
    frmKHGetCPAdd:=TfrmKHGetCPAdd.Create(Application);
    with frmKHGetCPAdd do
    begin
      FCYID:=Trim(Self.CDS_Main.fieldbyname('CKNO').AsString);
      CopyInt:=99;
      //FCPID:=Trim(Self.ADOQueryTree.fieldbyname('CPID').AsString);
      //FCPNo:=Trim(Self.ADOQueryTree.fieldbyname('CPNo').AsString);
      if ShowModal=1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmKHGetCPAdd.Free;
  end;
end;

procedure TfrmKHGetCPManage.ToolButton10Click(Sender: TObject);
var
   fPrintFile:string;
begin
  if CDS_Main.IsEmpty then Exit;
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    SQL.Add('exec P_Select_Get_CP_YDang :CKID,:Begdate,:EndDate,:PState');
    Parameters.ParamByName('CKID').Value:=Trim(CDS_Main.fieldbyname('CKNO').AsString);
    Parameters.ParamByName('Begdate').Value:='';
    Parameters.ParamByName('EndDate').Value:='';
    Parameters.ParamByName('PState').Value:=3;
    Open;
  end;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\拿样清单(无单价).rmf' ;
  if FileExists(fPrintFile) then
  begin
    RM2.LoadFromFile(fPrintFile);
    RMVariables['printer']:=Trim(DName);
    RM2.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\拿样清单(无单价).rmf'),'提示',0);
  end;
end;

end.
