From 25eb22a3db82226f57991cccb04983e4764c1c97 Mon Sep 17 00:00:00 2001 From: Jared Kipe Date: Mon, 31 Jan 2022 16:37:03 -0800 Subject: [PATCH] [ADD] hr_commission_timesheet: for 13.0 --- hr_commission_timesheet/__init__.py | 3 + hr_commission_timesheet/__manifest__.py | 17 +++ hr_commission_timesheet/models/__init__.py | 5 + hr_commission_timesheet/models/account.py | 21 +++ hr_commission_timesheet/models/commission.py | 44 ++++++ hr_commission_timesheet/models/contract.py | 10 ++ hr_commission_timesheet/tests/__init__.py | 3 + .../tests/test_commission.py | 144 ++++++++++++++++++ .../views/contract_views.xml | 15 ++ 9 files changed, 262 insertions(+) create mode 100644 hr_commission_timesheet/__init__.py create mode 100644 hr_commission_timesheet/__manifest__.py create mode 100644 hr_commission_timesheet/models/__init__.py create mode 100644 hr_commission_timesheet/models/account.py create mode 100644 hr_commission_timesheet/models/commission.py create mode 100644 hr_commission_timesheet/models/contract.py create mode 100644 hr_commission_timesheet/tests/__init__.py create mode 100644 hr_commission_timesheet/tests/test_commission.py create mode 100644 hr_commission_timesheet/views/contract_views.xml diff --git a/hr_commission_timesheet/__init__.py b/hr_commission_timesheet/__init__.py new file mode 100644 index 00000000..09434554 --- /dev/null +++ b/hr_commission_timesheet/__init__.py @@ -0,0 +1,3 @@ +# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details. + +from . import models diff --git a/hr_commission_timesheet/__manifest__.py b/hr_commission_timesheet/__manifest__.py new file mode 100644 index 00000000..eaa1d9c9 --- /dev/null +++ b/hr_commission_timesheet/__manifest__.py @@ -0,0 +1,17 @@ +# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details. + +{ + 'name': 'Timesheet Commissions', + 'description': 'Pay commission on billed timesheets.', + 'version': '13.0.1.0.0', + 'author': 'Hibou Corp. ', + 'depends': [ + 'hr_commission', + 'sale_timesheet', + ], + 'category': 'Timesheets', + 'data': [ + 'views/contract_views.xml', + ], + 'installable': True, +} diff --git a/hr_commission_timesheet/models/__init__.py b/hr_commission_timesheet/models/__init__.py new file mode 100644 index 00000000..36555b89 --- /dev/null +++ b/hr_commission_timesheet/models/__init__.py @@ -0,0 +1,5 @@ +# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details. + +from . import account +from . import commission +from . import contract diff --git a/hr_commission_timesheet/models/account.py b/hr_commission_timesheet/models/account.py new file mode 100644 index 00000000..d075c395 --- /dev/null +++ b/hr_commission_timesheet/models/account.py @@ -0,0 +1,21 @@ +# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details. + +from odoo import models + + +class AccountMove(models.Model): + _inherit = 'account.move' + + def amount_for_commission(self, commission=None): + if commission and commission.rate_type == 'timesheet': + timesheets = self.timesheet_ids.filtered(lambda ts: ts.employee_id == commission.employee_id) + amount = 0.0 + for timesheet in timesheets: + invoice_line = self.invoice_line_ids.filtered(lambda l: timesheet.so_line in l.sale_line_ids) + if invoice_line: + unit_amount = timesheet.unit_amount + if 'work_billing_rate' in timesheet and timesheet.work_type_id: + unit_amount *= timesheet.work_billing_rate + amount += invoice_line.price_unit * unit_amount + return amount + return super().amount_for_commission(commission) diff --git a/hr_commission_timesheet/models/commission.py b/hr_commission_timesheet/models/commission.py new file mode 100644 index 00000000..c6936fca --- /dev/null +++ b/hr_commission_timesheet/models/commission.py @@ -0,0 +1,44 @@ +# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details. + +from odoo import api, fields, models + + +class HrCommission(models.Model): + _inherit = 'hr.commission' + + rate_type = fields.Selection(selection_add=[('timesheet', 'Timesheets')]) + + @api.onchange('source_move_id', 'contract_id', 'rate_type', 'base_amount', 'rate') + def _compute_amount(self): + for commission in self.filtered(lambda c: c.rate_type == 'timesheet'): + commission.rate = commission.contract_id.timesheet_commission_rate + return super()._compute_amount() + + @api.model + def invoice_validated(self, moves): + source_moves = self._filter_source_moves_for_creation(moves) + res = super().invoice_validated(moves) + commission_obj = self.sudo() + created_commissions = commission_obj.browse() + for move in source_moves: + timesheets = move.timesheet_ids + employees = timesheets.mapped('employee_id') + for employee in employees: + contract = employee.contract_id + move_amount = move.amount_for_commission() + if all((contract, contract.timesheet_commission_rate)): + commission = commission_obj.create({ + 'employee_id': employee.id, + 'contract_id': contract.id, + 'source_move_id': move.id, + 'base_amount': move_amount, + 'rate_type': 'timesheet', + 'company_id': move.company_id.id, + }) + move.commission_ids += commission + created_commissions += commission + + if created_commissions and move.company_id.commission_type == 'on_invoice': + created_commissions.sudo().action_confirm() + + return res diff --git a/hr_commission_timesheet/models/contract.py b/hr_commission_timesheet/models/contract.py new file mode 100644 index 00000000..6a14b8ce --- /dev/null +++ b/hr_commission_timesheet/models/contract.py @@ -0,0 +1,10 @@ +# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details. + +from odoo import fields, models + + +class HrContract(models.Model): + _inherit = 'hr.contract' + + timesheet_commission_rate = fields.Float(string='Timesheet Commission %', + help='Rate to pay for invoiced timesheet value.') diff --git a/hr_commission_timesheet/tests/__init__.py b/hr_commission_timesheet/tests/__init__.py new file mode 100644 index 00000000..4745f69f --- /dev/null +++ b/hr_commission_timesheet/tests/__init__.py @@ -0,0 +1,3 @@ +# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details. + +from . import test_commission diff --git a/hr_commission_timesheet/tests/test_commission.py b/hr_commission_timesheet/tests/test_commission.py new file mode 100644 index 00000000..b3c99a73 --- /dev/null +++ b/hr_commission_timesheet/tests/test_commission.py @@ -0,0 +1,144 @@ +# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details. + +from odoo.tests import common + + +class TestCommission(common.TransactionCase): + + def setUp(self): + super().setUp() + self.sales_user = self._createUser() + self.sales_employee = self._createEmployee(self.sales_user) + self.user = self.env.ref('base.user_demo') + self.employee = self.env.ref('hr.employee_qdp') # This is the employee associated with above user. + self.employee_contract = self._createContract(self.employee, 0.0, 0.0, 5.0) + self.product = self.env.ref('product.product_product_1') + self.product.write({ + 'lst_price': 100.0, + 'type': 'service', + 'service_policy': 'delivered_timesheet', + 'service_tracking': 'task_in_project', + }) + self.partner = self.env.ref('base.res_partner_2') + + def _createUser(self, login='coach'): + return self.env['res.users'].create({ + 'name': 'Coach', + 'email': 'coach', + 'login': login, + }) + + def _createEmployee(self, user): + return self.env['hr.employee'].create({ + 'birthday': '1985-03-14', + 'country_id': self.ref('base.us'), + 'department_id': self.ref('hr.dep_rd'), + 'gender': 'male', + 'name': 'Jared', + 'address_home_id': user.partner_id.id, + 'user_id': user.id, + }) + + def _createContract(self, employee, commission_rate, admin_commission_rate=0.0, timesheet_rate=0.0): + return self.env['hr.contract'].create({ + 'date_start': '2016-01-01', + 'date_end': '2030-12-31', + 'name': 'Contract for tests', + 'wage': 1000.0, + # 'type_id': self.ref('hr_contract.hr_contract_type_emp'), + 'employee_id': employee.id, + 'resource_calendar_id': self.ref('resource.resource_calendar_std'), + 'commission_rate': commission_rate, + 'admin_commission_rate': admin_commission_rate, + 'timesheet_commission_rate': timesheet_rate, + 'state': 'open', # if not "Running" then no automatic selection when Payslip is created in 11.0 + }) + + def _create_sale(self, user): + # Create sale + sale = self.env['sale.order'].create({ + 'partner_id': self.partner.id, + 'order_line': [ + (0, 0, { + 'product_id': self.product.id, + 'product_uom_qty': 1, + 'price_unit': 100.0, + }), + (0, 0, { + 'product_id': self.product.id, + 'product_uom_qty': 1, + 'price_unit': 150.0, + }), + ], + 'user_id': user.id, + }) + + self.assertEqual(sale.user_id.id, user.id) + self.assertEqual(sale.state, 'draft') + return sale + + def test_01_workflow(self): + sale = self._create_sale(self.sales_user) + self.assertEqual(sale.amount_total, 250.0, "Order total not correct (maybe taxed?).") + self.assertEqual(sale.user_id, self.sales_user, "Salesperson not correct.") + sale.action_confirm() + + self.assertIn(sale.state, ('sale', 'done')) + self.assertEqual(sale.invoice_status, 'no', "SO should be invoiced on timesheets.") + self.assertEqual(len(sale.tasks_ids), 2) + task_1, task_2 = sale.tasks_ids + project = sale.tasks_ids.mapped('project_id') + timesheet_100_1 = self.env['account.analytic.line'].create({ + 'date': '2022-01-01', + 'employee_id': self.employee.id, + 'name': 'Test', + 'unit_amount': 10.0, + 'project_id': project.id, + 'task_id': task_1.id, + }) + line_1 = sale.order_line.filtered(lambda l: l.qty_delivered == 10.0) + self.assertTrue(line_1) + self.assertTrue(line_1.qty_delivered, 10.0) + line_2 = sale.order_line - line_1 + timesheet_100_2 = self.env['account.analytic.line'].create({ + 'date': '2022-01-05', + 'employee_id': self.employee.id, + 'name': 'Test', + 'unit_amount': 90.0, + 'project_id': project.id, + 'task_id': task_1.id, + }) + self.assertTrue(line_1.qty_delivered, 100.0) + # create a timesheet for a DIFFERENT employee + timesheet_100_3 = self.env['account.analytic.line'].create({ + 'date': '2022-01-05', + 'employee_id': self.sales_employee.id, + 'name': 'Test', + 'unit_amount': 10.0, + 'project_id': project.id, + 'task_id': task_1.id, + }) + self.assertTrue(line_1.qty_delivered, 110.0) + timesheet_150_1 = self.env['account.analytic.line'].create({ + 'date': '2022-01-07', + 'employee_id': self.employee.id, + 'name': 'Test', + 'unit_amount': 100.0, + 'project_id': project.id, + 'task_id': task_2.id, + }) + self.assertTrue(line_2.qty_delivered, 100.0) + + self.assertEqual(sale.invoice_status, 'to invoice', "Should be ready to invoice.") + wiz = self.env['sale.advance.payment.inv'].with_context(active_ids=sale.ids).create({}) + wiz.create_invoices() + self.assertTrue(sale.invoice_ids, "Should have an invoice.") + invoice = sale.invoice_ids + self.assertEqual(invoice.state, 'draft') + invoice.action_post() + self.assertEqual(invoice.state, 'posted') + self.assertTrue(invoice.commission_ids) + self.assertEqual(len(invoice.commission_ids), 1) + + commission_emp = invoice.commission_ids + self.assertEqual(commission_emp.amount, 1250.0) diff --git a/hr_commission_timesheet/views/contract_views.xml b/hr_commission_timesheet/views/contract_views.xml new file mode 100644 index 00000000..a0589121 --- /dev/null +++ b/hr_commission_timesheet/views/contract_views.xml @@ -0,0 +1,15 @@ + + + + + hr.contract.form.inherit + hr.contract + + + + + + + + + \ No newline at end of file