~openerp-commiter/openobject-addons/trunk-extra-addons

« back to all changes in this revision

Viewing changes to library/product.py

merging new development from indian accounting

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
# Copyright (c) 2004-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
 
5
#
 
6
# WARNING: This program as such is intended to be used by professional
 
7
# programmers who take the whole responsability of assessing all potential
 
8
# consequences resulting from its eventual inadequacies and bugs
 
9
# End users who are looking for a ready-to-use solution with commercial
 
10
# garantees and support are strongly adviced to contract a Free Software
 
11
# Service Company
 
12
#
 
13
# This program is Free Software; you can redistribute it and/or
 
14
# modify it under the terms of the GNU General Public License
 
15
# as published by the Free Software Foundation; either version 2
 
16
# of the License, or (at your option) any later version.
 
17
#
 
18
# This program is distributed in the hope that it will be useful,
 
19
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
20
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
21
# GNU General Public License for more details.
 
22
#
 
23
# You should have received a copy of the GNU General Public License
 
24
# along with this program; if not, write to the Free Software
 
25
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
26
#
 
27
##############################################################################
 
28
 
 
29
from osv import osv, fields
 
30
import time
 
31
 
 
32
class many2manysym(fields.many2many):
 
33
 
 
34
    def get(self, cr, obj, ids, name, user=None, offset=0, context={}, values={}):
 
35
        res = {}
 
36
        if not ids:
 
37
            return res
 
38
        ids_s = ','.join(map(str,ids))
 
39
        for id in ids:
 
40
            res[id] = []
 
41
        limit_str = self._limit is not None and ' limit %d' % self._limit or ''
 
42
 
 
43
        for (self._id2, self._id1) in [(self._id2, self._id1), (self._id1, self._id2)]:
 
44
            cr.execute('select '+self._id2+','+self._id1+' from '+self._rel+' where '+self._id1+' in ('+ids_s+')'+limit_str+' offset %d', (offset,))
 
45
            for r in cr.fetchall():
 
46
                res[r[1]].append(r[0])
 
47
        return res
 
48
 
 
49
 
 
50
class product_template(osv.osv):
 
51
    _inherit = "product.template"
 
52
    _columns = {
 
53
        'name': fields.char('Name', size=256, required=True, select=True),
 
54
        }
 
55
product_template()
 
56
 
 
57
# Book variant of product
 
58
class product_product(osv.osv):
 
59
    _name = "product.product"
 
60
    _inherit = "product.product"
 
61
 
 
62
 
 
63
    def name_get(self, cr, user, ids, context={}):
 
64
        if not len(ids):
 
65
            return []
 
66
        def _name_get(d):
 
67
            #name = self._product_partner_ref(cr, user, [d['id']], '', '', context)[d['id']]
 
68
            #code = self._product_code(cr, user, [d['id']], '', '', context)[d['id']]
 
69
            name = d.get('name','')
 
70
            ean = d.get('ean13',False)
 
71
            if ean:
 
72
                name = '[%s] %s' % (ean,name)
 
73
            return (d['id'], name)
 
74
        result = map(_name_get, self.read(cr, user, ids, ['name','ean13'], context))
 
75
        return result
 
76
 
 
77
    def name_search(self, cr, user, name, args=[], operator='ilike', context={}, limit=80):
 
78
        ids = self.search(cr, user, [('default_code','=',name)]+ args, limit=limit)
 
79
        if not len(ids):
 
80
            ids = self.search(cr, user, [('ean13','=',name)]+ args, limit=limit)
 
81
        if not len(ids):
 
82
            ids = self.search(cr, user, [('default_code',operator,name)]+ args, limit=limit)
 
83
            ids += self.search(cr, user, [('name',operator,name)]+ args, limit=limit)
 
84
        result = self.name_get(cr, user, ids, context)
 
85
        return result
 
86
 
 
87
    def _tax_incl(self, cr, uid, ids, field_name, arg, context):
 
88
        res = {}
 
89
        for product in self.browse(cr, uid, ids):
 
90
            val=0.0
 
91
            for c in self.pool.get('account.tax').compute(cr, uid, product.taxes_id, product.list_price, 1, False):
 
92
                val += round(c['amount'], 2)
 
93
            res[product.id]=round(val + product.list_price,2)
 
94
 
 
95
        return res
 
96
 
 
97
    def _get_partner_code_name(self, cr, uid, ids, product_id, partner_id, context={}):
 
98
        product = self.browse(cr, uid, [product_id], context)[0]
 
99
        for supinfo in product.seller_ids:
 
100
            if supinfo.name.id == partner_id:
 
101
                return {'code': supinfo.product_code, 'name': supinfo.product_name}
 
