unit U_PBRkCKlist;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, StdCtrls, ExtCtrls, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData,
  cxButtonEdit, cxDropDownEdit, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls, cxGridCustomView,
  cxGrid, DBClient, ADODB, cxGridCustomPopupMenu, cxGridPopupMenu, cxContainer,
  cxTextEdit, cxCurrencyEdit, cxCheckBox, RM_System, RM_Dataset, RM_Common,
  RM_Class, RM_GridReport, cxPC, Menus, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator;

type
  TfrmPBRkCKlist = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1P_CodeName: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1CRType: TcxGridDBColumn;
    v1CRTime: TcxGridDBColumn;
    Label2: TLabel;
    Label3: TLabel;
    CodeName: TEdit;
    CRType: TComboBox;
    Label5: TLabel;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ckName: TComboBox;
    Label6: TLabel;
    v1P_Code: TcxGridDBColumn;
    VC_CRID: TcxGridDBColumn;
    Label7: TLabel;
    CRNo: TEdit;
    v1Price: TcxGridDBColumn;
    v1money: TcxGridDBColumn;
    gangNo: TEdit;
    Label10: TLabel;
    RMDB_Main: TRMDBDataSet;
    ADOQueryPrint: TADOQuery;
    RM1: TRMGridReport;
    v1Column10: TcxGridDBColumn;
    Label17: TLabel;
    fromFactoryName: TEdit;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ADOQueryMain: TADOQuery;
    VC_PS: TcxGridDBColumn;
    Label4: TLabel;
    kuwei: TEdit;
    v1Column7: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    VC_LLKCPS: TcxGridDBColumn;
    VC_CRNO: TcxGridDBColumn;
    CDS_KC: TClientDataSet;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure CodeNameChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure VC_CRIDCompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
    procedure VC_CRNOCompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
    procedure CDS_MainBeforeOpen(DataSet: TDataSet);
    procedure Tv1DataControllerFilterBeforeChange(Sender: TcxDBDataFilterCriteria; ADataSet: TDataSet; const AFilterText: string);
    procedure Tv1DataControllerSummaryFooterSummaryItemsSummary(ASender: TcxDataSummaryItems; Arguments: TcxSummaryEventArguments; var OutArguments: TcxSummaryEventOutArguments);
    procedure Tv1TcxGridDBDataControllerTcxDataSummaryFooterSummaryItems4GetText(Sender: TcxDataSummaryItem; const AValue: Variant; AIsFooter: Boolean; var AText: string);
    procedure Tv1TcxGridDBDataControllerTcxDataSummaryFooterSummaryItems7GetText(Sender: TcxDataSummaryItem; const AValue: Variant; AIsFooter: Boolean; var AText: string);
  private
    procedure InitGrid();
    procedure SetStatus();
    { Private declarations }
  public
    fCKName, canshu1: string;
    fCRType, FZZType: string;
    FRKPS, FKCPS: Integer;
    { Public declarations }
  end;

//var
//  frmPBRklist: TfrmPBRklist;

implementation

uses
  U_DataLink, U_Fun10, U_ZDYHelp, U_PBRk, U_FjList_RZ;
{$R *.dfm}

procedure TfrmPBRkCKlist.SetStatus();
begin
//  TBEdit.Visible:=False;
//  ToolButton1.Visible:=False;
//  TBDel.Visible:=False;
//  TBPrint2.Visible:=False;
//  TBPrint1.Visible:=False;
//  ToolButton2.Visible:=False;
//  ToolButton3.Visible:=False;
//  if Trim(DParameters2)='高权限' then
//  begin
//    case cxTabControl1.TabIndex of
//    0:begin
//        TBEdit.Visible:=true;
//        ToolButton1.Visible:=true;
//        TBDel.Visible:=true;
//        ToolButton2.Visible:=true;
//        TBPrint1.Visible:=true;
//      end;
//    1:begin
//        TBPrint2.Visible:=true;
//        TBPrint1.Visible:=true;
//        ToolButton3.Visible:=true;
//      end;
//    2:begin
//
//      end;
//    end
//  end
//  else
//  begin
//    case cxTabControl1.TabIndex of
//    0:begin
//        TBEdit.Visible:=true;
//        ToolButton1.Visible:=true;
//        TBDel.Visible:=true;
//        TBPrint1.Visible:=true;
//      end;
//    1:begin
//        TBPrint2.Visible:=true;
//        TBPrint1.Visible:=true;
//      end;
//    2:begin
//
//      end;
//    end
//  end;
end;

