unit U_DJBRKList_DCGCLMonth;

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;

type
  TfrmDJBRKList_DCGCLMonth = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    Label3: TLabel;
    SCPerson: TEdit;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1APMonth: TcxGridDBColumn;
    v1SCPerson: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1MJMaoZ: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    v1PS: TcxGridDBColumn;
    v1APYear: TcxGridDBColumn;
    Label2: TLabel;
    APYear: TEdit;
    CheckBox1: TCheckBox;
    Label1: TLabel;
    APMonth: TComboBox;
    Label4: TLabel;
    EndMonth: TComboBox;
    EndYear: TEdit;
    Label5: TLabel;
    Label6: TLabel;
    C_CodeName: TEdit;
    v1C_CodeName: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    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 ConNoChange(Sender: TObject);
    procedure SPSpecChange(Sender: TObject);
    procedure BTNoteChange(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
  private
    canshu1,canshu2:string;
    procedure InitGrid();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmDJBRKList_DCGCLMonth: TfrmDJBRKList_DCGCLMonth;

implementation
uses
  U_DataLink,U_RTFun,U_DJBCKInPut,U_DJBCKInPut_JTZS;

{$R *.dfm}

procedure TfrmDJBRKList_DCGCLMonth.FormDestroy(Sender: TObject);
begin
  frmDJBRKList_DCGCLMonth:=nil;
end;

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

procedure TfrmDJBRKList_DCGCLMonth.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  canshu1:=Trim(DParameters1);
  canshu2:=Trim(DParameters2);
  APYear.text:=uppercase(formatdatetime('yyyy',SGetServerDate10(ADOQueryTemp)));
  APMonth.text:=uppercase(formatdatetime('MM',SGetServerDate10(ADOQueryTemp)));
  EndMonth.text:=uppercase(formatdatetime('MM',SGetServerDate10(ADOQueryTemp)));
  EndYear.Text:=uppercase(formatdatetime('yyyy',SGetServerDate10(ADOQueryTemp)));
end;

procedure TfrmDJBRKList_DCGCLMonth.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    if checkbox1.Checked=False then
    begin
      v1APMonth.Visible:=true;
      with ADOQueryMain do
      begin
        Close;
        sql.Clear;
        sql.Add('select SUM(PP.MJMaoZ) MJMaoZ,SUM(PP.PS) ps,Year(AP.APDate) APYear,Month(AP.APDate) APMonth,AP.SCPerson,C_CodeName ');
        sql.add(',ZQty=(select Sum(Z.Qty) from Cloth_JiTaiZShu Z where Z.SCPerson=AP.SCPerson and Z.Subid=PP.Subid and Month(Z.ZSDate)=Month(AP.APDate) and year(Z.ZSDate)=year(AP.APDate)) ');
        sql.add('from (select SUM(P.MJMaoZ) MJMaoZ,count(P.MJID) PS,P.APID,P.Subid ');
        sql.add(',C_CodeName=(select Top 1 C_CodeName from Cloth_Sub B where B.Subid=P.Subid)');
        sql.add('from WFB_MJJY_PB P group by P.APID,P.Subid) PP ');
        sql.add('inner join JYCon_Sub_AnPai AP on AP.APID=PP.APID');
        sql.add('where Year(AP.APDate)>='''+trim(APYear.Text)+''' and Year(AP.APDate)<='''+trim(EndYear.Text)+''' ');
        sql.Add('and Month(AP.APDate)>='''+trim(APMonth.Text)+''' and Month(AP.APDate)<='''+trim(EndMonth.Text)+''' ');
        if SCPerson.Text<>'' then
        sql.add('and AP.SCPerson like '''+'%'+trim(SCPerson.Text)+'%'+''' ');
        if C_CodeName.Text<>'' then
        sql.add('and PP.C_CodeName like '''+'%'+trim(C_CodeName.Text)+'%'+''' ');
        sql.Add('group by AP.SCPerson,PP.C_CodeName,PP.Subid,Year(AP.APDate),Month(AP.APDate) ');
        sql.add('having Sum(PP.PS)<>0 ');
        Open;
      end;
    end
    else
    begin
      v1APMonth.Visible:=False;
      with ADOQueryMain do
      begin
        Close;
        sql.Clear;
        sql.Add('select SUM(PP.MJMaoZ) MJMaoZ,SUM(PP.PS) ps,Year(AP.APDate) APYear,AP.SCPerson,PP.C_CodeName ');
        sql.add(',ZQty=(select Sum(Z.Qty) from Cloth_JiTaiZShu Z where Z.SCPerson=AP.SCPerson and Z.Subid=PP.Subid and year(Z.ZSDate)=year(AP.APDate)) ');
        sql.add('from (select SUM(P.MJMaoZ) MJMaoZ,count(P.MJID) PS,P.APID,P.Subid ');
        sql.add(',C_CodeName=(select Top 1 C_CodeName from Cloth_Sub B where B.Subid=P.Subid)');
        sql.add('from WFB_MJJY_PB P group by P.APID,P.Subid) PP ');
        sql.add('inner join JYCon_Sub_AnPai AP on AP.APID=PP.APID');
        sql.add('where Year(AP.APDate)>='''+trim(APYear.Text)+''' and Year(AP.APDate)<='''+trim(EndYear.Text)+''' ');
        if SCPerson.Text<>'' then
        sql.add('and AP.SCPerson like '''+'%'+trim(SCPerson.Text)+'%'+''' ');
        if C_CodeName.Text<>'' then
        sql.add('and PP.C_CodeName like '''+'%'+trim(C_CodeName.Text)+'%'+''' ');
        sql.Add('group by Year(AP.APDate),AP.SCPerson,PP.Subid,PP.C_CodeName ');
        sql.add('having Sum(PP.PS)<>0 ');
        Open;
      end;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally;
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmDJBRKList_DCGCLMonth.TBRafreshClick(Sender: TObject);
begin
  Toolbar1.SetFocus;
  InitGrid();
end;

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

procedure TfrmDJBRKList_DCGCLMonth.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('机台转数列表1',Tv1,'待检布仓库');
  Close;
end;

procedure TfrmDJBRKList_DCGCLMonth.FormShow(Sender: TObject);
begin
  ReadCxGrid('机台转数列表1',Tv1,'待检布仓库');
  //initgrid();
end;

procedure TfrmDJBRKList_DCGCLMonth.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then exit;
  TcxGridToExcel('机台转数列表',cxGrid2);
end;

procedure TfrmDJBRKList_DCGCLMonth.TBFindClick(Sender: TObject);
begin
  InitGrid();
end;

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

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

procedure TfrmDJBRKList_DCGCLMonth.ConNoChange(Sender: TObject);
begin
  TBFind.Click;
end;

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

procedure TfrmDJBRKList_DCGCLMonth.BTNoteChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmDJBRKList_DCGCLMonth.CheckBox1Click(Sender: TObject);
begin
  initgrid();
end;

end.
