summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/purchase_order.py
blob: 4931d738d3c6c0f7f4dfc1ecea6a03973498c96b (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
from odoo import fields, models, api


class PurchaseOrder(models.Model):
    _inherit = 'purchase.order'

    sale_order_id = fields.Many2one('sale.order', string='Sale Order')

    def sale_order_sync(self):
        if not self.sale_order_id:
            return

        purchase_orders = self.search(['&', ('sale_order_id', '=', self.sale_order_id.id), ('id', '!=', self.id)])
        products_exception = []
        for purchase_order in purchase_orders:
            for order_line in purchase_order.order_line:
                products_exception.append(order_line.product_id.id)

        self.order_line.unlink()
        for order_line in self.sale_order_id.order_line:
            if order_line.product_id.id and order_line.product_id.id not in products_exception:
                values = {
                    'order_id': self.id,
                    'product_id': order_line.product_id.id,
                    'name': order_line.product_id.display_name,
                    'product_qty': order_line.product_qty,
                }
                self.env['purchase.order.line'].sudo().create(values)