|
| 1 | +from odoo import models |
| 2 | +from odoo.exceptions import ValidationError |
| 3 | + |
| 4 | +class SaleOrder(models.Model): |
| 5 | + _inherit = 'sale.order' |
| 6 | + |
| 7 | + # ------------------------------------------------------------------------- |
| 8 | + # METHODS |
| 9 | + # ------------------------------------------------------------------------- |
| 10 | + |
| 11 | + def create_delivery_order(self, warehouse, order_lines): |
| 12 | + """Create a delivery order (picking) for the given warehouse and order lines.""" |
| 13 | + |
| 14 | + delivery_picking = self.env['stock.picking'].create({ |
| 15 | + 'partner_id': self.partner_id.id, |
| 16 | + 'picking_type_id': warehouse.out_type_id.id, |
| 17 | + 'location_id': warehouse.lot_stock_id.id, |
| 18 | + 'location_dest_id': self.partner_id.property_stock_customer.id, |
| 19 | + 'origin': self.name, |
| 20 | + }) |
| 21 | + |
| 22 | + for line in order_lines: |
| 23 | + self.env['stock.move'].create({ |
| 24 | + 'picking_id': delivery_picking.id, |
| 25 | + 'name': line.product_id.name, |
| 26 | + 'product_id': line.product_id.id, |
| 27 | + 'product_uom_qty': line.product_uom_qty, |
| 28 | + 'product_uom': line.product_uom.id, |
| 29 | + 'location_id': warehouse.lot_stock_id.id, |
| 30 | + 'location_dest_id': self.partner_id.property_stock_customer.id, |
| 31 | + }) |
| 32 | + |
| 33 | + return delivery_picking |
| 34 | + |
| 35 | + def check_stock_availability(self, product, warehouse): |
| 36 | + """Check if the entire order can be fulfilled from a single warehouse.""" |
| 37 | + |
| 38 | + stock_quant = self.env['stock.quant'].search([ |
| 39 | + ('product_id', '=', product.id), |
| 40 | + ('location_id', '=', warehouse.lot_stock_id.id) |
| 41 | + ], limit=1) |
| 42 | + return stock_quant.quantity > 0 if stock_quant else False |
| 43 | + |
| 44 | + def can_fulfill_from_single_warehouse(self, warehouse): |
| 45 | + if not self.order_line: |
| 46 | + return False |
| 47 | + |
| 48 | + for line in self.order_line: |
| 49 | + if self.check_stock_availability(line.product_id, warehouse) == False: |
| 50 | + return False |
| 51 | + |
| 52 | + return True |
| 53 | + |
| 54 | + def group_lines_by_warehouse(self): |
| 55 | + """Group order lines by available warehouse based on stock availability.""" |
| 56 | + |
| 57 | + warehouse_lines = {} |
| 58 | + for line in self.order_line: |
| 59 | + product = line.product_id |
| 60 | + primary_stock = self.check_stock_availability( |
| 61 | + product, product.primary_warehouse_id) |
| 62 | + secondary_stock = self.check_stock_availability( |
| 63 | + product, product.secondary_warehouse_id) if product.secondary_warehouse_id else False |
| 64 | + |
| 65 | + if primary_stock: |
| 66 | + warehouse = product.primary_warehouse_id |
| 67 | + elif secondary_stock: |
| 68 | + warehouse = product.secondary_warehouse_id |
| 69 | + else: |
| 70 | + raise ValidationError( |
| 71 | + f"The product '{product.name}' has no stock in both primary and secondary warehouses.") |
| 72 | + |
| 73 | + if warehouse not in warehouse_lines: |
| 74 | + warehouse_lines[warehouse] = [] |
| 75 | + warehouse_lines[warehouse].append(line) |
| 76 | + |
| 77 | + return warehouse_lines |
| 78 | + |
| 79 | + # ------------------------------------------------------------------------- |
| 80 | + # ACTIONS |
| 81 | + # ------------------------------------------------------------------------- |
| 82 | + |
| 83 | + def action_confirm(self): |
| 84 | + """Confirm the sale order and create delivery orders based on stock availability.""" |
| 85 | + |
| 86 | + primary_warehouse = self.order_line[0].product_id.primary_warehouse_id |
| 87 | + secondary_warehouse = self.order_line[0].product_id.secondary_warehouse_id |
| 88 | + |
| 89 | + if self.can_fulfill_from_single_warehouse(primary_warehouse): |
| 90 | + for line in self.order_line: |
| 91 | + line.warehouse_id = primary_warehouse # Assign all products to Primary Warehouse |
| 92 | + res = self.create_delivery_order( |
| 93 | + primary_warehouse, self.order_line) |
| 94 | + self.write({'picking_ids': [(4, res.id)]}) |
| 95 | + elif secondary_warehouse and self.can_fulfill_from_single_warehouse(secondary_warehouse): |
| 96 | + for line in self.order_line: |
| 97 | + line.warehouse_id = secondary_warehouse # Assign all products to Secondary Warehouse |
| 98 | + res = self.create_delivery_order( |
| 99 | + secondary_warehouse, self.order_line) |
| 100 | + self.write({'picking_ids': [(4, res.id)]}) |
| 101 | + else: |
| 102 | + warehouse_lines = self.group_lines_by_warehouse() |
| 103 | + for warehouse, lines in warehouse_lines.items(): |
| 104 | + res = self.create_delivery_order(warehouse, lines) |
| 105 | + self.write({'picking_ids': [(4, res.id)]}) |
| 106 | + |
| 107 | + self.write({'state': 'sale'}) |
| 108 | + return True |
0 commit comments