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

--转货商品写库存流水
create procedure pr_item_flow 
(
@adt_operDate datetime,
@atom_itemNO varchar(20),
@atom_qty numeric(16,4),
@atom_sale_price numeric(16,4),
@as_branchNO varchar(6),
@as_operid varchar(6),
@as_flowno varchar(18),
@ls_saleWay varchar(1)
)
WITH ENCRYPTION
as
begin
declare @item_no varchar(20),             --商品编码
		@item_sup_flag   varchar(8),  -- 销售方式，联营，购销。
		@rate        numeric(16,4),   --联营扣率。
		@stock_qty numeric(16,4),
		@cost_price numeric(16,4),
		@atom_source_price numeric(16,4),
		@combine_sta varchar(1),
		@temp_cost_price1 numeric(16,4),
		@temp_cost_price2 numeric(16,4),
		@temp_source_price1 numeric(16,4),
		@item_qty numeric(16,4),			--【单位转换】商品需要按比例转换数量
		@sale_price numeric(16,4),
		@sale_qty numeric(16,4)
    
select top 1 @item_no=B.item_no,@combine_sta=B.combine_sta,@temp_cost_price1=B.price,
		@temp_source_price1=B.sale_price,@item_sup_flag=B.item_sup_flag,@rate=isnull(B.sup_rate,0) ,
		@item_qty = a.item_qty
from t_bd_item_combsplit A,t_bd_item_info B 
where A.item_no=@atom_itemNO and A.comb_item_no=B.item_no 
and (B.combine_sta='6' or B.combine_sta = '7')
    
SELECT @cost_price = avg_cost,@stock_qty =stock_qty 
FROM t_im_branch_stock 
WHERE (branch_no = @as_branchNO) AND (item_no = @item_no)  
    
select @temp_cost_price2=price,@atom_source_price=sale_price 
from t_pc_branch_price 
where branch_no=@as_branchNO and item_no=@item_no
if @combine_sta = '7' 
begin
	if @item_qty = 0 return --未设置成分数量
	--换算成主商品的数量
	select @sale_qty = @atom_qty / @item_qty
	--折合成主商品的价格。
	set @sale_price= (@atom_qty * @atom_sale_price) / @sale_qty
end
if(@cost_price is null)--成本价
begin
	if (@temp_cost_price2 is null)
		set @cost_price=@temp_cost_price2
	else
		set @cost_price=@temp_cost_price1
end
if(@item_sup_flag = 'C') set @cost_price = @atom_sale_price * (1 - @rate)  -- 联营商品，成本价 = 售价 * (1-联营扣率);联营供应商
if(@cost_price is null) set @cost_price=0
if(@stock_qty is null) set @stock_qty=0 --库存
if(@atom_source_price is null) --原价
begin
	if(@temp_cost_price1 is null)
		set @atom_source_price=0
	else
		set @atom_source_price=@temp_source_price1
end
    
--该存储过程仅对零售商品有效
if @combine_sta = '7' 
	insert into t_im_flow(oper_date,item_no,real_qty,price,sheet_amt,
		cost_price,sale_price,branch_no,trans_no,db_no,
		oper_id,voucher_no,memo,pre_qnty,sale_way,
		combine_sta,sheet_item) 
	values (@adt_operDate,@item_no,@sale_qty,@sale_price,@sale_qty*@sale_price,
		@cost_price,@atom_source_price,@as_branchNO,'PT','-',
		@as_operid,@as_flowno,'前台销售：单位转换商品',@stock_qty - @sale_qty,@ls_saleWay,
		@combine_sta,@atom_itemNO)
else
	insert into t_im_flow(oper_date,item_no,real_qty,price,sheet_amt,
		cost_price,sale_price,branch_no,trans_no,db_no,
		oper_id,voucher_no,memo,pre_qnty,sale_way,
		combine_sta,sheet_item) 
	values (@adt_operDate,@item_no,@atom_qty,@atom_sale_price,@atom_qty*@atom_sale_price,
		@cost_price,@atom_source_price,@as_branchNO,'PT','-',
		@as_operid,@as_flowno,'前台销售：自动转货商品',@stock_qty -@atom_qty,@ls_saleWay,
		@combine_sta,@atom_itemNO)
end
go
