2009-10-14 11:15:34 +00:00
# -*- coding: utf-8 -*-
2006-12-07 13:41:40 +00:00
##############################################################################
2009-11-13 05:41:16 +00:00
#
2009-05-18 15:24:44 +00:00
# OpenERP, Open Source Management Solution
2010-01-12 09:18:39 +00:00
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
2006-12-07 13:41:40 +00:00
#
2008-11-03 19:18:56 +00:00
# This program is free software: you can redistribute it and/or modify
2009-10-14 11:15:34 +00:00
# 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.
2006-12-07 13:41:40 +00:00
#
2008-11-03 19:18:56 +00:00
# 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
2009-10-14 11:15:34 +00:00
# GNU Affero General Public License for more details.
2006-12-07 13:41:40 +00:00
#
2009-10-14 11:15:34 +00:00
# You should have received a copy of the GNU Affero General Public License
2009-11-13 05:41:16 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2006-12-07 13:41:40 +00:00
#
##############################################################################
import math
2010-11-11 12:41:24 +00:00
import re
2013-12-06 10:31:50 +00:00
import time
2014-03-05 17:14:56 +00:00
from _common import ceiling
2012-12-06 14:56:32 +00:00
2015-02-25 16:46:38 +00:00
2015-06-16 15:41:33 +00:00
from openerp import api , tools , SUPERUSER_ID
2014-10-01 14:46:03 +00:00
from openerp . osv import osv , fields , expression
2012-12-06 14:56:32 +00:00
from openerp . tools . translate import _
2014-04-28 15:33:55 +00:00
from openerp . tools import DEFAULT_SERVER_DATETIME_FORMAT
2014-05-27 07:42:52 +00:00
import psycopg2
2012-12-06 14:56:32 +00:00
import openerp . addons . decimal_precision as dp
2014-11-25 17:13:41 +00:00
from openerp . tools . float_utils import float_round , float_compare
2006-12-28 09:44:56 +00:00
2012-07-20 15:24:21 +00:00
def ean_checksum ( eancode ) :
""" returns the checksum of an ean string of length 13, returns -1 if the string has the wrong length """
2014-01-27 09:18:56 +00:00
if len ( eancode ) != 13 :
2012-07-20 15:24:21 +00:00
return - 1
2010-11-19 13:02:08 +00:00
oddsum = 0
evensum = 0
total = 0
2010-11-22 11:09:17 +00:00
eanvalue = eancode
2010-11-19 13:02:08 +00:00
reversevalue = eanvalue [ : : - 1 ]
finalean = reversevalue [ 1 : ]
for i in range ( len ( finalean ) ) :
2012-07-26 09:53:15 +00:00
if i % 2 == 0 :
2010-11-19 13:02:08 +00:00
oddsum + = int ( finalean [ i ] )
else :
evensum + = int ( finalean [ i ] )
total = ( oddsum * 3 ) + evensum
2010-12-16 12:40:52 +00:00
check = int ( 10 - math . ceil ( total % 10.0 ) ) % 10
2012-07-20 15:24:21 +00:00
return check
2010-11-19 13:02:08 +00:00
2012-07-20 15:24:21 +00:00
def check_ean ( eancode ) :
""" returns True if eancode is a valid ean13 string, or null """
if not eancode :
return True
2014-01-27 09:18:56 +00:00
if len ( eancode ) != 13 :
2012-07-20 15:24:21 +00:00
return False
try :
int ( eancode )
except :
2010-11-19 13:02:08 +00:00
return False
2012-07-20 15:24:21 +00:00
return ean_checksum ( eancode ) == int ( eancode [ - 1 ] )
2012-07-20 17:00:12 +00:00
def sanitize_ean13 ( ean13 ) :
""" Creates and returns a valid ean13 from an invalid one """
if not ean13 :
return " 0000000000000 "
2012-07-25 12:51:15 +00:00
ean13 = re . sub ( " [A-Za-z] " , " 0 " , ean13 ) ;
ean13 = re . sub ( " [^0-9] " , " " , ean13 ) ;
2012-07-20 17:00:12 +00:00
ean13 = ean13 [ : 13 ]
if len ( ean13 ) < 13 :
ean13 = ean13 + ' 0 ' * ( 13 - len ( ean13 ) )
2012-07-25 12:51:15 +00:00
return ean13 [ : - 1 ] + str ( ean_checksum ( ean13 ) )
2012-07-20 17:00:12 +00:00
2006-12-07 13:41:40 +00:00
#----------------------------------------------------------
# UOM
#----------------------------------------------------------
class product_uom_categ ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = ' product.uom.categ '
_description = ' Product uom categ '
_columns = {
2014-01-26 21:05:06 +00:00
' name ' : fields . char ( ' Name ' , required = True , translate = True ) ,
2008-07-22 15:11:28 +00:00
}
2006-12-07 13:41:40 +00:00
class product_uom ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = ' product.uom '
_description = ' Product Unit of Measure '
2010-07-17 09:56:56 +00:00
def _compute_factor_inv ( self , factor ) :
2011-09-08 06:31:05 +00:00
return factor and ( 1.0 / factor ) or 0.0
2010-07-17 09:56:56 +00:00
2010-11-22 10:37:53 +00:00
def _factor_inv ( self , cursor , user , ids , name , arg , context = None ) :
2008-07-22 15:11:28 +00:00
res = { }
for uom in self . browse ( cursor , user , ids , context = context ) :
2010-07-17 09:56:56 +00:00
res [ uom . id ] = self . _compute_factor_inv ( uom . factor )
2008-07-22 15:11:28 +00:00
return res
2010-11-22 10:37:53 +00:00
def _factor_inv_write ( self , cursor , user , id , name , value , arg , context = None ) :
2010-07-17 09:56:56 +00:00
return self . write ( cursor , user , id , { ' factor ' : self . _compute_factor_inv ( value ) } , context = context )
2010-07-23 13:17:30 +00:00
2012-10-17 16:28:55 +00:00
def name_create ( self , cr , uid , name , context = None ) :
""" The UoM category and factor are required, so we ' ll have to add temporary values
for imported UoMs """
2014-12-23 15:58:31 +00:00
if not context :
context = { }
2012-10-17 16:28:55 +00:00
uom_categ = self . pool . get ( ' product.uom.categ ' )
2014-12-23 15:58:31 +00:00
values = { self . _rec_name : name , ' factor ' : 1 }
2012-10-17 16:28:55 +00:00
# look for the category based on the english name, i.e. no context on purpose!
# TODO: should find a way to have it translated but not created until actually used
2014-12-23 15:58:31 +00:00
if not context . get ( ' default_category_id ' ) :
categ_misc = ' Unsorted/Imported Units '
categ_id = uom_categ . search ( cr , uid , [ ( ' name ' , ' = ' , categ_misc ) ] )
if categ_id :
values [ ' category_id ' ] = categ_id [ 0 ]
else :
values [ ' category_id ' ] = uom_categ . name_create (
cr , uid , categ_misc , context = context ) [ 0 ]
uom_id = self . create ( cr , uid , values , context = context )
2012-10-17 16:28:55 +00:00
return self . name_get ( cr , uid , [ uom_id ] , context = context ) [ 0 ]
2010-11-19 13:48:01 +00:00
def create ( self , cr , uid , data , context = None ) :
2010-07-17 09:56:56 +00:00
if ' factor_inv ' in data :
2014-01-27 09:18:56 +00:00
if data [ ' factor_inv ' ] != 1 :
2010-07-17 09:56:56 +00:00
data [ ' factor ' ] = self . _compute_factor_inv ( data [ ' factor_inv ' ] )
del ( data [ ' factor_inv ' ] )
return super ( product_uom , self ) . create ( cr , uid , data , context )
2008-07-22 15:11:28 +00:00
2011-11-08 15:21:42 +00:00
_order = " name "
2008-07-22 15:11:28 +00:00
_columns = {
2014-01-26 21:05:06 +00:00
' name ' : fields . char ( ' Unit of Measure ' , required = True , translate = True ) ,
2015-01-27 10:36:52 +00:00
' category_id ' : fields . many2one ( ' product.uom.categ ' , ' Unit of Measure Category ' , required = True , ondelete = ' cascade ' ,
2012-10-23 13:02:42 +00:00
help = " Conversion between Units of Measure can only occur if they belong to the same category. The conversion will be made based on the ratios. " ) ,
2014-10-16 15:23:51 +00:00
' factor ' : fields . float ( ' Ratio ' , required = True , digits = 0 , # force NUMERIC with unlimited precision
2012-10-18 07:24:56 +00:00
help = ' How much bigger or smaller this unit is compared to the reference Unit of Measure for this category: \n ' \
2010-07-07 23:44:19 +00:00
' 1 * (reference unit) = ratio * (this unit) ' ) ,
2014-10-16 15:23:51 +00:00
' factor_inv ' : fields . function ( _factor_inv , digits = 0 , # force NUMERIC with unlimited precision
2010-06-26 15:15:23 +00:00
fnct_inv = _factor_inv_write ,
2013-03-12 11:36:33 +00:00
string = ' Bigger Ratio ' ,
2012-04-25 11:29:50 +00:00
help = ' How many times this Unit of Measure is bigger than the reference Unit of Measure in this category: \n ' \
2010-07-07 23:44:19 +00:00
' 1 * (this unit) = ratio * (reference unit) ' , required = True ) ,
2014-11-21 14:19:53 +00:00
' rounding ' : fields . float ( ' Rounding Precision ' , digits = 0 , required = True ,
2010-07-07 23:44:19 +00:00
help = " The computed quantity will be a multiple of this value. " \
2012-04-25 11:29:50 +00:00
" Use 1.0 for a Unit of Measure that cannot be further split, such as a piece. " ) ,
2010-07-07 23:44:19 +00:00
' active ' : fields . boolean ( ' Active ' , help = " By unchecking the active field you can disable a unit of measure without deleting it. " ) ,
2012-04-25 11:29:50 +00:00
' uom_type ' : fields . selection ( [ ( ' bigger ' , ' Bigger than the reference Unit of Measure ' ) ,
( ' reference ' , ' Reference Unit of Measure for this category ' ) ,
2012-10-08 07:16:25 +00:00
( ' smaller ' , ' Smaller than the reference Unit of Measure ' ) ] , ' Type ' , required = 1 ) ,
2008-07-22 15:11:28 +00:00
}
_defaults = {
2010-07-07 23:44:19 +00:00
' active ' : 1 ,
' rounding ' : 0.01 ,
2014-11-24 09:51:23 +00:00
' factor ' : 1 ,
2010-07-07 23:44:19 +00:00
' uom_type ' : ' reference ' ,
2014-12-02 13:28:54 +00:00
' factor ' : 1.0 ,
2008-07-22 15:11:28 +00:00
}
2009-09-24 10:46:21 +00:00
_sql_constraints = [
2011-09-18 13:53:10 +00:00
( ' factor_gt_zero ' , ' CHECK (factor!=0) ' , ' The conversion ratio for a unit of measure cannot be 0! ' )
2009-09-24 10:46:21 +00:00
]
2014-12-02 13:28:54 +00:00
def _compute_qty ( self , cr , uid , from_uom_id , qty , to_uom_id = False , round = True , rounding_method = ' UP ' ) :
2008-07-22 15:11:28 +00:00
if not from_uom_id or not qty or not to_uom_id :
return qty
uoms = self . browse ( cr , uid , [ from_uom_id , to_uom_id ] )
if uoms [ 0 ] . id == from_uom_id :
from_unit , to_unit = uoms [ 0 ] , uoms [ - 1 ]
else :
from_unit , to_unit = uoms [ - 1 ] , uoms [ 0 ]
2014-12-02 13:28:54 +00:00
return self . _compute_qty_obj ( cr , uid , from_unit , qty , to_unit , round = round , rounding_method = rounding_method )
2008-12-18 19:39:18 +00:00
2014-12-02 13:28:54 +00:00
def _compute_qty_obj ( self , cr , uid , from_unit , qty , to_unit , round = True , rounding_method = ' UP ' , context = None ) :
2010-11-11 12:41:24 +00:00
if context is None :
context = { }
2014-01-27 09:18:56 +00:00
if from_unit . category_id . id != to_unit . category_id . id :
2010-11-11 12:41:24 +00:00
if context . get ( ' raise-exception ' , True ) :
2012-08-07 11:31:37 +00:00
raise osv . except_osv ( _ ( ' Error! ' ) , _ ( ' Conversion from Product UoM %s to Default UoM %s is not possible as they both belong to different Category!. ' ) % ( from_unit . name , to_unit . name , ) )
2010-11-11 12:41:24 +00:00
else :
return qty
[FIX] product: remove unnecessary UoM rounding step, add missing test
Remove the intermediate rounding inside _compute_qty(), as it
is not necessary after rev. fa2f7b86 and has undesired side-effects.
An extra float_round() operation inside _compute_qty()
had been added at rev. 311c77bb to avoid a float representation
error in UoM factors that could bias the ceiling() operation
done as the last conversion step.
Example 1:
Dozen has a factor of 1/12, which was previously stored in the
database with a decimal accuracy of 12 significant decimal digits.
This meant the factor was exactly stored as 0.08333333333333.
When reading this back into a Python float, the precision was not
sufficient, and the UoM conversion of 1 Dozen to Units gave a
result of 12.00000000000047961...
After the final ceiling() operation to Unit's rounding, the
converted value ended up as 13.
This problem was initially solved using an extra rounding.
However at revision fa2f7b86 the decimal precision used to store
UoM factors was increased to preserve all significant digits.
This added the extra precision necessary to read the Dozen factor
back into an accurate float value of 1/12, and the conversion of
1 Dozen now gives 12.0 Units, even without the intermediate
rounding operation. (Works for other factor values too)
At the same time that extra rounding operation has undesired
side-effects, as it requires a fixed precision derived from
the rounding precisions of the UoMs. But there is no given precision
that would work in all cases for this intermediate value. It is
always possible to find a valid combination of UoM roundings
that breaks that intermediate step, e.g. by forcing integer
roundings.
Example 2:
Let Grams have a rounding precision set to 1 because no smaller
quantities are allowed, and Kilograms a rounding of 0.001 to allow
representing 1 Gram. (gram factor = 1000 and kilogram rounding = .001
by default)
If we try to convert 1234 Grams into Kilograms, the extra rounding
introduced in 311c77bb will cause a rounding of 1234.0/1000.0 at
the precision of Grams (1), which gives 1.0 as a result.
The net result of this conversion gives 1234.0 Gram = 1.0 Kilogram,
while the correct result (1.234 Kilogram) is perfectly compatible
with the UoM settings.
Similar errors could be triggered with various rounding settings, as
long as the intermediate rounding needs a finite precision.
Two extra tests have been added to cover Example 1 and Example 2.
--
Related to #2072, #1125, #1126, #2672
Closes #2495, #2498
2014-11-27 11:36:28 +00:00
amount = qty / from_unit . factor
2008-12-18 19:39:18 +00:00
if to_unit :
2013-06-29 09:13:28 +00:00
amount = amount * to_unit . factor
2013-05-28 08:56:54 +00:00
if round :
2014-12-02 13:28:54 +00:00
amount = float_round ( amount , precision_rounding = to_unit . rounding , rounding_method = rounding_method )
2008-07-22 15:11:28 +00:00
return amount
def _compute_price ( self , cr , uid , from_uom_id , price , to_uom_id = False ) :
2014-12-09 14:28:37 +00:00
if ( not from_uom_id or not price or not to_uom_id
or ( to_uom_id == from_uom_id ) ) :
2008-07-22 15:11:28 +00:00
return price
2014-04-28 17:38:42 +00:00
from_unit , to_unit = self . browse ( cr , uid , [ from_uom_id , to_uom_id ] )
2014-01-27 09:18:56 +00:00
if from_unit . category_id . id != to_unit . category_id . id :
2008-07-22 15:11:28 +00:00
return price
2010-06-26 15:15:23 +00:00
amount = price * from_unit . factor
2008-07-22 15:11:28 +00:00
if to_uom_id :
2010-12-09 06:35:03 +00:00
amount = amount / to_unit . factor
2008-07-22 15:11:28 +00:00
return amount
2010-07-07 23:44:19 +00:00
def onchange_type ( self , cursor , user , ids , value ) :
if value == ' reference ' :
2010-06-26 15:15:23 +00:00
return { ' value ' : { ' factor ' : 1 , ' factor_inv ' : 1 } }
return { }
2012-08-03 17:26:36 +00:00
2011-11-03 10:45:21 +00:00
def write ( self , cr , uid , ids , vals , context = None ) :
2014-06-03 14:13:54 +00:00
if isinstance ( ids , ( int , long ) ) :
ids = [ ids ]
2011-11-03 10:45:21 +00:00
if ' category_id ' in vals :
for uom in self . browse ( cr , uid , ids , context = context ) :
2011-11-28 12:19:40 +00:00
if uom . category_id . id != vals [ ' category_id ' ] :
2012-08-07 11:34:14 +00:00
raise osv . except_osv ( _ ( ' Warning! ' ) , _ ( " Cannot change the category of existing Unit of Measure ' %s ' . " ) % ( uom . name , ) )
2011-11-03 10:45:21 +00:00
return super ( product_uom , self ) . write ( cr , uid , ids , vals , context = context )
2008-01-04 08:04:47 +00:00
2006-12-07 13:41:40 +00:00
class product_ul ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = " product.ul "
2014-03-20 14:09:59 +00:00
_description = " Logistic Unit "
2008-07-22 15:11:28 +00:00
_columns = {
2014-01-26 21:05:06 +00:00
' name ' : fields . char ( ' Name ' , select = True , required = True , translate = True ) ,
2009-12-21 09:50:54 +00:00
' type ' : fields . selection ( [ ( ' unit ' , ' Unit ' ) , ( ' pack ' , ' Pack ' ) , ( ' box ' , ' Box ' ) , ( ' pallet ' , ' Pallet ' ) ] , ' Type ' , required = True ) ,
2014-03-20 12:13:35 +00:00
' height ' : fields . float ( ' Height ' , help = ' The height of the package ' ) ,
' width ' : fields . float ( ' Width ' , help = ' The width of the package ' ) ,
' length ' : fields . float ( ' Length ' , help = ' The length of the package ' ) ,
' weight ' : fields . float ( ' Empty Package Weight ' ) ,
2008-07-22 15:11:28 +00:00
}
2006-12-07 13:41:40 +00:00
#----------------------------------------------------------
# Categories
#----------------------------------------------------------
class product_category ( osv . osv ) :
2015-06-16 15:41:33 +00:00
@api.multi
def name_get ( self ) :
def get_names ( cat ) :
""" Return the list [cat.name, cat.parent_id.name, ...] """
res = [ ]
while cat :
res . append ( cat . name )
cat = cat . parent_id
return res
return [ ( cat . id , " / " . join ( reversed ( get_names ( cat ) ) ) ) for cat in self ]
2008-07-22 15:11:28 +00:00
2013-11-08 16:52:17 +00:00
def name_search ( self , cr , uid , name , args = None , operator = ' ilike ' , context = None , limit = 100 ) :
if not args :
args = [ ]
if not context :
context = { }
if name :
# Be sure name_search is symetric to name_get
2015-02-25 15:02:45 +00:00
categories = name . split ( ' / ' )
parents = list ( categories )
child = parents . pop ( )
domain = [ ( ' name ' , operator , child ) ]
if parents :
names_ids = self . name_search ( cr , uid , ' / ' . join ( parents ) , args = args , operator = ' ilike ' , context = context , limit = limit )
category_ids = [ name_id [ 0 ] for name_id in names_ids ]
if operator in expression . NEGATIVE_TERM_OPERATORS :
category_ids = self . search ( cr , uid , [ ( ' id ' , ' not in ' , category_ids ) ] )
domain = expression . OR ( [ [ ( ' parent_id ' , ' in ' , category_ids ) ] , domain ] )
else :
domain = expression . AND ( [ [ ( ' parent_id ' , ' in ' , category_ids ) ] , domain ] )
for i in range ( 1 , len ( categories ) ) :
domain = [ [ ( ' name ' , operator , ' / ' . join ( categories [ - 1 - i : ] ) ) ] , domain ]
if operator in expression . NEGATIVE_TERM_OPERATORS :
domain = expression . AND ( domain )
else :
domain = expression . OR ( domain )
ids = self . search ( cr , uid , expression . AND ( [ domain , args ] ) , limit = limit , context = context )
2013-11-08 16:52:17 +00:00
else :
ids = self . search ( cr , uid , args , limit = limit , context = context )
return self . name_get ( cr , uid , ids , context )
2010-11-22 10:37:53 +00:00
def _name_get_fnc ( self , cr , uid , ids , prop , unknow_none , context = None ) :
res = self . name_get ( cr , uid , ids , context = context )
2008-07-22 15:11:28 +00:00
return dict ( res )
_name = " product.category "
_description = " Product Category "
_columns = {
2014-01-26 21:05:06 +00:00
' name ' : fields . char ( ' Name ' , required = True , translate = True , select = True ) ,
2012-01-05 09:58:38 +00:00
' complete_name ' : fields . function ( _name_get_fnc , type = " char " , string = ' Name ' ) ,
2012-01-06 09:29:30 +00:00
' parent_id ' : fields . many2one ( ' product.category ' , ' Parent Category ' , select = True , ondelete = ' cascade ' ) ,
2009-01-27 11:15:46 +00:00
' child_id ' : fields . one2many ( ' product.category ' , ' parent_id ' , string = ' Child Categories ' ) ,
2011-12-09 06:03:08 +00:00
' sequence ' : fields . integer ( ' Sequence ' , select = True , help = " Gives the sequence order when displaying a list of product categories. " ) ,
2012-10-18 08:51:42 +00:00
' type ' : fields . selection ( [ ( ' view ' , ' View ' ) , ( ' normal ' , ' Normal ' ) ] , ' Category Type ' , help = " A category of the view type is a virtual category that can be used as the parent of another category to create a hierarchical structure. " ) ,
2012-01-05 09:58:38 +00:00
' parent_left ' : fields . integer ( ' Left Parent ' , select = 1 ) ,
' parent_right ' : fields . integer ( ' Right Parent ' , select = 1 ) ,
2008-07-22 15:11:28 +00:00
}
2010-07-23 13:17:30 +00:00
2010-05-03 08:59:51 +00:00
2010-03-17 06:11:43 +00:00
_defaults = {
2014-05-28 16:30:03 +00:00
' type ' : ' normal ' ,
2010-03-17 06:11:43 +00:00
}
2010-05-03 08:59:51 +00:00
2012-01-05 09:58:38 +00:00
_parent_name = " parent_id "
_parent_store = True
2012-01-05 11:22:47 +00:00
_parent_order = ' sequence, name '
2012-01-05 09:58:38 +00:00
_order = ' parent_left '
2012-08-03 17:26:36 +00:00
2008-07-22 15:11:28 +00:00
_constraints = [
2014-01-26 21:09:21 +00:00
( osv . osv . _check_recursion , ' Error ! You cannot create recursive categories. ' , [ ' parent_id ' ] )
2008-07-22 15:11:28 +00:00
]
2006-12-07 13:41:40 +00:00
2014-04-28 15:33:55 +00:00
class produce_price_history ( osv . osv ) :
2013-12-06 16:30:54 +00:00
"""
Keep track of the ` ` product . template ` ` standard prices as they are changed .
"""
2014-04-28 15:33:55 +00:00
_name = ' product.price.history '
2013-12-06 16:30:54 +00:00
_rec_name = ' datetime '
_order = ' datetime desc '
_columns = {
' company_id ' : fields . many2one ( ' res.company ' , required = True ) ,
2014-04-14 08:48:21 +00:00
' product_template_id ' : fields . many2one ( ' product.template ' , ' Product Template ' , required = True , ondelete = ' cascade ' ) ,
2013-12-06 16:30:54 +00:00
' datetime ' : fields . datetime ( ' Historization Time ' ) ,
' cost ' : fields . float ( ' Historized Cost ' ) ,
}
def _get_default_company ( self , cr , uid , context = None ) :
if ' force_company ' in context :
return context [ ' force_company ' ]
else :
company = self . pool [ ' res.users ' ] . browse ( cr , uid , uid ,
context = context ) . company_id
return company . id if company else False
_defaults = {
' datetime ' : fields . datetime . now ,
' company_id ' : _get_default_company ,
}
2014-05-27 07:42:52 +00:00
#----------------------------------------------------------
2014-05-30 08:00:54 +00:00
# Product Attributes
2014-05-27 07:42:52 +00:00
#----------------------------------------------------------
class product_attribute ( osv . osv ) :
_name = " product.attribute "
_description = " Product Attribute "
_columns = {
' name ' : fields . char ( ' Name ' , translate = True , required = True ) ,
2014-07-06 14:44:26 +00:00
' value_ids ' : fields . one2many ( ' product.attribute.value ' , ' attribute_id ' , ' Values ' , copy = True ) ,
2014-05-27 07:42:52 +00:00
}
2013-08-28 15:16:58 +00:00
2014-05-27 07:42:52 +00:00
class product_attribute_value ( osv . osv ) :
_name = " product.attribute.value "
_order = ' sequence '
def _get_price_extra ( self , cr , uid , ids , name , args , context = None ) :
result = dict . fromkeys ( ids , 0 )
if not context . get ( ' active_id ' ) :
return result
2013-08-28 15:16:58 +00:00
for obj in self . browse ( cr , uid , ids , context = context ) :
2014-05-27 07:42:52 +00:00
for price_id in obj . price_ids :
if price_id . product_tmpl_id . id == context . get ( ' active_id ' ) :
result [ obj . id ] = price_id . price_extra
break
2013-08-28 15:16:58 +00:00
return result
2014-05-27 07:42:52 +00:00
def _set_price_extra ( self , cr , uid , id , name , value , args , context = None ) :
2014-06-03 12:19:51 +00:00
if context is None :
context = { }
2014-05-27 07:42:52 +00:00
if ' active_id ' not in context :
return None
p_obj = self . pool [ ' product.attribute.price ' ]
p_ids = p_obj . search ( cr , uid , [ ( ' value_id ' , ' = ' , id ) , ( ' product_tmpl_id ' , ' = ' , context [ ' active_id ' ] ) ] , context = context )
if p_ids :
p_obj . write ( cr , uid , p_ids , { ' price_extra ' : value } , context = context )
else :
2014-06-03 12:19:51 +00:00
p_obj . create ( cr , uid , {
2014-05-27 07:42:52 +00:00
' product_tmpl_id ' : context [ ' active_id ' ] ,
' value_id ' : id ,
' price_extra ' : value ,
} , context = context )
2013-08-28 15:16:58 +00:00
2014-11-06 17:44:19 +00:00
def name_get ( self , cr , uid , ids , context = None ) :
if context and not context . get ( ' show_attribute ' , True ) :
return super ( product_attribute_value , self ) . name_get ( cr , uid , ids , context = context )
res = [ ]
for value in self . browse ( cr , uid , ids , context = context ) :
res . append ( [ value . id , " %s : %s " % ( value . attribute_id . name , value . name ) ] )
return res
2013-08-28 15:16:58 +00:00
_columns = {
2014-05-27 07:42:52 +00:00
' sequence ' : fields . integer ( ' Sequence ' , help = " Determine the display order " ) ,
' name ' : fields . char ( ' Value ' , translate = True , required = True ) ,
2014-07-08 07:12:17 +00:00
' attribute_id ' : fields . many2one ( ' product.attribute ' , ' Attribute ' , required = True , ondelete = ' cascade ' ) ,
2014-05-27 07:42:52 +00:00
' product_ids ' : fields . many2many ( ' product.product ' , id1 = ' att_id ' , id2 = ' prod_id ' , string = ' Variants ' , readonly = True ) ,
2014-05-30 08:00:54 +00:00
' price_extra ' : fields . function ( _get_price_extra , type = ' float ' , string = ' Attribute Price Extra ' ,
2014-05-27 07:42:52 +00:00
fnct_inv = _set_price_extra ,
digits_compute = dp . get_precision ( ' Product Price ' ) ,
help = " Price Extra: Extra price for the variant with this attribute value on sale price. eg. 200 price extra, 1000 + 200 = 1200. " ) ,
2014-05-30 08:00:54 +00:00
' price_ids ' : fields . one2many ( ' product.attribute.price ' , ' value_id ' , string = ' Attribute Prices ' , readonly = True ) ,
2014-05-27 07:42:52 +00:00
}
_sql_constraints = [
( ' value_company_uniq ' , ' unique (name,attribute_id) ' , ' This attribute value already exists ! ' )
]
_defaults = {
2014-05-28 16:30:03 +00:00
' price_extra ' : 0.0 ,
2014-05-27 07:42:52 +00:00
}
2014-07-08 07:12:17 +00:00
def unlink ( self , cr , uid , ids , context = None ) :
ctx = dict ( context or { } , active_test = False )
product_ids = self . pool [ ' product.product ' ] . search ( cr , uid , [ ( ' attribute_value_ids ' , ' in ' , ids ) ] , context = ctx )
if product_ids :
raise osv . except_osv ( _ ( ' Integrity Error! ' ) , _ ( ' The operation cannot be completed: \n You trying to delete an attribute value with a reference on a product variant. ' ) )
return super ( product_attribute_value , self ) . unlink ( cr , uid , ids , context = context )
2014-05-27 07:42:52 +00:00
class product_attribute_price ( osv . osv ) :
_name = " product.attribute.price "
_columns = {
2014-07-08 07:12:17 +00:00
' product_tmpl_id ' : fields . many2one ( ' product.template ' , ' Product Template ' , required = True , ondelete = ' cascade ' ) ,
' value_id ' : fields . many2one ( ' product.attribute.value ' , ' Product Attribute Value ' , required = True , ondelete = ' cascade ' ) ,
2014-05-27 07:42:52 +00:00
' price_extra ' : fields . float ( ' Price Extra ' , digits_compute = dp . get_precision ( ' Product Price ' ) ) ,
}
class product_attribute_line ( osv . osv ) :
_name = " product.attribute.line "
2014-07-23 11:16:59 +00:00
_rec_name = ' attribute_id '
2014-05-27 07:42:52 +00:00
_columns = {
2014-07-08 07:12:17 +00:00
' product_tmpl_id ' : fields . many2one ( ' product.template ' , ' Product Template ' , required = True , ondelete = ' cascade ' ) ,
' attribute_id ' : fields . many2one ( ' product.attribute ' , ' Attribute ' , required = True , ondelete = ' restrict ' ) ,
2014-05-27 07:42:52 +00:00
' value_ids ' : fields . many2many ( ' product.attribute.value ' , id1 = ' line_id ' , id2 = ' val_id ' , string = ' Product Attribute Value ' ) ,
2013-08-28 15:16:58 +00:00
}
2006-12-07 13:41:40 +00:00
#----------------------------------------------------------
# Products
#----------------------------------------------------------
class product_template ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = " product.template "
2014-01-06 13:57:27 +00:00
_inherit = [ ' mail.thread ' ]
2008-07-22 15:11:28 +00:00
_description = " Product Template "
2014-09-09 13:02:18 +00:00
_order = " name "
2012-01-12 14:44:56 +00:00
2013-08-20 14:12:03 +00:00
def _get_image ( self , cr , uid , ids , name , args , context = None ) :
result = dict . fromkeys ( ids , False )
for obj in self . browse ( cr , uid , ids , context = context ) :
result [ obj . id ] = tools . image_get_resized_images ( obj . image , avoid_resize_medium = True )
return result
def _set_image ( self , cr , uid , id , name , value , args , context = None ) :
return self . write ( cr , uid , [ id ] , { ' image ' : tools . image_resize_image_big ( value ) } , context = context )
2014-05-27 07:42:52 +00:00
def _is_product_variant ( self , cr , uid , ids , name , arg , context = None ) :
2014-07-06 14:44:26 +00:00
return self . _is_product_variant_impl ( cr , uid , ids , name , arg , context = context )
2014-05-28 16:30:03 +00:00
2014-07-06 14:44:26 +00:00
def _is_product_variant_impl ( self , cr , uid , ids , name , arg , context = None ) :
2014-07-29 13:20:25 +00:00
return dict . fromkeys ( ids , False )
2014-05-28 16:30:03 +00:00
2014-05-27 07:42:52 +00:00
def _product_template_price ( self , cr , uid , ids , name , arg , context = None ) :
plobj = self . pool . get ( ' product.pricelist ' )
res = { }
quantity = context . get ( ' quantity ' ) or 1.0
pricelist = context . get ( ' pricelist ' , False )
partner = context . get ( ' partner ' , False )
if pricelist :
# Support context pricelists specified as display_name or ID for compatibility
if isinstance ( pricelist , basestring ) :
pricelist_ids = plobj . name_search (
cr , uid , pricelist , operator = ' = ' , context = context , limit = 1 )
pricelist = pricelist_ids [ 0 ] [ 0 ] if pricelist_ids else pricelist
if isinstance ( pricelist , ( int , long ) ) :
products = self . browse ( cr , uid , ids , context = context )
qtys = map ( lambda x : ( x , quantity , partner ) , products )
pl = plobj . browse ( cr , uid , pricelist , context = context )
price = plobj . _price_get_multi ( cr , uid , pl , qtys , context = context )
for id in ids :
res [ id ] = price . get ( id , 0.0 )
for id in ids :
res . setdefault ( id , 0.0 )
return res
2014-05-05 12:07:43 +00:00
def get_history_price ( self , cr , uid , product_tmpl , company_id , date = None , context = None ) :
if context is None :
context = { }
if date is None :
date = time . strftime ( DEFAULT_SERVER_DATETIME_FORMAT )
price_history_obj = self . pool . get ( ' product.price.history ' )
2014-05-09 07:33:15 +00:00
history_ids = price_history_obj . search ( cr , uid , [ ( ' company_id ' , ' = ' , company_id ) , ( ' product_template_id ' , ' = ' , product_tmpl ) , ( ' datetime ' , ' <= ' , date ) ] , limit = 1 )
2014-05-05 12:07:43 +00:00
if history_ids :
return price_history_obj . read ( cr , uid , history_ids [ 0 ] , [ ' cost ' ] , context = context ) [ ' cost ' ]
return 0.0
def _set_standard_price ( self , cr , uid , product_tmpl_id , value , context = None ) :
''' Store the standard price change in order to be able to retrieve the cost of a product template for a given date '''
2014-06-02 13:48:24 +00:00
if context is None :
context = { }
2014-05-05 12:07:43 +00:00
price_history_obj = self . pool [ ' product.price.history ' ]
user_company = self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid , context = context ) . company_id . id
company_id = context . get ( ' force_company ' , user_company )
price_history_obj . create ( cr , uid , {
' product_template_id ' : product_tmpl_id ,
' cost ' : value ,
' company_id ' : company_id ,
} , context = context )
2014-05-27 07:42:52 +00:00
def _get_product_variant_count ( self , cr , uid , ids , name , arg , context = None ) :
res = { }
2015-02-02 15:07:41 +00:00
for product in self . browse ( cr , uid , ids , context = context ) :
2014-05-27 07:42:52 +00:00
res [ product . id ] = len ( product . product_variant_ids )
return res
2008-07-22 15:11:28 +00:00
_columns = {
2014-01-26 21:05:06 +00:00
' name ' : fields . char ( ' Name ' , required = True , translate = True , select = True ) ,
2012-10-23 13:02:42 +00:00
' product_manager ' : fields . many2one ( ' res.users ' , ' Product Manager ' ) ,
2013-01-18 11:33:09 +00:00
' description ' : fields . text ( ' Description ' , translate = True ,
help = " A precise description of the Product, used only for internal information purposes. " ) ,
' description_purchase ' : fields . text ( ' Purchase Description ' , translate = True ,
help = " A description of the Product that you want to communicate to your suppliers. "
2014-07-16 08:23:57 +00:00
" This description will be copied to every Purchase Order, Receipt and Supplier Invoice/Refund. " ) ,
2013-01-24 14:41:56 +00:00
' description_sale ' : fields . text ( ' Sale Description ' , translate = True ,
help = " A description of the Product that you want to communicate to your customers. "
" This description will be copied to every Sale Order, Delivery Order and Customer Invoice/Refund " ) ,
2013-10-29 07:48:15 +00:00
' type ' : fields . selection ( [ ( ' consu ' , ' Consumable ' ) , ( ' service ' , ' Service ' ) ] , ' Product Type ' , required = True , help = " Consumable are product where you don ' t manage stock, a service is a non-material product provided by a company or an individual. " ) ,
2009-09-24 10:46:21 +00:00
' rental ' : fields . boolean ( ' Can be Rent ' ) ,
2014-05-27 07:42:52 +00:00
' categ_id ' : fields . many2one ( ' product.category ' , ' Internal Category ' , required = True , change_default = True , domain = " [( ' type ' , ' = ' , ' normal ' )] " , help = " Select category for the current product " ) ,
' price ' : fields . function ( _product_template_price , type = ' float ' , string = ' Price ' , digits_compute = dp . get_precision ( ' Product Price ' ) ) ,
2012-10-23 13:02:42 +00:00
' list_price ' : fields . float ( ' Sale Price ' , digits_compute = dp . get_precision ( ' Product Price ' ) , help = " Base price to compute the customer price. Sometimes called the catalog price. " ) ,
2014-05-27 07:42:52 +00:00
' lst_price ' : fields . related ( ' list_price ' , type = " float " , string = ' Public Price ' , digits_compute = dp . get_precision ( ' Product Price ' ) ) ,
2013-06-03 08:58:18 +00:00
' standard_price ' : fields . property ( type = ' float ' , digits_compute = dp . get_precision ( ' Product Price ' ) ,
2015-01-06 16:45:44 +00:00
help = " Cost price of the product template used for standard stock valuation in accounting and used as a base price on purchase orders. "
" Expressed in the default unit of measure of the product. " ,
2014-02-03 17:30:00 +00:00
groups = " base.group_user " , string = " Cost Price " ) ,
2009-01-15 15:00:39 +00:00
' volume ' : fields . float ( ' Volume ' , help = " The volume in m3. " ) ,
2012-04-23 10:20:03 +00:00
' weight ' : fields . float ( ' Gross Weight ' , digits_compute = dp . get_precision ( ' Stock Weight ' ) , help = " The gross weight in Kg. " ) ,
' weight_net ' : fields . float ( ' Net Weight ' , digits_compute = dp . get_precision ( ' Stock Weight ' ) , help = " The net weight in Kg. " ) ,
2012-08-31 15:04:19 +00:00
' warranty ' : fields . float ( ' Warranty ' ) ,
2012-10-31 04:50:27 +00:00
' sale_ok ' : fields . boolean ( ' Can be Sold ' , help = " Specify if the product can be selected in a sales order line. " ) ,
2014-05-27 07:42:52 +00:00
' pricelist_id ' : fields . dummy ( string = ' Pricelist ' , relation = ' product.pricelist ' , type = ' many2one ' ) ,
2010-10-09 16:01:43 +00:00
' state ' : fields . selection ( [ ( ' ' , ' ' ) ,
( ' draft ' , ' In Development ' ) ,
( ' sellable ' , ' Normal ' ) ,
( ' end ' , ' End of Lifecycle ' ) ,
2012-10-23 13:02:42 +00:00
( ' obsolete ' , ' Obsolete ' ) ] , ' Status ' ) ,
2012-09-02 13:14:13 +00:00
' uom_id ' : fields . many2one ( ' product.uom ' , ' Unit of Measure ' , required = True , help = " Default Unit of Measure used for all stock operation. " ) ,
2010-08-30 10:58:45 +00:00
' uom_po_id ' : fields . many2one ( ' product.uom ' , ' Purchase Unit of Measure ' , required = True , help = " Default Unit of Measure used for purchase orders. It must be in the same category than the default unit of measure. " ) ,
2008-07-22 15:11:28 +00:00
' uos_id ' : fields . many2one ( ' product.uom ' , ' Unit of Sale ' ,
2013-04-30 14:04:45 +00:00
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. ' ) ,
2012-04-25 11:29:50 +00:00
' uos_coeff ' : fields . float ( ' Unit of Measure -> UOS Coeff ' , digits_compute = dp . get_precision ( ' Product UoS ' ) ,
2012-10-31 04:50:27 +00:00
help = ' Coefficient to convert default Unit of Measure to Unit of Sale \n '
2010-05-17 15:10:35 +00:00
' uos = uom * coeff ' ) ,
2012-09-27 06:08:59 +00:00
' mes_type ' : fields . selection ( ( ( ' fixed ' , ' Fixed ' ) , ( ' variable ' , ' Variable ' ) ) , ' Measure Type ' ) ,
2012-04-23 10:20:03 +00:00
' company_id ' : fields . many2one ( ' res.company ' , ' Company ' , select = 1 ) ,
2013-08-20 14:12:03 +00:00
# image: all image fields are base64 encoded and PIL-supported
' image ' : fields . binary ( " Image " ,
help = " This field holds the image used as image for the product, limited to 1024x1024px. " ) ,
' image_medium ' : fields . function ( _get_image , fnct_inv = _set_image ,
2014-05-27 07:42:52 +00:00
string = " Medium-sized image " , type = " binary " , multi = " _get_image " ,
2013-08-20 14:12:03 +00:00
store = {
2014-01-09 15:26:21 +00:00
' product.template ' : ( lambda self , cr , uid , ids , c = { } : ids , [ ' image ' ] , 10 ) ,
2013-08-20 14:12:03 +00:00
} ,
help = " Medium-sized image of the product. It is automatically " \
" resized as a 128x128px image, with aspect ratio preserved, " \
" only when the image exceeds one of those sizes. Use this field in form views or some kanban views. " ) ,
' image_small ' : fields . function ( _get_image , fnct_inv = _set_image ,
string = " Small-sized image " , type = " binary " , multi = " _get_image " ,
store = {
2014-01-09 15:26:21 +00:00
' product.template ' : ( lambda self , cr , uid , ids , c = { } : ids , [ ' image ' ] , 10 ) ,
2013-08-20 14:12:03 +00:00
} ,
help = " Small-sized image of the product. It is automatically " \
" resized as a 64x64px image, with aspect ratio preserved. " \
" Use this field anywhere a small image is required. " ) ,
2014-06-03 19:45:21 +00:00
' packaging_ids ' : fields . one2many (
' product.packaging ' , ' product_tmpl_id ' , ' Logistical Units ' ,
help = " Gives the different ways to package the same product. This has no impact on "
" the picking order and is mainly used if you use the EDI module. " ) ,
2014-05-27 07:42:52 +00:00
' seller_ids ' : fields . one2many ( ' product.supplierinfo ' , ' product_tmpl_id ' , ' Supplier ' ) ,
' seller_delay ' : fields . related ( ' seller_ids ' , ' delay ' , type = ' integer ' , string = ' Supplier Lead Time ' ,
2014-07-16 08:23:57 +00:00
help = " This is the average delay in days between the purchase order confirmation and the receipts for this product and for the default supplier. It is used by the scheduler to order requests based on reordering delays. " ) ,
2014-05-27 07:42:52 +00:00
' seller_qty ' : fields . related ( ' seller_ids ' , ' qty ' , type = ' float ' , string = ' Supplier Quantity ' ,
help = " This is minimum quantity to purchase from Main Supplier. " ) ,
' seller_id ' : fields . related ( ' seller_ids ' , ' name ' , type = ' many2one ' , relation = ' res.partner ' , string = ' Main Supplier ' ,
help = " Main Supplier who has highest priority in Supplier List. " ) ,
' active ' : fields . boolean ( ' Active ' , help = " If unchecked, it will allow you to hide the product without removing it. " ) ,
' color ' : fields . integer ( ' Color Index ' ) ,
2014-09-29 15:12:49 +00:00
' is_product_variant ' : fields . function ( _is_product_variant , type = ' boolean ' , string = ' Is product variant ' ) ,
2014-05-27 07:42:52 +00:00
2014-05-30 08:00:54 +00:00
' attribute_line_ids ' : fields . one2many ( ' product.attribute.line ' , ' product_tmpl_id ' , ' Product Attributes ' ) ,
2014-05-27 07:42:52 +00:00
' product_variant_ids ' : fields . one2many ( ' product.product ' , ' product_tmpl_id ' , ' Products ' , required = True ) ,
2014-05-30 08:00:54 +00:00
' product_variant_count ' : fields . function ( _get_product_variant_count , type = ' integer ' , string = ' # of Product Variants ' ) ,
2014-05-27 07:42:52 +00:00
# related to display product product information if is_product_variant
' ean13 ' : fields . related ( ' product_variant_ids ' , ' ean13 ' , type = ' char ' , string = ' EAN13 Barcode ' ) ,
' default_code ' : fields . related ( ' product_variant_ids ' , ' default_code ' , type = ' char ' , string = ' Internal Reference ' ) ,
2008-07-22 15:11:28 +00:00
}
2009-05-18 15:24:44 +00:00
2014-05-27 07:42:52 +00:00
def _price_get_list_price ( self , product ) :
return 0.0
def _price_get ( self , cr , uid , products , ptype = ' list_price ' , context = None ) :
if context is None :
context = { }
if ' currency_id ' in context :
pricetype_obj = self . pool . get ( ' product.price.type ' )
price_type_id = pricetype_obj . search ( cr , uid , [ ( ' field ' , ' = ' , ptype ) ] ) [ 0 ]
price_type_currency_id = pricetype_obj . browse ( cr , uid , price_type_id ) . currency_id . id
res = { }
product_uom_obj = self . pool . get ( ' product.uom ' )
for product in products :
2014-10-14 09:59:57 +00:00
# standard_price field can only be seen by users in base.group_user
# Thus, in order to compute the sale price from the cost price for users not in this group
# We fetch the standard price as the superuser
if ptype != ' standard_price ' :
res [ product . id ] = product [ ptype ] or 0.0
else :
2014-11-04 12:04:50 +00:00
company_id = product . env . user . company_id . id
2014-11-04 11:30:01 +00:00
product = product . with_context ( force_company = company_id )
res [ product . id ] = res [ product . id ] = product . sudo ( ) [ ptype ]
2014-05-27 07:42:52 +00:00
if ptype == ' list_price ' :
res [ product . id ] + = product . _name == " product.product " and product . price_extra or 0.0
if ' uom ' in context :
uom = product . uom_id or product . uos_id
res [ product . id ] = product_uom_obj . _compute_price ( cr , uid ,
uom . id , res [ product . id ] , context [ ' uom ' ] )
# Convert from price_type currency to asked one
if ' currency_id ' in context :
# Take the price_type currency from the product field
# This is right cause a field cannot be in more than one currency
res [ product . id ] = self . pool . get ( ' res.currency ' ) . compute ( cr , uid , price_type_currency_id ,
context [ ' currency_id ' ] , res [ product . id ] , context = context )
return res
2008-07-22 15:11:28 +00:00
def _get_uom_id ( self , cr , uid , * args ) :
2014-05-27 07:42:52 +00:00
return self . pool [ " product.uom " ] . search ( cr , uid , [ ] , limit = 1 , order = ' id ' ) [ 0 ]
2008-07-22 15:11:28 +00:00
2010-11-19 13:48:01 +00:00
def _default_category ( self , cr , uid , context = None ) :
2010-11-23 07:05:05 +00:00
if context is None :
2010-11-22 10:37:53 +00:00
context = { }
2008-10-28 16:23:02 +00:00
if ' categ_id ' in context and context [ ' categ_id ' ] :
return context [ ' categ_id ' ]
2010-10-10 19:30:31 +00:00
md = self . pool . get ( ' ir.model.data ' )
2011-04-27 06:00:52 +00:00
res = False
try :
2012-07-26 13:17:00 +00:00
res = md . get_object_reference ( cr , uid , ' product ' , ' product_category_all ' ) [ 1 ]
2011-04-27 06:00:52 +00:00
except ValueError :
res = False
2011-04-27 06:33:21 +00:00
return res
2008-10-28 16:23:02 +00:00
2015-05-05 16:20:35 +00:00
def onchange_type ( self , cr , uid , ids , type ) :
return { }
2012-03-05 18:40:03 +00:00
def onchange_uom ( self , cursor , user , ids , uom_id , uom_po_id ) :
2010-10-16 23:13:43 +00:00
if uom_id :
return { ' value ' : { ' uom_po_id ' : uom_id } }
2011-11-14 21:33:19 +00:00
return { }
2009-01-15 16:24:45 +00:00
2014-05-27 07:42:52 +00:00
def create_variant_ids ( self , cr , uid , ids , context = None ) :
product_obj = self . pool . get ( " product.product " )
ctx = context and context . copy ( ) or { }
if ctx . get ( " create_product_variant " ) :
return None
ctx . update ( active_test = False , create_product_variant = True )
tmpl_ids = self . browse ( cr , uid , ids , context = ctx )
for tmpl_id in tmpl_ids :
# list of values combination
2014-11-17 13:24:03 +00:00
variant_alone = [ ]
2014-05-27 07:42:52 +00:00
all_variants = [ [ ] ]
2014-05-28 17:42:15 +00:00
for variant_id in tmpl_id . attribute_line_ids :
2014-11-17 13:24:03 +00:00
if len ( variant_id . value_ids ) == 1 :
variant_alone . append ( variant_id . value_ids [ 0 ] )
temp_variants = [ ]
for variant in all_variants :
2014-05-27 07:42:52 +00:00
for value_id in variant_id . value_ids :
2015-02-26 13:39:53 +00:00
temp_variants . append ( sorted ( variant + [ int ( value_id ) ] ) )
2015-02-11 14:01:20 +00:00
if temp_variants :
all_variants = temp_variants
2014-11-17 13:24:03 +00:00
# adding an attribute with only one value should not recreate product
# write this attribute on every product to make sure we don't lose them
for variant_id in variant_alone :
product_ids = [ ]
for product_id in tmpl_id . product_variant_ids :
if variant_id . id not in map ( int , product_id . attribute_value_ids ) :
product_ids . append ( product_id . id )
product_obj . write ( cr , uid , product_ids , { ' attribute_value_ids ' : [ ( 4 , variant_id . id ) ] } , context = ctx )
2014-05-27 07:42:52 +00:00
# check product
2014-05-28 09:46:32 +00:00
variant_ids_to_active = [ ]
2014-05-27 07:42:52 +00:00
variants_active_ids = [ ]
variants_inactive = [ ]
for product_id in tmpl_id . product_variant_ids :
2015-02-26 13:39:53 +00:00
variants = sorted ( map ( int , product_id . attribute_value_ids ) )
2014-05-27 07:42:52 +00:00
if variants in all_variants :
variants_active_ids . append ( product_id . id )
all_variants . pop ( all_variants . index ( variants ) )
2014-05-28 09:29:20 +00:00
if not product_id . active :
2014-05-28 09:46:32 +00:00
variant_ids_to_active . append ( product_id . id )
2014-05-27 07:42:52 +00:00
else :
variants_inactive . append ( product_id )
2014-05-28 09:46:32 +00:00
if variant_ids_to_active :
product_obj . write ( cr , uid , variant_ids_to_active , { ' active ' : True } , context = ctx )
2014-05-27 07:42:52 +00:00
# create new product
for variant_ids in all_variants :
values = {
' product_tmpl_id ' : tmpl_id . id ,
2014-05-28 17:42:15 +00:00
' attribute_value_ids ' : [ ( 6 , 0 , variant_ids ) ]
2014-05-27 07:42:52 +00:00
}
id = product_obj . create ( cr , uid , values , context = ctx )
variants_active_ids . append ( id )
# unlink or inactive product
for variant_id in map ( int , variants_inactive ) :
try :
2015-06-20 09:14:32 +00:00
with cr . savepoint ( ) , tools . mute_logger ( ' openerp.sql_db ' ) :
2014-05-27 07:42:52 +00:00
product_obj . unlink ( cr , uid , [ variant_id ] , context = ctx )
except ( psycopg2 . Error , osv . except_osv ) :
product_obj . write ( cr , uid , [ variant_id ] , { ' active ' : False } , context = ctx )
pass
return True
2013-12-06 16:30:54 +00:00
def create ( self , cr , uid , vals , context = None ) :
''' Store the initial standard price in order to be able to retrieve the cost of a product template for a given date '''
product_template_id = super ( product_template , self ) . create ( cr , uid , vals , context = context )
2014-05-27 16:03:15 +00:00
if not context or " create_product_product " not in context :
self . create_variant_ids ( cr , uid , [ product_template_id ] , context = context )
2014-05-05 12:07:43 +00:00
self . _set_standard_price ( cr , uid , product_template_id , vals . get ( ' standard_price ' , 0.0 ) , context = context )
2014-06-17 08:48:44 +00:00
# TODO: this is needed to set given values to first variant after creation
# these fields should be moved to product as lead to confusion
related_vals = { }
if vals . get ( ' ean13 ' ) :
related_vals [ ' ean13 ' ] = vals [ ' ean13 ' ]
if vals . get ( ' default_code ' ) :
related_vals [ ' default_code ' ] = vals [ ' default_code ' ]
if related_vals :
self . write ( cr , uid , product_template_id , related_vals , context = context )
2013-12-06 16:30:54 +00:00
return product_template_id
2011-11-04 09:55:28 +00:00
def write ( self , cr , uid , ids , vals , context = None ) :
2013-12-06 16:30:54 +00:00
''' Store the standard price change in order to be able to retrieve the cost of a product template for a given date '''
2014-06-03 14:13:54 +00:00
if isinstance ( ids , ( int , long ) ) :
2014-05-05 12:07:43 +00:00
ids = [ ids ]
2014-11-26 17:20:28 +00:00
if ' uom_po_id ' in vals :
new_uom = self . pool . get ( ' product.uom ' ) . browse ( cr , uid , vals [ ' uom_po_id ' ] , context = context )
2011-11-03 11:18:41 +00:00
for product in self . browse ( cr , uid , ids , context = context ) :
2014-11-26 17:20:28 +00:00
old_uom = product . uom_po_id
if old_uom . category_id . id != new_uom . category_id . id :
raise osv . except_osv ( _ ( ' Unit of Measure categories Mismatch! ' ) , _ ( " New Unit of Measure ' %s ' must belong to same Unit of Measure category ' %s ' as of old Unit of Measure ' %s ' . If you need to change the unit of measure, you may deactivate this product from the ' Procurements ' tab and create a new one. " ) % ( new_uom . name , old_uom . category_id . name , old_uom . name , ) )
2013-12-06 16:30:54 +00:00
if ' standard_price ' in vals :
for prod_template_id in ids :
2014-05-05 12:07:43 +00:00
self . _set_standard_price ( cr , uid , prod_template_id , vals [ ' standard_price ' ] , context = context )
2014-05-27 07:42:52 +00:00
res = super ( product_template , self ) . write ( cr , uid , ids , vals , context = context )
2014-05-28 17:42:15 +00:00
if ' attribute_line_ids ' in vals or vals . get ( ' active ' ) :
2014-05-27 07:42:52 +00:00
self . create_variant_ids ( cr , uid , ids , context = context )
2014-05-28 09:29:20 +00:00
if ' active ' in vals and not vals . get ( ' active ' ) :
ctx = context and context . copy ( ) or { }
ctx . update ( active_test = False )
product_ids = [ ]
for product in self . browse ( cr , uid , ids , context = ctx ) :
product_ids = map ( int , product . product_variant_ids )
self . pool . get ( " product.product " ) . write ( cr , uid , product_ids , { ' active ' : vals . get ( ' active ' ) } , context = ctx )
2014-05-27 07:42:52 +00:00
return res
2011-10-24 13:11:26 +00:00
2013-11-01 10:25:28 +00:00
def copy ( self , cr , uid , id , default = None , context = None ) :
if default is None :
default = { }
2014-01-21 12:21:39 +00:00
template = self . browse ( cr , uid , id , context = context )
default [ ' name ' ] = _ ( " %s (copy) " ) % ( template [ ' name ' ] )
return super ( product_template , self ) . copy ( cr , uid , id , default = default , context = context )
2013-11-01 10:25:28 +00:00
2008-07-22 15:11:28 +00:00
_defaults = {
2009-12-23 17:16:39 +00:00
' company_id ' : lambda s , cr , uid , c : s . pool . get ( ' res.company ' ) . _company_default_get ( cr , uid , ' product.template ' , context = c ) ,
2012-10-03 11:38:46 +00:00
' list_price ' : 1 ,
' standard_price ' : 0.0 ,
2013-10-29 07:48:15 +00:00
' sale_ok ' : 1 ,
2008-07-22 15:11:28 +00:00
' uom_id ' : _get_uom_id ,
' uom_po_id ' : _get_uom_id ,
2013-07-12 14:14:07 +00:00
' uos_coeff ' : 1.0 ,
' mes_type ' : ' fixed ' ,
2008-10-28 16:23:02 +00:00
' categ_id ' : _default_category ,
2012-10-03 11:38:46 +00:00
' type ' : ' consu ' ,
2014-05-28 16:30:03 +00:00
' active ' : True ,
2008-07-22 15:11:28 +00:00
}
2010-11-19 13:48:01 +00:00
def _check_uom ( self , cursor , user , ids , context = None ) :
for product in self . browse ( cursor , user , ids , context = context ) :
2014-01-27 09:18:56 +00:00
if product . uom_id . category_id . id != product . uom_po_id . category_id . id :
2008-07-22 15:11:28 +00:00
return False
return True
2010-11-19 13:48:01 +00:00
def _check_uos ( self , cursor , user , ids , context = None ) :
for product in self . browse ( cursor , user , ids , context = context ) :
2008-07-22 15:11:28 +00:00
if product . uos_id \
and product . uos_id . category_id . id \
== product . uom_id . category_id . id :
return False
return True
_constraints = [
2014-11-26 17:20:28 +00:00
( _check_uom , ' Error: The default Unit of Measure and the purchase Unit of Measure must be in the same category. ' , [ ' uom_id ' ] ) ,
2008-07-22 15:11:28 +00:00
]
2010-11-19 13:48:01 +00:00
def name_get ( self , cr , user , ids , context = None ) :
2010-11-23 07:05:05 +00:00
if context is None :
2010-11-22 10:37:53 +00:00
context = { }
2008-07-22 15:11:28 +00:00
if ' partner_id ' in context :
pass
return super ( product_template , self ) . name_get ( cr , user , ids , context )
2006-12-07 13:41:40 +00:00
2015-02-25 16:46:38 +00:00
def name_search ( self , cr , user , name = ' ' , args = None , operator = ' ilike ' , context = None , limit = 100 ) :
2015-03-18 10:22:39 +00:00
# Only use the product.product heuristics if there is a search term and the domain
# does not specify a match on `product.template` IDs.
if not name or any ( term [ 0 ] == ' id ' for term in ( args or [ ] ) ) :
return super ( product_template , self ) . name_search (
cr , user , name = name , args = args , operator = operator , context = context , limit = limit )
2015-08-26 08:18:40 +00:00
template_ids = set ( )
2015-02-25 16:46:38 +00:00
product_product = self . pool [ ' product.product ' ]
2015-08-26 08:18:40 +00:00
results = product_product . name_search ( cr , user , name , args , operator = operator , context = context , limit = limit )
2015-02-25 16:46:38 +00:00
product_ids = [ p [ 0 ] for p in results ]
2015-08-26 08:18:40 +00:00
for p in product_product . browse ( cr , user , product_ids , context = context ) :
template_ids . add ( p . product_tmpl_id . id )
while ( results and len ( template_ids ) < limit ) :
domain = [ ( ' product_tmpl_id ' , ' not in ' , list ( template_ids ) ) ]
results = product_product . name_search (
cr , user , name , args + domain , operator = operator , context = context , limit = limit )
product_ids = [ p [ 0 ] for p in results ]
for p in product_product . browse ( cr , user , product_ids , context = context ) :
template_ids . add ( p . product_tmpl_id . id )
2015-03-18 10:22:39 +00:00
# re-apply product.template order + name_get
return super ( product_template , self ) . name_search (
2015-08-26 08:18:40 +00:00
cr , user , ' ' , args = [ ( ' id ' , ' in ' , list ( template_ids ) ) ] ,
[FIX] product: import product.template fields using default_code
This rev. is related to 489a96c2572bbe2cc04c0658ed2b862aca31fc0f
It wasn't possible anymore to perform an import of
a product.template field in a model
(e.g. mrp.bom), while it should be the case.
In the context of an import,
the operator of the name_search is '='.
Therefore, in this super call, the operator was '='
and the name was '' (empty).
In such a case, ('name', '=', '') is added
to the search domain by the base name_search method
(in models.py),
leading to the domain
[('id', 'in', template_ids), ('name', '=', '')]
which will lead to no results.
Forcing 'ilike' as domain is correct, as the actual
name_search, with the correct operator,
has already been performed in the lines above, the
point of this second name_search is to get
the right order along with the right name_get.
opw-632089
2015-04-03 08:59:24 +00:00
operator = ' ilike ' , context = context , limit = limit )
2014-08-08 15:22:14 +00:00
2006-12-07 13:41:40 +00:00
class product_product ( osv . osv ) :
2014-01-22 17:02:18 +00:00
_name = " product.product "
_description = " Product "
_inherits = { ' product.template ' : ' product_tmpl_id ' }
_inherit = [ ' mail.thread ' ]
_order = ' default_code,name_template '
2010-11-19 13:48:01 +00:00
def _product_price ( self , cr , uid , ids , name , arg , context = None ) :
2013-12-06 22:18:09 +00:00
plobj = self . pool . get ( ' product.pricelist ' )
2008-07-22 15:11:28 +00:00
res = { }
2010-11-23 07:05:05 +00:00
if context is None :
2010-11-22 10:37:53 +00:00
context = { }
2010-11-02 05:29:14 +00:00
quantity = context . get ( ' quantity ' ) or 1.0
2008-07-22 15:11:28 +00:00
pricelist = context . get ( ' pricelist ' , False )
2012-01-05 12:22:19 +00:00
partner = context . get ( ' partner ' , False )
2008-07-22 15:11:28 +00:00
if pricelist :
2013-09-06 15:50:47 +00:00
# Support context pricelists specified as display_name or ID for compatibility
if isinstance ( pricelist , basestring ) :
2013-12-06 22:18:09 +00:00
pricelist_ids = plobj . name_search (
2013-09-06 15:50:47 +00:00
cr , uid , pricelist , operator = ' = ' , context = context , limit = 1 )
pricelist = pricelist_ids [ 0 ] [ 0 ] if pricelist_ids else pricelist
2013-12-03 22:39:10 +00:00
2014-03-17 12:01:26 +00:00
if isinstance ( pricelist , ( int , long ) ) :
products = self . browse ( cr , uid , ids , context = context )
qtys = map ( lambda x : ( x , quantity , partner ) , products )
pl = plobj . browse ( cr , uid , pricelist , context = context )
price = plobj . _price_get_multi ( cr , uid , pl , qtys , context = context )
for id in ids :
res [ id ] = price . get ( id , 0.0 )
2008-07-22 15:11:28 +00:00
for id in ids :
res . setdefault ( id , 0.0 )
return res
2014-05-27 07:42:52 +00:00
def view_header_get ( self , cr , uid , view_id , view_type , context = None ) :
if context is None :
context = { }
res = super ( product_product , self ) . view_header_get ( cr , uid , view_id , view_type , context )
if ( context . get ( ' categ_id ' , False ) ) :
return _ ( ' Products: ' ) + self . pool . get ( ' product.category ' ) . browse ( cr , uid , context [ ' categ_id ' ] , context = context ) . name
return res
2008-07-22 15:11:28 +00:00
def _product_lst_price ( self , cr , uid , ids , name , arg , context = None ) :
product_uom_obj = self . pool . get ( ' product.uom ' )
2014-05-27 07:42:52 +00:00
res = dict . fromkeys ( ids , 0.0 )
2008-07-22 15:11:28 +00:00
for product in self . browse ( cr , uid , ids , context = context ) :
if ' uom ' in context :
uom = product . uos_id or product . uom_id
res [ product . id ] = product_uom_obj . _compute_price ( cr , uid ,
uom . id , product . list_price , context [ ' uom ' ] )
else :
res [ product . id ] = product . list_price
2014-06-11 10:50:41 +00:00
res [ product . id ] = res [ product . id ] + product . price_extra
2008-07-22 15:11:28 +00:00
return res
2014-07-18 09:33:09 +00:00
def _set_product_lst_price ( self , cr , uid , id , name , value , args , context = None ) :
product_uom_obj = self . pool . get ( ' product.uom ' )
product = self . browse ( cr , uid , id , context = context )
if ' uom ' in context :
uom = product . uos_id or product . uom_id
value = product_uom_obj . _compute_price ( cr , uid ,
context [ ' uom ' ] , value , uom . id )
value = value - product . price_extra
2014-11-13 11:30:26 +00:00
return product . write ( { ' list_price ' : value } )
2014-07-18 09:33:09 +00:00
2010-11-19 13:48:01 +00:00
def _get_partner_code_name ( self , cr , uid , ids , product , partner_id , context = None ) :
2008-07-22 15:11:28 +00:00
for supinfo in product . seller_ids :
if supinfo . name . id == partner_id :
2014-05-27 07:42:52 +00:00
return { ' code ' : supinfo . product_code or product . default_code , ' name ' : supinfo . product_name or product . name }
res = { ' code ' : product . default_code , ' name ' : product . name }
2010-09-22 13:06:46 +00:00
return res
2008-07-22 15:11:28 +00:00
2010-11-19 13:48:01 +00:00
def _product_code ( self , cr , uid , ids , name , arg , context = None ) :
2008-07-22 15:11:28 +00:00
res = { }
2010-11-23 07:05:05 +00:00
if context is None :
2010-11-22 10:37:53 +00:00
context = { }
2010-11-19 13:48:01 +00:00
for p in self . browse ( cr , uid , ids , context = context ) :
2010-11-22 10:37:53 +00:00
res [ p . id ] = self . _get_partner_code_name ( cr , uid , [ ] , p , context . get ( ' partner_id ' , None ) , context = context ) [ ' code ' ]
2008-07-22 15:11:28 +00:00
return res
2010-11-19 13:48:01 +00:00
def _product_partner_ref ( self , cr , uid , ids , name , arg , context = None ) :
2008-07-22 15:11:28 +00:00
res = { }
2010-11-23 07:05:05 +00:00
if context is None :
2010-11-22 10:37:53 +00:00
context = { }
2010-11-19 13:48:01 +00:00
for p in self . browse ( cr , uid , ids , context = context ) :
2010-11-22 10:37:53 +00:00
data = self . _get_partner_code_name ( cr , uid , [ ] , p , context . get ( ' partner_id ' , None ) , context = context )
2008-10-28 12:10:01 +00:00
if not data [ ' code ' ] :
2009-07-02 15:11:59 +00:00
data [ ' code ' ] = p . code
2008-10-28 12:10:01 +00:00
if not data [ ' name ' ] :
data [ ' name ' ] = p . name
2014-05-27 07:42:52 +00:00
res [ p . id ] = ( data [ ' code ' ] and ( ' [ ' + data [ ' code ' ] + ' ] ' ) or ' ' ) + ( data [ ' name ' ] or ' ' )
2014-01-22 09:58:26 +00:00
return res
2012-08-03 17:26:36 +00:00
2014-07-06 14:44:26 +00:00
def _is_product_variant_impl ( self , cr , uid , ids , name , arg , context = None ) :
2014-05-27 07:42:52 +00:00
return dict . fromkeys ( ids , True )
2012-07-11 00:23:33 +00:00
2013-04-12 13:08:56 +00:00
def _get_name_template_ids ( self , cr , uid , ids , context = None ) :
2013-04-15 10:21:21 +00:00
template_ids = self . pool . get ( ' product.product ' ) . search ( cr , uid , [ ( ' product_tmpl_id ' , ' in ' , ids ) ] )
2013-04-12 13:08:56 +00:00
return list ( set ( template_ids ) )
2013-04-12 13:08:56 +00:00
2014-05-27 07:42:52 +00:00
def _get_image_variant ( self , cr , uid , ids , name , args , context = None ) :
result = dict . fromkeys ( ids , False )
for obj in self . browse ( cr , uid , ids , context = context ) :
result [ obj . id ] = obj . image_variant or getattr ( obj . product_tmpl_id , name )
return result
def _set_image_variant ( self , cr , uid , id , name , value , args , context = None ) :
image = tools . image_resize_image_big ( value )
2015-06-24 09:37:21 +00:00
2014-05-27 07:42:52 +00:00
product = self . browse ( cr , uid , id , context = context )
2015-06-24 09:37:21 +00:00
if product . product_tmpl_id . image :
product . image_variant = image
else :
product . product_tmpl_id . image = image
2014-05-27 07:42:52 +00:00
2014-05-28 17:04:23 +00:00
def _get_price_extra ( self , cr , uid , ids , name , args , context = None ) :
result = dict . fromkeys ( ids , False )
for product in self . browse ( cr , uid , ids , context = context ) :
price_extra = 0.0
2014-05-28 17:42:15 +00:00
for variant_id in product . attribute_value_ids :
2014-05-28 17:04:23 +00:00
for price_id in variant_id . price_ids :
if price_id . product_tmpl_id . id == product . product_tmpl_id . id :
price_extra + = price_id . price_extra
result [ product . id ] = price_extra
return result
2008-07-22 15:11:28 +00:00
_columns = {
2014-05-27 07:42:52 +00:00
' price ' : fields . function ( _product_price , type = ' float ' , string = ' Price ' , digits_compute = dp . get_precision ( ' Product Price ' ) ) ,
2014-12-19 10:51:22 +00:00
' price_extra ' : fields . function ( _get_price_extra , type = ' float ' , string = ' Variant Extra Price ' , help = " This is the sum of the extra price of all attributes " , digits_compute = dp . get_precision ( ' Product Price ' ) ) ,
2014-07-18 09:33:09 +00:00
' lst_price ' : fields . function ( _product_lst_price , fnct_inv = _set_product_lst_price , type = ' float ' , string = ' Public Price ' , digits_compute = dp . get_precision ( ' Product Price ' ) ) ,
2012-08-31 13:50:10 +00:00
' code ' : fields . function ( _product_code , type = ' char ' , string = ' Internal Reference ' ) ,
2011-07-01 23:41:24 +00:00
' partner_ref ' : fields . function ( _product_partner_ref , type = ' char ' , string = ' Customer ref ' ) ,
2014-01-26 21:05:06 +00:00
' default_code ' : fields . char ( ' Internal Reference ' , select = True ) ,
2012-10-23 13:02:42 +00:00
' active ' : fields . boolean ( ' Active ' , help = " If unchecked, it will allow you to hide the product without removing it. " ) ,
2014-08-07 10:50:13 +00:00
' product_tmpl_id ' : fields . many2one ( ' product.template ' , ' Product Template ' , required = True , ondelete = " cascade " , select = True , auto_join = True ) ,
2012-10-23 13:02:42 +00:00
' ean13 ' : fields . char ( ' EAN13 Barcode ' , size = 13 , help = " International Article Number used for product identification. " ) ,
2014-01-26 21:05:06 +00:00
' name_template ' : fields . related ( ' product_tmpl_id ' , ' name ' , string = " Template Name " , type = ' char ' , store = {
2013-04-12 13:08:56 +00:00
' product.template ' : ( _get_name_template_ids , [ ' name ' ] , 10 ) ,
2013-04-17 09:29:14 +00:00
' product.product ' : ( lambda self , cr , uid , ids , c = None : ids , [ ] , 10 ) ,
2014-01-22 17:02:18 +00:00
} , select = True ) ,
2014-07-08 07:12:17 +00:00
' attribute_value_ids ' : fields . many2many ( ' product.attribute.value ' , id1 = ' prod_id ' , id2 = ' att_id ' , string = ' Attributes ' , readonly = True , ondelete = ' restrict ' ) ,
2014-09-29 15:12:49 +00:00
' is_product_variant ' : fields . function ( _is_product_variant_impl , type = ' boolean ' , string = ' Is product variant ' ) ,
2014-05-27 07:42:52 +00:00
# image: all image fields are base64 encoded and PIL-supported
' image_variant ' : fields . binary ( " Variant Image " ,
help = " This field holds the image used as image for the product variant, limited to 1024x1024px. " ) ,
' image ' : fields . function ( _get_image_variant , fnct_inv = _set_image_variant ,
string = " Big-sized image " , type = " binary " ,
help = " Image of the product variant (Big-sized image of product template if false). It is automatically " \
" resized as a 1024x1024px image, with aspect ratio preserved. " ) ,
' image_small ' : fields . function ( _get_image_variant , fnct_inv = _set_image_variant ,
string = " Small-sized image " , type = " binary " ,
help = " Image of the product variant (Small-sized image of product template if false). " ) ,
' image_medium ' : fields . function ( _get_image_variant , fnct_inv = _set_image_variant ,
string = " Medium-sized image " , type = " binary " ,
help = " Image of the product variant (Medium-sized image of product template if false). " ) ,
2008-07-22 15:11:28 +00:00
}
2013-10-24 06:58:05 +00:00
2014-01-22 17:02:18 +00:00
_defaults = {
2014-05-28 16:30:03 +00:00
' active ' : 1 ,
2014-01-22 17:02:18 +00:00
' color ' : 0 ,
}
2011-01-13 11:37:35 +00:00
def unlink ( self , cr , uid , ids , context = None ) :
unlink_ids = [ ]
unlink_product_tmpl_ids = [ ]
for product in self . browse ( cr , uid , ids , context = context ) :
2014-08-14 14:47:01 +00:00
# Check if product still exists, in case it has been unlinked by unlinking its template
if not product . exists ( ) :
continue
2011-01-13 11:37:35 +00:00
tmpl_id = product . product_tmpl_id . id
# Check if the product is last product of this template
other_product_ids = self . search ( cr , uid , [ ( ' product_tmpl_id ' , ' = ' , tmpl_id ) , ( ' id ' , ' != ' , product . id ) ] , context = context )
if not other_product_ids :
2013-03-25 15:37:06 +00:00
unlink_product_tmpl_ids . append ( tmpl_id )
2011-01-13 11:37:35 +00:00
unlink_ids . append ( product . id )
2013-03-25 15:37:06 +00:00
res = super ( product_product , self ) . unlink ( cr , uid , unlink_ids , context = context )
# delete templates after calling super, as deleting template could lead to deleting
# products due to ondelete='cascade'
2011-01-13 11:37:35 +00:00
self . pool . get ( ' product.template ' ) . unlink ( cr , uid , unlink_product_tmpl_ids , context = context )
2013-03-25 15:37:06 +00:00
return res
2008-07-22 15:11:28 +00:00
2015-05-05 16:20:35 +00:00
def onchange_type ( self , cr , uid , ids , type ) :
return { }
2012-03-05 18:40:03 +00:00
def onchange_uom ( self , cursor , user , ids , uom_id , uom_po_id ) :
2008-07-30 06:29:44 +00:00
if uom_id and uom_po_id :
uom_obj = self . pool . get ( ' product.uom ' )
uom = uom_obj . browse ( cursor , user , [ uom_id ] ) [ 0 ]
uom_po = uom_obj . browse ( cursor , user , [ uom_po_id ] ) [ 0 ]
if uom . category_id . id != uom_po . category_id . id :
return { ' value ' : { ' uom_po_id ' : uom_id } }
return False
2010-11-19 13:48:01 +00:00
def _check_ean_key ( self , cr , uid , ids , context = None ) :
2012-07-09 13:40:24 +00:00
for product in self . read ( cr , uid , ids , [ ' ean13 ' ] , context = context ) :
2014-05-27 07:42:52 +00:00
if not check_ean ( product [ ' ean13 ' ] ) :
return False
return True
2008-07-22 15:11:28 +00:00
2012-09-21 07:49:05 +00:00
_constraints = [ ( _check_ean_key , ' You provided an invalid " EAN13 Barcode " reference. You may use the " Internal Reference " field instead. ' , [ ' ean13 ' ] ) ]
2008-07-22 15:11:28 +00:00
def on_order ( self , cr , uid , ids , orderline , quantity ) :
pass
2010-11-24 09:20:15 +00:00
def name_get ( self , cr , user , ids , context = None ) :
if context is None :
context = { }
2012-08-09 12:04:19 +00:00
if isinstance ( ids , ( int , long ) ) :
ids = [ ids ]
2008-07-22 15:11:28 +00:00
if not len ( ids ) :
return [ ]
2014-05-27 07:42:52 +00:00
2008-07-22 15:11:28 +00:00
def _name_get ( d ) :
name = d . get ( ' name ' , ' ' )
2014-08-18 12:47:53 +00:00
code = context . get ( ' display_default_code ' , True ) and d . get ( ' default_code ' , False ) or False
2008-07-22 15:11:28 +00:00
if code :
name = ' [ %s ] %s ' % ( code , name )
return ( d [ ' id ' ] , name )
2010-11-24 09:20:15 +00:00
partner_id = context . get ( ' partner_id ' , False )
2014-10-02 08:12:51 +00:00
if partner_id :
partner_ids = [ partner_id , self . pool [ ' res.partner ' ] . browse ( cr , user , partner_id , context = context ) . commercial_partner_id . id ]
else :
partner_ids = [ ]
2010-11-24 09:20:15 +00:00
2014-01-09 12:44:23 +00:00
# all user don't have access to seller and partner
# check access and use superuser
self . check_access_rights ( cr , user , " read " )
self . check_access_rule ( cr , user , ids , " read " , context = context )
2010-11-24 09:20:15 +00:00
result = [ ]
2014-01-09 12:44:23 +00:00
for product in self . browse ( cr , SUPERUSER_ID , ids , context = context ) :
2014-05-28 17:42:15 +00:00
variant = " , " . join ( [ v . name for v in product . attribute_value_ids ] )
2014-05-27 07:42:52 +00:00
name = variant and " %s ( %s ) " % ( product . name , variant ) or product . name
2014-03-19 16:33:59 +00:00
sellers = [ ]
2014-10-02 13:36:58 +00:00
if partner_ids :
2014-10-14 15:04:19 +00:00
sellers = filter ( lambda x : x . name . id in partner_ids , product . seller_ids )
2010-11-24 09:20:15 +00:00
if sellers :
for s in sellers :
2015-01-06 17:22:54 +00:00
seller_variant = s . product_name and (
variant and " %s ( %s ) " % ( s . product_name , variant ) or s . product_name
) or False
2010-11-24 09:20:15 +00:00
mydict = {
2010-12-30 08:06:07 +00:00
' id ' : product . id ,
2014-09-17 15:54:34 +00:00
' name ' : seller_variant or name ,
2010-12-30 08:06:07 +00:00
' default_code ' : s . product_code or product . default_code ,
2010-11-24 09:20:15 +00:00
}
result . append ( _name_get ( mydict ) )
else :
2010-12-21 19:32:03 +00:00
mydict = {
' id ' : product . id ,
2014-05-27 07:42:52 +00:00
' name ' : name ,
2010-12-21 19:32:03 +00:00
' default_code ' : product . default_code ,
}
result . append ( _name_get ( mydict ) )
2008-07-22 15:11:28 +00:00
return result
2009-12-09 11:43:34 +00:00
def name_search ( self , cr , user , name = ' ' , args = None , operator = ' ilike ' , context = None , limit = 100 ) :
2008-07-22 15:11:28 +00:00
if not args :
2012-01-25 16:59:30 +00:00
args = [ ]
2009-07-06 19:39:59 +00:00
if name :
2014-10-01 10:39:32 +00:00
positive_operators = [ ' = ' , ' ilike ' , ' =ilike ' , ' like ' , ' =like ' ]
ids = [ ]
if operator in positive_operators :
ids = self . search ( cr , user , [ ( ' default_code ' , ' = ' , name ) ] + args , limit = limit , context = context )
if not ids :
ids = self . search ( cr , user , [ ( ' ean13 ' , ' = ' , name ) ] + args , limit = limit , context = context )
if not ids and operator not in expression . NEGATIVE_TERM_OPERATORS :
2012-01-25 16:59:30 +00:00
# Do not merge the 2 next lines into one single search, SQL search performance would be abysmal
# on a database with thousands of matching products, due to the huge merge+unique needed for the
# OR operator (and given the fact that the 'name' lookup results come from the ir.translation table
# Performing a quick memory merge of ids in Python will give much better performance
2015-02-25 16:52:03 +00:00
ids = self . search ( cr , user , args + [ ( ' default_code ' , operator , name ) ] , limit = limit , context = context )
2012-11-02 08:40:04 +00:00
if not limit or len ( ids ) < limit :
2012-01-25 16:59:30 +00:00
# we may underrun the limit because of dupes in the results, that's fine
2014-01-27 10:00:37 +00:00
limit2 = ( limit - len ( ids ) ) if limit else False
2015-02-25 16:52:03 +00:00
ids + = self . search ( cr , user , args + [ ( ' name ' , operator , name ) , ( ' id ' , ' not in ' , ids ) ] , limit = limit2 , context = context )
2014-10-01 10:39:32 +00:00
elif not ids and operator in expression . NEGATIVE_TERM_OPERATORS :
ids = self . search ( cr , user , args + [ ' & ' , ( ' default_code ' , operator , name ) , ( ' name ' , operator , name ) ] , limit = limit , context = context )
if not ids and operator in positive_operators :
2012-01-25 16:59:30 +00:00
ptrn = re . compile ( ' ( \ [(.*?) \ ]) ' )
res = ptrn . search ( name )
if res :
ids = self . search ( cr , user , [ ( ' default_code ' , ' = ' , res . group ( 2 ) ) ] + args , limit = limit , context = context )
2009-07-06 19:39:59 +00:00
else :
ids = self . search ( cr , user , args , limit = limit , context = context )
2010-11-22 10:37:53 +00:00
result = self . name_get ( cr , user , ids , context = context )
2008-07-22 15:11:28 +00:00
return result
#
# Could be overrided for variants matrices prices
#
2010-09-29 09:57:42 +00:00
def price_get ( self , cr , uid , ids , ptype = ' list_price ' , context = None ) :
2013-12-06 22:18:09 +00:00
products = self . browse ( cr , uid , ids , context = context )
2014-05-27 07:42:52 +00:00
return self . pool . get ( " product.template " ) . _price_get ( cr , uid , products , ptype = ptype , context = context )
2008-07-22 15:11:28 +00:00
def copy ( self , cr , uid , id , default = None , context = None ) :
2010-11-23 07:05:05 +00:00
if context is None :
2008-07-22 15:11:28 +00:00
context = { }
2015-02-13 14:01:00 +00:00
if default is None :
default = { }
2014-05-14 08:04:16 +00:00
product = self . browse ( cr , uid , id , context )
2014-01-27 10:19:57 +00:00
if context . get ( ' variant ' ) :
2014-01-23 18:32:08 +00:00
# if we copy a variant or create one, we keep the same template
2014-01-27 10:19:57 +00:00
default [ ' product_tmpl_id ' ] = product . product_tmpl_id . id
2014-01-23 18:32:08 +00:00
elif ' name ' not in default :
2014-01-27 10:19:57 +00:00
default [ ' name ' ] = _ ( " %s (copy) " ) % ( product . name , )
2014-01-23 18:32:08 +00:00
return super ( product_product , self ) . copy ( cr , uid , id , default = default , context = context )
2012-09-11 08:39:19 +00:00
def search ( self , cr , uid , args , offset = 0 , limit = None , order = None , context = None , count = False ) :
if context is None :
context = { }
2014-01-15 12:27:23 +00:00
if context . get ( ' search_default_categ_id ' ) :
2012-09-11 08:39:19 +00:00
args . append ( ( ( ' categ_id ' , ' child_of ' , context [ ' search_default_categ_id ' ] ) ) )
2013-03-05 08:35:13 +00:00
return super ( product_product , self ) . search ( cr , uid , args , offset = offset , limit = limit , order = order , context = context , count = count )
2012-09-11 08:39:19 +00:00
2014-01-22 17:02:18 +00:00
def open_product_template ( self , cr , uid , ids , context = None ) :
""" Utility method used to add an " Open Template " button in product views """
product = self . browse ( cr , uid , ids [ 0 ] , context = context )
return { ' type ' : ' ir.actions.act_window ' ,
' res_model ' : ' product.template ' ,
' view_mode ' : ' form ' ,
' res_id ' : product . product_tmpl_id . id ,
' target ' : ' new ' }
2014-05-27 16:03:15 +00:00
def create ( self , cr , uid , vals , context = None ) :
if context is None :
context = { }
2014-05-28 16:30:03 +00:00
ctx = dict ( context or { } , create_product_product = True )
return super ( product_product , self ) . create ( cr , uid , vals , context = ctx )
2014-05-27 16:03:15 +00:00
2006-12-07 13:41:40 +00:00
2014-08-08 15:22:14 +00:00
def need_procurement ( self , cr , uid , ids , context = None ) :
return False
2014-10-21 19:12:58 +00:00
def _compute_uos_qty ( self , cr , uid , ids , uom , qty , uos , context = None ) :
'''
Computes product ' s invoicing quantity in UoS from quantity in UoM.
Takes into account the
: param uom : Source unit
: param qty : Source quantity
: param uos : Target UoS unit .
'''
if not uom or not qty or not uos :
return qty
uom_obj = self . pool [ ' product.uom ' ]
product_id = ids [ 0 ] if isinstance ( ids , ( list , tuple ) ) else ids
product = self . browse ( cr , uid , product_id , context = context )
if isinstance ( uos , ( int , long ) ) :
uos = uom_obj . browse ( cr , uid , uos , context = context )
if isinstance ( uom , ( int , long ) ) :
uom = uom_obj . browse ( cr , uid , uom , context = context )
if product . uos_id : # Product has UoS defined
# We cannot convert directly between units even if the units are of the same category
# as we need to apply the conversion coefficient which is valid only between quantities
# in product's default UoM/UoS
qty_default_uom = uom_obj . _compute_qty_obj ( cr , uid , uom , qty , product . uom_id ) # qty in product's default UoM
qty_default_uos = qty_default_uom * product . uos_coeff
return uom_obj . _compute_qty_obj ( cr , uid , product . uos_id , qty_default_uos , uos )
else :
return uom_obj . _compute_qty_obj ( cr , uid , uom , qty , uos )
2014-08-08 15:22:14 +00:00
2006-12-07 13:41:40 +00:00
class product_packaging ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = " product.packaging "
2008-09-22 05:56:30 +00:00
_description = " Packaging "
2008-07-22 15:11:28 +00:00
_rec_name = ' ean '
2010-06-22 10:45:21 +00:00
_order = ' sequence '
2008-07-22 15:11:28 +00:00
_columns = {
2009-12-21 13:14:12 +00:00
' sequence ' : fields . integer ( ' Sequence ' , help = " Gives the sequence order when displaying a list of packaging. " ) ,
2014-01-26 21:05:06 +00:00
' name ' : fields . text ( ' Description ' ) ,
2008-09-22 05:56:30 +00:00
' qty ' : fields . float ( ' Quantity by Package ' ,
2009-12-21 09:50:54 +00:00
help = " The total number of products you can put by pallet or box. " ) ,
2014-03-20 14:09:59 +00:00
' ul ' : fields . many2one ( ' product.ul ' , ' Package Logistic Unit ' , required = True ) ,
2009-12-22 13:08:10 +00:00
' ul_qty ' : fields . integer ( ' Package by layer ' , help = ' The number of packages by layer ' ) ,
2014-03-20 14:09:59 +00:00
' ul_container ' : fields . many2one ( ' product.ul ' , ' Pallet Logistic Unit ' ) ,
2009-12-21 09:50:54 +00:00
' rows ' : fields . integer ( ' Number of Layers ' , required = True ,
help = ' The number of layers on a pallet or box ' ) ,
2014-06-03 09:53:41 +00:00
' product_tmpl_id ' : fields . many2one ( ' product.template ' , ' Product ' , select = 1 , ondelete = ' cascade ' , required = True ) ,
2014-01-26 21:05:06 +00:00
' ean ' : fields . char ( ' EAN ' , size = 14 , help = " The EAN code of the package unit. " ) ,
' code ' : fields . char ( ' Code ' , help = " The code of the transport unit. " ) ,
2008-09-22 05:56:30 +00:00
' weight ' : fields . float ( ' Total Package Weight ' ,
2009-12-21 09:50:54 +00:00
help = ' The weight of a full package, pallet or box. ' ) ,
2008-07-22 15:11:28 +00:00
}
2010-12-06 13:11:02 +00:00
def _check_ean_key ( self , cr , uid , ids , context = None ) :
for pack in self . browse ( cr , uid , ids , context = context ) :
2014-05-27 07:42:52 +00:00
if not check_ean ( pack . ean ) :
return False
return True
2010-11-19 13:02:08 +00:00
2010-11-23 12:05:08 +00:00
_constraints = [ ( _check_ean_key , ' Error: Invalid ean code ' , [ ' ean ' ] ) ]
2010-11-19 13:02:08 +00:00
2010-11-19 13:48:01 +00:00
def name_get ( self , cr , uid , ids , context = None ) :
2009-09-24 10:46:21 +00:00
if not len ( ids ) :
return [ ]
res = [ ]
2010-12-06 13:11:02 +00:00
for pckg in self . browse ( cr , uid , ids , context = context ) :
2009-09-24 10:46:21 +00:00
p_name = pckg . ean and ' [ ' + pckg . ean + ' ] ' or ' '
p_name + = pckg . ul . name
res . append ( ( pckg . id , p_name ) )
return res
2010-11-19 13:48:01 +00:00
def _get_1st_ul ( self , cr , uid , context = None ) :
2008-07-22 15:11:28 +00:00
cr . execute ( ' select id from product_ul order by id asc limit 1 ' )
res = cr . fetchone ( )
return ( res and res [ 0 ] ) or False
_defaults = {
2014-05-28 16:30:03 +00:00
' rows ' : 3 ,
' sequence ' : 1 ,
2008-07-22 15:11:28 +00:00
' ul ' : _get_1st_ul ,
}
def checksum ( ean ) :
salt = ' 31 ' * 6 + ' 3 '
sum = 0
for ean_part , salt_part in zip ( ean , salt ) :
sum + = int ( ean_part ) * int ( salt_part )
return ( 10 - ( sum % 10 ) ) % 10
checksum = staticmethod ( checksum )
2006-12-07 13:41:40 +00:00
class product_supplierinfo ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = " product.supplierinfo "
_description = " Information about a product supplier "
2010-11-19 13:48:01 +00:00
def _calc_qty ( self , cr , uid , ids , fields , arg , context = None ) :
2010-07-24 09:42:22 +00:00
result = { }
2010-11-19 13:48:01 +00:00
for supplier_info in self . browse ( cr , uid , ids , context = context ) :
2010-07-24 09:42:22 +00:00
for field in fields :
result [ supplier_info . id ] = { field : False }
2011-09-24 13:33:31 +00:00
qty = supplier_info . min_qty
2010-07-24 09:42:22 +00:00
result [ supplier_info . id ] [ ' qty ' ] = qty
return result
2008-07-22 15:11:28 +00:00
_columns = {
2010-11-12 13:07:53 +00:00
' name ' : fields . many2one ( ' res.partner ' , ' Supplier ' , required = True , domain = [ ( ' supplier ' , ' = ' , True ) ] , ondelete = ' cascade ' , help = " Supplier of this product " ) ,
2014-01-26 21:05:06 +00:00
' product_name ' : fields . char ( ' Supplier Product Name ' , help = " This supplier ' s product name will be used when printing a request for quotation. Keep empty to use the internal one. " ) ,
' product_code ' : fields . char ( ' Supplier Product Code ' , help = " This supplier ' s product code will be used when printing a request for quotation. Keep empty to use the internal one. " ) ,
2010-04-18 15:32:44 +00:00
' sequence ' : fields . integer ( ' Sequence ' , help = " Assigns the priority to the list of product supplier. " ) ,
2013-12-09 16:32:54 +00:00
' product_uom ' : fields . related ( ' product_tmpl_id ' , ' uom_po_id ' , type = ' many2one ' , relation = ' product.uom ' , string = " Supplier Unit of Measure " , readonly = " 1 " , help = " This comes from the product form. " ) ,
2012-04-25 11:29:50 +00:00
' min_qty ' : fields . float ( ' Minimal Quantity ' , required = True , help = " The minimal quantity to purchase to this supplier, expressed in the supplier Product Unit of Measure if not empty, in the default unit of measure of the product otherwise. " ) ,
' qty ' : fields . function ( _calc_qty , store = True , type = ' float ' , string = ' Quantity ' , multi = " qty " , help = " This is a quantity which is converted into Default Unit of Measure. " ) ,
2014-01-23 16:32:43 +00:00
' product_tmpl_id ' : fields . many2one ( ' product.template ' , ' Product Template ' , required = True , ondelete = ' cascade ' , select = True , oldname = ' product_id ' ) ,
2014-07-16 08:23:57 +00:00
' delay ' : fields . integer ( ' Delivery Lead Time ' , required = True , help = " Lead time in days between the confirmation of the purchase order and the receipt of the products in your warehouse. Used by the scheduler for automatic computation of the purchase order planning. " ) ,
2014-07-06 14:44:26 +00:00
' pricelist_ids ' : fields . one2many ( ' pricelist.partnerinfo ' , ' suppinfo_id ' , ' Supplier Pricelist ' , copy = True ) ,
2009-12-22 19:07:37 +00:00
' company_id ' : fields . many2one ( ' res.company ' , ' Company ' , select = 1 ) ,
2008-07-22 15:11:28 +00:00
}
_defaults = {
2014-06-05 07:55:30 +00:00
' min_qty ' : 0.0 ,
2014-05-28 16:30:03 +00:00
' sequence ' : 1 ,
' delay ' : 1 ,
2010-12-30 08:06:07 +00:00
' company_id ' : lambda self , cr , uid , c : self . pool . get ( ' res.company ' ) . _company_default_get ( cr , uid , ' product.supplierinfo ' , context = c ) ,
2008-07-22 15:11:28 +00:00
}
2014-09-09 17:06:26 +00:00
2008-07-22 15:11:28 +00:00
_order = ' sequence '
2006-12-07 13:41:40 +00:00
class pricelist_partnerinfo ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = ' pricelist.partnerinfo '
_columns = {
2014-01-26 21:05:06 +00:00
' name ' : fields . char ( ' Description ' ) ,
2008-07-22 15:11:28 +00:00
' suppinfo_id ' : fields . many2one ( ' product.supplierinfo ' , ' Partner Information ' , required = True , ondelete = ' cascade ' ) ,
2012-04-25 11:29:50 +00:00
' min_quantity ' : fields . float ( ' Quantity ' , required = True , help = " The minimal quantity to trigger this rule, expressed in the supplier Unit of Measure if any or in the default Unit of Measure of the product otherrwise. " ) ,
2012-07-11 13:06:15 +00:00
' price ' : fields . float ( ' Unit Price ' , required = True , digits_compute = dp . get_precision ( ' Product Price ' ) , help = " This price will be considered as a price for the supplier Unit of Measure if any or the default Unit of Measure of the product otherwise " ) ,
2008-07-22 15:11:28 +00:00
}
_order = ' min_quantity asc '
2012-09-24 10:33:07 +00:00
class res_currency ( osv . osv ) :
_inherit = ' res.currency '
def _check_main_currency_rounding ( self , cr , uid , ids , context = None ) :
cr . execute ( ' SELECT digits FROM decimal_precision WHERE name like %s ' , ( ' Account ' , ) )
digits = cr . fetchone ( )
2012-09-24 11:16:41 +00:00
if digits and len ( digits ) :
2012-09-24 10:33:07 +00:00
digits = digits [ 0 ]
main_currency = self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid , context = context ) . company_id . currency_id
for currency_id in ids :
if currency_id == main_currency . id :
2014-11-25 17:13:41 +00:00
if float_compare ( main_currency . rounding , 10 * * - digits , precision_digits = 6 ) == - 1 :
2012-09-24 10:33:07 +00:00
return False
return True
_constraints = [
( _check_main_currency_rounding , ' Error! You cannot define a rounding factor for the company \' s main currency that is smaller than the decimal precision of \' Account \' . ' , [ ' rounding ' ] ) ,
]
class decimal_precision ( osv . osv ) :
_inherit = ' decimal.precision '
def _check_main_currency_rounding ( self , cr , uid , ids , context = None ) :
cr . execute ( ' SELECT id, digits FROM decimal_precision WHERE name like %s ' , ( ' Account ' , ) )
res = cr . fetchone ( )
2012-09-24 11:16:41 +00:00
if res and len ( res ) :
2012-09-24 10:33:07 +00:00
account_precision_id , digits = res
main_currency = self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid , context = context ) . company_id . currency_id
for decimal_precision in ids :
if decimal_precision == account_precision_id :
2014-11-25 17:13:41 +00:00
if float_compare ( main_currency . rounding , 10 * * - digits , precision_digits = 6 ) == - 1 :
2012-09-24 10:33:07 +00:00
return False
return True
_constraints = [
( _check_main_currency_rounding , ' Error! You cannot define the decimal precision of \' Account \' as greater than the rounding factor of the company \' s main currency ' , [ ' digits ' ] ) ,
]
2008-07-23 14:41:47 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: