|
| 1 | +# Copyright 2023 ACSONE SA/NV |
| 2 | +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). |
| 3 | +from odoo import api, fields, models |
| 4 | +from odoo.osv.expression import NEGATIVE_TERM_OPERATORS |
| 5 | +from odoo.tools import float_compare, float_is_zero |
| 6 | + |
| 7 | + |
| 8 | +class StockPicking(models.Model): |
| 9 | + |
| 10 | + _inherit = "stock.picking" |
| 11 | + |
| 12 | + is_completed = fields.Boolean( |
| 13 | + compute="_compute_is_completed", |
| 14 | + search="_search_is_completed", |
| 15 | + help="This field means if all picking operations have been done (each quantity is > 0)", |
| 16 | + ) |
| 17 | + |
| 18 | + @api.depends( |
| 19 | + "state", |
| 20 | + "move_ids_without_package.quantity_done", |
| 21 | + "move_line_ids.qty_done", |
| 22 | + "move_ids.quantity_done", |
| 23 | + ) |
| 24 | + def _compute_is_completed(self): |
| 25 | + """ |
| 26 | + We must implement several fields value retrieval to allow a good behaviour on |
| 27 | + form side as several fields can be filled in: |
| 28 | + - move_line_ids.qty_done |
| 29 | + - move_ids_without_package |
| 30 | + """ |
| 31 | + precision_digits = self.env["decimal.precision"].precision_get( |
| 32 | + "Product Unit of Measure" |
| 33 | + ) |
| 34 | + # We must use recordset (and not ids) as web interface can trigger this |
| 35 | + # and record arrives here with NewId |
| 36 | + ready_pickings = self.browse() |
| 37 | + for picking in self.filtered(lambda picking: picking.state == "assigned"): |
| 38 | + if all( |
| 39 | + not float_is_zero(move_line.qty_done, precision_digits=precision_digits) |
| 40 | + and float_compare( |
| 41 | + move_line.qty_done, |
| 42 | + move_line.reserved_uom_qty, |
| 43 | + precision_digits=precision_digits, |
| 44 | + ) |
| 45 | + >= 0 |
| 46 | + for move_line in picking.move_line_ids.filtered( |
| 47 | + lambda m: m.state not in ("done", "cancel") |
| 48 | + ) |
| 49 | + ): |
| 50 | + ready_pickings |= picking |
| 51 | + elif all( |
| 52 | + not float_is_zero( |
| 53 | + move_id.quantity_done, precision_digits=precision_digits |
| 54 | + ) |
| 55 | + and float_compare( |
| 56 | + move_id.quantity_done, |
| 57 | + move_id.product_uom_qty, |
| 58 | + precision_digits=precision_digits, |
| 59 | + ) |
| 60 | + >= 0 |
| 61 | + for move_id in picking.move_ids.filtered( |
| 62 | + lambda m: m.state not in ("done", "cancel") |
| 63 | + ) |
| 64 | + ): |
| 65 | + ready_pickings |= picking |
| 66 | + ready_pickings.update({"is_completed": True}) |
| 67 | + (self - ready_pickings).update({"is_completed": False}) |
| 68 | + |
| 69 | + def _get_is_completed_domain(self): |
| 70 | + return [ |
| 71 | + ("move_ids.quantity_done", ">", 0), |
| 72 | + ("state", "not in", ["done", "cancel"]), |
| 73 | + ] |
| 74 | + |
| 75 | + def _get_is_not_completed_domain(self): |
| 76 | + return [ |
| 77 | + "|", |
| 78 | + ("move_ids.quantity_done", "=", 0), |
| 79 | + ("state", "in", ["done", "cancel"]), |
| 80 | + ] |
| 81 | + |
| 82 | + def _search_is_completed(self, operator, value): |
| 83 | + negative = operator in NEGATIVE_TERM_OPERATORS |
| 84 | + |
| 85 | + if (value and negative) or (not value and not negative): |
| 86 | + pickings = self.search(self._get_is_not_completed_domain()) |
| 87 | + returned_pickings = pickings.filtered_domain([("is_completed", "=", False)]) |
| 88 | + else: |
| 89 | + pickings = self.search(self._get_is_completed_domain()) |
| 90 | + returned_pickings = pickings.filtered_domain([("is_completed", "=", True)]) |
| 91 | + |
| 92 | + return [("id", "in", returned_pickings.ids)] |
0 commit comments