anchor.diff:  %% Appliers
   -export([current_state/2
           ,periodic_fees/2
  -        ,import_periodic_fees/3
  -        ,import_accounts/3
  -        ,import_onbill_data1/3
           ,generate_docs/2
           ,sync_onbills/2
           ,is_allowed/1
  -        ,maybe_format_address_element/2
           ]).
   
   -include_lib("tasks/src/tasks.hrl").
  
positive.code:         andalso kz_account:is_reseller(AccountDoc)
          orelse kz_account:is_superduper_admin(AuthAccountDoc).
  
  create_account(ResellerId, AccountName, Realm) ->
      Tree = crossbar_util:get_tree(ResellerId) ++ [ResellerId],
      Props = [{<<"pvt_type">>, kz_account:type()}
              ,{<<"name">>, AccountName}
              ,{<<"realm">>, Realm}
              ,{<<"timezone">>,<<"Europe/Moscow">>}
              ,{<<"language">>,<<"ru-ru">>}
              ,{<<"pvt_tree">>, Tree}
              ],
      Ctx1 = cb_context:set_account_id(cb_context:new(), ResellerId),
      Ctx2 = cb_context:set_doc(Ctx1, kz_json:set_values(Props, kz_json:new())),
      cb_accounts:put(Ctx2).
  
  create_users(_AccountId, [], _Context) -> 'ok';
  create_users(AccountId, [UserName|Users], Context) -> 
      UserPassword = kz_binary:rand_hex(10),
      Props = props:filter_empty([
           {[<<"username">>], UserName}
          ,{[<<"first_name">>], <<"Firstname">>}
          ,{[<<"last_name">>], <<"Surname">>}
          ,{[<<"email">>], UserName}
          ,{[<<"password">>], UserPassword}
          ,{[<<"timezone">>],<<"Europe/Moscow">>}
          ,{[<<"priv_level">>], <<"admin">>}
          ]),
      UserData = kz_json:set_values(Props, ?MK_USER),
      Ctx1 = cb_context:set_account_id(Context, AccountId),
      Ctx2 = cb_context:set_doc(Ctx1, UserData),
      Ctx3 = cb_context:set_req_data(Ctx2, UserData),
      Ctx4 = cb_users_v1:put(Ctx3),
      send_email(Ctx4),
      timer:sleep(1000),
      create_users(AccountId, Users, Context).
  
  -spec send_email(cb_context:context()) -> 'ok'.
  send_email(Context) ->
      Doc = cb_context:doc(Context),
      ReqData = cb_context:req_data(Context),
      Req = [{<<"Account-ID">>, cb_context:account_id(Context)}
            ,{<<"User-ID">>, kz_doc:id(Doc)}
            ,{<<"Password">>, kz_json:get_value(<<"password">>, ReqData)}
             | kz_api:default_headers(?APP_NAME, ?APP_VERSION)
            ],
      kapps_notify_publisher:cast(Req, fun kapi_notifications:publish_new_user/1).
  
  -spec maybe_format_address_element(any(), any()) -> 'ok'.
  maybe_format_address_element([], Acc) ->
      Acc;
  maybe_format_address_element([<<>>], Acc) ->
      Acc;
  maybe_format_address_element([H], <<>>) ->
      H;
  maybe_format_address_element([H], Acc) ->
      <<Acc/binary, ", ", H/binary>>;
  maybe_format_address_element([<<>>|T], Acc) ->
      maybe_format_address_element(T, Acc);
  maybe_format_address_element([H|T], Acc) when Acc == <<>> ->
      maybe_format_address_element(T, <<H/binary>>);
  maybe_format_address_element([H|T], Acc) ->
      maybe_format_address_element(T, <<Acc/binary, ", ", H/binary>>).
  
  format_agrm_date(<<YYYY:4/binary, "-", MM:2/binary, "-", DD:2/binary>>) ->
      <<DD/binary, ".", MM/binary, ".", YYYY/binary>>;
  format_agrm_date(AgrmDate) ->
      AgrmDate.
  
  agrm_vals(_, _, 'undefined') ->
      [];
  agrm_vals(<<"I0#", AgrmNumber/binary>>, AgrmDate, AgrmTypeId) ->
      agrm_vals(AgrmNumber, AgrmDate, AgrmTypeId);
  agrm_vals(<<"0WG#", AgrmNumber/binary>>, AgrmDate, AgrmTypeId) ->
      agrm_vals(AgrmNumber, AgrmDate, AgrmTypeId);
  agrm_vals(AgrmNumber, AgrmDate, <<"1">>) ->
      [{[<<"agrm">>,<<"onnet">>,<<"number">>], AgrmNumber}
      ,{[<<"agrm">>,<<"onnet">>,<<"date">>], format_agrm_date(AgrmDate)}];
  agrm_vals(AgrmNumber, AgrmDate, <<"2">>) ->
      [{[<<"agrm">>,<<"beeline_spb">>,<<"number">>], <<"I0#", AgrmNumber/binary>>}
      ,{[<<"agrm">>,<<"beeline_spb">>,<<"date">>], format_agrm_date(AgrmDate)}];
  agrm_vals(AgrmNumber, AgrmDate, <<"3">>) ->
      [{[<<"agrm">>,<<"beeline_msk">>,<<"number">>], <<"0WG#", AgrmNumber/binary>>}
      ,{[<<"agrm">>,<<"beeline_msk">>,<<"date">>], format_agrm_date(AgrmDate)}].
  
  remove_periodic_fees_from_db([]) ->
      'ok';
  remove_periodic_fees_from_db([DescendantId | DescendantsIds]) ->
      Services = kz_services:delete_service_plan(<<"voip_service_plan">>, kz_services:fetch(DescendantId)),
      kz_services:save(kz_services:add_service_plan(<<"onnet_periodic_fees">>, Services)),
      DbName = kz_util:format_account_id(DescendantId, 'encoded'),
      case kz_datamgr:get_result_ids(DbName, <<"periodic_fees/crossbar_listing">>, []) of
          {'ok', Ids} ->
              kz_datamgr:del_docs(DbName, Ids);
          {'error', _R} ->
              lager:debug("unable to get periodic_fees docs of ~s: ~p", [DescendantId, _R])
      end,
      timer:sleep(100),
      remove_periodic_fees_from_db(DescendantsIds).
  
  -spec replicate_onbill_doc(ne_binary()) ->
                                            {'ok', kz_json:object()} |
                                            {'error', any()}.
  
negative.code:     ,{<<"doc">>, <<"Just an experimentsl feature.\n"
                     "No additional parametres needed.\n"
                   >>}
      ];
  
  action(<<"generate_docs">>) ->
  