|
| 1 | +# Copyright 2024 Akretion (http://www.akretion.com). |
| 2 | +# @author Florian Mounier <florian.mounier@akretion.com> |
| 3 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
| 4 | +from functools import reduce |
| 5 | + |
| 6 | +from odoo import _, models |
| 7 | +from odoo.exceptions import UserError |
| 8 | + |
| 9 | + |
| 10 | +class StockPicking(models.Model): |
| 11 | + _inherit = "stock.picking" |
| 12 | + |
| 13 | + def _is_batch_roulier(self): |
| 14 | + # Check if the picking is part of a batch with a roulier carrier |
| 15 | + self.ensure_one() |
| 16 | + return ( |
| 17 | + self.batch_id |
| 18 | + and self.batch_id.carrier_id |
| 19 | + and self.batch_id.carrier_id._is_roulier() |
| 20 | + ) |
| 21 | + |
| 22 | + def send_to_shipper(self): |
| 23 | + self.ensure_one() |
| 24 | + if self._is_batch_roulier(): |
| 25 | + # We are in a batch with a roulier carrier |
| 26 | + # We need to send unsent packages |
| 27 | + packages = self.package_ids - self.batch_id.sent_package_ids |
| 28 | + if not packages: |
| 29 | + # Nothing to send |
| 30 | + return |
| 31 | + |
| 32 | + # First sanity checks |
| 33 | + # Check that all package pickings have the same sender/receiver: |
| 34 | + for package in packages: |
| 35 | + package_pickings = ( |
| 36 | + self.env["stock.move.line"] |
| 37 | + .search( |
| 38 | + [ |
| 39 | + "|", |
| 40 | + ("result_package_id", "=", package.id), |
| 41 | + ("package_id", "=", package.id), |
| 42 | + ] |
| 43 | + ) |
| 44 | + .mapped("picking_id") |
| 45 | + ) |
| 46 | + |
| 47 | + # Set carrier on pickings |
| 48 | + package_pickings.write({"carrier_id": self.carrier_id.id}) |
| 49 | + |
| 50 | + # Check sender/receiver uniformity |
| 51 | + for kind in ("sender", "receiver"): |
| 52 | + addresses = reduce( |
| 53 | + lambda x, y: x | y, |
| 54 | + ( |
| 55 | + getattr(package_picking, f"_get_{kind}")() |
| 56 | + or self.env["res.partner"] |
| 57 | + for package_picking in package_pickings |
| 58 | + ), |
| 59 | + ) |
| 60 | + if not addresses: |
| 61 | + raise UserError( |
| 62 | + _( |
| 63 | + "Can't determine %(kind)s address for pickings: %(pickings)s" |
| 64 | + ) |
| 65 | + % { |
| 66 | + "kind": kind, |
| 67 | + "pickings": ", ".join(package_pickings.mapped("name")), |
| 68 | + } |
| 69 | + ) |
| 70 | + if len(addresses) > 1: |
| 71 | + raise UserError( |
| 72 | + _( |
| 73 | + "Multiple %(kind)s addresses found for pickings: %(pickings)s" |
| 74 | + ) |
| 75 | + % { |
| 76 | + "kind": kind, |
| 77 | + "pickings": ", ".join(package_pickings.mapped("name")), |
| 78 | + } |
| 79 | + ) |
| 80 | + |
| 81 | + # Send packages |
| 82 | + res = self.batch_id.carrier_id.send_shipping(self)[0] |
| 83 | + # Mark packages as sent (for use in _roulier_generate_labels) |
| 84 | + self.batch_id.sent_package_ids |= packages |
| 85 | + # Update tracking number |
| 86 | + if res["tracking_number"]: |
| 87 | + self.batch_id.carrier_tracking_ref = ";".join( |
| 88 | + [ |
| 89 | + tracking |
| 90 | + for tracking in ( |
| 91 | + self.batch_id.carrier_tracking_ref, |
| 92 | + res["tracking_number"], |
| 93 | + ) |
| 94 | + if tracking |
| 95 | + ] |
| 96 | + ) |
| 97 | + return |
| 98 | + |
| 99 | + return super().send_to_shipper() |
| 100 | + |
| 101 | + def _roulier_generate_labels(self): |
| 102 | + if self._is_batch_roulier(): |
| 103 | + label_info = [] |
| 104 | + for picking in self: |
| 105 | + # Generate labels only for unsent packages |
| 106 | + packages = picking.package_ids - picking.batch_id.sent_package_ids |
| 107 | + label_info.append(packages._generate_labels(picking)) |
| 108 | + return label_info |
| 109 | + |
| 110 | + return super()._roulier_generate_labels() |
| 111 | + |
| 112 | + def get_shipping_label_values(self, label): |
| 113 | + self.ensure_one() |
| 114 | + if self._is_batch_roulier(): |
| 115 | + # Attach the label to the batch instead of the picking |
| 116 | + return { |
| 117 | + "name": label["name"], |
| 118 | + "res_id": self.batch_id.id, |
| 119 | + "res_model": "stock.picking.batch", |
| 120 | + "datas": label["file"], |
| 121 | + "file_type": label["file_type"], |
| 122 | + } |
| 123 | + return super().get_shipping_label_values(label) |
0 commit comments