~eduardo-bayardo-bias/openobject-addons/addons_interface

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python
#-*- coding:utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution    
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
#    d$
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    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
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import time
import netsvc
from osv import fields, osv
from mx import DateTime
from tools import config
from tools.translate import _

class res_partner(osv.osv):
    _inherit = 'res.partner'
    _columns = {
        'over_credit':fields.boolean('Allow Over Credit?', required=False),
    }

    def create(self, cr, uid, data, context=None):
        if 'vat' in data:
            query="select id from res_partner where translate(upper(vat), ' ''-_', '')=translate(upper('%s'), ' ''-_', '')"
            cr.execute(query%data['vat'])
            if cr.fetchall():
                raise osv.except_osv(_('Error !'), _('El RFC de la empresa ya existe'))
        return super(res_partner, self).create(cr, uid, data, context)

    def write(self, cr, uid, ids, vals, context=None):
        group_id = self.pool.get('res.partner.address').get_obj(cr, uid, 'res.groups', 'account.group_account_invoice')
        invoice_group_users = [x.id for x in self.pool.get('res.groups').browse(cr, uid, group_id).users]
        group_id = self.pool.get('res.partner.address').get_obj(cr, uid, 'res.groups', 'account_payment.group_account_payment')
        payment_group_users = [x.id for x in self.pool.get('res.groups').browse(cr, uid, group_id).users]
        if 'vat' in vals:
            query="select id from res_partner where translate(upper(vat), ' ''-_', '')=translate(upper('%s'), ' ''-_', '') AND id != %s"
            cr.execute(query%(vals['vat'], ids[0]))
            if not uid in invoice_group_users:
                raise osv.except_osv(_('Error !'), _('Solo los usuarios en el grupo Accounting / Invoice pueden modificar el RFC'))
            elif cr.fetchall():
                raise osv.except_osv(_('Error !'), _('El RFC de la empresa ya existe'))
        if (('over_credit' in vals) or ('credit_limit' in vals) or ('property_product_pricelist' in vals) or ('property_payment_term' in vals)) and not (uid in payment_group_users):
                raise osv.except_osv(_('Error !'), _('Solo los usuarios en el grupo Accounting / Payment pueden modificar el RFC'))
        return super(res_partner, self).write(cr, uid, ids, vals, context=context)

res_partner()

class res_partner_address(osv.osv):
    _inherit = 'res.partner.address'

    def get_obj(self, cr, uid, model, external_id):
        # Return id from Model and External ID
        obj_id = False
        try:
            external_id = external_id.split('.')[1]
        except:
            osv.except_osv(_('Warning!'), _('Such External ID does not Exist!'))
        model_data_pool = self.pool.get('ir.model.data')
        ir_model_data_id = model_data_pool.search(cr, uid, [('model', '=', model), ('name', '=', external_id)])
        if ir_model_data_id:
            ir_model_data_obj = model_data_pool.browse(cr, uid, ir_model_data_id)[0]
            obj = self.pool.get(model).browse(cr, uid, ir_model_data_obj.res_id)
            obj_id = obj and obj.id
        else:
            osv.except_osv(_('Warning!'), _('Such External ID does not Exist!'))
        return obj_id

    def write(self, cr, uid, ids, vals, context=None):
        group_id = self.get_obj(cr, uid, 'res.groups', 'account.group_account_invoice')
        users = [x.id for x in self.pool.get('res.groups').browse(cr, uid, group_id).users]
        if ('type' in vals) and (vals['type']=='invoice'):
            if not uid in users:
                raise osv.except_osv(_('Error !'), _('Solo los usuarios en el grupo Accounting / Invoice pueden modificar el contacto de facturación'))
        else:
            for rec in self.browse(cr, uid, ids, context=context):
                if rec.type == 'invoice' and not (uid in users):
                    raise osv.except_osv(_('Error !'), _('Solo los usuarios en el grupo Accounting / Invoice pueden modificar el contacto de facturación'))
        return super(res_partner_address, self).write(cr, uid, ids, vals, context=context)

    def unlink(self, cr, uid, ids, context=None):
        if context is None:
            context = {}
        group_id = self.get_obj(cr, uid, 'res.groups', 'account.group_account_invoice')
        users = [x.id for x in self.pool.get('res.groups').browse(cr, uid, group_id).users]
        for rec in self.browse(cr, uid, ids, context=context):
                if rec.type == 'invoice' and not (uid in users):
                    raise osv.except_osv(_('Error !'), _('Solo los usuarios en el grupo Accounting / Invoice pueden eliminar el contacto de facturación'))
        return super(res_partner_address, self).unlink(cr, uid, ids, context=context)

res_partner_address()

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: