File: party.py

package info (click to toggle)
tryton-modules-web-shop-vue-storefront 7.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 456 kB
  • sloc: python: 2,246; xml: 58; makefile: 11; sh: 3
file content (135 lines) | stat: -rw-r--r-- 4,908 bytes parent folder | download
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
# This file is part of Tryton.  The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from stdnum import get_cc_module

try:
    import phonenumbers
    from phonenumbers import NumberParseException, PhoneNumberFormat
except ImportError:
    phonenumbers = None

from trytond.i18n import gettext
from trytond.model import fields
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Eval
from trytond.tools import remove_forbidden_chars

from .exceptions import BadRequest
from .web import join_name, split_name


class Address(metaclass=PoolMeta):
    __name__ = 'party.address'

    vsf_telephone = fields.Many2One(
        'party.contact_mechanism', "Telephone",
        domain=[
            ('party', '=', Eval('party', -1)),
            ('type', 'in', ['phone', 'mobile']),
            ])

    def get_vsf(self, for_party=None):
        if for_party and for_party != self.party:
            firstname, lastname = split_name(
                self.party_name or for_party.name)
        else:
            firstname, lastname = split_name(
                self.party_name or self.party.name)
        address = {
            'id': self.id,
            'firstname': firstname,
            'lastname': lastname,
            'street': self.street.splitlines(),
            'city': self.city,
            'country_id': self.country.code if self.country else None,
            'postcode': self.postal_code,
            }
        if self.subdivision:
            address['region'] = {
                'region': self.subdivision.name,
                }
        if self.vsf_telephone:
            address['telephone'] = self.vsf_telephone.value
        if for_party:
            address['company'] = self.party.name
            address['vat_id'] = (
                self.party.tax_identifier.code
                if self.party.tax_identifier else None)
        return address

    def set_vsf(self, data, for_party=None):
        pool = Pool()
        Country = pool.get('country.country')
        Subdivision = pool.get('country.subdivision')
        ContactMechanism = pool.get('party.contact_mechanism')

        name = remove_forbidden_chars(
            join_name(data['firstname'], data['lastname']))
        party = for_party or self.party
        if name != party.name:
            self.party_name = name
        self.street = '\n'.join(map(str, data['street']))
        self.city = remove_forbidden_chars(data['city'])
        if data['country_id']:
            try:
                self.country, = Country.search([
                        ('code', '=', data['country_id']),
                        ], limit=1)
            except ValueError:
                raise BadRequest(gettext(
                        'web_shop_vue_storefront.msg_unknown_country_code',
                        code=data['country_id']))
        self.postal_code = data['postcode']

        if data.get('region') and data['region']['region']:
            domain = [
                ('name', '=', data['region']['region']),
                ]
            if self.country:
                domain.append(('country', '=', self.country.id))
            subdivisions = Subdivision.search(domain, limit=1)
            if subdivisions:
                self.subdivision, = subdivisions

        if data.get('telephone'):
            value = remove_forbidden_chars(data['telephone'])
            if phonenumbers:
                try:
                    phonenumber = phonenumbers.parse(
                        data['telephone'], data['country_id'])
                    value = phonenumbers.format_number(
                        phonenumber, PhoneNumberFormat.INTERNATIONAL)
                except NumberParseException:
                    pass
            contacts = ContactMechanism.search([
                    ('party', '=', self.party.id),
                    ('type', 'in', ['phone', 'mobile']),
                    ('value', '=', value),
                    ], limit=1)
            if contacts:
                self.vsf_telephone, = contacts
            else:
                contact = ContactMechanism(
                    party=self.party,
                    type='phone',
                    value=value)
                contact.save()
                self.vsf_telephone = contact


class Identifier(metaclass=PoolMeta):
    __name__ = 'party.identifier'

    def set_vsf_tax_identifier(self, code):
        pool = Pool()
        Party = pool.get('party.party')
        for type in Party.tax_identifier_types():
            module = get_cc_module(*type.split('_', 1))
            if module and module.is_valid(code):
                self.type = type
                self.code = code
                break
        else:
            raise BadRequest(gettext(
                    'web_shop_vue_storefront.msg_invalid_tax_identifier',
                    code=code))