# Copyright 2016-2019 Akretion (http://www.akretion.com/)
# Copyright 2020 Criptomart <tech@criptomart.net>
# @author: Alexis de Lattre <alexis.delattre@akretion.com>
# @author Santi Noreña (<santi@criptomart.net>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import logging
import math

from odoo import exceptions, models, fields, api, _
from odoo.tools import float_is_zero, float_round, float_compare

_logger = logging.getLogger(__name__)


class StockMove(models.Model):
    _inherit = "stock.move"

    def product_price_update_before_done(self):
        super(StockMove, self).product_price_update_before_done()
        for move in self.filtered(lambda move: move.location_id.usage == "supplier"):
            if (
                move.product_id.last_purchase_price_compute_type
                == "with_three_discounts"
            ):
                price_updated = float_round(
                    move.purchase_line_id.price_subtotal
                    / move.purchase_line_id.product_qty,
                    precision_digits=2,
                )
            elif move.product_id.last_purchase_price_compute_type == "with_discount":
                price_updated = float_round(
                    move.purchase_line_id.price_unit
                    * (1 - move.purchase_line_id.discount1 / 100),
                    precision_digits=2,
                )
            elif (
                move.product_id.last_purchase_price_compute_type == "with_two_discounts"
            ):
                price_updated = float_round(
                    move.purchase_line_id.price_unit
                    * (1 - move.purchase_line_id.discount1 / 100)
                    * (1 - move.purchase_line_id.discount2 / 100),
                    precision_digits=2,
                )
            else:
                price_updated = move.purchase_line_id.price_unit

            # Convert price to product's base UoM if purchase UoM is different
            if (
                move.purchase_line_id
                and move.purchase_line_id.product_uom != move.product_id.uom_id
            ):
                price_updated = move.purchase_line_id.product_uom._compute_price(
                    price_updated, move.product_id.uom_id
                )

            if float_compare(
                move.product_id.last_purchase_price_received,
                price_updated,
                precision_digits=2,
            ) and not float_is_zero(move.quantity_done, precision_digits=3):
                _logger.info(
                    "Update last_purchase_price_received:  %s for product %s Previous price: %s"
                    % (
                        price_updated,
                        move.product_id.default_code,
                        move.product_id.last_purchase_price_received,
                    )
                )
                move.product_id.with_company(
                    move.company_id
                ).last_purchase_price_received = price_updated
                move.product_id.with_company(
                    move.company_id
                ).product_tmpl_id._compute_theoritical_price()
