Skip to content

Commit

Permalink
Simple implementation of subscription trial period
Browse files Browse the repository at this point in the history
  • Loading branch information
tarteo committed Mar 26, 2024
1 parent 6202d92 commit bac5452
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
13 changes: 9 additions & 4 deletions argocd_sale/models/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ class Subscription(models.Model):

def generate_invoice(self):
res = super().generate_invoice()
if self.sale_subscription_line_ids.filtered(
lambda l: l.product_id.application_template_id
if (
self.sale_subscription_line_ids.filtered(
lambda l: l.product_id.application_template_id
)
and len(self.invoice_ids) == 1
):
# Set price to 1 for first invoice
# Set price of the first invoice to 1.00
free_period = self._get_free_period()
if free_period:
self.invoice_ids.invoice_line_ids.filtered(
Expand Down Expand Up @@ -139,6 +142,8 @@ def _get_free_period(self):
if self.partner_id.parent_id:
existing_subs += self.partner_id.subscription_ids
existing_subs -= self
if existing_subs:
return None
free_period = int(
self.env["ir.config_parameter"]
.sudo()
Expand All @@ -147,7 +152,7 @@ def _get_free_period(self):
free_period_type = (
self.env["ir.config_parameter"]
.sudo()
.get_param("argocd_website.subscription_free_period_type")
.get_param("argocd_sale.subscription_free_period_type")
)
if not free_period:
return None
Expand Down
14 changes: 14 additions & 0 deletions argocd_sale/tests/test_free_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def test_next_payment_date(self):
sub_tmpl = self.env.ref("argocd_sale.demo_subscription_template")
sub_product_tmpl = self.env.ref("argocd_sale.demo_curq_basis_product_template")
sub_product = sub_product_tmpl.product_variant_ids[0]
sub_product.list_price = 30.0 # Make sure it's not 1.0
partner_id = self.env.ref("base.partner_admin")

self.env["ir.config_parameter"].set_param(
Expand All @@ -31,3 +32,16 @@ def test_next_payment_date(self):
sub.generate_invoice()
sub._invoice_paid_hook()
self.assertEqual(sub.recurring_next_date, expected_next_payment_date)
self.assertEqual(
sub.invoice_ids.amount_untaxed,
1.0,
"Invoice amount of first invoice must be 1.0",
)

sub.generate_invoice()
sub._invoice_paid_hook()
self.assertEqual(
sub.invoice_ids[0].amount_untaxed,
30.0,
"Next invoice should be the normal price",
)

0 comments on commit bac5452

Please sign in to comment.