unit U_ZdyAttachmentBJ;

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, cxCalendar,
  cxButtonEdit, cxTextEdit, cxDBLookupComboBox, RM_e_Xls, RM_E_llPDF, cxPC,
  cxContainer, cxImage, cxDBEdit, Menus ,StrUtils;

type
  TfrmZdyAttachmentCB = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    Label3: TLabel;
    custNo: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_Main: TDataSource;
    CDS_Main: TClientDataSet;
    TBADD: TToolButton;
    v2Column6: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    TBedit: TToolButton;
    v2Column11: TcxGridDBColumn;
    v2DEFStr7: TcxGridDBColumn;
    v2Column12: TcxGridDBColumn;
    v2Column13: TcxGridDBColumn;
    Label5: TLabel;
    begdate: TDateTimePicker;
    Label6: TLabel;
    enddate: TDateTimePicker;
    v2Column4: TcxGridDBColumn;
    custName: TEdit;
    Label2: TLabel;
    v2Column15: TcxGridDBColumn;
    ToolButton5: TToolButton;
    ADOQueryPrint: TADOQuery;
    RM1: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    RMDBDataSet1: TRMDBDataSet;
    v2Column18: TcxGridDBColumn;
    v2Column19: TcxGridDBColumn;
    v2Column14: TcxGridDBColumn;
    v2Column16: TcxGridDBColumn;
    v2Column17: TcxGridDBColumn;
    v2Column20: TcxGridDBColumn;
    v2Column21: TcxGridDBColumn;
    v2Column22: TcxGridDBColumn;
    v2Column28: TcxGridDBColumn;
    Tchk: TToolButton;
    Tnochk: TToolButton;
    cxTabControl1: TcxTabControl;
    v2Column23: TcxGridDBColumn;
    v2Column24: TcxGridDBColumn;
    ToolButton3: TToolButton;
    ADOQueryImage: TADOQuery;
    DataSource1: TDataSource;
    GroupBox1: TGroupBox;
    Picture4: TcxDBImage;
    PopupMenu1: TPopupMenu;
    IdFTP1: TIdFTP;
    Label1: TLabel;
    P_CodeName: TEdit;
    Label4: TLabel;
    P_Code: TEdit;
    v2Column25: TcxGridDBColumn;
    v2Column26: 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 TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure custNoChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TBeditClick(Sender: TObject);
    procedure TBADDClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure TchkClick(Sender: TObject);
    procedure TnochkClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure Picture4Click(Sender: TObject);
    procedure Picture4DblClick(Sender: TObject);
    procedure Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
  private
  fFlileFlag:string;
    { Private declarations }
    procedure InitGrid();
    procedure InitImage(fwbid:string);
    procedure SetStatus();
  public
    fkhType:string;
    Fmanage:string;
    canshu1:string;

  end;

var
  frmZdyAttachmentCB: TfrmZdyAttachmentCB;

implementation
uses
  U_DataLink,U_Fun10,U_ZDYHelp,
  U_ZdyAttachmentBJ_Edit;

{$R *.dfm}

procedure TfrmZdyAttachmentCB.SetStatus();
begin
  TBedit.Visible:=false;
  Tbdel.Visible:=false;
  Tchk.Visible:=false;
  Tnochk.Visible:=false;
  IF trim(canshu1)='业务员' then
  begin
    case cxTabControl1.TabIndex of
    0:begin
       TBedit.Visible:=true;
       Tbdel.Visible:=true;
      end;
    1:begin
     //  tbcdel.Visible:=true;
      end;
     end;
   end
   else
   begin
    case cxTabControl1.TabIndex of
    0:begin
       TBedit.Visible:=true;
       Tbdel.Visible:=true;
       Tchk.Visible:=true;
      end;
    1:begin
       Tnochk.Visible:=true;
      end;
     end;
   end;
end;

