|
| 1 | +# Copyright 2021 Camptocamp SA |
| 2 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) |
| 3 | + |
| 4 | +from lxml import etree |
| 5 | + |
| 6 | +from odoo import fields, models |
| 7 | +from odoo.osv import expression |
| 8 | +from odoo.tools.safe_eval import safe_eval |
| 9 | + |
| 10 | +from odoo.addons.base.models.ir_ui_view import ( |
| 11 | + transfer_modifiers_to_node, |
| 12 | + transfer_node_to_modifiers, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +class StockPicking(models.Model): |
| 17 | + _inherit = "stock.picking" |
| 18 | + |
| 19 | + delivery_notification_sent = fields.Boolean(default=False) |
| 20 | + |
| 21 | + def _send_confirmation_email(self): |
| 22 | + for picking in self: |
| 23 | + skip_delivery_cost = picking._handle_send_to_shipper_at_operation() |
| 24 | + picking = picking.with_context(skip_delivery_cost=skip_delivery_cost) |
| 25 | + super(StockPicking, picking)._send_confirmation_email() |
| 26 | + |
| 27 | + def _handle_send_to_shipper_at_operation(self): |
| 28 | + """Send the delivery notice to the carrier from a specific operation type. |
| 29 | +
|
| 30 | + We are only interested by sending the delivery notice, the delivery fee |
| 31 | + still have to be added to the SO by the ship operation. |
| 32 | +
|
| 33 | + Return True if the operation has send the delivery notice. |
| 34 | + """ |
| 35 | + self.ensure_one() |
| 36 | + if not self.carrier_id: |
| 37 | + # If the current operation has no carrier defined, but a carrier |
| 38 | + # has been found from the ship and is configured to match the |
| 39 | + # current operation type: force the sending of the delivery notice |
| 40 | + # to the carrier |
| 41 | + related_ship = self.ship_picking_id |
| 42 | + carrier = related_ship.carrier_id |
| 43 | + if ( |
| 44 | + carrier.integration_level == "rate_and_ship" |
| 45 | + and carrier.send_delivery_notice_on == "custom" |
| 46 | + and self.picking_type_id |
| 47 | + in carrier.send_delivery_notice_picking_type_ids |
| 48 | + ): |
| 49 | + self.carrier_id = carrier |
| 50 | + self.with_context(skip_delivery_cost=True).send_to_shipper() |
| 51 | + # Flag the current operation and the ship one. |
| 52 | + # Mandatory to not execute twice 'send_to_shipper' method |
| 53 | + self.delivery_notification_sent = True |
| 54 | + related_ship.delivery_notification_sent = True |
| 55 | + related_ship.carrier_price = self.carrier_price |
| 56 | + related_ship.carrier_tracking_ref = self.carrier_tracking_ref |
| 57 | + return True |
| 58 | + return False |
| 59 | + |
| 60 | + def send_to_shipper(self): |
| 61 | + # Do not send delivery notice to the carrier if it has already been sent |
| 62 | + # through a previous operation (like a pack) |
| 63 | + self.ensure_one() |
| 64 | + if self.delivery_notification_sent: |
| 65 | + # But we still need to add the delivery cost to the SO |
| 66 | + self._add_delivery_cost_to_so() |
| 67 | + return False |
| 68 | + return super().send_to_shipper() |
| 69 | + |
| 70 | + def _add_delivery_cost_to_so(self): |
| 71 | + if self.env.context.get("skip_delivery_cost"): |
| 72 | + return |
| 73 | + return super()._add_delivery_cost_to_so() |
| 74 | + |
| 75 | + def fields_view_get( |
| 76 | + self, view_id=None, view_type="form", toolbar=False, submenu=False |
| 77 | + ): |
| 78 | + # Override to hide the "Send to shipper" button if the delivery |
| 79 | + # notification has already been sent |
| 80 | + result = super().fields_view_get( |
| 81 | + view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu |
| 82 | + ) |
| 83 | + if result.get("name") == "stock.picking.form": |
| 84 | + result["arch"] = self._fields_view_get_adapt_send_to_shipper_attrs( |
| 85 | + result["arch"] |
| 86 | + ) |
| 87 | + return result |
| 88 | + |
| 89 | + def _fields_view_get_adapt_send_to_shipper_attrs(self, view_arch): |
| 90 | + """Hide 'Send to Shipper' button if 'delivery_notification_sent' is True.""" |
| 91 | + doc = etree.XML(view_arch) |
| 92 | + xpath_expr = "//button[@name='send_to_shipper']" |
| 93 | + attrs_key = "invisible" |
| 94 | + nodes = doc.xpath(xpath_expr) |
| 95 | + for field in nodes: |
| 96 | + attrs = safe_eval(field.attrib.get("attrs", "{}")) |
| 97 | + if not attrs[attrs_key]: |
| 98 | + continue |
| 99 | + invisible_domain = expression.OR( |
| 100 | + [attrs[attrs_key], [("delivery_notification_sent", "=", True)]] |
| 101 | + ) |
| 102 | + attrs[attrs_key] = invisible_domain |
| 103 | + field.set("attrs", str(attrs)) |
| 104 | + modifiers = {} |
| 105 | + transfer_node_to_modifiers( |
| 106 | + field, modifiers, self.env.context, in_tree_view=True |
| 107 | + ) |
| 108 | + transfer_modifiers_to_node(modifiers, field) |
| 109 | + return etree.tostring(doc, encoding="unicode") |
0 commit comments