unit U_PBOuGLtListHZ;

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, RM_Common, RM_Class, RM_e_Xls, RM_Dataset, RM_System,
  RM_GridReport, cxCheckBox, Menus, MovePanel, BtnEdit, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator;

type
  TfrmPBOuGLtListHZ = 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;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    CDS_Print: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    Label10: TLabel;
    BatchNo: TEdit;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    MovePanel2: TMovePanel;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v2Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Label3: TLabel;
    PrtCodeName: TEdit;
    CDS_DH: TClientDataSet;
    ADOQueryDH: TADOQuery;
    ADOQueryPrint: TADOQuery;
    v1Column14: TcxGridDBColumn;
    Label14: TLabel;
    CustomerNoName: TEdit;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    Label4: TLabel;
    CheJian: TEdit;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    Label5: TLabel;
    PrtCode: TEdit;
    Label6: TLabel;
    CONNO: TEdit;
    v1Column4: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    Label7: TLabel;
    CRType: TComboBox;
    v1Column11: TcxGridDBColumn;
    HZMoney: TLabel;
    PJPrice: TLabel;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Label8: TLabel;
    SHDanWei: 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 MPRTCodeNameChange(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure PrtCodeNameChange(Sender: TObject);
    procedure CheJianChange(Sender: TObject);
    procedure CRTypeChange(Sender: TObject);
  private
    procedure InitGrid();
    procedure GetPrice();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmPBOuGLtListHZ: TfrmPBOuGLtListHZ;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmPBOuGLtListHZ.FormDestroy(Sender: TObject);
begin
  frmPBOuGLtListHZ := nil;
end;

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

procedure TfrmPBOuGLtListHZ.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;

  EndDate.DateTime := SGetServerDateTime(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime - 30;
end;
procedure TfrmPBOuGLtListHZ.GetPrice();
var
  FMoney,FPrice,FQty:string;
begin
  FMoney:=Tv1.DataController.Summary.FooterSummaryTexts[2];
  FQty:=Tv1.DataController.Summary.FooterSummaryTexts[0];
  if Trim(FMoney)='' then FMoney:='0';
  if Trim(FQty)='' then FQty:='0';
  if StrToFloat(FQty)=0 then
  begin
    HZMoney.Caption:='总金额：';
    PJPrice.Caption:='平均单价：';
    Exit;
  end;
  if StrToFloat(FMoney)=0 then
  begin
    HZMoney.Caption:='总金额：';
    PJPrice.Caption:='平均单价：';
    Exit;
  end;
  FPrice:=FloatToStr(SSWR(StrToFloat(FMoney)*1.00/StrToFloat(FQty)));
  HZMoney.Caption:='总金额：'+FMoney;
  PJPrice.Caption:='平均单价：'+FPrice;
end;
procedure TfrmPBOuGLtListHZ.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;

      SQL.Add('select AAA.*  ');
      SQL.Add(',xprice=isnull(JGTiaoJia,0)+isnull(PBJGPrice,0) ');
      SQL.Add(',xMoney=KGQty*isnull(isnull(JGTiaoJia,0)+isnull(PBJGPrice,0),0) ');
      SQL.Add(' from (');
      SQL.Add('select A.CKOrdNo ');
      SQL.Add(',C.PrtCode,C.PrtCodeName,C.prtmf,C.prtkz,C.CustomerNoName,C.BatchNo ,C.CRType,C.SHDanWei');
      SQL.Add(',E.C_Color,F.CONNO,E.JGTiaoJia,G.PBJGPrice,A.CheJian');
      SQL.Add('  ,convert(char(10),CRTime,120) CRTime,SUM(A.KGQty) KGQty,COUNT(MJID) PS  ');

      sql.add('  from CK_PBCP_CR A inner join  FHSQ_Main C  on A.CKOrdNo=C.FSId  ');
      Sql.add('  inner join Cloth_Sub E on A.MainId=E.MainId ');
      Sql.add('  inner join Cloth_Main F on A.MainId=F.MainId ');
      sql.Add('  left join CP_Info G on E.C_CodeName=G.SPName');
      Sql.add(' where A.CRFlag=''出库'' ');
      sql.add(' and A.CRTime>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + ''' ');
      sql.Add(' and A.CRTime<''' + Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)) + '''');
      SQL.Add(' group by A.CKOrdNo ,convert(char(10),CRTime,120) ,CustomerNoName  , PrtCode,PrtCodeName,C.CRType,C.SHDanWei,prtmf,prtkz,E.C_Color,F.CONNO,C.BatchNo');
      SQL.Add('          ,E.JGTiaoJia,G.PBJGPrice,A.CheJian) AAA');
      SQL.Add(' order by AAA.CKOrdNo ,convert(char(10),CRTime,120) ,CustomerNoName  , PrtCode,PrtCodeName,prtmf,prtkz,AAA.C_Color,AAA.CONNO,AAA.BatchNo');

      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmPBOuGLtListHZ.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;
  InitGrid();
  GetPrice();
end;

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

procedure TfrmPBOuGLtListHZ.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(Self.Caption + '1', Tv1, '坯布仓库');
  Close;
end;

procedure TfrmPBOuGLtListHZ.FormShow(Sender: TObject);
begin
  ReadCxGrid(Self.Caption, Tv1, '坯布仓库');
  ReadCxGrid(Self.Caption + '1', Tv1, '坯布仓库');
end;

procedure TfrmPBOuGLtListHZ.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  SelExportData(Tv1, ADOQueryMain, '');
end;

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

procedure TfrmPBOuGLtListHZ.MPRTCodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

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

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

procedure TfrmPBOuGLtListHZ.PrtCodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmPBOuGLtListHZ.CheJianChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmPBOuGLtListHZ.CRTypeChange(Sender: TObject);
begin
  TBFind.Click;
end;

end.

