unit U_KKKWCXList;

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, dxSkinsCore, dxSkinBlack, dxSkinBlue, dxSkinBlueprint,
  dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray, dxSkinOffice2013LightGray,
  dxSkinOffice2013White, dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic,
  dxSkinSharp, dxSkinSharpPlus, dxSkinSilver, dxSkinSpringTime, dxSkinStardust,
  dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinsDefaultPainters,
  dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue,
  dxSkinscxPCPainter;

type
  TfrmKKKWCXList = 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;
    conno: TEdit;
    RKplace: TEdit;
    Label6: TLabel;
    cangku: TEdit;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column2: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    ADOQueryPrint: TADOQuery;
    v1Column21: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    IsJYTime: TCheckBox;
    N3: TMenuItem;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Label1: TLabel;
    RCgangno: TEdit;
    ToolButton1: TToolButton;
    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 connoKeyPress(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);
    procedure ToolButton1Click(Sender: TObject);
  private
    CurrentPage, RecordsNumber: Integer;
    procedure InitGrid();
    { Private declarations }
  public
    canshu1: string;
    { Public declarations }
  end;

var
  frmKKKWCXList: TfrmKKKWCXList;

implementation

uses
  U_DataLink, U_RTFun, U_Fun;

{$R *.dfm}

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

procedure TfrmKKKWCXList.FormCreate(Sender: TObject);
var
  fsj: string;
begin

  BegDate.DateTime := SGetServerDate10(ADOQueryTemp) - 180;
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);

  fsj := 'select distinct(WSTaiTou) name from CK_SXPB_CR   ';

end;

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

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      SQL.Add('exec  P_select_Kkkw  ');
      sql.Add(' @Begdate=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + ''' ');
      sql.Add(',@Enddate=''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + ''' ');
//      sql.Add('select case when isnull(zpps,0)>0 then  (select max(filler) from  CK_BanCP_CR X where X.apid=A.spid and filler<>''女工检验'' )  else Filler end  as Filler,A.*  ');
//      sql.Add(',case when (PiQty-isnull(ZPPS,0)-isnull(CPPS,0))>0 then (PiQty-isnull(ZPPS,0)-isnull(CPPS,0))  else 0 end  as WDJPS ');
//      sql.Add(',ydjps=(isnull(ZPPS,0)+isnull(CPPS,0))');
//      sql.Add(',RKplace=(select max(RKplace) from  CK_BanCP_CR X    where X.apid=A.spid )');
//      sql.Add('from CK_SXPB_CR A  where A.CKName= ''待检布'' and A.CRType in ( ''回修完成'', ''加工完成'', ''客户退货'', ''改色完成'') ');
//      sql.Add(' and CRTime>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
//      sql.Add(' and CRTime<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
//      sql.Add(' and    (select CPKCPS from JYOrder_Main X where X.mainid=A.ORDMainIdRK)>0');
////      sql.Add(',@criteria= '  );
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);              

  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

  CurrentPage := 1;
  InitGrid();
end;

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

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

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

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

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

procedure TfrmKKKWCXList.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 TfrmKKKWCXList.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 TfrmKKKWCXList.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmKKKWCXList.connoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    ToolButton1.Click;
//    InitGrid();
  end;
end;

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

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

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

procedure TfrmKKKWCXList.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 TfrmKKKWCXList.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 TfrmKKKWCXList.CheckBox1Click(Sender: TObject);
begin
  if CheckBox1.Checked then
  begin
    FactoryName.Tag := 1;
  end
  else
  begin
    FactoryName.Tag := 2;
  end;
  InitGrid();
end;

procedure TfrmKKKWCXList.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 TfrmKKKWCXList.FormDestroy(Sender: TObject);
begin
  frmKKKWCXList := nil;
end;

procedure TfrmKKKWCXList.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  end;
end;

end.

