Skip to content

Commit c89f1b7

Browse files
author
matthieu.saison
committed
[14.0][ADD] new module account_journal_display_type
1 parent f2172a5 commit c89f1b7

File tree

8 files changed

+261
-0
lines changed

8 files changed

+261
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl).
2+
3+
from . import models
4+
from .hook import post_init_hook
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2024 Akretion (https://www.akretion.com).
2+
# @author Matthieu SAISON <matthieu.saison@akretion.com>
3+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
4+
{
5+
"name": "Account Journal Display Type",
6+
"summary": "Account Journal type for payment",
7+
"version": "14.0.1.0.0",
8+
"development_status": "Beta",
9+
"category": "Accounting",
10+
"website": "https://github.com/OCA/account-financial-tools",
11+
"author": "Akretion, " "Odoo Community Association (OCA)",
12+
"maintainers": ["matthieu_saison"],
13+
"license": "AGPL-3",
14+
"depends": ["account", "account_statement_completion_label_simple"],
15+
"data": ["views/account_journal_view.xml"],
16+
"post_init_hook": "post_init_hook",
17+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from odoo import SUPERUSER_ID, api
2+
3+
4+
def post_init_hook(cr, registry):
5+
env = api.Environment(cr, SUPERUSER_ID, {})
6+
journal_ids = env["account.journal"].search([])
7+
for journal in journal_ids:
8+
journal.display_type = journal.type
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import account_journal
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
from odoo import api, fields, models
2+
3+
4+
class AccountJournal(models.Model):
5+
_inherit = "account.journal"
6+
7+
display_type = fields.Selection(
8+
[
9+
("sale", "Sales"),
10+
("purchase", "Purchase"),
11+
("cash", "Cash"),
12+
("bank", "Bank"),
13+
("payment", "Payment"),
14+
("general", "Miscellaneous"),
15+
],
16+
required=True,
17+
inverse="_inverse_type",
18+
help="Select 'Sale' for customer invoices journals.\n"
19+
"Select 'Purchase' for vendor bills journals.\n"
20+
"Select 'Cash' or 'Bank' for journals that are used in customer or vendor payments.\n"
21+
"Select 'General' for miscellaneous operations journals.",
22+
)
23+
type = fields.Selection(compute="_compute_type", store=True)
24+
default_account_id = fields.Many2one(
25+
comodel_name="account.account",
26+
compute="_compute_default_account_id",
27+
readonly=False,
28+
store=True,
29+
)
30+
payment_debit_account_id = fields.Many2one(
31+
comodel_name="account.account",
32+
compute="_compute_payment_debit_account_id",
33+
readonly=False,
34+
store=True,
35+
)
36+
37+
@api.depends("display_type")
38+
def _compute_type(self):
39+
for record in self:
40+
if record.display_type == "payment":
41+
record.type = "bank"
42+
else:
43+
record.type = record.display_type
44+
45+
@api.depends("display_type", "payment_debit_account_id")
46+
def _compute_default_account_id(self):
47+
for record in self:
48+
if record.display_type == "payment":
49+
record.default_account_id = record.payment_debit_account_id
50+
51+
@api.depends("display_type", "default_account_id")
52+
def _compute_payment_debit_account_id(self):
53+
for record in self:
54+
if record.type == "cash":
55+
record.payment_debit_account_id = record.default_account_id
56+
record.payment_credit_account_id = record.default_account_id
57+
58+
@api.model
59+
def _fill_missing_values(self, vals):
60+
# _fill_missing_values automaticly create a account if not set,
61+
# this code bypass this behavior
62+
if vals.get("display_type") == "payment":
63+
vals["default_account_id"] = True
64+
if vals.get("display_type") == "cash":
65+
vals["payment_debit_account_id"] = True
66+
vals["payment_credit_account_id"] = True
67+
super()._fill_missing_values(vals)
68+
if vals.get("display_type") == "payment":
69+
vals.pop("default_account_id")
70+
# allow journal creation if display_type not define
71+
if not vals.get("display_type"):
72+
vals["display_type"] = vals["type"]
73+
if vals.get("display_type") == "cash":
74+
vals.pop("payment_debit_account_id")
75+
vals.pop("payment_credit_account_id")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import test_journal_display_type
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
from odoo.tests import SavepointCase
2+
from odoo.tests.common import new_test_user, users
3+
4+
5+
class TestJournalDisplayType(SavepointCase):
6+
def setUp(self):
7+
super().setUp()
8+
9+
self.account = self.env["account.account"].create(
10+
{
11+
"name": "Test account",
12+
"code": "TAC",
13+
"user_type_id": self.env.ref("account.data_account_type_payable").id,
14+
"reconcile": True,
15+
}
16+
)
17+
self.manager = new_test_user(
18+
self.env, "test_manager", "account.group_account_manager"
19+
)
20+
21+
@users("test_manager")
22+
def test_journal_payment_with_account(self):
23+
24+
journal = self.env["account.journal"].create(
25+
{
26+
"name": "Bank with account",
27+
"display_type": "cash",
28+
"type": "cash",
29+
"code": "BK100",
30+
"payment_debit_account_id": self.account.id,
31+
"payment_credit_account_id": self.account.id,
32+
}
33+
)
34+
journal.display_type = "payment"
35+
self.assertEqual(journal.type, "bank")
36+
self.assertEqual(journal.default_account_id, journal.payment_debit_account_id)
37+
38+
@users("test_manager")
39+
def test_journal_payment_without_account(self):
40+
41+
journal = self.env["account.journal"].create(
42+
{
43+
"name": "Bank without account",
44+
"display_type": "payment",
45+
"type": "bank",
46+
"code": "BK101",
47+
}
48+
)
49+
self.assertTrue(journal.payment_debit_account_id)
50+
self.assertTrue(journal.payment_credit_account_id)
51+
self.assertEqual(journal.type, "bank")
52+
self.assertEqual(journal.default_account_id, journal.payment_debit_account_id)
53+
54+
@users("test_manager")
55+
def test_journal_cash_with_account(self):
56+
57+
journal = self.env["account.journal"].create(
58+
{
59+
"name": "Cash with account",
60+
"display_type": "cash",
61+
"type": "cash",
62+
"code": "BK102",
63+
"default_account_id": self.account.id,
64+
}
65+
)
66+
self.assertEqual(journal.type, "cash")
67+
self.assertEqual(journal.default_account_id, self.account)
68+
self.assertEqual(journal.default_account_id, journal.payment_debit_account_id)
69+
self.assertEqual(journal.default_account_id, journal.payment_credit_account_id)
70+
71+
@users("test_manager")
72+
def test_journal_bank_without_account(self):
73+
74+
journal = self.env["account.journal"].create(
75+
{
76+
"name": "Bank without account",
77+
"type": "bank",
78+
"code": "BK103",
79+
}
80+
)
81+
self.assertTrue(journal.payment_debit_account_id)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<odoo>
3+
<data>
4+
<record id="view_account_journal_form" model="ir.ui.view">
5+
<field name="model">account.journal</field>
6+
<field name="inherit_id" ref="account.view_account_journal_form" />
7+
<field name="priority">1</field>
8+
<field name="arch" type="xml">
9+
<field name="type" position="attributes">
10+
<attribute name="attrs">{'invisible': True}</attribute>
11+
</field>
12+
<field name="type" position="after">
13+
<field name="display_type" />
14+
</field>
15+
16+
<xpath
17+
expr="//page[@name='bank_account']/group/group[1]/field[@name='default_account_id'][1]"
18+
position="attributes"
19+
>
20+
<attribute name="attrs">
21+
{'required': [ ('type', '=', 'bank'), ('display_type', '=', 'bank')], 'invisible': [('display_type', '!=', 'bank')]}</attribute>
22+
</xpath>
23+
24+
<xpath
25+
expr="//page[@name='bank_account']/group/group[1]/field[@name='default_account_id'][2]"
26+
position="attributes"
27+
>
28+
<attribute name="attrs">
29+
{'required': [('type', '=', 'cash')], 'invisible': [('type', '!=', 'cash')]}
30+
</attribute>
31+
</xpath>
32+
33+
<xpath
34+
expr="//page[@name='bank_account']/group/group[1]/field[@name='suspense_account_id'][1]"
35+
position="attributes"
36+
>
37+
<attribute name="attrs">
38+
{'required': [('display_type', 'in', ('bank', 'cash'))], 'invisible': [('display_type', 'not in', ('bank', 'cash'))]}</attribute>
39+
</xpath>
40+
41+
<xpath
42+
expr="//page[@name='bank_account']/group/group[2]"
43+
position="attributes"
44+
>
45+
<attribute name="attrs">
46+
{'invisible': ['|', ('type', '!=', 'bank'), ('display_type', '=', 'payment') ]}</attribute>
47+
</xpath>
48+
49+
<xpath
50+
expr="//page[@name='journal_entries']/group/group[1]/field[@name='payment_debit_account_id'][1]"
51+
position="attributes"
52+
>
53+
<attribute name="attrs">
54+
{'required': [('type', 'in', ('bank', 'cash'))], 'invisible': [('type', 'not in', ('bank', 'cash'))]}</attribute>
55+
</xpath>
56+
57+
<xpath
58+
expr="//page[@name='journal_entries']/group/group[2]/field[@name='payment_credit_account_id'][1]"
59+
position="attributes"
60+
>
61+
<attribute name="attrs">
62+
{'required': [('type', 'in', ('bank', 'cash'))], 'invisible': [('type', 'not in', ('bank', 'cash'))]}</attribute>
63+
</xpath>
64+
65+
<xpath expr="//page[@name='journal_entries']" position="attributes">
66+
<attribute name="attrs">
67+
{'invisible': [('type', 'in', ['sale', 'purchase', 'general','cash'])]}</attribute>
68+
</xpath>
69+
70+
71+
</field>
72+
</record>
73+
</data>
74+
</odoo>

0 commit comments

Comments
 (0)