if exists(select 1 from sysobjects where name='pr_wxsf_vip_sale_flow')
  drop procedure pr_wxsf_vip_sale_flow
go


create proc pr_wxsf_vip_sale_flow 
(  
	@card_id		varchar(20),	--会员卡id
	@begin_date		datetime,		--起始时间
	@end_date		datetime		--结束时间
)
as
begin
	--功能:烘焙10线上会员查看线下消费记录功能
	set nocount on 
 
	select		s.flow_no,		 --线下单号 
			    (select top 1 branch_name from t_bd_branch_info b where b.branch_no = s.branch_no) as branch_name, --门店名称
				pt.total_num,	 --合计件数
				pt.real_amt,	 --实付金额  
				pt.discount_amt, --优惠金额
				(s.source_price) as sale_price,	 --销售原价
				sum(s.sale_qnty) as sale_qty,	 --商品数量 	
				b.item_name,	 --商品名称
				max(s.oper_date) as sale_date,  --销售时间
				s.sell_way		--销售方式A-销售B-退货 C-赠送
	 from t_rm_saleflow s with(nolock) inner join t_bd_item_info b with(nolock) on s.item_no = b.item_no
		  inner join (select flow_no,count(item_no) as total_num,SUM(sale_money) as real_amt,SUM(pref_amt) as discount_amt  
					from t_rm_saleflow with(nolock) where oper_date between @begin_date and @end_date
					and card_id = @card_id and isnull(wx_no,'') = '' group by flow_no) pt on s.flow_no = pt.flow_no
	where s.oper_date between @begin_date and @end_date and
		  s.card_id = @card_id and isnull(s.card_id,'')<>'' and isnull(wx_no,'') = ''
	group by s.flow_no,s.branch_no,s.sell_way,b.item_no,b.item_name,s.source_price,pt.total_num,pt.real_amt,pt.discount_amt 
					 
	set nocount off 
end
go