Debian Codegraph @

addons/delivery_mondialrelay/wizard/choose_delivery_carrier.py

raw
1 # -*- coding: utf-8 -*-
2 # Part of Odoo. See LICENSE file for full copyright and licensing details.
3
4 from odoo import fields, models, api, _
5 from odoo.tools.json import scriptsafe as json_safe
6 from odoo.exceptions import ValidationError
7
8
9 class ChooseDeliveryCarrier(models.TransientModel):
10 _inherit = 'choose.delivery.carrier'
11
12 shipping_zip = fields.Char(related='order_id.partner_shipping_id.zip')
13 shipping_country_code = fields.Char(related='order_id.partner_shipping_id.country_id.code')
14
15 is_mondialrelay = fields.Boolean(compute='_compute_is_mondialrelay')
16 mondialrelay_last_selected = fields.Char(string="Last Relay Selected")
17 mondialrelay_last_selected_id = fields.Char(compute='_compute_mr_last_selected_id')
18 mondialrelay_brand = fields.Char(related='carrier_id.mondialrelay_brand')
19 mondialrelay_colLivMod = fields.Char(related='carrier_id.mondialrelay_packagetype')
20 mondialrelay_allowed_countries = fields.Char(compute='_compute_mr_allowed_countries')
21
22 @api.depends('carrier_id')
23 def _compute_is_mondialrelay(self):
24 self.ensure_one()
25 self.is_mondialrelay = self.carrier_id.product_id.default_code == "MR"
26
27 @api.depends('carrier_id', 'order_id.partner_shipping_id')
28 def _compute_mr_last_selected_id(self):
29 self.ensure_one()
30 if self.order_id.partner_shipping_id.is_mondialrelay:
31 self.mondialrelay_last_selected_id = '%s-%s' % (
32 self.shipping_country_code,
33 self.order_id.partner_shipping_id.ref.lstrip('MR#'),
34 )
35 else:
36 self.mondialrelay_last_selected_id = ''
37
38 @api.depends('carrier_id')
39 def _compute_mr_allowed_countries(self):
40 self.ensure_one()
41 self.mondialrelay_allowed_countries = ','.join(self.carrier_id.country_ids.mapped('code')).upper() or ''
42
43 def button_confirm(self):
44 if self.carrier_id.is_mondialrelay:
45 if not self.mondialrelay_last_selected:
46 raise ValidationError(_('Please, choose a Parcel Point'))
47 data = json_safe.loads(self.mondialrelay_last_selected)
48 partner_shipping = self.order_id.partner_id._mondialrelay_search_or_create({
49 'id': data['id'],
50 'name': data['name'],
51 'street': data['street'],
52 'street2': data['street2'],
53 'zip': data['zip'],
54 'city': data['city'],
55 'country_code': data['country'][:2].lower(),
56 })
57 if partner_shipping != self.order_id.partner_shipping_id:
58 self.order_id.partner_shipping_id = partner_shipping
59
60 return super().button_confirm()