from odoo import models


class SaleAdvancePaymentInv(models.TransientModel):
    _inherit = "sale.advance.payment.inv"

    def _prepare_down_payment_lines_values(self, order):

        if not order.is_tax_computed_externally:
            return super()._prepare_down_payment_lines_values(order)

        line = self._prepare_base_downpayment_line_values(order)
        if self.advance_payment_method == 'percentage':
            line["price_unit"] = order.amount_total * (self.amount / 100)
        else:
            line["price_unit"] = self.fixed_amount

        line["price_unit"] = min(line["price_unit"], order.amount_total)
        return [line], [False]
