unit U_DJBCKKCList_DDSH;

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;

type
  TfrmDJBCKKCList_DDSH = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    RMDBHZ: TRMDBDataSet;
    CDS_PRT: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v2MPRTCode: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1PRTMF: TcxGridDBColumn;
    v1MPRTKZ: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1SumCKQty: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Label3: TLabel;
    Label4: TLabel;
    Label6: TLabel;
    PRTCodeName: TEdit;
    PRTCode: TEdit;
    prtspec: TEdit;
    v1PRTColorNo: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    v1PRTHX: TcxGridDBColumn;
    Label9: TLabel;
    ConNo: TEdit;
    Label10: TLabel;
    PRTColor: TEdit;
    Label11: TLabel;
    StyleNo: TEdit;
    Label13: TLabel;
    PRTHX: TEdit;
    ToolButton2: TToolButton;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    v1ConNo: TcxGridDBColumn;
    v1PRTCodeName: TcxGridDBColumn;
    v1PRTColorEng: TcxGridDBColumn;
    v1StyleNo: TcxGridDBColumn;
    Label5: TLabel;
    prtmf: TEdit;
    v1DDTPQty: TcxGridDBColumn;
    v1DJCKQty: TcxGridDBColumn;
    v1sumJYQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1PBSH: TcxGridDBColumn;
    v1JYSH: TcxGridDBColumn;
    v1CPKC: TcxGridDBColumn;
    v1PBCKDate: TcxGridDBColumn;
    v1HCDate: TcxGridDBColumn;
    v1JYDate: TcxGridDBColumn;
    v1CKDate: TcxGridDBColumn;
    v1DDTPPS: TcxGridDBColumn;
    v1DJCKPS: TcxGridDBColumn;
    v1JYPS: TcxGridDBColumn;
    v1CKPS: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure MPRTCodeNameChange(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure SXBatchNOChange(Sender: TObject);
    procedure PRTCodeChange(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton2Click(Sender: TObject);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
  private
    procedure InitGrid();
    { Private declarations }
  public
    canshu1,formid,FConNo:string;
    { Public declarations }
  end;

var
  frmDJBCKKCList_DDSH: TfrmDJBCKKCList_DDSH;

implementation
uses
  U_DataLink,U_RTFun;

{$R *.dfm}

procedure TfrmDJBCKKCList_DDSH.FormDestroy(Sender: TObject);
begin
  frmDJBCKKCList_DDSH:=nil;
end;

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

procedure TfrmDJBCKKCList_DDSH.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    {with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' Select PBCKDate=(select Top 1 F.CRTime from CK_SXPB_CR F inner join CK_SXPB_CR G on G.ORDMainidCK=F.ORDMainidCK and F.CRFlag=''出库'' ');
      sql.add(' and isnull(F.CKName,'''')=''坯布'' and G.CRFlag=''出库'' and isnull(G.CKName,'''')=''待检布'' and isnull(G.CRType,'''')=''检验出库'' ');
      sql.add(' inner join JYOrderCon_Sub H on H.Subid=G.ConCKID inner join JYOrderCon_Main I on I.Mainid=H.Mainid where I.ConNo=AAA.ConNo order by F.CRTime desc)');
      sql.add(',HCDate=(select Top 1 F.CRTime from CK_SXPB_CR F inner join CK_SXPB_CR G on G.ORDMainidCK=F.ORDMainidRK and F.CRFlag=''入库'' ');
      sql.add(' and isnull(F.CKName,'''')=''待检'' and G.CRFlag=''出库'' and isnull(G.CKName,'''')=''待检布'' and isnull(G.CRType,'''')=''检验出库'' ');
      sql.add(' inner join JYOrderCon_Sub H on H.Subid=G.ConCKID inner join JYOrderCon_Main I on I.Mainid=H.Mainid where I.ConNo=AAA.ConNo order by F.CRTime desc)');
      sql.Add(',JYDate=(select Top 1 F.FillTime from WFB_MJJY F inner join JYOrderCon_Sub G on F.ConID=G.Subid ');
      sql.add('inner join JYOrderCon_Main H on H.Mainid=G.Mainid where H.ConNo=AAA.ConNO order by F.FillTime desc)');
      sql.Add(',CKDate=(select Top 1 F.CRTime from CK_BanCP_CR F inner join JYOrderCon_Sub G on F.ConID=G.Subid ');
      sql.add('inner join JYOrderCon_Main H on H.Mainid=G.Mainid where H.ConNo=AAA.ConNO and F.CRFlag=''出库'' and F.CPType=''正品'' order by F.CRTime desc) ');
      SQL.ADD(',AAA.ConNo,AAA.StyleNo,AAA.PrtCode,AAA.PrtCodeName,AAA.PRTspec,AAA.SOrdDefStr4,AAA.PRTColorNo,AAA.PRTHX,AAA.PRTColor,AAA.PRTMF,AAA.PRTKZ,AAA.OrderUnit ');
      sql.add(',Sum(TPPS) TPPS,Sum(DJCKPS) DJCKPS,Sum(sumJYPS) JYPS,Sum(SumCKPS) CKPS');
      sql.add(',Sum(DDTPQty) DDTPQty,Sum(DDTPPS) DDTPPS,Sum(SumCKQty) SumCKQty,Sum(sumJYQty) SumJYQty,Sum(DJCKQty) DJCKQty,Sum(PRTOrderQty) PRTOrderQty,Sum(PRTOrderQty*1.02) PRTOrderQty2');
      sql.add(',Sum(PRTOrderQty*1.05) PRTOrderQty5,Sum(PRTOrderQty*1.1) PRTOrderQty10,Sum(PRTOrderQty*0.98) PRTOrderQty98,Sum(PRTOrderQty*0.95) PRTOrderQty95,Sum(PRTOrderQty*0.9) PRTOrderQty90');
      sql.add(',Sum(SumCKQty*1.02) SumCKQty2,Sum(SumCKQty*1.05) SumCKQty5,Sum(SumCKQty*1.1) SumCKQty10');
      sql.add(',Sum(SumCKQty*0.98) SumCKQty98,Sum(SumCKQty*0.95) SumCKQty95,Sum(SumCKQty*0.9) SumCKQty90');
      sql.add(',PBSH=(case when isnull(Sum(DDTPQty),0)=0 then 0 else (Sum(DDTPQty)-Sum(DJCKQty))/Sum(DDTPQty)*100 end)');
      sql.add(',JYSH=(case when isnull(Sum(DJCKQty),0)=0 then 0 else (Sum(DJCKQty)-Sum(sumJYQty))/Sum(DJCKQty)*100 end)');
      sql.add(',CPKC=(Sum(sumJYQty)-Sum(SumCKQty))');
      sql.add('from (select AA.ConNo,AA.StyleNo,AA.PrtCode,AA.PrtCodeName,AA.PRTspec,AA.SOrdDefStr4,AA.PRTColorNo,AA.PRTHX,AA.PRTColor,AA.PRTMF,AA.PRTKZ,AA.PRTOrderQty,AA.OrderUnit ');
      sql.add(',AA.DJRKQty,AA.TPQty,Sum(AA.Qty) DJCKQty,SUM(AA.CKQty) SumCKQty,SUM(AA.JYQty) sumJYQty ');
      sql.add(',AA.DJRKPS,AA.TPPS,Sum(AA.PiQty) DJCKPS,SUM(AA.CKPS) SumCKPS,SUM(AA.JYPS) sumJYPS ');
      sql.Add(',DDTPQty=(case when isnull(AA.DJRKQty,0)=0 then 0 else cast(Sum(AA.Qty)/AA.DJRKQty*AA.TPQty as decimal(18,2)) end) ');
      sql.Add(',DDTPPS=(case when isnull(AA.DJRKPS,0)=0 then 0 else cast(Sum(AA.PiQty)/AA.DJRKPS*AA.TPPS as decimal(18,0)) end) ');
      sql.Add('from (select C.ConNo,B.StyleNo,B.PrtCode,B.PrtCodeName,B.PRTspec,B.SOrdDefStr4,B.PRTColorNo,B.PRTHX,B.PRTColor,B.PRTMF,B.PRTKZ,B.PRTOrderQty,B.OrderUnit,A.Qty,A.PiQty ');
      sql.Add(',CKQty=(select isnull(Sum(F.MJQty4),0) from CK_BanCP_CR G inner join WFB_MJJY F on F.MJID=G.MJID where F.APID=A.SPID and G.CRFlag=''出库'' and G.CPType=''正品'') ');
      sql.Add(',CKPS=(select isnull(COUNT(F.MJID),0) from CK_BanCP_CR G inner join WFB_MJJY F on F.MJID=G.MJID where F.APID=A.SPID and G.CRFlag=''出库'' and G.CPType=''正品'') ');
      sql.Add(',JYQty=(select isnull(SUM(F.MJQty4),0) from WFB_MJJY F where F.APID=A.SPID)');
      sql.Add(',JYPS=(select isnull(COUNT(F.MJID),0) from WFB_MJJY F where F.APID=A.SPID)');
      sql.Add(',DJRKQty=(select isnull(Sum(F.Qty),0) From CK_SXPB_CR F where F.OrdMainidRK=D.Mainid and F.CRFlag=''入库'' and isnull(F.CKName,'''')=''待检布'') ');
      sql.Add(',DJRKPS=(select isnull(Sum(F.PiQty),0) From CK_SXPB_CR F where F.OrdMainidRK=D.Mainid and F.CRFlag=''入库'' and isnull(F.CKName,'''')=''待检布'') ');
      sql.Add(',TPQty=(select isnull(Sum(F.Qty),0) from CK_SXPB_CR F where F.ORDMainIdCK=D.Mainid and F.CRFlag=''出库'' and isnull(F.CKName,'''')=''坯布'') ');
      sql.Add(',TPPS=(select isnull(Sum(F.PiQty),0) from CK_SXPB_CR F where F.ORDMainIdCK=D.Mainid and F.CRFlag=''出库'' and isnull(F.CKName,'''')=''坯布'') ');
      sql.Add('from CK_SXPB_CR A ');
      sql.Add('inner join JYOrderCon_Sub B on B.SubId=A.ConCKID ');
      sql.Add('inner join JYOrderCon_Main C on C.MainId=B.MainId ');
      sql.Add('left join JYOrder_Sub D on D.SubId=A.ORDSubIdCK ');
      sql.Add('left join JYOrder_Main E on E.MainId=D.MainId ');
      sql.Add('where A.CRFlag=''出库'' and isnull(A.CKName,'''')=''待检布'' ');
      sql.add('and isnull(A.CRType,'''')=''检验出库'' ');
      if trim(canshu1)<>'高权限' then
      sql.add('and (ConPerson1='''+trim(DName)+''' or KeFu='''+trim(DName)+''')');
      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))+''')AA ');
      sql.Add('group BY AA.ConNo,AA.StyleNo,AA.PrtCode,AA.PrtCodeName,AA.PRTspec,AA.SOrdDefStr4,AA.PRTColorNo,AA.PRTHX,AA.PRTColor,AA.PRTMF,AA.PRTKZ,AA.PRTOrderQty,AA.OrderUnit,AA.DJRKQty,AA.TPQty,AA.DJRKPS,AA.TPPS) AAA ');
      sql.Add('group by AAA.ConNo,AAA.StyleNo,AAA.PrtCode,AAA.PrtCodeName,AAA.PRTspec,AAA.SOrdDefStr4,AAA.PRTColorNo,AAA.PRTHX,AAA.PRTColor,AAA.PRTMF,AAA.PRTKZ,AAA.OrderUnit');
      Open;
    end;}
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' Select PBCKDate=(select Top 1 F.CRTime from CK_SXPB_CR F inner join CK_SXPB_CR G on G.ORDMainidCK=F.ORDMainidCK and F.CRFlag=''出库'' ');
      sql.add(' and isnull(F.CKName,'''')=''坯布'' and G.CRFlag=''出库'' and isnull(G.CKName,'''')=''待检布'' and isnull(G.CRType,'''')=''检验出库'' ');
      sql.add(' inner join JYOrderCon_Sub H on H.Subid=G.ConCKID inner join JYOrderCon_Main I on I.Mainid=H.Mainid where I.ConNo=AAA.ConNo order by F.CRTime desc)');
      sql.add(',HCDate=(select Top 1 F.CRTime from CK_SXPB_CR F inner join CK_SXPB_CR G on G.ORDMainidCK=F.ORDMainidRK and F.CRFlag=''入库'' ');
      sql.add(' and isnull(F.CKName,'''')=''待检布'' and G.CRFlag=''出库'' and isnull(G.CKName,'''')=''待检布'' and isnull(G.CRType,'''')=''检验出库'' ');
      sql.add(' inner join JYOrderCon_Sub H on H.Subid=G.ConCKID inner join JYOrderCon_Main I on I.Mainid=H.Mainid where I.ConNo=AAA.ConNo order by F.CRTime desc)');
      sql.Add(',JYDate=(select Top 1 F.FillTime from WFB_MJJY F inner join JYOrderCon_Sub G on F.ConID=G.Subid ');
      sql.add('inner join JYOrderCon_Main H on H.Mainid=G.Mainid where H.ConNo=AAA.ConNO order by F.FillTime desc)');
      sql.Add(',CKDate=(select Top 1 F.CRTime from CK_BanCP_CR F inner join JYOrderCon_Sub G on F.ConID=G.Subid ');
      sql.add('inner join JYOrderCon_Main H on H.Mainid=G.Mainid where H.ConNo=AAA.ConNO and F.CRFlag=''出库'' and F.CPType=''正品'' order by F.CRTime desc) ');
      SQL.ADD(',AAA.ConNo,AAA.StyleNo,AAA.PrtCode,AAA.PrtCodeName,AAA.PRTspec,AAA.SOrdDefStr4,AAA.PRTColorNo,AAA.PRTHX,AAA.PRTColor,AAA.PRTMF,AAA.PRTKZ,AAA.OrderUnit ');
      sql.add(',Sum(TPPS) TPPS,Sum(DJCKPS) DJCKPS,sumJYPS JYPS,SumCKPS CKPS');
      sql.add(',Sum(DDTPQty) DDTPQty,Sum(DDTPPS) DDTPPS,SumCKQty,SumJYQty,Sum(DJCKQty) DJCKQty,Sum(PRTOrderQty) PRTOrderQty,Sum(PRTOrderQty*1.02) PRTOrderQty2');
      sql.add(',Sum(PRTOrderQty*1.05) PRTOrderQty5,Sum(PRTOrderQty*1.1) PRTOrderQty10,Sum(PRTOrderQty*0.98) PRTOrderQty98,Sum(PRTOrderQty*0.95) PRTOrderQty95,Sum(PRTOrderQty*0.9) PRTOrderQty90');
      sql.add(',SumCKQty*1.02 SumCKQty2,SumCKQty*1.05 SumCKQty5,SumCKQty*1.1 SumCKQty10');
      sql.add(',SumCKQty*0.98 SumCKQty98,SumCKQty*0.95 SumCKQty95,SumCKQty*0.9 SumCKQty90');
      sql.add(',PBSH=(case when isnull(Sum(DDTPQty),0)=0 then 0 else (Sum(DDTPQty)-Sum(DJCKQty))/Sum(DDTPQty)*100 end)');
      sql.add(',JYSH=(case when isnull(Sum(DJCKQty),0)=0 then 0 else (Sum(DJCKQty)-sumJYQty)/Sum(DJCKQty)*100 end)');
      sql.add(',CPKC=(sumJYQty-SumCKQty)');
      sql.add('from (select AA.ConNo,AA.StyleNo,AA.PrtCode,AA.PrtCodeName,AA.PRTspec,AA.SOrdDefStr4,AA.PRTColorNo,AA.PRTHX,AA.PRTColor,AA.PRTMF,AA.PRTKZ,AA.PRTOrderQty,AA.OrderUnit ');
      sql.add(',AA.DJRKQty,AA.TPQty,Sum(AA.Qty) DJCKQty,AA.CKQty SumCKQty,AA.JYQty sumJYQty ');
      sql.add(',AA.DJRKPS,AA.TPPS,Sum(AA.PiQty) DJCKPS,AA.CKPS SumCKPS,AA.JYPS sumJYPS ');
      sql.Add(',DDTPQty=(case when isnull(AA.DJRKQty,0)=0 then 0 else cast(Sum(AA.Qty)/AA.DJRKQty*AA.TPQty as decimal(18,2)) end) ');
      sql.Add(',DDTPPS=(case when isnull(AA.DJRKPS,0)=0 then 0 else cast(Sum(AA.PiQty)/AA.DJRKPS*AA.TPPS as decimal(18,0)) end) ');
      sql.Add('from (select C.ConNo,B.StyleNo,B.PrtCode,B.PrtCodeName,B.PRTspec,B.SOrdDefStr4,B.PRTColorNo,B.PRTHX,B.PRTColor,B.PRTMF,B.PRTKZ,B.PRTOrderQty,B.OrderUnit,A.Qty,A.PiQty ');
      sql.Add(',CKQty=(select isnull(Sum(F.MJQty4),0) from CK_BanCP_CR G inner join WFB_MJJY F on G.MJID=F.MJID where G.ConID=B.Subid and G.CRFlag=''出库'' and G.CPType=''正品'') ');
      sql.Add(',CKPS=(select isnull(COUNT(G.MJID),0) from CK_BanCP_CR G where G.ConID=B.Subid and G.CRFlag=''出库'' and G.CPType=''正品'') ');
      sql.Add(',JYQty=(select isnull(SUM(F.MJQty4),0) from WFB_MJJY F where F.ConID=B.Subid)');
      sql.Add(',JYPS=(select isnull(COUNT(F.MJID),0) from WFB_MJJY F where F.ConID=B.Subid)');
      sql.Add(',DJRKQty=(select isnull(Sum(F.Qty),0) From CK_SXPB_CR F where F.OrdMainidRK=D.Mainid and F.CRFlag=''入库'' and isnull(F.CKName,'''')=''待检布'' and CRType=''加工完成'') ');
      sql.Add(',DJRKPS=(select isnull(Sum(F.PiQty),0) From CK_SXPB_CR F where F.OrdMainidRK=D.Mainid and F.CRFlag=''入库'' and isnull(F.CKName,'''')=''待检布'' and CRType=''加工完成'') ');
      sql.Add(',TPQty=(select isnull(Sum(F.Qty),0) from CK_SXPB_CR F left join CK_SXPB_CR G on G.SPID=F.FZSPID where F.ORDMainIdCK=D.Mainid and F.CRFlag=''出库'' and isnull(F.CKName,'''')=''坯布'' and G.CRType<>''加工完成'') ');
      sql.Add(',TPPS=(select isnull(Sum(F.PiQty),0) from CK_SXPB_CR F left join CK_SXPB_CR G on G.SPID=F.FZSPID where F.ORDMainIdCK=D.Mainid and F.CRFlag=''出库'' and isnull(F.CKName,'''')=''坯布'' and G.CRType<>''加工完成'') ');
      sql.Add('from CK_SXPB_CR A ');
      sql.Add('inner join JYOrderCon_Sub B on B.SubId=A.ConCKID ');
      sql.Add('inner join JYOrderCon_Main C on C.MainId=B.MainId ');
      sql.Add('left join JYOrder_Sub D on D.SubId=A.ORDSubIdCK ');
      sql.Add('left join JYOrder_Main E on E.MainId=D.MainId ');
      sql.Add('where A.CRFlag=''出库'' and isnull(A.CKName,'''')=''待检布'' ');
      sql.add('and isnull(A.CRType,'''')=''检验出库'' ');
      if trim(canshu1)<>'高权限' then
      sql.add('and (ConPerson1='''+trim(DName)+''' or KeFu='''+trim(DName)+''')');
      if formid='1' then
      begin
        sql.add(' and A.ConNO='''+Trim(ConNO.Text)+''' and B.PRTCodeName='''+Trim(PRTCodeName.Text)+'''');
        sql.add(' and B.PRTspec='''+Trim(PRTspec.Text)+''' and B.PRTColor='''+Trim(PRTColor.Text)+'''');
      end
      else
      begin
        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))+''' ');
      end;
      sql.add(')AA ');
      sql.Add('group BY AA.ConNo,AA.StyleNo,AA.PrtCode,AA.PrtCodeName,AA.PRTspec,AA.SOrdDefStr4,AA.PRTColorNo,AA.PRTHX,AA.PRTColor,AA.PRTMF,AA.PRTKZ,AA.PRTOrderQty,AA.OrderUnit,AA.DJRKQty,AA.TPQty,AA.DJRKPS,AA.TPPS,AA.CKQty,AA.JYQty,AA.CKPS,AA.JYPS) AAA ');
      sql.Add('group by AAA.ConNo,AAA.StyleNo,AAA.PrtCode,AAA.PrtCodeName,AAA.PRTspec,AAA.SOrdDefStr4,AAA.PRTColorNo,AAA.PRTHX,AAA.PRTColor,AAA.PRTMF,AAA.PRTKZ,AAA.OrderUnit,AAA.SumCKQty,AAA.sumJYQty,AAA.SumCKPS,AAA.sumJYPS');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

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

procedure TfrmDJBCKKCList_DDSH.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('坯布损耗123',Tv1,'待检布仓库');
  Close;
end;
                                                      
procedure TfrmDJBCKKCList_DDSH.FormShow(Sender: TObject);
begin
  ReadCxGrid('坯布损耗123',Tv1,'待检布仓库');
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime:=EndDate.DateTime-15;
  canshu1:=trim(DParameters1);
  ConNO.Text:=trim(FConNO);
  InitGrid();
end;

procedure TfrmDJBCKKCList_DDSH.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 TfrmDJBCKKCList_DDSH.MPRTCodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

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

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

procedure TfrmDJBCKKCList_DDSH.SXBatchNOChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmDJBCKKCList_DDSH.PRTCodeChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmDJBCKKCList_DDSH.Tv1CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  ModalResult:=1;
end;

procedure TfrmDJBCKKCList_DDSH.ToolButton2Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  TcxGridToExcel('待检布库存',cxGrid2);
end;

procedure TfrmDJBCKKCList_DDSH.ConNoKeyPress(Sender: TObject;
  var Key: Char);
begin
  if key=#13 then
  begin
    try
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' Select PBCKDate=(select Top 1 F.CRTime from CK_SXPB_CR F inner join CK_SXPB_CR G on G.ORDMainidCK=F.ORDMainidCK and F.CRFlag=''出库'' ');
      sql.add(' and isnull(F.CKName,'''')=''坯布'' and G.CRFlag=''出库'' and isnull(G.CKName,'''')=''待检布'' and isnull(G.CRType,'''')=''检验出库'' ');
      sql.add(' inner join JYOrderCon_Sub H on H.Subid=G.ConCKID inner join JYOrderCon_Main I on I.Mainid=H.Mainid where I.ConNo=AAA.ConNo order by F.CRTime desc)');
      sql.add(',HCDate=(select Top 1 F.CRTime from CK_SXPB_CR F inner join CK_SXPB_CR G on G.ORDMainidCK=F.ORDMainidRK and F.CRFlag=''入库'' ');
      sql.add(' and isnull(F.CKName,'''')=''待检'' and G.CRFlag=''出库'' and isnull(G.CKName,'''')=''待检布'' and isnull(G.CRType,'''')=''检验出库'' ');
      sql.add(' inner join JYOrderCon_Sub H on H.Subid=G.ConCKID inner join JYOrderCon_Main I on I.Mainid=H.Mainid where I.ConNo=AAA.ConNo order by F.CRTime desc)');
      sql.Add(',JYDate=(select Top 1 F.FillTime from WFB_MJJY F inner join JYOrderCon_Sub G on F.ConID=G.Subid ');
      sql.add('inner join JYOrderCon_Main H on H.Mainid=G.Mainid where H.ConNo=AAA.ConNO order by F.FillTime desc)');
      sql.Add(',CKDate=(select Top 1 F.CRTime from CK_BanCP_CR F inner join JYOrderCon_Sub G on F.ConID=G.Subid ');
      sql.add('inner join JYOrderCon_Main H on H.Mainid=G.Mainid where H.ConNo=AAA.ConNO and F.CRFlag=''出库'' and F.CPType=''正品'' order by F.CRTime desc) ');
      SQL.ADD(',AAA.ConNo,AAA.StyleNo,AAA.PrtCode,AAA.PrtCodeName,AAA.PRTspec,AAA.SOrdDefStr4,AAA.PRTColorNo,AAA.PRTHX,AAA.PRTColor,AAA.PRTMF,AAA.PRTKZ,AAA.OrderUnit ');
      sql.add(',Sum(TPPS) TPPS,Sum(DJCKPS) DJCKPS,sumJYPS JYPS,SumCKPS CKPS');
      sql.add(',Sum(DDTPQty) DDTPQty,Sum(DDTPPS) DDTPPS,SumCKQty,SumJYQty,Sum(DJCKQty) DJCKQty,Sum(PRTOrderQty) PRTOrderQty,Sum(PRTOrderQty*1.02) PRTOrderQty2');
      sql.add(',Sum(PRTOrderQty*1.05) PRTOrderQty5,Sum(PRTOrderQty*1.1) PRTOrderQty10,Sum(PRTOrderQty*0.98) PRTOrderQty98,Sum(PRTOrderQty*0.95) PRTOrderQty95,Sum(PRTOrderQty*0.9) PRTOrderQty90');
      sql.add(',SumCKQty*1.02 SumCKQty2,SumCKQty*1.05 SumCKQty5,SumCKQty*1.1 SumCKQty10');
      sql.add(',SumCKQty*0.98 SumCKQty98,SumCKQty*0.95 SumCKQty95,SumCKQty*0.9 SumCKQty90');
      sql.add(',PBSH=(case when isnull(Sum(DDTPQty),0)=0 then 0 else (Sum(DDTPQty)-Sum(DJCKQty))/Sum(DDTPQty)*100 end)');
      sql.add(',JYSH=(case when isnull(Sum(DJCKQty),0)=0 then 0 else (Sum(DJCKQty)-sumJYQty)/Sum(DJCKQty)*100 end)');
      sql.add(',CPKC=(sumJYQty-SumCKQty)');
      sql.add('from (select AA.ConNo,AA.Subid,AA.StyleNo,AA.PrtCode,AA.PrtCodeName,AA.PRTspec,AA.SOrdDefStr4,AA.PRTColorNo,AA.PRTHX,AA.PRTColor,AA.PRTMF,AA.PRTKZ,AA.PRTOrderQty,AA.OrderUnit ');
      sql.add(',AA.DJRKQty,AA.TPQty,Sum(AA.Qty) DJCKQty,AA.CKQty SumCKQty,AA.JYQty sumJYQty ');
      sql.add(',AA.DJRKPS,AA.TPPS,Sum(AA.PiQty) DJCKPS,AA.CKPS SumCKPS,AA.JYPS sumJYPS ');
      sql.Add(',DDTPQty=(case when isnull(AA.DJRKQty,0)=0 then 0 else cast(Sum(AA.Qty)/AA.DJRKQty*AA.TPQty as decimal(18,2)) end) ');
      sql.Add(',DDTPPS=(case when isnull(AA.DJRKPS,0)=0 then 0 else cast(Sum(AA.PiQty)/AA.DJRKPS*AA.TPPS as decimal(18,0)) end) ');
      sql.Add('from (select C.ConNo,B.Subid,B.StyleNo,B.PrtCode,B.PrtCodeName,B.PRTspec,B.SOrdDefStr4,B.PRTColorNo,B.PRTHX,B.PRTColor,B.PRTMF,B.PRTKZ,B.PRTOrderQty,B.OrderUnit,A.Qty,A.PiQty ');
      sql.Add(',CKQty=(select isnull(Sum(F.MJQty4),0) from CK_BanCP_CR G inner join WFB_MJJY F on G.MJID=F.MJID where G.ConID=B.Subid and G.CRFlag=''出库'' and G.CPType=''正品'') ');
      sql.Add(',CKPS=(select isnull(COUNT(G.MJID),0) from CK_BanCP_CR G where G.ConID=B.Subid and G.CRFlag=''出库'' and G.CPType=''正品'') ');
      sql.Add(',JYQty=(select isnull(SUM(F.MJQty4),0) from WFB_MJJY F where F.ConID=B.Subid)');
      sql.Add(',JYPS=(select isnull(COUNT(F.MJID),0) from WFB_MJJY F where F.ConID=B.Subid)');
      sql.Add(',DJRKQty=(select isnull(Sum(F.Qty),0) From CK_SXPB_CR F where F.OrdMainidRK=D.Mainid and F.CRFlag=''入库'' and isnull(F.CKName,'''')=''待检布'') ');
      sql.Add(',DJRKPS=(select isnull(Sum(F.PiQty),0) From CK_SXPB_CR F where F.OrdMainidRK=D.Mainid and F.CRFlag=''入库'' and isnull(F.CKName,'''')=''待检布'') ');
      sql.Add(',TPQty=(select isnull(Sum(F.Qty),0) from CK_SXPB_CR F left join CK_SXPB_CR G on G.SPID=F.FZSPID where F.ORDMainIdCK=D.Mainid and F.CRFlag=''出库'' and isnull(F.CKName,'''')=''坯布'' and G.CRType<>''加工完成'') ');
      sql.Add(',TPPS=(select isnull(Sum(F.PiQty),0) from CK_SXPB_CR F left join CK_SXPB_CR G on G.SPID=F.FZSPID where F.ORDMainIdCK=D.Mainid and F.CRFlag=''出库'' and isnull(F.CKName,'''')=''坯布'' and G.CRType<>''加工完成'') ');
      sql.Add('from CK_SXPB_CR A ');
      sql.Add('inner join JYOrderCon_Sub B on B.SubId=A.ConCKID ');
      sql.Add('inner join JYOrderCon_Main C on C.MainId=B.MainId ');
      sql.Add('left join JYOrder_Sub D on D.SubId=A.ORDSubIdCK ');
      sql.Add('left join JYOrder_Main E on E.MainId=D.MainId ');
      sql.Add('where A.CRFlag=''出库'' and isnull(A.CKName,'''')=''待检布'' ');
      sql.add('and isnull(A.CRType,'''')=''检验出库'' ');
      if trim(canshu1)<>'高权限' then
      sql.add('and (ConPerson1='''+trim(DName)+''' or KeFu='''+trim(DName)+''')');
      sql.add('and C.ConNO like '''+'%'+trim(ConNo.Text)+'%'+''')AA ');
      sql.Add('group BY AA.ConNo,AA.Subid,AA.StyleNo,AA.PrtCode,AA.PrtCodeName,AA.PRTspec,AA.SOrdDefStr4,AA.PRTColorNo,AA.PRTHX,AA.PRTColor,AA.PRTMF,AA.PRTKZ,AA.PRTOrderQty,AA.OrderUnit,AA.DJRKQty,AA.TPQty,AA.DJRKPS,AA.TPPS,AA.CKQty,AA.JYQty,AA.CKPS,AA.JYPS) AAA ');
      sql.Add('group by AAA.ConNo,AAA.StyleNo,AAA.PrtCode,AAA.PrtCodeName,AAA.PRTspec,AAA.SOrdDefStr4,AAA.PRTColorNo,AAA.PRTHX,AAA.PRTColor,AAA.PRTMF,AAA.PRTKZ,AAA.OrderUnit,AAA.SumCKQty,AAA.sumJYQty,AAA.SumCKPS,AAA.sumJYPS');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
  end;
end;

end.