102
        return {'code' : product.default_code, 'name' : product.name}
 
103
 
 
104
    def _product_code(self, cr, uid, ids, name, arg, context={}):
 
105
        res = {}
 
106
        for p in self.browse(cr, uid, ids, context):
 
107
            res[p.id] = self._get_partner_code_name(cr, uid, [], p.id, context.get('partner_id', None), context)['code']
 
108
        return res
 
109
 
 
110
 
 
111
    _columns = {
 
112
        'isbn': fields.char('Isbn code', size=64, unique=True),
 
113
        'catalog_num': fields.char('Catalog number', size=64),
 
114
        #'number': fields.char('Number', size=64, readonly=True), # ancien numero interne
 
115
        'author_om_ids': fields.one2many('author.book.rel','product_id','Authors'),
 
116
        'lang': fields.many2many('res.lang','lang_book_rel','product_id','lang_id', 'Language'),
 
117
        'editor': fields.many2one('res.partner','Editor', change_default=True),
 
118
        'code': fields.function(_product_code, method=True, type='char', string='Acronym'),
 
119
        'catalog_num': fields.char('Catalog number', size=64),
 
120
        'date_parution': fields.date('Release date'),
 
121
        'creation_date': fields.datetime('Creation date', readonly=True),
 
122
        'date_retour': fields.date('Return date'),
 
123
        'tome' : fields.char('Tome', size=8),
 
124
        'nbpage': fields.integer('Number of pages', size=8),
 
125
        'rack': fields.many2one('library.rack','Rack', size=16),
 
126
        'online': fields.boolean('Visible on website'),
 
127
        'state': fields.selection([('draft', 'Not yet published'),('sellable','Available'),('end','Sold Out'),('obsolete','Printing w/o Date')], 'State'),
 
128
        'link_ids': many2manysym('product.product','book_book_rel','product_id1','product_id2','Related Books'),
 
129
        'back':fields.selection([('hard', 'Hardback'),('paper','Paperback')], 'Reliure'),
 
130
        'collection': fields.many2one('library.collection','Collection'),
 
131
        'pocket':fields.char('Pocket', size=32),
 
132
        'num_pocket':fields.char('Collection Num.', size=32),
 
133
        'num_edition':fields.integer('Num. edition'),
 
134
        'format':fields.char('Format', size=128),
 
135
        'price_cat': fields.many2one('library.price.category', "Price category"),
 
136
#       'categ_id': fields.many2one('product.category','Category', required=True, change_default=False),
 
137
    }
 
138
 
 
139
    _defaults = {
 
140
        'creation_date': lambda *a : time.strftime('%Y-%m-%d %H:%M:%S'),
 
141
        'state': lambda *a : 'sellable',
 
142
        'back': lambda *a : 'paper',
 
143
        'procure_method': lambda *a: 'make_to_order',
 
144
        'date_retour': lambda *a : str(int(time.strftime("%Y"))+1) + time.strftime("-%m-%d"),
 
145
    }
 
146
 
 
147
    _sql_constraints = [
 
148
        ('unique_ean13', 'unique(ean13)',  'The ean13 field must be unique across all the products'),
 
149
        ('code_uniq', 'unique (code)', 'The code of the product must be unique !')
 
150
    ]
 
151
product_product()
 
152
 
 
153
class author_book_rel(osv.osv):
 
154
    _name = "author.book.rel"
 
155
    _rec_name = "author_id"
 
156
    _columns = {
 
157
        'author_id': fields.many2one('library.author','Author', ondelete='cascade'),
 
158
        'product_id': fields.many2one('product.product','Book', ondelete='cascade')
 
159
    }
 
160
author_book_rel()
 
161
 
 
162
 
 
163
class product_product_in(osv.osv):
 
164
    _inherit = "product.product"
 
165
    _columns = {
 
166
        'author_ids': fields.many2many('library.author','author_book_rel','product_id','author_id','Authors'),
 
167
    }
 
168
    def copy(self, cr, uid, id, default=None,context={}):
 
169
        if not default:
 
170
            default = {}
 
171
        default.update({ 'author_ids':[] })
 
172
        return super(product_product_in, self).copy(cr, uid, id, default, context)
 
173
    _constraints = [
 
174
 
 
175
    ]
 
176
product_product_in()
 
177
 
 
178
class library_author(osv.osv):
 
179
    _inherit = 'library.author'
 
180
    _columns = {
 
181
        'book_ids': fields.many2many('product.product','author_book_rel','author_id','product_id','Books', select=1), 
 
182
    }
 
183
library_author()
 
184
 
 
185
 
 
186
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
187