unit U_DJBKKListHD;

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, Math, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxBarBuiltInMenu;

type
  TfrmDJBKKListHD = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    CDS_PRT: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Label3: TLabel;
    Label5: TLabel;
    Label8: TLabel;
    SPName: TEdit;
    FactoryName: TEdit;
    SPID: TEdit;
    SPSpec: TEdit;
    Label6: TLabel;
    SPCF: TEdit;
    Label7: TLabel;
    OrderNo: TEdit;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Label12: TLabel;
    CRType: TComboBox;
    v1Column19: TcxGridDBColumn;
    Label11: TLabel;
    RCGangNo: TEdit;
    ADOQueryPrint: TADOQuery;
    v1Column21: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    Label16: TLabel;
    PBFactoryName: TEdit;
    v1Column5: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    v1Column24: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    Label9: TLabel;
    conno: TEdit;
    Label10: TLabel;
    SPColor: TEdit;
    v1Column25: TcxGridDBColumn;
    Panel6: TPanel;
    Label31: TLabel;
    LBCPAP: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TComboBox;
    IsJYTime: TCheckBox;
    v1Column26: TcxGridDBColumn;
    v1Column27: TcxGridDBColumn;
    cxgrdISHD: TcxGridDBColumn;
    v1Column28: TcxGridDBColumn;
    Label1: TLabel;
    WSTaiTou: TComboBox;
    v1Column29: TcxGridDBColumn;
    N3: TMenuItem;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    v1Column30: TcxGridDBColumn;
    Label4: TLabel;
    DJLockStr: TComboBox;
    v1Column31: TcxGridDBColumn;
    Label13: TLabel;
    XiaoPiType: TComboBox;
    Label14: TLabel;
    XiaoPiStr: TComboBox;
    v1Column32: TcxGridDBColumn;
    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 N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure SPIDKeyPress(Sender: TObject; var Key: Char);
    procedure CRTypeChange(Sender: TObject);
    procedure BTLPClick(Sender: TObject);
    procedure BTNPClick(Sender: TObject);
    procedure v1Column28PropertiesEditValueChanged(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    CurrentPage, RecordsNumber: Integer;
    procedure InitGrid();
    { Private declarations }
  public
    canshu1: string;
    { Public declarations }
  end;

var
  frmDJBKKListHD: TfrmDJBKKListHD;

implementation

uses
  U_DataLink, U_RTFun, U_Fun;

{$R *.dfm}


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

procedure TfrmDJBKKListHD.FormCreate(Sender: TObject);
var
  fsj: string;
begin
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime;

  fsj := 'select distinct(WSTaiTou) name from CK_SXPB_CR   ';
  SInitComBoxBySql(ADOQueryCmd, WSTaiTou, False, fsj);
end;

procedure TfrmDJBKKListHD.InitGrid();
var
  fwhere, Pwhere: string;
begin

  if IsJYTime.Checked then
  begin
    fwhere := ' where CRTime>=' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime));
    fwhere := fwhere + ' and CRTime<' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1));
  end
  else
    fwhere := ' where 1=1 ';
  Pwhere := SGetFilters(Panel1, 1, 2);
  if Pwhere <> '' then
    fwhere := fwhere + ' and ' + Pwhere;
  case cxTabControl1.TabIndex of
    0:
      begin
        fwhere := fwhere + ' and isnull(ISHD,0)=0 '
      end;
    1:
      begin
        fwhere := fwhere + ' and isnull(ISHD,0)=1 '
      end;
  end;

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('exec P_Page_KaiKa ');
      sql.Add('@pageIndex=' + inttostr(CurrentPage));
      sql.Add(',@pageSize=' + inttostr(RecordsNumber));
      sql.Add(',@criteria= ' + quotedstr(fwhere));
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
    LBCPAP.Caption := inttostr(CurrentPage) + '/' + inttostr(ceil(CDS_Main.FieldByName('TotalCount').AsInteger / RecordsNumber));

  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmDJBKKListHD.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;

  CurrentPage := 1;
  InitGrid();
end;

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

procedure TfrmDJBKKListHD.TBCloseClick(Sender: TObject);
begin
  if Trim(DName)='ADMIN' then
  begin
    WriteCxGridAll(trim(self.Caption), Tv1,ADOQueryCmd,ADOQueryTemp);
  end;
  Close;
end;

procedure TfrmDJBKKListHD.FormShow(Sender: TObject);
begin
  CurrentPage := 1;
  RecordsNumber := 500;

  ReadCxGridAll(trim(self.Caption), Tv1);
  InitGrid();
end;

procedure TfrmDJBKKListHD.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel('开卡核对列表', cxGrid2);
end;

procedure TfrmDJBKKListHD.N1Click(Sender: TObject);
var
  i:integer;
begin
 Screen.Cursor := crHourGlass;
 tv1.DataController.GotoFirst;
 for i := 0 to tv1.DataController.FilteredRecordCount - 1 do
 begin
   tv1.DataController.GetItemByFieldName('ssel').EditValue  := True;
   tv1.DataController.GotoNext;
 end;
 Screen.Cursor := crDefault;
end;

procedure TfrmDJBKKListHD.N2Click(Sender: TObject);
var
  i:integer;
begin
 Screen.Cursor := crHourGlass;
 tv1.DataController.GotoFirst;
 for i := 0 to tv1.DataController.FilteredRecordCount - 1 do
 begin
   tv1.DataController.GetItemByFieldName('ssel').EditValue  := False;
   tv1.DataController.GotoNext;
 end;
 Screen.Cursor := crDefault;
end;

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

procedure TfrmDJBKKListHD.SPIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    InitGrid();
  end;
end;

procedure TfrmDJBKKListHD.CRTypeChange(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmDJBKKListHD.BTLPClick(Sender: TObject);
begin
  if CurrentPage > 1 then
    CurrentPage := CurrentPage - 1;
  InitGrid();
end;

procedure TfrmDJBKKListHD.BTNPClick(Sender: TObject);
begin
  if CurrentPage < CDS_Main.FieldByName('TotalCount').AsInteger / RecordsNumber then
    CurrentPage := CurrentPage + 1;
  InitGrid();
end;

procedure TfrmDJBKKListHD.v1Column28PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxCheckBox(Sender).EditValue;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    if mvalue = 'False' then
      sql.Add('update CK_SXPB_CR set ISHD=0 where SPID=' + quotedstr(Trim(CDS_Main.fieldbyname('SPID').AsString)))
    else
      sql.Add('update CK_SXPB_CR set ISHD=1 where SPID=' + quotedstr(Trim(CDS_Main.fieldbyname('SPID').AsString)));

    sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
    sql.Add(' ' + quotedstr(trim(DName)));
    sql.Add(',getdate() ');
    sql.Add(',' + quotedstr(trim(self.Caption)));
    sql.Add(',' + quotedstr(trim('修改核对开卡')));
    sql.Add(',' + quotedstr(Trim(CDS_Main.fieldbyname('SPID').AsString)));
    sql.Add(',' + quotedstr(trim('成功')));
    sql.Add(')');
    execsql;
  end;
end;

procedure TfrmDJBKKListHD.N3Click(Sender: TObject);
begin

  with CDS_Main do
  begin
    first;
    while not Eof do
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;

        sql.Add('update CK_SXPB_CR set ISHD=1 where SPID=' + quotedstr(Trim(CDS_Main.fieldbyname('SPID').AsString)));

        sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
        sql.Add(' ' + quotedstr(trim(DName)));
        sql.Add(',getdate() ');
        sql.Add(',' + quotedstr(trim(self.Caption)));
        sql.Add(',' + quotedstr(trim('修改核对开卡')));
        sql.Add(',' + quotedstr(Trim(CDS_Main.fieldbyname('SPID').AsString)));
        sql.Add(',' + quotedstr(trim('成功')));
        sql.Add(')');
        execsql;
      end;
      Next;
    end;
  end;

end;

procedure TfrmDJBKKListHD.CheckBox1Click(Sender: TObject);
begin
  if CheckBox1.Checked then
  begin
    FactoryName.Tag := 1;
  end
  else
  begin
    FactoryName.Tag := 2;
  end;
  InitGrid();
end;

procedure TfrmDJBKKListHD.CheckBox2Click(Sender: TObject);
var
  FTag, i: Integer;
begin

  if CheckBox2.Checked then
  begin
    FTag := 1;
  end
  else
  begin
    FTag := 2;
  end;

  with Panel1 do
  begin
    for i := 0 to ControlCount - 1 do
    begin
      if Controls[i] is TLabel then
        continue;

      if Controls[i] is TEdit then
      begin

        TEdit(Controls[i]).Tag := FTag;

      end
      else if Controls[i] is TComboBox then
      begin

        TComboBox(Controls[i]).Tag := FTag;
      end;
    end;
  end;
  InitGrid();

end;

procedure TfrmDJBKKListHD.FormDestroy(Sender: TObject);
begin
  frmDJBKKListHD:=nil;
end;

end.

