From 3f2240b49af78ba4acf5a6eb0f2662542659b847 Mon Sep 17 00:00:00 2001 From: Cedric Collins Date: Tue, 18 Oct 2022 20:31:59 -0500 Subject: [PATCH] [MIG] account_payment_disperse: migrate to 15.0 --- account_payment_disperse/__manifest__.py | 3 +- account_payment_disperse/models/__init__.py | 1 - account_payment_disperse/models/account.py | 19 - .../models/account_patch.py | 418 ++++-------------- .../security/ir.model.access.csv | 2 + .../tests/test_payment_multi.py | 262 ++++++----- .../wizard/register_payment_wizard.py | 295 ++++++++---- .../wizard/register_payment_wizard_views.xml | 15 +- 8 files changed, 468 insertions(+), 547 deletions(-) delete mode 100644 account_payment_disperse/models/account.py create mode 100644 account_payment_disperse/security/ir.model.access.csv diff --git a/account_payment_disperse/__manifest__.py b/account_payment_disperse/__manifest__.py index 27272533..2779efc7 100644 --- a/account_payment_disperse/__manifest__.py +++ b/account_payment_disperse/__manifest__.py @@ -2,7 +2,7 @@ { 'name': 'Payment Disperse', - 'version': '13.0.1.0.0', + 'version': '15.0.1.0.0', 'author': 'Hibou Corp.', 'license': 'OPL-1', 'category': 'Accounting', @@ -15,6 +15,7 @@ Pay multiple invoices with one Payment, and manually disperse the amount per inv 'account', ], 'data': [ + 'security/ir.model.access.csv', 'wizard/register_payment_wizard_views.xml', ], 'installable': True, diff --git a/account_payment_disperse/models/__init__.py b/account_payment_disperse/models/__init__.py index e62b0928..e0cbf1a5 100644 --- a/account_payment_disperse/models/__init__.py +++ b/account_payment_disperse/models/__init__.py @@ -1,4 +1,3 @@ # Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details. -from . import account from . import account_patch diff --git a/account_payment_disperse/models/account.py b/account_payment_disperse/models/account.py deleted file mode 100644 index 08dd58d8..00000000 --- a/account_payment_disperse/models/account.py +++ /dev/null @@ -1,19 +0,0 @@ -# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details. - -from odoo import models, _ - - -class AccountPayment(models.Model): - _inherit = 'account.payment' - - def _values_for_payment_invoice_line(self, line, currency_id): - sign = 1.0 if self.payment_type == 'outbound' else -1.0 - i_write_off_amount = -line.difference if line.difference and line.close_balance else 0.0 - i_amount_currency = (line.amount + i_write_off_amount) if currency_id else 0.0 - i_amount = sign * (line.amount + i_write_off_amount) - return { - 'amount_currency': i_amount_currency, - 'currency_id': currency_id, - 'debit': i_amount if i_amount > 0.0 else 0.0, - 'credit': -i_amount if i_amount < 0.0 else 0.0, - } diff --git a/account_payment_disperse/models/account_patch.py b/account_payment_disperse/models/account_patch.py index 3c2e3520..f18c425a 100644 --- a/account_payment_disperse/models/account_patch.py +++ b/account_payment_disperse/models/account_patch.py @@ -1,8 +1,7 @@ # Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details. -from odoo import api, fields, models, _ -from odoo.exceptions import UserError, ValidationError -from odoo.tools import float_is_zero +from odoo import _ +from odoo.exceptions import UserError """ Patched because other modules may extend original functions. @@ -11,342 +10,97 @@ Patched because other modules may extend original functions. from odoo.addons.account.models import account_payment -def post(self): - """ Create the journal items for the payment and update the payment's state to 'posted'. - A journal entry is created containing an item in the source liquidity account (selected journal's default_debit or default_credit) - and another in the destination reconcilable account (see _compute_destination_account_id). - If invoice_ids is not empty, there will be one reconcilable move line per invoice to reconcile with. - If the payment is a transfer, a second journal entry is created in the destination journal to receive money from the transfer account. - """ - """ - Manual Payment Disperse functionality: - During reconciliation, reconcile specific lines to specific invoices so that the invoices - are paid down in the desired way. - """ - register_wizard = None - if self._context.get('payment_wizard_id'): - register_wizard = self.env['account.payment.register'].browse(self._context.get('payment_wizard_id')).exists() - is_manual_disperse = bool(register_wizard and register_wizard.is_manual_disperse) - - AccountMove = self.env['account.move'].with_context(default_type='entry') - for rec in self: - - if rec.state != 'draft': - raise UserError(_("Only a draft payment can be posted.")) - - if any(inv.state != 'posted' for inv in rec.invoice_ids): - raise ValidationError(_("The payment cannot be processed because the invoice is not open!")) - - # keep the name in case of a payment reset to draft - if not rec.name: - # Use the right sequence to set the name - if rec.payment_type == 'transfer': - sequence_code = 'account.payment.transfer' - else: - if rec.partner_type == 'customer': - if rec.payment_type == 'inbound': - sequence_code = 'account.payment.customer.invoice' - if rec.payment_type == 'outbound': - sequence_code = 'account.payment.customer.refund' - if rec.partner_type == 'supplier': - if rec.payment_type == 'inbound': - sequence_code = 'account.payment.supplier.refund' - if rec.payment_type == 'outbound': - sequence_code = 'account.payment.supplier.invoice' - rec.name = self.env['ir.sequence'].next_by_code(sequence_code, sequence_date=rec.payment_date) - if not rec.name and rec.payment_type != 'transfer': - raise UserError(_("You have to define a sequence for %s in your company.") % (sequence_code,)) - - moves = AccountMove.create(rec._prepare_payment_moves()) - moves.filtered(lambda move: move.journal_id.post_at != 'bank_rec').post() - - # Update the state / move before performing any reconciliation. - move_name = self._get_move_name_transfer_separator().join(moves.mapped('name')) - rec.write({'state': 'posted', 'move_name': move_name}) - - if rec.payment_type in ('inbound', 'outbound'): - # ==== 'inbound' / 'outbound' ==== - if is_manual_disperse: - move = moves[0] - digits_rounding_precision = move.company_id.currency_id.rounding - # pick out lines... - for i in register_wizard.payment_invoice_ids: - i_values = rec._values_for_payment_invoice_line(i, None) - if not any((i_values['debit'], i_values['credit'])): - continue - move_line = move.line_ids.filtered(lambda l: not l.reconciled and l.account_id == rec.destination_account_id and l.account_id != l.payment_id.writeoff_account_id and - float_is_zero(l.debit - i_values['debit'], precision_rounding=digits_rounding_precision) and - float_is_zero(l.credit - i_values['credit'], precision_rounding=digits_rounding_precision)) - i_lines = i.invoice_id.line_ids.filtered(lambda l: not l.reconciled and l.account_id == rec.destination_account_id) - if move_line and i_lines: - (move_line + i_lines).reconcile() - else: - if rec.invoice_ids: - (moves[0] + rec.invoice_ids).line_ids \ - .filtered(lambda line: not line.reconciled and line.account_id == rec.destination_account_id and not (line.account_id == line.payment_id.writeoff_account_id and line.name == line.payment_id.writeoff_label))\ - .reconcile() - elif rec.payment_type == 'transfer': - # ==== 'transfer' ==== - moves.mapped('line_ids')\ - .filtered(lambda line: line.account_id == rec.company_id.transfer_account_id)\ - .reconcile() - - return True - - -def _prepare_payment_moves(self): - ''' Prepare the creation of journal entries (account.move) by creating a list of python dictionary to be passed - to the 'create' method. - - Example 1: outbound with write-off: - - Account | Debit | Credit - --------------------------------------------------------- - BANK | 900.0 | - RECEIVABLE | | 1000.0 - WRITE-OFF ACCOUNT | 100.0 | - - Example 2: internal transfer from BANK to CASH: - - Account | Debit | Credit - --------------------------------------------------------- - BANK | | 1000.0 - TRANSFER | 1000.0 | - CASH | 1000.0 | - TRANSFER | | 1000.0 - - :return: A list of Python dictionary to be passed to env['account.move'].create. +def _synchronize_from_moves(self, changed_fields): + ''' Update the account.payment regarding its related account.move. + Also, check both models are still consistent. + :param changed_fields: A set containing all modified fields on account.move. ''' - """ - Manual Payment Disperse functionality: - During journal entry creation, create a single line per invoice for the amount - to be paid on that invoice. - However, all write-offs are accumulated on a single line. - """ - register_wizard = None - if self._context.get('payment_wizard_id'): - register_wizard = self.env['account.payment.register'].browse( - self._context.get('payment_wizard_id')).exists() - is_manual_disperse = bool(register_wizard and register_wizard.is_manual_disperse) + if self._context.get('skip_account_move_synchronization'): + return - all_move_vals = [] - for payment in self: - company_currency = payment.company_id.currency_id - move_names = payment.move_name.split(payment._get_move_name_transfer_separator()) if payment.move_name else None + for pay in self.with_context(skip_account_move_synchronization=True): - # Compute amounts. - write_off_amount = payment.payment_difference_handling == 'reconcile' and -payment.payment_difference or 0.0 - # Manual Disperse - if is_manual_disperse: - write_off_amount = sum(register_wizard.payment_invoice_ids.filtered( - lambda l: l.close_balance and l.difference and l.invoice_id in payment.invoice_ids).mapped('difference')) - if payment.payment_type == 'outbound': - write_off_amount *= -1.0 + # After the migration to 14.0, the journal entry could be shared between the account.payment and the + # account.bank.statement.line. In that case, the synchronization will only be made with the statement line. + if pay.move_id.statement_line_id: + continue - if payment.payment_type in ('outbound', 'transfer'): - counterpart_amount = payment.amount - liquidity_line_account = payment.journal_id.default_debit_account_id - else: - counterpart_amount = -payment.amount - liquidity_line_account = payment.journal_id.default_credit_account_id + move = pay.move_id + move_vals_to_write = {} + payment_vals_to_write = {} - # Manage currency. - if payment.currency_id == company_currency: - # Single-currency. - balance = counterpart_amount - write_off_balance = write_off_amount - counterpart_amount = write_off_amount = 0.0 - currency_id = False - else: - # Multi-currencies. - balance = payment.currency_id._convert(counterpart_amount, company_currency, payment.company_id, payment.payment_date) - write_off_balance = payment.currency_id._convert(write_off_amount, company_currency, payment.company_id, payment.payment_date) - currency_id = payment.currency_id.id + if 'journal_id' in changed_fields: + if pay.journal_id.type not in ('bank', 'cash'): + raise UserError(_("A payment must always belongs to a bank or cash journal.")) - # Manage custom currency on journal for liquidity line. - if payment.journal_id.currency_id and payment.currency_id != payment.journal_id.currency_id: - # Custom currency on journal. - if payment.journal_id.currency_id == company_currency: - # Single-currency - liquidity_line_currency_id = False + if 'line_ids' in changed_fields: + all_lines = move.line_ids + liquidity_lines, counterpart_lines, writeoff_lines = pay._seek_for_lines() + + # PATCHED + # if len(liquidity_lines) != 1: + if len(liquidity_lines.account_id) != 1: + raise UserError(_( + "Journal Entry %s is not valid. In order to proceed, the journal items must " + "include one and only one outstanding payments/receipts account.", + move.display_name, + )) + + # PATCHED + # if len(counterpart_lines) != 1: + if len(counterpart_lines.account_id) != 1: + raise UserError(_( + "Journal Entry %s is not valid. In order to proceed, the journal items must " + "include one and only one receivable/payable account (with an exception of " + "internal transfers).", + move.display_name, + )) + + if writeoff_lines and len(writeoff_lines.account_id) != 1: + raise UserError(_( + "Journal Entry %s is not valid. In order to proceed, " + "all optional journal items must share the same account.", + move.display_name, + )) + + if any(line.currency_id != all_lines[0].currency_id for line in all_lines): + raise UserError(_( + "Journal Entry %s is not valid. In order to proceed, the journal items must " + "share the same currency.", + move.display_name, + )) + + if any(line.partner_id != all_lines[0].partner_id for line in all_lines): + raise UserError(_( + "Journal Entry %s is not valid. In order to proceed, the journal items must " + "share the same partner.", + move.display_name, + )) + + if counterpart_lines.account_id.user_type_id.type == 'receivable': + partner_type = 'customer' else: - liquidity_line_currency_id = payment.journal_id.currency_id.id - liquidity_amount = company_currency._convert( - balance, payment.journal_id.currency_id, payment.company_id, payment.payment_date) - else: - # Use the payment currency. - liquidity_line_currency_id = currency_id - liquidity_amount = counterpart_amount + partner_type = 'supplier' - # Compute 'name' to be used in receivable/payable line. - rec_pay_line_name = '' - if payment.payment_type == 'transfer': - rec_pay_line_name = payment.name - else: - if payment.partner_type == 'customer': - if payment.payment_type == 'inbound': - rec_pay_line_name += _("Customer Payment") - elif payment.payment_type == 'outbound': - rec_pay_line_name += _("Customer Credit Note") - elif payment.partner_type == 'supplier': - if payment.payment_type == 'inbound': - rec_pay_line_name += _("Vendor Credit Note") - elif payment.payment_type == 'outbound': - rec_pay_line_name += _("Vendor Payment") - if payment.invoice_ids: - rec_pay_line_name += ': %s' % ', '.join(payment.invoice_ids.mapped('name')) + liquidity_amount = liquidity_lines.amount_currency - # Compute 'name' to be used in liquidity line. - if payment.payment_type == 'transfer': - liquidity_line_name = _('Transfer to %s') % payment.destination_journal_id.name - else: - liquidity_line_name = payment.name + move_vals_to_write.update({ + 'currency_id': liquidity_lines.currency_id.id, + 'partner_id': liquidity_lines.partner_id.id, + }) + payment_vals_to_write.update({ + 'amount': abs(liquidity_amount), + 'partner_type': partner_type, + 'currency_id': liquidity_lines.currency_id.id, + 'destination_account_id': counterpart_lines.account_id.id, + 'partner_id': liquidity_lines.partner_id.id, + }) + if liquidity_amount > 0.0: + payment_vals_to_write.update({'payment_type': 'inbound'}) + elif liquidity_amount < 0.0: + payment_vals_to_write.update({'payment_type': 'outbound'}) - # ==== 'inbound' / 'outbound' ==== - - move_vals = { - 'date': payment.payment_date, - 'ref': payment.communication, - 'journal_id': payment.journal_id.id, - 'currency_id': payment.journal_id.currency_id.id or payment.company_id.currency_id.id, - 'partner_id': payment.partner_id.id, - 'line_ids': [ - # Receivable / Payable / Transfer line. - # (0, 0, { - # 'name': rec_pay_line_name, - # 'amount_currency': counterpart_amount + write_off_amount if currency_id else 0.0, - # 'currency_id': currency_id, - # 'debit': balance + write_off_balance > 0.0 and balance + write_off_balance or 0.0, - # 'credit': balance + write_off_balance < 0.0 and -balance - write_off_balance or 0.0, - # 'date_maturity': payment.payment_date, - # 'partner_id': payment.partner_id.commercial_partner_id.id, - # 'account_id': payment.destination_account_id.id, - # 'payment_id': payment.id, - # }), - # Liquidity line. - (0, 0, { - 'name': liquidity_line_name, - 'amount_currency': -liquidity_amount if liquidity_line_currency_id else 0.0, - 'currency_id': liquidity_line_currency_id, - 'debit': balance < 0.0 and -balance or 0.0, - 'credit': balance > 0.0 and balance or 0.0, - 'date_maturity': payment.payment_date, - 'partner_id': payment.partner_id.commercial_partner_id.id, - 'account_id': liquidity_line_account.id, - 'payment_id': payment.id, - }), - ], - } - - if is_manual_disperse: - for i in register_wizard.payment_invoice_ids.filtered(lambda l: l.invoice_id in payment.invoice_ids): - i_rec_pay_line_name = rec_pay_line_name - i_values = payment._values_for_payment_invoice_line(i, currency_id) - if not any((i_values['debit'], i_values['credit'])): - # do not make useless lines - continue - move_vals['line_ids'].insert(0, (0, 0, { - 'name': i_rec_pay_line_name, - # 'amount_currency': counterpart_amount + write_off_amount if currency_id else 0.0, - # 'amount_currency': i_amount_currency, - # 'currency_id': currency_id, - # # 'debit': balance + write_off_balance > 0.0 and balance + write_off_balance or 0.0, - # 'debit': i_amount if i_amount > 0.0 else 0.0, - # # 'credit': balance + write_off_balance < 0.0 and -balance - write_off_balance or 0.0, - # 'credit': -i_amount if i_amount < 0.0 else 0.0, - 'date_maturity': payment.payment_date, - 'partner_id': payment.partner_id.commercial_partner_id.id, - 'account_id': payment.destination_account_id.id, - 'payment_id': payment.id, - **i_values, - })) - else: - # insert because existing tests expect them in a set order. - move_vals['line_ids'].insert(0, (0, 0, { - 'name': rec_pay_line_name, - 'amount_currency': counterpart_amount + write_off_amount if currency_id else 0.0, - 'currency_id': currency_id, - 'debit': balance + write_off_balance > 0.0 and balance + write_off_balance or 0.0, - 'credit': balance + write_off_balance < 0.0 and -balance - write_off_balance or 0.0, - 'date_maturity': payment.payment_date, - 'partner_id': payment.partner_id.commercial_partner_id.id, - 'account_id': payment.destination_account_id.id, - 'payment_id': payment.id, - })) - - if write_off_balance: - # Write-off line. - move_vals['line_ids'].append((0, 0, { - 'name': payment.writeoff_label, - 'amount_currency': -write_off_amount, - 'currency_id': currency_id, - 'debit': write_off_balance < 0.0 and -write_off_balance or 0.0, - 'credit': write_off_balance > 0.0 and write_off_balance or 0.0, - 'date_maturity': payment.payment_date, - 'partner_id': payment.partner_id.commercial_partner_id.id, - 'account_id': payment.writeoff_account_id.id, - 'payment_id': payment.id, - })) - - if move_names: - move_vals['name'] = move_names[0] - - all_move_vals.append(move_vals) - - # ==== 'transfer' ==== - if payment.payment_type == 'transfer': - journal = payment.destination_journal_id - - # Manage custom currency on journal for liquidity line. - if journal.currency_id and payment.currency_id != journal.currency_id: - # Custom currency on journal. - liquidity_line_currency_id = journal.currency_id.id - transfer_amount = company_currency._convert(balance, journal.currency_id, payment.company_id, payment.payment_date) - else: - # Use the payment currency. - liquidity_line_currency_id = currency_id - transfer_amount = counterpart_amount - - transfer_move_vals = { - 'date': payment.payment_date, - 'ref': payment.communication, - 'partner_id': payment.partner_id.id, - 'journal_id': payment.destination_journal_id.id, - 'line_ids': [ - # Transfer debit line. - (0, 0, { - 'name': payment.name, - 'amount_currency': -counterpart_amount if currency_id else 0.0, - 'currency_id': currency_id, - 'debit': balance < 0.0 and -balance or 0.0, - 'credit': balance > 0.0 and balance or 0.0, - 'date_maturity': payment.payment_date, - 'partner_id': payment.partner_id.commercial_partner_id.id, - 'account_id': payment.company_id.transfer_account_id.id, - 'payment_id': payment.id, - }), - # Liquidity credit line. - (0, 0, { - 'name': _('Transfer from %s') % payment.journal_id.name, - 'amount_currency': transfer_amount if liquidity_line_currency_id else 0.0, - 'currency_id': liquidity_line_currency_id, - 'debit': balance > 0.0 and balance or 0.0, - 'credit': balance < 0.0 and -balance or 0.0, - 'date_maturity': payment.payment_date, - 'partner_id': payment.partner_id.commercial_partner_id.id, - 'account_id': payment.destination_journal_id.default_credit_account_id.id, - 'payment_id': payment.id, - }), - ], - } - - if move_names and len(move_names) == 2: - transfer_move_vals['name'] = move_names[1] - - all_move_vals.append(transfer_move_vals) - - return all_move_vals - - -account_payment.account_payment.post = post -account_payment.account_payment._prepare_payment_moves = _prepare_payment_moves + move.write(move._cleanup_write_orm_values(move, move_vals_to_write)) + pay.write(move._cleanup_write_orm_values(pay, payment_vals_to_write)) + +account_payment.AccountPayment._synchronize_from_moves = _synchronize_from_moves diff --git a/account_payment_disperse/security/ir.model.access.csv b/account_payment_disperse/security/ir.model.access.csv new file mode 100644 index 00000000..4ca38042 --- /dev/null +++ b/account_payment_disperse/security/ir.model.access.csv @@ -0,0 +1,2 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +account_payment_disperse.access_account_payment_register_payment_invoice,access_account_payment_register_payment_invoice,account_payment_disperse.model_account_payment_register_payment_invoice,account.group_account_invoice,1,1,1,0 diff --git a/account_payment_disperse/tests/test_payment_multi.py b/account_payment_disperse/tests/test_payment_multi.py index ab2b75a4..ecd6e3e6 100644 --- a/account_payment_disperse/tests/test_payment_multi.py +++ b/account_payment_disperse/tests/test_payment_multi.py @@ -1,146 +1,153 @@ # Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details. -from odoo.addons.account.tests.test_payment import TestPayment +from psycopg2.errors import CheckViolation +from odoo.addons.account.tests.common import AccountTestInvoicingCommon from odoo.tests.common import Form from odoo.tests import tagged -from odoo.exceptions import ValidationError -import time -# Fun fact... if you install enterprise accounting, you'll get errors -# due to required fields being missing... -# The classic fix would be the following @tagged, but for some reason this makes -# odoo.tests.common.Form suddenly not calculate the amount on the register payment wizard -# @tagged('post_install', '-at_install') -class PaymentMultiTest(TestPayment): +@tagged('post_install', '-at_install') +class PaymentMultiTest(AccountTestInvoicingCommon): + @classmethod + def setUpClass(cls, chart_template_ref=None): + super().setUpClass(chart_template_ref=chart_template_ref) + + # Customer invoices sharing the same batch. + cls.out_invoice_1 = cls.env['account.move'].create({ + 'move_type': 'out_invoice', + 'date': '2017-01-01', + 'invoice_date': '2017-01-01', + 'partner_id': cls.partner_a.id, + 'currency_id': cls.currency_data['currency'].id, + 'invoice_line_ids': [(0, 0, {'product_id': cls.product_a.id, 'price_unit': 100.0})], + }) + cls.out_invoice_2 = cls.env['account.move'].create({ + 'move_type': 'out_invoice', + 'date': '2017-01-01', + 'invoice_date': '2017-01-01', + 'partner_id': cls.partner_a.id, + 'currency_id': cls.currency_data['currency'].id, + 'invoice_line_ids': [(0, 0, {'product_id': cls.product_a.id, 'price_unit': 500.0})], + }) + (cls.out_invoice_1 | cls.out_invoice_2).action_post() + + # Vendor bills, in_invoice_1 + in_invoice_2 are sharing the same batch but not in_invoice_3. + cls.in_invoice_1 = cls.env['account.move'].create({ + 'move_type': 'in_invoice', + 'date': '2017-01-01', + 'invoice_date': '2017-01-01', + 'partner_id': cls.partner_a.id, + 'invoice_line_ids': [(0, 0, {'product_id': cls.product_a.id, 'price_unit': 100.0})], + }) + cls.in_invoice_2 = cls.env['account.move'].create({ + 'move_type': 'in_invoice', + 'date': '2017-01-01', + 'invoice_date': '2017-01-01', + 'partner_id': cls.partner_a.id, + 'invoice_line_ids': [(0, 0, {'product_id': cls.product_a.id, 'price_unit': 500.0})], + }) + (cls.in_invoice_1 | cls.in_invoice_2).action_post() - def test_multiple_payments_partial(self): - """ Create test to pay several vendor bills/invoices at once """ - # One payment for inv_1 and inv_2 (same partner) - inv_1 = self.create_invoice(amount=100, currency_id=self.currency_eur_id, partner=self.partner_agrolait.id) - inv_2 = self.create_invoice(amount=500, currency_id=self.currency_eur_id, partner=self.partner_agrolait.id) + def test_00_disperse_values_currency(self): + # 500 Gold (invoice) is 1000 Silver (payment) is 250 USD (company) + platinum = self.setup_multi_currency_data({ + 'name': "Silver", + 'symbol': 'S', + 'currency_unit_label': "Silver", + 'currency_subunit_label': "Copper", + }, rate2017=4.0)['currency'] + payment_register = Form(self.env['account.payment.register'].with_context(active_model='account.move', active_ids=self.out_invoice_2.ids)) + payment_register.currency_id = platinum + self.assertEqual(payment_register.amount, 1000.0) + payment_register.is_manual_disperse = True - ids = [inv_1.id, inv_2.id] - payment_register = Form(self.register_payments_model.with_context(active_ids=ids)) - payment_register.journal_id = self.bank_journal_euro - payment_register.payment_date = time.strftime('%Y') + '-07-15' + with payment_register.payment_invoice_ids.edit(0) as f: + f.amount = 999.0 + payment_register = payment_register.save() + disperse_line = payment_register.payment_invoice_ids[0] + self.assertEqual(disperse_line.residual, 1000.0) + self.assertEqual(disperse_line.residual_due, 1000.0) + self.assertEqual(disperse_line.difference, 1.0) + self.assertEqual(disperse_line.partner_id, self.out_invoice_2.partner_id) + + def test_10_multiple_payments_partial(self): + """ Create test to pay several vendor bills/invoices at once """ + active_ids = (self.out_invoice_1 | self.out_invoice_2).ids + payment_register = Form(self.env['account.payment.register'].with_context(active_model='account.move', active_ids=active_ids)) payment_register.is_manual_disperse = True with payment_register.payment_invoice_ids.edit(0) as f: f.amount = 99.0 - f.save() with payment_register.payment_invoice_ids.edit(1) as f: f.amount = 300.0 - f.save() self.assertEqual(payment_register.amount, 399.0, 'Amount isn\'t the amount from lines') # Persist object payment_register = payment_register.save() - payment_register.create_payments() + payment = payment_register._create_payments() - payment_ids = self.payment_model.search([('invoice_ids', 'in', ids)], order="id desc") - self.assertEqual(len(payment_ids), 1, 'Need only one payment.') - self.assertEqual(payment_ids.amount, 399.0) + self.assertEqual(len(payment), 1, 'Need only one payment.') + self.assertEqual(payment.amount, 399.0) + counterpart_lines = payment.line_ids.filtered(lambda l: l.account_id.internal_type in ('receivable', 'payable')) + self.assertEqual(len(counterpart_lines), 2) + liquidity_line = payment.line_ids - counterpart_lines + self.assertEqual(liquidity_line.amount_currency, 399.0) + self.assertEqual(counterpart_lines[0].amount_currency, -99.0) + self.assertEqual(counterpart_lines[1].amount_currency, -300.0) - self.assertEqual(inv_1.amount_residual_signed, 1.0) - self.assertEqual(inv_2.amount_residual_signed, 200.0) + self.assertEqual(self.out_invoice_1.amount_residual, 1.0) + self.assertEqual(self.out_invoice_2.amount_residual, 200.0) - payment_register = Form(self.register_payments_model.with_context(active_ids=ids)) - payment_register.journal_id = self.bank_journal_euro - payment_register.payment_date = time.strftime('%Y') + '-07-15' + payment_register = Form(self.env['account.payment.register'].with_context(active_model='account.move', active_ids=active_ids)) payment_register.is_manual_disperse = True with payment_register.payment_invoice_ids.edit(0) as f: f.amount = 0.0 - f.save() with payment_register.payment_invoice_ids.edit(1) as f: f.amount = 200.0 - f.save() payment_register = payment_register.save() - payment_register.create_payments() - self.assertEqual(inv_1.amount_residual_signed, 1.0) - self.assertEqual(inv_2.amount_residual_signed, 0.0) + payment_register._create_payments() + self.assertEqual(self.out_invoice_1.amount_residual, 1.0) + self.assertEqual(self.out_invoice_2.amount_residual, 0.0) - def test_multiple_payments_write_off(self): - inv_1 = self.create_invoice(amount=100, currency_id=self.currency_eur_id, partner=self.partner_agrolait.id) - inv_2 = self.create_invoice(amount=500, currency_id=self.currency_eur_id, partner=self.partner_agrolait.id) - - ids = [inv_1.id, inv_2.id] - payment_register = Form(self.register_payments_model.with_context(active_ids=ids)) - payment_register.journal_id = self.bank_journal_euro - payment_register.payment_date = time.strftime('%Y') + '-07-15' + def test_20_multiple_payments_write_off(self): + active_ids = (self.out_invoice_1 | self.out_invoice_2).ids + + payment_register = Form(self.env['account.payment.register'].with_context(active_model='account.move', active_ids=active_ids)) payment_register.is_manual_disperse = True - payment_register.writeoff_account_id = self.transfer_account + payment_register.writeoff_account_id = self.company_data['default_account_revenue'] with payment_register.payment_invoice_ids.edit(0) as f: f.amount = 100.0 f.close_balance = True - f.save() with payment_register.payment_invoice_ids.edit(1) as f: f.amount = 300.0 f.close_balance = True - f.save() self.assertEqual(payment_register.amount, 400.0, 'Amount isn\'t the amount from lines') + self.assertEqual(payment_register.payment_difference_handling, 'reconcile') payment_register = payment_register.save() - self.assertEqual(sum(payment_register.mapped('payment_invoice_ids.difference')), -200.0) - payment_register.create_payments() + self.assertEqual(sum(payment_register.mapped('payment_invoice_ids.difference')), 200.0) + self.assertEqual(payment_register.payment_difference_handling, 'reconcile') + payment = payment_register._create_payments() - payment_ids = self.payment_model.search([('invoice_ids', 'in', ids)], order="id desc") - self.assertEqual(len(payment_ids), 1, 'Need only one payment.') - self.assertEqual(payment_ids.amount, 400.0) + self.assertEqual(len(payment), 1, 'Need only one payment.') + self.assertEqual(payment.amount, 400.0) - self.assertEqual(inv_1.amount_residual_signed, 0.0) - self.assertEqual(inv_2.amount_residual_signed, 0.0) + self.assertEqual(self.out_invoice_1.amount_residual, 0.0) + self.assertEqual(self.out_invoice_2.amount_residual, 0.0) - def test_multiple_payments_partial_multi(self): - """ Create test to pay several vendor bills/invoices at once """ - # One payment for inv_1 and inv_2 (different partners) - inv_1 = self.create_invoice(amount=100, currency_id=self.currency_eur_id, partner=self.partner_agrolait.id) - inv_2 = self.create_invoice(amount=500, currency_id=self.currency_eur_id, partner=self.partner_china_exp.id) - - ids = [inv_1.id, inv_2.id] - payment_register = Form(self.register_payments_model.with_context(active_ids=ids)) - payment_register.journal_id = self.bank_journal_euro - payment_register.payment_date = time.strftime('%Y') + '-07-15' + def test_30_vendor_multiple_payments_write_off(self): + active_ids = (self.in_invoice_1 | self.in_invoice_2).ids + payment_register = Form(self.env['account.payment.register'].with_context(active_model='account.move', active_ids=active_ids)) payment_register.is_manual_disperse = True with payment_register.payment_invoice_ids.edit(0) as f: f.amount = 100.0 - f.save() with payment_register.payment_invoice_ids.edit(1) as f: f.amount = 300.0 - f.save() - - self.assertEqual(payment_register.amount, 400.0, 'Amount isn\'t the amount from lines') - - payment_register = payment_register.save() - payment_register.create_payments() - - payment_ids = self.payment_model.search([('invoice_ids', 'in', ids)], order="id desc") - self.assertEqual(len(payment_ids), 2, 'Need two payments.') - self.assertEqual(sum(payment_ids.mapped('amount')), 400.0) - - self.assertEqual(inv_1.amount_residual_signed, 0.0) - self.assertEqual(inv_2.amount_residual_signed, 200.0) - - def test_vendor_multiple_payments_write_off(self): - inv_1 = self.create_invoice(amount=100, type='in_invoice', currency_id=self.currency_eur_id, partner=self.partner_agrolait.id) - inv_2 = self.create_invoice(amount=500, type='in_invoice', currency_id=self.currency_eur_id, partner=self.partner_agrolait.id) - ids = [inv_1.id, inv_2.id] - payment_register = Form(self.register_payments_model.with_context(active_ids=ids)) - payment_register.journal_id = self.bank_journal_euro - payment_register.payment_date = time.strftime('%Y') + '-07-15' - payment_register.is_manual_disperse = True - - with payment_register.payment_invoice_ids.edit(0) as f: - f.amount = 100.0 - f.save() - with payment_register.payment_invoice_ids.edit(1) as f: - f.amount = 300.0 - f.save() self.assertEqual(payment_register.amount, 400.0) @@ -148,16 +155,69 @@ class PaymentMultiTest(TestPayment): payment_register = payment_register.save() payment_register.action_toggle_close_balance() - with self.assertRaises(ValidationError): - payment_register.create_payments() + with self.assertRaises(CheckViolation): + payment_register._create_payments() # Need the writeoff account - payment_register.writeoff_account_id = self.transfer_account - payment_register.create_payments() + payment_register.writeoff_account_id = self.company_data['default_account_revenue'] + payment = payment_register._create_payments() - payment_ids = self.payment_model.search([('invoice_ids', 'in', ids)], order="id desc") - self.assertEqual(len(payment_ids), 1, 'Need only one payment.') - self.assertEqual(payment_ids.amount, 400.0) + self.assertEqual(len(payment), 1, 'Need only one payment.') + self.assertEqual(payment.amount, 400.0) - self.assertEqual(inv_1.amount_residual_signed, 0.0) - self.assertEqual(inv_2.amount_residual_signed, 0.0) + self.assertEqual(self.in_invoice_1.amount_residual, 0.0) + self.assertEqual(self.in_invoice_2.amount_residual, 0.0) + + def test_40_line_is_zero(self): + active_ids = (self.out_invoice_1 | self.out_invoice_2).ids + + payment_register = Form(self.env['account.payment.register'].with_context(active_model='account.move', active_ids=active_ids)) + payment_register.is_manual_disperse = True + + with payment_register.payment_invoice_ids.edit(0) as f: + f.amount = 0.0 + with payment_register.payment_invoice_ids.edit(1) as f: + f.amount = 300.0 + + self.assertEqual(payment_register.amount, 300.0, 'Amount isn\'t the amount from lines') + self.assertEqual(payment_register.payment_difference_handling, 'open') + + payment_register = payment_register.save() + self.assertEqual(sum(payment_register.mapped('payment_invoice_ids.difference')), 300.0) + payment = payment_register._create_payments() + + self.assertEqual(len(payment), 1, 'Need only one payment.') + counterpart_lines = payment.line_ids.filtered(lambda l: l.account_id.internal_type in ('receivable', 'payable')) + self.assertEqual(len(counterpart_lines), 1) + self.assertEqual(payment.amount, 300.0) + + self.assertEqual(self.out_invoice_1.amount_residual, 100.0) + self.assertEqual(self.out_invoice_2.amount_residual, 200.0) + + def test_50_line_is_zero_closed(self): + active_ids = (self.out_invoice_1 | self.out_invoice_2).ids + + payment_register = Form(self.env['account.payment.register'].with_context(active_model='account.move', active_ids=active_ids)) + payment_register.is_manual_disperse = True + payment_register.writeoff_account_id = self.company_data['default_account_revenue'] + + with payment_register.payment_invoice_ids.edit(0) as f: + f.amount = 0.0 + f.close_balance = True + with payment_register.payment_invoice_ids.edit(1) as f: + f.amount = 300.0 + + self.assertEqual(payment_register.amount, 300.0, 'Amount isn\'t the amount from lines') + self.assertEqual(payment_register.payment_difference_handling, 'reconcile') + + payment_register = payment_register.save() + self.assertEqual(sum(payment_register.mapped('payment_invoice_ids.difference')), 300.0) + payment = payment_register._create_payments() + + self.assertEqual(len(payment), 1, 'Need only one payment.') + counterpart_lines = payment.line_ids.filtered(lambda l: l.account_id.internal_type in ('receivable', 'payable')) + self.assertEqual(len(counterpart_lines), 2) + self.assertEqual(payment.amount, 300.0) + + self.assertEqual(self.out_invoice_1.amount_residual, 0.0) + self.assertEqual(self.out_invoice_2.amount_residual, 200.0) diff --git a/account_payment_disperse/wizard/register_payment_wizard.py b/account_payment_disperse/wizard/register_payment_wizard.py index 68f0d462..ff15fb82 100644 --- a/account_payment_disperse/wizard/register_payment_wizard.py +++ b/account_payment_disperse/wizard/register_payment_wizard.py @@ -2,8 +2,6 @@ from datetime import datetime from odoo import api, fields, models, _ -from odoo.exceptions import ValidationError -from odoo.addons.account.models.account_payment import MAP_INVOICE_TYPE_PARTNER_TYPE class AccountRegisterPayments(models.TransientModel): @@ -11,9 +9,7 @@ class AccountRegisterPayments(models.TransientModel): is_manual_disperse = fields.Boolean(string='Disperse Manually') payment_invoice_ids = fields.One2many('account.payment.register.payment_invoice', 'wizard_id', string='Invoices') - writeoff_account_id = fields.Many2one('account.account', string="Difference Account", domain="[('deprecated', '=', False)]", copy=False) - requires_writeoff_account = fields.Boolean(compute='_compute_requires_writeoff_account') - amount = fields.Float(string='Amount', compute='_compute_amount') + payment_difference_handling = fields.Selection(compute='_compute_payment_difference_handling', store=True, readonly=False) due_date_cutoff = fields.Date(string='Due Date Cutoff', default=fields.Date.today) due_date_behavior = fields.Selection([ ('due', 'Due'), @@ -21,88 +17,177 @@ class AccountRegisterPayments(models.TransientModel): ], string='Due Date Behavior', default='due') @api.model - def default_get(self, fields): - rec = super(AccountRegisterPayments, self).default_get(fields) - if 'invoice_ids' in rec: - invoice_ids = rec['invoice_ids'][0][2] - rec['payment_invoice_ids'] = [(0, 0, {'invoice_id': i}) for i in invoice_ids] + def default_get(self, default_fields): + rec = super(AccountRegisterPayments, self).default_get(default_fields) + if 'line_ids' in rec: + lines = self.env['account.move.line'].browse(rec['line_ids'][0][2]) + if lines: + rec['payment_invoice_ids'] = [fields.Command.create({'invoice_id': i}) for i in lines.move_id.ids] return rec - - def create_payments(self): - for payment in self.filtered(lambda p: not p.amount and p.invoice_ids): - payment._compute_amount() - for payment in self.filtered(lambda p: p.is_manual_disperse): - line_amount = sum(payment.payment_invoice_ids.mapped('amount')) - if abs(line_amount - payment.amount) >= 0.01: - raise ValidationError('Cannot pay for %0.2f worth of invoices with %0.2f total.' % - (line_amount, payment.amount)) - if not payment.writeoff_account_id and payment.payment_invoice_ids.filtered(lambda l: l.close_balance and l.difference): - raise ValidationError('Closing balance will require a difference account.') - new_self = self.with_context(payment_wizard_id=self.id) - return super(AccountRegisterPayments, new_self).create_payments() - + @api.onchange('is_manual_disperse') - def _ensure_is_manual_disperse(self): - for payment in self: - payment.group_payment = payment.is_manual_disperse + def _ensure_is_grouped(self): + for wizard in self.filtered('is_manual_disperse'): + wizard.group_payment = True + + @api.onchange('group_payment') + def _ensure_is_not_manual(self): + for wizard in self.filtered(lambda w: not w.group_payment): + wizard.is_manual_disperse = False - @api.depends('payment_invoice_ids.amount', 'invoice_ids', 'journal_id', 'payment_date', 'is_manual_disperse') + @api.depends('payment_invoice_ids.close_balance') + def _compute_payment_difference_handling(self): + for wizard in self: + if any(wizard.payment_invoice_ids.mapped('close_balance')): + wizard.payment_difference_handling = 'reconcile' + else: + wizard.payment_difference_handling = 'open' + + @api.depends('source_amount', 'source_amount_currency', 'source_currency_id', 'company_id', 'currency_id', 'payment_date', 'is_manual_disperse', 'payment_invoice_ids.amount') def _compute_amount(self): - for payment in self.filtered(lambda p: p.is_manual_disperse): - payment.amount = sum(payment.mapped('payment_invoice_ids.amount') or [0.0]) - for payment in self.filtered(lambda p: not p.is_manual_disperse): - if payment.invoice_ids: - payment.amount = self.env['account.payment']._compute_payment_amount( - payment.invoice_ids, - payment.invoice_ids[0].currency_id, - payment.journal_id, - payment.payment_date) - else: - payment.amount = 0.0 - - @api.depends('payment_invoice_ids.difference', 'payment_invoice_ids.close_balance') - def _compute_requires_writeoff_account(self): - for payment in self: - if payment.is_manual_disperse: - payment.requires_writeoff_account = bool(payment.payment_invoice_ids.filtered( - lambda l: l.difference and l.close_balance)) - else: - payment.requires_writeoff_account = False - - def _prepare_payment_vals(self, invoices): - '''Create the payment values. - - :param invoices: The invoices/bills to pay. In case of multiple - documents, they need to be grouped by partner, bank, journal and - currency. - :return: The payment values as a dictionary. - ''' + is_manual = self.filtered(lambda p: p.is_manual_disperse) + super(AccountRegisterPayments, self - is_manual)._compute_amount() + for wizard in is_manual: + wizard.amount = sum(wizard.mapped('payment_invoice_ids.amount') or [0.0]) + + def _create_payment_vals_from_wizard(self): + res = super(AccountRegisterPayments, self)._create_payment_vals_from_wizard() if self.is_manual_disperse: - # this will already be positive for both invoice types unlink the below amount (abs) - amount = sum(self.payment_invoice_ids.filtered(lambda l: l.invoice_id in invoices).mapped('amount')) - sign = 1.0 - if invoices: - invoice = invoices[0] - sign = 1.0 if invoice.type in ('out_invoice', 'out_refund') else -1.0 + write_off_line_vals = res.get('write_off_line_vals') + if write_off_line_vals: + write_off_line_vals['amount'] = sum(self.payment_invoice_ids.filtered('close_balance').mapped('difference')) + res['line_ids'] = self._create_payment_line_vals( + write_off_line_vals=write_off_line_vals, + ) + return res + + def _create_payment_line_vals(self, write_off_line_vals=None): + write_off_line_vals = write_off_line_vals or {} + + outstanding_account_id = self._get_outstanding_account_id() + if not outstanding_account_id: + raise UserError(_( + "You can't create a new payment without an outstanding payments/receipts account set either on the company or the %s payment method in the %s journal.", + self.payment_method_line_id.name, self.journal_id.display_name)) + + write_off_amount_currency = write_off_line_vals.get('amount', 0.0) + + if self.payment_type == 'inbound': + # Receive money. + liquidity_amount_currency = self.amount + elif self.payment_type == 'outbound': + # Send money. + liquidity_amount_currency = -self.amount + write_off_amount_currency *= -1 else: - amount = self.env['account.payment']._compute_payment_amount(invoices, invoices[0].currency_id, - self.journal_id, self.payment_date) - sign = 1.0 if amount > 0.0 else -1.0 - values = { - 'journal_id': self.journal_id.id, - 'payment_method_id': self.payment_method_id.id, - 'payment_date': self.payment_date, - 'communication': self._prepare_communication(invoices), - 'invoice_ids': [(6, 0, invoices.ids)], - 'payment_type': 'inbound' if sign > 0 else 'outbound', - 'amount': abs(amount), - 'currency_id': invoices[0].currency_id.id, - 'partner_id': invoices[0].commercial_partner_id.id, - 'partner_type': MAP_INVOICE_TYPE_PARTNER_TYPE[invoices[0].type], - 'partner_bank_account_id': invoices[0].invoice_partner_bank_id.id, - 'writeoff_account_id': self.writeoff_account_id.id, - } - return values + liquidity_amount_currency = write_off_amount_currency = 0.0 + + write_off_balance = self.currency_id._convert( + write_off_amount_currency, + self.company_id.currency_id, + self.company_id, + self.payment_date, + ) + liquidity_balance = self.currency_id._convert( + liquidity_amount_currency, + self.company_id.currency_id, + self.company_id, + self.payment_date, + ) + + # Compute a default label to set on the journal items. + + payment_display_name = self.env['account.payment']._prepare_payment_display_name() + + default_line_name = self.env['account.move.line']._get_default_line_name( + payment_display_name['%s-%s' % (self.payment_type, self.partner_type)], + self.amount, + self.currency_id, + self.payment_date, + partner=self.partner_id, + ) + + line_vals_list = [ + # Liquidity line. + { + 'name': default_line_name, + 'date_maturity': self.payment_date, + 'amount_currency': liquidity_amount_currency, + 'currency_id': self.currency_id.id, + 'debit': liquidity_balance if liquidity_balance > 0.0 else 0.0, + 'credit': -liquidity_balance if liquidity_balance < 0.0 else 0.0, + 'partner_id': self.partner_id.id, + 'account_id': outstanding_account_id, + }, + ] + for line in self.payment_invoice_ids.filtered(lambda l: not self.currency_id.is_zero(l.amount) + or l.close_balance): + # Receivable / Payable. + line_vals_list.append({ + 'name': default_line_name, + 'date_maturity': self.payment_date, + # 'amount_currency': counterpart_amount_currency, + # 'currency_id': self.currency_id.id, + # 'debit': counterpart_balance if counterpart_balance > 0.0 else 0.0, + # 'credit': -counterpart_balance if counterpart_balance < 0.0 else 0.0, + 'partner_id': self.partner_id.id, + 'account_id': self.line_ids[0].account_id.id, + **line._values_for_payment_invoice_line() + }) + if not self.currency_id.is_zero(write_off_amount_currency): + # Write-off line. + line_vals_list.append({ + 'name': write_off_line_vals.get('name') or default_line_name, + 'amount_currency': write_off_amount_currency, + 'currency_id': self.currency_id.id, + 'debit': write_off_balance if write_off_balance > 0.0 else 0.0, + 'credit': -write_off_balance if write_off_balance < 0.0 else 0.0, + 'partner_id': self.partner_id.id, + 'account_id': write_off_line_vals.get('account_id'), + }) + return [fields.Command.create(line_vals) for line_vals in line_vals_list] + + def _get_outstanding_account_id(self): + outstanding_account_id = False + if self.payment_type == 'inbound': + outstanding_account_id = (self.payment_method_line_id.payment_account_id + or self.journal_id.company_id.account_journal_payment_debit_account_id) + elif self.payment_type == 'outbound': + outstanding_account_id = (self.payment_method_line_id.payment_account_id + or self.journal_id.company_id.account_journal_payment_credit_account_id) + return outstanding_account_id and outstanding_account_id.id + + def _reconcile_payments(self, to_process, edit_mode=False): + if self.is_manual_disperse: + domain = [ + ('parent_state', '=', 'posted'), + ('account_internal_type', 'in', ('receivable', 'payable')), + ('reconciled', '=', False), + ] + for vals in to_process: + # account_id = vals['batch']['payment_values']['account_id'] + payment = vals['payment'] + account = payment.destination_account_id + payment_lines = vals['payment'].line_ids.filtered_domain(domain) + move_lines = vals['to_reconcile'] + currency = self.company_id.currency_id + + for disperse_line in self.payment_invoice_ids.filtered(lambda l: l.invoice_id in move_lines.move_id): + line_values = disperse_line._values_for_payment_invoice_line() + lines_to_reconcile = payment_lines.filtered( + lambda l: l.account_id == account + and not l.reconciled + and currency.is_zero(l.debit - line_values['debit']) + and currency.is_zero(l.credit - line_values['credit']) + ) + lines_to_reconcile |= move_lines.filtered( + lambda l: l.account_id == account + and not l.reconciled + and l.move_id == disperse_line.invoice_id + ) + lines_to_reconcile.reconcile() + else: + super(AccountRegisterPayments, self)._reconcile_payments(to_process, edit_mode=edit_mode) def action_fill_residual(self): for payment in self: @@ -127,7 +212,7 @@ class AccountRegisterPayments(models.TransientModel): 'name': _('Register Payment'), 'res_model': 'account.payment.register', 'view_mode': 'form', - 'view_id': self.env.ref('account.view_account_payment_form_multi').id, + 'view_id': self.env.ref('account.view_account_payment_register_form').id, 'context': self.env.context, 'target': 'new', 'res_id': self.id, @@ -137,6 +222,7 @@ class AccountRegisterPayments(models.TransientModel): class AccountRegisterPaymentsInvoiceLine(models.TransientModel): _name = 'account.payment.register.payment_invoice' + _description = 'Register Payment Invoice' wizard_id = fields.Many2one('account.payment.register') invoice_id = fields.Many2one('account.move', string='Invoice', required=True) @@ -147,12 +233,12 @@ class AccountRegisterPaymentsInvoiceLine(models.TransientModel): amount = fields.Float(string='Amount') close_balance = fields.Boolean(string='Close Balance', help='Write off remaining balance.') - @api.depends('invoice_id', 'wizard_id.due_date_cutoff', 'wizard_id.due_date_behavior', 'invoice_id.partner_id') + @api.depends('invoice_id', 'wizard_id.due_date_cutoff', 'wizard_id.due_date_behavior', 'invoice_id.partner_id', 'wizard_id.currency_id', 'wizard_id.company_id') def _compute_invoice_balances(self): dummy_date = datetime(1980, 1, 1) for line in self: invoice = line.invoice_id.browse(line.invoice_id.id) - sign = 1.0 if invoice.type in ('out_invoice', 'out_refund') else -1.0 + sign = 1.0 if invoice.move_type in ('out_invoice', 'out_refund') else -1.0 residual = sign * invoice.amount_residual_signed cutoff_date = line.wizard_id.due_date_cutoff @@ -185,10 +271,28 @@ class AccountRegisterPaymentsInvoiceLine(models.TransientModel): total_reconciled -= partial_line.amount for partial_line in move_line.matched_credit_ids: total_reconciled += partial_line.amount + if self.wizard_id.currency_id: + residual = invoice.company_id.currency_id._convert( + residual, + self.wizard_id.currency_id, + self.wizard_id.company_id, + self.wizard_id.payment_date, + ) + total_amount = invoice.company_id.currency_id._convert( + total_amount, + self.wizard_id.currency_id, + self.wizard_id.company_id, + self.wizard_id.payment_date, + ) + total_reconciled = invoice.company_id.currency_id._convert( + total_reconciled, + self.wizard_id.currency_id, + self.wizard_id.company_id, + self.wizard_id.payment_date, + ) values = { 'residual': residual, 'residual_due': sign * (total_amount - total_reconciled), - 'difference': (line.amount or 0.0) - residual, 'partner_id': invoice.partner_id.id, } line.update(values) @@ -196,4 +300,23 @@ class AccountRegisterPaymentsInvoiceLine(models.TransientModel): @api.depends('amount', 'residual', 'residual_due', 'invoice_id') def _compute_difference(self): for line in self: - line.difference = line.amount - line.residual + line.difference = line.residual - line.amount + + def _values_for_payment_invoice_line(self): + self.ensure_one() + + sign = 1.0 if self.wizard_id.payment_type == 'outbound' else -1.0 + i_write_off_amount = self.difference if self.difference and self.close_balance else 0.0 + i_amount_currency = (self.amount + i_write_off_amount) + i_amount = sign * self.wizard_id.currency_id._convert( + i_amount_currency, + self.wizard_id.company_id.currency_id, + self.wizard_id.company_id, + self.wizard_id.payment_date, + ) + return { + 'amount_currency': sign * i_amount_currency, + 'currency_id': self.wizard_id.currency_id.id, + 'debit': i_amount if i_amount > 0.0 else 0.0, + 'credit': -i_amount if i_amount < 0.0 else 0.0, + } diff --git a/account_payment_disperse/wizard/register_payment_wizard_views.xml b/account_payment_disperse/wizard/register_payment_wizard_views.xml index 0371539c..48129622 100644 --- a/account_payment_disperse/wizard/register_payment_wizard_views.xml +++ b/account_payment_disperse/wizard/register_payment_wizard_views.xml @@ -1,13 +1,16 @@ - - account.payment.form.multi.inherit + + account.payment.register.form.inherit account.payment.register - + - - + + + + {'invisible': [('is_manual_disperse', '=', True)]} @@ -26,8 +29,6 @@ - -