unit U_CPBaoJiaChk;

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, Menus, cxPC;

type
  TfrmCPBaoJiaChk = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    Label1: TLabel;
    CYName: TEdit;
    Label4: TLabel;
    CYEName: TEdit;
    Label5: TLabel;
    CYSpec: TEdit;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    SaveDialog1: TSaveDialog;
    ToolButton6: TToolButton;
    Label3: TLabel;
    CYNO: TEdit;
    Panel2: TPanel;
    Label7: TLabel;
    CYKZ: TEdit;
    Label8: TLabel;
    Label9: TLabel;
    CYNOOld: TEdit;
    CYECF: TEdit;
    Label10: TLabel;
    CYMF: TEdit;
    CDS_BJ: TClientDataSet;
    DSBJ: TDataSource;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Label2: TLabel;
    begdate: TDateTimePicker;
    enddate: TDateTimePicker;
    ADOQuerySub: TADOQuery;
    DS_Chk: TDataSource;
    CDS_Chk: TClientDataSet;
    cxGridPopupMenu3: TcxGridPopupMenu;
    cxGrid1: TcxGrid;
    Tv2: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    v1Column41: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column25: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxSplitter2: TcxSplitter;
    cxGrid4: TcxGrid;
    Tv21: TcxGridDBTableView;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    v21Column1: TcxGridDBColumn;
    Label6: TLabel;
    BJPerson: TEdit;
    Label11: TLabel;
    KHName: TEdit;
    v2Column1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v21Column2: TcxGridDBColumn;
    v21Column3: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    v2Column11: TcxGridDBColumn;
    v2Column12: TcxGridDBColumn;
    v2Column13: TcxGridDBColumn;
    v2Column14: TcxGridDBColumn;
    v2Column15: TcxGridDBColumn;
    v2Column16: TcxGridDBColumn;
    v2Column17: TcxGridDBColumn;
    ToolButton3: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxDBTreeList1DblClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure CYNoChange(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure CYNOOldChange(Sender: TObject);
    procedure Tv2CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TBRafreshClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    { Private declarations }
    CPID:string;
    PState:Integer;
    FCPID,FTopID:String;
    procedure InitGrid();
    procedure ReadINIFile();

  public
    { Public declarations }
    FFint:Integer;
  end;

var
  frmCPBaoJiaChk: TfrmCPBaoJiaChk;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp;

{$R *.dfm}



procedure TfrmCPBaoJiaChk.InitGrid();
begin

  try
    ADOQuerySub.DisableControls;
    with ADOQuerySub do
    begin
      Filtered:=False;
      Close;
      SQL.Clear;
      SQL.Add('select A.BJPerSon,A.BJPrice,A.BZType,A.KHName,A.BJID,A.MBShiChang,A.Qty,A.PinZhiYQ, A.BJTime,');
      sql.Add('A.Chker,A.ChkStatus,A.ChkPrice,A.ChkNote,A.BJNO,A.BJCount,A.YongJIn,A.CPPrice,A.PriceSY,A.QtyUnit,A.Note,B.* ');
      sql.Add('from CP_BaoJia A');
      SQL.Add('left join CP_YDang B on A.CYID=B.CYID ');
      {if Trim(DParameters1)='高权限' then
      begin

      end else
      begin
        sql.Add('where exists(select * from CP_BaoJia_Chk C where C.ChkTime>=:begdate and ChkTime<:enddate ');
        sql.Add(' and isnull(C.ChkStatus,'''')<>'''' and C.BJID=A.BJID and NextChkPerson='''+Trim(DName)+''')');
        sql.Add(' and A.ChkStatus=''审核通过''');
      end;}
      sql.Add(' where isnull(A.Chker,'''')<>'''' and A.ChkStatus=''审核通过'' ');
      sql.Add(' and A.FillTime>=:begdate and A.FillTime<:enddate ');

      Parameters.ParamByName('begdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',begdate.DateTime));
      Parameters.ParamByName('enddate').Value:=Trim(FormatDateTime('yyyy-MM-dd',enddate.DateTime+1));
      Open;
    end;
    SCreateCDS20(ADOQuerySub,CDS_BJ);
    SInitCDSData20(ADOQuerySub,CDS_BJ);
  finally
    ADOQuerySub.EnableControls;
  end;
end;

procedure TfrmCPBaoJiaChk.FormDestroy(Sender: TObject);
begin
  frmCPBaoJiaChk:=nil;
end;

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

procedure TfrmCPBaoJiaChk.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('bjshWTSel',Tv2,'报价管理');
  WriteCxGrid('bjshWTSSel',Tv21,'报价管理');
  if  DirectoryExists(ExtractFileDir('D:\Right1209')) then
    winexec('cmd /c rd /s /q D:\Right1209',sw_hide);
  Close;
end;
procedure TfrmCPBaoJiaChk.FormShow(Sender: TObject);
begin
  ReadCxGrid('bjshWTSel',Tv2,'报价管理');
  ReadCxGrid('bjshWTSSel',Tv21,'报价管理');
  enddate.Date:=SGetServerDate(ADOQueryCmd);
  begdate.Date:=enddate.Date-7;
  InitGrid();
end;

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

procedure TfrmCPBaoJiaChk.ToolButton2Click(Sender: TObject);
begin
  if ADOQuerySub.Active then
  begin
    SDofilter(ADOQuerySub,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQuerySub,CDS_BJ);
    SInitCDSData20(ADOQuerySub,CDS_BJ);
  end;
end;

procedure TfrmCPBaoJiaChk.CYNoChange(Sender: TObject);
begin
  if Length(Trim(TEdit(Sender).Text))<3 then Exit;
  if ADOQuerySub.Active then
  begin
    SDofilter(ADOQuerySub,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQuerySub,CDS_BJ);
    SInitCDSData20(ADOQuerySub,CDS_BJ);
  end;

end;

procedure TfrmCPBaoJiaChk.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_BJ.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 TfrmCPBaoJiaChk.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 TfrmCPBaoJiaChk.Tv2CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  ToolButton6.Click;
end;

procedure TfrmCPBaoJiaChk.CYNOOldChange(Sender: TObject);
begin

  if ADOQuerySub.Active then
  begin
    SDofilter(ADOQuerySub,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQuerySub,CDS_BJ);
    SInitCDSData20(ADOQuerySub,CDS_BJ);
  end;
end;

procedure TfrmCPBaoJiaChk.Tv2CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if CDS_BJ.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CP_BaoJia_Chk where BJID='''+Trim(CDS_BJ.fieldbyname('BJID').AsString)+'''');
    sql.Add(' order by ChkOrder ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_Chk);
  SInitCDSData20(ADOQueryTemp,CDS_Chk);
end;

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

procedure TfrmCPBaoJiaChk.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
  if CDS_BJ.IsEmpty then
  begin
    with Self.ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CP_BaoJia_Chk where 1<>1');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,CDS_Chk);
    SInitCDSData20(ADOQueryTemp,CDS_Chk);
  end;
end;

procedure TfrmCPBaoJiaChk.ToolButton3Click(Sender: TObject);
begin
  if CDS_BJ.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  ModalResult:=1;
end;

end.
