Permalink
Browse files

WHISTLE-1217: WIP

  • Loading branch information...
1 parent 32bd35e commit f273f0808448e3f68ab04b5910add764ae484e53 @k-anderson k-anderson committed Aug 2, 2012
@@ -152,13 +152,13 @@ save(#number{}=Number) ->
Routines = [fun(#number{}=N) -> N#number{number_doc=record_to_json(N)} end
,fun(#number{}=N) -> exec_providers(N, save) end
,fun(#number{}=N) -> get_updated_phone_number_docs(N) end
- ,fun(#number{}=N) -> update_service_plans(N) end
,fun({_, #number{}}=E) -> E;
(#number{}=N) -> save_number_doc(N)
end
,fun({_, #number{}}=E) -> E;
(#number{}=N) -> save_phone_number_docs(N)
end
+ ,fun(#number{}=N) -> update_service_plans(N) end
],
lists:foldl(fun(F, J) -> F(J) end, Number, Routines).
@@ -206,13 +206,13 @@ delete(Number) ->
Routines = [fun(#number{}=N) -> N#number{number_doc=record_to_json(N)} end
,fun(#number{}=N) -> exec_providers(N, delete) end
,fun(#number{}=N) -> get_updated_phone_number_docs(N) end
- ,fun(#number{}=N) -> update_service_plans(N) end
,fun({_, #number{}}=E) -> E;
(#number{}=N) -> delete_number_doc(N)
end
,fun({_, #number{}}=E) -> E;
(#number{}=N) -> save_phone_number_docs(N)
end
+ ,fun(#number{}=N) -> update_service_plans(N) end
],
lists:foldl(fun(F, J) -> F(J) end, Number, Routines).
@@ -676,7 +676,7 @@ exec_providers(Number, Action) ->
exec_providers([], _, Number) -> Number;
exec_providers([Provider|Providers], Action, Number) ->
- case wnm_util:try_load_module(<<"wnm_", Provider/binary>>) of
+ case wh_util:try_load_module(<<"wnm_", Provider/binary>>) of
false ->
lager:debug("provider ~s is unknown, skipping", [Provider]),
exec_providers(Providers, Action, Number);
@@ -886,58 +886,17 @@ load_phone_number_doc(Account) ->
{error, _}=E -> E
end.
-
%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec update_service_plans/1 :: (wnm_number()) -> wnm_number().
-update_service_plans(#number{phone_number_docs=PhoneNumbers, assigned_to=undefined
- ,prev_assigned_to=PrevAssignedTo}=N) ->
- case dict:find(PrevAssignedTo, PhoneNumbers) of
- error -> N;
- {ok, PhoneNumber} ->
- update_service_plan(PhoneNumber, N)
- end;
-update_service_plans(#number{phone_number_docs=PhoneNumbers, assigned_to=AssignedTo}=N) ->
- case dict:find(AssignedTo, PhoneNumbers) of
- error -> N;
- {ok, PhoneNumber} ->
- update_service_plan(PhoneNumber, N)
- end.
-
-%%--------------------------------------------------------------------
-%% @private
-%% @doc
-%%
-%% @end
-%%--------------------------------------------------------------------
--spec update_service_plan/2 :: (wh_json:json_object(), wnm_number()) -> wnm_number().
-update_service_plan(PhoneNumber, #number{resellers=undefined, assigned_to=undefined
- ,prev_assigned_to=Account}=N) ->
- case wh_resellers:fetch(Account) of
- {error, no_service_plan} -> N;
- {ok, Resellers} ->
- update_service_plan(PhoneNumber, N#number{resellers=Resellers})
- end;
-update_service_plan(PhoneNumber, #number{resellers=undefined, assigned_to=Account}=N) ->
- case wh_resellers:fetch(Account) of
- {error, no_service_plan} -> N;
- {ok, Resellers} ->
- update_service_plan(PhoneNumber, N#number{resellers=Resellers})
- end;
-update_service_plan(PhoneNumber, #number{resellers=Resellers}=N) ->
- Routines = [fun(R) -> wh_service_numbers:update(PhoneNumber, R) end
- ,fun(R) -> wh_resellers:commit_changes(R) end
- ],
- try lists:foldl(fun(F, R) -> F(R) end, Resellers, Routines) of
- ok -> N
- catch
- throw:{_, Reason} ->
- error_service_restriction(Reason, N)
- end.
+update_service_plans(#number{assigned_to=AssignedTo, prev_assigned_to=PrevAssignedTo}=N) ->
+ _ = wh_services:reconcile(AssignedTo, <<"phone_numbers">>),
+ _ = wh_services:reconcile(PrevAssignedTo, <<"phone_numbers">>),
+ N.
%%--------------------------------------------------------------------
%% @private
@@ -946,24 +905,10 @@ update_service_plan(PhoneNumber, #number{resellers=Resellers}=N) ->
%% @end
%%--------------------------------------------------------------------
-spec activate_feature/2 :: (ne_binary(), wnm_number()) -> wnm_number().
-activate_feature(Feature, #number{resellers=undefined, assigned_to=Account, activations=Activations
- ,features=Features}=N) ->
- case wh_resellers:fetch(Account) of
- {error, no_service_plan} -> N#number{activations=sets:add_element({features, Feature}, Activations)
- ,features=sets:add_element(Feature, Features)};
- {ok, Resellers} ->
- activate_feature(Feature, N#number{resellers=Resellers})
- end;
-activate_feature(Feature, #number{resellers=Resellers, activations=Activations
+activate_feature(Feature, #number{resellers=undefined, activations=Activations
,features=Features}=N) ->
- try wh_service_numbers:activate_feature(Feature, Resellers) of
- R -> N#number{activations=sets:add_element({features, Feature}, Activations)
- ,features=sets:add_element(Feature, Features)
- ,resellers=R}
- catch
- throw:{_, Reason} ->
- error_service_restriction(Reason, N)
- end.
+ N#number{activations=sets:add_element({features, Feature}, Activations)
+ ,features=sets:add_element(Feature, Features)}.
%%--------------------------------------------------------------------
%% @private
@@ -972,17 +917,5 @@ activate_feature(Feature, #number{resellers=Resellers, activations=Activations
%% @end
%%--------------------------------------------------------------------
-spec activate_phone_number/1 :: (wnm_number()) -> wnm_number().
-activate_phone_number(#number{number=Num, resellers=undefined, assigned_to=Account, activations=Activations}=N) ->
- case wh_resellers:fetch(Account) of
- {error, no_service_plan} -> N#number{activations=sets:add_element({phone_number, Num}, Activations)};
- {ok, Resellers} ->
- activate_phone_number(N#number{resellers=Resellers})
- end;
-activate_phone_number(#number{number=Num, resellers=Resellers, activations=Activations}=N) ->
- try wh_service_numbers:activate_phone_number(Num, Resellers) of
- R -> N#number{activations=sets:add_element({phone_number, Num}, Activations)
- ,resellers=R}
- catch
- throw:{_, Reason} ->
- error_service_restriction(Reason, N)
- end.
+activate_phone_number(#number{number=Num, resellers=undefined, activations=Activations}=N) ->
+ N#number{activations=sets:add_element({phone_number, Num}, Activations)}.
@@ -0,0 +1,10 @@
+{
+ "_id": "_design/services",
+ "language": "javascript",
+ "views": {
+ "cascade_quantities": {
+ "map": "function (doc) {if (doc.pvt_type != 'service' || doc.pvt_deleted || !doc.quantities) return;for (var key in doc.quantities) {var obj = doc.quantities[key];for (var prop in obj) {if (obj[prop] < 1) continue;for (var idx in doc.pvt_tree) {emit([doc.pvt_tree[idx] || doc._id, key, prop], obj[prop]);}}}}",
+ "reduce": "_sum"
+ }
+ }
+}
@@ -0,0 +1,21 @@
+%%%-------------------------------------------------------------------
+%%% @copyright (C) 2012, VoIP, INC
+%%% @doc
+%%%
+%%% @end
+%%% @contributors
+%%%-------------------------------------------------------------------
+-module(wh_bookkeeper_braintree).
+
+
+item_quantity(Category, Item, Quantity, Bookkeeper) ->
+ ok.
+
+item_price(Category, Item, Quantity, Bookkeeper) ->
+ ok.
+
+discount_quantity(Category, Item, Quantity, Bookkeeper) ->
+ ok.
+
+discount_price(Category, Item, Quantity, Bookkeeper) ->
+ ok.
@@ -11,6 +11,12 @@
-include_lib("whistle_services/src/whistle_services.hrl").
+%%--------------------------------------------------------------------
+%% @public
+%% @doc
+%%
+%% @end
+%%--------------------------------------------------------------------
-spec reconcile/1 :: (wh_services:services()) -> wh_services:services().
reconcile(Services) ->
AccountId = wh_services:account_id(Services),
@@ -11,6 +11,12 @@
-include_lib("whistle_services/src/whistle_services.hrl").
+%%--------------------------------------------------------------------
+%% @public
+%% @doc
+%%
+%% @end
+%%--------------------------------------------------------------------
-spec reconcile/1 :: (wh_services:services()) -> wh_services:services().
reconcile(Services) ->
AccountId = wh_services:account_id(Services),
@@ -11,6 +11,12 @@
-include_lib("whistle_services/src/whistle_services.hrl").
+%%--------------------------------------------------------------------
+%% @public
+%% @doc
+%%
+%% @end
+%%--------------------------------------------------------------------
-spec reconcile/1 :: (wh_services:services()) -> wh_services:services().
reconcile(Services) ->
AccountId = wh_services:account_id(Services),
@@ -25,6 +31,12 @@ reconcile(Services) ->
update_numbers(wh_json:get_keys(wh_json:public_fields(JObj)), JObj, S2)
end.
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%%
+%% @end
+%%--------------------------------------------------------------------
-spec update_numbers/3 :: ([ne_binary(),...] | [], wh_json:json_object(), wh_services:services()) -> wh_services:services().
update_numbers([], _, Services) ->
Services;
@@ -42,16 +54,28 @@ update_numbers([Number|Numbers], JObj, Services) ->
update_numbers(Numbers, JObj, UpdatedServices)
end.
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%%
+%% @end
+%%--------------------------------------------------------------------
-spec update_number_quantities/2 :: (ne_binary(), wh_services:services()) -> wh_services:services().
update_number_quantities(Number, Services) ->
Classification = wnm_util:classify_number(Number),
- Quantity = wh_services:get_quantity(<<"phone_numbers">>, Classification, Services),
+ Quantity = wh_services:update_quantity(<<"phone_numbers">>, Classification, Services),
wh_services:update(<<"phone_numbers">>, Classification, Quantity + 1, Services).
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%%
+%% @end
+%%--------------------------------------------------------------------
-spec update_feature_quantities/2 :: ([ne_binary(),...] | [], wh_services:services()) -> wh_services:services().
update_feature_quantities([], Services) ->
Services;
update_feature_quantities([Feature|Features], Services) ->
- Quantity = wh_services:get_quantity(<<"number_features">>, Feature, Services),
+ Quantity = wh_services:update_quantity(<<"number_features">>, Feature, Services),
UpdatedServices = wh_services:update(<<"number_features">>, Feature, Quantity + 1, Services),
update_feature_quantities(Features, UpdatedServices).
@@ -0,0 +1,83 @@
+%%%-------------------------------------------------------------------
+%%% @copyright (C) 2012, VoIP, INC
+%%% @doc
+%%%
+%%% @end
+%%% @contributors
+%%%-------------------------------------------------------------------
+-module(wh_service_invoice).
+
+-include_lib("whistle_services/src/whistle_services.hrl").
+
+-export([sync/1]).
+
+-record(wh_invoice, {account_id = undefined
+ ,account_db = undefined
+ ,billing_id = undefined
+ ,items = wh_service_items:empty() :: wh_service_items:items()
+ ,service_plans = wh_service_plans:empty() :: wh_service_plans:plans()
+ ,services = wh_services:empty()
+ }).
+
+%%--------------------------------------------------------------------
+%% @public
+%% @doc
+%%
+%% @end
+%%--------------------------------------------------------------------
+-spec sync/1 :: (ne_binary()) -> #wh_invoice{}.
+sync(Account) ->
+ Invoice = initialize(Account),
+ create_items(Invoice).
+
+%%--------------------------------------------------------------------
+%% @public
+%% @doc
+%%
+%% @end
+%%--------------------------------------------------------------------
+-spec initialize/1 :: (ne_binary()) -> #wh_invoice{}.
+initialize(Account) ->
+ Routines = [fun(I) -> I#wh_invoice{account_id=wh_util:format_account_id(Account, raw)
+ ,account_db=wh_util:format_account_id(Account, encoded)}
+ end
+ ,fun(#wh_invoice{account_id=AccountId}=I) ->
+ I#wh_invoice{services=wh_services:fetch(AccountId)}
+ end
+ ,fun(#wh_invoice{account_db=AccountDb, account_id=AccountId}=I) ->
+ case couch_mgr:open_doc(AccountDb, AccountId) of
+ {ok, JObj} ->
+ BillingId = wh_json:get_ne_value(<<"billing_id">>, JObj, AccountId),
+ I#wh_invoice{billing_id=BillingId};
+ {error, _R} ->
+ error_database(AccountId)
+ end
+ end
+ ,fun(#wh_invoice{account_id=AccountId}=I) ->
+ I#wh_invoice{service_plans=wh_service_plans:fetch(AccountId)}
+ end
+
+ ],
+ lists:foldl(fun(F, I) -> F(I) end, #wh_invoice{}, Routines).
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%%
+%% @end
+%%--------------------------------------------------------------------
+-spec create_items/1 :: (#wh_invoice{}) -> #wh_invoice{}.
+create_items(#wh_invoice{service_plans=ServicePlans, services=Services}=Invoice) ->
+ Invoice#wh_invoice{items=wh_service_plans:create_items(Services, ServicePlans)}.
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%%
+%% @end
+%%--------------------------------------------------------------------
+-spec error_database/1 :: (atom()) -> no_return().
+error_database(Reason) ->
+ Error = <<"Unable to open document ", (wh_util:to_binary(Reason))/binary>>,
+ lager:debug("~s", [Error]),
+ throw({database_error, Error}).
@@ -0,0 +1,10 @@
+%%%-------------------------------------------------------------------
+%%% @copyright (C) 2012, VoIP, INC
+%%% @doc
+%%%
+%%% @end
+%%% @contributors
+%%%-------------------------------------------------------------------
+-module(wh_service_invoices).
+
+-include_lib("whistle_services/src/whistle_services.hrl").
Oops, something went wrong.

0 comments on commit f273f08

Please sign in to comment.