Skip to content

Commit

Permalink
[17.0][MIG] sale_global_discount: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
miguel-S73 authored and sbiosca-s73 committed Dec 30, 2024
1 parent 0b24567 commit d0e60bd
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 32 deletions.
4 changes: 4 additions & 0 deletions sale_global_discount/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ Contributors

- Omar Castiñeira <[email protected]>

- `Studio73 <https://www.studio73.es>`__

- Miguel Gandia

Maintainers
-----------

Expand Down
2 changes: 1 addition & 1 deletion sale_global_discount/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
{
"name": "Sale Global Discount",
"version": "16.0.1.0.0",
"version": "17.0.1.0.0",
"category": "Sales Management",
"author": "Tecnativa," "Odoo Community Association (OCA)",
"website": "https://github.com/OCA/sale-workflow",
Expand Down
22 changes: 12 additions & 10 deletions sale_global_discount/hooks.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,41 @@
from odoo.tools.sql import column_exists


def _pre_init_global_discount_fields(cr):
if not column_exists(cr, "sale_order", "amount_global_discount"):
cr.execute(
def _pre_init_global_discount_fields(env):
if not column_exists(env.cr, "sale_order", "amount_global_discount"):
env.cr.execute(
"""
ALTER TABLE "sale_order"
ADD COLUMN "amount_global_discount" double precision DEFAULT 0
"""
)
cr.execute(
env.cr.execute(
"""
ALTER TABLE "sale_order" ALTER COLUMN "amount_global_discount" DROP DEFAULT
"""
)
if not column_exists(cr, "sale_order", "amount_untaxed_before_global_discounts"):
cr.execute(
if not column_exists(
env.cr, "sale_order", "amount_untaxed_before_global_discounts"
):
env.cr.execute(
"""
ALTER TABLE "sale_order"
ADD COLUMN "amount_untaxed_before_global_discounts" double precision
"""
)
cr.execute(
env.cr.execute(
"""
update sale_order set amount_untaxed_before_global_discounts = amount_untaxed
"""
)
if not column_exists(cr, "sale_order", "amount_total_before_global_discounts"):
cr.execute(
if not column_exists(env.cr, "sale_order", "amount_total_before_global_discounts"):
env.cr.execute(
"""
ALTER TABLE "sale_order"
ADD COLUMN "amount_total_before_global_discounts" double precision
"""
)
cr.execute(
env.cr.execute(
"""
update sale_order set amount_total_before_global_discounts = amount_total
"""
Expand Down
94 changes: 81 additions & 13 deletions sale_global_discount/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import _, api, exceptions, fields, models
from odoo.tools.misc import formatLang


class SaleOrder(models.Model):
Expand All @@ -14,6 +15,9 @@ class SaleOrder(models.Model):
domain="[('discount_scope', '=', 'sale'), "
"('account_id', '!=', False), '|', "
"('company_id', '=', company_id), ('company_id', '=', False)]",
compute="_compute_global_discount_ids",
store=True,
readonly=False,
)
# HACK: Looks like UI doesn't behave well with Many2many fields and
# negative groups when the same field is shown. In this case, we want to
Expand Down Expand Up @@ -68,7 +72,7 @@ def _check_global_discounts_sanity(self):
return True
taxes_keys = {}
for line in self.order_line.filtered(
lambda l: not l.display_type and l.product_id
lambda _line: not _line.display_type and _line.product_id
):
if not line.tax_id:
raise exceptions.UserError(
Expand Down Expand Up @@ -135,20 +139,84 @@ def _compute_amounts(self):
return res

def _compute_tax_totals(self):
return super(
SaleOrder, self.with_context(from_tax_calculation=True)
)._compute_tax_totals()

@api.onchange("partner_id")
def onchange_partner_id_set_gbl_disc(self):
self.global_discount_ids = (
self.partner_id.customer_global_discount_ids.filtered(
lambda d: d.company_id == self.company_id
res = super()._compute_tax_totals()
for order in self:
amount_discount = 0.0
amount_discount_by_tax = {}
total_discount_tax = 0.0
for line in order.order_line:
if (
line.display_type
or not line.product_id
or line.product_id.bypass_global_discount
):
continue
for gbl_disc in order.global_discount_ids:
amount_discount += line.price_subtotal * gbl_disc.discount / 100
for tax in line.tax_id:
amount_discount_by_tax.setdefault(tax.tax_group_id.id, 0.0)
discounted_tax = tax.compute_all(
line.price_subtotal * gbl_disc.discount / 100,
line.order_id.currency_id,
1.0,
product=line.product_id,
partner=line.order_id.partner_shipping_id,
)["taxes"][0]["amount"]
amount_discount_by_tax[tax.tax_group_id.id] += discounted_tax
total_discount_tax += discounted_tax
currency = order.currency_id
amount_untaxed = order.tax_totals["amount_untaxed"] - amount_discount
amount_total = (
order.tax_totals["amount_total"] - amount_discount - total_discount_tax
)
or self.partner_id.commercial_partner_id.customer_global_discount_ids.filtered(
lambda d: d.company_id == self.company_id
for tax_dict in order.tax_totals["groups_by_subtotal"].values():
for tax in tax_dict:
tax["tax_group_base_amount"] = amount_untaxed
tax["formatted_tax_group_base_amount"] = formatLang(
self.env, amount_untaxed, currency_obj=currency
)
tax_dict = tax_dict[0]
tax_dict["tax_group_amount"] -= amount_discount_by_tax.get(
tax_dict["tax_group_id"], 0.0
)
tax_dict["formatted_tax_group_amount"] = formatLang(
self.env, tax_dict["tax_group_amount"], currency_obj=currency
)
order.tax_totals.update(
{
"amount_untaxed": amount_untaxed,
"formatted_amount_untaxed": formatLang(
self.env, amount_untaxed, currency_obj=currency
),
"amount_total": amount_total,
"formatted_amount_total": formatLang(
self.env, amount_total, currency_obj=currency
),
}
)
)
for subtotal in order.tax_totals["subtotals"]:
subtotal["amount"] = amount_untaxed
subtotal["formatted_amount"] = formatLang(
self.env, amount_untaxed, currency_obj=currency
)
return res

@api.depends("partner_id", "company_id")
def _compute_global_discount_ids(self):
for order in self:
commercial = order.partner_id.commercial_partner_id
commercial_global_disc = commercial.customer_global_discount_ids
partner_global_disc = order.partner_id.customer_global_discount_ids
discounts = self.env["global.discount"]
_discounts = self.env["global.discount"]
if partner_global_disc:
_discounts = partner_global_disc
else:
_discounts = commercial_global_disc
for discount in _discounts:
if discount.company_id == order.company_id:
discounts |= discount
order.global_discount_ids = discounts

def _prepare_invoice(self):
invoice_vals = super()._prepare_invoice()
Expand Down
4 changes: 4 additions & 0 deletions sale_global_discount/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
- David Vidal
- Pedro M. Baeza
- Omar Castiñeira \<<[email protected]>\>

- [Studio73](https://www.studio73.es)
- Miguel Gandia

15 changes: 8 additions & 7 deletions sale_global_discount/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@

/*
:Author: David Goodger ([email protected])
:Id: $Id: html4css1.css 9511 2024-01-13 09:50:07Z milde $
:Id: $Id: html4css1.css 8954 2022-01-20 10:10:25Z milde $
:Copyright: This stylesheet has been placed in the public domain.
Default cascading style sheet for the HTML output of Docutils.
Despite the name, some widely supported CSS2 features are used.
See https://docutils.sourceforge.io/docs/howto/html-stylesheets.html for how to
customize this style sheet.
Expand Down Expand Up @@ -275,7 +274,7 @@
margin-left: 2em ;
margin-right: 2em }

pre.code .ln { color: gray; } /* line numbers */
pre.code .ln { color: grey; } /* line numbers */
pre.code, code { background-color: #eeeeee }
pre.code .comment, code .comment { color: #5C6576 }
pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
Expand All @@ -301,7 +300,7 @@
span.pre {
white-space: pre }

span.problematic, pre.problematic {
span.problematic {
color: red }

span.section-subtitle {
Expand Down Expand Up @@ -441,14 +440,16 @@ <h2><a class="toc-backref" href="#toc-entry-7">Contributors</a></h2>
</ul>
</li>
<li>Omar Castiñeira &lt;<a class="reference external" href="mailto:omar&#64;comunitea.com">omar&#64;comunitea.com</a>&gt;</li>
<li><a class="reference external" href="https://www.studio73.es">Studio73</a><ul>
<li>Miguel Gandia</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#toc-entry-8">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org">
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
</a>
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
<p>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.</p>
Expand Down
4 changes: 3 additions & 1 deletion sale_global_discount/tests/test_sale_global_discount.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ def setUpClass(cls, chart_template_ref=None):
"account_id": cls.account.id,
}
)
cls.pricelist = cls.env.ref("product.list0")
cls.pricelist = cls.env["product.pricelist"].create(
{"name": "Public Pricelist", "sequence": 1}
)
cls.partner_1 = cls.env["res.partner"].create(
{"name": "Mr. Odoo", "property_product_pricelist": cls.pricelist.id}
)
Expand Down

0 comments on commit d0e60bd

Please sign in to comment.