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
4 changes: 4 additions & 0 deletions purchase_global_discount/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from . import (
models, # noqa : F401
wizard, # noqa : F401
)
18 changes: 18 additions & 0 deletions purchase_global_discount/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "purchase_dicount",
"description": "Add discount button with its wizard",
"author": "odoo-pupat",
"website": "https://www.odoo.com/",
"category": "Purchase-custom",
"version": "0.1",
"application": True,
"installable": True,
"depends": ["purchase"],
"data": [
"security/ir.model.access.csv",
"views/purchase_order_views.xml",
"wizard/purchase_order_discount_views.xml",
],
"assets": {},
"license": "LGPL-3",
}
3 changes: 3 additions & 0 deletions purchase_global_discount/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import (
purchase_order, # noqa: F401
)
15 changes: 15 additions & 0 deletions purchase_global_discount/models/purchase_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from odoo import models


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

def action_open_discount_wizard(self):
self.ensure_one()
return {
"name": "Discount",
"type": "ir.actions.act_window",
"res_model": "purchase.order.discount",
"view_mode": "form",
"target": "new",
}
2 changes: 2 additions & 0 deletions purchase_global_discount/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_purchase_discount_wizard,purchase.discount.wizard,model_purchase_order_discount,base.group_user,1,1,1,1
15 changes: 15 additions & 0 deletions purchase_global_discount/views/purchase_order_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="purchase_view_form" model="ir.ui.view">
<field name="name">purchase.view.form.purchase.discount</field>
<field name="model">purchase.order</field>
<field name="inherit_id" ref="purchase.purchase_order_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='order_line']" position="after">
<div class="d-flex justify-content-end mt-2 mb-2">
<button string="Discount" name="action_open_discount_wizard" type="object" class="btn btn-secondary"/>
</div>
</xpath>
</field>
</record>
</odoo>
1 change: 1 addition & 0 deletions purchase_global_discount/wizard/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import purchase_order_discount # noqa: F401
50 changes: 50 additions & 0 deletions purchase_global_discount/wizard/purchase_order_discount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from odoo import api, fields, models
from odoo.exceptions import ValidationError


class PurchaseOrderDiscount(models.TransientModel):
_name = "purchase.order.discount"
_description = "Discount Wizard"

purchase_order_id = fields.Many2one(
"purchase.order",
default=lambda self: self.env.context.get("active_id"),
required=True,
)
discount_percentage = fields.Float(string="Percentage")
discount_type = fields.Selection(
selection=[
("percentage", "%"),
("amount", "$"),
],
default="percentage",
)
percentage = fields.Float(compute="_compute_initial_discount")

@api.depends("discount_type", "discount_percentage")
def _compute_initial_discount(self):
if self.discount_type == "amount":
if self.purchase_order_id.amount_untaxed == 0:
raise ValidationError("No more discount possible")
self.percentage = (
self.discount_percentage * 100 / self.purchase_order_id.amount_untaxed
)
else:
self.percentage = self.discount_percentage

def action_apply_discount(self):
self.ensure_one()
if self.discount_type == "amount":
self.purchase_order_id.order_line.write(
{
"discount": (
self.discount_percentage
* 100
/ self.purchase_order_id.amount_untaxed
)
}
)
else:
self.purchase_order_id.order_line.write(
{"discount": self.discount_percentage}
)
22 changes: 22 additions & 0 deletions purchase_global_discount/wizard/purchase_order_discount_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>

<record id="purchase_order_line_wizard_form" model="ir.ui.view">
<field name="name">purchase.order.discount.form</field>
<field name="model">purchase.order.discount</field>
<field name="arch" type="xml">
<form>
<sheet>

<field name="discount_percentage" nolabel="1"/>
<field name="discount_type" nolabel="1"/>
<field name="percentage" class='text-muted' nolabel="1"/>
</sheet>
<footer>
<button type="object" string="Apply" name="action_apply_discount" class="btn btn-primary"/>
<button special="cancel" string="Cancel" class="btn btn-secondary"/>
</footer>
</form>
</field>
</record>
</odoo>