unit U_YPCKKCList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridCustomTableView,
  cxGridTableView, cxGridBandedTableView, cxGridDBBandedTableView,
  cxGridLevel, cxClasses, cxControls, cxGridCustomView, cxGridDBTableView,
  cxGrid, StdCtrls, ComCtrls, ExtCtrls, ToolWin, cxGridCustomPopupMenu,
  cxGridPopupMenu, ADODB, DBClient, cxDropDownEdit, cxCheckBox, Menus,
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdFTP,ShellAPI,IniFiles,
  cxTextEdit, cxButtonEdit,jpeg;
 //DParameters2:='允许更新备注'
type
  TfrmYPCKKCList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    CheckBox1: TCheckBox;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column3: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1P_ChnName: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Quantity: TcxGridDBColumn;
    v1UnitName: TcxGridDBColumn;
    VKCNote: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Label1: TLabel;
    Label5: TLabel;
    Label3: TLabel;
    Label7: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    CYName: TEdit;
    CYSpec: TEdit;
    CYNO: TEdit;
    CYKZ: TEdit;
    CYECF: TEdit;
    CYMF: TEdit;
    Label2: TLabel;
    v1Column1: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    Image2: TImage;
    CYType: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure MPRTCodeNameChange(Sender: TObject);
    procedure CYNoChange(Sender: TObject);
    procedure VKCNotePropertiesEditValueChanged(Sender: TObject);
    procedure CYSpecChange(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
  private
    procedure InitGrid();
    procedure ReadINIFile();
    procedure InitImage(FFCYID:string);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmYPCKKCList: TfrmYPCKKCList;

implementation
uses
  U_DataLink,U_RTFun;

{$R *.dfm}

procedure TfrmYPCKKCList.FormDestroy(Sender: TObject);
begin
  frmYPCKKCList:=nil;
end;

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

procedure TfrmYPCKKCList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      SQL.Add('select  A.CRID,A.BCID,A.KCQty,A.KCQtyUnit,A.CYColor,A.KCNote,A.CYType ');
      sql.Add(' ,A.CRPrice,A.BZTYpe,A.PBFactory,A.RCFactory,A.HJGFactory,A.CYID,A.WeiZhi,A.MiLenNote');
      SQL.Add(',B.CYNO,B.CYName,B.CYMF,B.CYKZ,B.CYSpec,B.CYCF,B.CYHX,B.CYEName,B.CYECF,B.CYColorEng');
      sql.Add(',MLen=dbo.F_Get_MLen(A.CYID)');
      sql.Add(' from CK_YP_KC A ');
      sql.Add(' left join CP_YDang B on A.CYID=B.CYID ');
      if CheckBox1.Checked=False then
        sql.add('where A.KCQty>0')
      else
        sql.Add('where 1=1 ');
      {SQL.Add(' order by ');
      sql.Add(' substring(WeiZhi,1,1),Cast(substring(WeiZhi,2,charindex(''-'',WeiZhi)-2) as Int)');
      sql.Add(',Cast(substring(WeiZhi,charindex(''-'',WeiZhi)+1,len(WeiZhi)-charindex(''-'',WeiZhi)) as Int)'); }
      //ShowMessage(SQL.Text);
      Open;

    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmYPCKKCList.TBRafreshClick(Sender: TObject);
begin
  CYNo.SetFocus;
  InitGrid();
end;

procedure TfrmYPCKKCList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('样品库存列表AS',Tv1,'样品仓库');
  Close;
end;

procedure TfrmYPCKKCList.FormShow(Sender: TObject);
begin

  ReadCxGrid('样品库存列表AS',Tv1,'样品仓库');
  
  //InitGrid();
  if Trim(DParameters2)='允许更新备注' then
  begin
    VKCNote.Options.Focusing:=True;
  end else
  begin
    VKCNote.Options.Focusing:=False;
  end;
end;

procedure TfrmYPCKKCList.TBExportClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then exit;
  TcxGridToExcel('库存列表',cxGrid1);
end;

procedure TfrmYPCKKCList.TBFindClick(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 TfrmYPCKKCList.MPRTCodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmYPCKKCList.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 TfrmYPCKKCList.CYNoChange(Sender: TObject);
begin
  if Trim(CYNO.Text)<>'' then
  begin
    if Length(CYNo.Text)<3 then Exit;
  end;

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

procedure TfrmYPCKKCList.VKCNotePropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  with CDS_Main do
  begin
    Edit;
    FieldByName('KCNote').Value:=Trim(mvalue);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate CK_YP_KC set KCNote='''+Trim(mvalue)+'''');
    sql.Add(' where CRID='+Cds_Main.fieldbyname('CRID').AsString);
    ExecSQL;
  end;
end;

procedure TfrmYPCKKCList.CYSpecChange(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 TfrmYPCKKCList.Tv1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if CDS_Main.IsEmpty then Exit;
  InitImage(Trim(CDS_Main.fieldbyname('CYID').AsString));
end;
procedure TfrmYPCKKCList.InitImage(FFCYID:string);
var
  jpg:TJpegImage;
  myStream:TADOBlobStream;
begin
  if Trim(FFCYID)='' 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(FFCYID)+'''');
        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;

end.
