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
2 changes: 2 additions & 0 deletions purchase_discount/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import models
from . import wizard
18 changes: 18 additions & 0 deletions purchase_discount/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
'name': 'purchase_dicount',
'description': "Add function for disscount",
'author': "meet kavathiya",
'website': "https://www.odoo.com/",
'category': "Real-estate",
'version': "0.1",
'application': True,
'installable': True,
'depends': ['purchase'],
'data': [
'security/ir.model.access.csv',
'views/purchase_order_view.xml',
'wizard/purchase_order_discount_view.xml'
],
'assets': {},
'license': 'LGPL-3',
}
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
15 changes: 15 additions & 0 deletions purchase_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_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_order_discount,access_purchase_order_discount,model_purchase_order_discount,base.group_user,1,1,1,1
15 changes: 15 additions & 0 deletions purchase_discount/views/purchase_order_view.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="inherited_purchase_view_form" model="ir.ui.view">
<field name="name">purchase.view.form.inherit.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_discount/wizard/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import purchase_order_discount
61 changes: 61 additions & 0 deletions purchase_discount/wizard/purchase_order_discount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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.constrains('discount_type', 'discount_percentage')
def _check_discount_amount(self):
for wizard in self:
if (
(wizard.discount_type in ('percentage')
and wizard.discount_percentage > 100.0)
or (wizard.discount_type in ('amount')
and wizard.discount_percentage > wizard.purchase_order_id.amount_untaxed)
):
raise ValidationError("Invalid discount amount")

@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}
)
23 changes: 23 additions & 0 deletions purchase_discount/wizard/purchase_order_discount_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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="purchase_order_id" invisible="1"/> -->
<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" data-hotkey="q"/>
<button special="cancel" string="Discard" class="btn btn-secondary" data-hotkey="x"/>
</footer>
</form>
</field>
</record>

</odoo>