Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[14.0][MIG] contract_operating_unit #382

Merged
merged 11 commits into from
Jun 18, 2021
82 changes: 82 additions & 0 deletions contract_operating_unit/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
=======================
Contract Operating Unit
=======================

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
:alt: License: LGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Foperating--unit-lightgray.png?logo=github
:target: https://github.com/OCA/operating-unit/tree/12.0/contract_operating_unit
:alt: OCA/operating-unit
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/operating-unit-12-0/operating-unit-12-0-contract_operating_unit
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/213/12.0
:alt: Try me on Runbot

|badge1| |badge2| |badge3| |badge4| |badge5|

This module introduces the following features:

* Adds the Operating Unit (OU) to Contracts.

* Security rules are defined to ensure that users can only see the Contracts of that Operating Units in which they are allowed access to.

**Table of contents**

.. contents::
:local:

Usage
=====

Assign contracts to specific operating units within the company

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/operating-unit/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/operating-unit/issues/new?body=module:%20contract_operating_unit%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* ForgeFlow S.L.

Contributors
~~~~~~~~~~~~

* Aaron Henriquez <ahenriquez@forgeflow.com>

Maintainers
~~~~~~~~~~~

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/operating-unit <https://github.com/OCA/operating-unit/tree/12.0/contract_operating_unit>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
4 changes: 4 additions & 0 deletions contract_operating_unit/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from . import models
from . import tests
14 changes: 14 additions & 0 deletions contract_operating_unit/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2020 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

{
"name": "Contract Operating Unit",
"version": "14.0.1.0.0",
"license": "LGPL-3",
"author": "ForgeFlow S.L., Odoo Community Association (OCA)",
"website": "https://github.com/OCA/operating-unit",
"category": "Invoicing",
"depends": ["contract", "account_operating_unit"],
"data": ["views/contract_view.xml", "security/contract_security.xml"],
"installable": True,
}
1 change: 1 addition & 0 deletions contract_operating_unit/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import contract
25 changes: 25 additions & 0 deletions contract_operating_unit/models/contract.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2020 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from odoo import fields, models


class ContractContract(models.Model):

_inherit = "contract.contract"

operating_unit_id = fields.Many2one(
"operating.unit",
"Operating Unit",
default=lambda self: self.env["res.users"].operating_unit_default_get(
self._uid
),
)

def _prepare_invoice(self, date_invoice, journal=None):
(invoice_vals, move_form) = super()._prepare_invoice(
date_invoice, journal=journal
)
if self.operating_unit_id:
invoice_vals["operating_unit_id"] = self.operating_unit_id.id
return (invoice_vals, move_form)
2 changes: 2 additions & 0 deletions contract_operating_unit/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Aaron Henriquez <ahenriquez@forgeflow.com>
* Kitti U. <kittiu@ecosoft.co.th>
5 changes: 5 additions & 0 deletions contract_operating_unit/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
This module introduces the following features:

* Adds the Operating Unit (OU) to Contracts.

* Security rules are defined to ensure that users can only see the Contracts of that Operating Units in which they are allowed access to.
1 change: 1 addition & 0 deletions contract_operating_unit/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Assign contracts to specific operating units within the company
17 changes: 17 additions & 0 deletions contract_operating_unit/security/contract_security.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>

<record id="ir_rule_contract_allowed_operating_units" model="ir.rule">
<field name="model_id" ref="contract.model_contract_contract" />
<field
name="domain_force"
>['|',('operating_unit_id','=',False),('operating_unit_id','in',[g.id for g in user.operating_unit_ids])]</field>
<field name="name">Contracts from allowed operating units</field>
<field name="global" eval="True" />
<field eval="0" name="perm_unlink" />
<field eval="0" name="perm_write" />
<field eval="1" name="perm_read" />
<field eval="0" name="perm_create" />
</record>

</odoo>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions contract_operating_unit/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_contract_operating_unit
97 changes: 97 additions & 0 deletions contract_operating_unit/tests/test_contract_operating_unit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Copyright 2020 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from odoo.tests import common


class TestContractOperatingUnit(common.TransactionCase):
def setUp(self):
super(TestContractOperatingUnit, self).setUp()

self.res_users_model = self.env["res.users"]
self.contract_model = self.env["contract.contract"]

