|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 3 | + |
| 4 | +from odoo import _, api, fields, models, Command |
| 5 | +from odoo.exceptions import UserError |
| 6 | + |
| 7 | + |
| 8 | +class ReturnPicking(models.TransientModel): |
| 9 | + _inherit = "stock.return.picking" |
| 10 | + |
| 11 | + picking_ids = fields.Many2many("stock.picking", string="Pickings") |
| 12 | + picking_type_id = fields.Many2one("stock.picking.type", string="Operation Type") |
| 13 | + allowed_product_ids = fields.Many2many( |
| 14 | + "product.product", string="Allowed Products" |
| 15 | + ) |
| 16 | + |
| 17 | + @api.model |
| 18 | + def default_get(self, fields): |
| 19 | + active_ids = self.get_active_ids() |
| 20 | + if len(active_ids) == 1: |
| 21 | + return super().default_get(fields) |
| 22 | + res = {} |
| 23 | + pickings = self.env["stock.picking"].browse(active_ids) |
| 24 | + if any(p.state != "done" for p in pickings): |
| 25 | + raise UserError(_("All selected pickings must be in 'Done' state.")) |
| 26 | + |
| 27 | + picking_type = pickings[0].picking_type_id |
| 28 | + if any(p.picking_type_id != picking_type for p in pickings): |
| 29 | + raise UserError( |
| 30 | + _("All selected pickings must have the same operation type.") |
| 31 | + ) |
| 32 | + if picking_type.code == "outgoing": |
| 33 | + if any(not p.sale_id for p in pickings): |
| 34 | + raise UserError(_("All selected pickings must have a sale order.")) |
| 35 | + else: |
| 36 | + for picking in pickings: |
| 37 | + purchase_order = self.env["purchase.order"].search( |
| 38 | + [("picking_ids", "in", picking.ids)], limit=1 |
| 39 | + ) |
| 40 | + if not purchase_order: |
| 41 | + raise UserError( |
| 42 | + _("All selected pickings must have a purchase order.") |
| 43 | + ) |
| 44 | + |
| 45 | + res = { |
| 46 | + "picking_ids": [Command.set(active_ids)], |
| 47 | + "picking_id": pickings[0].id, |
| 48 | + "picking_type_id": picking_type.id, |
| 49 | + "allowed_product_ids": [Command.set(pickings.mapped("move_ids.product_id").ids)], |
| 50 | + "product_return_moves": [Command.clear()], |
| 51 | + } |
| 52 | + |
| 53 | + return res |
| 54 | + |
| 55 | + def get_active_ids(self): |
| 56 | + active_ids = self.env.context.get("active_ids", []) |
| 57 | + if isinstance(active_ids, int): |
| 58 | + active_ids = [active_ids] |
| 59 | + return active_ids |
| 60 | + |
| 61 | + def _get_picking_ids(self): |
| 62 | + return self.product_return_moves.filtered( |
| 63 | + lambda line: line.quantity > 0 |
| 64 | + ).mapped("picking_id") |
| 65 | + |
| 66 | + def _get_origin(self): |
| 67 | + new_origin = "Return of " |
| 68 | + for picking in self._get_picking_ids(): |
| 69 | + new_origin += picking.origin + ", " |
| 70 | + return new_origin[:-2] |
| 71 | + |
| 72 | + def create_picking_wizard(self, picking_id): |
| 73 | + return self.env["stock.return.picking"].create( |
| 74 | + { |
| 75 | + "picking_id": picking_id.id, |
| 76 | + "product_return_moves": [ |
| 77 | + Command.create( |
| 78 | + { |
| 79 | + "product_id": line.product_id.id, |
| 80 | + "quantity": line.quantity, |
| 81 | + "to_refund": line.to_refund, |
| 82 | + "move_id": line.move_id.id, |
| 83 | + }, |
| 84 | + ) |
| 85 | + for line in self.product_return_moves.filtered( |
| 86 | + lambda l: l.picking_id == picking_id |
| 87 | + ) |
| 88 | + ], |
| 89 | + } |
| 90 | + ) |
| 91 | + |
| 92 | + def action_create_returns_all(self): |
| 93 | + if len(self.get_active_ids()) == 1: |
| 94 | + return super().action_create_returns_all() |
| 95 | + new_picking = self._create_return() |
| 96 | + |
| 97 | + for picking in self._get_picking_ids(): |
| 98 | + picking.write({"new_return_ids": [Command.link(new_picking.id)]}) |
| 99 | + last_message = self.env["mail.message"].search( |
| 100 | + [("res_id", "=", new_picking.id), ("model", "=", "stock.picking")], |
| 101 | + order="id desc", |
| 102 | + limit=1, |
| 103 | + ) |
| 104 | + if last_message: |
| 105 | + last_message.unlink() |
| 106 | + new_picking.message_post_with_source( |
| 107 | + "mail.message_origin_link", |
| 108 | + render_values={"self": new_picking, "origin": self._get_picking_ids()}, |
| 109 | + subtype_xmlid="mail.mt_note", |
| 110 | + ) |
| 111 | + |
| 112 | + vals = { |
| 113 | + "sale_id": self._get_picking_ids()[0].sale_id.id, |
| 114 | + "sale_ids": self._get_picking_ids().mapped("sale_id").ids, |
| 115 | + "origin": self._get_origin(), |
| 116 | + "return_id": self._get_picking_ids()[0].id |
| 117 | + } |
| 118 | + |
| 119 | + new_picking.write(vals) |
| 120 | + return { |
| 121 | + "name": _("Returned Picking"), |
| 122 | + "view_mode": "form", |
| 123 | + "res_model": "stock.picking", |
| 124 | + "res_id": new_picking.id, |
| 125 | + "type": "ir.actions.act_window", |
| 126 | + "context": self.env.context, |
| 127 | + } |
| 128 | + |
| 129 | + def action_create_returns(self): |
| 130 | + new_pickings = None |
| 131 | + active_ids = self.get_active_ids() |
| 132 | + if len(active_ids) == 1: |
| 133 | + return super().action_create_returns() |
| 134 | + |
| 135 | + for picking_id in self._get_picking_ids(): |
| 136 | + picking_return_wizard = self.create_picking_wizard(picking_id) |
| 137 | + |
| 138 | + new_pickings =picking_return_wizard.with_context( |
| 139 | + active_ids=picking_id.id |
| 140 | + )._create_return() |
| 141 | + new_pickings.sale_id = picking_id.sale_id.id |
| 142 | + new_pickings.return_id = picking_id.id |
| 143 | + |
| 144 | + return { |
| 145 | + "name": _("Returned Pickings"), |
| 146 | + "view_mode": "form", |
| 147 | + "res_model": "stock.picking", |
| 148 | + "res_id": new_pickings.id, |
| 149 | + "type": "ir.actions.act_window", |
| 150 | + "context": self.env.context, |
| 151 | + } |
| 152 | + |
| 153 | + def create_exchanges(self): |
| 154 | + active_ids = self.get_active_ids() |
| 155 | + if len(active_ids) == 1: |
| 156 | + return super().action_create_exchanges() |
| 157 | + else: |
| 158 | + action = None |
| 159 | + for picking_id in self.picking_ids: |
| 160 | + picking_return_wizard = self.create_picking_wizard(picking_id) |
| 161 | + action = picking_return_wizard.with_context( |
| 162 | + active_ids=picking_id.id |
| 163 | + ).action_create_exchanges() |
| 164 | + return action |
0 commit comments