diff --git a/pos_stock_available_online/models/__init__.py b/pos_stock_available_online/models/__init__.py index ceb6084326..6120b3e7f2 100644 --- a/pos_stock_available_online/models/__init__.py +++ b/pos_stock_available_online/models/__init__.py @@ -1,5 +1,7 @@ from . import pos_config from . import pos_session from . import res_config_settings +from . import stock_notifier_pos_mixin from . import stock_quant from . import stock_warehouse +from . import stock_move diff --git a/pos_stock_available_online/models/stock_move.py b/pos_stock_available_online/models/stock_move.py new file mode 100644 index 0000000000..b9eefc1873 --- /dev/null +++ b/pos_stock_available_online/models/stock_move.py @@ -0,0 +1,30 @@ +from odoo import models + + +class StockMove(models.Model): + _name = "stock.move" + _inherit = ["stock.move", "stock.notifier.pos.mixin"] + + def write(self, vals): + states_dict = {line.id: line.state for line in self} if "state" in vals else {} + res = super().write(vals) + if not states_dict: + return res + for line in self: + if states_dict.get(line.id) != line.state: + line._notify_pos() + return res + + def _action_done(self, cancel_backorder=False): + # As stock will be updated once move has updated its state, skip + # notification from quant + return super( + StockMove, self.with_context(skip_quant_notify_pos=True) + )._action_done(cancel_backorder=cancel_backorder) + + def _get_warehouses_to_notify(self): + warehouses = super()._get_warehouses_to_notify() + warehouses |= self.warehouse_id + warehouses |= self.location_id.warehouse_id + warehouses |= self.location_dest_id.warehouse_id + return warehouses diff --git a/pos_stock_available_online/models/stock_notifier_pos_mixin.py b/pos_stock_available_online/models/stock_notifier_pos_mixin.py new file mode 100644 index 0000000000..2f5912bd11 --- /dev/null +++ b/pos_stock_available_online/models/stock_notifier_pos_mixin.py @@ -0,0 +1,51 @@ +from odoo import models + + +class StockNotifierPosMixin(models.AbstractModel): + _name = "stock.notifier.pos.mixin" + _description = "Stock Notifier POS Mixin" + + def _prepare_pos_message(self): + """ + Return prepared message to send to POS + """ + self.ensure_one() + return self.warehouse_id._prepare_vals_for_pos(self.product_id) + + def _skip_notify_pos(self): + """ + Skip notification to POS + """ + return False + + def _get_warehouses_to_notify(self): + self.ensure_one() + return self.env["stock.warehouse"] + + def _notify_pos(self): + """ + Send notification to POS + """ + pos_session_obj = self.env["pos.session"] + for record in self: + if record._skip_notify_pos(): + continue + for warehouse in self._get_warehouses_to_notify(): + configs = pos_session_obj.search( + [ + ("state", "=", "opened"), + ("config_id.display_product_quantity", "=", True), + "|", + ("config_id.additional_warehouse_ids", "in", [warehouse.id]), + ("config_id.main_warehouse_id", "=", warehouse.id), + "|", + ("config_id.iface_available_categ_ids", "=", False), + ( + "config_id.iface_available_categ_ids", + "in", + [record.product_id.pos_categ_id.id], + ), + ], + ).mapped("config_id") + if configs: + configs._notify_available_quantity(record._prepare_pos_message()) diff --git a/pos_stock_available_online/models/stock_quant.py b/pos_stock_available_online/models/stock_quant.py index 8fa75e21f1..4c6b282d77 100644 --- a/pos_stock_available_online/models/stock_quant.py +++ b/pos_stock_available_online/models/stock_quant.py @@ -1,47 +1,23 @@ -import logging - from odoo import models -_logger = logging.getLogger(__name__) - class StockQuant(models.Model): - _inherit = "stock.quant" - - def _prepare_pos_message(self): - """ - Return prepared message to send to POS - """ - self.ensure_one() - return self.warehouse_id._prepare_vals_for_pos(self.product_id) - - def _notify_pos(self): - """ - Send notification to POS - """ - pos_session_obj = self.env["pos.session"] - for quant in self: - warehouse_id = quant.warehouse_id.id - configs = pos_session_obj.search( - [ - ("state", "=", "opened"), - ("config_id.display_product_quantity", "=", True), - "|", - ("config_id.additional_warehouse_ids", "in", [warehouse_id]), - ("config_id.main_warehouse_id", "=", warehouse_id), - "|", - ("config_id.iface_available_categ_ids", "=", False), - ( - "config_id.iface_available_categ_ids", - "in", - [quant.product_id.pos_categ_id.id], - ), - ], - ).mapped("config_id") - if configs: - configs._notify_available_quantity(quant._prepare_pos_message()) + _name = "stock.quant" + _inherit = ["stock.quant", "stock.notifier.pos.mixin"] def write(self, vals): res = super().write(vals) self._notify_pos() return res + + def _skip_notify_pos(self): + self.ensure_one() + return ( + self.env.context.get("skip_quant_notify_pos", False) + or super()._skip_notify_pos() + ) + + def _get_warehouses_to_notify(self): + warehouses = super()._get_warehouses_to_notify() + warehouses |= self.warehouse_id + return warehouses