Skip to content

Commit 8834bb2

Browse files
committed
[ADD] membership_sale_prorate
Added comments
1 parent 299c370 commit 8834bb2

File tree

12 files changed

+665
-0
lines changed

12 files changed

+665
-0
lines changed

membership_sale_prorate/README.rst

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
=======================
2+
Membership Sale Prorate
3+
=======================
4+
5+
..
6+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7+
!! This file is generated by oca-gen-addon-readme !!
8+
!! changes will be overwritten. !!
9+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10+
!! source digest: sha256:481e8310c0b91adbd533f0662f28937fa890d6ca476e734e32059244e832f3fb
11+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
12+
13+
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
14+
:target: https://odoo-community.org/page/development-status
15+
:alt: Beta
16+
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
17+
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
18+
:alt: License: AGPL-3
19+
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fvertical--association-lightgray.png?logo=github
20+
:target: https://github.com/OCA/vertical-association/tree/16.0/membership_sale_prorate
21+
:alt: OCA/vertical-association
22+
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
23+
:target: https://translation.odoo-community.org/projects/vertical-association-16-0/vertical-association-16-0-membership_sale_prorate
24+
:alt: Translate me on Weblate
25+
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
26+
:target: https://runboat.odoo-community.org/builds?repo=OCA/vertical-association&target_branch=16.0
27+
:alt: Try me on Runboat
28+
29+
|badge1| |badge2| |badge3| |badge4| |badge5|
30+
31+
This module calculates and sets the quantity for sale order lines having prorate membership products so that the sale order line price is calculated accordingly
32+
33+
**Table of contents**
34+
35+
.. contents::
36+
:local:
37+
38+
Bug Tracker
39+
===========
40+
41+
Bugs are tracked on `GitHub Issues <https://github.com/OCA/vertical-association/issues>`_.
42+
In case of trouble, please check there if your issue has already been reported.
43+
If you spotted it first, help us to smash it by providing a detailed and welcomed
44+
`feedback <https://github.com/OCA/vertical-association/issues/new?body=module:%20membership_sale_prorate%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
45+
46+
Do not contact contributors directly about support or help with technical issues.
47+
48+
Credits
49+
=======
50+
51+
Authors
52+
~~~~~~~
53+
54+
* Onestein
55+
56+
Contributors
57+
~~~~~~~~~~~~
58+
59+
* `Onestein <http://www.onestein.eu>`_
60+
61+
Maintainers
62+
~~~~~~~~~~~
63+
64+
This module is maintained by the OCA.
65+
66+
.. image:: https://odoo-community.org/logo.png
67+
:alt: Odoo Community Association
68+
:target: https://odoo-community.org
69+
70+
OCA, or the Odoo Community Association, is a nonprofit organization whose
71+
mission is to support the collaborative development of Odoo features and
72+
promote its widespread use.
73+
74+
This module is part of the `OCA/vertical-association <https://github.com/OCA/vertical-association/tree/16.0/membership_sale_prorate>`_ project on GitHub.
75+
76+
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

membership_sale_prorate/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import models
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2024 Onestein (<http://www.onestein.eu>)
2+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
3+
{
4+
"name": "Membership Sale Prorate",
5+
"summary": "Calculates and sets the quantity for sale order lines having "
6+
"prorate membership products so that the sale order line price "
7+
"is calculated accordingly",
8+
"version": "16.0.1.0.0",
9+
"license": "AGPL-3",
10+
"category": "Association",
11+
"author": "Onestein, Odoo Community Association (OCA)",
12+
"website": "https://github.com/OCA/vertical-association",
13+
"depends": [
14+
"membership",
15+
"membership_prorate",
16+
"sale",
17+
],
18+
"data": [],
19+
"auto_install": True,
20+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import sale_order_line
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright 2024 Onestein (<http://www.onestein.eu>)
2+
from datetime import timedelta
3+
4+
from odoo import api, fields, models
5+
from odoo.tests import Form
6+
7+
8+
class SaleOrderLine(models.Model):
9+
_inherit = "sale.order.line"
10+
11+
def _prepare_sale_line_prorate_vals(self, sale_line):
12+
product = sale_line.product_id
13+
date_order = (
14+
sale_line.order_id.date_order
15+
and sale_line.order_id.date_order.date()
16+
or fields.Date.today()
17+
)
18+
date_from, date_to = self.env["account.move.line"]._get_membership_interval(
19+
product, date_order
20+
)
21+
if not date_from:
22+
return {"quantity": 1.0}
23+
if date_order < date_from:
24+
date_order = date_from
25+
if date_order > date_to:
26+
date_order = date_to
27+
theoretical_duration = date_to - date_from + timedelta(1)
28+
real_duration = date_to - date_order + timedelta(1)
29+
if theoretical_duration != real_duration:
30+
return {
31+
"quantity": round(
32+
float(real_duration.days) / theoretical_duration.days, 2
33+
),
34+
}
35+
36+
@api.model_create_multi
37+
def create(self, vals_list):
38+
sale_lines = super().create(vals_list)
39+
for sale_line in sale_lines.filtered(
40+
lambda r: r.product_id.membership and r.product_id.membership_prorate
41+
):
42+
# Change quantity accordingly the prorate
43+
sale_line_vals = self._prepare_sale_line_prorate_vals(sale_line)
44+
if sale_line_vals:
45+
quantity = sale_line_vals["quantity"]
46+
order = sale_line.order_id
47+
with Form(order) as sale_form:
48+
index = next(
49+
(
50+
index
51+
for (index, d) in enumerate(sale_form.order_line._records)
52+
if d["id"] == sale_line.id
53+
),
54+
None,
55+
)
56+
if index is not None:
57+
with sale_form.order_line.edit(index) as line_form:
58+
line_form.product_uom_qty = quantity
59+
return sale_lines
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* `Onestein <http://www.onestein.eu>`_
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This module calculates and sets the quantity for sale order lines having prorate membership products so that the sale order line price is calculated accordingly

0 commit comments

Comments
 (0)