procedure TfrmZdyAttachmentCB.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin

      Close;
      Filtered:=False;
      SQL.Clear;
      sql.Add('select A.* from KH_CP_BJ A');
      sql.Add(' where A.CRTime>='''+formatdateTime('yyyy-MM-dd',begdate.DateTime)+'''');
      sql.Add(' and A.CRTime<'''+formatdateTime('yyyy-MM-dd',enddate.DateTime+1)+'''');
      IF trim(canshu1)='业务员'then
      sql.Add(' and A.Filler='+quotedstr(trim(DName)));
      if cxTabControl1.TabIndex<2 then
      sql.Add('and A.status='''+inttostr(cxTabControl1.TabIndex)+''' ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;


procedure TfrmZdyAttachmentCB.FormDestroy(Sender: TObject);
begin
  frmZdyAttachmentCB:=nil;
end;

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

procedure TfrmZdyAttachmentCB.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption+'1',Tv2,'客户自定义');
  Close;
end;
procedure TfrmZdyAttachmentCB.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 KH_CP_BJ where BJNO='''+Trim(CDS_Main.fieldbyname('BJNO').AsString)+'''');
      ExecSQL;
    end;
  initgrid();
end;

procedure TfrmZdyAttachmentCB.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption+'1',Tv2,'客户自定义');
  fFlileFlag:=UserDataFlag+'HX';
  SetStatus();
  InitGrid();
end;

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

procedure TfrmZdyAttachmentCB.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 TfrmZdyAttachmentCB.custNoChange(Sender: TObject);
begin
  ToolButton2.Click;
end;


procedure TfrmZdyAttachmentCB.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel(self.Caption,cxgrid2);
 // SelExportData(Tv2,ADOQueryMain,'客户资料列表');
end;

procedure TfrmZdyAttachmentCB.TBeditClick(Sender: TObject);
begin
  IF CDS_main.IsEmpty then exit;
  try
    frmZdyAttachmentBJ_Edit:=TfrmZdyAttachmentBJ_Edit.Create(Application);
    with frmZdyAttachmentBJ_Edit do
    begin
      fkeyNo:=self.cds_main.fieldbyname('BJNO').asstring;
      fType:=1;
        canshu1:=self.canshu1;
      fFlileFlag:=self.fFlileFlag;
      if ShowModal=1 then
      begin
        TBRafresh.Click;
      end;
    end;
  finally
    frmZdyAttachmentBJ_Edit.Free;
  end;
end;

procedure TfrmZdyAttachmentCB.TBADDClick(Sender: TObject);
begin
  try
    frmZdyAttachmentBJ_Edit:=TfrmZdyAttachmentBJ_Edit.Create(Application);
    with frmZdyAttachmentBJ_Edit do
    begin
      fkeyNO:='';
      fType:=0;
      canshu1:=self.canshu1;
      fFlileFlag:=self.fFlileFlag;
      if ShowModal=1 then
      begin
        TBRafresh.Click;
      end;
    end;
  finally
    frmZdyAttachmentBJ_Edit.Free;
  end;
end;

procedure TfrmZdyAttachmentCB.FormCreate(Sender: TObject);
begin
  enddate.Date:=date();
  begdate.Date:=strtodate(formatdateTime('yyyy-MM',enddate.Date)+'-01');
  canshu1:=trim(DParameters2);
end;

procedure TfrmZdyAttachmentCB.ToolButton5Click(Sender: TObject);
var
   fPrintFile:string;
   Porderno:string;
   i,j:Integer;
begin
  if CDS_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\报价信息表.rmf' ;
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,  ');
    sql.Add('FilesOther=(select Top 1 X1.FilesOther from TP_File X1,CP_YDang X2 where X1.WBID=X2.CYID and X2.CYNO=A.P_Code)');
    sql.Add('from KH_CP_BJ A  ');
    sql.Add('Where A.BJNO='''+Trim(CDS_Main.fieldbyname('BJNO').AsString)+'''');
    Open;
  end;
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+trim(fPrintFile)),'提示',0);
    exit;
  end;

end;

procedure TfrmZdyAttachmentCB.TchkClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  KH_CP_BJ SET status=''1'' ');
      sql.Add('where BJNO='+quotedstr(trim(CDS_Main.fieldbyname('BJNO').AsString)));
      execsql;
    end;
    application.MessageBox('审核成功！','提示信息');
    TBRafresh.Click;
  except
   application.MessageBox('审核失败！','提示信息',0);
  end;
end;

procedure TfrmZdyAttachmentCB.TnochkClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  KH_CP_BJ SET status=''0'' ');
      sql.Add('where BJNO='+quotedstr(trim(CDS_Main.fieldbyname('BJNO').AsString)));
      execsql;
    end;
    application.MessageBox('撤销审核成功！','提示信息');
    TBRafresh.Click;
  except
   application.MessageBox('撤销审核失败！','提示信息',0);
  end;
end;

procedure TfrmZdyAttachmentCB.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  InitGrid();
end;

procedure TfrmZdyAttachmentCB.ToolButton3Click(Sender: TObject);
var
   fPrintFile:string;
   Porderno:string;
   i,j:Integer;
begin
  if CDS_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\拿样清单.rmf' ;
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.CYMF,B.CYKZ,  ');
    sql.Add('FilesOther=(select Top 1 X1.FilesOther from TP_File X1,CP_YDang X2 where X1.WBID=X2.CYID and X2.CYNO=A.P_Code) ');
    sql.Add('from KH_CP_BJ A  ');
    sql.Add('left join CP_YDang B on B.CYNO=A.P_Code');
    sql.Add('Where A.BJNO='''+Trim(CDS_Main.fieldbyname('BJNO').AsString)+'''');
    Open;
  end;
  if FileExists(fPrintFile) then
  begin
    RMVariables['dtxz'] :=FormatDateTime('yyyy-mm-dd',date());
    RMVariables['zdr'] :=trim(DName);
    RMVariables['Filler'] :=trim(CDS_Main.fieldbyname('Filler').AsString);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+trim(fPrintFile)),'提示',0);
    exit;
  end;

end;

procedure TfrmZdyAttachmentCB.InitImage(fwbid:string);
begin
  ADOQueryImage.close;
  IF fwbid='' then exit;
  with ADOQueryImage do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File A');
    sql.Add('where WBID='+quotedstr(trim(fwbid)));
    open;
  end;
end;
procedure TfrmZdyAttachmentCB.Picture4Click(Sender: TObject);
begin

end;
//打开图片
procedure TfrmZdyAttachmentCB.Picture4DblClick(Sender: TObject);
var
  fileName,sFieldName:string;
  //fileName:string;
 //	ff: TADOBlobStream;
 // FJStream : TMemoryStream;
begin
  IF Picture4.Picture.Height=0 then exit;
  sFieldName:=leftbstr(ExtractFilePath(Application.ExeName),1)+':\图片查看';
  if not DirectoryExists(pchar(sFieldName)) then
  CreateDirectory(pchar(sFieldName),nil);
  fileName:=CDS_Main.fieldbyname('BGFile').AsString;
  sFieldName:=sFieldName+'\'+trim(fileName);

  try
    IdFTP1.Host := ReadINIFileStr('SYSTEMSET.INI','SERVER','服务器地址','127.0.0.1');
    IdFTP1.Username := 'three';
		IdFTP1.Password := '641010';
    IdFTP1.Connect();
  except
    ;
  end;

  if IdFTP1.Connected then
  begin

      application.ProcessMessages;
    try
      IdFTP1.Get(fFlileFlag+'\'+Trim(fileName), sFieldName,true, false); 
    except
      Application.MessageBox('客户图样文件不存在', '提示', MB_ICONWARNING);
      IdFTP1.Quit;
      Exit;
    end;
  end
  else
  begin
    Application.MessageBox('无法连接文件服务器', '提示', MB_ICONWARNING);
    IdFTP1.Quit;
    Exit;
  end;
   if IdFTP1.Connected then IdFTP1.Quit;
  ShellExecute(Handle, 'open',PChar(sFieldName),'', '', SW_SHOWNORMAL);

end;

procedure TfrmZdyAttachmentCB.Tv2FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  initImage(CDS_Main.fieldbyname('BGFile').AsString);
end;

end.


