unit U_HCSHList;

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, RM_Common, RM_Class, RM_e_Xls, RM_Dataset,
  RM_System, RM_GridReport, Menus, cxCalendar, cxButtonEdit, cxTextEdit, BtnEdit,
  cxPC;

type
  TfrmHCSHList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    CDS_PRT: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ADOQueryPrint: TADOQuery;
    tbJS: TToolButton;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column21: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column24: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Panel1: TPanel;
    Label2: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label10: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    SPName: TEdit;
    BatchNo: TEdit;
    chkdate: TCheckBox;
    FactoryName: TEdit;
    cxTabControl1: TcxTabControl;
    tbJSCX: TToolButton;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    Label1: TLabel;
    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 N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure SPIDKeyPress(Sender: TObject; var Key: Char);
    procedure SPIDChange(Sender: TObject);
    procedure SPSpecChange(Sender: TObject);
    procedure FactoryNameChange(Sender: TObject);
  //  procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure tbJSClick(Sender: TObject);
    procedure tbJSCXClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    procedure SetStatus();
    procedure InitGrid();
    { Private declarations }
  public
    canshu1: string;
    { Public declarations }
  end;

var
  frmHCSHList: TfrmHCSHList;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmHCSHList.SetStatus();
begin
  tbJS.Visible := False;
  tbJSCX.Visible := False;
  if Trim(canshu1) = '管理' then
  begin
    case cxTabControl1.TabIndex of
      0:
        begin
          tbJS.Visible := true;

        end;
      1:
        begin

          tbJSCX.Visible := true;
        end;
      2:
        begin

        end;

    end;
  end;
end;

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

procedure TfrmHCSHList.FormCreate(Sender: TObject);
begin
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime - 30;

end;

procedure TfrmHCSHList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;

      if chkdate.checked = true then
      begin
        sql.Add('exec P_View_PCSH ');
        sql.Add(' @PageStatus=''' + inttostr(cxTabControl1.TabIndex) + '''');
        sql.Add(' ,@shtag=''1''');

        sql.Add(' ,@begdate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', begdate.DateTime))));
        sql.Add(',@Enddate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1))));
      end
      else
      begin
        sql.Add('exec P_View_PCSH2 ');
        sql.Add(' @PageStatus=''' + inttostr(cxTabControl1.TabIndex) + '''');
        sql.Add(' ,@shtag=''0''');

        sql.Add(' ,@begdate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', begdate.DateTime))));
        sql.Add(',@Enddate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1))));
      end;

//     showmessage(sql.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;

end;

procedure TfrmHCSHList.TBRafreshClick(Sender: TObject);
begin

  InitGrid();

end;

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

procedure TfrmHCSHList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption), Tv1, '库龄分析');
  Close;
end;

procedure TfrmHCSHList.FormShow(Sender: TObject);
begin
  SetStatus();

  ReadCxGrid(trim(self.Caption), Tv1, '库龄分析');

  InitGrid();

end;

procedure TfrmHCSHList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel('待检布入库列表', cxGrid2);
end;

procedure TfrmHCSHList.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 TfrmHCSHList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

procedure TfrmHCSHList.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, False);
end;

procedure TfrmHCSHList.SPIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    {if Length(Trim(SPID.Text))<4 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add(' select A.* ');
        sql.Add(',ConNo=(select ConNo from Contract_Main CM where CM.MainId=A.CGMainId)');
        //sql.Add(' ,ConNo=(select ConNo from Contract_Main CM where CM.MainId=A.CGMainId )');
        sql.Add(' from CK_SXPB_CR A');
        sql.add(' where SPID like :SXID');
        Parameters.ParamByName('SXID').Value:='%'+Trim(SXID.Text)+'%';
        Open;
        //ShowMessage(SQL.Text);
      end;
      SCreateCDS20(ADOQueryMain,CDS_Main);
      SInitCDSData20(ADOQueryMain,CDS_Main);
    finally
      ADOQueryMain.EnableControls;
    end; }
  end;
end;

procedure TfrmHCSHList.SPIDChange(Sender: TObject);
begin

  TBFind.Click;
end;

procedure TfrmHCSHList.SPSpecChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmHCSHList.FactoryNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

//procedure TfrmHCSHList.Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
//var
//  FYJSH, FSJSH: double;
//begin
//
//  if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('YJSH').Index] = null then
//    Exit;
//  if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('SJSH').Index] = null then
//    Exit;
//  FYJSH := AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('YJSH').Index];
//  FSJSH := AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('SJSH').Index];
//
//  if FYJSH < FSJSH then
//  begin
//
//    ACanvas.Brush.Color := $4763FF;
//  end;
//
//end;

procedure TfrmHCSHList.tbJSClick(Sender: TObject);
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('insert into  CK_SXPB_CR_BatchNo(BatchNo,GXNo,Factory) ');
      sql.Add('values( ' + QuotedStr(trim(CDS_Main.FieldByName('BatchNo').AsString)));
      sql.Add(' , ' + trim(CDS_Main.FieldByName('GXNO').AsString));
      sql.Add(' , ' + QuotedStr(trim(CDS_Main.FieldByName('FactoryName').AsString)));
      sql.Add(') ');
      execsql;
    end;
    application.MessageBox('订单审核成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('订单审核失败！', '提示信息', 0);
  end;
end;

procedure TfrmHCSHList.tbJSCXClick(Sender: TObject);
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   CK_SXPB_CR_BatchNo ');
      sql.Add('where BatchNo=' + quotedstr(trim(CDS_Main.fieldbyname('BatchNo').AsString)));
      sql.Add('and GXNo=' + trim(CDS_Main.fieldbyname('GXNO').AsString));
      sql.Add('and Factory=' + quotedstr(trim(CDS_Main.fieldbyname('FactoryName').AsString)));
      execsql;
    end;

    application.MessageBox('订单审核成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('订单审核失败！', '提示信息', 0);
  end;
end;

procedure TfrmHCSHList.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  InitGrid();
end;

procedure TfrmHCSHList.FormDestroy(Sender: TObject);
begin
  frmHCSHList := nil;
end;

end.

