Skip to content

Commit

Permalink
Merge dce8fb6 into e12fe07
Browse files Browse the repository at this point in the history
  • Loading branch information
tafaRU committed Jun 19, 2015
2 parents e12fe07 + dce8fb6 commit 305d3fc
Show file tree
Hide file tree
Showing 10 changed files with 346 additions and 0 deletions.
45 changes: 45 additions & 0 deletions sale_line_price_properties_based/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
:alt: License: AGPL-3

Sale line price properties based
================================

This module allows to use python formaulas to compute the sale order line
price.

You can configure the 'Price formula' on the product form using python code.

Formula example:
```
area = float(properties['Width']) * float(properties['Length'])
result = area / 2.0
if 'Painting' in properties:
result = result + 5
```

When changing properties on sale order line, the system will automatically
compute the line price unit.

Credits
=======

Contributors
------------

* Lorenzo Battistini <lorenzo.battistini@agilebg.com>
* Alex Comba <alex.comba@agilebg.com>

Maintainer
----------

.. image:: http://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: http://odoo-community.org

This module is maintained by the OCA.

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

To contribute to this module, please visit http://odoo-community.org.
21 changes: 21 additions & 0 deletions sale_line_price_properties_based/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (C) 2014-15 Agile Business Group sagl (<http://www.agilebg.com>)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from . import product
from . import sale_order_line
43 changes: 43 additions & 0 deletions sale_line_price_properties_based/__openerp__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (C) 2014-15 Agile Business Group sagl
# (<http://www.agilebg.com>)
# @author Lorenzo Battistini <lorenzo.battistini@agilebg.com>
# @author Alex Comba <alex.comba@agilebg.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
{
'name': "Sale line price properties based",
'version': '1.0',
'category': '',
'author': "Agile Business Group,Odoo Community Association (OCA)",
'website': 'http://www.agilebg.com',
'license': 'AGPL-3',
"depends": [
'sale_properties_easy_creation',
],
"data": [
'product_view.xml',
],
"demo": [
'product_demo.xml',
],
"test": [
'test/sale_order.yml',
],
"installable": True
}
63 changes: 63 additions & 0 deletions sale_line_price_properties_based/i18n/it.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Translation of OpenERP Server.
# This file contains the translation of the following modules:
# * sale_line_price_properties_based
#
msgid ""
msgstr ""
"Project-Id-Version: OpenERP Server 7.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2014-08-19 09:46+0000\n"
"PO-Revision-Date: 2014-08-19 09:46+0000\n"
"Last-Translator: <>\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"

#. module: sale_line_price_properties_based
#: field:product.product,price_formula_id:0
msgid "Price formula"
msgstr "Formula prezzo"

#. module: sale_line_price_properties_based
#: code:_description:0
#: model:ir.model,name:sale_line_price_properties_based.model_product_product
#, python-format
msgid "Product"
msgstr "Prodotto"

#. module: sale_line_price_properties_based
#: code:_description:0
#: model:ir.model,name:sale_line_price_properties_based.model_sale_order_line
#, python-format
msgid "Sales Order Line"
msgstr "Linea d'ordine di vendita"

#. module: sale_line_price_properties_based
#: help:product.product,price_formula_id:0
msgid "You can use the variables\n"
" - self\n"
" - cr\n"
" - uid\n"
" - ptype\n"
" - properties (dictionary of properties)\n"
"You have to put the result in the 'result' variable"
msgstr "E' possibile usare le variabili\n"
" - self\n"
" - cr\n"
" - uid\n"
" - ptype\n"
" - properties (dizionario di proprietà)\n"
"Bisogna mettere il risultato nella variabile 'result'"

#. module: sale_line_price_properties_based
#: view:sale.order:0
msgid "product_id_change(parent.pricelist_id,product_id,product_uom_qty,product_uom,False,False,False,parent.partner_id, False, False, parent.date_order, False, False, False, context)"
msgstr "product_id_change(parent.pricelist_id,product_id,product_uom_qty,product_uom,False,False,False,parent.partner_id, False, False, parent.date_order, False, False, False, context)"

#. module: sale_line_price_properties_based
#: view:sale.order:0
msgid "{'property_ids': property_ids}"
msgstr "{'property_ids': property_ids}"

83 changes: 83 additions & 0 deletions sale_line_price_properties_based/product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (C) 2014-15 Agile Business Group sagl
# (<http://www.agilebg.com>)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.osv import orm, fields
from openerp.tools.translate import _
import logging

_logger = logging.getLogger(__name__)


class ProductProduct(orm.Model):
_inherit = "product.product"

_columns = {
'price_formula_id': fields.many2one(
'mrp.property.formula', 'Price formula',
help="You can use the variables\n"
" - self\n"
" - cr\n"
" - uid\n"
" - ptype\n"
" - product_id\n"
" - properties (dictionary of properties)\n"
"You have to put the result in the 'result' variable"),
}


class ProductTemplate(orm.Model):
_inherit = "product.template"

def _price_get(self, cr, uid, products, ptype='list_price', context=None):
if context is None:
context = {}

res = super(ProductTemplate, self)._price_get(
cr, uid, products, ptype=ptype, context=context)
if 'properties' in context:
for product in products:
res = super(ProductTemplate, self)._price_get(
cr, uid, products, ptype=ptype, context=context)
if product.price_formula_id:
localdict = {
'self': self,
'cr': cr,
'uid': uid,
'ptype': ptype,
'product_id': product.id,
'properties': context['properties'],
}
try:
exec product.price_formula_id.formula_text in localdict
except KeyError:
_logger.warning(
"KeyError for formula '%s' and prop_dict '%s'"
% (product.price_formula_id.formula_text,
context['properties']))
continue
try:
amount = localdict['result']
except KeyError:
raise orm.except_orm(
_('Error'),
_("Formula must contain 'result' variable"))
res[product.id] = amount
return res
8 changes: 8 additions & 0 deletions sale_line_price_properties_based/product_demo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record model="product.product" id="product.product_product_7">
<field name="price_formula_id" ref="sale_properties_easy_creation.area_formula"></field>
</record>
</data>
</openerp>
15 changes: 15 additions & 0 deletions sale_line_price_properties_based/product_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record id="product_normal_form_formula_view" model="ir.ui.view">
<field name="name">product.normal.formula.form</field>
<field name="model">product.product</field>
<field name="inherit_id" ref="product.product_normal_form_view"></field>
<field name="arch" type="xml">
<field name="lst_price" position="after">
<field name="price_formula_id"></field>
</field>
</field>
</record>
</data>
</openerp>
55 changes: 55 additions & 0 deletions sale_line_price_properties_based/sale_order_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (C) 2014-15 Agile Business Group sagl
# (<http://www.agilebg.com>)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp import models, api
from openerp.tools.translate import _
from openerp.exceptions import except_orm


class SaleOrderLine(models.Model):
_inherit = 'sale.order.line'

@api.onchange(
'property_ids', 'product_id', 'product_uom_qty', 'product_uom'
)
def price_property_ids_changed(self):
prop_dict = {}
prop_ctx = self.env.context.copy()
if 'lang' in prop_ctx:
del prop_ctx['lang']
if self.product_id:
for prop in self.env['mrp.property'].with_context(prop_ctx).browse(
[p.id for p in self.property_ids]
):
if prop.group_id.name in prop_dict:
raise except_orm(
_('Error'),
_('Property of group %s already present')
% prop.group_id.name)
prop_dict[prop.group_id.name] = prop.value
self.price_unit = self.pool.get('product.pricelist').price_get(
self._cr, self._uid, [self.order_id.pricelist_id.id],
self.product_id.id, self.product_uom_qty or 1.0,
self.order_id.partner_id.id, {
'uom': self.product_uom.id,
'date': self.order_id.date_order,
'properties': prop_dict,
})[self.order_id.pricelist_id.id]
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions sale_line_price_properties_based/test/sale_order.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-
I check the on_change
-
!python {model: sale.order.line}: |
if context is None:
context = {}
context['property_ids'] = [(6, False, [
ref('sale_properties_easy_creation.length_5'),
ref('sale_properties_easy_creation.width_1'),
])]
res = self.product_id_change(cr, uid, [], ref('product.list0'), ref('product.product_product_7'), qty=8,
partner_id=ref('base.res_partner_2'), context=context)
assert res['value']['price_unit'] == 2.5, "Price unit must be 2.5, %s found" % res['value']['price_unit']

0 comments on commit 305d3fc

Please sign in to comment.