|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright 2020 ACSONE SA/NV |
| 3 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
| 4 | + |
| 5 | +from odoo import api, fields, models, _ |
| 6 | +from odoo.osv.expression import NEGATIVE_TERM_OPERATORS |
| 7 | + |
| 8 | + |
| 9 | +class StockLocation(models.Model): |
| 10 | + |
| 11 | + _inherit = "stock.location" |
| 12 | + |
| 13 | + product_restriction = fields.Selection( |
| 14 | + string="Product restriction", |
| 15 | + selection="_selection_product_restriction", |
| 16 | + help="If 'Same product' is selected the system will prevent to put " |
| 17 | + "items of different products into the same location.", |
| 18 | + index=True, |
| 19 | + required=True, |
| 20 | + compute="_compute_product_restriction", |
| 21 | + store=True, |
| 22 | + default="any", |
| 23 | + ) |
| 24 | + |
| 25 | + specific_product_restriction = fields.Selection( |
| 26 | + string="Specific product restriction", |
| 27 | + selection="_selection_product_restriction", |
| 28 | + help="If specified the restriction specified will apply to " |
| 29 | + "the current location and all its children", |
| 30 | + default=False, |
| 31 | + ) |
| 32 | + |
| 33 | + parent_product_restriction = fields.Selection( |
| 34 | + selection="_selection_product_restriction", |
| 35 | + store=True, |
| 36 | + readonly=True, |
| 37 | + related="location_id.product_restriction", |
| 38 | + ) |
| 39 | + |
| 40 | + has_restriction_violation = fields.Boolean( |
| 41 | + compute="_compute_restriction_violation", |
| 42 | + search="_search_has_restriction_violation", |
| 43 | + ) |
| 44 | + |
| 45 | + restriction_violation_message = fields.Char( |
| 46 | + compute="_compute_restriction_violation" |
| 47 | + ) |
| 48 | + |
| 49 | + @api.model |
| 50 | + def _selection_product_restriction(self): |
| 51 | + return [ |
| 52 | + ("any", "Items of any products are allowed into the location"), |
| 53 | + ( |
| 54 | + "same", |
| 55 | + "Only items of the same product allowed into the location", |
| 56 | + ), |
| 57 | + ] |
| 58 | + |
| 59 | + @api.depends("specific_product_restriction", "parent_product_restriction") |
| 60 | + def _compute_product_restriction(self): |
| 61 | + default_value = "any" |
| 62 | + for rec in self: |
| 63 | + rec.product_restriction = ( |
| 64 | + rec.specific_product_restriction |
| 65 | + or rec.parent_product_restriction |
| 66 | + or default_value |
| 67 | + ) |
| 68 | + |
| 69 | + @api.depends("product_restriction") |
| 70 | + def _compute_restriction_violation(self): |
| 71 | + records = self |
| 72 | + if self.env.in_onchange: |
| 73 | + records = self._origin |
| 74 | + if not records: |
| 75 | + # case where the compute is called from the create form |
| 76 | + return |
| 77 | + ProductProduct = self.env["product.product"] |
| 78 | + SQL = """ |
| 79 | + SELECT |
| 80 | + stock_quant.location_id, |
| 81 | + array_agg(distinct(product_id)) |
| 82 | + FROM |
| 83 | + stock_quant, |
| 84 | + stock_location |
| 85 | + WHERE |
| 86 | + stock_quant.location_id in %s |
| 87 | + and stock_location.id = stock_quant.location_id |
| 88 | + and stock_location.product_restriction = 'same' |
| 89 | + GROUP BY |
| 90 | + stock_quant.location_id |
| 91 | + HAVING count(distinct(product_id)) > 1 |
| 92 | + """ |
| 93 | + self.env.cr.execute(SQL, (tuple(records.ids),)) |
| 94 | + product_ids_by_location_id = dict(self.env.cr.fetchall()) |
| 95 | + for record in self: |
| 96 | + record_id = record.id |
| 97 | + if self.env.in_onchange: |
| 98 | + record_id = self._origin.id |
| 99 | + has_restriction_violation = False |
| 100 | + restriction_violation_message = False |
| 101 | + product_ids = product_ids_by_location_id.get(record_id) |
| 102 | + if product_ids: |
| 103 | + products = ProductProduct.browse(product_ids) |
| 104 | + has_restriction_violation = True |
| 105 | + restriction_violation_message = _( |
| 106 | + "This location should only contain items of the same " |
| 107 | + "product but it contains items of products %s" |
| 108 | + ) % " | ".join(products.mapped("name")) |
| 109 | + record.has_restriction_violation = has_restriction_violation |
| 110 | + record.restriction_violation_message = ( |
| 111 | + restriction_violation_message |
| 112 | + ) |
| 113 | + |
| 114 | + def _search_has_restriction_violation(self, operator, value): |
| 115 | + search_has_violation = ( |
| 116 | + # has_restriction_violation != False |
| 117 | + (operator in NEGATIVE_TERM_OPERATORS and not value) |
| 118 | + or |
| 119 | + # has_restriction_violation = True |
| 120 | + (operator not in NEGATIVE_TERM_OPERATORS and value) |
| 121 | + ) |
| 122 | + SQL = """ |
| 123 | + SELECT |
| 124 | + stock_quant.location_id |
| 125 | + FROM |
| 126 | + stock_quant, |
| 127 | + stock_location |
| 128 | + WHERE |
| 129 | + stock_location.id = stock_quant.location_id |
| 130 | + and stock_location.product_restriction = 'same' |
| 131 | + GROUP BY |
| 132 | + stock_quant.location_id |
| 133 | + HAVING count(distinct(product_id)) > 1 |
| 134 | + """ |
| 135 | + self.env.cr.execute(SQL) |
| 136 | + violation_ids = [r[0] for r in self.env.cr.fetchall()] |
| 137 | + if search_has_violation: |
| 138 | + op = "in" |
| 139 | + else: |
| 140 | + op = "not in" |
| 141 | + return [("id", op, violation_ids)] |
0 commit comments