anchor.diff:  	end.
   %% @hidden
   authorize1(radius, ServiceType,
  -		#service{attributes = Attributes,
  -		product = #product_instance{product = OfferId,
  -		characteristics = Chars}} = Subscriber, Timestamp,
  -		Address, Direction, SessionAttributes) ->
  -	F = fun({'Session-Id', _}) ->
  -			true;
  -		({?AcctSessionId, _}) ->
  -			true;
  -		(_) ->
  -			false
  -	end,
  -	case lists:any(F, get_session_id(SessionAttributes)) of
  -		true ->
  -			case lists:keyfind("radiusReserveSessionTime", 1, Chars) of
  -				{_, RRST} when is_integer(RRST) ->
  -					case mnesia:read(offer, OfferId, read) of
  -						[#offer{} = P] ->
  -							authorize2(radius, ServiceType, Subscriber, P,
  -									Timestamp, Address, Direction, SessionAttributes, RRST);
  -						[] ->
  -							throw(offer_not_found)
  +		#service{attributes = Attributes, product = ProdRef} =
  +		Service, Timestamp, Address, Direction, SessionAttributes) ->
  +	case mnesia:read(product, ProdRef, read) of
  +		[#product{characteristics = Chars,
  +				product = OfferId, balance = BucketRefs}] ->
  +			Buckets = lists:flatten([mnesia:select(bucket,
  +					[{'$1', [{'==', Id, {element, #bucket.id, '$1'}}], ['$1']}])
  +					|| Id <- BucketRefs]),
  +			F = fun({'Session-Id', _}) ->
  +					true;
  +				({?AcctSessionId, _}) ->
  +					true;
  +				(_) ->
  +					false
  +			end,
  +			case lists:any(F, get_session_id(SessionAttributes)) of
  +				true ->
  +					case lists:keyfind("radiusReserveSessionTime", 1, Chars) of
  +						{_, RRST} when is_integer(RRST) ->
  +							case mnesia:read(offer, OfferId, read) of
  +								[#offer{} = Offer] ->
  +									authorize2(radius, ServiceType, Service, Buckets, Offer,
  +											Timestamp, Address, Direction, SessionAttributes, RRST);
  +								[] ->
  +									throw(offer_not_found)
  +							end;
  +						false ->
  +							authorize5(Service, Buckets, ServiceType, SessionAttributes, Attributes)
   					end;
   				false ->
  -					authorize5(Subscriber, ServiceType, SessionAttributes, Attributes)
  +					authorize5(Service, Buckets, ServiceType, SessionAttributes, Attributes)
   			end;
  -		false ->
  -			authorize5(Subscriber, ServiceType, SessionAttributes, Attributes)
  +		[] ->
  +			throw(product_not_found)
   	end;
   authorize1(diameter, ServiceType,
  -		#service{attributes = Attributes} = Subscriber,
  -		_Timestamp, _Address, _Direction, SessionAttributes) ->
  -	authorize5(Subscriber, ServiceType, SessionAttributes, Attributes).
  +		#service{attributes = Attributes, product = ProdRef} =
  +		Service, _Timestamp, _Address, _Direction, SessionAttributes) ->
  +	case mnesia:read(product, ProdRef, read) of
  +		[#product{balance = BucketRefs}] ->
  +			Buckets = lists:flatten([mnesia:select(bucket,
  +					[{'$1', [{'==', Id, {element, #bucket.id, '$1'}}], ['$1']}])
  +					|| Id <- BucketRefs]),
  +			authorize5(Service, Buckets, ServiceType, SessionAttributes, Attributes);
  +		[] ->
  +			throw(product_not_found)
  +	end.
   %% @hidden
   authorize2(radius = Protocol, ServiceType,
  -		#service{attributes = Attributes} = Subscriber, Timestamp,
  -		#offer{specification = undefined, bundle = Bundle},
  -		Address, Direction, SessionAttributes, Reserve)
  -		when Reserve > 0, Bundle /= [] ->
  +		#service{attributes = Attributes} = Service, Buckets, Timestamp,
  +		#offer{specification = undefined, bundle = Bundle}, Address, Direction,
  +		SessionAttributes, Reserve) when Reserve > 0, Bundle /= [] ->
   	try
   		F = fun(#bundled_po{name = OfferId}, Acc) ->
   				case mnesia:read(offer, OfferId, read) of
  
positive.code: 				{_Description, Amount, _} ->
  					case Amount of
  						N when N >= 0 ->
  							authorize4(Protocol, ServiceType, Subscriber,
  								Price#price{amount = N}, SessionAttributes, Reserve);
  						_N ->
  							throw(negative_amount)
  
negative.code: 				{PriceReserve, _Buckets3}  ->
  					SessionTimeout = UnitsReserved + UnitReserve,
  					NewAttr = radius_attributes:store(?SessionTimeout, SessionTimeout, Attr),
  					authorize5(Service, Buckets1, ServiceType, SessionAttributes, NewAttr);
  				{0, _Buckets3}  when UnitsReserved == 0 ->
  					{unauthorized, out_of_credit, ExistingAttr};
  				{PriceReserved, _Buckets3} ->
  					SessionTimeout = UnitsReserved + ((PriceReserved div UnitPrice) * UnitSize),
  					NewAttr = radius_attributes:store(?SessionTimeout, SessionTimeout, Attr),
  					authorize5(Service, Buckets1, ServiceType, SessionAttributes, NewAttr)
  			end
  	end.
  %% @hidden
  authorize5(#service{session_attributes = ExistingAttr} = Service,
  		Buckets, ServiceType, SessionAttributes, Attributes) ->
  	F = fun(#bucket{remain_amount = R, units = U})
  				when
  				((ServiceType == undefined) orelse
  