|
| 1 | +from odoo import api, fields, models, _ |
| 2 | +from collections import defaultdict |
| 3 | + |
| 4 | + |
| 5 | +class ProductProduct(models.Model): |
| 6 | + _inherit = 'product.product' |
| 7 | + |
| 8 | + def _get_variant_list(self): |
| 9 | + """Returns a list of product variants grouped by attribute name.""" |
| 10 | + self.ensure_one() |
| 11 | + |
| 12 | + grouped_values = defaultdict(set) |
| 13 | + |
| 14 | + for line in self.attribute_line_ids: |
| 15 | + attr_name = line.attribute_id.name |
| 16 | + for value in line.value_ids: |
| 17 | + grouped_values[attr_name].add(value.name) |
| 18 | + |
| 19 | + variant_list = [] |
| 20 | + for attr_name, values in grouped_values.items(): |
| 21 | + variant_list.append({ |
| 22 | + 'name': attr_name, |
| 23 | + 'values': [ |
| 24 | + { |
| 25 | + 'name': value_name, |
| 26 | + 'search': f'{self.name} {value_name}', |
| 27 | + } |
| 28 | + for value_name in sorted(values) |
| 29 | + ], |
| 30 | + }) |
| 31 | + |
| 32 | + return variant_list |
| 33 | + |
| 34 | + def _get_warehouse_list(self): |
| 35 | + """Returns a list of warehouses with overall quantities and details by location.""" |
| 36 | + self.ensure_one() |
| 37 | + |
| 38 | + product = self |
| 39 | + warehouse_obj = self.env['stock.warehouse'] |
| 40 | + location_obj = self.env['stock.location'] |
| 41 | + quant_obj = self.env['stock.quant'] |
| 42 | + move_obj = self.env['stock.move'] |
| 43 | + |
| 44 | + warehouses = warehouse_obj.search([]) |
| 45 | + |
| 46 | + warehouse_list = [] |
| 47 | + |
| 48 | + for warehouse in warehouses: |
| 49 | + # Total quantities per warehouse using context |
| 50 | + available_qty = product.with_context(warehouse=warehouse.id).qty_available |
| 51 | + forecasted_qty = product.with_context(warehouse=warehouse.id).virtual_available |
| 52 | + |
| 53 | + # Internal warehouse locations |
| 54 | + internal_locations = location_obj.search([ |
| 55 | + ('usage', '=', 'internal'), |
| 56 | + ('location_id', 'child_of', warehouse.view_location_id.id), |
| 57 | + ]) |
| 58 | + location_ids = internal_locations.ids |
| 59 | + |
| 60 | + # Stock available by location |
| 61 | + quants = quant_obj.read_group( |
| 62 | + domain=[ |
| 63 | + ('product_id', '=', product.id), |
| 64 | + ('location_id', 'in', location_ids), |
| 65 | + ], |
| 66 | + fields=['quantity', 'location_id'], |
| 67 | + groupby=['location_id'], |
| 68 | + ) |
| 69 | + |
| 70 | + # Future incoming and outgoing movements |
| 71 | + incoming_moves = move_obj.read_group( |
| 72 | + domain=[ |
| 73 | + ('product_id', '=', product.id), |
| 74 | + ('location_dest_id', 'in', location_ids), |
| 75 | + ('state', 'in', ['confirmed', 'waiting', 'assigned']), |
| 76 | + ], |
| 77 | + fields=['product_uom_qty', 'location_dest_id'], |
| 78 | + groupby=['location_dest_id'], |
| 79 | + ) |
| 80 | + |
| 81 | + outgoing_moves = move_obj.read_group( |
| 82 | + domain=[ |
| 83 | + ('product_id', '=', product.id), |
| 84 | + ('location_id', 'in', location_ids), |
| 85 | + ('state', 'in', ['confirmed', 'waiting', 'assigned']), |
| 86 | + ], |
| 87 | + fields=['product_uom_qty', 'location_id'], |
| 88 | + groupby=['location_id'], |
| 89 | + ) |
| 90 | + |
| 91 | + # Index quantities by location |
| 92 | + available_by_location = { |
| 93 | + q['location_id'][0]: q['quantity'] for q in quants |
| 94 | + } |
| 95 | + incoming_by_location = { |
| 96 | + m['location_dest_id'][0]: m['product_uom_qty'] for m in incoming_moves |
| 97 | + } |
| 98 | + outgoing_by_location = { |
| 99 | + m['location_id'][0]: m['product_uom_qty'] for m in outgoing_moves |
| 100 | + } |
| 101 | + |
| 102 | + # Build list of locations |
| 103 | + location_list = [] |
| 104 | + for loc in internal_locations: |
| 105 | + loc_id = loc.id |
| 106 | + available = available_by_location.get(loc_id, 0.0) |
| 107 | + incoming = incoming_by_location.get(loc_id, 0.0) |
| 108 | + outgoing = outgoing_by_location.get(loc_id, 0.0) |
| 109 | + forecasted = available + incoming - outgoing |
| 110 | + |
| 111 | + # SOnly show relevant locations (if any exist) |
| 112 | + if available or incoming or outgoing: |
| 113 | + location_list.append({ |
| 114 | + 'name': loc.display_name, |
| 115 | + 'available_quantity': available, |
| 116 | + 'forecasted_quantity': forecasted, |
| 117 | + }) |
| 118 | + |
| 119 | + # Build warehouse result |
| 120 | + warehouse_list.append({ |
| 121 | + 'name': warehouse.name, |
| 122 | + 'available_quantity': available_qty, |
| 123 | + 'forecasted_quantity': forecasted_qty, |
| 124 | + 'uom': product.uom_id.name, |
| 125 | + 'locations': sorted(location_list, key=lambda x: x['name']), |
| 126 | + }) |
| 127 | + |
| 128 | + return warehouse_list |
| 129 | + |
| 130 | + def get_product_info_pos(self, price, quantity, pos_config_id): |
| 131 | + |
| 132 | + res = super().get_product_info_pos(price, quantity, pos_config_id) |
| 133 | + |
| 134 | + pos_config = self.env['pos.config'].browse(pos_config_id) |
| 135 | + |
| 136 | + # Display warehouses with or without locations according to configuration |
| 137 | + if pos_config.display_product_locations: |
| 138 | + res['warehouses'] = self._get_warehouse_list() |
| 139 | + |
| 140 | + # Group variants with the same attribute |
| 141 | + res['variants'] = self._get_variant_list() |
| 142 | + |
| 143 | + return res |
0 commit comments