unit U_CKProductJYHZKCList;

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, MovePanel, cxButtonEdit, cxCalendar, RM_System, RM_Common,
  RM_Class, RM_GridReport, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxBarBuiltInMenu, cxPC;

type
  TfrmCKProductJYHZKCList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label3: TLabel;
    PRTCodeName: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    Tv1: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    v2Column5: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    Label5: TLabel;
    orderNo: TEdit;
    v1Column6: TcxGridDBColumn;
    Label8: TLabel;
    MPRTKZ: TEdit;
    Label9: TLabel;
    MPRTMF: TEdit;
    MovePanel2: TMovePanel;
    Label11: TLabel;
    Label12: TLabel;
    YWY: TEdit;
    OrdPerson1: TEdit;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    Panel4: TPanel;
    Label13: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    RichEdit1: TRichEdit;
    v1Column9: TcxGridDBColumn;
    RMGridReport1: TRMGridReport;
    ToolButton1: TToolButton;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    Tv1Column1: 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 PRTCodeNameChange(Sender: TObject);
    procedure PRTColorChange(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure TBPrintClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure orderNoKeyPress(Sender: TObject; var Key: Char);
  private
    FLeft, FTop: Integer;
    procedure InitGrid();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCKProductJYHZKCList: TfrmCKProductJYHZKCList;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_JYOrderCDOne;

{$R *.dfm}

procedure TfrmCKProductJYHZKCList.FormDestroy(Sender: TObject);
begin
  frmCKProductJYHZKCList := nil;
end;

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

procedure TfrmCKProductJYHZKCList.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  BegDate.DateTime := SGetServerDateTime(ADOQueryTemp);
  EndDate.DateTime := SGetServerDateTime(ADOQueryTemp)
end;

procedure TfrmCKProductJYHZKCList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      Filtered := False;
      sql.Add('select * from (');
      sql.Add('select A.MainId,A.OrderNo,A.ConNO,');
      sql.Add('Qty=(select SUM(X.MJLen) from WFB_MJJY X where A.Mainid=X.Mainid ),');
      sql.Add('KGQty=(select SUM(X.MJMaoZ) from WFB_MJJY X where A.Mainid=X.Mainid ),');
      sql.Add('MJQty4=(select SUM(X.MJQty4) from WFB_MJJY X where A.Mainid=X.Mainid ),');
      sql.Add('JQty=(select count(*) from WFB_MJJY X where A.Mainid=X.Mainid ),');

      sql.Add('DDRoll=(select count(*) from WFB_MJJY X where A.Mainid=X.Mainid ),');
      sql.Add('CKROLL=(select count(*) from WFB_MJJY X where A.Mainid=X.Mainid and X.CKFlag=''已出库'') ,');
      sql.Add('WCKROLL=(select count(*) from WFB_MJJY B where A.Mainid=B.Mainid and B.CKFlag=''未出库''),');
      sql.Add('DDQty=(select sum(MJLen) from WFB_MJJY X where A.Mainid=X.Mainid ),');
      sql.Add('CKQty=(select isnull(sum(MJLen),0) from WFB_MJJY X where A.Mainid=X.Mainid and X.CKFlag=''已出库'') ,');
      sql.Add('WCKQty=(select sum(MJLen) from WFB_MJJY B where A.Mainid=B.Mainid and B.CKFlag=''未出库''),');
      sql.Add('DDMZQty=(select sum(MJMaoz) from WFB_MJJY X where A.Mainid=X.Mainid ),');
      sql.Add('CKMZQty=(select sum(MJMaoz) from WFB_MJJY X where A.Mainid=X.Mainid and X.CKFlag=''已出库'') ,');
      sql.Add('WCKMZQty=(select sum(MJMaoz) from WFB_MJJY B where A.Mainid=B.Mainid and B.CKFlag=''未出库''),');

      sql.Add('khconNO=(select top 1 khconNo from JYOrderCon_Main X where X.conNO=A.conNO)');
      sql.Add('from  JYOrder_Main A');
      Sql.add('where A.OrdDate>=''' + formatdateTime('yyyy-MM-dd', begdate.Date) + ''' ');
      Sql.add('and A.OrdDate<''' + formatdateTime('yyyy-MM-dd', enddate.Date + 1) + ''' ');
      sql.Add(')AA where 1=1 ');

//      sql.Add(' and CKROLL<>0 and WCKROLL>0');

      Open;
//      ShowMessage(SQL.Text);
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCKProductJYHZKCList.TBRafreshClick(Sender: TObject);
begin
  //BegDate.SetFocus;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  InitGrid();
  MovePanel2.Visible := False;
end;

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

procedure TfrmCKProductJYHZKCList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption + tv1.Name, Tv1, '成品仓库');
  Close;
end;

procedure TfrmCKProductJYHZKCList.FormShow(Sender: TObject);
begin

  ReadCxGrid(self.Caption + tv1.Name, Tv1, '成品仓库');

  InitGrid();
end;

procedure TfrmCKProductJYHZKCList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel('库存汇总列表', cxGrid2);
end;

procedure TfrmCKProductJYHZKCList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  end;
end;

procedure TfrmCKProductJYHZKCList.PRTCodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKProductJYHZKCList.PRTColorChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKProductJYHZKCList.Image2Click(Sender: TObject);
begin
  Panel4.Visible := False;
end;

procedure TfrmCKProductJYHZKCList.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  Panel4.Left := FLeft;
  Panel4.Top := FTop + 110;
  Panel4.Visible := True;
  Panel10.Caption := Trim(TV1.Controller.FocusedColumn.Caption);
  RichEdit1.Text := CDS_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString;
  application.ProcessMessages;
end;

procedure TfrmCKProductJYHZKCList.Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FLeft := X;
  FTop := Y;
end;

procedure TfrmCKProductJYHZKCList.TBPrintClick(Sender: TObject);
begin
  RMGridReport1.PreviewButtons := [pbZoom, pbLoad, pbSave, pbPrint, pbFind, pbPageSetup, pbExit, pbExport, pbNavigator];
end;

procedure TfrmCKProductJYHZKCList.ToolButton1Click(Sender: TObject);
begin
  if cds_main.IsEmpty then
    Exit;

  frmJYOrderCDOne := TfrmJYOrderCDOne.Create(Application);
  with frmJYOrderCDOne do
  begin
    orderno.Text := trim(self.CDS_Main.fieldbyname('orderno').asstring);


//    MJstr4.Text := trim(self.CDS_Main.fieldbyname('MJStr4').asstring);
//    PRTColor.Text := trim(self.CDS_Main.fieldbyname('PRTColor').asstring);
    if ShowModal = 1 then
    begin
    //     InitGrid();
    end;
    free;
  end;
end;

procedure TfrmCKProductJYHZKCList.cxTabControl1Change(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmCKProductJYHZKCList.orderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Close;
        sql.Clear;
        Filtered := False;
        sql.Add('select * from (');
        sql.Add('select A.MainId,C.OrderNo,C.ConNO,');
        sql.Add('SUM(A.MJLen) as Qty,SUM(A.MJMaoZ) as KGQty,SUM(A.MJQty4) as MJQty4,');
        sql.Add(' count(A.MainId) as JQty, ');
        sql.Add('DDRoll=(select count(*) from WFB_MJJY X where A.Mainid=X.Mainid ),');
        sql.Add('CKROLL=(select count(*) from WFB_MJJY X where A.Mainid=X.Mainid and X.CKFlag=''已出库'') ,');
        sql.Add('WCKROLL=(select count(*) from WFB_MJJY B where A.Mainid=B.Mainid and B.CKFlag=''未出库''),');
        sql.Add('DDQty=(select sum(MJLen) from WFB_MJJY X where A.Mainid=X.Mainid ),');
        sql.Add('CKQty=(select isnull(sum(MJLen),0) from WFB_MJJY X where A.Mainid=X.Mainid and X.CKFlag=''已出库'') ,');
        sql.Add('WCKQty=(select sum(MJLen) from WFB_MJJY B where A.Mainid=B.Mainid and B.CKFlag=''未出库''),');
        sql.Add('DDMZQty=(select sum(MJMaoz) from WFB_MJJY X where A.Mainid=X.Mainid ),');
        sql.Add('CKMZQty=(select sum(MJMaoz) from WFB_MJJY X where A.Mainid=X.Mainid and X.CKFlag=''已出库'') ,');
        sql.Add('WCKMZQty=(select sum(MJMaoz) from WFB_MJJY B where A.Mainid=B.Mainid and B.CKFlag=''未出库''),');

        sql.Add('khconNO=(select top 1 khconNo from JYOrderCon_Main X where X.conNO=C.conNO)');
        sql.Add('from WFB_MJJY A ');
        sql.Add('inner join JYOrder_Main C on C.MainId=A.MainId ');
        sql.Add(' where C.OrderNo like ''%' + Trim(orderno.Text) + '%'' ');
        Sql.add('group by A.MainId,C.OrderNo,C.ConNO');
        sql.Add(')AA where 1=1 ');

//        sql.Add(' and CKROLL<>0 and WCKROLL>0');

        Open;
//      ShowMessage(SQL.Text);
      end;
      SCreateCDS20(ADOQueryMain, CDS_Main);
      SInitCDSData20(ADOQueryMain, CDS_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

end.

