Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions purchase_discount/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
15 changes: 15 additions & 0 deletions purchase_discount/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
'author': 'Odoo S.A.',
'name': 'Purchase Discount',
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the description.

"description": """
This module introduces a global discount feature for Purchase Orders,
allowing users to apply either value-based or percentage-based discounts.
""",
'depends': ['purchase'],
'license': 'LGPL-3',
'data': [
'views/purchase_order_views.xml'
],
'application': True,
'installable': True
}
1 change: 1 addition & 0 deletions purchase_discount/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import purchase_order
53 changes: 53 additions & 0 deletions purchase_discount/models/purchase_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from odoo import models, fields, api
from odoo.exceptions import ValidationError
from odoo.tools import _


class PurchaseOrder(models.Model):
_inherit = "purchase.order"

discount_type = fields.Selection(
selection=[
("value", "$"),
("percentage", "%"),
],
default="percentage",
)
discount_in_value = fields.Float(string="Discount")
discount_in_percentage = fields.Float(compute="_compute_discount_percentage", store=True, readonly=True)

@api.depends("discount_type", "discount_in_value")
def _compute_discount_percentage(self):
for record in self:
if record.discount_type == "value" and record.amount_total > 0:
record.discount_in_percentage = (record.discount_in_value * 100) / (record.amount_total)
elif record.discount_type == "percentage":
record.discount_in_percentage = record.discount_in_value
else:
record.discount_in_percentage = 0.0

@api.constrains("discount_in_value", "discount_type", "amount_total")
def _check_discount_price(self):
for record in self:
if record.discount_type == "percentage":
if record.discount_in_value < 0 or record.discount_in_value > 100:
raise ValidationError("discount value is not valid please check again")
if record.discount_type == "value":
if record.discount_in_value < 0 or record.discount_in_value > record.amount_total:
raise ValidationError("discount value is not valid please check again")

def apply_discount(self):
self.order_line.discount = self.discount_in_percentage

def action_discount(self):
return {
"type": "ir.actions.act_window",
"name": _("Discount"),
"res_model": "purchase.order",
"view_mode": "form",
"view_id": self.env.ref(
"purchase_discount.view_purchase_order_discount_form"
).id,
"res_id": self.id,
"target": "new",
}
46 changes: 46 additions & 0 deletions purchase_discount/views/purchase_order_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0"?>
<odoo>
<record id="view_purchase_order_discount_form" model="ir.ui.view">
<field name="name">purchase.order.discount.form</field>
<field name="model">purchase.order</field>
<field name="priority">17</field>
<field name="arch" type="xml">
<form>
<sheet>
<div class="d-flex flex-row gap-2">
<label for="discount_in_value">Discount</label>
<div style="max-width:150px;">
<field name="discount_in_value"/>
</div>
<div style="max-width:50px;">
<field name="discount_type"/>
</div>
<span class="text-muted" invisible="discount_type == 'percentage'">
(<field name="discount_in_percentage"/>%)
</span>
</div>
</sheet>
<footer>
<button name="apply_discount" type="object" string="Apply" class="oe_highlight"/>
<button string="Cancel" class="btn-secondary" special="cancel"/>
</footer>
</form>
</field>
</record>

<record id="purchase_order_form_inherit" model="ir.ui.view">
<field name="name">purchase.order.view.form.inherit</field>
<field name="model">purchase.order</field>
<field name="inherit_id" ref="purchase.purchase_order_form"/>
<field name="arch" type="xml">
<xpath expr="//*[hasclass('oe_subtotal_footer')]" position="replace">
<div class="d-flex flex-column align-items-end">
<button name="action_discount" type="object" class="oe_highlight" string="Discount" invisible="state in ('purchase', 'cancel')"/>
<group class="oe_subtotal_footer">
<field name="tax_totals" widget="account-tax-totals-field" nolabel="1" readonly="1"/>
</group>
</div>
</xpath>
</field>
</record>
</odoo>