diff --git a/odex25_accounting/exp_budget_check/__manifest__.py b/odex25_accounting/exp_budget_check/__manifest__.py index 225d5b1b7..6a0759a18 100644 --- a/odex25_accounting/exp_budget_check/__manifest__.py +++ b/odex25_accounting/exp_budget_check/__manifest__.py @@ -27,6 +27,7 @@ # always loaded 'data': [ 'security/security.xml', + 'data/server_action.xml', 'views/account_invoice_view.xml', 'views/hr_expense_view.xml', ], diff --git a/odex25_accounting/exp_budget_check/data/server_action.xml b/odex25_accounting/exp_budget_check/data/server_action.xml new file mode 100644 index 000000000..749f69dee --- /dev/null +++ b/odex25_accounting/exp_budget_check/data/server_action.xml @@ -0,0 +1,13 @@ + + + + + Migrate invoice_rec_id to invoice_rec_ids + + code + + env['account.payment'].migrate_invoice_rec_id_to_invoice_rec_ids() + + + + \ No newline at end of file diff --git a/odex25_accounting/exp_budget_check/models/account_invoice.py b/odex25_accounting/exp_budget_check/models/account_invoice.py index f1a98124b..7ecf13f4a 100644 --- a/odex25_accounting/exp_budget_check/models/account_invoice.py +++ b/odex25_accounting/exp_budget_check/models/account_invoice.py @@ -64,17 +64,21 @@ class AccountMove(models.Model): rec_payment_count = fields.Integer(compute='_compute_rec_payment_count', string='# Payments') def _compute_rec_payment_count(self): + # for invoice in self: + # payments = self.env['account.payment'].sudo().search_count([ + # ('invoice_rec_id', '=', invoice.id) + # ]) + # invoice.rec_payment_count = payments for invoice in self: - payments = self.env['account.payment'].sudo().search_count([ - ('invoice_rec_id', '=', invoice.id) - ]) - invoice.rec_payment_count = payments + payments = self.env['account.payment'].sudo().search([]).filtered(lambda r: invoice.id in r.invoice_rec_ids) + invoice.rec_payment_count = len(payments) def action_open_related_payment_records(self): """ Opens a tree view with related records filtered by a dynamic domain """ - payments = self.env['account.payment'].search([ - ('invoice_rec_id', '=', self.id) - ]).ids + # payments = self.env['account.payment'].search([ + # ('invoice_rec_id', '=', self.id) + # ]).ids + payments = self.env['account.payment'].search([]).filtered(lambda r: self.id in r.invoice_rec_ids).ids return { 'name': _('Payments'), diff --git a/odex25_accounting/exp_budget_check/views/account_invoice_view.xml b/odex25_accounting/exp_budget_check/views/account_invoice_view.xml index b30989cfd..8b335d37f 100644 --- a/odex25_accounting/exp_budget_check/views/account_invoice_view.xml +++ b/odex25_accounting/exp_budget_check/views/account_invoice_view.xml @@ -8,7 +8,7 @@ - + diff --git a/odex25_accounting/exp_budget_check/wizard/payment_register.py b/odex25_accounting/exp_budget_check/wizard/payment_register.py index c369fce29..9eba2625a 100644 --- a/odex25_accounting/exp_budget_check/wizard/payment_register.py +++ b/odex25_accounting/exp_budget_check/wizard/payment_register.py @@ -1,4 +1,4 @@ -from odoo import models, fields, _ +from odoo import models, fields, api, _ from odoo.exceptions import UserError @@ -6,6 +6,18 @@ class AccountPayment(models.Model): _inherit = "account.payment" invoice_rec_id = fields.Many2one(comodel_name='account.move', string='Invoice', copy=False) + invoice_rec_ids = fields.Many2many(comodel_name='account.move', string='Invoice', copy=False) + + + @api.model + def migrate_invoice_rec_id_to_invoice_rec_ids(self): + """Migrates Many2one `invoice_rec_id` to Many2many `invoice_rec_ids`.""" + records = self.search([('invoice_rec_id', '!=', False)]) + + for record in records: + record.invoice_rec_ids = [(4, record.invoice_rec_id.id)] # Add existing ID + + return f"Migrated {len(records)} records from invoice_rec_id to invoice_rec_ids." class AccountPaymentRegister(models.TransientModel): @@ -13,11 +25,12 @@ class AccountPaymentRegister(models.TransientModel): def _create_payments(self): self.ensure_one() - active_id = self.env.context.get('active_id') - if active_id: - invoice_payment = self.env['account.payment'].search( - [('invoice_rec_id', '=', active_id), - ('state', '=', 'draft')]) + active_ids = self.env.context.get('active_ids') + if active_ids: + # invoice_payment = self.env['account.payment'].search( + # [('invoice_rec_id', '=', active_id), + # ('state', '=', 'draft')]) + invoice_payment = self.env['account.payment'].search([('state', '=', 'draft')]).filtered(lambda r: set(active_ids) & set(r.invoice_rec_ids.ids)) if invoice_payment: raise UserError( _('You can not create payment for this invoice because there is a draft payment for it')) @@ -58,7 +71,7 @@ class AccountPaymentRegister(models.TransientModel): self._reconcile_payments(process_final, edit_mode=edit_mode) for payment in payments: if payment.payment_type == 'outbound': - payment.invoice_rec_id = active_id + payment.invoice_rec_ids = [(4, active_id) for active_id in active_ids] payment.action_cancel() payment.action_draft() return payments