account_allowed_journal/models/account_payment_register.py

33 lines
1.7 KiB
Python

# -*- 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]