~tempo-openerp/+junk/axima_fix

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# -*- coding: utf-8 -*-
##############################################################################
#
#    Original Module for OpenERP, Open Source Management Solution
#    Copyright (C) 20014 TeMPO CONSULTING (<http://tempo-consulting.fr>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from openerp.osv import osv, fields


class res_company(osv.Model):

    """Extend the res.company model to meet AXIMA requirements"""
    _inherit = 'res.company'

    def _get_city2(self, cr, uid, ids, fieldname, args, context=None):
        """
        city: extract of that is before 'CEDEX'
        """
        def remove_cedex(city):
            tokens = (' CEDEX', ' Cedex', ' cedex', )
            cedex_index = -1
            for t in tokens:
                cedex_index = city.find(t)
                if cedex_index >= 0:
                    break
            if cedex_index >= 0:
                if cedex_index == 0:
                    city = ''
                else:
                    city = city[0:cedex_index]
            return city.rstrip(' ')
        
        if isinstance(ids, (int, long)):
            ids = [ids]
        res = {}
        sub_company_city = False
        
        user = self.pool.get('res.users').browse(cr, uid, [uid],
            context=context)
        if user and user[0] and user[0].sub_company_address:
            # city is sub company address one
            # get last line of address, and pass zip code
            pos = user[0].sub_company_address.rfind("\n")
            if pos:
                tmp_city = user[0].sub_company_address[pos:]  # last line
                if tmp_city:
                    pos = tmp_city.find(' ')  # pass zip code
                    if pos:
                        sub_company_city = remove_cedex(tmp_city[pos:])
            if sub_company_city:
                return { id: sub_company_city for id in ids }
                    
        for r in self.read(cr, uid, ids, ['city'], context=context):
            res[r['id']] = remove_cedex(r['city'])
        return res

    def _get_region_selection(self, cursor, user_id, context=None):
        """Return the tuple containing valid values for the region field"""
        regions = (u"CENTRE", u"Dpt Industries", u"EST",  u"NORD", u"OUEST",
                   u"PARIS – EST",  u"SUD EST", u"SUD OUEST")
        selection = []
        for region in regions:
            selection.append((region, region))
        # (('key_or_value', 'string_to_display'), ... )
        return tuple(selection)

    _post_sales_person_label = u"Responsable du service après-vente"
    _post_sales_as_label = u"Assistants du service après-vente"

    _columns = {
                'sap_company_code': fields.char(u"Référence SAP",
                                                size=10,
                                                required=True,
                                                select=1),
                'office_code': fields.char(u"Code agence",
                                                  size=10,
                                                  required=True,
                                                  select=1),
                'region': fields.selection(_get_region_selection,
                                           u"Région",
                                           required=False
                                           # required=True,
                                           ),
                'person_in_charge': fields.many2one('res.partner',
                                                    u"Responsable de l’agence",
                                                    required=True),
                'post_sales_person': fields.many2one('res.partner',
                                                     _post_sales_person_label,
                                                     required=False),
                'post_sales_assistants': fields.many2many('res.partner',
                                                          'company_psa_rel',
                                                          'company_id',
                                                          'partner_id',
                                                          _post_sales_as_label,
                                                          required=False
                                                          ),
                'company_assitants': fields.many2many('res.partner',
                                                      'company_ca_rel',
                                                      'company_id',
                                                      'partner_id',
                                                      u"Assistants d'agence",
                                                      required=False,
                                                      # required=True, # les
                                                      # données sont incomplète
                                                      ),
                'region_director': fields.many2one('res.partner',
                                                   u"Directeur de région",
                                                   # required=True # peut-être,
                                                   # mais pas avec l’état
                                                   # actuel de la base
                                                   required=False,
                                                   ),
                'city2': fields.function(_get_city2, type='char', string="Ville", method=True),
    }
 
    _defaults = {
        'sap_company_code': False,
        'office_code': False,
        'region': False,
        'person_in_charge': False,
        'post_sales_person': False,
        'post_sales_assistants': False,
        'company_assitants': False,
        'region_director': False,
    }