~argilsoft/+junk/hesatec_product_cost_multicompany

« back to all changes in this revision

Viewing changes to product_cost_multicompany.py

  • Committer: Israel CA
  • Date: 2013-11-06 15:37:27 UTC
  • Revision ID: israel.cruz@hesatecnica.com-20131106153727-cve3kke5pv9stv4u
Importacion inicial...

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
 
6
#
 
7
#    This program is free software: you can redistribute it and/or modify
 
8
#    it under the terms of the GNU Affero General Public License as
 
9
#    published by the Free Software Foundation, either version 3 of the
 
10
#    License, or (at your option) any later version.
 
11
#
 
12
#    This program is distributed in the hope that it will be useful,
 
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#    GNU Affero General Public License for more details.
 
16
#
 
17
#    You should have received a copy of the GNU Affero General Public License
 
18
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
#
 
20
##############################################################################
 
21
 
 
22
import logging
 
23
from datetime import datetime
 
24
import time
 
25
 
 
26
from openerp import tools
 
27
from openerp.osv import osv, fields
 
28
from openerp.tools.translate import _
 
29
 
 
30
import openerp.addons.decimal_precision as dp
 
31
 
 
32
 
 
33
 
 
34
class product_cost_multicompany(osv.osv):
 
35
    _name = "product.cost_multicompany"
 
36
    _description = "Product Cost Multicompany"
 
37
    
 
38
    _rec_name = "product_id"
 
39
    
 
40
    _columns = {
 
41
        'product_id'     : fields.many2one('product.product', 'Product', required=True, select=True),        
 
42
        'company_id'     : fields.many2one('res.company', 'Company', required=True,  select=True),
 
43
        'standard_price' : fields.float('Cost', digits_compute=dp.get_precision('Product Price'), help="Cost price of the product", groups="base.group_user"),
 
44
    }
 
45
    
 
46
    _defaults = {
 
47
        'standard_price': 0.0,
 
48
    }
 
49
    
 
50
    _order = "company_id, product_id"
 
51
    
 
52
    _sql_constraints = [
 
53
        ('product_company_uniq', 'unique(company_id, product_id)', 'The product cost must be unique per company!'),
 
54
    ]
 
55
 
 
56
class product_product(osv.osv):
 
57
    _inherit = 'product.product'
 
58
 
 
59
    def _get_standard_price(self, cr, uid, ids, name, arg, context=None):
 
60
        res = {}
 
61
        if context is None:
 
62
            context = {}
 
63
        for p in self.browse(cr, uid, ids, context=context):
 
64
            #if p.cost_method == 'standard':
 
65
            #    res[p.id] = p.standard_price2
 
66
            #else:
 
67
            product_price_obj = self.pool.get('product.cost_multicompany')
 
68
            price_id = product_price_obj.search(cr, uid, [('product_id','=',ids[0]), ('company_id','=',self.pool.get('res.users').browse(cr, uid, uid).company_id.id)])            
 
69
            res[p.id] = product_price_obj.read(cr, uid, price_id)[0]['standard_price'] if price_id else 0.0
 
70
        return res
 
71
    
 
72
    _columns = {
 
73
        'standard_prices'   : fields.one2many('product.cost_multicompany', 'product_id', string="Standard Price"),
 
74
        'standard_price'    : fields.function(_get_standard_price, type='float', string='Cost', digits_compute=dp.get_precision('Product Price'), help="Cost price of the product used for standard stock valuation in accounting and used as a base price on purchase orders.", groups="base.group_user"),
 
75
                }
 
76
    
 
77
    def write(self, cr, uid, ids, vals, context=None):
 
78
        xvals = vals
 
79
        #print "vals: ", vals        
 
80
        if 'standard_price' in xvals:            
 
81
            company_id = self.pool.get('res.users').browse(cr, uid, uid).company_id.id        
 
82
            product_price_obj = self.pool.get('product.cost_multicompany')
 
83
            price_id = product_price_obj.search(cr, uid, [('product_id','=',ids[0]), ('company_id','=',company_id)])
 
84
            if price_id:
 
85
                product_price_obj.write(cr, uid, price_id, {'standard_price': xvals['standard_price']})
 
86
            else:                
 
87
                res = product_price_obj.create(cr, uid, {'company_id' : company_id, 'product_id' : ids[0], 'standard_price' : xvals['standard_price']})
 
88
        return super(product_product, self).write(cr, uid, ids, xvals, context=context)
 
89
 
 
90
        
 
91
 
 
92
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: