[ADD] account_move_default_journal: A module to have more meaningful default journals in account move forms.

This commit is contained in:
Danny de Jong
2023-08-31 17:02:30 +02:00
committed by OCA-git-bot
parent 8a81a14f96
commit cc8c6fc193
12 changed files with 264 additions and 0 deletions

View File

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

View File

@@ -0,0 +1,15 @@
# Copyright 2021-2023 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
{
"name": "Default Journal",
"version": "14.0.1.0.0",
"author": "Therp BV,Odoo Community Association (OCA)",
"license": "AGPL-3",
"category": "Accounting & Finance",
"summary": "Configure a default journal for new account moves",
"website": "https://github.com/OCA/account-financial-tools",
"depends": [
"account",
],
"data": ["views/account_config_settings.xml", "views/res_company_views.xml"],
}

View File

@@ -0,0 +1,3 @@
from . import account_config_settings
from . import account_move
from . import res_company

View File

@@ -0,0 +1,23 @@
# Copyright 2021-2023 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo import fields, models
class ResConfigSettings(models.TransientModel):
_inherit = "res.config.settings"
account_move_default_general_journal_id = fields.Many2one(
"account.journal",
string="Default journal for new miscellaneous bookings",
related="company_id.default_general_journal_id",
)
account_move_default_sale_journal_id = fields.Many2one(
"account.journal",
string="Default journal for new sale bookings",
related="company_id.default_sale_journal_id",
)
account_move_default_purchase_journal_id = fields.Many2one(
"account.journal",
string="Default journal for new purchase bookings",
related="company_id.default_purchase_journal_id",
)

View File

@@ -0,0 +1,31 @@
# Copyright 2021-2023 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo import api, models
class AccountMove(models.Model):
_inherit = "account.move"
@api.model
def _search_default_journal(self, journal_types):
company_id = self._context.get("default_company_id", self.env.company.id)
company = self.env["res.company"].browse(company_id)
# Use the appropriate config parameter for the journal type
journal = None
if "sale" in journal_types:
journal = company.default_sale_journal_id
elif "purchase" in journal_types:
journal = company.default_purchase_journal_id
elif "general" in journal_types:
journal = company.default_general_journal_id
# Use the found journal, otherwise default back to normal behavior
if journal:
default_currency_id = self._context.get("default_currency_id")
if (
not default_currency_id
or journal.currency_id.id == default_currency_id.id
):
return journal
return super()._search_default_journal(journal_types)

View File

@@ -0,0 +1,17 @@
# Copyright 2023 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo import fields, models
class ResCompany(models.Model):
_inherit = "res.company"
default_general_journal_id = fields.Many2one(
"account.journal", string="Default journal for new general bookings"
)
default_sale_journal_id = fields.Many2one(
"account.journal", string="Default journal for new sale bookings"
)
default_purchase_journal_id = fields.Many2one(
"account.journal", string="Default journal for new purchase bookings"
)

View File

@@ -0,0 +1 @@
* Danny de Jong <ddejong@therp.nl>

View File

@@ -0,0 +1,3 @@
This module allows you to set defaults for journals, so that default values for
the journal field in forms can be configured. For one, this prevents users from
accidentally picking an old journal that gets usually offered as a default.

View File

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

View File

