# Copyright (C) 2021: Criptomart (<https://criptomart.net/>)
# @author: Criptomart (<tech@criptomart.net>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import logging

from odoo import models, fields, api

_logger = logging.getLogger(__name__)

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

    @api.onchange('product_id')
    def onchange_product_id(self):
        result = super(PurchaseOrderLine, self).onchange_product_id()
        supplier_infos = self.env['product.supplierinfo'].search([('name', '=', self.partner_id.name)])
        product_ids = self.env['product.product']
        for supplier_info in supplier_infos:
             product_ids += supplier_info.product_tmpl_id.product_variant_ids
        result.update({'domain': {'product_id': [('id', 'in', product_ids.ids)]}})

        return result
