| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from odoo import fields, models |
| 5 |
|
| 6 |
|
| 7 |
class ReportMrpReport_Mo_Overview(models.AbstractModel): |
| 8 |
_inherit = 'report.mrp.report_mo_overview' |
| 9 |
|
| 10 |
def _get_extra_replenishments(self, product): |
| 11 |
res = super()._get_extra_replenishments(product) |
| 12 |
domain = [('state', 'in', ['draft', 'sent', 'to approve']), ('product_id', '=', product.id)] |
| 13 |
warehouse_id = self.env.context.get('warehouse_id', False) |
| 14 |
if warehouse_id: |
| 15 |
domain += [('order_id.picking_type_id.warehouse_id', '=', warehouse_id)] |
| 16 |
po_lines = self.env['purchase.order.line'].search(domain, order='date_planned, id') |
| 17 |
|
| 18 |
for po_line in po_lines: |
| 19 |
line_qty = po_line.product_qty |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
dest_moves = self.env['stock.move'].browse(po_line.move_dest_ids._rollup_move_dests()) |
| 24 |
for move in dest_moves: |
| 25 |
if not move.raw_material_production_id: |
| 26 |
continue |
| 27 |
prod_qty = min(line_qty, move.product_uom._compute_quantity(move.product_uom_qty, po_line.product_uom_id)) |
| 28 |
res.append(self._format_extra_replenishment(po_line, prod_qty, move.raw_material_production_id.id)) |
| 29 |
line_qty -= prod_qty |
| 30 |
if line_qty: |
| 31 |
res.append(self._format_extra_replenishment(po_line, line_qty)) |
| 32 |
|
| 33 |
return res |
| 34 |
|
| 35 |
def _format_extra_replenishment(self, po_line, quantity, production_id=False): |
| 36 |
po = po_line.order_id |
| 37 |
price = po_line.tax_ids.compute_all( |
| 38 |
po_line.price_unit, |
| 39 |
currency=po.currency_id, |
| 40 |
quantity=quantity, |
| 41 |
product=po_line.product_id, |
| 42 |
partner=po.partner_id, |
| 43 |
rounding_method="round_globally", |
| 44 |
)['total_void'] |
| 45 |
return { |
| 46 |
'_name': 'purchase.order', |
| 47 |
'id': po.id, |
| 48 |
'cost': price, |
| 49 |
'quantity': quantity, |
| 50 |
'uom': po_line.product_uom_id, |
| 51 |
'production_id': production_id |
| 52 |
} |
| 53 |
|
| 54 |
def _get_replenishment_receipt(self, doc_in, components): |
| 55 |
res = super()._get_replenishment_receipt(doc_in, components) |
| 56 |
if doc_in._name == 'purchase.order': |
| 57 |
if doc_in.state != 'purchase': |
| 58 |
return self._format_receipt_date('estimated', doc_in.date_planned) |
| 59 |
in_pickings = doc_in.picking_ids.filtered(lambda p: p.state not in ('done', 'cancel')) |
| 60 |
planned_date = max(in_pickings.mapped('scheduled_date')) if in_pickings else doc_in.date_planned |
| 61 |
return self._format_receipt_date('expected', planned_date) |
| 62 |
return res |
| 63 |
|
| 64 |
def _get_resupply_data(self, rules, rules_delay, quantity, uom_id, product, production): |
| 65 |
res = super()._get_resupply_data(rules, rules_delay, quantity, uom_id, product, production) |
| 66 |
if any(rule for rule in rules if rule.action == 'buy' and product.seller_ids): |
| 67 |
supplier = product._select_seller(quantity=quantity, uom_id=product.uom_id) |
| 68 |
if supplier: |
| 69 |
return { |
| 70 |
'delay': supplier.delay + rules_delay, |
| 71 |
'cost': supplier.price * uom_id._compute_quantity(quantity, supplier.product_uom_id), |
| 72 |
'currency': supplier.currency_id, |
| 73 |
} |
| 74 |
return res |
| 75 |
|
| 76 |
def _is_doc_in_done(self, doc_in): |
| 77 |
if doc_in._name == 'purchase.order': |
| 78 |
return doc_in.state == 'purchase' and all(move.state in ('done', 'cancel') for move in doc_in.order_line.move_ids) |
| 79 |
return super()._is_doc_in_done(doc_in) |
| 80 |
|
| 81 |
def _get_origin(self, move): |
| 82 |
if move.purchase_line_id: |
| 83 |
return move.purchase_line_id.order_id |
| 84 |
return super()._get_origin(move) |
| 85 |
|
| 86 |
def _get_replenishment_mo_cost(self, product, quantity, uom_id, currency, move_in=False): |
| 87 |
if move_in and move_in.purchase_line_id: |
| 88 |
po_line = move_in.purchase_line_id |
| 89 |
po = po_line.order_id |
| 90 |
price = po_line.tax_ids.compute_all( |
| 91 |
po_line.price_unit, |
| 92 |
currency=po.currency_id, |
| 93 |
quantity=uom_id._compute_quantity(quantity, move_in.purchase_line_id.product_uom_id), |
| 94 |
product=po_line.product_id, |
| 95 |
partner=po.partner_id, |
| 96 |
rounding_method='round_globally', |
| 97 |
)['total_void'] |
| 98 |
price = po_line.currency_id._convert(price, currency, (move_in.company_id or self.env.company), fields.Date.today()) |
| 99 |
return currency.round(price) |
| 100 |
return super()._get_replenishment_mo_cost(product, quantity, uom_id, currency, move_in) |