# -*- coding: utf-8 -*-
# Copyright (C) 2014 GRAP (http://www.grap.coop)
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from datetime import datetime

from openerp.osv import fields
from openerp.osv.orm import Model
import openerp.addons.decimal_precision as dp


class product_product(Model):
    _inherit = 'product.product'

    # Column Section
    _columns = {
        'scale_group_id': fields.many2one(
            'product.scale.group', string='Scale Group'),
        'scale_sequence': fields.integer(
            string='Scale Sequence'),
        'scale_tare_weight': fields.float(
            digits_compute=dp.get_precision('Stock Weight'),
            string='Scale Tare Weight',  help="Set here Constant tare weight"
            " for the given product. This tare will be substracted when"
            " the product is weighted. Usefull only for weightable product.\n"
            "The tare is defined with kg uom."),
    }

    # View Section
#     def send_scale_create(self, cr, uid, ids, context=None):
#         for product in self.browse(cr, uid, ids, context=context):
#             self._send_to_scale_bizerba(
#                 cr, uid, 'create', product, context=context)
#         return True
# 
#     def send_scale_write(self, cr, uid, ids, context=None):
#         for product in self.browse(cr, uid, ids, context=context):
#             self._send_to_scale_bizerba(
#                 cr, uid, 'write', product, context=context)
#         return True
# 
#     def send_scale_unlink(self, cr, uid, ids, context=None):
#         for product in self.browse(cr, uid, ids, context=context):
#             self._send_to_scale_bizerba(
#                 cr, uid, 'unlink', product, context=context)
#         return True
# 
#     # Custom Section
#     def _send_to_scale_bizerba(self, cr, uid, action, product, context=None):
#         log_obj = self.pool['product.scale.log']
#         log_obj.create(cr, uid, {
#             'log_date': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
#             'scale_system_id': product.scale_group_id.scale_system_id.id,
#             'product_id': product.id,
#             'action': action,
#             }, context=context)
# 
#     def _check_vals_scale_bizerba(self, cr, uid, vals, product, context=None):
#         system = product.scale_group_id.scale_system_id
#         system_fields = [x.name for x in system.field_ids]
#         vals_fields = vals.keys()
#         return set(system_fields).intersection(vals_fields)
# 
#     # Overload Section
#     def create(self, cr, uid, vals, context=None):
#         send_to_scale = vals.get('scale_group_id', False)
#         res = super(product_product, self).create(
#             cr, uid, vals, context=context)
#         if send_to_scale:
#             product = self.browse(cr, uid, res, context=context)
#             self._send_to_scale_bizerba(
#                 cr, uid, 'create', product, context=context)
#         return res
# 
#     def write(self, cr, uid, ids, vals, context=None):
#         defered = {}
#         for product in self.browse(cr, uid, ids, context=context):
#             ignore = not product.scale_group_id\
#                 and 'scale_group_id' not in vals.keys()
#             if not ignore:
#                 if not product.scale_group_id:
#                     # (the product is new on this group)
#                     defered[product.id] = 'create'
#                 else:
#                     if vals.get('scale_group_id', False) and (
#                             vals.get('scale_group_id', False) !=
#                             product.scale_group_id):
#                         # (the product has moved from a group to another)
#                         # Remove from obsolete group
#                         self._send_to_scale_bizerba(
#                             cr, uid, 'unlink', product, context=context)
#                         # Create in the new group
#                         defered[product.id] = 'create'
#                     elif self._check_vals_scale_bizerba(
#                             cr, uid, vals, product, context=context):
#                         # Data related to the scale
#                         defered[product.id] = 'write'
# 
#         res = super(product_product, self).write(
#             cr, uid, ids, vals, context=context)
# 
#         for product_id, action in defered.iteritems():
#             product = self.browse(cr, uid, product_id, context=context)
#             self._send_to_scale_bizerba(
#                 cr, uid, action, product, context=context)
# 
#         return res
# 
#     def unlink(self, cr, uid, ids, context=None):
#         for product in self.browse(cr, uid, ids, context=context):
#             if product.scale_group_id:
#                 self._send_to_scale_bizerba(
#                     cr, uid, 'unlink', product, context=context)
#         return super(product_product, self).unlink(
#             cr, uid, ids, context=context)

