unit U_DDDJCLList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxGrid, DBClient, cxCheckBox, cxCalendar, cxSplitter, RM_Dataset, RM_System,
  RM_Common, RM_Class, RM_GridReport, RM_e_Xls, Menus, MovePanel, cxTextEdit,
  cxContainer, cxCurrencyEdit, BtnEdit, RM_e_main, RM_e_htm, RM_e_Graphic,
  RM_e_Jpeg, RM_e_txt, RM_e_csv, RM_E_llPDF, Math, Clipbrd, cxPC, 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
  TfrmDDDJCLList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_2: TDataSource;
    TBExport: TToolButton;
    CDS_2: TClientDataSet;
    RM2: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    ADOQueryPrint: TADOQuery;
    RMXLSExport1: TRMXLSExport;
    Panel1: TPanel;
    Label3: TLabel;
    Label5: TLabel;
    Label11: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    orderno: TEdit;
    MPRTCodeName: TEdit;
    Label1: TLabel;
    PRTColor: TEdit;
    Label2: TLabel;
    MPRTCode: TEdit;
    Label4: TLabel;
    PRTHX: TEdit;
    cxPageControl1: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column33: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxTabSheet2: TcxTabSheet;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridDBColumn21: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    CDS_1: TClientDataSet;
    DS_1: TDataSource;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Tv1Column1: TcxGridDBColumn;
    TV2Column1: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure ordernoKeyPress(Sender: TObject; var Key: Char);
    procedure cxPageControl1Change(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    DQdate: TDateTime;
    FLeft, FTop: Integer;
    CurrentPage, RecordsNumber: Integer;
    procedure InitGrid();
    procedure SetStatus();
    { Private declarations }
  public
    FFInt: Integer;
    canshu1: string;
    { Public declarations }
  end;

var
  frmDDDJCLList: TfrmDDDJCLList;

implementation

uses
  U_DataLink, U_Fun;

{$R *.dfm}
procedure TfrmDDDJCLList.SetStatus();
var
  i: Integer;
begin

  case cxPageControl1.ActivePageIndex of
    0:
      begin
        ClearOrHideControls(Panel1, '0', False, True);
        ClearOrHideControls(Panel1, '1', True, False);
      end;
    1:
      begin
        ClearOrHideControls(Panel1, '0', false, True);
        ClearOrHideControls(Panel1, '1', True, True);
      end;
  end;
end;

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

procedure TfrmDDDJCLList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('打卷产量表1', Tv1, '进度管理');
  WriteCxGrid('打卷产量表2', Tv2, '进度管理');
  Close;
end;

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

  Pwhere := SGetFilters(Panel1, 1, 2);

  if trim(Pwhere) <> '' then
  begin
    fwhere := fwhere + ' and ' + trim(Pwhere);
  end;
  try
    ADOQueryMain.DisableControls;
    case cxPageControl1.ActivePageIndex of
      0:
        begin
          with ADOQueryMain do
          begin
            Filtered := False;
            Close;
            sql.Clear;
            sql.Add('select * from (   ');
            sql.Add('select AA.*  ');
            Sql.Add(',OrderUnit=(select top 1 OrderUnit from JYOrder_Sub X where X.MainId=AA.MainId) ');
            Sql.Add(',PRTOrderQty=(select sum(PRTOrderQty) OrderUnit from JYOrder_Sub X where X.MainId=AA.MainId) ');
            sql.Add(',B.khconNo,OrderNo=cast (B.OrderNo as varchar),B.MPRTCodeName,B.MPRTCode,B.MPRTMF,B.MPRTKZ');
            sql.Add(' from (select A.MainId  ');
            sql.Add(',BS=(SELECT COUNT(DISTINCT BAOID) FROM WFB_MJJY D WHERE D.MAINID=A.MAINID    ');
            sql.Add(' and  d.Filltime>=' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)));
            sql.Add(' and d.Filltime<' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)));
            sql.Add('and ISNULL(d.baoid,'''') <>''''');
            SQL.Add(')');
