|
|
for order in self:
mls = order.account_payment_ids.mapped("move_id.line_ids").filtered(
lambda x: x.account_id.account_type == "liability_payable"
and x.parent_state == "posted"
)
advance_amount = 0.0
for line in mls:
line_currency = line.currency_id or line.company_id.currency_id
# Exclude reconciled pre-payments amount because once reconciled
# the pre-payment will reduce bill residual amount like any
# other payment.
line_amount = (
line.amount_residual_currency
if line.currency_id
else line.amount_residual
)
if line_currency != order.currency_id:
advance_amount += line.currency_id._convert(
line_amount,
order.currency_id,
order.company_id,
line.date or fields.Date.today(),
)
else:
advance_amount += line_amount
# Consider payments in related invoices.
invoice_paid_amount = 0.0
for inv in order.invoice_ids:
invoice_paid_amount += inv.amount_total - inv.amount_residual
amount_residual = order.amount_total - advance_amount - invoice_paid_amount
payment_state = "not_paid"
if mls or not order.currency_id.is_zero(invoice_paid_amount):
has_due_amount = float_compare(
amount_residual, 0.0, precision_rounding=order.currency_id.rounding
)
if has_due_amount <= 0:
payment_state = "paid"
elif has_due_amount > 0:
payment_state = "partial"
order.payment_line_ids = mls
order.amount_residual = amount_residual
order.advance_payment_status = payment_state
|
|