~vauxoo/addons-vauxoo/6.0-trunk

« back to all changes in this revision

Viewing changes to product_customs_rate/product_customs_rate.py

  • Committer: Gabriela (Vauxoo)
  • Date: 2012-01-02 16:24:49 UTC
  • Revision ID: gabrielaquilarque97@gmail.com-20120102162449-lhxnrtif2ud36du2

[ADD] Added new module invoice_so.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- encoding: utf-8 -*-
2
 
##############################################################################
3
 
#
4
 
#    OpenERP, Open Source Management Solution
5
 
#    Copyright (C) 2011 Cubic ERP - Teradata SAC (<http://cubicerp.com>).
6
 
#       (Modified by)   Vauxoo - http://www.vauxoo.com/
7
 
#                       info Vauxoo (info@vauxoo.com)
8
 
#    Modified by - juan@vauxoo.com
9
 
#    This program is free software: you can redistribute it and/or modify
10
 
#    it under the terms of the GNU Affero General Public License as
11
 
#    published by the Free Software Foundation, either version 3 of the
12
 
#    License, or (at your option) any later version.
13
 
#
14
 
#    This program is distributed in the hope that it will be useful,
15
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 
#    GNU Affero General Public License for more details.
18
 
#
19
 
#    You should have received a copy of the GNU Affero General Public License
20
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 
#
22
 
##############################################################################
23
 
 
24
 
from openerp.osv import osv, fields
25
 
 
26
 
 
27
 
class product_customs_rate(osv.Model):
28
 
 
29
 
    def name_get(self, cr, uid, ids, context=None):
30
 
        if not len(ids):
31
 
            return []
32
 
        reads = self.read(cr, uid, ids, ['name', 'code'], context=context)
33
 
        res = []
34
 
        for record in reads:
35
 
            name = record['name']
36
 
            if record['code']:
37
 
                name = record['code']+': '+name
38
 
            res.append((record['id'], name))
39
 
        return res
40
 
 
41
 
    def name_search(self, cr, user, name, args=None, operator='ilike',
42
 
                    context=None, limit=100):
43
 
        if not args:
44
 
            args = []
45
 
        args = args[:]
46
 
        ids = []
47
 
        if name:
48
 
            ids = self.search(cr, user, [(
49
 
                'code', '=like', name+"%")]+args, limit=limit)
50
 
            if not ids:
51
 
                ids = self.search(cr, user, [(
52
 
                    'name', operator, name)] + args, limit=limit)
53
 
            if not ids and len(name.split()) >= 2:
54
 
                # Separating code and name of account for searching
55
 
                operand1, operand2 = name.split(
56
 
                    ': ', 1)  # name can contain spaces e.g. OpenERP S.A.
57
 
                ids = self.search(cr, user, [('code', operator, operand1), (
58
 
                    'name', operator, operand2)] + args, limit=limit)
59
 
        else:
60
 
            ids = self.search(cr, user, args, context=context, limit=limit)
61
 
        return self.name_get(cr, user, ids, context=context)
62
 
 
63
 
    def _name_get_fnc(self, cr, uid, ids, prop, unknow_none, context=None):
64
 
        res = self.name_get(cr, uid, ids, context=context)
65
 
        return dict(res)
66
 
 
67
 
    _name = 'product.customs.rate'
68
 
    _description = 'Customs Rate'
69
 
    _columns = {
70
 
        'code': fields.char('Code', size=64),
71
 
        'name': fields.char('Name', size=2048, required=True, translate=True,
72
 
                            select=True),
73
 
        'active': fields.boolean('Active'),
74
 
        'complete_name': fields.function(_name_get_fnc, method=True,
75
 
                                         type="char", string='Name'),
76
 
        'parent_id': fields.many2one('product.customs.rate',
77
 
                                     'Parent Customs Rate', select=True,
78
 
                                     domain=[('type', '=', 'view')]),
79
 
        'child_ids': fields.one2many('product.customs.rate', 'parent_id',
80
 
                                     string='Child Customs Rate'),
81
 
        'type': fields.selection([('view', 'View'), ('normal', 'Normal')],
82
 
                                 'Customs Rate Type', required=True),
83
 
        'tax_ids': fields.many2many('account.tax', 'product_customs_rate_tax',
84
 
                                    'customs_rate_id', 'tax_id', 'Taxes')
85
 
 
86
 
    }
87
 
    _defaults = {
88
 
        'active': 1,
89
 
        'type': 'normal',
90
 
    }
91
 
    _order = "code"
92
 
 
93
 
    def _check_recursion(self, cr, uid, ids, context=None):
94
 
        level = 100
95
 
        while len(ids):
96
 
            cr.execute('select distinct parent_id\
97
 
                        from product_customs_rate where id IN %s', (
98
 
                tuple(ids),))
99
 
            ids = filter(None, map(lambda x: x[0], cr.fetchall()))
100
 
            if not level:
101
 
                return False
102
 
            level -= 1
103
 
        return True
104
 
 
105
 
    _constraints = [
106
 
        (_check_recursion, 'Error !\
107
 
            You can not create recursive Customs Rate.', [
108
 
         'parent_id'])
109
 
    ]
110
 
 
111
 
    def child_get(self, cr, uid, ids):
112
 
        return [ids]