unit U_Samplelist_So;

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, cxPC,
  cxCalendar, RM_Common, RM_Class, RM_GridReport, RM_System, RM_Dataset,
  RM_e_Xls, ShellAPI, cxContainer, cxImage, cxDBEdit, StrUtils, IdBaseComponent,
  IdComponent, IdTCPConnection, IdTCPClient, IdFTP;

type
  TfrmSamplelist_so = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBExport: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1P_CodeName: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1CRTime: TcxGridDBColumn;
    v1Filler: TcxGridDBColumn;
    v1FillTime: TcxGridDBColumn;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    P_CodeName: TEdit;
    P_Code: TEdit;
    CRType: TComboBox;
    Label5: TLabel;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ckName: TComboBox;
    Label6: TLabel;
    v1P_Code: TcxGridDBColumn;
    v1INoutNo: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    Label9: TLabel;
    defstr6: TEdit;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    RMDBMain: TRMDBDataSet;
    RM1: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    Panel2: TPanel;
    Picture4: TcxDBImage;
    IdFTP1: TIdFTP;
    ADOQueryImage: TADOQuery;
    DSImage: TDataSource;
    v1Column16: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure P_CodeNameChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure Picture4DblClick(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
  private
    fFlileFlag: string;
    procedure InitGrid();
    procedure SetComboBox();
    procedure Setstatus();
    procedure InitImage();
    procedure LookImage(FileName: string);
    { Private declarations }
  public
    fCKName: string;
    fCRFlag: string;
    canshu1: string;
    { Public declarations }
  end;

var
  frmSamplelist_so: TfrmSamplelist_so;

implementation

uses
  U_DataLink, U_Fun10, U_ZDYHelp, U_SampleLR_SO;
{$R *.dfm}

procedure TfrmSamplelist_so.LookImage(FileName: string);
var
  sFieldName: string;
 //	ff: TADOBlobStream;
 // FJStream : TMemoryStream;
begin

  sFieldName := leftbstr(ExtractFilePath(Application.ExeName), 1) + ':\图片查看';

  if not DirectoryExists(pchar(sFieldName)) then
    CreateDirectory(pchar(sFieldName), nil);

  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

    Panel2.Caption := '正在下载数据，请稍等...';
    Panel2.Visible := true;
    application.ProcessMessages;
    try
      IdFTP1.Get(fFlileFlag + '\' + Trim(FileName), sFieldName, true, false);
    except
      Panel2.Visible := false;
      Application.MessageBox('客户图样文件不存在', '提示', MB_ICONWARNING);
      IdFTP1.Quit;
      Exit;
    end;
  end
  else
  begin
    Panel2.Visible := false;
    Application.MessageBox('无法连接文件服务器', '提示', MB_ICONWARNING);
    IdFTP1.Quit;
    Exit;
  end;
  Panel2.Visible := false;
  if IdFTP1.Connected then
    IdFTP1.Quit;
  ShellExecute(Handle, 'open', PChar(sFieldName), '', '', SW_SHOWNORMAL);
end;

procedure TfrmSamplelist_so.InitImage();
begin

  ADOQueryImage.Close;
  if CDS_Main.IsEmpty then
    exit;
  with ADOQueryImage do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File A');
    sql.Add('where WBID=' + quotedstr(trim(CDS_Main.fieldbyname('JSNO').AsString)));
    sql.Add('and TFType=' + quotedstr(trim('SO')));
    open;
  end;
end;

procedure TfrmSamplelist_so.SetStatus();
begin
  TBedit.Visible := false;
  Tbdel.Visible := false;
  case cxTabControl1.TabIndex of
    0:
      begin
        TBedit.Visible := true;
        Tbdel.Visible := true;
      end;
    1:
      begin
     //  tbcdel.Visible:=true;
      end;
  end;
end;

procedure TfrmSamplelist_so.SetComboBox();
begin
  ckName.Items.Clear;
  with adoQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where Type=''YPCK'' ');
    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=''YPRKTYPE''');
    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 TfrmSamplelist_so.InitGrid();
begin
  try
    with adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select A.* from JY_Sample A');
      sql.Add('where CRTime>=''' + formatdateTime('yyyy-MM-dd', begdate.Date) + ''' ');
      sql.Add('and CRTime<''' + formatdateTime('yyyy-MM-dd', enddate.Date + 1) + ''' ');
      sql.Add('and CRFlag=''' + trim(fCRFlag) + ''' ');
      if trim(canshu1) <> '高权限' then
        sql.Add('and Filler=''' + trim(DName) + ''' ');
      open;
    end;
    SCreateCDS20(adoqueryTmp, CDS_Main);
    SInitCDSData20(adoqueryTmp, CDS_Main);
  finally
  end;
end;

procedure TfrmSamplelist_so.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  writeCxGrid(self.Caption, Tv1);
  Action := cafree;
end;

procedure TfrmSamplelist_so.FormDestroy(Sender: TObject);
begin
  frmSamplelist_so := nil;
end;

procedure TfrmSamplelist_so.TBAddClick(Sender: TObject);
begin
  frmSampleLR_SO := TfrmSampleLR_SO.create(self);
  with frmSampleLR_SO do
  begin
    fkeyNo := '';
    fCRFlag := self.fCRFlag;
    fckName := self.fckName;
    canshu1 := self.canshu1;
    if showmodal = 1 then
    begin
      TBRafresh.Click;
      self.CDS_Main.Locate('JSNo', trim(fkeyNo), []);
    end;
    free;
  end;
end;

procedure TfrmSamplelist_so.P_CodeNameChange(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 TfrmSamplelist_so.FormShow(Sender: TObject);
begin
  fFlileFlag := UserDataFlag + 'SO';
  readCxGrid(self.Caption, Tv1);
//  SetComboBox();
  SetStatus();
  InitGrid();
end;

procedure TfrmSamplelist_so.FormCreate(Sender: TObject);
begin
  BegDate.Date := DServerDate - 7;
  EndDate.Date := DServerDate;
  cxGrid1.Align := alclient;
  canshu1 := trim(DParameters1);
end;

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

procedure TfrmSamplelist_so.TBEditClick(Sender: TObject);
var
  rkNum, ckNum: double;
begin
  if CDS_Main.IsEmpty then
    Exit;

  frmSampleLR_SO := TfrmSampleLR_SO.create(self);
  with frmSampleLR_SO do
  begin
    fkeyNo := self.cds_main.fieldbyname('JSNO').AsString;
    fCRFlag := self.fCRFlag;
    canshu1 := self.canshu1;
    if showmodal = 1 then
    begin
      TBRafresh.Click;
      self.CDS_Main.Locate('JSNo', trim(fkeyNo), []);
    end;
    free;
  end;
end;

procedure TfrmSamplelist_so.TBDelClick(Sender: TObject);
var
  rkNum, ckNum: double;
begin
  if CDS_Main.IsEmpty then
    Exit;

  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete  JY_Sample      ');
      SQL.Add('where  JSNO=''' + Trim(CDS_Main.fieldbyname('JSNO').AsString) + ''' ');
      SQL.Add('and  JSID=''' + Trim(CDS_Main.fieldbyname('JSID').AsString) + ''' ');
      sql.Add('delete from YF_Money_CR ');
      sql.Add('where mainID=''' + Trim(CDS_Main.fieldbyname('JSNO').AsString) + ''' ');
      execsql;
    end;
    TBRafresh.Click;
  except
    Application.MessageBox('数据删除失败！', '提示信息', 0);
  end;
end;

procedure TfrmSamplelist_so.TBRafreshClick(Sender: TObject);
begin
  initGrid();
end;

procedure TfrmSamplelist_so.TBPrintClick(Sender: TObject);
var
  fPrintFile: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  if fCRFlag = 'SY' then
    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\SO样报表.rmf';
  if fCRFlag = 'CY' then
    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\色样报表.rmf';
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\' + Trim(fPrintFile) + '.rmf'), '提示', 0);
  end;
end;

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

procedure TfrmSamplelist_so.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  initGrid();
end;

procedure TfrmSamplelist_so.FormResize(Sender: TObject);
begin
  Panel2.Left := self.Width - Panel2.Width - 30;
  Panel2.Top := self.Height - Panel2.Height - 80;
end;

procedure TfrmSamplelist_so.Picture4DblClick(Sender: TObject);
begin
  if TcxDBImage(Sender).Picture.Height = 0 then
    exit;

  LookImage(ADOQueryImage.fieldbyname(TcxDBImage(Sender).Hint).asstring);
end;

procedure TfrmSamplelist_so.Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  InitImage();
end;

end.