@@ -0,0 +1,79 @@
# Copyright 2023 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo.tests import TransactionCase
class TestDefaults(TransactionCase):
def setUp(self):
super().setUp()
self.different_currency = self.env["res.currency"].create(
{
"name": "Test Currency",
"symbol": "?",
}
)
self.company = self.env.ref("base.main_company")
# This journal gets selected before the 'General Journal' because of its code:
self.journal_first = self.env["account.journal"].create(
{
"name": "First Journal",
"code": "1st",
"type": "general",
"company_id": self.company.id,
}
)
self.journal_general = self.env["account.journal"].create(
{
"name": "General Journal",
"code": "GJ",
"type": "general",
"company_id": self.company.id,
"currency_id": self.different_currency.id,
}
)
self.journal_sale = self.env["account.journal"].create(
{
"name": "Sale Journal",
"code": "SJ",
"type": "sale",
"company_id": self.company.id,
}
)
self.journal_purchase = self.env["account.journal"].create(
{
"name": "Purchase Journal",
"code": "PJ",
"type": "purchase",
"company_id": self.company.id,
}
)
def test_defaults(self):
# Test whether the first journal gets selected with no default set
move = self.env["account.move"].create({"move_type": "entry"})
self.assertNotEqual(move.journal_id, self.journal_general)
# Test whether the journal with the relevant currency is chosen
move = (
self.env["account.move"]
.with_context({"default_currency_id": self.different_currency.id})
.create({"move_type": "entry"})
)
self.assertEqual(move.journal_id, self.journal_general)
move = (
self.env["account.move"]
.with_context({"default_currency_id": self.different_currency.id})
.create({"move_type": "out_invoice"})
)
self.assertNotEqual(move.journal_id, self.journal_general)
# Test whether the correct default journals are chosen
self.company.default_general_journal_id = self.journal_general
self.company.default_sale_journal_id = self.journal_sale
self.company.default_purchase_journal_id = self.journal_purchase
move = self.env["account.move"].create({"move_type": "entry"})
self.assertEqual(move.journal_id, self.journal_general)
move = self.env["account.move"].create({"move_type": "out_invoice"})
self.assertEqual(move.journal_id, self.journal_sale)
move = self.env["account.move"].create({"move_type": "in_invoice"})
self.assertEqual(move.journal_id, self.journal_purchase)

View File

@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<record id="res_config_settings_view_form" model="ir.ui.view">
<field name="name">res.config.settings.view.form.inherit.journals</field>
<field name="model">res.config.settings</field>
<field name="inherit_id" ref="account.res_config_settings_view_form" />
<field name="arch" type="xml">
<xpath expr="//div[@data-key='account']" position="inside">
<h2>Journals</h2>
<div
class="row mt16 o_settings_container"
id="journals_setting_container"
>
<div class="col-12 col-lg-6 o_setting_box">
<div class="o_setting_left_pane" />
<div class="o_setting_left_pane" />
<div class="o_setting_right_pane">
<span class="o_form_label">Default Journals</span>
<div class="text-muted">
Default journals for new bookings and invoices.
</div>
<div class="content-group">
<div class="row mt16">
<label
string="General Journal"
for="account_move_default_general_journal_id"
class="col-lg-3 o_light_label"
/>
<field
name="account_move_default_general_journal_id"
domain="[('type', '=', 'general')]"
/>
</div>
<div class="row mt16">
<label
string="Sale Journal"
for="account_move_default_sale_journal_id"
class="col-lg-3 o_light_label"
/>
<field
name="account_move_default_sale_journal_id"
domain="[('type', '=', 'sale')]"
/>
</div>
<div class="row mt16">
<label
string="Purchase Journal"
for="account_move_default_purchase_journal_id"
class="col-lg-3 o_light_label"
/>
<field
name="account_move_default_purchase_journal_id"
domain="[('type', '=', 'purchase')]"
/>
</div>
</div>
</div>
</div>
</div>
</xpath>
</field>
</record>
</odoo>

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<record id="view_company_form" model="ir.ui.view">
<field name="name">res.company.form</field>
<field name="model">res.company</field>
<field name="inherit_id" ref="base.view_company_form" />
<field name="arch" type="xml">
<xpath expr="//page[@name='general_info']/group/group" position="inside">
<field
name="default_general_journal_id"
string="General Journal"
domain="[('type', '=', 'general')]"
/>
<field
name="default_sale_journal_id"
string="Sale Journal"
domain="[('type', '=', 'sale')]"
/>
<field
name="default_purchase_journal_id"
string="Purchase Journal"
domain="[('type', '=', 'purchase')]"
/>
</xpath>
</field>
</record>
</odoo>