|
| 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 odoo import _, api, fields, models |
| 5 | +from odoo.exceptions import UserError |
| 6 | +from odoo.tools.float_utils import float_compare |
| 7 | + |
| 8 | + |
| 9 | +class StockPickingBatch(models.Model): |
| 10 | + _inherit = "stock.picking.batch" |
| 11 | + |
| 12 | + def _get_default_weight_uom(self): |
| 13 | + return self.env[ |
| 14 | + "product.template" |
| 15 | + ]._get_weight_uom_name_from_ir_config_parameter() |
| 16 | + |
| 17 | + carrier_price = fields.Float(string="Shipping Cost") |
| 18 | + delivery_type = fields.Selection(related="carrier_id.delivery_type", readonly=True) |
| 19 | + carrier_id = fields.Many2one( |
| 20 | + "delivery.carrier", string="Carrier", check_company=True |
| 21 | + ) |
| 22 | + weight = fields.Float( |
| 23 | + compute="_compute_weight", |
| 24 | + digits="Stock Weight", |
| 25 | + store=True, |
| 26 | + help="Total weight of the products in the picking.", |
| 27 | + compute_sudo=True, |
| 28 | + ) |
| 29 | + carrier_tracking_ref = fields.Char(string="Tracking Reference", copy=False) |
| 30 | + carrier_tracking_url = fields.Char( |
| 31 | + string="Tracking URL", compute="_compute_carrier_tracking_url" |
| 32 | + ) |
| 33 | + weight_uom_name = fields.Char( |
| 34 | + string="Weight unit of measure label", |
| 35 | + compute="_compute_weight_uom_name", |
| 36 | + readonly=True, |
| 37 | + default=_get_default_weight_uom, |
| 38 | + ) |
| 39 | + sent_package_ids = fields.One2many( |
| 40 | + "stock.quant.package", |
| 41 | + "batch_id", |
| 42 | + string="Packages", |
| 43 | + ) |
| 44 | + |
| 45 | + @api.constrains("carrier_id") |
| 46 | + def _check_carrier_id_is_roulier(self): |
| 47 | + for batch in self: |
| 48 | + if batch.carrier_id and not batch.carrier_id._is_roulier(): |
| 49 | + raise UserError(_("Only Roulier carrier is supported")) |
| 50 | + |
| 51 | + def _compute_weight_uom_name(self): |
| 52 | + for package in self: |
| 53 | + package.weight_uom_name = self.env[ |
| 54 | + "product.template" |
| 55 | + ]._get_weight_uom_name_from_ir_config_parameter() |
| 56 | + |
| 57 | + @api.depends("carrier_id", "carrier_tracking_ref") |
| 58 | + def _compute_carrier_tracking_url(self): |
| 59 | + for batch in self: |
| 60 | + batch.carrier_tracking_url = ( |
| 61 | + ( |
| 62 | + # Similar flawed logic as in delivery_roulier |
| 63 | + batch.picking_ids.package_ids[0]._get_tracking_link() |
| 64 | + if batch.carrier_tracking_ref |
| 65 | + and len(batch.picking_ids.package_ids) > 0 |
| 66 | + else False |
| 67 | + ) |
| 68 | + if batch.carrier_id and batch.carrier_id._is_roulier() |
| 69 | + else False |
| 70 | + ) |
| 71 | + |
| 72 | + @api.depends("move_ids") |
| 73 | + def _compute_weight(self): |
| 74 | + for batch in self: |
| 75 | + batch.weight = sum( |
| 76 | + move.weight for move in batch.move_ids if move.state != "cancel" |
| 77 | + ) |
| 78 | + |
| 79 | + def cancel_shipment(self): |
| 80 | + for batch in self: |
| 81 | + batch.carrier_id.cancel_shipment(self) |
| 82 | + msg = "Shipment %s cancelled" % batch.carrier_tracking_ref |
| 83 | + batch.message_post(body=msg) |
| 84 | + batch.carrier_tracking_ref = False |
| 85 | + batch.sent_package_ids = [(5, 0, 0)] |
| 86 | + |
| 87 | + def action_done(self): |
| 88 | + if not self.carrier_id or not ( |
| 89 | + self.carrier_id.integration_level == "rate_and_ship" |
| 90 | + and self.picking_type_id.code != "incoming" |
| 91 | + ): |
| 92 | + return super().action_done() |
| 93 | + |
| 94 | + self.ensure_one() |
| 95 | + pickings = self.picking_ids.filtered( |
| 96 | + lambda picking: picking.state not in ("cancel", "done") |
| 97 | + ) |
| 98 | + if pickings.carrier_id - self.carrier_id: |
| 99 | + raise UserError( |
| 100 | + _("Pickings %(pickings)s already have a different carrier") |
| 101 | + % { |
| 102 | + "pickings": ", ".join( |
| 103 | + pickings.filtered( |
| 104 | + lambda p: p.carrier_id and p.carrier_id != self.carrier_id |
| 105 | + ).mapped("name") |
| 106 | + ) |
| 107 | + } |
| 108 | + ) |
| 109 | + pickings.write({"carrier_id": self.carrier_id.id}) |
| 110 | + # Delivery Roulier works with packages, so we need to generate a package |
| 111 | + # if it doesn't exist, this is simalar to action_put_in_pack but without |
| 112 | + # the checks |
| 113 | + picking_move_lines = self.move_line_ids |
| 114 | + move_line_ids = picking_move_lines.filtered( |
| 115 | + lambda ml: float_compare( |
| 116 | + ml.qty_done, 0.0, precision_rounding=ml.product_uom_id.rounding |
| 117 | + ) |
| 118 | + > 0 |
| 119 | + and not ml.result_package_id |
| 120 | + ) |
| 121 | + if not move_line_ids: |
| 122 | + move_line_ids = picking_move_lines.filtered( |
| 123 | + lambda ml: float_compare( |
| 124 | + ml.product_uom_qty, |
| 125 | + 0.0, |
| 126 | + precision_rounding=ml.product_uom_id.rounding, |
| 127 | + ) |
| 128 | + > 0 |
| 129 | + and float_compare( |
| 130 | + ml.qty_done, 0.0, precision_rounding=ml.product_uom_id.rounding |
| 131 | + ) |
| 132 | + == 0 |
| 133 | + ) |
| 134 | + if move_line_ids: |
| 135 | + move_line_ids.picking_id[0]._put_in_pack(move_line_ids, False) |
| 136 | + |
| 137 | + return super().action_done() |
| 138 | + |
| 139 | + def open_website_url(self): |
| 140 | + """Open tracking page. |
| 141 | +
|
| 142 | + More than 1 tracking number: display a list of packages |
| 143 | + Else open directly the tracking page |
| 144 | + """ |
| 145 | + self.ensure_one() |
| 146 | + if not self.carrier_id or not self.carrier_id._is_roulier(): |
| 147 | + return super().open_website_url() |
| 148 | + |
| 149 | + packages = self.sent_package_ids |
| 150 | + if len(packages) == 0: |
| 151 | + raise UserError(_("No packages found for this picking")) |
| 152 | + elif len(packages) == 1: |
| 153 | + return packages.open_website_url() # shortpath |
| 154 | + |
| 155 | + # display a list of pickings |
| 156 | + xmlid = "stock.action_package_view" |
| 157 | + action = self.env["ir.actions.act_window"]._for_xml_id(xmlid) |
| 158 | + action["domain"] = [("id", "in", packages.ids)] |
| 159 | + action["context"] = {"batch_id": self.id} |
| 160 | + return action |
0 commit comments