unit U_CKYCLKC;

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;

type
  TfrmCKYCLKC = 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;
    Label3: TLabel;
    Label4: TLabel;
    Label7: TLabel;
    YCLName: TEdit;
    YCLSpec: TEdit;
    GYSName: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    Tv1: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    v2Column1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    ToolButton1: TToolButton;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure YCLNameChange(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton1Click(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCKYCLKC: TfrmCKYCLKC;

implementation
uses
  U_DataLink,U_Fun,U_CRMX;

{$R *.dfm}

procedure TfrmCKYCLKC.FormDestroy(Sender: TObject);
begin
  frmCKYCLKC:=nil;
end;

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

procedure TfrmCKYCLKC.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  BegDate.DateTime:=SGetServerDateTime(ADOQueryTemp)-30;
  EndDate.DateTime:=SGetServerDateTime(ADOQueryTemp)
end;

procedure TfrmCKYCLKC.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      SQL.Add('exec CK_YCL_CRCHZ :begdate,:enddate,:CKName');
      Parameters.ParamByName('begdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime));
      Parameters.ParamByName('enddate').Value:=Trim(FormatDateTime('yyyy-MM-dd',enddate.DateTime+1));
      Parameters.ParamByName('CKName').Value:=Trim(DParameters1);
      Open;
      //ShowMessage(SQL.Text);
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCKYCLKC.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;
  InitGrid();
end;

procedure TfrmCKYCLKC.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  end;
end;

procedure TfrmCKYCLKC.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('原材料库存2',Tv1,'原材料仓库');
  Close;
end;

procedure TfrmCKYCLKC.FormShow(Sender: TObject);
begin

  ReadCxGrid('原材料库存2',Tv1,'原材料仓库');
  if Trim(DParameters2)='原料类型' then
  begin
    ToolButton1.Visible:=True;
    v2Column9.Options.Focusing:=True;
    v2Column9.Visible:=True;
  end else
  begin
    ToolButton1.Visible:=False;
    v2Column9.Options.Focusing:=False;
    v2Column9.Visible:=False;
  end;
  InitGrid();
end;

procedure TfrmCKYCLKC.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then exit;
  TcxGridToExcel(Trim(DParameters1)+'库存',cxGrid2);
end;

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

procedure TfrmCKYCLKC.Tv1CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  try
    frmCRMX:=TfrmCRMX.Create(Application);
    with frmCRMX do
    begin
      Fbegdate:=FormatDateTime('yyyy-MM-dd',Self.BegDate.DateTime);
      Fenddate:=FormatDateTime('yyyy-MM-dd',Self.enddate.DateTime+1);
      {FGYS:=Trim(Self.CDS_Main.fieldbyname('GYS').AsString);
      FYCLCode:=Trim(Self.CDS_Main.fieldbyname('YCLCode').AsString);
      FYCLSpec:=Trim(Self.CDS_Main.fieldbyname('YCLSpec').AsString);
      FCRUnit:=Trim(Self.CDS_Main.fieldbyname('KCUint').AsString);  }
      CRID:=Trim(Self.CDS_Main.fieldbyname('CRID').AsString);
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmCRMX.Free;
  end;
end;

procedure TfrmCKYCLKC.ToolButton1Click(Sender: TObject);
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    BegDate.SetFocus;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while not Eof do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update CK_YCL_KC Set KCType='''+Trim(CDS_Main.fieldbyname('KCType').AsString)+'''');
          SQL.Add(' where CRID='+CDS_Main.fieldbyname('CRID').AsString);
          ExecSQL;
        end;
        Next;
      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功！','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;

end;

end.
