diff --git a/models/__init__.py b/models/__init__.py index 65265a7..d35689f 100644 --- a/models/__init__.py +++ b/models/__init__.py @@ -1,2 +1,5 @@ from . import res_users from . import account_journal +from . import account_payment +from . import account_payment_register + diff --git a/models/account_journal.py b/models/account_journal.py index b29e275..43ad97d 100644 --- a/models/account_journal.py +++ b/models/account_journal.py @@ -32,7 +32,7 @@ class AccountJournal(models.Model): if not bypass: allowed_ids = user.sudo().allowed_journal_ids.ids domain = [('id', 'in', allowed_ids)] + list(domain) - + return super(AccountJournal, self)._search(domain, offset=offset, limit=limit, order=order, **kwargs) def write(self, vals): diff --git a/models/account_payment.py b/models/account_payment.py new file mode 100644 index 0000000..e73e4ee --- /dev/null +++ b/models/account_payment.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +from odoo import models, api + +class AccountPayment(models.Model): + _inherit = 'account.payment' + + @api.depends('company_id', 'partner_id') + def _compute_journal_id(self): + # Call the parent method first to compute standard default journal + super(AccountPayment, self)._compute_journal_id() + + # Override default journal if the user has specific allowed journals + user = self.env.user + allowed_journals = user.sudo().allowed_journal_ids + if allowed_journals: + for payment in self: + # If the currently selected journal is not in user's allowed journals + if not payment.journal_id or payment.journal_id not in allowed_journals: + # Try to select from intersection of allowed journals and available journals + allowed_available = allowed_journals & payment.available_journal_ids + if allowed_available: + payment.journal_id = allowed_available[0] + else: + # Fallback to the first allowed journal matching payment company + company = payment.company_id or self.env.company + company_journals = allowed_journals.filtered( + lambda j: j.company_id.id in [company.id, False] or j.company_id in company.parent_ids or j.company_id in company.child_ids + ) + if company_journals: + payment.journal_id = company_journals[0] + else: + payment.journal_id = allowed_journals[0] diff --git a/models/account_payment_register.py b/models/account_payment_register.py new file mode 100644 index 0000000..5ffa00b --- /dev/null +++ b/models/account_payment_register.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +from odoo import models, api + +class AccountPaymentRegister(models.TransientModel): + _inherit = 'account.payment.register' + + @api.depends('available_journal_ids') + def _compute_journal_id(self): + # Call the parent method first to compute standard default journal + super(AccountPaymentRegister, self)._compute_journal_id() + + # Override default journal if the user has specific allowed journals + user = self.env.user + allowed_journals = user.sudo().allowed_journal_ids + if allowed_journals: + for wizard in self: + # If the currently selected journal is not in user's allowed journals + if not wizard.journal_id or wizard.journal_id not in allowed_journals: + # Try to select from intersection of allowed journals and available journals + allowed_available = allowed_journals & wizard.available_journal_ids + if allowed_available: + wizard.journal_id = allowed_available[0] + else: + # Fallback to the first allowed journal matching wizard company + company = wizard.company_id or self.env.company + company_journals = allowed_journals.filtered( + lambda j: j.company_id.id in [company.id, False] or j.company_id in company.parent_ids or j.company_id in company.child_ids + ) + if company_journals: + wizard.journal_id = company_journals[0] + else: + wizard.journal_id = allowed_journals[0]