Skip to content

Commit

Permalink
[ADD] membership_sale_prorate
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteMeAsap committed Oct 11, 2024
1 parent 299c370 commit d0f61e3
Show file tree
Hide file tree
Showing 12 changed files with 665 additions and 0 deletions.
76 changes: 76 additions & 0 deletions membership_sale_prorate/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
=======================
Membership Sale Prorate
=======================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:481e8310c0b91adbd533f0662f28937fa890d6ca476e734e32059244e832f3fb
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fvertical--association-lightgray.png?logo=github
:target: https://github.com/OCA/vertical-association/tree/16.0/membership_sale_prorate
:alt: OCA/vertical-association
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/vertical-association-16-0/vertical-association-16-0-membership_sale_prorate
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/vertical-association&target_branch=16.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

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

**Table of contents**

.. contents::
:local:

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/vertical-association/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`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**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* Onestein

Contributors
~~~~~~~~~~~~

* `Onestein <http://www.onestein.eu>`_

Maintainers
~~~~~~~~~~~

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/vertical-association <https://github.com/OCA/vertical-association/tree/16.0/membership_sale_prorate>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions membership_sale_prorate/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
20 changes: 20 additions & 0 deletions membership_sale_prorate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2024 Onestein (<http://www.onestein.eu>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
{
"name": "Membership Sale Prorate",
"summary": "Calculates and sets the quantity for sale order lines having "
"prorate membership products so that the sale order line price "
"is calculated accordingly",
"version": "16.0.1.0.0",
"license": "AGPL-3",
"category": "Association",
"author": "Onestein, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/vertical-association",
"depends": [
"membership",
"membership_prorate",
"sale",
],
"data": [],
"auto_install": True,
}
1 change: 1 addition & 0 deletions membership_sale_prorate/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import sale_order_line
59 changes: 59 additions & 0 deletions membership_sale_prorate/models/sale_order_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright 2024 Onestein (<http://www.onestein.eu>)
from datetime import timedelta

from odoo import api, fields, models
from odoo.tests import Form


class SaleOrderLine(models.Model):
_inherit = "sale.order.line"

def _prepare_sale_line_prorate_vals(self, sale_line):
product = sale_line.product_id
date_order = (
sale_line.order_id.date_order
and sale_line.order_id.date_order.date()
or fields.Date.today()
)
date_from, date_to = self.env["account.move.line"]._get_membership_interval(
product, date_order
)
if not date_from:
return {"quantity": 1.0}
if date_order < date_from:
date_order = date_from
if date_order > date_to:
date_order = date_to
theoretical_duration = date_to - date_from + timedelta(1)
real_duration = date_to - date_order + timedelta(1)
if theoretical_duration != real_duration:
return {
"quantity": round(
float(real_duration.days) / theoretical_duration.days, 2
),
}

@api.model_create_multi
def create(self, vals_list):
sale_lines = super().create(vals_list)
for sale_line in sale_lines.filtered(
lambda r: r.product_id.membership and r.product_id.membership_prorate
):
# Change quantity accordingly the prorate
sale_line_vals = self._prepare_sale_line_prorate_vals(sale_line)
if sale_line_vals:
quantity = sale_line_vals["quantity"]
order = sale_line.order_id
with Form(order) as sale_form:
index = next(
(
index
for (index, d) in enumerate(sale_form.order_line._records)
if d["id"] == sale_line.id
),
None,
)
if index is not None:
with sale_form.order_line.edit(index) as line_form:
line_form.product_uom_qty = quantity
return sale_lines
1 change: 1 addition & 0 deletions membership_sale_prorate/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* `Onestein <http://www.onestein.eu>`_
1 change: 1 addition & 0 deletions membership_sale_prorate/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +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
Loading

0 comments on commit d0f61e3

Please sign in to comment.