//            sql.Add(' and not exists (select CRType from CK_BanCP_CR X where CRType=''回修出库'' and X.MJID=d.MJID)  )');
            sql.Add(',CPJYps=SUM(case when A.MJType=''正品'' then 1 else 0 end)    ');
            sql.Add(',CPJYJZ=SUM(case when A.MJType=''正品'' then MJQty4 else 0 end)     ');
            sql.Add(',CPJYMZ=SUM(case when A.MJType=''正品'' then MJMaoZ else 0 end)   ');
            sql.Add(',CPJYQty=SUM(case when A.MJType=''正品'' then MJLen else 0 end)   ');
            sql.Add(',CPJYYQty=SUM(case when A.MJType=''正品'' then MJYLen else 0 end)  ');
            sql.Add(',CCPJYps=SUM(case when A.MJType=''次品'' then 1 else 0 end)        ');
            sql.Add(',CCPJYJZ=SUM(case when A.MJType=''次品'' then MJQty4 else 0 end)   ');
            sql.Add(',CCPJYMZ=SUM(case when A.MJType=''次品'' then MJMaoZ else 0 end)   ');
            sql.Add(',CCPJYQty=SUM(case when A.MJType=''次品'' then MJLen else 0 end)   ');
            sql.Add(',CCPJYYQty=SUM(case when A.MJType=''次品'' then MJYLen else 0 end)   ');
            sql.Add('from  WFB_MJJY A  ');
            sql.Add(' where  A.Filltime>=' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)));
            sql.Add(' and A.Filltime<' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)));
            sql.Add(' and not exists (select CRType from CK_BanCP_CR X where CRType=''回修出库'' and X.MJID=A.MJID)  ');
            sql.Add(' group by  MainId ');

            sql.Add(') AA inner join JYOrder_Main B on AA.MainId=B.MainId ');
            sql.Add(' ) AAA where 1=1 ' + fwhere);
//            ShowMessage(SQL.Text);
            Open;
          end;
          SCreateCDS20(ADOQueryMain, CDS_1);
          SInitCDSData20(ADOQueryMain, CDS_1);
        end;
      1:
        begin
          with ADOQueryMain do
          begin
            Filtered := False;
            Close;
            sql.Clear;
            sql.Add('exec P_View_MRDDDJCL ');
            sql.Add('@BegDate=' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)));
            sql.Add(',@EndDate=' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)));
            sql.Add(',@criteria= ' + quotedstr(fwhere));
//            ShowMessage(SQL.Text);
            Open;
          end;
          SCreateCDS20(ADOQueryMain, CDS_2);
          SInitCDSData20(ADOQueryMain, CDS_2);
        end;

    end;

  finally
    ADOQueryMain.EnableControls;
  end;
//  try
//    ADOQueryMain.DisableControls;
//    with ADOQueryMain do
//    begin
//      Filtered := False;
//      Close;
//      sql.Clear;
//      sql.Add('exec P_View_MRDDDJCL ');
//      sql.Add('@BegDate=' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)));
//      sql.Add(',@EndDate=' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)));
//      Open;
//    end;
//    SCreateCDS20(ADOQueryMain, CDS_Main);
//    SInitCDSData20(ADOQueryMain, CDS_Main);
//
//  finally
//    ADOQueryMain.EnableControls;
//  end;
end;

procedure TfrmDDDJCLList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  case cxPageControl1.ActivePageIndex of
    0:
      begin
        SelExportData(Tv1, ADOQueryMain, Self.Caption);
      end;

    1:
      begin
          SelExportData(Tv2, ADOQueryMain, Self.Caption);
      end;
  end;

end;

procedure TfrmDDDJCLList.TBRafreshClick(Sender: TObject);
begin
  OrderNo.SetFocus;
  InitGrid();
end;

procedure TfrmDDDJCLList.FormCreate(Sender: TObject);
var
  fsj: string;
begin
  ReadCxGrid('打卷产量表1', Tv1, '进度管理');
  ReadCxGrid('打卷产量表2', Tv2, '进度管理');
//  fsj := 'select distinct(Filler) name from WFB_MJJY   ';
//  SInitComBoxBySql(ADOQueryCmd, JYER, False, fsj);

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

procedure TfrmDDDJCLList.FormDestroy(Sender: TObject);
begin
  frmDDDJCLList := nil;
end;

procedure TfrmDDDJCLList.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  AssignmentControls(Panel1, CDS_1, '0');
  cxPageControl1.ActivePageIndex := 1;
end;

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

procedure TfrmDDDJCLList.cxPageControl1Change(Sender: TObject);
begin
  SetStatus();
  InitGrid();
end;

procedure TfrmDDDJCLList.FormShow(Sender: TObject);
begin
  SetStatus();
end;

end.

