unit U_DJBRKList_JTCLMonth;

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, Series, TeEngine, TeeProcs, Chart,
  DbChart, cxSplitter;

type
  TfrmDJBRKList_JTCLMonth = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label1: TLabel;
    CDS_Main: TClientDataSet;
    Label3: TLabel;
    CarNo: TEdit;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1APMonth: TcxGridDBColumn;
    v1CarNo: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1MJMaoZ: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    v1PS: TcxGridDBColumn;
    APMonth: TComboBox;
    v1APYear: TcxGridDBColumn;
    Label2: TLabel;
    APYear: TEdit;
    CheckBox1: TCheckBox;
    Label4: TLabel;
    EndMonth: TComboBox;
    EndYear: TEdit;
    Label5: TLabel;
    C_CodeName: TEdit;
    v1C_CodeName: TcxGridDBColumn;
    CheckBox2: TCheckBox;
    cxSplitter1: TcxSplitter;
    DBChart1: TDBChart;
    Series1: TBarSeries;
    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);
    procedure CheckBox2Click(Sender: TObject);
  private
    canshu1,canshu2:string;
    procedure InitGrid();
    procedure TXCSH();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmDJBRKList_JTCLMonth: TfrmDJBRKList_JTCLMonth;

implementation
uses
  U_DataLink,U_RTFun,U_DJBCKInPut,U_DJBCKInPut_JTZS;

{$R *.dfm}

procedure TfrmDJBRKList_JTCLMonth.TXCSH();
var
  i:integer;
begin
  if CheckBox2.Checked=False then
  begin
    for i:=0 to DBChart1.SeriesCount-1 do
    begin
      DBChart1.Series[i].Active:=False;
    end;
    DBChart1.Series[0].Active:=True;
    DBChart1.Series[0].DataSource:=nil;
    DBChart1.Series[0].DataSource:=ADOQueryMain;
    DBChart1.Series[0].XLabelsSource:='CarNo';
    DBChart1.Series[0].YValues.ValueSource:='MJMaoZ';
  end;
end;

procedure TfrmDJBRKList_JTCLMonth.FormDestroy(Sender: TObject);
begin
  frmDJBRKList_JTCLMonth:=nil;
end;

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

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

procedure TfrmDJBRKList_JTCLMonth.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    if CheckBox2.Checked=False then
    begin
      v1C_CodeName.Visible:=False;
    end
    else
    begin
      v1C_CodeName.Visible:=true;
    end;
    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,');
        sql.Add('Month(AP.APDate) APMonth,AP.CarNo');
        if CheckBox2.Checked=True then
        sql.Add(',PP.C_CodeName ');
        sql.add(',ZQty=(select Sum(Z.Qty) from Cloth_JiTaiZShu Z where Z.CarNo=AP.CarNo ');
        if CheckBox2.Checked=True then
        sql.Add('and Z.Subid=PP.Subid ');
        sql.Add('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 CarNo.Text<>'' then
        sql.add('and AP.CarNo='''+trim(CarNo.Text)+''' ');
        if C_CodeName.Text<>'' then
        sql.add('and PP.C_CodeName like '''+'%'+trim(C_CodeName.Text)+'%'+''' ');
        sql.Add('group by AP.CarNo');
        if CheckBox2.Checked=True then
        sql.Add(',PP.C_CodeName,PP.Subid');
        sql.Add(',Year(AP.APDate),Month(AP.APDate) ');
        sql.add('having Sum(PP.PS)<>0 ');
        sql.add('order by Year(AP.APDate),Month(AP.APDate),cast(AP.CarNo as int)');
        Open;
      end;
    end
    else
    begin
      v1APMonth.Visible:=False;
      with ADOQueryMain do
      begin
        Close;
        sql.Clear;
        sql.Add('select AP.CarNo,Year(AP.APDate) APYear ');
        if CheckBox2.Checked=True then
        sql.Add(',PP.C_CodeName ');
        sql.add(',ZQty=(select Sum(Z.Qty) from Cloth_JiTaiZShu Z where Z.CarNo=AP.CarNo ');
        if CheckBox2.Checked=True then
        sql.Add('and Z.Subid=PP.Subid ');
        sql.Add('and year(Z.ZSDate)=year(AP.APDate)) ');
        sql.Add(',SUM(PP.MJMaoZ) MJMaoZ,SUM(PP.PS) ps');
        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 CarNo.Text<>'' then
        sql.add('and AP.CarNo='''+trim(CarNo.Text)+''' ');
        if C_CodeName.Text<>'' then
        sql.add('and PP.C_CodeName like '''+'%'+trim(C_CodeName.Text)+'%'+''' ');
        sql.Add('group by AP.CarNo,Year(AP.APDate) ');
        if CheckBox2.Checked=True then
        sql.Add(',PP.C_CodeName,PP.Subid ');
        sql.add('having Sum(PP.PS)<>0 ');
        sql.add('order by Year(AP.APDate),cast(AP.CarNo as int)');
        Open;
      end;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally;
    ADOQueryMain.EnableControls;
  end;
end;

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

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

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

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

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

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

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

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

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

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

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

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

procedure TfrmDJBRKList_JTCLMonth.CheckBox2Click(Sender: TObject);
begin
  initgrid();
  TXCSH();
end;

end.
