Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

16.0 fix down payment #95

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 108 additions & 1 deletion l10n_pt_account_invoicexpress/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import uuid

from odoo import _, api, exceptions, fields, models
from odoo.tools import float_compare
from odoo.addons.account.models.account_move import TYPE_REVERSE_MAP


class AccountMove(models.Model):
Expand Down Expand Up @@ -282,14 +284,100 @@
invoice.message_post(body=msg)

def _post(self, soft=True):
credit_note_lines = self._prepare_credit_note_lines()

res = super()._post(soft=soft)
for invoice in self:
if not invoice.invoicexpress_id:
invoice._check_invoicexpress_doctype_config()
invoice.action_create_invoicexpress_invoice()
invoice.action_send_invoicexpress_email(ignore_no_config=True)
if credit_note_lines.get(invoice.id):
invoice.create_credit_note(credit_note_lines[invoice.id])

Check warning on line 296 in l10n_pt_account_invoicexpress/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

l10n_pt_account_invoicexpress/models/account_move.py#L296

Added line #L296 was not covered by tests

return res

def _prepare_credit_note_lines(self):
credit_note_lines = {}

for invoice in self:
if not invoice.invoicexpress_id:
negative_lines = invoice.invoice_line_ids.filtered(
lambda l: l.is_downpayment and float_compare(l.price_total, 0.0, 2) < 0
)
if negative_lines:
# Prepare negative lines for credit note
credit_note_lines.update({
invoice.id: [{
'product_id': negative_line.product_id.id,
'quantity': abs(negative_line.quantity),
'price_unit': negative_line.price_unit,
'sale_line_ids': negative_line.sale_line_ids.ids,
} for negative_line in negative_lines]
})

# Delete negative lines from invoice before post
negative_lines.unlink()

Check warning on line 320 in l10n_pt_account_invoicexpress/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

l10n_pt_account_invoicexpress/models/account_move.py#L320

Added line #L320 was not covered by tests

# Delete down payment section
invoice = invoice.with_context(lang='en_US')

Check warning on line 323 in l10n_pt_account_invoicexpress/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

l10n_pt_account_invoicexpress/models/account_move.py#L323

Added line #L323 was not covered by tests
down_payment_section = invoice.invoice_line_ids.filtered(
lambda l: l.display_type == 'line_section' and l.name == 'Down Payments'
)
down_payment_section.unlink()

Check warning on line 327 in l10n_pt_account_invoicexpress/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

l10n_pt_account_invoicexpress/models/account_move.py#L327

Added line #L327 was not covered by tests

return credit_note_lines

def create_credit_note(self, lines):
self.ensure_one()

Check warning on line 332 in l10n_pt_account_invoicexpress/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

l10n_pt_account_invoicexpress/models/account_move.py#L332

Added line #L332 was not covered by tests

# The credit note should use the same exempt reason that was used in the
# invoice with the down payment
l10npt_vat_exempt_reason = None
invoices = self.line_ids.sale_line_ids.order_id.invoice_ids
invoices = invoices.filtered('l10npt_vat_exempt_reason')

Check warning on line 338 in l10n_pt_account_invoicexpress/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

l10n_pt_account_invoicexpress/models/account_move.py#L336-L338

Added lines #L336 - L338 were not covered by tests
for invoice in invoices:
has_downpayment = any(
line.is_downpayment and float_compare(line.price_total, 0.0, 2) >= 0
for line in invoice.invoice_line_ids
)
if has_downpayment:
l10npt_vat_exempt_reason = invoice.l10npt_vat_exempt_reason.id
break

Check warning on line 346 in l10n_pt_account_invoicexpress/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

l10n_pt_account_invoicexpress/models/account_move.py#L345-L346

Added lines #L345 - L346 were not covered by tests

