From 6e387294491c15b7050f6bea7c07910a01b5286f Mon Sep 17 00:00:00 2001
From: Nicolas Martinelli <nim@odoo.com>
Date: Thu, 27 Aug 2015 12:15:18 +0200
Subject: [PATCH] [ADD] product_uos: adaptation due to the new Sale module

New module to centralize UoS management.

Reason: complete rewrite of the Sale module.

Responsible: fp, dbo, nim
---
 addons/product_uos/__init__.py                |  4 +++
 addons/product_uos/__openerp__.py             | 25 +++++++++++++
 addons/product_uos/models/__init__.py         |  4 +++
 addons/product_uos/models/product_uos.py      | 35 +++++++++++++++++++
 addons/product_uos/views/product_uos_view.xml | 19 ++++++++++
 5 files changed, 87 insertions(+)
 create mode 100644 addons/product_uos/__init__.py
 create mode 100644 addons/product_uos/__openerp__.py
 create mode 100644 addons/product_uos/models/__init__.py
 create mode 100644 addons/product_uos/models/product_uos.py
 create mode 100644 addons/product_uos/views/product_uos_view.xml

diff --git a/addons/product_uos/__init__.py b/addons/product_uos/__init__.py
new file mode 100644
index 000000000000..81fc81d29c95
--- /dev/null
+++ b/addons/product_uos/__init__.py
@@ -0,0 +1,4 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+import models
diff --git a/addons/product_uos/__openerp__.py b/addons/product_uos/__openerp__.py
new file mode 100644
index 000000000000..4cc1cd2b6614
--- /dev/null
+++ b/addons/product_uos/__openerp__.py
@@ -0,0 +1,25 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+{
+    'name': 'Sale - Secondary UoM',
+    'version': '1.0',
+    'category': 'Sales Management',
+    'sequence': 14,
+    'summary': 'Unit of Sale',
+    'description': """
+Manage secondary units of sale
+==============================
+
+Sell products in one unit of measure that is different from the one
+you manage the inventory.
+    """,
+    'author': 'Odoo',
+    'website': 'https://www.odoo.com',
+    'depends': ['sale'],
+    'data': [
+        'views/product_uos_view.xml',
+    ],
+    'installable': True,
+    'auto_install': False,
+}
diff --git a/addons/product_uos/models/__init__.py b/addons/product_uos/models/__init__.py
new file mode 100644
index 000000000000..02d8fb5a5252
--- /dev/null
+++ b/addons/product_uos/models/__init__.py
@@ -0,0 +1,4 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+import product_uos
diff --git a/addons/product_uos/models/product_uos.py b/addons/product_uos/models/product_uos.py
new file mode 100644
index 000000000000..f39f79413caf
--- /dev/null
+++ b/addons/product_uos/models/product_uos.py
@@ -0,0 +1,35 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from openerp import api, fields, models
+import openerp.addons.decimal_precision as dp
+
+
+class ProductTemplate(models.Model):
+    _inherit = "product.template"
+
+    uos_id = fields.Many2one('product.uom', 'Unit of Sale',
+                             help='Specify a unit of measure here if invoicing is made in another'
+                             ' unit of measure than inventory. Keep empty to use the default unit of measure.')
+    uos_coeff = fields.Float('Unit of Measure -> UOS Coeff', digits_compute=dp.get_precision('Product Unit of Measure'),
+                             help='Coefficient to convert default Unit of Measure to Unit of Sale'
+                             ' uos = uom * coeff')
+
+
+class SaleOrderLine(models.Model):
+    _inherit = 'sale.order.line'
+
+    @api.one
+    def _set_uos(self):
+        if self.product_id.uos_coeff:
+            self.product_uom_qty = self.product_uos_qty / self.product_id.uos_coeff
+            self.product_uom = self.product_id.uom_id
+
+    @api.one
+    def _compute_uos(self):
+        self.product_uos_qty = self.product_uom_qty * self.product_id.uos_coeff
+
+    product_uos_qty = fields.Float(string='Quantity', digits_compute=dp.get_precision('Product Unit of Measure'),
+                                   compute='_compute_uos', inverse='_set_uos', readonly=False)
+    product_uos = fields.Many2one('product.uom', string='Unit of Measure', required=True,
+                                  related='product_id.uos_id', readonly=True)
diff --git a/addons/product_uos/views/product_uos_view.xml b/addons/product_uos/views/product_uos_view.xml
new file mode 100644
index 000000000000..f224b4d574e8
--- /dev/null
+++ b/addons/product_uos/views/product_uos_view.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="utf-8"?>
+<openerp>
+    <data>
+        <!-- Inherit Form view -->
+        <record id="product_template_form_view_inherit_product_uos" model="ir.ui.view">
+            <field name="name">product.template.common.form.inherit</field>
+            <field name="model">product.template</field>
+            <field name="inherit_id" ref="product.product_template_form_view"/>
+            <field name="arch" type="xml">
+                <xpath expr="//field[@name='name']" position="after">
+                    <group groups="product.group_uos" string="Unit of Measure">
+                        <field name="uos_id" options="{'no_open':True,'no_create':True}"/>
+                        <field name="uos_coeff" groups="base.group_no_one"/>
+                    </group>
+                </xpath>
+            </field>
+        </record>
+    </data>
+</openerp>
-- 
GitLab