if exists(select 1 from sysobjects where name='tr_rm_saleflow')
  drop Trigger tr_rm_saleflow
go

create trigger tr_rm_saleflow on t_rm_saleflow 
for insert
as
begin

  declare @ls_branchNO char(6),       -- 仓库编码
      @ls_ItemNO char(20),        -- 商品编码
      @ls_ClassNO char(6),        -- 商品类别编码
      @ln_saleQty numeric(16,4),    -- 商品销售数量
      @ln_sourcePrc numeric(16,4),  -- 商品原来预定的单价
      @ln_salePrc numeric(16,4),    -- 商品销售单价
      @ln_sourceAmt numeric(16,4),  -- 商品原来预定的销售金额
      @ln_saleAmt numeric(16,4),    -- 商品销售金额
      @ln_dctAmt numeric(16,4),   --商品折扣金额.
      @ls_saleWay char(1),        -- 销售方式
      @ldt_saleDateTime datetime,   -- 销售时间
      @ls_operDate char(10),       -- 字符型日期（当天），格式为：yyyymmdd
      @ls_ItemExistFlag char(1),   -- 明细商品在商品销售日结表(t_rm_daySum)中是否已存在标志，'0'=不存在；'1'=已存在
      @ls_casherExistFlag char(1), -- 收银员在收银员日结表(t_rm_daySum_cls)中是否已存在标志，'0'=不存在；'1'=已存在
      @ls_casher  char(4),
      @ln_discountAmt numeric(16,4),
      @ls_spec_flag varchar(10),
      @t_in_price numeric(16,4),
      @cost_price numeric(16,4),
      @oper_id	char(6),
      @flow_no char(18),
      @stock_qty numeric(16,4),
      @stock_type char(1),  	--门店类型
      @local_branch char(6),
      @sale_man  char(6), --营业员
      @ln_sale_rate numeric(16,4) --营业员提成比率
      
	-- 当前销售流水数据
	select @ls_branchNO=branch_no,
		  @ls_ItemNO=item_no,
		  @ln_saleQty=sale_qnty,
		  @ln_sourcePrc=source_price,
		  @ln_salePrc=sale_price,
		  @ls_saleWay=sell_way,
		  @ldt_saleDateTime=oper_date,
		  @ls_casher=oper_id,
		  @ls_spec_flag=spec_flag,
		  @ln_dctAmt=pref_amt,
		  @cost_price = cost_price,
		  @oper_id = oper_id,
		  @flow_no = flow_no,
		  @sale_man=sale_man,
		  @ln_sale_rate=acc_num
		from inserted
	
	SELECT @local_branch = isnull(sys_var_value, '') FROM t_sys_system WHERE sys_var_id = 'g_stock_no'

	--If @local_branch <> @ls_branchNO

	--Begin
	--
	SELECT @stock_type = trade_type FROM t_bd_branch_info WHERE branch_no = left(@ls_branchNO,3)

	--   if @stock_type = '7' or @stock_type = '8' or ( left(@local_branch,3) <> left(@ls_branchNO,3))
	--      return
	--End
        --门店多仓库
        if ( left(@local_branch,3) <> left(@ls_branchNO,3) ) and  ( @stock_type = '7' or @stock_type = '8' )
			return  

	-- 计算原来预定的销售金额和实际的销售金额 金额只取两位小数
	select @ln_sourceAmt = round(@ln_saleQty * @ln_sourcePrc,2)
	select @ln_saleAmt = round(@ln_saleQty * @ln_salePrc,2)

	select @stock_qty = isnull(stock_qty,0) from t_im_branch_stock where branch_no = @ls_branchNO and item_no = @ls_ItemNO

	if @stock_qty is null 
           select @stock_qty = 0

	-- 直接取让利金额
	if (@ls_spec_flag is null or ltrim(@ls_spec_flag)='') select @ln_dctAmt = 0

	
	-- 转换字符型日期（当天），格式为：'112'=yyyymmdd
	select @ls_operDate=convert(char(10),@ldt_saleDateTime,121)
	
	-- 查找在商品销售日结表中是否已存在
	if not exists(select * from t_rm_daysum
	  where(branch_no=@ls_branchNO and item_no=@ls_ItemNO and oper_date=@ls_operDate)) -- 不存在
	  select @ls_ItemExistFlag='0'
	else  -- 已存在
	  select @ls_ItemExistFlag='1'
	
	-- 写商品销售日结表(t_rm_daySum).............................................start
	if @ls_ItemExistFlag='0' -- 明细不存在，增加
	  begin
	    if @ls_saleWay='A' -- 销售
	      insert into t_rm_daysum(branch_no,item_no,oper_date,sale_qty,ret_qty,giv_qty,sale_amt,ret_amt,giv_amt,sale_price,pre_qty,dct_amount) values(
	        @ls_branchNO,@ls_ItemNO,@ls_operDate,@ln_saleQty,0,0,@ln_saleAmt,0,0,@ln_salePrc,0,@ln_dctAmt)
	    else if @ls_saleWay='B' -- 退货
	      insert into t_rm_daysum(branch_no,item_no,oper_date,sale_qty,ret_qty,giv_qty,sale_amt,ret_amt,giv_amt,sale_price,pre_qty,dct_amount) values(
	        @ls_branchNO,@ls_ItemNO,@ls_operDate,0,@ln_saleQty,0,0,@ln_saleAmt,0,@ln_salePrc,0,-@ln_dctAmt)
	    else if @ls_saleWay='C' -- 赠送
	      insert into t_rm_daysum(branch_no,item_no,oper_date,sale_qty,ret_qty,giv_qty,sale_amt,ret_amt,giv_amt,sale_price,pre_qty,dct_amount) values(
	        @ls_branchNO,@ls_ItemNO,@ls_operDate,0,0,@ln_saleQty,0,0,@ln_saleAmt,@ln_salePrc,0,0)
	  end
	else  -- 明细已存在，更新
	  begin
		    if @ls_saleWay='A' -- 销售
		      update t_rm_daysum set
		        sale_qty=sale_qty+@ln_saleQty,
		        sale_amt=sale_amt+@ln_saleAmt,
		        sale_price=@ln_salePrc,
			dct_amount=dct_amount + @ln_dctAmt,
			com_flag='0'
		      where branch_no=@ls_branchNO and item_no=@ls_ItemNO and oper_date=@ls_operDate
		    else if @ls_saleWay='B' -- 退货
		      update t_rm_daysum set
		        ret_qty=ret_qty+@ln_saleQty,
		        ret_amt=ret_amt+@ln_saleAmt,
		        sale_price=@ln_salePrc,
			dct_amount=dct_amount - @ln_dctAmt,
			com_flag='0'
		      where branch_no=@ls_branchNO and item_no=@ls_ItemNO and oper_date=@ls_operDate
		    else if @ls_saleWay='C' -- 赠送
		      update t_rm_daysum set
		        giv_qty=giv_qty+@ln_saleQty,
		        giv_amt=giv_amt+@ln_saleAmt,
		        sale_price=@ln_salePrc,
			com_flag='0'
		      where branch_no=@ls_branchNO and item_no=@ls_ItemNO and oper_date=@ls_operDate
	  end

	-- 查找在营业员日结表中是否已存在
	if not exists(select * from t_da_saleman_daysum
	  where(branch_no=@ls_branchNO and item_no=@ls_ItemNO and oper_date=@ls_operDate and sale_id = @sale_man)) -- 不存在
	  select @ls_ItemExistFlag='0'
	else  -- 已存在
	  select @ls_ItemExistFlag='1'
	
	-- 写营业员日结表(t_da_saleman_daysum)
	if @ls_ItemExistFlag='0' -- 明细不存在，增加
	  begin
	    if @ls_saleWay='A' -- 销售	      
	      insert into t_da_saleman_daysum(branch_no,item_no,oper_date,sale_id,cost_price,pos_qty,pos_ret_qty,pos_send_qty,pos_amt,pos_ret_amt,pos_send_amt,sale_price,sale_rate,pos_dct_amt,com_flag) values(
	        @ls_branchNO,@ls_ItemNO,@ls_operDate,@sale_man,@cost_price,@ln_saleQty,0,0,@ln_saleAmt,0,0,@ln_salePrc,@ln_sale_rate,@ln_dctAmt,'0')
	    else if @ls_saleWay='B' -- 退货
	      insert into t_da_saleman_daysum(branch_no,item_no,oper_date,sale_id,cost_price,pos_qty,pos_ret_qty,pos_send_qty,pos_amt,pos_ret_amt,pos_send_amt,sale_price,sale_rate,pos_dct_amt,com_flag) values(
	        @ls_branchNO,@ls_ItemNO,@ls_operDate,@sale_man,@cost_price,0,@ln_saleQty,0,0,@ln_saleAmt,0,@ln_salePrc,@ln_sale_rate,-@ln_dctAmt,'0')
	    else if @ls_saleWay='C' -- 赠送
	      insert into t_da_saleman_daysum(branch_no,item_no,oper_date,sale_id,cost_price,pos_qty,pos_ret_qty,pos_send_qty,pos_amt,pos_ret_amt,pos_send_amt,sale_price,sale_rate,pos_dct_amt,com_flag) values(
	        @ls_branchNO,@ls_ItemNO,@ls_operDate,@sale_man,@cost_price,0,0,@ln_saleQty,0,0,@ln_saleAmt,@ln_salePrc,@ln_sale_rate,0,'0')
	  end
	else  -- 明细已存在，更新
	  begin
		    if @ls_saleWay='A' -- 销售
		      update t_da_saleman_daysum set
		        pos_qty=pos_qty+@ln_saleQty,
		        pos_amt=pos_amt+@ln_saleAmt,
		        sale_price=@ln_salePrc,
			pos_dct_amt=pos_dct_amt + @ln_dctAmt,
			sale_rate=@ln_sale_rate,
			com_flag='0'
		      where branch_no=@ls_branchNO and item_no=@ls_ItemNO and oper_date=@ls_operDate and sale_id=@sale_man
		    else if @ls_saleWay='B' -- 退货
		      update t_da_saleman_daysum set
		        pos_ret_qty=pos_ret_qty+@ln_saleQty,
		        pos_ret_amt=pos_ret_amt+@ln_saleAmt,
		        sale_price=@ln_salePrc,
			pos_dct_amt=pos_dct_amt - @ln_dctAmt,
			sale_rate=@ln_sale_rate,
			com_flag='0'
		      where branch_no=@ls_branchNO and item_no=@ls_ItemNO and oper_date=@ls_operDate and sale_id=@sale_man
		    else if @ls_saleWay='C' -- 赠送
		      update t_da_saleman_daysum set
		        pos_send_qty=pos_send_qty+@ln_saleQty,
		        pos_send_amt=pos_send_amt+@ln_saleAmt,
		        sale_price=@ln_salePrc,
			sale_rate=@ln_sale_rate,
			com_flag='0'
		      where branch_no=@ls_branchNO and item_no=@ls_ItemNO and oper_date=@ls_operDate and sale_id=@sale_man
	  end

	--更新库存

	-- 判断是否为促销组合商品，是则将成份商品写流水
        if exists(select 1 from t_bd_item_info where item_no=@ls_itemNO and combine_sta ='1')

	begin
	  exec pr_combitem_flow @ldt_saleDateTime,@ls_itemNO,@ln_saleQty,@ln_salePrc,@ls_branchNO,@oper_id,@flow_no
          if @@error != 0 return
	end

	else

	begin

	 if @ls_saleWay='A' -- 销售
	   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) 
		values (@ldt_saleDateTime,@ls_itemNO,@ln_saleQty,@ln_salePrc,@ln_saleAmt,@cost_price,@ln_sourcePrc,@ls_branchNO,'PT','-',@oper_id,@flow_no,'前台销售', @stock_qty -@ln_saleQty,'A')
	 else if @ls_saleWay='B' -- 退货
	   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) 
		values (@ldt_saleDateTime,@ls_itemNO,@ln_saleQty,@ln_salePrc,@ln_saleAmt,@cost_price,@ln_sourcePrc,@ls_branchNO,'PT','-',@oper_id,@flow_no,'前台销售', @stock_qty -@ln_saleQty,'B')
         else if @ls_saleWay='C' -- 赠送
	   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) 
		values (@ldt_saleDateTime,@ls_itemNO,@ln_saleQty,0,0,@cost_price,@ln_sourcePrc,@ls_branchNO,'PT','-',@oper_id,@flow_no,'前台销售', @stock_qty -@ln_saleQty,'C')
	end
end
go