unit U_DJBRKList_PBMonth;

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_PBMonth = 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;
    ConNo: TEdit;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1APMonth: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    v1JYQty: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    v1JYPS: TcxGridDBColumn;
    v1APYear: TcxGridDBColumn;
    Label2: TLabel;
    APYear: TEdit;
    CheckBox1: TCheckBox;
    Label1: TLabel;
    APMonth: TComboBox;
    Label4: TLabel;
    EndMonth: TComboBox;
    EndYear: TEdit;
    Label5: TLabel;
    v1JGPrice: TcxGridDBColumn;
    v1JGMoney: TcxGridDBColumn;
    v1C_CodeName: TcxGridDBColumn;
    Label6: TLabel;
    C_CodeName: TEdit;
    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_PBMonth: TfrmDJBRKList_PBMonth;

implementation
uses
  U_DataLink,U_RTFun,U_DJBCKInPut,U_DJBCKInPut_JTZS;

{$R *.dfm}

procedure TfrmDJBRKList_PBMonth.FormDestroy(Sender: TObject);
begin
  frmDJBRKList_PBMonth:=nil;
end;

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

procedure TfrmDJBRKList_PBMonth.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_PBMonth.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    if checkbox1.Checked=False then
    begin
      v1APMonth.Visible:=true;
      with ADOQueryMain do
      begin
        Close;
        sql.Clear;
        sql.Add('select M.ConNo,M.JGPrice,JGMoney=Sum(AP.MJMaoZ*M.JGPrice),Year(AP.filltime) APYear,Month(AP.filltime) APMonth');
        sql.add(',C_CodeName=(select Top 1 C_CodeName from Cloth_Sub B where B.Subid=AP.Subid)');
        sql.add(',count(AP.MJID) JYPS,Sum(AP.MJMaoZ) JYQty ');
        sql.add('from WFB_MJJY_PB AP ');
        sql.add('inner join Cloth_Main M on AP.Mainid=M.Mainid ');
        sql.add('where 1=1 ');
        sql.add('and Year(AP.filltime)>='''+trim(APYear.Text)+''' and Year(AP.filltime)<='''+trim(EndYear.Text)+''' ');
        sql.Add('and Month(AP.filltime)>='''+trim(APMonth.Text)+''' and Month(AP.filltime)<='''+trim(EndMonth.Text)+''' ');
        if ConNo.Text<>'' then
        sql.add('and ConNo like '''+'%'+trim(ConNo.Text)+'%'+''' ');
        sql.Add('group by M.ConNo,M.JGPrice,Year(AP.filltime),Month(AP.filltime),AP.Subid ');
        sql.add('order by Year(AP.filltime),Month(AP.filltime),M.ConNo');
        Open;
      end;
    end
    else
    begin
      v1APMonth.Visible:=False;
      with ADOQueryMain do
      begin
        Close;
        sql.Clear;
        sql.Add('select M.ConNo,M.JGPrice,JGMoney=Sum(AP.MJMaoZ*M.JGPrice),Year(AP.filltime) APYear');
        sql.add(',C_CodeName=(select Top 1 C_CodeName from Cloth_Sub B where B.Subid=AP.Subid)');
        sql.add(',count(AP.MJID) JYPS,Sum(AP.MJMaoZ) JYQty ');
        sql.add('from WFB_MJJY_PB AP ');
        sql.add('inner join Cloth_Main M on AP.Mainid=M.Mainid ');
        sql.add('where 1=1');
        sql.add('and Year(AP.filltime)>='''+trim(APYear.Text)+''' and Year(AP.filltime)<='''+trim(EndYear.Text)+''' ');
        if ConNo.Text<>'' then
        sql.add('and ConNo like '''+'%'+trim(ConNo.Text)+'%'+''' ');
        sql.Add('group by M.ConNo,M.JGPrice,Year(AP.filltime),AP.Subid');
        sql.add('order by Year(AP.filltime),M.ConNo');
        Open;
      end;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally;
    ADOQueryMain.EnableControls;
  end;
end;

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

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

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

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

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

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

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

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

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

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

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

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

end.
