summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/report_stock_forecasted.py
blob: 5f9427f868481e8a463467cf5866ae1783b5031e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from odoo import api, models

class ReplenishmentReport(models.AbstractModel):
    _inherit = 'report.stock.report_product_product_replenishment'

    @api.model
    def _get_report_lines(self, product_template_ids, product_variant_ids, wh_location_ids):
        lines = super(ReplenishmentReport, self)._get_report_lines(product_template_ids, product_variant_ids, wh_location_ids)
        result_dict = {}

        for line in lines:
            document_out = line.get('document_out')

            if document_out and "SO/" in document_out.name:
                order_id = document_out.id
                if document_out == False:
                    continue
                product_id = line.get('product', {}).get('id')
                query = [('product_id', '=', product_id)]

                if order_id:
                    result = self._calculate_result(line)
                    quantity = line.get('quantity', 0)
                    result_dict.setdefault(order_id, []).append((result, quantity))

                for order_id, results in result_dict.items():
                    sales_order = self.env['sale.order'].browse(order_id)

                    for result, quantity in results:
                        self.env['sales.order.fullfillment'].create({
                            'sales_order_id': sales_order.id,
                            'product_id': product_id,
                            'reserved_from': result,
                            'qty_fullfillment': quantity,
                        })

        return lines

    def _calculate_result(self, line):
        if line['document_in']:
            return str(line["document_in"].name)
        elif line['reservation'] and not line['document_in']:
            return 'Reserved from stock'
        elif line['replenishment_filled']:
            if line['document_out']:
                return 'Inventory On Hand'
            else:
                return 'Free Stock'
        else:
            return 'Unfulfilled'