credit_note = self.env['account.move'].create({
'ref': _('Reversal of: %s', self.name),
'date': self.date,
'invoice_date_due': self.date,
'invoice_date': self.date,
'journal_id': self.journal_id.id,
'invoice_payment_term_id': None,
'invoice_user_id': self.invoice_user_id.id,
'auto_post': 'no',
'move_type': TYPE_REVERSE_MAP[self.move_type],
'reversed_entry_id': self.id,
'currency_id': self.currency_id.id,
'l10npt_vat_exempt_reason': l10npt_vat_exempt_reason,
'invoice_line_ids': [
(0, 0, {
'product_id': line['product_id'],
'quantity': line['quantity'],
'price_unit': line['price_unit'],
'sale_line_ids': [(6, 0, line['sale_line_ids'])],
'tax_ids': None,
}) for line in lines
],
})

# Post and reconcile credit note
if l10npt_vat_exempt_reason:
credit_note.action_post()

Check warning on line 374 in l10n_pt_account_invoicexpress/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

l10n_pt_account_invoicexpress/models/account_move.py#L374

Added line #L374 was not covered by tests

outstanding_lines = credit_note.line_ids

Check warning on line 376 in l10n_pt_account_invoicexpress/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

l10n_pt_account_invoicexpress/models/account_move.py#L376

Added line #L376 was not covered by tests
outstanding_lines = outstanding_lines.filtered(lambda l: float_compare(l.balance, 0.0, 2) < 0)
for outstanding_line in outstanding_lines:
self.js_assign_outstanding_line(outstanding_line.id)

Check warning on line 379 in l10n_pt_account_invoicexpress/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

l10n_pt_account_invoicexpress/models/account_move.py#L379

Added line #L379 was not covered by tests

def _track_subtype(self, init_values):
res = super()._track_subtype(init_values)
if "payment_state" in init_values and self.payment_state == "paid":
Expand All @@ -300,7 +388,10 @@

def _mark_invoice_paid(self):
InvoiceXpress = self.env["account.invoicexpress"]
for invoice in self.filtered("can_invoicexpress"):
# Credit notes are considered paid by default when finalized in InvoiceXpress
for invoice in self.filtered(
lambda i: i.can_invoicexpress and i.move_type not in ('out_refund', 'in_refund')
):
doctype = invoice.invoicexpress_doc_type
if not doctype:
raise exceptions.UserError(
Expand All @@ -325,6 +416,22 @@
msg = _("InvoiceXpress record has been modified to Paid.")
self.message_post(body=msg)

def action_register_payment(self):
for invoice in self:
if not invoice.invoicexpress_id:
continue

Check warning on line 422 in l10n_pt_account_invoicexpress/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

l10n_pt_account_invoicexpress/models/account_move.py#L422

Added line #L422 was not covered by tests

if invoice.reversed_entry_id:
raise exceptions.UserError(_('Please reconcile outstanding credits instead in the reversed invoice.'))

Check warning on line 425 in l10n_pt_account_invoicexpress/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

l10n_pt_account_invoicexpress/models/account_move.py#L425

Added line #L425 was not covered by tests

if invoice.invoice_has_outstanding:
for payment in invoice.invoice_outstanding_credits_debits_widget.get('content'):
move = self.env['account.move'].browse(payment['move_id'])

Check warning on line 429 in l10n_pt_account_invoicexpress/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

l10n_pt_account_invoicexpress/models/account_move.py#L429

Added line #L429 was not covered by tests
if move.move_type in ('out_refund', 'in_refund') and move.reversed_entry_id == invoice:
raise exceptions.UserError(_('Please reconcile outstanding credits first.'))

Check warning on line 431 in l10n_pt_account_invoicexpress/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

l10n_pt_account_invoicexpress/models/account_move.py#L431

Added line #L431 was not covered by tests

return super(AccountMove, self).action_register_payment()

Check warning on line 433 in l10n_pt_account_invoicexpress/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

l10n_pt_account_invoicexpress/models/account_move.py#L433

Added line #L433 was not covered by tests


class AccountMoveLine(models.Model):
_inherit = "account.move.line"
Expand Down
Loading