Permalink
Browse files

WHISTLE-1217: support billing_id

  • Loading branch information...
1 parent c742702 commit ca2977b291a1aff5ec9d417ed31d01723ab6b6bb @k-anderson k-anderson committed Aug 16, 2012
@@ -59,48 +59,44 @@ create_items(Category, Item, ServiceItems, ServicePlan, Services) ->
Min;
_ -> ItemQuantity
end,
- case Quantity > 0 of
- false -> ServiceItems;
- true ->
- Rate = get_rate(Quantity, ItemPlan),
- %% allow service plans to re-map item names (IE: softphone items "as" sip_device)
- As = wh_json:get_ne_value(<<"as">>, ItemPlan, Item),
- Routines = [fun(I) -> wh_service_item:set_category(Category, I) end
- ,fun(I) -> wh_service_item:set_item(As, I) end
- ,fun(I) -> wh_service_item:set_quantity(Quantity, I) end
- ,fun(I) -> wh_service_item:set_rate(Rate, I) end
- ,fun(I) ->
- case wh_json:get_value([<<"discounts">>, <<"single">>], ItemPlan) of
- undefined -> I;
- SingleDiscount ->
- SingleRate = wh_json:get_float_value(<<"rate">>, SingleDiscount, Rate),
- wh_service_item:set_single_discount_rate(SingleRate, I)
- end
+ Rate = get_rate(Quantity, ItemPlan),
+ %% allow service plans to re-map item names (IE: softphone items "as" sip_device)
+ As = wh_json:get_ne_value(<<"as">>, ItemPlan, Item),
+ Routines = [fun(I) -> wh_service_item:set_category(Category, I) end
+ ,fun(I) -> wh_service_item:set_item(As, I) end
+ ,fun(I) -> wh_service_item:set_quantity(Quantity, I) end
+ ,fun(I) -> wh_service_item:set_rate(Rate, I) end
+ ,fun(I) ->
+ case wh_json:get_value([<<"discounts">>, <<"single">>], ItemPlan) of
+ undefined -> I;
+ SingleDiscount ->
+ SingleRate = wh_json:get_float_value(<<"rate">>, SingleDiscount, Rate),
+ wh_service_item:set_single_discount_rate(SingleRate, I)
end
- ,fun(I) ->
- case wh_json:get_value([<<"discounts">>, <<"cumulative">>], ItemPlan) of
- undefined -> I;
- CumulativeDiscount ->
- CumulativeQuantity = case wh_json:get_integer_value(<<"maximum">>, CumulativeDiscount, 0) of
- Max when Max < Quantity ->
- lager:debug("item '~s/~s' quantity ~p exceeds cumulative discount max, using ~p"
- ,[Category, As, Quantity, Max]),
- Max;
- _ -> Quantity
- end,
- CumulativeRate = case get_rate(Quantity, CumulativeDiscount) of
- undefined -> Rate;
- Else -> Else
- end,
- I2 = wh_service_item:set_cumulative_discount(CumulativeQuantity, I),
- wh_service_item:set_cumulative_discount_rate(CumulativeRate, I2)
- end
+ end
+ ,fun(I) ->
+ case wh_json:get_value([<<"discounts">>, <<"cumulative">>], ItemPlan) of
+ undefined -> I;
+ CumulativeDiscount ->
+ CumulativeQuantity = case wh_json:get_integer_value(<<"maximum">>, CumulativeDiscount, 0) of
+ Max when Max < Quantity ->
+ lager:debug("item '~s/~s' quantity ~p exceeds cumulative discount max, using ~p"
+ ,[Category, As, Quantity, Max]),
+ Max;
+ _ -> Quantity
+ end,
+ CumulativeRate = case get_rate(Quantity, CumulativeDiscount) of
+ undefined -> Rate;
+ Else -> Else
+ end,
+ I2 = wh_service_item:set_cumulative_discount(CumulativeQuantity, I),
+ wh_service_item:set_cumulative_discount_rate(CumulativeRate, I2)
end
- ,fun(I) -> wh_service_item:set_bookkeepers(bookkeeper_jobj(Category, As, ServicePlan), I) end
- ],
- ServiceItem = lists:foldl(fun(F, I) -> F(I) end, wh_service_items:find(Category, As, ServiceItems), Routines),
- wh_service_items:update(ServiceItem, ServiceItems)
- end.
+ end
+ ,fun(I) -> wh_service_item:set_bookkeepers(bookkeeper_jobj(Category, As, ServicePlan), I) end
+ ],
+ ServiceItem = lists:foldl(fun(F, I) -> F(I) end, wh_service_items:find(Category, As, ServiceItems), Routines),
+ wh_service_items:update(ServiceItem, ServiceItems).
%%--------------------------------------------------------------------
%% @private
@@ -10,7 +10,8 @@
-behaviour(gen_server).
-export([start_link/0]).
--export([run/0]).
+-export([sync/1]).
+-export([clean/1]).
-export([init/1
,handle_call/3
,handle_cast/2
@@ -37,8 +38,16 @@
start_link() ->
gen_server:start_link(?MODULE, [], []).
-run() ->
- maybe_sync_service().
+sync(Account) ->
+ immediate_sync(Account).
+
+clean(Account) ->
+ AccountId = wh_util:format_account_id(Account, raw),
+ case couch_mgr:open_doc(?WH_SERVICES_DB, AccountId) of
+ {error, _}=E -> E;
+ {ok, ServiceJObj} ->
+ immediate_sync(AccountId, wh_json:set_value(<<"pvt_deleted">>, true, ServiceJObj))
+ end.
%%%===================================================================
%%% gen_server callbacks
@@ -277,3 +286,27 @@ maybe_update_billing_id(BillingId, AccountId, ServiceJObj) ->
couch_mgr:save_doc(?WH_SERVICES_DB, wh_json:set_value(<<"billing_id">>, AccountId, ServiceJObj))
end
end.
+
+-spec immediate_sync/1 :: (ne_binary()) -> wh_std_return().
+-spec immediate_sync/2 :: (ne_binary(), wh_json:json_object()) -> wh_std_return().
+
+immediate_sync(Account) ->
+ AccountId = wh_util:format_account_id(Account, raw),
+ case couch_mgr:open_doc(?WH_SERVICES_DB, AccountId) of
+ {error, _}=E -> E;
+ {ok, ServiceJObj} ->
+ immediate_sync(AccountId, ServiceJObj)
+ end.
+
+immediate_sync(AccountId, ServiceJObj) ->
+ case wh_service_plans:create_items(ServiceJObj) of
+ {error, no_plans}=E -> E;
+ {ok, ServiceItems} ->
+ %% TODO: support other bookkeepers...
+ try wh_bookkeeper_braintree:sync(ServiceItems, AccountId) of
+ _ -> {ok, ServiceJObj}
+ catch
+ throw:{_, R} -> {error, R};
+ _E:R -> {error, R}
+ end
+ end.
Oops, something went wrong.

0 comments on commit ca2977b

Please sign in to comment.