unit U_Bpkclist;

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, cxCheckBox,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, 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
  TfrmBpkclist = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBExport: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1P_CodeName: TcxGridDBColumn;
    v1P_SPEC: TcxGridDBColumn;
    v1P_MF: TcxGridDBColumn;
    v1P_KZ: TcxGridDBColumn;
    v1RollNum: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1CRType: TcxGridDBColumn;
    v1CRTime: TcxGridDBColumn;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    P_CodeName: TEdit;
    factoryName: TEdit;
    CRType: TComboBox;
    Label5: TLabel;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    TOk: TToolButton;
    v1Ssel: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    v1MJID: TcxGridDBColumn;
    v1MJXH: TcxGridDBColumn;
    v1CPType: TcxGridDBColumn;
    ckName: TComboBox;
    Label6: TLabel;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    cust: TEdit;
    Label7: TLabel;
    gangNO: TEdit;
    Label8: TLabel;
    v1Column4: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure P_CodeNameChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TOkClick(Sender: TObject);
  private
    procedure InitGrid();
    procedure SetComboBox();
    { Private declarations }
  public
    fType: integer;
    fCKName: string;
    
    { Public declarations }
  end;

var
  frmBpkclist: TfrmBpkclist;

implementation

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

procedure TfrmBpkclist.SetComboBox();
begin
  ckName.Items.Clear;
  with adoQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where Type=''BPCK'' ');
    if trim(fCKName) <> '' then
      sql.Add('and zdyName=' + quotedstr(trim(fCKName)));
    open;
    while not eof do
    begin
      ckName.Items.Add(trim(fieldbyname('zdyName').AsString));
      next;
    end;
  end;
  if ckName.Items.Count > 0 then
    ckName.ItemIndex := 0;

  CRType.Items.Clear;
  CRType.Items.Add('');
  with adoQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where Type=''BPRKTYPE''');
    if trim(fCKName) <> '' then
      sql.Add('and note=' + quotedstr(trim(fCKName)));
    open;
    while not eof do
    begin
      CRType.Items.Add(trim(fieldbyname('zdyName').AsString));
      next;
    end;
  end;
  if CRType.Items.Count > 0 then
    CRType.ItemIndex := 0;
end;

procedure TfrmBpkclist.InitGrid();
begin
  try
    with adoqueryTmp do
    begin
      close;
      sql.Clear;
//      sql.Add('select A.*,B.cust,B.factoryName from BP_KC A');
//      sql.Add('inner join BP_inout B on B.KCID=A.KCID and B.CRFlag=''Èë¿â'' ');
//      sql.Add('where 1=1');        sum(([dbo].[Get_ShuZi](defstr3))   sum((cast(ISNULL(defstr2,0) as float))

      SQL.Add('SELECT count(A.mxid) as RollNum,sum(A.kcqty2) as qty,B.cust,B.factoryName,B.CRTime,B.CRType,B.P_CodeName,B.P_SPEC,B.P_Color,B.gangNo,B.P_KZ,B.P_MF,B.QtyUnit,B.ckName ');
      SQL.Add(',PreBPqty=(select sum(([dbo].[Get_ShuZi](defstr2))*(isnull(prtorderqty,0))) from JYOrder_Sub X inner join KH_Zdy_CPCB M on M.ZdyCode=X.PRTCode left join JYOrder_Main N on N.mainid=X.mainid ');
      SQL.Add(' where M.defstr1=B.P_CodeName and  M.CBColor=X.prtcolor and left(SOrddefstr5,1)=''A''   and N.Status=''0'')');  // M.CBColor in (B.P_Color)
      SQL.Add('+(select  sum(([dbo].[Get_ShuZi](defstr3))*(isnull(prtorderqty,0)))from JYOrder_Sub X  inner join KH_Zdy_CPCB M on M.ZdyCode=X.PRTCode left join JYOrder_Main N on N.mainid=X.mainid  ');
      SQL.Add(' where M.defstr1=B.P_CodeName and  M.CBColor=X.prtcolor and left(SOrddefstr5,1)=''C''  and N.Status=''0'' )');   //and  M.CBColor=B.P_Color
      sql.Add('FROM CK_SXPB_CRMX A inner join BP_InOut  B  on  A.crnos=B.crnos ');
      sql.Add(' where A.crzt=''Èë¿â'' ');
      SQL.Add('and   exists(select crnoS from BP_KC   d where  D.crnos=A.crnos)');
      if begdate.Checked then
        sql.Add('and B.CRTime>=''' + formatdateTime('yyyy-MM-dd', begdate.Date) + ''' ');
      if enddate.Checked then
        sql.Add('and B.CRTime<''' + formatdateTime('yyyy-MM-dd', enddate.Date + 1) + ''' ');
//      if not CheckBox1.Checked then
//        sql.Add('and (A.RollNum<>0 or A.qty<>0 ) ');
      if trim(FckName) <> '' then
        sql.Add('and B.ckName=' + quotedstr(trim(FckName)));
      sql.Add('	GROUP BY A.crnoS,B.cust,B.factoryName,B.CRTime,B.CRType,B.P_CodeName,B.P_SPEC,B.P_Color,B.gangNo,B.P_KZ,B.P_MF,B.QtyUnit,B.ckName ');
//      ShowMessage(SQL.text);
      open;
    end;
    SCreateCDS20(adoqueryTmp, CDS_Main);
    SInitCDSData20(adoqueryTmp, CDS_Main);
  finally
  end;
end;

procedure TfrmBpkclist.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  writeCxGrid(self.Caption, Tv1, '²¼Æ¥²Ö¿â');
  if fType = 10 then
    Action := cahide
  else
    Action := cafree;
end;

procedure TfrmBpkclist.FormDestroy(Sender: TObject);
begin
  frmBpkclist := nil;
end;

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

procedure TfrmBpkclist.FormShow(Sender: TObject);
begin
  if fType = 10 then
  begin
    TOk.Visible := true;
    v1Ssel.Visible := true;
  end;
  readCxGrid(self.Caption, Tv1, '²¼Æ¥²Ö¿â');
  SetComboBox();
  InitGrid();
end;

procedure TfrmBpkclist.FormCreate(Sender: TObject);
begin
  BegDate.Date := DServerDate - 7;
  EndDate.Date := DServerDate;
  cxGrid1.Align := alclient;

end;

procedure TfrmBpkclist.TBCloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmBpkclist.TBRafreshClick(Sender: TObject);
begin

  initGrid();
end;

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

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

procedure TfrmBpkclist.TOkClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  ModalResult := 1;
end;

end.

