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

« back to all changes in this revision

Viewing changes to library/library_editor_supplier.py

  • Committer: Jay vora
  • Date: 2008-06-09 08:43:58 UTC
  • Revision ID: jvo@tinyerp.com-c8b7029321d4f9d451745e462547a609739e1c2c

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##############################################################################
 
2
#
 
3
# Copyright (c) 2004-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
 
4
#
 
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
 
 
31
# class library_editor_supplier(osv.osv):
 
32
#       _name = "library.editor.supplier"
 
33
#       _description = "Suppliers associated to an editor"
 
34
#       _auto = False
 
35
#       _columns = {
 
36
#               'name': fields.many2one('res.partner', 'Editor', readonly= True),
 
37
#               'supplier_ids' : fields.many2many('res.partner','editor_supplier_rel','editor_id','supplier_id', 'Suppliers',readonly= True),
 
38
#       }
 
39
#       def init(self, cr):
 
40
#               cr.execute("""
 
41
#                       create or replace view library_editor_supplier as (
 
42
#                               select editor as id, editor as name from product_product group by editor
 
43
#                               )
 
44
#                       """)
 
45
# library_editor_supplier()
 
46
 
 
47
class library_editor_supplier(osv.osv):
 
48
        _name = "library.editor.supplier"
 
49
        _description = "many2many view for editor relations"
 
50
        _auto = False
 
51
        _columns = {
 
52
                'name': fields.many2one('res.partner', 'Editor'),
 
53
                'supplier_id' : fields.many2one('res.partner', 'Supplier'),
 
54
        }
 
55
 
 
56
        def init(self, cr):
 
57
                cr.execute("""
 
58
 
 
59
                        create or replace view library_editor_supplier as (
 
60
                                select
 
61
                                 case when min(ps.id) is null then - min(pp.id) else min(ps.id) end as id,
 
62
                                 case when pp.editor is null then 0 else pp.editor end as name,
 
63
                                 case when ps.name is null then 0 else ps.name end as supplier_id
 
64
                                from
 
65
                                 product_supplierinfo ps full outer join product_product pp on (ps.product_id = pp.product_tmpl_id)
 
66
                                where
 
67
                                 ((pp.editor is not null) or (ps.name is not null))
 
68
                                group by pp.editor, ps.name
 
69
                                 )""")
 
70
 
 
71
 
 
72
        def create(self, cr, user, vals, context={}):
 
73
                if not (vals['name'] and vals['supplier_id']) : raise osv.except_osv("Error","Please provide ..")
 
74
                # search for books of these editor not already linked with this supplier :
 
75
                select = 'select product_tmpl_id from product_product where editor = %s and id not in (select product_id from product_supplierinfo where name = %s)'%(vals['name'],vals['supplier_id'])
 
76
                cr.execute(select)
 
77
                if not cr.rowcount:
 
78
                        raise osv.except_osv("Error","No book to apply this relation")
 
79
 
 
80
                sup_info = self.pool.get('product.supplierinfo')
 
81
                last_id = 0
 
82
                for book_id in cr.fetchall():
 
83
                        tmp_id = sup_info.create(cr,user,{'name': vals['supplier_id'], 'product_id': book_id[0]},context)
 
84
                        last_id = last_id < tmp_id and last_id or tmp_id
 
85
                return last_id
 
86
        
 
87
 
 
88
        def unlink(self, cr, uid, ids, context={}):
 
89
 
 
90
                relations = self.browse(cr,uid,ids)
 
91
                for rel in relations:
 
92
                        if not (rel.name and rel.supplier_id) : continue
 
93
                        # search for the equivalent ids in product_supplierinfo (unpack the group)
 
94
                        cr.execute("select si.id from product_supplierinfo si join product_product pp on (si.product_id = pp.product_tmpl_id ) where pp.editor = %s and si.name = %s" %(rel.name.id,rel.supplier_id.id) )
 
95
                        ids = [x[0] for x in cr.fetchall()]
 
96
                        self.pool.get('product.supplierinfo').unlink(cr,uid,ids,context)
 
97
                return True
 
98
 
 
99
#       cr.execute('select name, supplier_id from library_editor_supplier where id in ('+','.join(map(str,ids))+')' )
 
100
 
 
101
 
 
102
        def write(self, cr, user, ids, vals, context={}):
 
103
                raise osv.except_osv("Warning","Only creation and suppression are allowed in this form.")
 
104
 
 
105
library_editor_supplier()