unit U_Bpkclist_HZ;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, StdCtrls, ExtCtrls, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData,
  cxButtonEdit, cxDropDownEdit, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls,
  cxGridCustomView, cxGrid, DBClient, ADODB, cxGridCustomPopupMenu,
  cxGridPopupMenu, cxCheckBox,IdFTP,ShellAPI,strutils;

type
  TfrmBpkclist_HZ = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBExport: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1P_CodeName: TcxGridDBColumn;
    v1P_SPEC: TcxGridDBColumn;
    v1RollNum: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Label2: TLabel;
    Label3: TLabel;
    custName: TEdit;
    CRType: TComboBox;
    Label5: TLabel;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    TOk: TToolButton;
    v1Ssel: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    ckName: TComboBox;
    Label6: TLabel;
    v1P_Code: TcxGridDBColumn;
    Label7: TLabel;
    batchNo: TEdit;
    Label8: TLabel;
    P_CodeName: TEdit;
    Label9: TLabel;
    P_Spec: TEdit;
    Label10: TLabel;
    Label11: TLabel;
    P_Color: TEdit;
    Label12: TLabel;
    P_KZ_S: TEdit;
    orderNo: TEdit;
    Label4: TLabel;
    ToolButton1: TToolButton;
    defstr1: TEdit;
    Label13: TLabel;
    P_Code: TEdit;
    Label14: TLabel;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure custNameChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TOkClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
  private
    procedure InitGrid();
    procedure SetComboBox();
    { Private declarations }
  public
    fType:integer;
    fCKName:string;
    
    { Public declarations }
  end;

var
  frmBpkclist_HZ: TfrmBpkclist_HZ;

implementation
uses
  U_DataLink,U_Fun10,U_ZDYHelp, U_BpRk;
{$R *.dfm}
procedure TfrmBpkclist_HZ.SetComboBox();
begin
  ckName.Items.Clear;
  with adoQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where Type=''CPCK'' ');
    if trim(fCKName)<>'' then
    sql.Add('and zdyName='+quotedstr(trim(fCKName)));
    open;
    while not eof do
    begin
      ckName.Items.Add(trim(fieldbyname('zdyName').AsString));
      next;
    end;
  end;
  if ckName.Items.Count>0 then ckName.ItemIndex:=0;

  CRType.Items.Clear;
  CRType.Items.Add('');
  with adoQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where Type=''CPRKTYPE''');
    if trim(fCKName)<>'' then
    sql.Add('and note='+quotedstr(trim(fCKName)));
    open;
    while not eof do
    begin
      CRType.Items.Add(trim(fieldbyname('zdyName').AsString));
      next;
    end;
  end;
  if CRType.Items.Count>0 then CRType.ItemIndex:=0;
end;
procedure TfrmBpkclist_HZ.InitGrid();
begin
  try
    with adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('exec P_Get_CP_KC_HZ');
      open;
    end;
    SCreateCDS20(adoqueryTmp,CDS_Main);
    SInitCDSData20(adoqueryTmp,CDS_Main);
  finally
  end;
end;

procedure TfrmBpkclist_HZ.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  writeCxGrid(self.Caption+tv1.Name+'3',Tv1,'成品仓库');
  if fType=10 then Action:=cahide
  else Action:=cafree;
end;

procedure TfrmBpkclist_HZ.FormDestroy(Sender: TObject);
begin
  frmBpkclist_HZ:=nil;
end;

procedure TfrmBpkclist_HZ.custNameChange(Sender: TObject);
begin
  if ADOQueryTmp.Active=False then Exit;
  SDofilter(ADOQueryTmp,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryTmp,CDS_Main);
  SInitCDSData20(ADOQueryTmp,CDS_Main);
end;

procedure TfrmBpkclist_HZ.FormShow(Sender: TObject);
begin
  if fType=10 then
  begin
    TOk.Visible:=true;
    v1Ssel.Visible:=true;
  end;
  readCxGrid(self.Caption+tv1.Name+'3',Tv1,'成品仓库');
  SetComboBox();
  InitGrid();
end;

procedure TfrmBpkclist_HZ.FormCreate(Sender: TObject);
begin
  BegDate.Date:=DServerDate-7;
  EndDate.Date:=DServerDate;
  cxGrid1.Align:=alclient;

end;

procedure TfrmBpkclist_HZ.TBCloseClick(Sender: TObject);
begin
    close;
end;

procedure TfrmBpkclist_HZ.TBRafreshClick(Sender: TObject);
begin

  initGrid();
end;

procedure TfrmBpkclist_HZ.TBPrintClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  TcxGridToExcel(self.Caption,cxgrid1);
end;

procedure TfrmBpkclist_HZ.TBExportClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  TcxGridToExcel(self.Caption,cxgrid1);
end;

procedure TfrmBpkclist_HZ.TOkClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  ModalResult:=1;
end;

procedure TfrmBpkclist_HZ.ToolButton1Click(Sender: TObject);
var
  IdFTP1: TIdFTP;
  FileName,FPath,FFName:string;
  FInt:integer;
begin
  if CDS_Main.IsEmpty then Exit;
  with adoqueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File ');
    sql.Add('where WBID='+quotedstr(trim(CDS_Main.fieldbyname('orderNo').AsString)));
    //sql.Add('and TFType=''产品''');
    open;
  end;
  IF adoqueryCmd.IsEmpty then
  begin
    application.MessageBox('此指示单没有图样信息！','提示信息',MB_ICONERROR);
    exit;
  end;
  FileName:= adoqueryCmd.fieldbyname('FileName').AsString;

  try
    IdFTP1:=TIdFTP.Create(self);
    IdFTP1.Host:=server;
    IdFTP1.Username:= 'three';
    IdFTP1.Password:= '641010';
    IdFTP1.Connect();
  except
    IdFTP1.Quit;
    IdFTP1.Free;
    Application.MessageBox('无法连接到文件服务器，请检查！', '提示', MB_ICONWARNING);
    Exit;
  end;  
  FPath:=Leftstr(ExtractFilePath(Application.ExeName),1)+':\图片查看\';
  if not DirectoryExists(ExtractFileDir(FPath)) then  CreateDir(ExtractFileDir(FPath));
  FFName:=Trim(FileName);
  FFName:=FPath+FFName;
  deleteFile(FFName);
  IdFTP1.Get('TP\'+FileName,FFName);
  if IdFTP1.Connected then
  begin
    IdFTP1.Quit;
    IdFTP1.Free;
  end;
  try
    ShellExecute(Handle, 'open',PChar(FFName),'', '', SW_SHOWNORMAL);
  except
  end;

end;

procedure TfrmBpkclist_HZ.Tv1CustomDrawCell(Sender: TcxCustomGridTableView;
  ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
  var ADone: Boolean);
begin
  IF AViewInfo.GridRecord.Values[tv1.GetColumnByFieldName('Qty').index]<AViewInfo.GridRecord.Values[tv1.GetColumnByFieldName('kc_min').index] then
  ACanvas.Brush.Color:=clred;
end;

end.
