~pexego/openobject-addons/6.1-pexego-sale_commission

« back to all changes in this revision

Viewing changes to sale_follow_up/partner.py

  • Committer: Omar (pexego)
  • Date: 2012-07-27 08:40:22 UTC
  • Revision ID: omar@pexego.es-20120727084022-qp3ludpr3vsuyuf6
[ADD] Traceability modules ported to 6.1

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-2011 Pexego (<www.pexego.es>). All Rights Reserved
 
6
#    $Omar Castiñeira Saavedra$
 
7
#
 
8
#    This program is free software: you can redistribute it and/or modify
 
9
#    it under the terms of the GNU General Public License as published by
 
10
#    the Free Software Foundation, either version 3 of the License, or
 
11
#    (at your option) any later version.
 
12
#
 
13
#    This program is distributed in the hope that it will be useful,
 
14
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
#    GNU General Public License for more details.
 
17
#
 
18
#    You should have received a copy of the GNU General Public License
 
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
#
 
21
##############################################################################
 
22
 
 
23
"""inherit res.partner for adds the functionally that a partner have a location"""
 
24
 
 
25
from osv import osv, fields
 
26
 
 
27
class res_partner(osv.osv):
 
28
    """inherit res.partner for adds the functionally that customer has a location"""
 
29
    _inherit = 'res.partner'
 
30
 
 
31
    _columns = {
 
32
        'create_customer_location': fields.boolean('Create customer location', help="If check creates a location if not exists for this customer to follow sent goods.")
 
33
    }
 
34
 
 
35
    _defaults = {
 
36
        'create_customer_location': lambda *a: True
 
37
    }
 
38
 
 
39
    def _set_partner_customer_location(self, cr, uid, partner_id, context=None):
 
40
        """creates customer location for partner in arguments"""
 
41
        if context is None: context = {}
 
42
        
 
43
        partner_id_obj = self.browse(cr, uid, partner_id)
 
44
 
 
45
        #checks if already exists partner location
 
46
        locations = self.pool.get('stock.location').search(cr, uid, [('partner_id', '=', partner_id)])
 
47
 
 
48
        if not locations:
 
49
            #creates a customer location for a specific partner if the partner is a costumer
 
50
            partner_location_id = self.pool.get('stock.location').create(cr, uid, vals = {
 
51
                                                        'location_id': partner_id_obj.property_stock_customer and partner_id_obj.property_stock_customer.id or False,
 
52
                                                        'name':partner_id_obj.name,
 
53
                                                        'usage': 'customer',
 
54
                                                        'partner_id': partner_id,
 
55
                                                        'company_id': self.pool.get('res.users').browse(cr, uid, uid).company_id.id
 
56
                                                    }, context=context)
 
57
 
 
58
            #updates the property of stock customer location for partner
 
59
            self.write(cr, uid, partner_id, vals={
 
60
                            'property_stock_customer': partner_location_id,
 
61
                        }, context=context)
 
62
 
 
63
        return True
 
64
 
 
65
    def create(self, cr, uid, vals, context=None):
 
66
        """Check to create customer location"""
 
67
        if context is None: context = {}
 
68
        partner_id = super(res_partner, self).create(cr, uid, vals, context = context)
 
69
 
 
70
        if vals.get('create_customer_location', False):
 
71
            self._set_partner_customer_location(cr, uid, partner_id, context=context)
 
72
            
 
73
        return partner_id
 
74
 
 
75
    def write(self, cr, uid, ids, vals, context=None):
 
76
        """Check to create customer location"""
 
77
        if context is None: context = {}
 
78
        res = super(res_partner, self).write(cr, uid, ids, vals, context = context)
 
79
 
 
80
        if vals.get('create_customer_location', False):
 
81
            if isinstance(ids, (int, long)):
 
82
                ids = [ids]
 
83
                
 
84
            for partner_id in ids:
 
85
                self._set_partner_customer_location(cr, uid, partner_id, context=context)
 
86
 
 
87
        return res
 
88
 
 
89
res_partner()
 
90
 
 
91
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: