[14.0][ADD] new module account_journal_display_type

This commit is contained in:
matthieu.saison
2024-02-07 17:16:09 +01:00
parent f2172a5f06
commit c89f1b74dc
8 changed files with 261 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl).
from . import models
from .hook import post_init_hook

View File

@@ -0,0 +1,17 @@
# Copyright 2024 Akretion (https://www.akretion.com).
# @author Matthieu SAISON <matthieu.saison@akretion.com>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{
"name": "Account Journal Display Type",
"summary": "Account Journal type for payment",
"version": "14.0.1.0.0",
"development_status": "Beta",
"category": "Accounting",
"website": "https://github.com/OCA/account-financial-tools",
"author": "Akretion, " "Odoo Community Association (OCA)",
"maintainers": ["matthieu_saison"],
"license": "AGPL-3",
"depends": ["account", "account_statement_completion_label_simple"],
"data": ["views/account_journal_view.xml"],
"post_init_hook": "post_init_hook",
}

View File

@@ -0,0 +1,8 @@
from odoo import SUPERUSER_ID, api
def post_init_hook(cr, registry):
env = api.Environment(cr, SUPERUSER_ID, {})
journal_ids = env["account.journal"].search([])
for journal in journal_ids:
journal.display_type = journal.type

View File

@@ -0,0 +1 @@
from . import account_journal

View File

@@ -0,0 +1,75 @@
from odoo import api, fields, models
class AccountJournal(models.Model):
_inherit = "account.journal"
display_type = fields.Selection(
[
("sale", "Sales"),
("purchase", "Purchase"),
("cash", "Cash"),
("bank", "Bank"),
("payment", "Payment"),
("general", "Miscellaneous"),
],
required=True,
inverse="_inverse_type",
help="Select 'Sale' for customer invoices journals.\n"
"Select 'Purchase' for vendor bills journals.\n"
"Select 'Cash' or 'Bank' for journals that are used in customer or vendor payments.\n"
"Select 'General' for miscellaneous operations journals.",
)
type = fields.Selection(compute="_compute_type", store=True)
default_account_id = fields.Many2one(
comodel_name="account.account",
compute="_compute_default_account_id",
readonly=False,
store=True,
)
payment_debit_account_id = fields.Many2one(
comodel_name="account.account",
compute="_compute_payment_debit_account_id",
readonly=False,
store=True,
)
@api.depends("display_type")
def _compute_type(self):
for record in self:
if record.display_type == "payment":
record.type = "bank"
else:
record.type = record.display_type
@api.depends("display_type", "payment_debit_account_id")
def _compute_default_account_id(self):
for record in self:
if record.display_type == "payment":
record.default_account_id = record.payment_debit_account_id
@api.depends("display_type", "default_account_id")
def _compute_payment_debit_account_id(self):
for record in self:
if record.type == "cash":
record.payment_debit_account_id = record.default_account_id
record.payment_credit_account_id = record.default_account_id
@api.model
def _fill_missing_values(self, vals):
# _fill_missing_values automaticly create a account if not set,
# this code bypass this behavior
if vals.get("display_type") == "payment":
vals["default_account_id"] = True
if vals.get("display_type") == "cash":
vals["payment_debit_account_id"] = True
vals["payment_credit_account_id"] = True
super()._fill_missing_values(vals)
if vals.get("display_type") == "payment":
vals.pop("default_account_id")
# allow journal creation if display_type not define
if not vals.get("display_type"):
vals["display_type"] = vals["type"]
if vals.get("display_type") == "cash":
vals.pop("payment_debit_account_id")
vals.pop("payment_credit_account_id")

View File

@@ -0,0 +1 @@
from . import test_journal_display_type

View File

@@ -0,0 +1,81 @@
from odoo.tests import SavepointCase
from odoo.tests.common import new_test_user, users
class TestJournalDisplayType(SavepointCase):
def setUp(self):
super().setUp()
self.account = self.env["account.account"].create(
{
"name": "Test account",
"code": "TAC",
"user_type_id": self.env.ref("account.data_account_type_payable").id,
"reconcile": True,
}
)
self.manager = new_test_user(
self.env, "test_manager", "account.group_account_manager"
)
@users("test_manager")
def test_journal_payment_with_account(self):
journal = self.env["account.journal"].create(
{
"name": "Bank with account",
"display_type": "cash",
"type": "cash",
"code": "BK100",
"payment_debit_account_id": self.account.id,
"payment_credit_account_id": self.account.id,
}
)
journal.display_type = "payment"
self.assertEqual(journal.type, "bank")
self.assertEqual(journal.default_account_id, journal.payment_debit_account_id)
@users("test_manager")
def test_journal_payment_without_account(self):
journal = self.env["account.journal"].create(
{
"name": "Bank without account",
"display_type": "payment",
"type": "bank",
"code": "BK101",
}
)
self.assertTrue(journal.payment_debit_account_id)
self.assertTrue(journal.payment_credit_account_id)
self.assertEqual(journal.type, "bank")
self.assertEqual(journal.default_account_id, journal.payment_debit_account_id)
@users("test_manager")
def test_journal_cash_with_account(self):
journal = self.env["account.journal"].create(
{
"name": "Cash with account",
"display_type": "cash",
"type": "cash",
"code": "BK102",
"default_account_id": self.account.id,
}
)
self.assertEqual(journal.type, "cash")
self.assertEqual(journal.default_account_id, self.account)
self.assertEqual(journal.default_account_id, journal.payment_debit_account_id)
self.assertEqual(journal.default_account_id, journal.payment_credit_account_id)
@users("test_manager")
def test_journal_bank_without_account(self):
journal = self.env["account.journal"].create(
{
"name": "Bank without account",
"type": "bank",
"code": "BK103",
}
)
self.assertTrue(journal.payment_debit_account_id)

View File

@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<data>
<record id="view_account_journal_form" model="ir.ui.view">
<field name="model">account.journal</field>
<field name="inherit_id" ref="account.view_account_journal_form" />
<field name="priority">1</field>
<field name="arch" type="xml">
<field name="type" position="attributes">
<attribute name="attrs">{'invisible': True}</attribute>
</field>
<field name="type" position="after">
<field name="display_type" />
</field>
<xpath
expr="//page[@name='bank_account']/group/group[1]/field[@name='default_account_id'][1]"
position="attributes"
>
<attribute name="attrs">
{'required': [ ('type', '=', 'bank'), ('display_type', '=', 'bank')], 'invisible': [('display_type', '!=', 'bank')]}</attribute>
</xpath>
<xpath
expr="//page[@name='bank_account']/group/group[1]/field[@name='default_account_id'][2]"
position="attributes"
>
<attribute name="attrs">
{'required': [('type', '=', 'cash')], 'invisible': [('type', '!=', 'cash')]}
</attribute>
</xpath>
<xpath
expr="//page[@name='bank_account']/group/group[1]/field[@name='suspense_account_id'][1]"
position="attributes"
>
<attribute name="attrs">
{'required': [('display_type', 'in', ('bank', 'cash'))], 'invisible': [('display_type', 'not in', ('bank', 'cash'))]}</attribute>
</xpath>
<xpath
expr="//page[@name='bank_account']/group/group[2]"
position="attributes"
>
<attribute name="attrs">
{'invisible': ['|', ('type', '!=', 'bank'), ('display_type', '=', 'payment') ]}</attribute>
</xpath>
<xpath
expr="//page[@name='journal_entries']/group/group[1]/field[@name='payment_debit_account_id'][1]"
position="attributes"
>
<attribute name="attrs">
{'required': [('type', 'in', ('bank', 'cash'))], 'invisible': [('type', 'not in', ('bank', 'cash'))]}</attribute>
</xpath>
<xpath
expr="//page[@name='journal_entries']/group/group[2]/field[@name='payment_credit_account_id'][1]"
position="attributes"
>
<attribute name="attrs">
{'required': [('type', 'in', ('bank', 'cash'))], 'invisible': [('type', 'not in', ('bank', 'cash'))]}</attribute>
</xpath>
<xpath expr="//page[@name='journal_entries']" position="attributes">
<attribute name="attrs">
{'invisible': [('type', 'in', ['sale', 'purchase', 'general','cash'])]}</attribute>
</xpath>
</field>
</record>
</data>
</odoo>