self.company = self.env.ref("base.main_company")
self.grp_contract_manager = self.env.ref("account.group_account_manager")
self.group_user = self.env.ref("base.group_user")

# Main Operating Unit
self.ou1 = self.env.ref("operating_unit.main_operating_unit")
# B2C Operating Unit
self.b2c = self.env.ref("operating_unit.b2c_operating_unit")

# Create Users
self.user1 = self._create_user(
"User_1",
[self.grp_contract_manager, self.group_user],
self.company,
[self.ou1, self.b2c],
)
self.user2 = self._create_user(
"User_2",
[self.grp_contract_manager, self.group_user],
self.company,
[self.b2c],
)

self.partner = self.env["res.partner"].create(
{
"name": "Test contract partner",
}
)

self.contract1 = (
self.env["contract.contract"]
.with_user(self.user1.id)
.create(
{
"name": "Maintenance of Servers",
"partner_id": self.partner.id,
"operating_unit_id": self.ou1.id,
}
)
)
self.contract2 = (
self.env["contract.contract"]
.with_user(self.user2.id)
.create(
{
"name": "Maintenance of Servers",
"partner_id": self.partner.id,
"operating_unit_id": self.b2c.id,
}
)
)

def _create_user(self, login, groups, company, operating_units, context=None):
"""Creates a user."""
group_ids = [group.id for group in groups]
user = self.res_users_model.create(
{
"name": "Test Contract User",
"login": login,
"password": "demo",
"email": "example@yourcompany.com",
"company_id": company.id,
"company_ids": [(4, company.id)],
"operating_unit_ids": [(4, ou.id) for ou in operating_units],
"groups_id": [(6, 0, group_ids)],
}
)
return user

def test_contract_ou(self):
"""Test Contract Operating Unit"""
# User 2 is only assigned to Operating Unit B2C, and cannot
# Access Contract records of Main Operating Unit.
record = self.contract_model.with_user(self.user2.id).search(
[
("id", "=", self.contract1.id),
("operating_unit_id", "=", self.ou1.id),
]
)
self.assertEqual(
record.ids,
[],
"User 2 should not have access to " "OU %s" % self.ou1.name,
)
74 changes: 74 additions & 0 deletions contract_operating_unit/views/contract_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>

<!--FORM view-->
<record id="contract_contract_customer_form_view" model="ir.ui.view">
<field
name="name"
>contract.contract form view (in contract_payment_mode)</field>
<field name="model">contract.contract</field>
<field name="inherit_id" ref="contract.contract_contract_customer_form_view" />
<field name="arch" type="xml">
<field name="tag_ids" position="after">
<field
name="operating_unit_id"
groups="operating_unit.group_multi_operating_unit"
/>
</field>
</field>
</record>

<!--TREE view-->
<record id="contract_contract_tree_view" model="ir.ui.view">
<field
name="name"
>contract.contract tree view (in contract_payment_mode)</field>
<field name="model">contract.contract</field>
<field name="inherit_id" ref="contract.contract_contract_tree_view" />
<field name="arch" type="xml">
<field name="partner_id" position="after">
<field
name="operating_unit_id"
groups="operating_unit.group_multi_operating_unit"
/>
</field>
</field>
</record>

<!--FORM view-->
<record id="contract_contract_search_view" model="ir.ui.view">
<field
name="name"
>contract.contract search view (in contract_payment_mode)</field>
<field name="model">contract.contract</field>
<field name="inherit_id" ref="contract.contract_contract_search_view" />
<field name="arch" type="xml">
<field name="name" position="after">
<field
name="operating_unit_id"
groups="operating_unit.group_multi_operating_unit"
/>
</field>
</field>
</record>


<!--Supplier FORM view-->
<record id="contract_contract_supplier_form_view" model="ir.ui.view">
<field
name="name"
>contract.contract supplier form view (in contract_payment_mode)</field>
<field name="model">contract.contract</field>
<field name="priority">18</field>
<field name="inherit_id" ref="contract.contract_contract_supplier_form_view" />
<field name="arch" type="xml">
<field name="tag_ids" position="after">
<field
name="operating_unit_id"
groups="operating_unit.group_multi_operating_unit"
/>
</field>
</field>
</record>

</odoo>
6 changes: 6 additions & 0 deletions setup/contract_operating_unit/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)