if exists(select 1 from sysobjects where name = 'tr_pc_price_flow_master')
   drop trigger tr_pc_price_flow_master
go


CREATE TRIGGER tr_pc_price_flow_master ON t_pc_price_flow_master 
FOR UPDATE 
AS
BEGIN
  IF NOT UPDATE(approve_flag) RETURN

	DECLARE 
			@sheet_no			CHAR(14),		--单据编号
			@branch_no			CHAR(6),			--仓库编号
			@trans_no			CHAR(2),			--交易编号
			@oper_id				CHAR(4),
			@supcust_no			CHAR(8),			--供应商客户代码
			@sale_way			CHAR(1),
			@item_no				CHAR(20) ,		--货品代码
			@price_type			CHAR(1),			--类型
			@week					CHAR(7),			--星期
			@begin_time			CHAR(8),			--时间1
			@end_time			CHAR(8),			--时间2
			@other1				CHAR(20),		--其它1
			@other2				CHAR(20),		--其它2
			@mother1				CHAR(20),		--其它1
			@mother2				CHAR(20),		--其它2
			@other3				CHAR(20),		--其它3
			@dept_id				CHAR(4),
			@sort_no				CHAR(5),			--序号
			@s_newsheetno		CHAR(14),
			@branch_id			CHAR(2),
			@branchlist			VARCHAR(255),  --窗机构列表
			@oper_date			DATETIME,		--操作日期
			@work_date			DATETIME,		--审核日期
			@start_date			DATETIME,		--日期1
			@end_date			DATETIME,		--日期2
			@old_price			NUMERIC(16,4),	--进货单价
			@new_price			NUMERIC(16,4),	--零售单价
			@discount			NUMERIC(5,4),	--折扣
			@buy_qty				NUMERIC(16,4),	--客单限量
			@limited_qty		NUMERIC(16,4),	--卖场限量
			@stock_qty			NUMERIC(16,4),
			@i_sort_no			INT,				--序号  
			@ln_cnt				INT,				--是否为调价单据
			@ls_property		varchar(1),
			@ls_trade_type		varchar(1),
			@ls_cls_brand_list  varchar(255),     ---类别、品牌列表
			@new_price1			NUMERIC(16,4),	--促销进价 tianmin 20180106
			@in_price           NUMERIC(16,4)	--原进价 tianmin 20180109

	SELECT @i_sort_no = 0, @ln_cnt = 0

	DECLARE priceflow_cur CURSOR FOR
		SELECT DISTINCT	m.trans_no,		m.branch_no,		m.sheet_no,		m.supcust_no,		m.sale_way,
								m.oper_id,		m.branchlist,		d.item_no,		d.price_type,		d.start_date,
								d.end_date,		d.week,				d.begin_time,	d.end_time,			d.old_price,
								d.new_price,	d.discount,			d.buy_qty,		d.other1,			d.other2,
								d.other3,		m.other1,			m.other2,		d.limited_qty,		d.stock_qty,
								m.dept_id,		m.work_date,		m.cls_brand_list,	d.new_price1,0
		FROM	inserted m, t_pc_price_flow_detail d  
		WHERE ( m.sheet_no = d.sheet_no ) AND (m.approve_flag = '1') and m.trans_no <> 'FS'
		union
		SELECT DISTINCT	m.trans_no,		m.branch_no,		m.sheet_no,		m.supcust_no,		m.sale_way,
								m.oper_id,		m.branchlist,		d.item_no,		d.price_type,		d.start_date,
								d.end_date,		d.week,				d.begin_time,	d.end_time,			d.old_price,
								d.new_price,	d.discount,			d.buy_qty,		d.other1,			d.other2,
								d.other3,		m.other1,			m.other2,		d.limited_qty,		d.stock_qty,
								m.dept_id,		m.work_date,		m.cls_brand_list,	d.new_price1,c.price
		FROM	inserted m, t_pc_price_flow_detail d ,t_bd_item_info c  
		WHERE ( m.sheet_no = d.sheet_no ) AND (m.approve_flag = '1')  and (c.item_no = d.item_no) and m.trans_no = 'FS'
	
	OPEN priceflow_cur

	FETCH priceflow_cur INTO	@trans_no,		@branch_no,		@sheet_no,		@supcust_no,		@sale_way,
										@oper_id,		@branchlist,	@item_no,		@price_type,		@start_date,
										@end_date,		@week,			@begin_time,	@end_time,			@old_price, 
										@new_price,		@discount,		@buy_qty,		@other1,				@other2,
										@other3,			@mother1,		@mother2,		@limited_qty,		@stock_qty,
										@dept_id,		@work_date,     @ls_cls_brand_list,	@new_price1,@in_price
	
	WHILE @@fetch_status = 0
		BEGIN	
			--START 组合促销
			IF @trans_no='PG'
				BEGIN
					SELECT @sort_no = @other1
					IF @begin_time is null  SELECT @begin_time = ''
					IF @end_time is null  SELECT @end_time = ''
					
					IF @branch_no = 'ALL' 
						BEGIN
							
							DECLARE branch_cur CURSOR FOR 
								SELECT branch_no FROM t_bd_branch_info WHERE ( property = '0' OR property = '1' ) and not ( trade_type = '3' and branch_no <> '00')
							
							OPEN branch_cur
							FETCH branch_cur INTO @branch_no
							WHILE @@fetch_status=0
								BEGIN
									IF NOT Exists(select 1 from t_rm_spec_price where other3 =@sheet_no and branch_no=@branch_no and item_no =@item_no)
									INSERT 
									INTO t_rm_spec_price(branch_no,			item_no,			special_type,			old_price,			spe_price,
																discount,			sale_qty,		start_date,				end_date,			week,
																begin_time,			end_time,		other1,					other2,				other3,
																limited_qty,		sort_no)  
									VALUES				 (	@branch_no,			@item_no,		'D',						@old_price,			@new_price,
																@discount,			@buy_qty,		@start_date,			@end_date,			@week,
																@begin_time,		@end_time,		@other1,					@other2,				@sheet_no,
																@limited_qty,		@sort_no)  
									
									FETCH branch_cur INTO @branch_no
								END 
							CLOSE branch_cur
							DEALLOCATE  branch_cur 
						END
									
					ELSE IF LEN(@branch_no) = 3 AND @branch_no<>'ALL' 
						BEGIN						
							WHILE LEN(@branchlist) > 0
								BEGIN
									SELECT @branch_no = SUBSTRING(@branchlist, 1, 2) -- 逐个分店更新价格
									SELECT @branchlist = (CASE LEN(@branchlist) WHEN 2 THEN '' ELSE SUBSTRING(@branchlist, 4, LEN(@branchlist) - 3) END )
									IF NOT Exists(select 1 from t_rm_spec_price where other3 =@sheet_no and branch_no=@branch_no and item_no =@item_no)
									INSERT 
									INTO t_rm_spec_price(branch_no,			item_no,			special_type,			old_price,			spe_price,
																discount,			sale_qty,		start_date,				end_date,			week,
																begin_time,			end_time,		other1,					other2,				other3,
																limited_qty,		sort_no)  
									VALUES				 (	@branch_no,			@item_no,		'D',						@old_price,			@new_price,
																@discount,			@buy_qty,		@start_date,			@end_date,			@week,
																@begin_time,		@end_time,		@other1,					@other2,				@sheet_no,
																@limited_qty,		@sort_no)  
								END  
						END 
						
					ELSE
						BEGIN
							IF NOT Exists(select 1 from t_rm_spec_price where other3 =@sheet_no and branch_no=@branch_no and item_no =@item_no)
							INSERT 
							INTO t_rm_spec_price(branch_no,			item_no,			special_type,			old_price,			spe_price,
														discount,			sale_qty,		start_date,				end_date,			week,
														begin_time,			end_time,		other1,					other2,				other3,
														limited_qty,		sort_no,			gift_qty)  
							VALUES				 (	@branch_no,			@item_no,		'D',						@old_price,			@new_price,
														@discount,			@buy_qty,		@start_date,			@end_date,			@week,
														@begin_time,		@end_time,		@other1,					@other2,				@sheet_no,
														@limited_qty,		@sort_no,		@dept_id)   
						END
				END
			--END 组合促销
						
			--START 特价促销
			SELECT @i_sort_no = @i_sort_no+1
			SELECT @sort_no = CONVERT(CHAR(5), @i_sort_no)
			
			IF @trans_no = 'PK' OR @trans_no = 'PS' OR @trans_no = 'PY' OR @trans_no = 'PE' OR @trans_no = 'PV' OR @trans_no = 'PN' 
									  OR @trans_no = 'PH' OR @trans_no = 'PU' OR @trans_no = 'PF' OR @trans_no = 'EP'  --零售&固定&会员
									  OR @trans_no = 'FS'  OR @trans_no = 'QC' --多商品任意促销单
				BEGIN
					IF @begin_time is null  SELECT @begin_time = ''
					IF @end_time is null  SELECT @end_time = ''
					--START 所有机构
					IF @branch_no = 'ALL' 
						BEGIN
							DECLARE branch_cur CURSOR FOR
								
								SELECT branch_no,property,trade_type FROM t_bd_branch_info WHERE ( property = '0' OR property = '1' ) and not ( trade_type = '3' and branch_no <> '00')
								
							OPEN branch_cur
							FETCH branch_cur INTO @branch_no,@ls_property,@ls_trade_type
							WHILE @@fetch_status=0
								BEGIN
									IF @trans_no = 'PE' OR @trans_no = 'PN'  --时段特价 超量购买特价
										BEGIN
											IF NOT Exists(select 1 from t_rm_spec_price where other3 =@sheet_no and branch_no=@branch_no and item_no =@item_no)
											INSERT 
											INTO t_rm_spec_price(branch_no,			item_no,			special_type,			old_price,			spe_price,
																		discount,			sale_qty,		start_date,				end_date,			week,
																		begin_time,			end_time,		other1,					other2,				other3,
																		limited_qty,		sort_no)  
									
		VALUES				 (	@branch_no,			@item_no,		@price_type,			@old_price,			@new_price,
																		@discount,			@buy_qty,		@start_date,			@end_date,			@week,
																		@begin_time,		@end_time,		@other1,					@other2,				@sheet_no,
																		@limited_qty,		@sort_no)
										END
									ELSE IF @trans_no = 'EP'  -- add by wujs 2005-08-27 远方需求增加的促销方式
										BEGIN
											SELECT @price_type = 'A'
											SELECT @end_date = CONVERT(CHAR(10), @end_date, 121) + ' 23:59:59.99'
								
											-- @buy_qty, 冲减积分 limited_qty 结余积分
											IF NOT Exists(select 1 from t_rm_spec_price where other3 =@sheet_no and branch_no=@branch_no and item_no =@item_no)
											INSERT 
											INTO t_rm_spec_price(branch_no,			item_no,			special_type,			old_price,			spe_price,
																		discount,			sale_qty,		start_date,				end_date,			week,
																		other1,				other2,			other3,					limited_qty,		sort_no)  
											VALUES				 (	@branch_no,			@item_no,		@price_type,			@old_price,			@new_price,
																		@discount,			@buy_qty,		@start_date,			@end_date,			@week,
																		@other1,				@other2,			@sheet_no,				@limited_qty,		@sort_no) 
										END  -- END With @trans_no='EP'
									ELSE IF @trans_no='PU'  --类别促销折扣add by jincm 2004-09-09
										IF exists (	SELECT	item_no
														FROM		t_rm_spec_price
														WHERE		(item_no = @item_no AND special_type = @price_type AND branch_no = @branch_no 
														AND		other3 = @sheet_no AND sort_no = @sort_no AND gift_qty = @dept_id ))
											BEGIN   -- BEGIN Exist , UPDATE
												IF (@discount <= 0)   --不作特价商品,将其价格和折扣都设置为零即可
													DELETE 
													FROM		t_rm_spec_price 
													WHERE		(item_no = @item_no AND special_type = @price_type AND branch_no = @branch_no
													AND		other3 = @sheet_no AND sort_no = @sort_no AND gift_qty = @dept_id)
												ELSE -- 更新特价信息（有效数据）
													UPDATE	t_rm_spec_price 
													SET		discount 		= @discount,
																start_date 	= @start_date,  
																end_date 		= @end_date, 
																begin_time 	= @begin_time, 
																end_time 		= @end_time, 
																other1 			= @other1, 
																other2 			= @mother2, 
																other3 			= @sheet_no,
																week 				= @week
													WHERE		(item_no=@item_no AND special_type = @price_type AND branch_no = @branch_no 
													AND		other3 = @sheet_no AND sort_no = @sort_no AND gift_qty = @dept_id)  
											END   -- END Exist , UPDATE
										ELSE  -- No Exist, INSERT
											BEGIN      
												IF NOT Exists(select 1 from t_rm_spec_price where other3 =@sheet_no and branch_no=@branch_no and item_no =@item_no)                
												INSERT 
												INTO t_rm_spec_price(branch_no,			item_no,			special_type,			old_price,			spe_price,
																			discount,			sale_qty,		start_date,				end_date,			begin_time,
																			end_time,			week,				other1,					other2,				other3,
																			sort_no,				gift_qty)  
												VALUES				 (	@branch_no,			@item_no,		@price_type,			0,						0,
																			@discount,			0,					@start_date,			@end_date,			@begin_time,
																			@end_time,			@week,			@other1,					@other2,				@sheet_no,
																			@sort_no,			@dept_id)  
											END
									Else If @trans_no = 'FS' --tianmin 20180106 促销计划(进价)单 此促销是插入促销特价单、促销进价单
										Begin
											--strat插入特价（PS）促销信息
											IF exists (	SELECT	item_no 
																	FROM		t_rm_spec_price
																	WHERE		(item_no = @item_no AND special_type = '0' AND branch_no = @branch_no AND other3 = @sheet_no 
																	AND		sort_no = @sort_no   AND gift_qty = @dept_id))
														BEGIN 
															IF (@new_price <= 0 )   --不作特价商品,将其价格和折扣都设置为零即可
																DELETE 
																FROM		t_rm_spec_price 
																WHERE		(item_no = @item_no AND special_type = '0' AND branch_no = @branch_no AND other3 = @sheet_no
																AND		sort_no = @sort_no   AND gift_qty = @dept_id)
															ELSE --更新特价信息（有效数据）
																UPDATE	t_rm_spec_price 
																SET		special_type = '0', 
																			old_price = @old_price,
																			spe_price = @new_price, 
																			discount = @discount,
																			sale_qty = @buy_qty, 
																			start_date = @start_date, 
																			end_date = @end_date,
																			week = @week, 
																			begin_time = @begin_time, 
																			end_time = @end_time, 
																			other1 = @other1, 
																			other2 = 'FS', 
																			other3 = @sheet_no,
																			limited_qty=@limited_qty,
																			sort_no=@sort_no,
																			cls_brand_list = @ls_cls_brand_list
																WHERE		(item_no = @item_no AND special_type = '0' AND branch_no = @branch_no AND other3 = @sheet_no 
																AND		sort_no = @sort_no  AND begin_time is null  AND gift_qty = @dept_id)  
														END 
													ELSE
														BEGIN
															IF NOT Exists(select 1 from t_rm_spec_price where other3 =@sheet_no and branch_no=@branch_no and item_no =@item_no AND special_type = '0')
															INSERT 
															INTO t_rm_spec_price(branch_no,			item_no,			special_type,			old_price,			spe_price,
																						discount,			sale_qty,		start_date,				end_date,			week,
																						begin_time,			end_time,		other1,					other2,				other3,
																						limited_qty,		sort_no,			gift_qty,		cls_brand_list)  
															VALUES				 (	@branch_no,			@item_no,		'0',			@old_price,			@new_price,
																						@discount,			@buy_qty,		@start_date,			@end_date,			@week,
																						@begin_time,		@end_time,		@other1,					'FS',				@sheet_no,
																						@limited_qty,		@sort_no,		@dept_id,			@ls_cls_brand_list)   
														END
												--end插入特价（PS）促销信息
												--strat插入进价（Pf）促销信息
											IF exists (	SELECT	item_no 
																	FROM		t_rm_spec_price
																	WHERE		(item_no = @item_no AND special_type = 'C' AND branch_no = @branch_no AND other3 = @sheet_no 
																	AND		sort_no = @sort_no    AND gift_qty = @dept_id))
														BEGIN 
															IF (@new_price1 <= 0 )   --不作特价商品,将其价格和折扣都设置为零即可
																DELETE 
																FROM		t_rm_spec_price 
																WHERE		(item_no = @item_no AND special_type = 'C' AND branch_no = @branch_no AND other3 = @sheet_no
																AND		sort_no = @sort_no   AND gift_qty = @dept_id)
															ELSE --更新特价信息（有效数据）
																UPDATE	t_rm_spec_price 
																SET		special_type = 'C', 
																			old_price = @in_price,
																			spe_price = @new_price1, 
																			discount = @discount,
																			sale_qty = @buy_qty, 
																			start_date = @start_date, 
																			end_date = @end_date,
																			week = @week, 
																			begin_time = @begin_time, 
																			end_time = @end_time, 
																			other1 = @other1, 
																			other2 = 'FS', 
																			other3 = @sheet_no,
																			limited_qty=@limited_qty,
																			sort_no=@sort_no,
																			cls_brand_list = @ls_cls_brand_list
																WHERE		(item_no = @item_no AND special_type = 'C' AND branch_no = @branch_no AND other3 = @sheet_no 
																AND		sort_no = @sort_no  AND begin_time is null  AND gift_qty = @dept_id)  
														END 
													ELSE
														BEGIN
															IF NOT Exists(select 1 from t_rm_spec_price where other3 =@sheet_no and branch_no=@branch_no and item_no =@item_no AND special_type = 'C')
															INSERT 
															INTO t_rm_spec_price(branch_no,			item_no,			special_type,			old_price,			spe_price,
																						discount,			sale_qty,		start_date,				end_date,			week,
																						begin_time,			end_time,		other1,					other2,				other3,
																						limited_qty,		sort_no,			gift_qty,		cls_brand_list)  
															VALUES				 (	@branch_no,			@item_no,		'C',			@in_price,			@new_price1,
																						@discount,			@buy_qty,		@start_date,			@end_date,			@week,
																						@begin_time,		@end_time,		@other1,					'FS',				@sheet_no,
																						@limited_qty,		@sort_no,		@dept_id,			@ls_cls_brand_list)   
														END
												--end插入进价（Pf）促销信息
												end
									ELSE IF @trans_no = 'PK' OR @trans_no = 'PS' OR @trans_no = 'PY' OR @trans_no = 'PV' OR @trans_no = 'PH' OR @trans_no = 'PF' OR @trans_no = 'QC'--多商品任意促销单
										BEGIN
											IF @trans_no='PH' 
												begin
													-----youdp--2013-07-31--超额奖励---supcust_no为1表示类别--2表示品牌---其余表示全场-----------------
													SELECT @old_price = CONVERT(NUMERIC(15,4), @mother1),
														   @new_price = CONVERT(NUMERIC(15,4), @mother2), 
														   @price_type = (case @supcust_no when '1' then 'G' when '2' then 'H' else '9' end)
													---------------------------------------------------------------------------------------------------
												end
											IF @trans_no='QC' 
												begin
													---多商品任意促销单
													SELECT @old_price = CONVERT(NUMERIC(15,4), @mother1),
														   @new_price = CONVERT(NUMERIC(15,4), @mother2), 
														   @price_type = 'N'
													---------------------------------------------------------------------------------------------------
												end
											IF @trans_no='PV'
												BEGIN
													SELECT @price_type = '2'		 
													SELECT @other1 = @mother1
												END
											----------youdp--2013-05-16---在总部不能给加盟店建立促销进价------------------------------------
											if not (@trans_no = 'PF' and @ls_trade_type = '2' and @ls_property = '0') 
												begin
													IF exists (	SELECT	item_no 
																	FROM		t_rm_spec_price
																	WHERE		(item_no = @item_no AND special_type = @price_type AND branch_no = @branch_no AND other3 = @sheet_no 
																	AND		sort_no = @sort_no  AND begin_time is null AND special_type <> '4' AND gift_qty = @dept_id))
														BEGIN 
															IF (@new_price <= 0 AND @discount <= 0)   --不作特价商品,将其价格和折扣都设置为零即可
																DELETE 
																FROM		t_rm_spec_price 
																WHERE		(item_no = @item_no AND special_type = @price_type AND branch_no = @branch_no AND other3 = @sheet_no
																AND		sort_no = @sort_no  AND begin_time is null AND special_type <> '4'AND gift_qty = @dept_id)
															ELSE --更新特价信息（有效数据）
																UPDATE	t_rm_spec_price 
																SET		special_type = @price_type, 
																			old_price = @old_price,
																			spe_price = @new_price, 
																			discount = @discount,
																			sale_qty = @buy_qty, 
																			start_date = @start_date, 
																			end_date = @end_date,
																			week = @week, 
																			begin_time = @begin_time, 
																			end_time = @end_time, 
																			other1 = @other1, 
																			other2 = @other2, 
																			other3 = @sheet_no,
																			limited_qty=@limited_qty,
																			sort_no=@sort_no,
																			cls_brand_list = @ls_cls_brand_list
																WHERE		(item_no = @item_no AND special_type = @price_type AND branch_no = @branch_no AND other3 = @sheet_no 
																AND		sort_no = @sort_no  AND begin_time is null AND special_type <> '4' AND gift_qty = @dept_id)  
														END 
													ELSE
														BEGIN
															IF NOT Exists(select 1 from t_rm_spec_price where other3 =@sheet_no and branch_no=@branch_no and item_no =@item_no)
															INSERT 
															INTO t_rm_spec_price(branch_no,			item_no,			special_type,			old_price,			spe_price,
																						discount,			sale_qty,		start_date,				end_date,			week,
																						begin_time,			end_time,		other1,					other2,				other3,
																						limited_qty,		sort_no,			gift_qty,		cls_brand_list
)  
															VALUES				 (	@branch_no,			@item_no,		@price_type,			@old_price,			@new_price,
																						@discount,			@buy_qty,		@start_date,			@end_date,			@week,
																						@begin_time,		@end_time,		@other1,					@other2,				@sheet_no,
																						@limited_qty,		@sort_no,		@dept_id,			@ls_cls_brand_list)   
														END
												end
										END
									FETCH branch_cur INTO @branch_no,@ls_property,@ls_trade_type
								END 
							CLOSE branch_cur
							DEALLOCATE  branch_cur 
						END 
					--END 所有机构
				
					--START 分店机构
					ELSE IF LEN(@branch_no) = 3 AND @branch_no<>'ALL' 
						BEGIN					
							WHILE LEN(@branchlist) > 0
								BEGIN
									SELECT @branch_no = SUBSTRING(@branchlist, 1, 2) -- 逐个分店更新价格
									SELECT @branchlist = (CASE LEN(@branchlist) WHEN 2 THEN '' ELSE SUBSTRING(@branchlist, 4, LEN(@branchlist) - 3) END )
									
									IF @trans_no = 'PE' OR @trans_no = 'PN'  --时段特价 超量购买特价
									BEGIN
										IF NOT Exists(select 1 from t_rm_spec_price where other3 =@sheet_no and branch_no=@branch_no and item_no =@item_no)
										INSERT 
										INTO t_rm_spec_price(branch_no,			item_no,			special_type,			old_price,			spe_price,
																	discount,			sale_qty,		start_date,				end_date,			week,
																	begin_time,			end_time,		other1,					other2,				other3,
																	limited_qty,		sort_no)  
										VALUES				 (	@branch_no,			@item_no,		@price_type,			@old_price,			@new_price,
																	@discount,			@buy_qty,		@start_date,			@end_date,			@week,
																	@begin_time,		@end_time,		@other1,					@other2,				@sheet_no,
																	@limited_qty,		@sort_no)   
									END
									ELSE IF @trans_no='EP'  -- add by wujs 2005-08-27 远方需求增加的促销方式
										BEGIN
											SELECT @price_type = 'A'
											SELECT @end_date = CONVERT(CHAR(10), @end_date, 121) + ' 23:59:59.99'
											
											-- @buy_qty, 冲减积分 limited_qty 结余积分
											IF NOT Exists(select 1 from t_rm_spec_price where other3 =@sheet_no and branch_no=@branch_no and item_no =@item_no)
											INSERT 
											INTO t_rm_spec_price(branch_no,			item_no,			special_type,			old_price,			spe_price,
																		discount,			sale_qty,		start_date,				end_date,			week,
																		other1,				other2,			other3,					limited_qty,		sort_no)  
											VALUES				 (	@branch_no,			@item_no,		@price_type,			@old_price,			@new_price,
																		@discount,			@buy_qty,		@start_date,			@end_date,			@week,
																		@other1,				@other2,			@sheet_no,				@limited_qty,		@sort_no) 
										END  -- END With @trans_no='EP'
									ELSE IF @trans_no = 'PU'  --类别促销折扣add by jincm 2004-09-09
										IF exists (	SELECT	item_no 
														FROM		t_rm_spec_price 
														WHERE		(item_no = @item_no AND special_type = @price_type AND branch_no = @branch_no 
														AND		other3 = @sheet_no AND sort_no = @sort_no AND gift_qty = @dept_id ))
											BEGIN   -- BEGIN Exist , UPDATE
												IF (@discount <= 0)   --不作特价商品,将其价格和折扣都设置为零即可
													DELETE 
													FROM		t_rm_spec_price 
													WHERE		(item_no = @item_no AND special_type = @price_type AND branch_no = @branch_no 
													AND		other3 = @sheet_no AND sort_no = @sort_no AND gift_qty = @dept_id)
												ELSE -- 更新特价信息（有效数据）
													UPDATE	t_rm_spec_price 
													SET		discount = @discount,
																start_date = @start_date,
																end_date = @end_date, 
																begin_time = @begin_time,
																end_time = @end_time, 
																other1 = @other1, 
																other2 = @mother2,
																other3 = @sheet_no,
																week = @week
													WHERE		(item_no = @item_no AND special_type = @price_type AND branch_no = @branch_no 
													AND		other3 = @sheet_no AND sort_no = @sort_no AND gift_qty = @dept_id)  

											END   -- END Exist , UPDATE
										ELSE  -- No Exist, INSERT
											BEGIN      
												IF NOT Exists(select 1 from t_rm_spec_price where other3 =@sheet_no and branch_no=@branch_no and item_no =@item_no)                
												INSERT 
												INTO t_rm_spec_price(branch_no,			item_no,			special_type,			old_price,			spe_price,
																			discount,			sale_qty,		start_date,				end_date,			begin_time,
																			end_time,			week,				other1,					other2,				other3,
																			sort_no,				gift_qty)  
												VALUES				 (	@branch_no,			@item_no,		@price_type,			0,						0, 
																			@discount,			0,					@start_date,			@end_date,			@begin_time,
																			@end_time,			@week,			@other1,					@other2,				@sheet_no,
																			@sort_no,			@dept_id)  
											END
								    Else If @trans_no = 'FS' --tianmin 20180106 促销计划(进价)单 此促销是插入促销特价单、促销进价单
										Begin
											--strat插入特价（PS）促销信息
											IF exists (	SELECT	item_no
																FROM		t_rm_spec_price
																WHERE		(item_no = @item_no AND special_type = '0' AND branch_no = @branch_no AND other3 = @sheet_no
																AND		sort_no = @sort_no  AND gift_qty = @dept_id))
													BEGIN 
														IF (@new_price <= 0 )   --不作特价商品,将其价格和折扣都设置为零即可
															DELETE 
															FROM		t_rm_spec_price 
															WHERE		(item_no = @item_no AND special_type = '0' AND branch_no = @branch_no AND other3 = @sheet_no 
															AND		sort_no = @sort_no AND gift_qty = @dept_id)
														ELSE --更新特价信息（有效数据）
															UPDATE	t_rm_spec_price
															SET		special_type = '0', 
																		old_price = @old_price,
																		spe_price = @new_price, 
																		discount = @discount,
																		sale_qty = @buy_qty, 
																		start_date = @start_date, 
																		end_date = @end_date,
																		week = @week,  
																		begin_time = @begin_time, 
																		end_time = @end_time, 
																		other1 = @other1, 
																		other2 = 'FS', 
																		other3 = @sheet_no,
																		limited_qty = @limited_qty,
																		sort_no = @sort_no,
																		cls_brand_list = @ls_cls_brand_list
															WHERE		(item_no = @item_no AND special_type = '0' AND branch_no = @branch_no AND other3 = @sheet_no 
															AND		sort_no = @sort_no   AND gift_qty = @dept_id)  
													END 
												ELSE
													BEGIN
														IF NOT Exists(select 1 from t_rm_spec_price where other3 =@sheet_no and branch_no=@branch_no and item_no =@item_no AND special_type = '0')
														INSERT 
														INTO t_rm_spec_price(branch_no,			item_no,			special_type,			old_price,			spe_price,
																					discount,			sale_qty,		start_date,				end_date,			week,
																					begin_time,			end_time,		other1,					other2,				other3,
																					limited_qty,		sort_no,			gift_qty,			cls_brand_list)  
														VALUES				 ( @branch_no,			@item_no,		'0',			@old_price,			@new_price,
																					@discount,			@buy_qty,		@start_date,			@end_date,			@week,
																					@begin_time,		@end_time,		@other1,					'FS',				@sheet_no,
																					@limited_qty,		@sort_no,		@dept_id,				@ls_cls_brand_list)   
													END
											    --end插入特价（PS）促销信息
											    --strat插入进价（Pf）促销信息
											IF exists (	SELECT	item_no
																FROM		t_rm_spec_price
																WHERE		(item_no = @item_no AND special_type = 'C' AND branch_no = @branch_no AND other3 = @sheet_no
																AND		sort_no = @sort_no  AND gift_qty = @dept_id))
													BEGIN 
														IF (@new_price <= 0 )   --不作特价商品,将其价格和折扣都设置为零即可
															DELETE 
															FROM		t_rm_spec_price 
															WHERE		(item_no = @item_no AND special_type = 'C' AND branch_no = @branch_no AND other3 = @sheet_no 
															AND		sort_no = @sort_no AND gift_qty = @dept_id)
														ELSE --更新特价信息（有效数据）
															UPDATE	t_rm_spec_price
															SET		special_type = 'C', 
																		old_price = @in_price,
																		spe_price = @new_price1, 
																		discount = @discount,
																		sale_qty = @buy_qty, 
																		start_date = @start_date, 
																		end_date = @end_date,
																		week = @week,  
																		begin_time = @begin_time, 
																		end_time = @end_time, 
																		other1 = @other1, 
																		other2 = 'FS', 
																		other3 = @sheet_no,
																		limited_qty = @limited_qty,
																		sort_no = @sort_no,
																		cls_brand_list = @ls_cls_brand_list
															WHERE		(item_no = @item_no AND special_type = 'C' AND branch_no = @branch_no AND other3 = @sheet_no 
															AND		sort_no = @sort_no   AND gift_qty = @dept_id)  
													END 
												ELSE
													BEGIN
														IF NOT Exists(select 1 from t_rm_spec_price where other3 =@sheet_no and branch_no=@branch_no and item_no =@item_no AND special_type = 'C')
														INSERT 
														INTO t_rm_spec_price(branch_no,			item_no,			special_type,			old_price,			spe_price,
																					discount,			sale_qty,		start_date,				end_date,			week,
																					begin_time,			end_time,		other1,					other2,				other3,
																					limited_qty,		sort_no,			gift_qty,			cls_brand_list)  
														VALUES				 ( @branch_no,			@item_no,		'C',			@in_price,			@new_price1,
																					@discount,			@buy_qty,		@start_date,			@end_date,			@week,
																					@begin_time,		@end_time,		@other1,					'FS',				@sheet_no,
																					@limited_qty,		@sort_no,		@dept_id,				@ls_cls_brand_list)   
													END
											    --end插入进价（Pf）促销信息
										end--fs
									ELSE IF @trans_no = 'PK' OR @trans_no = 'PS' OR @trans_no = 'PY' OR @trans_no = 'PV' OR @trans_no = 'PH' OR @trans_no = 'PF' OR @trans_no = 'QC'
										BEGIN
											IF @trans_no = 'PH' 
												begin
													-----youdp--2013-07-31--超额奖励---supcust_no为1表示类别--2表示品牌---其余表示全场-----------------
													SELECT @old_price = CONVERT(NUMERIC(15,4), @mother1),
														   @new_price = CONVERT(NUMERIC(15,4), @mother2), 
														   @price_type = (case @supcust_no when '1' then 'G' when '2' then 'H' else '9' end)
													---------------------------------------------------------------------------------------------------
												end

											IF @trans_no = 'QC' 
												begin
													-----多商品任意促销单-----------------
													SELECT @old_price = CONVERT(NUMERIC(15,4), @mother1),
														   @new_price = CONVERT(NUMERIC(15,4), @mother2), 
														   @price_type = 'N'
													---------------------------------------------------------------------------------------------------
												end
										
											IF @trans_no='PV' 
												BEGIN
													SELECT @price_type = '2'
													SELECT @other1 = @mother1
												END
												
											IF exists (	SELECT	item_no
															FROM		t_rm_spec_price
															WHERE		(item_no = @item_no AND special_type = @price_type AND branch_no = @branch_no AND other3 = @sheet_no
															AND		sort_no = @sort_no  AND begin_time is null AND special_type <> '4' AND gift_qty = @dept_id))
												BEGIN 
													IF (@new_price <= 0 AND @discount <= 0)   --不作特价商品,将其价格和折扣都设置为零即可
														DELETE 
														FROM		t_rm_spec_price 
														WHERE		(item_no = @item_no AND special_type = @price_type AND branch_no = @branch_no AND other3 = @sheet_no 
														AND		sort_no = @sort_no  AND begin_time is null AND special_type<>'4'AND gift_qty = @dept_id)
													ELSE --更新特价信息（有效数据）
														UPDATE	t_rm_spec_price
														SET		special_type = @price_type, 
																	old_price = @old_price,
																	spe_price = @new_price, 
																	discount = @discount,
																	sale_qty = @buy_qty, 
																	start_date = @start_date, 
																	end_date = @end_date,
																	week = @week,  
																	begin_time = @begin_time, 
																	end_time = @end_time, 
																	other1 = @other1, 
																	other2 = @other2, 
																	other3 = @sheet_no,
																	limited_qty = @limited_qty,
																	sort_no = @sort_no,
																	cls_brand_list = @ls_cls_brand_list
														WHERE		(item_no = @item_no AND special_type = @price_type AND branch_no = @branch_no AND other3 = @sheet_no 
														AND		sort_no = @sort_no  AND begin_time is null AND special_type <> '4' AND gift_qty = @dept_id)  
												END 
											ELSE
												BEGIN
													IF NOT Exists(select 1 from t_rm_spec_price where other3 =@sheet_no and branch_no=@branch_no and item_no =@item_no)
													INSERT 
													INTO t_rm_spec_price(branch_no,			item_no,			special_type,			old_price,			spe_price,
																				discount,			sale_qty,		start_date,				end_date,			week,
																				begin_time,			end_time,		other1,					other2,				other3,
																				limited_qty,		sort_no,			gift_qty,			cls_brand_list)  
													VALUES				 ( @branch_no,			@item_no,		@price_type,			@old_price,			@new_price,
																				@discount,			@buy_qty,		@start_date,			@end_date,			@week,
																				@begin_time,		@end_time,		@other1,					@other2,				@sheet_no,
																				@limited_qty,		@sort_no,		@dept_id,				@ls_cls_brand_list)   
												END
										END        
								END --END with WHILE
						END 
					--END 分店机构
	
					--START 单个机构调价			
					ELSE 
						BEGIN
							IF @trans_no = 'PE' OR @trans_no = 'PN'  --固定时间特价 超量购买特价
							BEGIN
								IF NOT Exists(select 1 from t_rm_spec_price where other3 =@sheet_no and branch_no=@branch_no and item_no =@item_no)
								INSERT 
								INTO t_rm_spec_price(branch_no,			item_no,			special_type,			old_price,			spe_price,
															discount,			sale_qty,		start_date,				end_date,			week,
															begin_time,			end_time,		other1,					other2,				other3,
															limited_qty,		sort_no)  
								VALUES				 (	@branch_no,			@item_no,		@price_type,			@old_price,			@new_price,
															@discount,			@buy_qty,		@start_date,			@end_date,			@week,
															@begin_time,		@end_time,		@other1,					@other2,				@sheet_no,
															@limited_qty,		@sort_no) 
							END
							ELSE IF @trans_no = 'PU'  --类别促销折扣add by jincm 2004-09-09
								IF exists (	SELECT	item_no 
												FROM		t_rm_spec_price 
												WHERE		(item_no = @item_no AND special_type = @price_type AND branch_no = @branch_no AND other3 = @sheet_no 
												AND		sort_no = @sort_no AND gift_qty = @dept_id ))
									BEGIN   --BEGIN Exist , UPDATE
										IF (@discount <= 0)   --不作特价商品,将其价格和折扣都设置为零即可
											DELETE 
											FROM		t_rm_spec_price 
											WHERE		(item_no = @item_no AND special_type = @price_type AND branch_no = @branch_no AND other3 = @sheet_no 
											AND		sort_no = @sort_no AND gift_qty = @dept_id )
										ELSE --更新特价信息（有效数据）
											UPDATE	t_rm_spec_price
											SET		discount = @discount,
														start_date = @start_date,
														end_date = @end_date, 
														begin_time = @begin_time, 
														end_time = @end_time, 
														other1 = @other1, 
														other2 = @mother2, 
														other3 = @sheet_no,
														week = @week
											WHERE		(item_no = @item_no AND special_type = @price_type AND branch_no = @branch_no AND other3 = @sheet_no 
											AND		sort_no = @sort_no AND gift_qty = @dept_id )  
									END   --END Exist , UPDATE
								ELSE  --No Exist, INSERT
									BEGIN               
										IF NOT Exists(select 1 from t_rm_spec_price where other3 =@sheet_no and branch_no=@branch_no and item_no =@item_no)       
										INSERT 
										INTO t_rm_spec_price(branch_no,			item_no,			special_type,			old_price,			spe_price,
																	discount,			sale_qty,		start_date,				end_date,			begin_time,
																	end_time,			week,				other1,					other2,				other3,
																	sort_no,				gift_qty)  
										VALUES				 (	@branch_no,			@item_no,		@price_type,			0,						0,
																	@discount,			0,					@start_date,			@end_date,			@begin_time,
																	@end_time,			@week,			@other1,					@other2,				@sheet_no,
																	@sort_no,			@dept_id)  
									END
							ELSE IF @trans_no = 'EP'  -- add by wujs 2005-08-27 远方需求增加的促销方式
								BEGIN
									SELECT @price_type = 'A'
									SELECT @end_date = CONVERT(CHAR(10), @end_date, 121) + ' 23:59:59.99'
					
									-- @buy_qty, 冲减积分 limited_qty 结余积分
									IF NOT Exists(select 1 from t_rm_spec_price where other3 =@sheet_no and branch_no=@branch_no and item_no =@item_no)
									INSERT 
									INTO t_rm_spec_price(branch_no,			item_no,			special_type,			old_price,			spe_price,
																discount,			sale_qty,		start_date,				end_date,			week,
																other1,				other2,			other3,					limited_qty,		sort_no)  
									VALUES				 (	@branch_no,			@item_no,		@price_type,			@old_price,			@new_price,
																@discount,			@buy_qty,		@start_date,			@end_date,			@week,
																@other1,				@other2,			@sheet_no,				@limited_qty,		@sort_no) 
								END  -- END With @trans_no='PP'	          
				            Else If @trans_no = 'FS' --tianmin 20180106 促销计划(进价)单 此促销是插入促销特价单、促销进价单
								Begin
									--strat插入特价（PS）促销信息
									IF exists (	SELECT	item_no
													FROM		t_rm_spec_price 
													WHERE		(item_no = @item_no AND special_type = '0' AND branch_no = @branch_no AND other3 = @sheet_no 
													AND		sort_no = @sort_no   AND gift_qty = @dept_id) )
										BEGIN   --BEGIN Exist , UPDATE
											IF (@new_price <= 0 )   --不作特价商品,将其价格和折扣都设置为零即可
												DELETE 
												FROM		t_rm_spec_price 
												WHERE		(item_no = @item_no AND special_type = '0' AND branch_no = @branch_no AND other3 = @sheet_no 
												AND		sort_no = @sort_no  AND gift_qty = @dept_id)
											ELSE -- 更新特价信息（有效数据）
												UPDATE	t_rm_spec_price
												SET		special_type = '0', 
															old_price = @old_price,
															spe_price = @new_price, 
															discount = @discount,
															sale_qty = @buy_qty, 
															start_date = @start_date,  
															end_date = @end_date, 
															week = @week, 
															begin_time = @begin_time, 
															end_time = @end_time, 
															other1 = @other1, 
															other2 = 'FS', 
															other3 = @sheet_no,
															limited_qty = @limited_qty,
															sort_no = @sort_no,
															cls_brand_list = @ls_cls_brand_list
												WHERE		(item_no = @item_no AND special_type = '0' AND branch_no = @branch_no AND other3 = @sheet_no 
												AND		sort_no = @sort_no )  
										END -- END Exist , UPDATE
									ELSE  -- No Exist, INSERT
										BEGIN
											IF NOT Exists(select 1 from t_rm_spec_price where other3 =@sheet_no and branch_no=@branch_no and item_no =@item_no AND special_type = '0')
											INSERT 
											INTO t_rm_spec_price(branch_no,			item_no,			special_type,			old_price,			spe_price,
																		discount,			sale_qty,		start_date,				end_date,			week,
																		begin_time,			end_time,		other1,					other2,				other3,
																		limited_qty,		sort_no,			gift_qty,		cls_brand_list)  
											VALUES				 ( @branch_no,			@item_no,		'0',			@old_price,			@new_price,
																		@discount,			@buy_qty,		@start_date,			@end_date,			@week,
																		@begin_time,		@end_time,		@other1,					'FS',				@sheet_no,
																		@limited_qty,		@sort_no,		@dept_id,			@ls_cls_brand_list)   
										END 
										--end插入特价（PS）促销信息
										--strat插入进价（Pf）促销信息
									IF exists (	SELECT	item_no
													FROM		t_rm_spec_price 
													WHERE		(item_no = @item_no AND special_type = 'C' AND branch_no = @branch_no AND other3 = @sheet_no 
													AND		sort_no = @sort_no   AND gift_qty = @dept_id) )
										BEGIN   --BEGIN Exist , UPDATE
											IF (@new_price <= 0 )   --不作特价商品,将其价格和折扣都设置为零即可
												DELETE 
												FROM		t_rm_spec_price 
												WHERE		(item_no = @item_no AND special_type = 'C' AND branch_no = @branch_no AND other3 = @sheet_no 
												AND		sort_no = @sort_no  AND gift_qty = @dept_id)
											ELSE -- 更新特价信息（有效数据）
												UPDATE	t_rm_spec_price
												SET		special_type = 'C', 
															old_price = @in_price,
															spe_price = @new_price1, 
															discount = @discount,
															sale_qty = @buy_qty, 
															start_date = @start_date,  
															end_date = @end_date, 
															week = @week, 
															begin_time = @begin_time, 
															end_time = @end_time, 
															other1 = @other1, 
															other2 = 'FS', 
															other3 = @sheet_no,
															limited_qty = @limited_qty,
															sort_no = @sort_no,
															cls_brand_list = @ls_cls_brand_list
												WHERE		(item_no = @item_no AND special_type = 'C' AND branch_no = @branch_no AND other3 = @sheet_no 
												AND		sort_no = @sort_no)  
										END -- END Exist , UPDATE
									ELSE  -- No Exist, INSERT
										BEGIN
											IF NOT Exists(select 1 from t_rm_spec_price where other3 =@sheet_no and branch_no=@branch_no and item_no =@item_no AND special_type = 'C')
											INSERT 
											INTO t_rm_spec_price(branch_no,			item_no,			special_type,			old_price,			spe_price,
																		discount,			sale_qty,		start_date,				end_date,			week,
																		begin_time,			end_time,		other1,					other2,				other3,
																		limited_qty,		sort_no,			gift_qty,		cls_brand_list)  
											VALUES				 ( @branch_no,			@item_no,		'C',			@in_price,			@new_price1,
																		@discount,			@buy_qty,		@start_date,			@end_date,			@week,
																		@begin_time,		@end_time,		@other1,					'FS',				@sheet_no,
																		@limited_qty,		@sort_no,		@dept_id,			@ls_cls_brand_list)   
										END 
										--end插入进价（Pf）促销信息
								End --fs
							ELSE IF @trans_no = 'PK' OR @trans_no = 'PS' OR @trans_no = 'PY' OR @trans_no = 'PV' OR @trans_no = 'PH' OR @trans_no = 'PF' OR @trans_no = 'QC'
								BEGIN
									IF @trans_no='PH' 
										begin
											-----youdp--2013-07-31--超额奖励---supcust_no为1表示类别--2表示品牌---其余表示全场-----------------
											SELECT @old_price = CONVERT(NUMERIC(15,4), @mother1),
												   @new_price = CONVERT(NUMERIC(15,4), @mother2), 
												   @price_type = (case @supcust_no when '1' then 'G' when '2' then 'H' else '9' end)
											---------------------------------------------------------------------------------------------------
										end
									
									IF @trans_no='QC' 
										begin
											-----多商品任意促销单-----------------
											SELECT @old_price = CONVERT(NUMERIC(15,4), @mother1),
												   @new_price = CONVERT(NUMERIC(15,4), @mother2), 
												   @price_type ='N'
											---------------------------------------------------------------------------------------------------
										end

									IF @trans_no = 'PV' 
										BEGIN
											SELECT @price_type = '2'
											SELECT @other1 = @mother1
										END
				
				
									IF exists (	SELECT	item_no
													FROM		t_rm_spec_price 
													WHERE		(item_no = @item_no AND special_type = @price_type AND branch_no = @branch_no AND other3 = @sheet_no 
													AND		sort_no = @sort_no  AND begin_time is null AND special_type <> '4' AND gift_qty = @dept_id) )
										BEGIN   --BEGIN Exist , UPDATE
											IF (@new_price <= 0 AND @discount <= 0)   --不作特价商品,将其价格和折扣都设置为零即可
												DELETE 
												FROM		t_rm_spec_price 
												WHERE		(item_no = @item_no AND special_type = @price_type AND branch_no = @branch_no AND other3 = @sheet_no 
												AND		sort_no = @sort_no  AND begin_time is null AND special_type <> '4' AND gift_qty = @dept_id)
											ELSE -- 更新特价信息（有效数据）
												UPDATE	t_rm_spec_price
												SET		special_type = @price_type, 
															old_price = @old_price,						
															spe_price = @new_price, 
															discount = @discount,
															sale_qty = @buy_qty, 
															start_date = @start_date,  
															end_date = @end_date, 
															week = @week, 
															begin_time = @begin_time, 
															end_time = @end_time, 
															other1 = @other1, 
															other2 = @other2, 
															other3 = @sheet_no,
															limited_qty = @limited_qty,
															sort_no = @sort_no,
															cls_brand_list = @ls_cls_brand_list
												WHERE		(item_no = @item_no AND special_type = @price_type AND branch_no = @branch_no AND other3 = @sheet_no 
												AND		sort_no = @sort_no  AND begin_time is null AND special_type <> '4')  
										END -- END Exist , UPDATE
									ELSE  -- No Exist, INSERT
										BEGIN
											IF NOT Exists(select 1 from t_rm_spec_price where other3 =@sheet_no and branch_no=@branch_no and item_no =@item_no)
											INSERT 
											INTO t_rm_spec_price(branch_no,			item_no,			special_type,			old_price,			spe_price,
																		discount,			sale_qty,		start_date,				end_date,			week,
																		begin_time,			end_time,		other1,					other2,				other3,
																		limited_qty,		sort_no,			gift_qty,		cls_brand_list)  
											VALUES				 ( @branch_no,			@item_no,		@price_type,			@old_price,			@new_price,
																		@discount,			@buy_qty,		@start_date,			@end_date,			@week,
																		@begin_time,		@end_time,		@other1,					@other2,				@sheet_no,
																		@limited_qty,		@sort_no,		@dept_id,			@ls_cls_brand_list)   
										END 
								END                 
						END
					--END 单个机构调价			
				END
			--END 特价促销
			
			-- modified by wujs 2005-09-30 增加成本调价单调成本价
			IF @trans_no='PC' -- 成本调价单
				BEGIN
					--写出入库流水 tangsj 2011-12-23
					--exec pr_pc_change_costprice_new @item_no, @branch_no, @new_price
					INSERT 
					INTO t_im_flow(voucher_no,			oper_date,			item_no,			real_qty,			price,
										cost_price,			sheet_amt,			branch_no,		d_branch_no,		trans_no,
										db_no,				supcust_no,			oper_id,			pre_qnty,			acc_date,
										costsheet_flag)
							SELECT	@sheet_no,			@work_date,			@item_no,		0,						@new_price,
										@new_price,			0,						branch_no,		'',					'PC',
										'+',					'',					@oper_id,		0,						@work_date,
										'0'
							FROM		t_bd_branch_info 
							WHERE		(@branch_no<>'ALL' AND branch_no=@branch_no) OR (@branch_no='ALL' AND LEN(branch_no)=2 
							AND		(property='1' OR trade_type='0' OR trade_type='4'))
					IF @@error != 0 
						BEGIN
							CLOSE priceflow_cur
							DEALLOCATE  priceflow_cur
							RETURN
						END
				END
			
			SELECT @branch_no = LTRIM(RTRIM(sys_var_value)) FROM t_sys_system WHERE LOWER(sys_var_id) = 'g_branch_no'
			UPDATE t_pc_price_flow_master set branch_flag = @branch_no WHERE sheet_no = @sheet_no
			
			IF @trans_no = 'PX' OR @trans_no = 'LK' -- OR @trans_no='PA' OR @trans_no='PB' OR @trans_no='PR' OR @trans_no='PJ'
				BEGIN
					--补插入机构价格表，这样补插会造成在分部插入其他机构的机构价格表记录，应该区分总分部
					IF (SELECT sys_var_value FROM t_sys_system WHERE LOWER(sys_var_id) = 'is_distributed' ) = '1' --总部            
						INSERT INTO t_pc_branch_price(	branch_no,			item_no,			supcust_no,			sale_way,			price,
														base_price,			sale_price,		vip_price,			sup_ly_rate,		branch_price,
														branch_sale,		item_stock)
											SELECT	b.branch_no,		a.item_no,		a.main_supcust,	a.item_sup_flag,	a.price,
														a.base_price,		a.sale_price,	a.vip_price,		a.sup_ly_rate,		a.branch_price,
														'1',					a.item_stock
											FROM		t_bd_item_info a,  t_bd_branch_info b 
											WHERE		b.property in ('0','1') AND a.item_no = @item_no
											AND		NOT exists (SELECT	1 
																		FROM		t_pc_branch_price c 
																		WHERE		c.branch_no = b.branch_no AND c.item_no = a.item_no AND c.item_no = @item_no)
					ELSE --分部
						INSERT INTO t_pc_branch_price(	branch_no,			item_no,			supcust_no,				sale_way,			price,
														base_price,			sale_price,		vip_price,				sup_ly_rate,		branch_price,
														branch_sale,		item_stock)
											SELECT	b.branch_no,		a.item_no,		a.main_supcust,		a.item_sup_flag,	a.price,
														a.base_price,		a.sale_price,	a.vip_price,			a.sup_ly_rate,		a.branch_price,
														'1',					a.item_stock
											FROM		t_bd_item_info a,  t_bd_branch_info b 
											WHERE		b.property ='1' AND a.item_no = @item_no
											AND		NOT exists (SELECT	1 
																		FROM		t_pc_branch_price c 
																		WHERE		c.branch_no = b.branch_no AND c.item_no = a.item_no AND c.item_no = @item_no)			
					SELECT @ln_cnt = @ln_cnt+1  
				END 
	
			IF @@error != 0 
				BEGIN
					CLOSE priceflow_cur
					DEALLOCATE  priceflow_cur
					RETURN
				END

			FETCH priceflow_cur INTO	@trans_no,		@branch_no,		@sheet_no,		@supcust_no,		@sale_way,
												@oper_id,		@branchlist,	@item_no,		@price_type,		@start_date,
												@end_date,		@week,			@begin_time,	@end_time,			@old_price, 
												@new_price,		@discount,		@buy_qty,		@other1,				@other2,
												@other3,			@mother1,		@mother2,		@limited_qty,		@stock_qty,
												@dept_id,		@work_date,     @ls_cls_brand_list,	@new_price1,@in_price		
		END
	CLOSE priceflow_cur
	DEALLOCATE  priceflow_cur 
	IF @ln_cnt>0 
		EXEC pr_pc_price_flow_effect
END
go