unit U_XiaoShouDataOldList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, DBClient, cxCheckBox, cxCalendar, cxSplitter,
  RM_Dataset, RM_System, RM_Common, RM_Class, RM_GridReport, RM_e_Xls,
  Menus, cxButtonEdit, cxDropDownEdit, cxPC, Buttons;

type
  TfrmXiaoShouDataOldList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Order_Main: TClientDataSet;
    Panel1: TPanel;
    v1Column1: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    Label3: TLabel;
    Label4: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    v1Column17: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    Label2: TLabel;
    MPRTCodeName: TEdit;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    Label5: TLabel;
    PRTColor: TEdit;
    labMYType: TLabel;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    Label6: TLabel;
    PRTHX: TEdit;
    v1Column8: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    v1Column2: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ZJStatusChange(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
  private
    canshu1:string;
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    { Private declarations }
  public
    FKHName:String;
    { Public declarations }
  end;

var
  frmXiaoShouDataOldList: TfrmXiaoShouDataOldList;

implementation
uses
  U_DataLink,U_RTFun;

{$R *.dfm}

procedure TfrmXiaoShouDataOldList.FormDestroy(Sender: TObject);
begin
  frmXiaoShouDataOldList:=nil;
end;

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

procedure TfrmXiaoShouDataOldList.FormCreate(Sender: TObject);
begin
  cxgrid1.Align:=alClient;
  canshu1:=Trim(DParameters1);
end;

procedure TfrmXiaoShouDataOldList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('老系统销售信息',Tv1,'贸易生产管理');
  Close;
end;

procedure TfrmXiaoShouDataOldList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select AA.CRTime,AA.CKOrdNo,AA.QtyUnit,AA.PS,AA.Qty,JM.ConNo,JM.CustomerNoName,JM.MPRTCodeName ');
      sql.Add(',JS.PRTColor,JM.MPRTMF,JS.PRTKZ,JS.SOrdDefStr1,JS.PRTHX');
      sql.Add(',JS.PRTPrice,HZFee=isnull(JS.SOrdqty1,0)+isnull(JS.SOrdqty2,0)+isnull(JS.SOrdqty3,0)+isnull(JS.SOrdqty4,0)');
      sql.Add(',FHMoney=Cast(JS.PRTPrice*AA.Qty as decimal(18,2))+isnull(JS.SOrdqty1,0)+isnull(JS.SOrdqty2,0)+isnull(JS.SOrdqty3,0)+isnull(JS.SOrdqty4,0)');
      sql.Add(',Case when isnull(JS.PriceUnit,'''')=''人民币'' then ''￥''  ');
      sql.Add(' when isnull(PriceUnit,'''')=''RMB'' then ''￥''  ');
      sql.Add(' when isnull(PriceUnit,'''')='''' then ''￥''  ');
      sql.Add(' else ''$'' end as BZType ');
      sql.Add('from (');
      sql.Add(' select MainId,SubId,CRTime,CKOrdNo,QtyUnit,PS=Count(*),Qty=sum(Qty) ');
      sql.Add(' from CK_BanCP_CR A  ');
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' where CRTime>=''2020-01-01'' ');
      end else
      begin
        sql.Add(' where CRTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
        sql.Add(' and CRTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
      end;

      sql.Add(' and isnull(A.CRType,'''')=''销售出库'' ');
      sql.Add(' and isnull(A.CKOrdNo,'''')<>'''' ');
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' and not exists(select * from CK_YCL_CR_Main CM where isnull(CM.CKNO,'''')=isnull(A.CKOrdNo,'''') )');
      end else
      if cxTabControl1.TabIndex=1 then
      begin
        sql.Add(' and  exists(select * from CK_YCL_CR_Main CM where isnull(CM.CKNO,'''')=isnull(A.CKOrdNo,'''') )');
      end;
      sql.Add('Group by MainId,SubId,CRTime,CKOrdNo,QtyUnit)AA');
      sql.Add('inner join JYOrder_Sub JS on AA.SubId=JS.SubId ');
      sql.Add('inner join JYOrder_Main JM on JS.MainId=JM.MainId ');
      sql.Add(' where JM.CustomerNoName like '''+'%'+Trim(FKHName)+'%'+'''');

      sql.Add('Union All ');

      sql.Add('select CRTime,CKOrdNo=InOutNo,QtyUnit,PS=Sum(RollNum),Qty=Sum(Qty),ConNo,CustomerNoName=CustName');
      sql.Add(',MPRTCodeName=P_CodeName,PRTColor=P_Color,MPRTMF=Cast(P_MF as varchar(20)),PRTKZ=Cast(P_KZ as varchar(20)),SOrdDefStr1=P_ColNo,PRTHX=P_HX');
      sql.Add(',PRTPrice=Price,HZFee=0.00,FHMoney=sum(Money),BZType=''￥'' ');
      sql.Add(' from CP_InOut1 A ');
      sql.Add(' where A.CustName like '''+'%'+Trim(FKHName)+'%'+'''');
      SQL.Add(' and CRType=''销售出库'' ');
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' and CRTime>=''2020-01-01'' ');
        sql.Add(' and not exists(select * from CK_YCL_CR_Main CM where isnull(CM.CKNO,'''')=isnull(A.InOutNo,'''') )');
      end else
      if cxTabControl1.TabIndex=1 then
      begin
        sql.Add(' and CRTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
        sql.Add(' and CRTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
        sql.Add(' and  exists(select * from CK_YCL_CR_Main CM where isnull(CM.CKNO,'''')=isnull(A.InOutNo,'''') )');
      end;
      sql.Add('Group by CRTime,InOutNo,QtyUnit,ConNo,CustName,P_CodeName,P_Color,P_MF,P_KZ,P_ColNo,P_HX,Price');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;


procedure TfrmXiaoShouDataOldList.InitForm();
begin
  BegDate.Date:=SGetServerDateMBeg(ADOQueryTemp);
  EndDate.Date:=SGetServerDate(ADOQueryTemp);
  ReadCxGrid('老系统销售信息',Tv1,'贸易生产管理');
  InitGrid();
end;

procedure TfrmXiaoShouDataOldList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmXiaoShouDataOldList.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmXiaoShouDataOldList.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmXiaoShouDataOldList.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmXiaoShouDataOldList.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmXiaoShouDataOldList.CustomerNoNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmXiaoShouDataOldList.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmXiaoShouDataOldList.ZJStatusChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmXiaoShouDataOldList.Tv1DblClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if cxTabControl1.TabIndex<>0 then Exit;
  ModalResult:=1;
end;

end.
