示例#1
0
def change_plan_status(
    request: HttpRequest,
    user: UserProfile,
    status: int = REQ("status", validator=check_int)
) -> HttpResponse:
    plan = get_current_plan_by_realm(user.realm)
    assert (plan is not None)  # for mypy
    do_change_plan_status(plan, status)
    return json_success()
示例#2
0
def change_plan_at_end_of_cycle(
    request: HttpRequest,
    user: UserProfile,
    status: int = REQ("status", validator=check_int)
) -> HttpResponse:
    assert (status
            in [CustomerPlan.ACTIVE, CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE])
    plan = get_current_plan_by_realm(user.realm)
    assert (plan is not None)  # for mypy
    do_change_plan_status(plan, status)
    return json_success()
示例#3
0
def change_plan_status(request: HttpRequest, user: UserProfile,
                       status: int=REQ("status", validator=check_int)) -> HttpResponse:
    assert(status in [CustomerPlan.ACTIVE, CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE, CustomerPlan.ENDED])

    plan = get_current_plan_by_realm(user.realm)
    assert(plan is not None)  # for mypy

    if status == CustomerPlan.ACTIVE:
        assert(plan.status == CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE)
        do_change_plan_status(plan, status)
    elif status == CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE:
        assert(plan.status == CustomerPlan.ACTIVE)
        do_change_plan_status(plan, status)
    elif status == CustomerPlan.ENDED:
        assert(plan.status == CustomerPlan.FREE_TRIAL)
        downgrade_now(user.realm)
    return json_success()
示例#4
0
def change_plan_status(request: HttpRequest, user: UserProfile,
                       status: int=REQ("status", validator=check_int)) -> HttpResponse:
    assert(status in [CustomerPlan.ACTIVE, CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE,
                      CustomerPlan.SWITCH_TO_ANNUAL_AT_END_OF_CYCLE, CustomerPlan.ENDED])

    plan = get_current_plan_by_realm(user.realm)
    assert(plan is not None)  # for mypy

    if status == CustomerPlan.ACTIVE:
        assert(plan.status == CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE)
        do_change_plan_status(plan, status)
    elif status == CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE:
        assert(plan.status == CustomerPlan.ACTIVE)
        downgrade_at_the_end_of_billing_cycle(user.realm)
    elif status == CustomerPlan.SWITCH_TO_ANNUAL_AT_END_OF_CYCLE:
        assert(plan.billing_schedule == CustomerPlan.MONTHLY)
        assert(plan.status == CustomerPlan.ACTIVE)
        assert(plan.fixed_price is None)
        do_change_plan_status(plan, status)
    elif status == CustomerPlan.ENDED:
        assert(plan.status == CustomerPlan.FREE_TRIAL)
        downgrade_now_without_creating_additional_invoices(user.realm)
    return json_success()
示例#5
0
def update_plan(
    request: HttpRequest,
    user: UserProfile,
    status: Optional[int] = REQ(
        "status",
        json_validator=check_int_in([
            CustomerPlan.ACTIVE,
            CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE,
            CustomerPlan.SWITCH_TO_ANNUAL_AT_END_OF_CYCLE,
            CustomerPlan.ENDED,
        ]),
        default=None,
    ),
    licenses: Optional[int] = REQ("licenses",
                                  json_validator=check_int,
                                  default=None),
    licenses_at_next_renewal: Optional[int] = REQ("licenses_at_next_renewal",
                                                  json_validator=check_int,
                                                  default=None),
) -> HttpResponse:
    plan = get_current_plan_by_realm(user.realm)
    assert plan is not None  # for mypy

    new_plan, last_ledger_entry = make_end_of_cycle_updates_if_needed(
        plan, timezone_now())
    if new_plan is not None:
        raise JsonableError(
            _("Unable to update the plan. The plan has been expired and replaced with a new plan."
              ))

    if last_ledger_entry is None:
        raise JsonableError(
            _("Unable to update the plan. The plan has ended."))

    if status is not None:
        if status == CustomerPlan.ACTIVE:
            assert plan.status == CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE
            do_change_plan_status(plan, status)
        elif status == CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE:
            assert plan.status == CustomerPlan.ACTIVE
            downgrade_at_the_end_of_billing_cycle(user.realm)
        elif status == CustomerPlan.SWITCH_TO_ANNUAL_AT_END_OF_CYCLE:
            assert plan.billing_schedule == CustomerPlan.MONTHLY
            assert plan.status == CustomerPlan.ACTIVE
            assert plan.fixed_price is None
            do_change_plan_status(plan, status)
        elif status == CustomerPlan.ENDED:
            assert plan.is_free_trial()
            downgrade_now_without_creating_additional_invoices(user.realm)
        return json_success()

    if licenses is not None:
        if plan.automanage_licenses:
            raise JsonableError(
                _("Unable to update licenses manually. Your plan is on automatic license management."
                  ))
        if last_ledger_entry.licenses == licenses:
            raise JsonableError(
                _("Your plan is already on {licenses} licenses in the current billing period."
                  ).format(licenses=licenses))
        if last_ledger_entry.licenses > licenses:
            raise JsonableError(
                _("You cannot decrease the licenses in the current billing period."
                  ).format(licenses=licenses))
        validate_licenses(plan.charge_automatically, licenses,
                          get_latest_seat_count(user.realm))
        update_license_ledger_for_manual_plan(plan,
                                              timezone_now(),
                                              licenses=licenses)
        return json_success()

    if licenses_at_next_renewal is not None:
        if plan.automanage_licenses:
            raise JsonableError(
                _("Unable to update licenses manually. Your plan is on automatic license management."
                  ))
        if last_ledger_entry.licenses_at_next_renewal == licenses_at_next_renewal:
            raise JsonableError(
                _("Your plan is already scheduled to renew with {licenses_at_next_renewal} licenses."
                  ).format(licenses_at_next_renewal=licenses_at_next_renewal))
        validate_licenses(
            plan.charge_automatically,
            licenses_at_next_renewal,
            get_latest_seat_count(user.realm),
        )
        update_license_ledger_for_manual_plan(
            plan,
            timezone_now(),
            licenses_at_next_renewal=licenses_at_next_renewal)
        return json_success()

    raise JsonableError(_("Nothing to change."))