-
Notifications
You must be signed in to change notification settings - Fork 2.3k
[ADD] sale: Auto-recalculate global discount on order line changes #881
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
arkp-odoo
wants to merge
1
commit into
odoo:18.0
Choose a base branch
from
odoo-dev:18.0-discount-update-arkp
base: 18.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Part of Odoo. See LICENSE file for full copyright and licensing details. | ||
from . import models | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
'name': 'Discount Price', | ||
'version': '1.0', | ||
'depends': ['sale_management'], | ||
'installable': True, | ||
'license': 'LGPL-3', | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Part of Odoo. See LICENSE file for full copyright and licensing details. | ||
from . import sale_order | ||
from . import sale_order_line |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from odoo import fields, models | ||
|
||
|
||
class SaleOrder(models.Model): | ||
_inherit = "sale.order" | ||
|
||
discount_percentage = fields.Float(string="Global Discount (%)") | ||
wizard_ids = fields.One2many("sale.order.discount", "sale_order_id", string="Discount Wizards") | ||
|
||
def _update_discount(self): | ||
discount_product = self.env["product.product"].search([("name", "=", "Discount")], limit=1) | ||
if not discount_product: | ||
return | ||
total_amount = sum(line.price_total for line in self.order_line if not line.is_discount_line()) | ||
if not total_amount: | ||
discount_lines = self.order_line.filtered(lambda l: l.is_discount_line()) | ||
discount_lines.unlink() | ||
return | ||
|
||
if self.wizard_ids: | ||
discount_percentage = self.wizard_ids[-1].discount_percentage | ||
else: | ||
return | ||
discount_amount = total_amount * (discount_percentage) | ||
discount_line = self.order_line.filtered(lambda l: l.is_discount_line()) | ||
if discount_line: | ||
discount_line.write( | ||
{ | ||
"price_unit": discount_amount * (-1), | ||
"name": f"Discount{discount_percentage * 100:.2f}%", | ||
} | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from odoo import api, models | ||
|
||
|
||
class SaleOrderLine(models.Model): | ||
_inherit = "sale.order.line" | ||
|
||
def is_discount_line(self): | ||
discount_product = self.order_id.env["product.product"].search([("name", "=", "Discount")], limit=1) | ||
return self.product_id == discount_product | ||
|
||
def unlink(self): | ||
order_ids = self.mapped("order_id") | ||
res = super().unlink() | ||
for order in order_ids: | ||
order._update_discount() | ||
return res | ||
|
||
@api.model_create_multi | ||
def create(self, vals): | ||
lines = super().create(vals) | ||
for line in lines: | ||
if line.order_id and not line.is_discount_line(): | ||
line.order_id._update_discount() | ||
return lines | ||
|
||
def write(self, vals): | ||
res = super().write(vals) | ||
for line in self: | ||
if not line.is_discount_line(): | ||
line.order_id._update_discount() | ||
return res |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.