Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration of sale_line_price_properties_based #145

Merged
merged 10 commits into from
Jun 9, 2017
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ env:
matrix:
- LINT_CHECK="1"
- TRANSIFEX="1"
- TESTS="1" ODOO_REPO="odoo/odoo" EXCLUDE="sale_quotation_sourcing,sale_sourced_by_line,sale_quotation_sourcing_stock_route_transit,sale_sourced_by_line_sale_transport_multi_address,sale_properties_dynamic_fields,sale_line_quantity_properties_based" LINT_CHECK="0"
- TESTS="1" ODOO_REPO="OCA/OCB" EXCLUDE="sale_quotation_sourcing,sale_sourced_by_line,sale_quotation_sourcing_stock_route_transit,sale_sourced_by_line_sale_transport_multi_address,sale_properties_dynamic_fields,sale_line_quantity_properties_based" LINT_CHECK="0"
- TESTS="1" ODOO_REPO="odoo/odoo" EXCLUDE="sale_quotation_sourcing,sale_sourced_by_line,sale_quotation_sourcing_stock_route_transit,sale_sourced_by_line_sale_transport_multi_address,sale_properties_dynamic_fields,sale_line_quantity_properties_based,sale_line_price_properties_based" LINT_CHECK="0"
- TESTS="1" ODOO_REPO="OCA/OCB" EXCLUDE="sale_quotation_sourcing,sale_sourced_by_line,sale_quotation_sourcing_stock_route_transit,sale_sourced_by_line_sale_transport_multi_address,sale_properties_dynamic_fields,sale_line_quantity_properties_based,sale_line_price_properties_based" LINT_CHECK="0"
- TESTS="1" ODOO_REPO="odoo/odoo" INCLUDE="sale_sourced_by_line" LINT_CHECK="0"
- TESTS="1" ODOO_REPO="OCA/OCB" INCLUDE="sale_sourced_by_line" LINT_CHECK="0"
- TESTS="1" ODOO_REPO="odoo/odoo" INCLUDE="sale_quotation_sourcing" LINT_CHECK="0"
Expand All @@ -37,6 +37,8 @@ env:
- TESTS="1" ODOO_REPO="OCA/OCB" INCLUDE="sale_properties_dynamic_fields" LINT_CHECK="0"
- TESTS="1" ODOO_REPO="odoo/odoo" INCLUDE="sale_line_quantity_properties_based" LINT_CHECK="0"
- TESTS="1" ODOO_REPO="OCA/OCB" INCLUDE="sale_line_quantity_properties_based" LINT_CHECK="0"
- TESTS="1" ODOO_REPO="odoo/odoo" INCLUDE="sale_line_price_properties_based" LINT_CHECK="0"
- TESTS="1" ODOO_REPO="OCA/OCB" INCLUDE="sale_line_price_properties_based" LINT_CHECK="0"

virtualenv:
system_site_packages: true
Expand Down
68 changes: 68 additions & 0 deletions sale_line_price_properties_based/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3

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

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

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

Formula example:

.. code::

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.

Usage
=====

.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/167/8.0

Bug Tracker
===========

Bugs are tracked on `GitHub Issues
<https://github.com/OCA/sale-workflow}/issues>`_. In case of trouble, please
check there if your issue has already been reported. If you spotted it first,
help us smash it by providing detailed and welcomed feedback.

Credits
=======

Images
------

* Odoo Community Association: `Icon <https://github.com/OCA/maintainer-tools/blob/master/template/module/static/description/icon.svg>`_.

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

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

Maintainer
----------

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://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 https://odoo-community.org.
5 changes: 5 additions & 0 deletions sale_line_price_properties_based/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2014 -2017 Alex Comba - Agile Business Group
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from . import models
23 changes: 23 additions & 0 deletions sale_line_price_properties_based/__openerp__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
# Copyright 2014 -2017 Alex Comba - Agile Business Group
# Copyright 2014 -2017 Lorenzo Battistini - Agile Business Group
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
{
'name': 'Sale line price properties based',
'version': '8.0.1.0.0',
'category': 'Sales Management',
'author': "Agile Business Group, Odoo Community Association (OCA)",
'website': 'https://www.agilebg.com',
'license': 'AGPL-3',
'depends': [
'sale_properties_dynamic_fields',
],
'data': [
'views/product_view.xml',
'views/sale_order_view.xml',
],
'demo': [
'demo/product_demo.xml',
],
'installable': True
}
8 changes: 8 additions & 0 deletions sale_line_price_properties_based/demo/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>
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}"