procedure TfrmPBRkCKlist.InitGrid();
begin
  try
    with ADOQueryMain do
    begin
      close;
      sql.Clear;
      sql.Add(' exec P_View_PBCHB ');
      sql.Add('@Begdate=''' + formatdateTime('yyyy-MM-dd', begdate.Date) + ''' ');
      sql.Add(',@enddate=''' + formatdateTime('yyyy-MM-dd', enddate.Date + 1) + ''' ');
      open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
    TBFind.Click;
  finally
  end;
end;

procedure TfrmPBRkCKlist.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := cafree;
end;

procedure TfrmPBRkCKlist.FormDestroy(Sender: TObject);
begin
//  frmPBRklist:=nil;
end;

procedure TfrmPBRkCKlist.CodeNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryMain, CDS_Main);
  SInitCDSData20(ADOQueryMain, CDS_Main);
end;

procedure TfrmPBRkCKlist.FormShow(Sender: TObject);
begin
  readCxGrid(self.Caption, Tv1, '面料仓库');
  SetStatus();

  CDS_KC.FieldDefs.Clear;
  CDS_KC.FieldDefs.Add('CRID', ftString, 60);
  CDS_KC.FieldDefs.Add('RKPS', ftInteger, 0);
  CDS_KC.FieldDefs.Add('KCPS', ftInteger, 0);
  CDS_KC.Close;
  CDS_KC.CreateDataSet;

//  InitGrid();
end;

procedure TfrmPBRkCKlist.FormCreate(Sender: TObject);
begin
  EndDate.Date := SGetServerDate10(ADOQueryTmp);
  BegDate.Date := EndDate.Date - 7;
  cxGrid1.Align := alclient;
end;

procedure TfrmPBRkCKlist.TBCloseClick(Sender: TObject);
begin
  writeCxGrid(self.Caption, Tv1, '面料仓库');
  close;
end;

procedure TfrmPBRkCKlist.TBRafreshClick(Sender: TObject);
begin
  initGrid();
end;

procedure TfrmPBRkCKlist.TBExportClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxgrid1);
end;

procedure TfrmPBRkCKlist.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryMain, CDS_Main);
  SInitCDSData20(ADOQueryMain, CDS_Main);
end;

procedure TfrmPBRkCKlist.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

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

procedure TfrmPBRkCKlist.VC_CRIDCompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
begin
  if ARow1.Values[VC_CRID.Index] = ARow2.Values[VC_CRID.Index] then
    AAreEqual := True
  else
    AAreEqual := False;
end;

procedure TfrmPBRkCKlist.VC_CRNOCompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
begin
  if ARow1.Values[VC_CRNO.Index] = ARow2.Values[VC_CRNO.Index] then
    AAreEqual := True
  else
    AAreEqual := False;
end;

procedure TfrmPBRkCKlist.CDS_MainBeforeOpen(DataSet: TDataSet);
begin
  CDS_KC.EmptyDataSet;
end;

procedure TfrmPBRkCKlist.Tv1DataControllerFilterBeforeChange(Sender: TcxDBDataFilterCriteria; ADataSet: TDataSet; const AFilterText: string);
begin
  CDS_KC.EmptyDataSet;
end;

procedure TfrmPBRkCKlist.Tv1DataControllerSummaryFooterSummaryItemsSummary(ASender: TcxDataSummaryItems; Arguments: TcxSummaryEventArguments; var OutArguments: TcxSummaryEventOutArguments);
var
  MCRID: string;
begin
  MCRID := vartostr(ASender.DataController.Values[Arguments.RecordIndex, VC_CRID.Index]);
  if TcxGridDBTableSummaryItem(Arguments.SummaryItem).Column = VC_PS then
  begin
    if not CDS_KC.Locate('CRID', MCRID, []) then
    begin
      with CDS_KC do
      begin
        Append;
        FieldByName('RKPS').Value := StrToIntDef(vartostr(ASender.DataController.Values[Arguments.RecordIndex, VC_PS.Index]), 0);
        FieldByName('KCPS').Value := StrToIntDef(vartostr(ASender.DataController.Values[Arguments.RecordIndex, VC_LLKCPS.Index]), 0);
        Post;
      end;
    end;
  end;
end;

procedure TfrmPBRkCKlist.Tv1TcxGridDBDataControllerTcxDataSummaryFooterSummaryItems4GetText(Sender: TcxDataSummaryItem; const AValue: Variant; AIsFooter: Boolean; var AText: string);
var
  MPS: Integer;
begin
  MPS := 0;
  if CDS_KC.IsEmpty then
    Exit;
  with CDS_KC do
  begin
    First;
    while not eof do
    begin
      MPS := MPS + FieldByName('RKPS').AsInteger;
      Next;
    end;
  end;
  AText := IntToStr(MPS);
end;

procedure TfrmPBRkCKlist.Tv1TcxGridDBDataControllerTcxDataSummaryFooterSummaryItems7GetText(Sender: TcxDataSummaryItem; const AValue: Variant; AIsFooter: Boolean; var AText: string);
var
  MPS: Integer;
begin
  MPS := 0;
  if CDS_KC.IsEmpty then
    Exit;
  with CDS_KC do
  begin
    First;
    while not eof do
    begin
      if FieldByName('KCPS').AsInteger > 0 then
        MPS := MPS + FieldByName('KCPS').AsInteger;
      Next;
    end;
  end;
  AText := IntToStr(MPS);
end;

end.

