From d1ff8856d0aebba410665da9ce681959c0d85633 Mon Sep 17 00:00:00 2001 From: Jared Kipe Date: Mon, 23 Apr 2018 09:54:32 -0700 Subject: [PATCH 1/9] Initial commit `hr_expense_vendor` for 11.0 --- hr_expense_vendor/__init__.py | 1 + hr_expense_vendor/__manifest__.py | 19 ++++++++++ hr_expense_vendor/models/__init__.py | 1 + hr_expense_vendor/models/hr_expense.py | 24 ++++++++++++ hr_expense_vendor/tests/__init__.py | 1 + hr_expense_vendor/tests/test_expenses.py | 40 ++++++++++++++++++++ hr_expense_vendor/views/hr_expense_views.xml | 26 +++++++++++++ 7 files changed, 112 insertions(+) create mode 100644 hr_expense_vendor/__init__.py create mode 100644 hr_expense_vendor/__manifest__.py create mode 100644 hr_expense_vendor/models/__init__.py create mode 100644 hr_expense_vendor/models/hr_expense.py create mode 100644 hr_expense_vendor/tests/__init__.py create mode 100644 hr_expense_vendor/tests/test_expenses.py create mode 100644 hr_expense_vendor/views/hr_expense_views.xml diff --git a/hr_expense_vendor/__init__.py b/hr_expense_vendor/__init__.py new file mode 100644 index 00000000..0650744f --- /dev/null +++ b/hr_expense_vendor/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/hr_expense_vendor/__manifest__.py b/hr_expense_vendor/__manifest__.py new file mode 100644 index 00000000..dee0c40e --- /dev/null +++ b/hr_expense_vendor/__manifest__.py @@ -0,0 +1,19 @@ +{ + 'name': 'HR Expense Vendor', + 'version': '11.0.1.0.0', + 'author': 'Hibou Corp. ', + 'category': 'Human Resources', + 'summary': 'Record the vendor paid on expenses.', + 'description': """ +Record the vendor paid on expenses. +""", + 'website': 'https://hibou.io/', + 'depends': [ + 'hr_expense', + ], + 'data': [ + 'views/hr_expense_views.xml', + ], + 'installable': True, + 'auto_install': False, +} diff --git a/hr_expense_vendor/models/__init__.py b/hr_expense_vendor/models/__init__.py new file mode 100644 index 00000000..86b1ea31 --- /dev/null +++ b/hr_expense_vendor/models/__init__.py @@ -0,0 +1 @@ +from . import hr_expense diff --git a/hr_expense_vendor/models/hr_expense.py b/hr_expense_vendor/models/hr_expense.py new file mode 100644 index 00000000..e2ef8b8e --- /dev/null +++ b/hr_expense_vendor/models/hr_expense.py @@ -0,0 +1,24 @@ +from odoo import api, fields, models +from odoo.exceptions import ValidationError + + +class HRExpense(models.Model): + _inherit = 'hr.expense' + + vendor_id = fields.Many2one('res.partner', string='Vendor') + + def _prepare_move_line(self, line): + values = super(HRExpense, self)._prepare_move_line(line) + if self.payment_mode == 'company_account': + if not self.vendor_id: + raise ValidationError('You must have an assigned vendor to process a Company Paid Expense') + values['partner_id'] = self.vendor_id.id + name = values['name'] + (' - ' + str(self.reference) if self.reference else '') + values['name'] = name[:64] + return values + + +class HRExpenseSheet(models.Model): + _inherit = 'hr.expense.sheet' + + expense_line_ids = fields.One2many(states={'done': [('readonly', True)], 'post': [('readonly', True)]}) diff --git a/hr_expense_vendor/tests/__init__.py b/hr_expense_vendor/tests/__init__.py new file mode 100644 index 00000000..bf77393e --- /dev/null +++ b/hr_expense_vendor/tests/__init__.py @@ -0,0 +1 @@ +from . import test_expenses diff --git a/hr_expense_vendor/tests/test_expenses.py b/hr_expense_vendor/tests/test_expenses.py new file mode 100644 index 00000000..2cceb774 --- /dev/null +++ b/hr_expense_vendor/tests/test_expenses.py @@ -0,0 +1,40 @@ +from odoo.addons.hr_expense.tests.test_expenses import TestCheckJournalEntry +from odoo.exceptions import ValidationError + + +class TestCheckVendor(TestCheckJournalEntry): + + def setUp(self): + super(TestCheckVendor, self).setUp() + self.vendor_id = self.env.ref('base.res_partner_3') + + def test_journal_entry_vendor(self): + # Non company_account is handled by the super class's `test_journal_entry_ + self.expense.payment_mode = 'company_account' + self.expense_line.payment_mode = 'company_account' + + # Submitted to Manager + self.assertEquals(self.expense.state, 'submit', 'Expense is not in Reported state') + # Approve + self.expense.approve_expense_sheets() + self.assertEquals(self.expense.state, 'approve', 'Expense is not in Approved state') + # Create Expense Entries + with self.assertRaises(ValidationError): + self.expense.action_sheet_move_create() + + self.expense_line.vendor_id = self.vendor_id + self.expense.action_sheet_move_create() + self.assertEquals(self.expense.state, 'done') + self.assertTrue(self.expense.account_move_id.id, 'Expense Journal Entry is not created') + + # [(line.debit, line.credit, line.tax_line_id.id) for line in self.expense.expense_line_ids.account_move_id.line_ids] + # should git this result [(0.0, 700.0, False), (63.64, 0.0, 179), (636.36, 0.0, False)] + for line in self.expense.account_move_id.line_ids: + if line.credit: + self.assertEqual(line.partner_id, self.vendor_id) + self.assertAlmostEquals(line.credit, 700.00) + else: + if not line.tax_line_id == self.tax: + self.assertAlmostEquals(line.debit, 636.36) + else: + self.assertAlmostEquals(line.debit, 63.64) diff --git a/hr_expense_vendor/views/hr_expense_views.xml b/hr_expense_vendor/views/hr_expense_views.xml new file mode 100644 index 00000000..9038e8ec --- /dev/null +++ b/hr_expense_vendor/views/hr_expense_views.xml @@ -0,0 +1,26 @@ + + + + hr.expense.sheet.form.inherit + hr.expense.sheet + + + + + + + + + + hr.expense.form.inherit + hr.expense + + + + + + + + \ No newline at end of file From f968a91c964de98495b991c5ff696290d555a807 Mon Sep 17 00:00:00 2001 From: Kristen Marie Kulha Date: Fri, 8 Jun 2018 15:09:45 -0700 Subject: [PATCH 2/9] Add README. --- hr_expense_vendor/README.rst | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 hr_expense_vendor/README.rst diff --git a/hr_expense_vendor/README.rst b/hr_expense_vendor/README.rst new file mode 100644 index 00000000..e702d50b --- /dev/null +++ b/hr_expense_vendor/README.rst @@ -0,0 +1,30 @@ +************************* +Hibou - HR Expense Vendor +************************* + +Records the vendor paid on expenses. + +For more information and add-ons, visit `Hibou.io `_. + + +============= +Main Features +============= + +* Validates presence of assigned vendor to process a Company Paid Expense. +* If the expense is company paid, then the vendor will be the partner used when creating the journal entry, this makes it much easier to reconcile. +* Additionally, adds the expense reference to the journal entry to make it easier to reconcile in either case. + + +.. image:: https://user-images.githubusercontent.com/15882954/41182457-9b692f92-6b2a-11e8-9d5a-d8ef2f19f198.png + :alt: 'Expense Detail' + :width: 988 + :align: left + +======= +License +======= + +Please see `LICENSE `_. + +Copyright Hibou Corp. 2018 From 1770accd507319bd58fcc0beb821e5b9989d306a Mon Sep 17 00:00:00 2001 From: Jared Kipe Date: Mon, 20 May 2019 12:31:31 -0700 Subject: [PATCH 3/9] MIG `hr_expense_vendor` to 12.0 --- hr_expense_vendor/__manifest__.py | 2 +- hr_expense_vendor/models/hr_expense.py | 19 +++++---- hr_expense_vendor/tests/test_expenses.py | 44 +++++++++++++------- hr_expense_vendor/views/hr_expense_views.xml | 2 +- 4 files changed, 43 insertions(+), 24 deletions(-) diff --git a/hr_expense_vendor/__manifest__.py b/hr_expense_vendor/__manifest__.py index dee0c40e..e48c836e 100644 --- a/hr_expense_vendor/__manifest__.py +++ b/hr_expense_vendor/__manifest__.py @@ -1,6 +1,6 @@ { 'name': 'HR Expense Vendor', - 'version': '11.0.1.0.0', + 'version': '12.0.1.0.0', 'author': 'Hibou Corp. ', 'category': 'Human Resources', 'summary': 'Record the vendor paid on expenses.', diff --git a/hr_expense_vendor/models/hr_expense.py b/hr_expense_vendor/models/hr_expense.py index e2ef8b8e..f30ae71d 100644 --- a/hr_expense_vendor/models/hr_expense.py +++ b/hr_expense_vendor/models/hr_expense.py @@ -7,15 +7,18 @@ class HRExpense(models.Model): vendor_id = fields.Many2one('res.partner', string='Vendor') - def _prepare_move_line(self, line): - values = super(HRExpense, self)._prepare_move_line(line) - if self.payment_mode == 'company_account': - if not self.vendor_id: + @api.multi + def _get_account_move_line_values(self): + move_line_values_by_expense = super(HRExpense, self)._get_account_move_line_values() + for expense in self.filtered(lambda e: e.payment_mode == 'company_account'): + if not expense.vendor_id: raise ValidationError('You must have an assigned vendor to process a Company Paid Expense') - values['partner_id'] = self.vendor_id.id - name = values['name'] + (' - ' + str(self.reference) if self.reference else '') - values['name'] = name[:64] - return values + move_line_values = move_line_values_by_expense[expense.id] + for line_values in move_line_values: + new_name = expense.name.split('\n')[0][:64] + (' - ' + str(self.reference) if self.reference else '') + line_values['name'] = new_name[:64] + line_values['partner_id'] = expense.vendor_id.id + return move_line_values_by_expense class HRExpenseSheet(models.Model): diff --git a/hr_expense_vendor/tests/test_expenses.py b/hr_expense_vendor/tests/test_expenses.py index 2cceb774..37b51d89 100644 --- a/hr_expense_vendor/tests/test_expenses.py +++ b/hr_expense_vendor/tests/test_expenses.py @@ -1,35 +1,51 @@ -from odoo.addons.hr_expense.tests.test_expenses import TestCheckJournalEntry +from odoo.addons.hr_expense.tests.test_expenses import TestAccountEntry from odoo.exceptions import ValidationError -class TestCheckVendor(TestCheckJournalEntry): +class TestCheckVendor(TestAccountEntry): def setUp(self): super(TestCheckVendor, self).setUp() self.vendor_id = self.env.ref('base.res_partner_3') def test_journal_entry_vendor(self): - # Non company_account is handled by the super class's `test_journal_entry_ - self.expense.payment_mode = 'company_account' - self.expense_line.payment_mode = 'company_account' + expense = self.env['hr.expense.sheet'].create({ + 'name': 'Expense for John Smith', + 'employee_id': self.employee.id, + }) + expense_line = self.env['hr.expense'].create({ + 'name': 'Car Travel Expenses', + 'employee_id': self.employee.id, + 'product_id': self.product_expense.id, + 'unit_amount': 700.00, + 'tax_ids': [(6, 0, [self.tax.id])], + 'sheet_id': expense.id, + 'analytic_account_id': self.analytic_account.id, + }) + expense.payment_mode = 'company_account' + expense_line.payment_mode = 'company_account' + expense_line._onchange_product_id() + # State should default to draft + self.assertEquals(expense.state, 'draft', 'Expense should be created in Draft state') # Submitted to Manager - self.assertEquals(self.expense.state, 'submit', 'Expense is not in Reported state') + expense.action_submit_sheet() + self.assertEquals(expense.state, 'submit', 'Expense is not in Reported state') # Approve - self.expense.approve_expense_sheets() - self.assertEquals(self.expense.state, 'approve', 'Expense is not in Approved state') + expense.approve_expense_sheets() + self.assertEquals(expense.state, 'approve', 'Expense is not in Approved state') # Create Expense Entries with self.assertRaises(ValidationError): - self.expense.action_sheet_move_create() + expense.action_sheet_move_create() - self.expense_line.vendor_id = self.vendor_id - self.expense.action_sheet_move_create() - self.assertEquals(self.expense.state, 'done') - self.assertTrue(self.expense.account_move_id.id, 'Expense Journal Entry is not created') + expense_line.vendor_id = self.vendor_id + expense.action_sheet_move_create() + self.assertEquals(expense.state, 'done') + self.assertTrue(expense.account_move_id.id, 'Expense Journal Entry is not created') # [(line.debit, line.credit, line.tax_line_id.id) for line in self.expense.expense_line_ids.account_move_id.line_ids] # should git this result [(0.0, 700.0, False), (63.64, 0.0, 179), (636.36, 0.0, False)] - for line in self.expense.account_move_id.line_ids: + for line in expense.account_move_id.line_ids: if line.credit: self.assertEqual(line.partner_id, self.vendor_id) self.assertAlmostEquals(line.credit, 700.00) diff --git a/hr_expense_vendor/views/hr_expense_views.xml b/hr_expense_vendor/views/hr_expense_views.xml index 9038e8ec..57a50710 100644 --- a/hr_expense_vendor/views/hr_expense_views.xml +++ b/hr_expense_vendor/views/hr_expense_views.xml @@ -15,7 +15,7 @@ hr.expense.form.inherit hr.expense - + Date: Wed, 11 Sep 2019 16:52:44 -0400 Subject: [PATCH 4/9] MIG `hr_expense_vendor` For Odoo 13.0 --- hr_expense_vendor/__manifest__.py | 2 +- hr_expense_vendor/models/hr_expense.py | 1 - hr_expense_vendor/views/hr_expense_views.xml | 3 +-- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/hr_expense_vendor/__manifest__.py b/hr_expense_vendor/__manifest__.py index e48c836e..861ca08f 100644 --- a/hr_expense_vendor/__manifest__.py +++ b/hr_expense_vendor/__manifest__.py @@ -1,6 +1,6 @@ { 'name': 'HR Expense Vendor', - 'version': '12.0.1.0.0', + 'version': '13.0.1.0.0', 'author': 'Hibou Corp. ', 'category': 'Human Resources', 'summary': 'Record the vendor paid on expenses.', diff --git a/hr_expense_vendor/models/hr_expense.py b/hr_expense_vendor/models/hr_expense.py index f30ae71d..6e5cf1fe 100644 --- a/hr_expense_vendor/models/hr_expense.py +++ b/hr_expense_vendor/models/hr_expense.py @@ -7,7 +7,6 @@ class HRExpense(models.Model): vendor_id = fields.Many2one('res.partner', string='Vendor') - @api.multi def _get_account_move_line_values(self): move_line_values_by_expense = super(HRExpense, self)._get_account_move_line_values() for expense in self.filtered(lambda e: e.payment_mode == 'company_account'): diff --git a/hr_expense_vendor/views/hr_expense_views.xml b/hr_expense_vendor/views/hr_expense_views.xml index 57a50710..e469cf18 100644 --- a/hr_expense_vendor/views/hr_expense_views.xml +++ b/hr_expense_vendor/views/hr_expense_views.xml @@ -18,8 +18,7 @@ - + From 12b169337ddda186d3a5bdcead1c9f70d3569581 Mon Sep 17 00:00:00 2001 From: Jared Kipe Date: Sun, 1 Dec 2019 11:08:39 -0800 Subject: [PATCH 5/9] FIX `hr_expense_vendor` don't crash when posting expense sheet with multiple expense lines and references --- hr_expense_vendor/models/hr_expense.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hr_expense_vendor/models/hr_expense.py b/hr_expense_vendor/models/hr_expense.py index 6e5cf1fe..47b99793 100644 --- a/hr_expense_vendor/models/hr_expense.py +++ b/hr_expense_vendor/models/hr_expense.py @@ -14,7 +14,7 @@ class HRExpense(models.Model): raise ValidationError('You must have an assigned vendor to process a Company Paid Expense') move_line_values = move_line_values_by_expense[expense.id] for line_values in move_line_values: - new_name = expense.name.split('\n')[0][:64] + (' - ' + str(self.reference) if self.reference else '') + new_name = expense.name.split('\n')[0][:64] + (' - ' + str(expense.reference) if expense.reference else '') line_values['name'] = new_name[:64] line_values['partner_id'] = expense.vendor_id.id return move_line_values_by_expense From fd3b4f7a00954625512219aa3c963e9f99d464c1 Mon Sep 17 00:00:00 2001 From: Cedric Collins Date: Wed, 14 Apr 2021 18:28:05 -0500 Subject: [PATCH 6/9] [MIG] hr_expense_vendor: migrate to 14.0 --- hr_expense_vendor/__manifest__.py | 2 +- hr_expense_vendor/tests/test_expenses.py | 81 ++++++++++---------- hr_expense_vendor/views/hr_expense_views.xml | 3 +- 3 files changed, 44 insertions(+), 42 deletions(-) diff --git a/hr_expense_vendor/__manifest__.py b/hr_expense_vendor/__manifest__.py index 861ca08f..2cde209e 100644 --- a/hr_expense_vendor/__manifest__.py +++ b/hr_expense_vendor/__manifest__.py @@ -1,6 +1,6 @@ { 'name': 'HR Expense Vendor', - 'version': '13.0.1.0.0', + 'version': '14.0.1.0.0', 'author': 'Hibou Corp. ', 'category': 'Human Resources', 'summary': 'Record the vendor paid on expenses.', diff --git a/hr_expense_vendor/tests/test_expenses.py b/hr_expense_vendor/tests/test_expenses.py index 37b51d89..949fa021 100644 --- a/hr_expense_vendor/tests/test_expenses.py +++ b/hr_expense_vendor/tests/test_expenses.py @@ -1,56 +1,59 @@ -from odoo.addons.hr_expense.tests.test_expenses import TestAccountEntry +from odoo.addons.hr_expense.tests.common import TestExpenseCommon from odoo.exceptions import ValidationError +from odoo.tests import Form, tagged -class TestCheckVendor(TestAccountEntry): +@tagged('-at_install', 'post_install') +class TestCheckVendor(TestExpenseCommon): - def setUp(self): - super(TestCheckVendor, self).setUp() - self.vendor_id = self.env.ref('base.res_partner_3') + @classmethod + def setUpClass(cls, chart_template_ref=None): + super(TestCheckVendor, cls).setUpClass(chart_template_ref=chart_template_ref) + cls.vendor_id = cls.env.ref('base.res_partner_3') + cls.tax = cls.env['account.tax'].create({ + 'name': 'Expense 10%', + 'amount': 10, + 'amount_type': 'percent', + 'type_tax_use': 'purchase', + 'price_include': True, + }) def test_journal_entry_vendor(self): - expense = self.env['hr.expense.sheet'].create({ - 'name': 'Expense for John Smith', - 'employee_id': self.employee.id, - }) - expense_line = self.env['hr.expense'].create({ - 'name': 'Car Travel Expenses', - 'employee_id': self.employee.id, - 'product_id': self.product_expense.id, - 'unit_amount': 700.00, - 'tax_ids': [(6, 0, [self.tax.id])], - 'sheet_id': expense.id, - 'analytic_account_id': self.analytic_account.id, - }) - expense.payment_mode = 'company_account' - expense_line.payment_mode = 'company_account' - expense_line._onchange_product_id() + expense_form = Form(self.env['hr.expense']) + expense_form.name = 'Car Travel Expenses' + expense_form.employee_id = self.expense_employee + expense_form.product_id = self.product_a + expense_form.unit_amount = 700.00 + expense_form.tax_ids.clear() + expense_form.tax_ids.add(self.tax) + expense_form.analytic_account_id = self.analytic_account_1 + expense_form.payment_mode = 'company_account' + expense = expense_form.save() - # State should default to draft - self.assertEquals(expense.state, 'draft', 'Expense should be created in Draft state') - # Submitted to Manager - expense.action_submit_sheet() - self.assertEquals(expense.state, 'submit', 'Expense is not in Reported state') + action_submit_expenses = expense.action_submit_expenses() + expense_sheet = self.env[action_submit_expenses['res_model']].browse(action_submit_expenses['res_id']) + + self.assertEqual(expense_sheet.state, 'submit', 'Expense is not in Submitted state') # Approve - expense.approve_expense_sheets() - self.assertEquals(expense.state, 'approve', 'Expense is not in Approved state') + expense_sheet.approve_expense_sheets() + self.assertEqual(expense_sheet.state, 'approve', 'Expense is not in Approved state') # Create Expense Entries with self.assertRaises(ValidationError): - expense.action_sheet_move_create() + expense_sheet.action_sheet_move_create() - expense_line.vendor_id = self.vendor_id - expense.action_sheet_move_create() - self.assertEquals(expense.state, 'done') - self.assertTrue(expense.account_move_id.id, 'Expense Journal Entry is not created') + expense.vendor_id = self.vendor_id + expense_sheet.action_sheet_move_create() + self.assertEqual(expense_sheet.state, 'done') + self.assertTrue(expense_sheet.account_move_id.id, 'Expense Journal Entry is not created') - # [(line.debit, line.credit, line.tax_line_id.id) for line in self.expense.expense_line_ids.account_move_id.line_ids] - # should git this result [(0.0, 700.0, False), (63.64, 0.0, 179), (636.36, 0.0, False)] - for line in expense.account_move_id.line_ids: + # [(line.debit, line.credit, line.tax_line_id.id) for line in expense_sheet.account_move_id.line_ids] + # should get this result [(0.0, 700.0, False), (63.64, 0.0, 179), (636.36, 0.0, False)] + for line in expense_sheet.account_move_id.line_ids: if line.credit: self.assertEqual(line.partner_id, self.vendor_id) - self.assertAlmostEquals(line.credit, 700.00) + self.assertAlmostEqual(line.credit, 700.00) else: if not line.tax_line_id == self.tax: - self.assertAlmostEquals(line.debit, 636.36) + self.assertAlmostEqual(line.debit, 636.36) else: - self.assertAlmostEquals(line.debit, 63.64) + self.assertAlmostEqual(line.debit, 63.64) diff --git a/hr_expense_vendor/views/hr_expense_views.xml b/hr_expense_vendor/views/hr_expense_views.xml index e469cf18..d1903271 100644 --- a/hr_expense_vendor/views/hr_expense_views.xml +++ b/hr_expense_vendor/views/hr_expense_views.xml @@ -6,8 +6,7 @@ - + From 472dbf02719d4938122e452594680b5f6599d445 Mon Sep 17 00:00:00 2001 From: Jared Kipe Date: Wed, 5 Jan 2022 13:57:52 -0800 Subject: [PATCH 7/9] [MIG] hr_expense_vendor: to Odoo 15.0 --- hr_expense_vendor/__manifest__.py | 2 +- hr_expense_vendor/tests/test_expenses.py | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/hr_expense_vendor/__manifest__.py b/hr_expense_vendor/__manifest__.py index 2cde209e..82199c50 100644 --- a/hr_expense_vendor/__manifest__.py +++ b/hr_expense_vendor/__manifest__.py @@ -1,6 +1,6 @@ { 'name': 'HR Expense Vendor', - 'version': '14.0.1.0.0', + 'version': '15.0.1.0.0', 'author': 'Hibou Corp. ', 'category': 'Human Resources', 'summary': 'Record the vendor paid on expenses.', diff --git a/hr_expense_vendor/tests/test_expenses.py b/hr_expense_vendor/tests/test_expenses.py index 949fa021..c4698b86 100644 --- a/hr_expense_vendor/tests/test_expenses.py +++ b/hr_expense_vendor/tests/test_expenses.py @@ -31,7 +31,11 @@ class TestCheckVendor(TestExpenseCommon): expense = expense_form.save() action_submit_expenses = expense.action_submit_expenses() - expense_sheet = self.env[action_submit_expenses['res_model']].browse(action_submit_expenses['res_id']) + expense_sheet_form = Form(self.env[action_submit_expenses['res_model']].with_context(**action_submit_expenses.get('context', {}))) + expense_sheet = expense_sheet_form.save() + + self.assertEqual(expense_sheet.state, 'draft', 'Expense is not in Draft state') + expense_sheet.action_submit_sheet() self.assertEqual(expense_sheet.state, 'submit', 'Expense is not in Submitted state') # Approve From 8d79f565501086a9b22f0c04dd1f1f4032aad3e8 Mon Sep 17 00:00:00 2001 From: Leo Pinedo Date: Thu, 20 Oct 2022 22:01:25 +0000 Subject: [PATCH 8/9] [MIG] hr_expense_vendor: to 16 --- hr_expense_vendor/__manifest__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hr_expense_vendor/__manifest__.py b/hr_expense_vendor/__manifest__.py index 82199c50..2dd1088b 100644 --- a/hr_expense_vendor/__manifest__.py +++ b/hr_expense_vendor/__manifest__.py @@ -1,6 +1,6 @@ { 'name': 'HR Expense Vendor', - 'version': '15.0.1.0.0', + 'version': '16.0.1.0.0', 'author': 'Hibou Corp. ', 'category': 'Human Resources', 'summary': 'Record the vendor paid on expenses.', From dcd4bab757c237fc46a9ef210d4dddd927021bc0 Mon Sep 17 00:00:00 2001 From: Cedric Collins Date: Tue, 14 Mar 2023 16:34:46 -0500 Subject: [PATCH 9/9] [MIG] hr_expense_vendor: migrate move creation and form view --- hr_expense_vendor/models/hr_expense.py | 73 ++++++++++++++++---- hr_expense_vendor/tests/test_expenses.py | 9 ++- hr_expense_vendor/views/hr_expense_views.xml | 2 +- 3 files changed, 66 insertions(+), 18 deletions(-) diff --git a/hr_expense_vendor/models/hr_expense.py b/hr_expense_vendor/models/hr_expense.py index 47b99793..66dcc46d 100644 --- a/hr_expense_vendor/models/hr_expense.py +++ b/hr_expense_vendor/models/hr_expense.py @@ -1,5 +1,6 @@ -from odoo import api, fields, models -from odoo.exceptions import ValidationError +from odoo import api, fields, models, _ +from odoo.exceptions import UserError +from odoo.tools import float_compare class HRExpense(models.Model): @@ -8,19 +9,67 @@ class HRExpense(models.Model): vendor_id = fields.Many2one('res.partner', string='Vendor') def _get_account_move_line_values(self): - move_line_values_by_expense = super(HRExpense, self)._get_account_move_line_values() - for expense in self.filtered(lambda e: e.payment_mode == 'company_account'): - if not expense.vendor_id: - raise ValidationError('You must have an assigned vendor to process a Company Paid Expense') - move_line_values = move_line_values_by_expense[expense.id] - for line_values in move_line_values: - new_name = expense.name.split('\n')[0][:64] + (' - ' + str(expense.reference) if expense.reference else '') - line_values['name'] = new_name[:64] - line_values['partner_id'] = expense.vendor_id.id - return move_line_values_by_expense + self.ensure_one() + expense = self + return { + 'name': expense.employee_id.name + ': ' + expense.name.split('\n')[0][:64], + 'account_id': expense.account_id.id, + 'quantity': expense.quantity or 1, + 'price_unit': expense.unit_amount if expense.unit_amount != 0 else expense.total_amount, + 'product_id': expense.product_id.id, + 'product_uom_id': expense.product_uom_id.id, + 'analytic_distribution': expense.analytic_distribution, + 'expense_id': expense.id, + 'partner_id': expense.vendor_id.id, + 'tax_ids': [(6, 0, expense.tax_ids.ids)], + 'currency_id': expense.currency_id.id, + } + + def action_move_create(self): + """ + Move creation value are no longer built in extendable methods, + so we need to override here to edit moves before they are posted + """ + company_paid = self.filtered(lambda e: e.payment_mode == 'company_account') + if company_paid.filtered(lambda e: not e.vendor_id): + raise UserError(_('You must have an assigned vendor to process a Company Paid Expense')) + moves_by_expense = super(HRExpense, self - company_paid).action_move_create() + company_moves = self.env['account.move'].create(company_paid.sheet_id._prepare_account_move_vals_list()) + company_moves._post() + + for expense in company_paid: + expense.sheet_id.paid_expense_sheets() + + moves_by_expense.update({move.expense_sheet_id.id: move for move in company_moves}) + return moves_by_expense class HRExpenseSheet(models.Model): _inherit = 'hr.expense.sheet' expense_line_ids = fields.One2many(states={'done': [('readonly', True)], 'post': [('readonly', True)]}) + + def _prepare_account_move_vals_list(self): + if self.filtered(lambda s: len(s.expense_line_ids.vendor_id) > 1): + raise UserError(_("You cannot create journal entries for different vendors in the same report.")) + return [{ + 'journal_id': ( + sheet.bank_journal_id + if sheet.payment_mode == 'company_account' else + sheet.journal_id + ).id, + 'move_type': 'in_receipt', + 'company_id': sheet.company_id.id, + 'partner_id': sheet.expense_line_ids.vendor_id.id, + 'date': sheet.accounting_date or fields.Date.context_today(sheet), + 'invoice_date': sheet.accounting_date or fields.Date.context_today(sheet), + 'ref': sheet.name, + # force the name to the default value, to avoid an eventual 'default_name' in the context + # to set it to '' which cause no number to be given to the account.move when posted. + 'name': '/', + 'expense_sheet_id': [fields.Command.set(sheet.ids)], + 'line_ids':[ + fields.Command.create(expense._get_account_move_line_values()) + for expense in sheet.expense_line_ids + ] + } for sheet in self] diff --git a/hr_expense_vendor/tests/test_expenses.py b/hr_expense_vendor/tests/test_expenses.py index c4698b86..2ecfbe98 100644 --- a/hr_expense_vendor/tests/test_expenses.py +++ b/hr_expense_vendor/tests/test_expenses.py @@ -1,5 +1,5 @@ from odoo.addons.hr_expense.tests.common import TestExpenseCommon -from odoo.exceptions import ValidationError +from odoo.exceptions import UserError from odoo.tests import Form, tagged @@ -22,11 +22,10 @@ class TestCheckVendor(TestExpenseCommon): expense_form = Form(self.env['hr.expense']) expense_form.name = 'Car Travel Expenses' expense_form.employee_id = self.expense_employee - expense_form.product_id = self.product_a - expense_form.unit_amount = 700.00 + expense_form.product_id = self.product_zero_cost + expense_form.total_amount = 700.00 expense_form.tax_ids.clear() expense_form.tax_ids.add(self.tax) - expense_form.analytic_account_id = self.analytic_account_1 expense_form.payment_mode = 'company_account' expense = expense_form.save() @@ -42,7 +41,7 @@ class TestCheckVendor(TestExpenseCommon): expense_sheet.approve_expense_sheets() self.assertEqual(expense_sheet.state, 'approve', 'Expense is not in Approved state') # Create Expense Entries - with self.assertRaises(ValidationError): + with self.assertRaises(UserError): expense_sheet.action_sheet_move_create() expense.vendor_id = self.vendor_id diff --git a/hr_expense_vendor/views/hr_expense_views.xml b/hr_expense_vendor/views/hr_expense_views.xml index d1903271..58e73d11 100644 --- a/hr_expense_vendor/views/hr_expense_views.xml +++ b/hr_expense_vendor/views/hr_expense_views.xml @@ -16,7 +16,7 @@ hr.expense - +