diff --git a/analytic_account_partition/security/ir.model.access.csv b/analytic_account_partition/security/ir.model.access.csv
index 2a172758..fe01a4ce 100644
--- a/analytic_account_partition/security/ir.model.access.csv
+++ b/analytic_account_partition/security/ir.model.access.csv
@@ -1,5 +1,5 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
-access_analytic_partition,access_analytic_partition,model_analytic_partition,account.group_account_invoice,1,1,1,0
-access_analytic_partition_manager,access_analytic_partition,model_analytic_partition,account.group_account_manager,1,1,1,1
-access_analytic_partition_line,access_analytic_partition_line,model_analytic_partition_line,account.group_account_invoice,1,1,1,0
-access_analytic_partition_line_manager,access_analytic_partition_line,model_analytic_partition_line,account.group_account_manager,1,1,1,1
+access_analytic_partition,access_analytic_partition,model_analytic_partition,account.group_account_invoice,1,1,0,0
+access_analytic_partition_manager,access_analytic_partition,model_analytic_partition,account.group_account_user,1,1,1,1
+access_analytic_partition_line,access_analytic_partition_line,model_analytic_partition_line,account.group_account_invoice,1,1,0,0
+access_analytic_partition_line_manager,access_analytic_partition_line,model_analytic_partition_line,account.group_account_user,1,1,1,1
diff --git a/analytic_partition_by_employee/models/__init__.py b/analytic_partition_by_employee/models/__init__.py
index 57099485..870faaf2 100644
--- a/analytic_partition_by_employee/models/__init__.py
+++ b/analytic_partition_by_employee/models/__init__.py
@@ -9,3 +9,4 @@
from . import account_voucher
from . import analytic_account
from . import account_invoice
+from . import res_company
diff --git a/analytic_partition_by_employee/models/analytic_account.py b/analytic_partition_by_employee/models/analytic_account.py
index 1213075e..a265f878 100644
--- a/analytic_partition_by_employee/models/analytic_account.py
+++ b/analytic_partition_by_employee/models/analytic_account.py
@@ -8,5 +8,5 @@
class AccountAnalyticAccount(models.Model):
_inherit = 'account.analytic.account'
- expense_group_id = fields.Many2one(
- 'expense.group', 'Grupo de Despesas', store=True)
+ department_id = fields.Many2one(
+ 'hr.department', 'Departamento', store=True)
diff --git a/analytic_partition_by_employee/models/hr_employee.py b/analytic_partition_by_employee/models/hr_employee.py
index f0e688cd..80f4b89d 100644
--- a/analytic_partition_by_employee/models/hr_employee.py
+++ b/analytic_partition_by_employee/models/hr_employee.py
@@ -3,6 +3,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import fields, models, api
+from odoo.exceptions import UserError
class Employee(models.Model):
@@ -35,14 +36,28 @@ def write(self, vals):
res = super(Employee, self).write(vals)
if vals.get('employee_partition_ids') or vals.get('active'):
self.compute_percent_per_employe()
+ self.check_employee_partition_ids()
return res
@api.model
def create(self, vals):
res = super(Employee, self).create(vals)
res.compute_percent_per_employe()
+ res.check_employee_partition_ids()
return res
+ def check_employee_partition_ids(self):
+ if not (self.company_id and self.department_id and
+ self.employee_partition_ids):
+ return
+ analytic_acc = self.env['account.analytic.account'].search([
+ ('partner_id', '=', self.company_id.partner_id.id),
+ ('department_id', '=', self.department_id.id)])
+ if analytic_acc and analytic_acc not in self.employee_partition_ids.\
+ mapped('analytic_account_id'):
+ raise UserError("É necessário ao menos uma linha no controle\
+de rateio correspondente à empresa e departamento deste funcionário!")
+
class HrEmployeePartition(models.Model):
_name = 'hr.employee.partition'
diff --git a/analytic_partition_by_employee/models/res_company.py b/analytic_partition_by_employee/models/res_company.py
new file mode 100644
index 00000000..c6019969
--- /dev/null
+++ b/analytic_partition_by_employee/models/res_company.py
@@ -0,0 +1,16 @@
+# -*- coding: utf-8 -*-
+# © 2018 Trustcode
+# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
+
+from odoo import models, api
+
+
+class ResCompany(models.Model):
+ _inherit = 'res.company'
+
+ @api.model
+ def create(self, vals):
+ res = super(ResCompany, self).create(vals)
+ if res.parent_id:
+ res.partner_id.is_branch = True
+ return res
diff --git a/analytic_partition_by_employee/models/res_partner.py b/analytic_partition_by_employee/models/res_partner.py
index d1f501a3..847d21e6 100644
--- a/analytic_partition_by_employee/models/res_partner.py
+++ b/analytic_partition_by_employee/models/res_partner.py
@@ -9,25 +9,23 @@ class ResPartner(models.Model):
_inherit = 'res.partner'
is_branch = fields.Boolean('É Filial')
- expense_group_ids = fields.Many2many(
- 'expense.group', string="Grupo de contas")
+ department_ids = fields.Many2many(
+ 'hr.department', string="Departamentos")
- def create_partition_group(self, analytic_accs):
+ def create_partition_group(self, analytic_acc):
part_group = self.env['analytic.partition'].create({
- 'name': 'Escritório ' + self.name,
- 'partition_line_ids': [(0, 0, {
- 'analytic_account_id': acc.id,
- 'partition_percent': 0
- }) for acc in analytic_accs[1:]]
- })
- analytic_accs[0].update({
- 'partition_id': part_group.id,
+ 'name': 'Rateio ' + self.name,
})
+ analytic_acc.partition_id = part_group
return part_group
def create_partition_lines(self, partition_id, analytic_accounts):
accounts = []
+ accounts_in_group = partition_id.partition_line_ids.mapped(
+ 'analytic_account_id')
for acc in analytic_accounts:
+ if acc in accounts_in_group:
+ continue
accounts.append(self.env['analytic.partition.line'].create({
'partition_id': partition_id.id,
'analytic_account_id': acc.id,
@@ -35,11 +33,11 @@ def create_partition_lines(self, partition_id, analytic_accounts):
}))
return accounts
- def deactivate_analytic_account(self, groups):
- for group in groups:
+ def deactivate_analytic_account(self, departments):
+ for dep in departments:
analytic_acc = self.env['account.analytic.account'].search([
('partner_id', '=', self.id),
- ('expense_group_id', '=', group.id)
+ ('department_id', '=', dep.id)
], limit=1)
if analytic_acc and analytic_acc.active:
analytic_acc.toggle_active()
@@ -51,6 +49,7 @@ def change_partition_account(self, analytic_acc):
partition_id = analytic_acc.partition_id
if not any(item.analytic_account_id.active for item in
partition_id.partition_line_ids):
+ partition_id.unlink()
return
analytic_acc.write({
'partition_id': False
@@ -62,65 +61,68 @@ def change_partition_account(self, analytic_acc):
line.unlink()
break
- def create_analytic_account(self, groups, create_group=False):
+ def create_analytic_accounts(self, departments):
analytic_accs = []
- for group in groups:
+ for dep in departments:
analytic_acc = self.env['account.analytic.account'].search([
('partner_id', '=', self.id),
- ('expense_group_id', '=', group.id),
+ ('department_id', '=', dep.id),
('active', '=', False)
])
if analytic_acc and not analytic_acc.active:
analytic_acc.toggle_active()
else:
- analytic_accs.append(
- self.env['account.analytic.account'].create({
- 'name': group.name,
- 'partner_id': self.id,
- 'expense_group_id': group.id,
- }))
- if create_group:
- part_group = self.create_partition_group(analytic_accs)
- else:
- part_group = self.env['account.analytic.account'].search([
- ('partner_id', '=', self.id),
- ('partition_id', '!=', False)], limit=1).partition_id
- self.create_partition_lines(part_group, analytic_accs)
+ analytic_acc = self.env['account.analytic.account'].create({
+ 'name': dep.name,
+ 'partner_id': self.id,
+ 'department_id': dep.id,
+ })
+ analytic_accs.append(analytic_acc)
+ part_group = self.env['account.analytic.account'].search([
+ ('partner_id', '=', self.id),
+ ('partition_id', '!=', False),
+ '|',
+ ('active', '=', False),
+ ('active', '=', True)], limit=1).partition_id
+ if not part_group:
+ part_group = self.create_partition_group(analytic_accs[0])
+ analytic_accs = analytic_accs[1:]
+ self.create_partition_lines(part_group, analytic_accs)
matrix_partition = self.env.ref(
"analytic_partition_by_employee.matrix_partition_group")
self.create_partition_lines(matrix_partition, analytic_accs)
return analytic_accs
- def _update_analytic_accounts(self, groups):
- groups_to_remove = [item for item in self.expense_group_ids]
- groups_to_create = [item for item in groups]
- for item in self.expense_group_ids:
- for group in groups:
- if item == group:
- groups_to_remove.remove(item)
- groups_to_create.remove(group)
+ def _update_analytic_accounts(self, departments):
+ departments_to_remove = [item for item in self.department_ids]
+ departments_to_create = [item for item in departments]
+ for item in self.department_ids:
+ for dep in departments:
+ if item == dep:
+ departments_to_remove.remove(item)
+ departments_to_create.remove(dep)
break
- if groups_to_create:
- self.create_analytic_account(groups_to_create)
- if groups_to_remove:
- self.deactivate_analytic_account(groups_to_remove)
+ if departments_to_create:
+ self.create_analytic_accounts(departments_to_create)
+ if departments_to_remove:
+ self.deactivate_analytic_account(departments_to_remove)
def _check_analytic_accounts(self, vals):
- groups = self.env['expense.group'].browse(
- vals['expense_group_ids'][0][2])
- if sorted(groups) != sorted(self.expense_group_ids):
- self._update_analytic_accounts(groups)
+ departments = self.env['hr.department'].browse(
+ vals['department_ids'][0][2])
+ if sorted(departments) != sorted(self.department_ids):
+ self._update_analytic_accounts(departments)
part_group = self.env['account.analytic.account'].search([
- ('expense_group_id', 'in', self.expense_group_ids.ids),
+ ('department_id', 'in', self.department_ids.ids),
('partner_id', '=', self.id),
('partition_id', '!=', False)], limit=1).partition_id
self.env.ref("analytic_partition_by_employee.matrix_partition_group").\
- calc_percent_by_employee()
+ sudo().calc_percent_by_employee()
part_group.calc_percent_by_employee()
@api.multi
def write(self, vals):
- if vals.get('expense_group_ids'):
+ if vals.get('department_ids'):
self._check_analytic_accounts(vals)
res = super(ResPartner, self).write(vals)
return res
@@ -128,15 +130,9 @@ def write(self, vals):
@api.model
def create(self, vals):
res = super(ResPartner, self).create(vals)
- if vals.get('expense_group_ids') and res.is_branch:
- groups = self.env['expense.group'].browse(
- vals['expense_group_ids'][0][2])
- if groups:
- res.create_analytic_account(groups, True)
+ if vals.get('department_ids') and res.is_branch:
+ departments = self.env['hr.department'].browse(
+ vals['department_ids'][0][2])
+ if departments:
+ res.create_analytic_accounts(departments)
return res
-
-
-class ExpenseGroup(models.Model):
- _name = 'expense.group'
-
- name = fields.Char('Nome')
diff --git a/analytic_partition_by_employee/security/ir.model.access.csv b/analytic_partition_by_employee/security/ir.model.access.csv
index d5eab091..905d710e 100644
--- a/analytic_partition_by_employee/security/ir.model.access.csv
+++ b/analytic_partition_by_employee/security/ir.model.access.csv
@@ -1,5 +1,3 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
-access_expense_group,access_expense_group,model_expense_group,,1,0,0,0
-access_expense_group_manager,access_expense_group,model_expense_group,account.group_account_invoice,1,1,1,1
access_hr_employee_partition,access_hr_employee_partition,model_hr_employee_partition,,1,0,0,0
access_hr_employee_partition_manager,access_hr_employee_partition,model_hr_employee_partition,account.group_account_invoice,1,1,1,1
diff --git a/analytic_partition_by_employee/tests/test_base.py b/analytic_partition_by_employee/tests/test_base.py
index aadcee60..7b6d0308 100644
--- a/analytic_partition_by_employee/tests/test_base.py
+++ b/analytic_partition_by_employee/tests/test_base.py
@@ -19,18 +19,18 @@ def setUp(self):
'account.data_account_type_receivable').id,
'company_id': self.main_company.id
})
- self.group_one = self.env['expense.group'].create({
- 'name': 'Grupo um'
+ self.department_one = self.env['hr.department'].create({
+ 'name': 'Departamento um'
})
- self.group_two = self.env['expense.group'].create({
- 'name': 'Grupo dois'
+ self.department_two = self.env['hr.department'].create({
+ 'name': 'Departamento dois'
})
self.branch_one = self.env['res.partner'].create({
'name': 'Nome Parceiro Um',
'is_branch': True,
'property_account_receivable_id': self.receivable_account.id,
- 'expense_group_ids': [
- [6, 0, [self.group_one.id, self.group_two.id]]]
+ 'department_ids': [
+ [6, 0, [self.department_one.id, self.department_two.id]]]
})
self.analytic_acc_one = self.env['account.analytic.account'].create({
'name': 'Analytic Account One'
diff --git a/analytic_partition_by_employee/tests/test_res_partner.py b/analytic_partition_by_employee/tests/test_res_partner.py
index 98d897b1..7320fb51 100644
--- a/analytic_partition_by_employee/tests/test_res_partner.py
+++ b/analytic_partition_by_employee/tests/test_res_partner.py
@@ -12,7 +12,7 @@ def test_create(self):
analytic_accs = self.env['account.analytic.account'].search([
('partner_id', '=', self.branch_one.id)])
self.assertEquals(len(analytic_accs), len(
- self.branch_one.expense_group_ids))
+ self.branch_one.department_ids))
part_group = self.partition_group
self.assertEquals(len(part_group.partition_line_ids) + 1,
len(analytic_accs))
diff --git a/analytic_partition_by_employee/views/analytic_account.xml b/analytic_partition_by_employee/views/analytic_account.xml
index 1183b69e..909ff7c2 100644
--- a/analytic_partition_by_employee/views/analytic_account.xml
+++ b/analytic_partition_by_employee/views/analytic_account.xml
@@ -5,7 +5,7 @@
-
+
diff --git a/analytic_partition_by_employee/views/hr_employee.xml b/analytic_partition_by_employee/views/hr_employee.xml
index f32dba9d..022dfdad 100644
--- a/analytic_partition_by_employee/views/hr_employee.xml
+++ b/analytic_partition_by_employee/views/hr_employee.xml
@@ -10,7 +10,7 @@
-
+
diff --git a/analytic_partition_by_employee/views/res_partner.xml b/analytic_partition_by_employee/views/res_partner.xml
index ac8ee802..384302a9 100644
--- a/analytic_partition_by_employee/views/res_partner.xml
+++ b/analytic_partition_by_employee/views/res_partner.xml
@@ -11,7 +11,7 @@
-
+