| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 3 |
|
| 4 |
from odoo import models |
| 5 |
|
| 6 |
|
| 7 |
class StockForecasted_Product_Product(models.AbstractModel): |
| 8 |
_inherit = 'stock.forecasted_product_product' |
| 9 |
|
| 10 |
def _get_report_header(self, product_template_ids, product_ids, wh_location_ids): |
| 11 |
res = super()._get_report_header(product_template_ids, product_ids, wh_location_ids) |
| 12 |
domain = [('state', 'in', ['draft', 'sent', 'to approve'])] |
| 13 |
domain += self._product_purchase_domain(product_template_ids, product_ids) |
| 14 |
warehouse_id = self.env.context.get('warehouse_id', False) |
| 15 |
if warehouse_id: |
| 16 |
domain += [('order_id.picking_type_id.warehouse_id', '=', warehouse_id)] |
| 17 |
company = self.env['stock.warehouse'].browse(warehouse_id).company_id |
| 18 |
else: |
| 19 |
company = self.env.company |
| 20 |
domain += [('company_id', '=', company.id)] |
| 21 |
po_lines = self.env['purchase.order.line'].sudo().search(domain).grouped('product_id') |
| 22 |
in_qty = {k.id: sum(v.mapped('product_uom_qty')) for k, v in po_lines.items()} |
| 23 |
self._add_product_quantities(res, product_template_ids, product_ids, 'draft_purchase_qty', in_qty) |
| 24 |
for product in self._get_products(product_template_ids, product_ids): |
| 25 |
if product not in po_lines: |
| 26 |
continue |
| 27 |
res['product'][product.id]['draft_purchase_orders'] = po_lines[product].mapped("order_id").sorted(key=lambda po: po.name).read(fields=['id', 'name']) |
| 28 |
res['product'][product.id]['draft_purchase_orders_matched'] = self.env.context.get('purchase_line_to_match_id') in po_lines[product].ids |
| 29 |
return res |
| 30 |
|
| 31 |
def _product_purchase_domain(self, product_template_ids, product_ids): |
| 32 |
if product_ids: |
| 33 |
return [('product_id', 'in', product_ids)] |
| 34 |
elif product_template_ids: |
| 35 |
subquery_products = self.env['product.product']._search( |
| 36 |
[('product_tmpl_id', 'in', product_template_ids)] |
| 37 |
) |
| 38 |
return [('product_id', 'in', subquery_products)] |