| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
from datetime import date |
| 3 |
|
| 4 |
from odoo import models |
| 5 |
from odoo.tools import float_is_zero, format_date |
| 6 |
|
| 7 |
|
| 8 |
class StockForecasted_Product_Product(models.AbstractModel): |
| 9 |
_inherit = 'stock.forecasted_product_product' |
| 10 |
|
| 11 |
def _get_report_header(self, product_template_ids, product_ids, wh_location_ids): |
| 12 |
res = super()._get_report_header(product_template_ids, product_ids, wh_location_ids) |
| 13 |
res["use_expiration_date"] = any(self.env['product.product'].browse(res["product_variants_ids"]).mapped('use_expiration_date')) |
| 14 |
if res["use_expiration_date"]: |
| 15 |
products = self._get_products(product_template_ids, product_ids) |
| 16 |
for product in products: |
| 17 |
res["product"][product.id]["to_remove_qty"] = res["product"][product.id]['quantity_on_hand'] + res["product"][product.id]['incoming_qty'] - res["product"][product.id]['outgoing_qty'] - res["product"][product.id]['virtual_available'] |
| 18 |
return res |
| 19 |
|
| 20 |
def _get_quant_domain(self, location_ids, products): |
| 21 |
res = super()._get_quant_domain(location_ids, products) |
| 22 |
if any(products.mapped('use_expiration_date')): |
| 23 |
res += ['|', ('removal_date', '=', False), ('removal_date', '>', date.today())] |
| 24 |
return res |
| 25 |
|
| 26 |
def _get_expired_quant_domain(self, location_ids, products): |
| 27 |
res = super()._get_quant_domain(location_ids, products) |
| 28 |
res += [('removal_date', '<=', date.today())] |
| 29 |
return res |
| 30 |
|
| 31 |
def _prepare_report_line(self, quantity, move_out=None, move_in=None, replenishment_filled=True, product=False, reserved_move=False, in_transit=False, read=True): |
| 32 |
res = super()._prepare_report_line(quantity, move_out, move_in, replenishment_filled, product, reserved_move, in_transit, read) |
| 33 |
removal_date = self.env.context.get('removal_date') |
| 34 |
if removal_date: |
| 35 |
res["removal_date"] = removal_date if removal_date == -1 else format_date(self.env, removal_date) |
| 36 |
return res |
| 37 |
|
| 38 |
def _free_stock_lines(self, product, free_stock, moves_data, wh_location_ids, read): |
| 39 |
res = [] |
| 40 |
if product.use_expiration_date: |
| 41 |
reserved_expired, unreserved_expired = self.env['stock.quant']._read_group(self._get_expired_quant_domain(wh_location_ids, product), aggregates=['reserved_quantity:sum', 'available_quantity:sum'])[0] |
| 42 |
# Insert the "To remove now" line here, before the free stock line |
| 43 |
if not product.uom_id.is_zero(unreserved_expired): |
| 44 |
res += [self.with_context(removal_date=-1)._prepare_report_line(unreserved_expired, product=product, read=read)] |
| 45 |
# Insert the "To remove on" lines here, before the free stock line |
| 46 |
|
| 47 |
to_reduce = sum(d['taken_from_stock'] for d in moves_data.values()) |
| 48 |
|
| 49 |
for removal_date, free_stock_at_date in self.env['stock.quant']._read_group( |
| 50 |
self._get_quant_domain(wh_location_ids, product), |
| 51 |
['removal_date:day'], ['available_quantity:sum'] |
| 52 |
): |
| 53 |
to_reduce_here = min(to_reduce, free_stock_at_date) |
| 54 |
to_reduce -= to_reduce_here |
| 55 |
free_stock_at_date -= to_reduce_here |
| 56 |
if not float_is_zero(free_stock_at_date, precision_rounding=product.uom_id.rounding) and removal_date: |
| 57 |
res.append(self.with_context(removal_date=removal_date)._prepare_report_line(free_stock_at_date, product=product, read=read)) |
| 58 |
|
| 59 |
# Compensate for any reserved products that are no longer fresh |
| 60 |
free_stock += reserved_expired |
| 61 |
return res + super()._free_stock_lines(product, free_stock, moves_data, wh_location_ids, read) |