6 changes: 6 additions & 0 deletions sale_line_price_properties_based/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright 2014 -2017 Alex Comba - Agile Business Group
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from . import product
from . import sale_order_line
58 changes: 58 additions & 0 deletions sale_line_price_properties_based/models/product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# -*- coding: utf-8 -*-
# Copyright 2014 -2017 Alex Comba - Agile Business Group
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from openerp.osv import orm, fields
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,
'uos_id': context['uos_id'],
'properties': context['properties'],
}
try:
res[product.id] = product.price_formula_id.\
compute_formula(localdict)
except ValueError as e:
_logger.warning(
"Formula evaluation error: '%s'" % e.message)
continue
return res
71 changes: 71 additions & 0 deletions sale_line_price_properties_based/models/sale_order_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# -*- coding: utf-8 -*-
# Copyright 2014 -2017 Alex Comba - Agile Business Group
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

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_uos')
def price_property_ids_changed(self):
prop_dict = {}
ctx = self.env.context.copy()
if 'lang' in ctx:
del ctx['lang']
if self.product_id:
for prop in self.env['mrp.property'].with_context(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
ctx.update({'uos_id': self.product_uos.id,
'uom': self.product_uom.id,
'date': self.order_id.date_order,
'properties': prop_dict,
})
self.price_unit = self.env['product.pricelist'].with_context(
ctx).price_get(
self.product_id.id,
self.product_uom_qty or 1.0,
self.order_id.partner_id.id)[self.order_id.pricelist_id.id]

def product_id_change(
self, cr, uid, ids, pricelist, product_id, qty=0,
uom=False, qty_uos=0, uos=False, name='', partner_id=False,
lang=False, update_tax=True, date_order=False, packaging=False,
fiscal_position=False, flag=False, context=None
):
res = super(SaleOrderLine, self).product_id_change(
cr, uid, ids, pricelist, product_id, qty=qty,
uom=uom, qty_uos=qty_uos, uos=uos,
name=name, partner_id=partner_id,
lang=lang, update_tax=update_tax,
date_order=date_order, packaging=packaging,
fiscal_position=fiscal_position, flag=flag, context=context)
if 'value' in res:
# get empty properties
res['value']['property_ids'] = []
# get empty properties dynamic fields
property_group_pool = self.pool['mrp.property.group']
group_to_empty_ids = property_group_pool.search(
cr, uid,
[('draw_dynamically', '=', True)], context=context)
groups = property_group_pool.browse(
cr, uid, group_to_empty_ids, context=context
)
for group in groups:
if (
group.field_id and
group.field_id.name and
group.field_id.name in res['value']
):
res['value'][group.field_id.name] = None
return res
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions sale_line_price_properties_based/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Alex Comba - Agile Business Group
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from . import test_sale_line_price_properties_based
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Alex Comba - Agile Business Group
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from openerp.tests.common import TransactionCase


class TestSaleLinePricePropertiesBased(TransactionCase):

def setUp(self):
super(TestSaleLinePricePropertiesBased, self).setUp()
self.partner = self.env.ref('base.res_partner_3')
# use product for which is set price_formula_id
product = self.env.ref('product.product_product_7')
self.so = self.env['sale.order'].create({
'partner_id': self.partner.id
})

length_5 = self.env.ref('sale_properties_easy_creation.length_5')
width_1 = self.env.ref('sale_properties_easy_creation.width_1')
self.sol = self.env['sale.order.line'].create({
'name': '/',
'order_id': self.so.id,
'product_id': product.id,
})
self.sol.property_ids = [length_5.id, width_1.id]

def test_price_property_ids_changed(self):
self.sol.price_property_ids_changed()
# according to sale_properties_easy_creation.area_formula
# price_unit should be (length_5 * width_1) / 2
self.assertEqual(self.sol.price_unit, 2.5)
15 changes: 15 additions & 0 deletions sale_line_price_properties_based/views/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>
Loading