@@ -1018,4036 +1018,4 @@
  %7D%0A%0A
-    # View Section%0A#     def send_scale_create(self, cr, uid, ids, context=None):%0A#         for product in self.browse(cr, uid, ids, context=context):%0A#             self._send_to_scale_bizerba(%0A#                 cr, uid, 'create', product, context=context)%0A#         return True%0A# %0A#     def send_scale_write(self, cr, uid, ids, context=None):%0A#         for product in self.browse(cr, uid, ids, context=context):%0A#             self._send_to_scale_bizerba(%0A#                 cr, uid, 'write', product, context=context)%0A#         return True%0A# %0A#     def send_scale_unlink(self, cr, uid, ids, context=None):%0A#         for product in self.browse(cr, uid, ids, context=context):%0A#             self._send_to_scale_bizerba(%0A#                 cr, uid, 'unlink', product, context=context)%0A#         return True%0A# %0A#     # Custom Section%0A#     def _send_to_scale_bizerba(self, cr, uid, action, product, context=None):%0A#         log_obj = self.pool%5B'product.scale.log'%5D%0A#         log_obj.create(cr, uid, %7B%0A#             'log_date': datetime.now().strftime('%25Y-%25m-%25d %25H:%25M:%25S'),%0A#             'scale_system_id': product.scale_group_id.scale_system_id.id,%0A#             'product_id': product.id,%0A#             'action': action,%0A#             %7D, context=context)%0A# %0A#     def _check_vals_scale_bizerba(self, cr, uid, vals, product, context=None):%0A#         system = product.scale_group_id.scale_system_id%0A#         system_fields = %5Bx.name for x in system.field_ids%5D%0A#         vals_fields = vals.keys()%0A#         return set(system_fields).intersection(vals_fields)%0A# %0A#     # Overload Section%0A#     def create(self, cr, uid, vals, context=None):%0A#         send_to_scale = vals.get('scale_group_id', False)%0A#         res = super(product_product, self).create(%0A#             cr, uid, vals, context=context)%0A#         if send_to_scale:%0A#             product = self.browse(cr, uid, res, context=context)%0A#             self._send_to_scale_bizerba(%0A#                 cr, uid, 'create', product, context=context)%0A#         return res%0A# %0A#     def write(self, cr, uid, ids, vals, context=None):%0A#         defered = %7B%7D%0A#         for product in self.browse(cr, uid, ids, context=context):%0A#             ignore = not product.scale_group_id%5C%0A#                 and 'scale_group_id' not in vals.keys()%0A#             if not ignore:%0A#                 if not product.scale_group_id:%0A#                     # (the product is new on this group)%0A#                     defered%5Bproduct.id%5D = 'create'%0A#                 else:%0A#                     if vals.get('scale_group_id', False) and (%0A#                             vals.get('scale_group_id', False) !=%0A#                             product.scale_group_id):%0A#                         # (the product has moved from a group to another)%0A#                         # Remove from obsolete group%0A#                         self._send_to_scale_bizerba(%0A#                             cr, uid, 'unlink', product, context=context)%0A#                         # Create in the new group%0A#                         defered%5Bproduct.id%5D = 'create'%0A#                     elif self._check_vals_scale_bizerba(%0A#                             cr, uid, vals, product, context=context):%0A#                         # Data related to the scale%0A#                         defered%5Bproduct.id%5D = 'write'%0A# %0A#         res = super(product_product, self).write(%0A#             cr, uid, ids, vals, context=context)%0A# %0A#         for product_id, action in defered.iteritems():%0A#             product = self.browse(cr, uid, product_id, context=context)%0A#             self._send_to_scale_bizerba(%0A#                 cr, uid, action, product, context=context)%0A# %0A#         return res%0A# %0A#     def unlink(self, cr, uid, ids, context=None):%0A#         for product in self.browse(cr, uid, ids, context=context):%0A#             if product.scale_group_id:%0A#                 self._send_to_scale_bizerba(%0A#                     cr, uid, 'unlink', product, context=context)%0A#         return super(product_product, self).unlink(%0A#             cr, uid, ids, context=context)%0A

remove commented code