/usr/share/pyshared/trytond/modules/party/party.py is in tryton-modules-party 2.2.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | #This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
import logging
from trytond.model import ModelView, ModelSQL, fields
from trytond.wizard import Wizard
from trytond.pyson import Bool, Eval
from trytond.transaction import Transaction
from trytond.pool import Pool
HAS_VATNUMBER = False
VAT_COUNTRIES = [('', '')]
try:
import vatnumber
HAS_VATNUMBER = True
for country in vatnumber.countries():
VAT_COUNTRIES.append((country, country))
except ImportError:
logging.getLogger('party').warning(
'Unable to import vatnumber. VAT number validation disabled.')
STATES = {
'readonly': ~Eval('active', True),
}
DEPENDS = ['active']
class Party(ModelSQL, ModelView):
"Party"
_description = __doc__
_name = "party.party"
name = fields.Char('Name', required=True, select=1,
states=STATES, depends=DEPENDS)
code = fields.Char('Code', required=True, select=1,
readonly=True, order_field="%(table)s.code_length %(order)s, " \
"%(table)s.code %(order)s")
code_length = fields.Integer('Code Length', select=1, readonly=True)
lang = fields.Many2One("ir.lang", 'Language', states=STATES,
depends=DEPENDS)
vat_number = fields.Char('VAT Number', help="Value Added Tax number",
states={
'readonly': ~Eval('active', True),
'required': Bool(Eval('vat_country')),
},
depends=['active', 'vat_country'])
vat_country = fields.Selection(VAT_COUNTRIES, 'VAT Country', states=STATES,
depends=DEPENDS,
help="Setting VAT country will enable validation of the VAT number.",
translate=False)
vat_code = fields.Function(fields.Char('VAT Code',
on_change_with=['vat_number', 'vat_country']), 'get_vat_code',
searcher='search_vat_code')
addresses = fields.One2Many('party.address', 'party',
'Addresses', states=STATES, depends=DEPENDS)
contact_mechanisms = fields.One2Many('party.contact_mechanism', 'party',
'Contact Mechanisms', states=STATES, depends=DEPENDS)
categories = fields.Many2Many('party.party-party.category',
'party', 'category', 'Categories', states=STATES, depends=DEPENDS)
active = fields.Boolean('Active', select=1)
full_name = fields.Function(fields.Char('Full Name'), 'get_full_name')
phone = fields.Function(fields.Char('Phone'), 'get_mechanism')
mobile = fields.Function(fields.Char('Mobile'), 'get_mechanism')
fax = fields.Function(fields.Char('Fax'), 'get_mechanism')
email = fields.Function(fields.Char('E-Mail'), 'get_mechanism')
website = fields.Function(fields.Char('Website'), 'get_mechanism')
def __init__(self):
super(Party, self).__init__()
self._sql_constraints = [
('code_uniq', 'UNIQUE(code)',
'The code of the party must be unique!')
]
self._constraints += [
('check_vat', 'invalid_vat'),
]
self._error_messages.update({
'invalid_vat': 'Invalid VAT number!',
})
self._order.insert(0, ('name', 'ASC'))
def default_active(self):
return True
def default_categories(self):
return Transaction().context.get('categories', [])
def default_addresses(self):
address_obj = Pool().get('party.address')
fields_names = list(x for x in set(address_obj._columns.keys()
+ address_obj._inherit_fields.keys())
if x not in ['id', 'create_uid', 'create_date',
'write_uid', 'write_date'])
return [address_obj.default_get(fields_names)]
def default_lang(self):
config_obj = Pool().get('party.configuration')
config = config_obj.browse(1)
return config.party_lang.id
def on_change_with_vat_code(self, vals):
return (vals.get('vat_country') or '') + (vals.get('vat_number') or '')
def get_vat_code(self, ids, name):
if not ids:
return []
res = {}
for party in self.browse(ids):
res[party.id] = (party.vat_country or '') + (party.vat_number or '')
return res
def search_vat_code(self, name, clause):
res = []
value = clause[2]
for country, _ in VAT_COUNTRIES:
if isinstance(value, basestring) \
and country \
and value.upper().startswith(country):
res.append(('vat_country', '=', country))
value = value[len(country):]
break
res.append(('vat_number', clause[1], value))
return res
def get_full_name(self, ids, name):
if not ids:
return []
res = {}
for party in self.browse(ids):
res[party.id] = party.name
return res
def get_mechanism(self, ids, name):
if not ids:
return []
res = {}
for party in self.browse(ids):
res[party.id] = ''
for mechanism in party.contact_mechanisms:
if mechanism.type == name:
res[party.id] = mechanism.value
break
return res
def create(self, values):
sequence_obj = Pool().get('ir.sequence')
config_obj = Pool().get('party.configuration')
values = values.copy()
if not values.get('code'):
config = config_obj.browse(1)
values['code'] = sequence_obj.get_id(config.party_sequence.id)
values['code_length'] = len(values['code'])
return super(Party, self).create(values)
def write(self, ids, vals):
if vals.get('code'):
vals = vals.copy()
vals['code_length'] = len(vals['code'])
return super(Party, self).write(ids, vals)
def copy(self, ids, default=None):
address_obj = Pool().get('party.address')
int_id = False
if isinstance(ids, (int, long)):
int_id = True
ids = [ids]
if default is None:
default = {}
default = default.copy()
default['code'] = False
default['addresses'] = False
new_ids = []
for party in self.browse(ids):
new_id = super(Party, self).copy(party.id, default=default)
address_obj.copy([x.id for x in party.addresses],
default={
'party': new_id,
})
new_ids.append(new_id)
if int_id:
return new_ids[0]
return new_ids
def search_rec_name(self, name, clause):
ids = self.search([('code',) + clause[1:]], order=[])
if ids:
ids += self.search([('name',) + clause[1:]], order=[])
return [('id', 'in', ids)]
return [('name',) + clause[1:]]
def address_get(self, party_id, type=None):
"""
Try to find an address for the given type, if no type match
the first address is return.
"""
address_obj = Pool().get("party.address")
address_ids = address_obj.search(
[("party", "=", party_id), ("active", "=", True)],
order=[('sequence', 'ASC'), ('id', 'ASC')])
if not address_ids:
return False
default_address = address_ids[0]
if not type:
return default_address
for address in address_obj.browse(address_ids):
if address[type]:
return address.id
return default_address
def check_vat(self, ids):
'''
Check the VAT number depending of the country.
http://sima-pc.com/nif.php
'''
if not HAS_VATNUMBER:
return True
for party in self.browse(ids):
vat_number = party.vat_number
if not party.vat_country:
continue
if not getattr(vatnumber, 'check_vat_' + \
party.vat_country.lower())(vat_number):
#Check if user doesn't have put country code in number
if vat_number.startswith(party.vat_country):
vat_number = vat_number[len(party.vat_country):]
self.write(party.id, {
'vat_number': vat_number,
})
else:
return False
return True
Party()
class PartyCategory(ModelSQL):
'Party - Category'
_name = 'party.party-party.category'
_table = 'party_category_rel'
_description = __doc__
party = fields.Many2One('party.party', 'Party', ondelete='CASCADE',
required=True, select=1)
category = fields.Many2One('party.category', 'Category', ondelete='CASCADE',
required=True, select=1)
PartyCategory()
class CheckVIESNoCheck(ModelView):
'Check VIES - No Check'
_name = 'party.check_vies.no_check'
_description = __doc__
CheckVIESNoCheck()
class CheckVIESCheck(ModelView):
'Check VIES - Check'
_name = 'party.check_vies.check'
_description = __doc__
parties_succeed = fields.Many2Many('party.party', None, None,
'Parties Succeed', readonly=True, states={
'invisible': ~Eval('parties_succeed'),
})
parties_failed = fields.Many2Many('party.party', None, None,
'Parties Failed', readonly=True, states={
'invisible': ~Eval('parties_failed'),
})
CheckVIESCheck()
class CheckVIES(Wizard):
'Check VIES'
_name = 'party.check_vies'
states = {
'init': {
'result': {
'type': 'choice',
'next_state': '_choice',
},
},
'no_check': {
'result': {
'type': 'form',
'object': 'party.check_vies.no_check',
'state': [
('end', 'Ok', 'tryton-ok', True),
],
},
},
'check': {
'actions': ['_check'],
'result': {
'type': 'form',
'object': 'party.check_vies.check',
'state': [
('end', 'Ok', 'tryton-ok', True),
],
},
},
}
def __init__(self):
super(CheckVIES, self).__init__()
self._error_messages.update({
'vies_unavailable': 'The VIES service is unavailable, ' \
'try again later.',
})
def _choice(self, data):
if not HAS_VATNUMBER or not hasattr(vatnumber, 'check_vies'):
return 'no_check'
return 'check'
def _check(self, data):
party_obj = Pool().get('party.party')
res = {
'parties_succeed': [],
'parties_failed': [],
}
parties = party_obj.browse(data['ids'])
for party in parties:
if not party.vat_code:
continue
try:
if not vatnumber.check_vies(party.vat_code):
res['parties_failed'].append(party.id)
else:
res['parties_succeed'].append(party.id)
except Exception, e:
if hasattr(e, 'faultstring') \
and hasattr(e.faultstring, 'find'):
if e.faultstring.find('INVALID_INPUT'):
res['parties_failed'].append(party.id)
continue
if e.faultstring.find('SERVICE_UNAVAILABLE') \
or e.faultstring.find('MS_UNAVAILABLE') \
or e.faultstring.find('TIMEOUT') \
or e.faultstring.find('SERVER_BUSY'):
self.raise_user_error('vies_unavailable')
raise
return res
CheckVIES()
|