Skip to content

Commit

Permalink
add account dms field
Browse files Browse the repository at this point in the history
  • Loading branch information
agent-z28 committed Sep 30, 2024
1 parent 554890a commit 4f54e69
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 0 deletions.
1 change: 1 addition & 0 deletions account_dms_field/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
12 changes: 12 additions & 0 deletions account_dms_field/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "Add dms field for account",
"version": "16.0.1.0.0",
"category": "Accounting/Accounting",
"website": "https://www.agenterp.com",
"author": "Agent ERP GmbH",
"depends": ["account", "dms_field"],
"data": ["views/account_move_view.xml"],
"demo": ["demo/account_dms_data.xml"],
"installable": True,
"license": "LGPL-3",
}
25 changes: 25 additions & 0 deletions account_dms_field/demo/account_dms_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<!-- Created a DMS access group. #Issue-249 -->
<record id="read_access_account_move_group" model="dms.access.group">
<field name="name">Everyone for Account DMS</field>
<field name="group_ids" eval="[(4, ref('account.group_account_invoice'))]" />
<field name="perm_create" eval="True" />
<field name="perm_write" eval="True" />
<field name="perm_unlink" eval="True" />
</record>

<!-- Created a DMS field template for account. #Issue-249 -->
<record id="field_template_account" model="dms.field.template">
<field name="name">Account</field>
<field name="storage_id" ref="dms.storage_demo" />
<field name="model_id" ref="account.model_account_move" />
<field name="user_field_id" ref="account.field_account_move__user_id" />
<field name="group_ids" eval="[(4, ref('read_access_account_move_group'))]" />
</record>

<!-- Created a account DMS directory. #Issue-249 -->
<function model="dms.field.template" name="_create_dms_directory">
<value eval="[ref('field_template_account')]" />
</function>
</odoo>
3 changes: 3 additions & 0 deletions account_dms_field/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import dms_field_template
from . import account_move
from . import dms_directory
6 changes: 6 additions & 0 deletions account_dms_field/models/account_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from odoo import models


class AccountMove(models.Model):
_name = "account.move"
_inherit = ["account.move", "dms.field.mixin"]
37 changes: 37 additions & 0 deletions account_dms_field/models/dms_directory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from odoo import _, api, models
from odoo.exceptions import ValidationError

from odoo.addons.dms.tools.file import check_name


class DmsDirectory(models.Model):
_inherit = "dms.directory"

@api.constrains("name")
def _check_name(self):
"""Override a method to update the record name for the account.move model.
#Issue-249"""
for record in self:
# BAD customization start. #Issue-249
name = record.name
# Updated the name of account move record due to an issue caused by a (/)
# in the record name when opening the temporary directory created in the
# check_name method. #Issue-249
if record.res_model == "account.move":
name = name.replace("/", "-")
# BAD customization end. #Issue-249
if self.env.context.get("check_name", True) and not check_name(name):
raise ValidationError(_("The directory name is invalid."))
if record.is_root_directory:
childs = record.sudo().storage_id.root_directory_ids.name_get()
else:
childs = record.sudo().parent_id.child_directory_ids.name_get()
if list(
filter(
lambda child: child[1] == record.name and child[0] != record.id,
childs,
)
):
raise ValidationError(
_("A directory with the same name already exists.")
)
14 changes: 14 additions & 0 deletions account_dms_field/models/dms_field_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from odoo import models


class DmsFieldTemplate(models.Model):
_inherit = "dms.field.template"

def _create_dms_directory(self):
"""New method to define the directory automatically in demo data avoiding
error if it already exists."""
self.ensure_one()
if not self.dms_directory_ids:
self.with_context(
res_model=self._name, res_id=self.id
).create_dms_directory()
1 change: 1 addition & 0 deletions account_dms_field/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_account_dms_field
59 changes: 59 additions & 0 deletions account_dms_field/tests/test_account_dms_field.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from odoo.addons.base.tests.common import TransactionCase


class TestAccountDmsField(TransactionCase):
def setUp(self):
"""Inherited method to create records. #Issue-249"""
super(TestAccountDmsField, self).setUp()
self.template = self.env.ref("account_dms_field.field_template_account")
self.storage = self.template.storage_id
self.access_group = self.template.group_ids
self.account_model = self.env["account.move"]
self.partner = self.env.ref("base.res_partner_12")
self.test_directory = self.env["dms.directory"].create(
{
"name": "Test Directory",
"parent_id": self.template.dms_directory_ids[0].id,
"storage_id": self.template.storage_id.id,
}
)

def test_01_account_document_directory(self):
"""New method to test account order documents field value. #Issue-249"""
template_ctx = self.template.with_context(
res_model=self.template._name, res_id=self.template.id
)
template_ctx._create_dms_directory()
account_move = self.account_model.create(
{
"partner_id": self.partner.id,
}
)
account_move.invalidate_model()
directory = account_move.dms_directory_ids
# Assert that only one directory is created for the account move. #Issue-249
self.assertEqual(len(directory), 1, "Directory length must be 1.")
# Assert that the storage associated with the directory is the same as the
# template's storage. #Issue-249
self.assertEqual(
directory.storage_id,
self.storage,
"Account move directory storage is different from the template storage.",
)
# Assert that the custom access group is present in the directory's group
# list. #Issue-249
self.assertIn(
self.access_group,
directory.group_ids,
"Account move directory groups are different from the template groups.",
)
# Map the names of child directories related to the account move directory.
# Issue-249
child_directory_names = directory.mapped("child_directory_ids.name")
# Assert that a specific child directory, "Test Directory", exists. #Issue-249
self.assertIn(
"Test Directory",
child_directory_names,
"Test Directory is not in the child directory of the account move "
"directory.",
)
17 changes: 17 additions & 0 deletions account_dms_field/views/account_move_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<!-- Inherited form view to add new page. #Issue-249 -->
<record id="view_move_form_inherit_account_dms_field" model="ir.ui.view">
<field name="name">view.move.form.inherit.account.dms.field</field>
<field name="model">account.move</field>
<field name="inherit_id" ref="account.view_move_form" />
<field name="arch" type="xml">
<!-- Added a new document page. #Issue-249 -->
<xpath expr="//notebook" position="inside">
<page name="documents" string="Documents" attrs="{'invisible': [('id', '=', False)]}">
<field name="dms_directory_ids" mode="dms_list" />
</page>
</xpath>
</field>
</record>
</odoo>

0 comments on commit 4f54e69

Please sign in to comment.