# Copyright 2021 Criptomart
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import logging

from odoo import _, api, fields, models

_logger = logging.getLogger(__name__)

class ProductProduct(models.Model):
    _inherit = 'product.product'


    @api.model
    def create(self, vals):
        category = self.env['product.category']
        categ_id = vals.get("categ_id")
        template_id = vals.get("product_tmpl_id")
        if categ_id:
            category = category.browse(categ_id)
            vals['taxes_id'] = category.sell_tax_id
            vals['supplier_taxes_id'] = category.buy_tax_id
        elif template_id:
            template = self.env["product.template"].browse(template_id)
            cat = template.categ_id
            if self.env.user.company_id.use_parent_categories_to_determine_taxes:
                while not cat.pos_categ_id and cat.parent_id:
                    cat = cat.parent_id
            if cat:
                template.taxes_id = cat.sell_tax_id
                template.supplier_taxes_id = cat.buy_tax_id

        return super(ProductProduct, self).create(vals)


