summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnand Kansagra <aka@openerp.com>2015-08-06 11:34:44 +0530
committerMartin Trigaux <mat@odoo.com>2015-12-16 10:29:03 +0100
commit1ca86e70dec275e24503c5441ce452a8e4763927 (patch)
treed9ae2302b2b624df27ec8643e1d9f65132ada4eb
parent332aded356f3183db346446c8a1dd3a8a23cdfb2 (diff)
[IMP] sale_margin: Migrate into new api.
-rw-r--r--addons/sale_margin/__init__.py1
-rw-r--r--addons/sale_margin/__openerp__.py4
-rw-r--r--addons/sale_margin/data/sale_margin_demo.xml4
-rw-r--r--addons/sale_margin/models/sale_order.py61
-rw-r--r--addons/sale_margin/report/__init__.py1
-rw-r--r--addons/sale_margin/report/sale_report.py7
-rw-r--r--addons/sale_margin/views/sale_margin_view.xml8
7 files changed, 31 insertions, 55 deletions
diff --git a/addons/sale_margin/__init__.py b/addons/sale_margin/__init__.py
index 0ed5c5445e8..a5f67aebd79 100644
--- a/addons/sale_margin/__init__.py
+++ b/addons/sale_margin/__init__.py
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import models
diff --git a/addons/sale_margin/__openerp__.py b/addons/sale_margin/__openerp__.py
index eae9ff673a9..706c55aa002 100644
--- a/addons/sale_margin/__openerp__.py
+++ b/addons/sale_margin/__openerp__.py
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
{
@@ -11,11 +12,8 @@ This module adds the 'Margin' on sales order.
This gives the profitability by calculating the difference between the Unit
Price and Cost Price.
""",
- 'author':'OpenERP SA',
'depends':['sale'],
'demo':['data/sale_margin_demo.xml'],
'test': ['test/sale_margin.yml'],
'data':['security/ir.model.access.csv','views/sale_margin_view.xml'],
- 'auto_install': False,
- 'installable': True,
}
diff --git a/addons/sale_margin/data/sale_margin_demo.xml b/addons/sale_margin/data/sale_margin_demo.xml
index a41f05304a4..8c1a96535a4 100644
--- a/addons/sale_margin/data/sale_margin_demo.xml
+++ b/addons/sale_margin/data/sale_margin_demo.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
-<openerp>
+<odoo>
<data noupdate="1">
<record id="sale.sale_order_line_1" model="sale.order.line">
@@ -43,4 +43,4 @@
</record>
</data>
-</openerp>
+</odoo>
diff --git a/addons/sale_margin/models/sale_order.py b/addons/sale_margin/models/sale_order.py
index 62659b5c046..f836d24935e 100644
--- a/addons/sale_margin/models/sale_order.py
+++ b/addons/sale_margin/models/sale_order.py
@@ -1,12 +1,16 @@
+# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
-from openerp import api
-from openerp.osv import fields, osv
-import openerp.addons.decimal_precision as dp
+from odoo import api, fields, models
+import odoo.addons.decimal_precision as dp
-class sale_order_line(osv.osv):
+
+class SaleOrderLine(models.Model):
_inherit = "sale.order.line"
+ margin = fields.Float(compute='_product_margin', digits=dp.get_precision('Product Price'), store=True)
+ purchase_price = fields.Float(string='Cost', digits=dp.get_precision('Product Price'))
+
@api.multi
@api.onchange('product_id', 'product_uom_qty')
def product_id_change_margin(self):
@@ -22,46 +26,19 @@ class sale_order_line(osv.osv):
price = frm_cur.with_context(ctx).compute(purchase_price, to_cur, round=False)
line.purchase_price = price
- def _product_margin(self, cr, uid, ids, field_name, arg, context=None):
- cur_obj = self.pool.get('res.currency')
- res = {}
- for line in self.browse(cr, uid, ids, context=context):
- cur = line.order_id.pricelist_id.currency_id
- res[line.id] = 0
- if line.product_id:
- tmp_margin = line.price_subtotal - ((line.purchase_price or line.product_id.standard_price) * line.product_uom_qty)
- res[line.id] = cur_obj.round(cr, uid, cur, tmp_margin)
- return res
-
- _columns = {
- 'margin': fields.function(_product_margin, string='Margin', digits_compute= dp.get_precision('Product Price'),
- store = True),
- 'purchase_price': fields.float('Cost', digits_compute= dp.get_precision('Product Price'))
- }
+ @api.depends('product_id', 'purchase_price', 'product_uom_qty', 'price_unit')
+ def _product_margin(self):
+ for line in self:
+ currency = line.order_id.pricelist_id.currency_id
+ line.margin = currency.round(line.price_subtotal - ((line.purchase_price or line.product_id.standard_price) * line.product_uom_qty))
-class sale_order(osv.osv):
+class SaleOrder(models.Model):
_inherit = "sale.order"
- def _product_margin(self, cr, uid, ids, field_name, arg, context=None):
- result = {}
- for sale in self.browse(cr, uid, ids, context=context):
- result[sale.id] = 0.0
- for line in sale.order_line:
- if line.state == 'cancel':
- continue
- result[sale.id] += line.margin or 0.0
- return result
-
- def _get_order(self, cr, uid, ids, context=None):
- result = {}
- for line in self.pool.get('sale.order.line').browse(cr, uid, ids, context=context):
- result[line.order_id.id] = True
- return result.keys()
+ margin = fields.Monetary(compute='_product_margin', help="It gives profitability by calculating the difference between the Unit Price and the cost.", currency_field='currency_id', digits=dp.get_precision('Product Price'), store=True)
- _columns = {
- 'margin': fields.function(_product_margin, string='Margin', help="It gives profitability by calculating the difference between the Unit Price and the cost.", store={
- 'sale.order.line': (_get_order, ['margin', 'purchase_price'], 20),
- 'sale.order': (lambda self, cr, uid, ids, c={}: ids, ['order_line'], 20),
- }, digits_compute= dp.get_precision('Product Price')),
- }
+ @api.depends('order_line.margin')
+ def _product_margin(self):
+ for order in self:
+ order.margin = sum(order.order_line.filtered(lambda r: r.state != 'cancel').mapped('margin'))
diff --git a/addons/sale_margin/report/__init__.py b/addons/sale_margin/report/__init__.py
index aa0c78c6ade..894dd3f737b 100644
--- a/addons/sale_margin/report/__init__.py
+++ b/addons/sale_margin/report/__init__.py
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
import sale_report
diff --git a/addons/sale_margin/report/sale_report.py b/addons/sale_margin/report/sale_report.py
index 9388ce3d5b5..5bfcaaacf34 100644
--- a/addons/sale_margin/report/sale_report.py
+++ b/addons/sale_margin/report/sale_report.py
@@ -1,12 +1,13 @@
# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
-from openerp import models, fields
+from odoo import fields, models
-class sale_report(models.Model):
+class SaleReport(models.Model):
_inherit = 'sale.report'
margin = fields.Float('Margin')
def _select(self):
- return super(sale_report, self)._select() + ", SUM(l.margin) AS margin"
+ return super(SaleReport, self)._select() + ", SUM(l.margin) AS margin"
diff --git a/addons/sale_margin/views/sale_margin_view.xml b/addons/sale_margin/views/sale_margin_view.xml
index d0cffa1e035..fa9c56fe28a 100644
--- a/addons/sale_margin/views/sale_margin_view.xml
+++ b/addons/sale_margin/views/sale_margin_view.xml
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
-<openerp>
-<data>
+<odoo>
<record model="ir.ui.view" id="sale_margin_sale_order">
<field name="name">sale.order.margin.view.form</field>
@@ -9,7 +8,7 @@
<field name="arch" type="xml">
<xpath expr="//group[@name='sale_total']" position="after">
<group name="sale_margin_grp">
- <field name="margin" widget='monetary' options="{'currency_field': 'currency_id'}" groups="base.group_user"/>
+ <field name="margin" groups="base.group_user"/>
</group>
</xpath>
</field>
@@ -37,5 +36,4 @@
</field>
</record>
-</data>
-</openerp>
+</odoo>