Skip to content

Commit 4706d7e

Browse files
committed
[ADD] sale_extension: add module to distribute cost over other sales lines
- add wizard to list order lines to distribute the cost task-4496358
1 parent 69e1a02 commit 4706d7e

File tree

9 files changed

+144
-0
lines changed

9 files changed

+144
-0
lines changed

sale_extension/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from . import models
2+
from . import wizard

sale_extension/__manifest__.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
'name': 'Sale Extension',
3+
'version': '1.0',
4+
'depends': ['base', 'sale', 'sale_management'],
5+
'author': "Kishan B. Gajera",
6+
'category': 'Sale/Sale',
7+
'description': """
8+
A sample sale extension
9+
""",
10+
11+
'application': True,
12+
'installable': True,
13+
14+
'data': [
15+
'security/ir.model.access.csv',
16+
'views/sale_order_views.xml',
17+
'wizard/cost_distribution_wizard.xml',
18+
],
19+
20+
'license':'LGPL-3',
21+
}

sale_extension/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import sale_order_line
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from odoo import models, fields
2+
3+
class SaleOrderLine(models.Model):
4+
_inherit = 'sale.order.line'
5+
6+
distributed_cost = fields.Float("Distributed Cost")
7+
8+
def action_distribute_cost(self):
9+
return {
10+
'type': 'ir.actions.act_window',
11+
'name': 'Distribute Cost',
12+
'res_model': 'cost.distribution.wizard',
13+
'view_mode': 'form',
14+
'target': 'new',
15+
'context': {
16+
'order_id': self.order_id.id,
17+
'default_order_line_id': self.id,
18+
'default_price_subtotal': self.price_subtotal,
19+
}
20+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
2+
sale_extension.access_cost_distribution_wizard,access_cost_distribution_wizard,sale_extension.model_cost_distribution_wizard,base.group_user,1,1,1,1
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<odoo>
3+
4+
<record id="view_order_form_inherit" model="ir.ui.view">
5+
<field name="name">sale.order.view.form.inherit</field>
6+
<field name="model">sale.order</field>
7+
<field name="inherit_id" ref="sale.view_order_form"/>
8+
<field name="arch" type="xml">
9+
<xpath expr="//field[@name='product_template_id']" position="after">
10+
<button icon="fa-share-alt" string=" " type="object" name="action_distribute_cost"></button>
11+
</xpath>
12+
<xpath expr="//field[@name='price_total'][@invisible='is_downpayment']" position="after">
13+
<field name="distributed_cost"></field>
14+
</xpath>
15+
</field>
16+
</record>
17+
18+
</odoo>

sale_extension/wizard/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import cost_distribution_wizard
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from odoo import models, fields, api
2+
from odoo.exceptions import UserError
3+
4+
class CostDistributionWizard(models.TransientModel):
5+
_name = "cost.distribution.wizard"
6+
_description = "A Wizard to distribute cost over other sales order lines."
7+
8+
order_line_id = fields.Many2one("sale.order.line", string="Order Line")
9+
order_line_ids = fields.Many2many("sale.order.line", string="Order Lines")
10+
order_line_cost = fields.Float(string="Cost to Distribute")
11+
order_id = fields.Many2one("sale.order")
12+
price_subtotal = fields.Float("Price Subtotal")
13+
14+
@api.model
15+
def default_get(self, fields_list):
16+
res = super(CostDistributionWizard, self).default_get(fields_list)
17+
order_id = self.env.context.get("order_id")
18+
default_order_line_id = self.env.context.get("default_order_line_id")
19+
default_price_subtotal = self.env.context.get("default_price_subtotal", 0)
20+
21+
if order_id:
22+
order = self.env["sale.order"].browse(order_id)
23+
order_line_ids = order.order_line.ids
24+
if default_order_line_id in order_line_ids:
25+
order_line_ids.remove(default_order_line_id)
26+
27+
res.update({
28+
"order_id": order_id,
29+
"order_line_ids": [(6, 0, order_line_ids)],
30+
"order_line_cost": default_price_subtotal,
31+
})
32+
33+
distributed_cost = round(default_price_subtotal / len(order_line_ids), 2) if order_line_ids else 0
34+
for line in self.env["sale.order.line"].browse(order_line_ids):
35+
line.write({"distributed_cost": distributed_cost})
36+
37+
return res
38+
39+
def distribute_cost(self):
40+
total_cost_distributed = sum(line.distributed_cost for line in self.order_line_ids)
41+
42+
if total_cost_distributed > self.price_subtotal:
43+
raise UserError("Distributed price is greater than the distributable price.")
44+
45+
for line in self.order_line_ids:
46+
line.price_subtotal += line.distributed_cost
47+
48+
original_order_line = self.env["sale.order.line"].browse(self.env.context.get("default_order_line_id"))
49+
original_order_line.price_subtotal -= total_cost_distributed
50+
51+
if total_cost_distributed == original_order_line.price_subtotal:
52+
pass
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<odoo>
3+
<record id="cost_distribution_wizard" model="ir.ui.view">
4+
<field name="name">cost.distribution.wizard.form</field>
5+
<field name="model">cost.distribution.wizard</field>
6+
<field name="arch" type="xml">
7+
<form>
8+
<sheet>
9+
<group style="text-align: right;">
10+
<field name="order_line_cost" readonly="1"/>
11+
</group>
12+
<field name="order_line_ids" invisible="price_subtotal == 0">
13+
<list editable="bottom">
14+
<field name="product_id" readonly="1" />
15+
<field name="price_subtotal" string="Current Cost" readonly="1" />
16+
<field name="distributed_cost" />
17+
</list>
18+
</field>
19+
<footer>
20+
<button string="Distribute" type="object" name="distribute_cost" class="btn-primary"/>
21+
<button string="Cancel" class="btn-secondary" special="cancel"/>
22+
</footer>
23+
</sheet>
24+
</form>
25+
</field>
26+
</record>
27+
</odoo>

0 commit comments

Comments
 (0)