Debian Codegraph @

addons/sale_mrp/models/sale_order_line.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # Part of Odoo. See LICENSE file for full copyright and licensing details.
3
4 from odoo import api, models
5 from odoo.tools import float_compare
6
7
8 class SaleOrderLine(models.Model):
9 _inherit = 'sale.order.line'
10
11 @api.depends('product_uom_qty', 'qty_delivered', 'product_id', 'state')
12 def _compute_qty_to_deliver(self):
13 """The inventory widget should now be visible in more cases if the product is consumable."""
14 super(SaleOrderLine, self)._compute_qty_to_deliver()
15 for line in self:
16 # Hide the widget for kits since forecast doesn't support them.
17 boms = self.env['mrp.bom']
18 if line.state == 'sale':
19 boms = line.move_ids.mapped('bom_line_id.bom_id')
20 elif line.state in ['draft', 'sent'] and line.product_id:
21 boms = boms._bom_find(line.product_id, company_id=line.company_id.id, bom_type='phantom')[line.product_id]
22 relevant_bom = boms.filtered(lambda b: b.type == 'phantom' and
23 (b.product_id == line.product_id or
24 (b.product_tmpl_id == line.product_id.product_tmpl_id and not b.product_id)))
25 if relevant_bom:
26 line.display_qty_widget = False
27 continue
28 if line.state == 'draft' and line.product_type == 'consu':
29 components = line.product_id.get_components()
30 if components and components != [line.product_id.id]:
31 line.display_qty_widget = True
32
33 def _prepare_qty_delivered(self):
34 delivered_qties = super()._prepare_qty_delivered()
35 for order_line in self:
36 if order_line.qty_delivered_method == 'stock_move':
37 boms = order_line.move_ids.filtered(lambda m: m.state != 'cancel').bom_line_id.bom_id
38 dropship = any(m._is_dropshipped() for m in order_line.move_ids)
39 # We fetch the BoMs of type kits linked to the order_line,
40 # the we keep only the one related to the finished produst.
41 # This bom should be the only one since bom_line_id was written on the moves
42 relevant_bom = boms.filtered(lambda b: b.type == 'phantom' and
43 (b.product_id == order_line.product_id or
44 (b.product_tmpl_id == order_line.product_id.product_tmpl_id and not b.product_id)))
45 if not relevant_bom:
46 relevant_bom = boms._bom_find(order_line.product_id, company_id=order_line.company_id.id, bom_type='phantom')[order_line.product_id]
47 if relevant_bom:
48 # not written on a move coming from a PO: all moves (to customer) must be done
49 # and the returns must be delivered back to the customer
50 # FIXME: if the components of a kit have different suppliers, multiple PO
51 # are generated. If one PO is confirmed and all the others are in draft, receiving
52 # the products for this PO will set the qty_delivered. We might need to check the
53 # state of all PO as well... but sale_mrp doesn't depend on purchase.
54 if dropship:
55 moves = order_line.move_ids.filtered(lambda m: m.state != 'cancel')
56 if any((m.location_dest_id.usage == 'customer' and m.state != 'done')
57 or (m.location_dest_id.usage != 'customer'
58 and m.state == 'done'
59 and float_compare(m.quantity,
60 sum(sub_m.product_uom._compute_quantity(sub_m.quantity, m.product_uom) for sub_m in m.returned_move_ids if sub_m.state == 'done'),
61 precision_rounding=m.product_uom.rounding) > 0)
62 for m in moves) or not moves:
63 delivered_qties[order_line] = 0
64 else:
65 delivered_qties[order_line] = order_line.product_uom_qty
66 continue
67 moves = order_line.move_ids.filtered(lambda m: m.state == 'done' and m.location_dest_usage != 'inventory')
68 filters = {
69 # in/out perspective w/ respect to moves is flipped for sale order document
70 'incoming_moves': lambda m:
71 m._is_outgoing() and
72 (not m.origin_returned_move_id or (m.origin_returned_move_id and m.to_refund)),
73 'outgoing_moves': lambda m:
74 m._is_incoming() and m.to_refund,
75 }
76 order_qty = order_line.product_uom_id._compute_quantity(order_line.product_uom_qty, relevant_bom.product_uom_id)
77 qty_delivered = moves._compute_kit_quantities(order_line.product_id, order_qty, relevant_bom, filters)
78 delivered_qties[order_line] += relevant_bom.product_uom_id._compute_quantity(qty_delivered, order_line.product_uom_id)
79
80 # If no relevant BOM is found, fall back on the all-or-nothing policy. This happens
81 # when the product sold is made only of kits. In this case, the BOM of the stock moves
82 # do not correspond to the product sold => no relevant BOM.
83 elif boms:
84 # if the move is ingoing, the product **sold** has delivered qty 0
85 if all(m.state == 'done' and m.location_dest_id.usage == 'customer' for m in order_line.move_ids):
86 delivered_qties[order_line] = order_line.product_uom_qty
87 else:
88 delivered_qties[order_line] = 0.0
89 return delivered_qties
90
91 def compute_uom_qty(self, new_qty, stock_move, rounding=True):
92 #check if stock move concerns a kit
93 if stock_move.bom_line_id:
94 return new_qty * stock_move.bom_line_id.product_qty
95 return super(SaleOrderLine, self).compute_uom_qty(new_qty, stock_move, rounding)
96
97 def _get_bom_component_qty(self, bom):
98 bom_quantity = self.product_id.uom_id._compute_quantity(1, bom.product_uom_id, rounding_method='HALF-UP')
99 boms, lines = bom.explode(self.product_id, bom_quantity)
100 components = {}
101 for line, line_data in lines:
102 product = line.product_id.id
103 uom = line.product_uom_id
104 qty = line_data['qty']
105 if components.get(product, False):
106 if uom.id != components[product]['uom']:
107 from_uom = uom
108 to_uom = self.env['uom.uom'].browse(components[product]['uom'])
109 qty = from_uom._compute_quantity(qty, to_uom)
110 components[product]['qty'] += qty
111 else:
112 # To be in the uom reference of the product
113 to_uom = self.env['product.product'].browse(product).uom_id
114 if uom.id != to_uom.id:
115 from_uom = uom
116 qty = from_uom._compute_quantity(qty, to_uom)
117 components[product] = {'qty': qty, 'uom': to_uom.id}
118 return components
119
120 @api.model
121 def _get_incoming_outgoing_moves_filter(self):
122 """ Method to be override: will get incoming moves and outgoing moves.
123
124 :return: Dictionary with incoming moves and outgoing moves
125 :rtype: dict
126 """
127 # The first move created was the one created from the intial rule that started it all.
128 sorted_moves = self.move_ids.sorted('id')
129 triggering_rule_ids = []
130 seen_wh_ids = set()
131 seen_bom_id = set()
132 for move in sorted_moves:
133 if move.bom_line_id.bom_id.id in seen_bom_id:
134 triggering_rule_ids.append(move.rule_id.id)
135 elif move.warehouse_id.id not in seen_wh_ids:
136 triggering_rule_ids.append(move.rule_id.id)
137 seen_wh_ids.add(move.warehouse_id.id)
138 if move.bom_line_id and move.bom_line_id.bom_id.type == 'phantom':
139 seen_bom_id.add(move.bom_line_id.bom_id.id)
140
141 return {
142 'incoming_moves': lambda m: (
143 m.state != 'cancel' and m.location_dest_usage != 'inventory'
144 and m.rule_id.id in triggering_rule_ids
145 and m.location_final_id.usage == 'customer'
146 and (not m.origin_returned_move_id or (m.origin_returned_move_id and m.to_refund)
147 )),
148 'outgoing_moves': lambda m: (
149 m.state != 'cancel' and m.location_dest_usage != 'inventory'
150 and m.location_id.usage == 'customer' and m.to_refund
151 ),
152 }
153
154 def _get_qty_procurement(self, previous_product_uom_qty=False):
155 self.ensure_one()
156 # Specific case when we change the qty on a SO for a kit product.
157 # We don't try to be too smart and keep a simple approach: we use the quantity of entire
158 # kits that are currently in delivery
159 bom = self.env['mrp.bom'].sudo()._bom_find(self.product_id, bom_type='phantom', company_id=self.company_id.id)[self.product_id]
160 if bom and self.move_ids:
161 moves = self.move_ids.filtered(lambda r: r.state != 'cancel' and r.location_dest_usage != 'inventory')
162 filters = self._get_incoming_outgoing_moves_filter()
163 order_qty = previous_product_uom_qty.get(self.id, 0) if previous_product_uom_qty else self.product_uom_qty
164 order_qty = self.product_uom_id._compute_quantity(order_qty, bom.product_uom_id)
165 qty = moves._compute_kit_quantities(self.product_id, order_qty, bom, filters)
166 return bom.product_uom_id._compute_quantity(qty, self.product_uom_id)
167 elif bom and previous_product_uom_qty:
168 return previous_product_uom_qty.get(self.id)
169 return super()._get_qty_procurement(previous_product_uom_qty=previous_product_uom_qty)