Files
odoo_source/addons/hr_expense/tests/common.py
T
Julien Alardot (jual) 68fbdc9640 [REF] *expense*: Technical hr_expense refactor
This refactoring handles a lot of needed changes that should have almost
no impact on the UX. The aim is to make the code more readable and coherent

Major changes:
- Improve tests and make sure they actually make sense and test the flow
- Split hr.expense and hr.expense.sheet models into two files
- Rename fields according to account naming conventions
  - all .*_amount become *_amount_currency and .*_amount_company -> .*_amount
  - unit_amount -> price_unit
  - all .*_amount or .*number.* for non-monetary -> nb_*
  - amount_tax -> tax_amount
- Move hr.expense.sample field to hr_expense_extract
- Replace hr.expense.same_currency field by hr.expense.is_multiple_currency
  to be consistent with hr.expense.sheet
- Remove hr.expense.unit_amount_display deprecated field
- Remove OCR-related & deprecated reference field
- Remove hr.expense.sheet.address_id deprecated field
- Simplify security fields (removing sheet_is_editable and merging the behaviour
  for fewer confusions)
- Edit product fields to use related fields when possible

Minor changes:
- Coding UTF-8 lines removal
- Cleaning some imports
- Making some formatting more consistent

task-3358176

closes odoo/odoo#130244

Related: odoo/upgrade#5016
Related: odoo/enterprise#45149
Signed-off-by: Quentin De Paoli <qdp@odoo.com>
2023-10-20 23:37:36 +00:00

125 lines
5.3 KiB
Python

# Part of Odoo. See LICENSE file for full copyright and licensing details.
from datetime import datetime
from freezegun import freeze_time
from odoo import Command
from odoo.addons.account.tests.common import AccountTestInvoicingCommon
from odoo.addons.mail.tests.common import mail_new_test_user
class TestExpenseCommon(AccountTestInvoicingCommon):
@classmethod
def setUpClass(cls, chart_template_ref=None):
super().setUpClass(chart_template_ref=chart_template_ref)
group_expense_manager = cls.env.ref('hr_expense.group_hr_expense_manager')
cls.expense_user_employee = mail_new_test_user(
cls.env,
name='expense_user_employee',
login='expense_user_employee',
email='expense_user_employee@example.com',
notification_type='email',
groups='base.group_user',
company_ids=[Command.set(cls.env.companies.ids)],
)
cls.expense_user_manager = mail_new_test_user(
cls.env,
name='Expense manager',
login='expense_manager_1',
email='expense_manager_1@example.com',
notification_type='email',
groups='base.group_user,hr_expense.group_hr_expense_manager',
company_ids=[Command.set(cls.env.companies.ids)],
)
cls.expense_employee = cls.env['hr.employee'].create({
'name': 'expense_employee',
'user_id': cls.expense_user_employee.id,
'work_contact_id': cls.expense_user_employee.partner_id.id,
})
# Allow the current accounting user to access the expenses.
cls.env.user.groups_id |= group_expense_manager
# Create analytic account
cls.analytic_plan = cls.env['account.analytic.plan'].create({'name': 'Plan Test', 'company_id': False})
cls.analytic_account_1 = cls.env['account.analytic.account'].create({
'name': 'analytic_account_1',
'plan_id': cls.analytic_plan.id,
})
cls.analytic_account_2 = cls.env['account.analytic.account'].create({
'name': 'analytic_account_2',
'plan_id': cls.analytic_plan.id,
})
# Create product without cost
cls.product_c = cls.env['product.product'].create({
'name': 'product_c with no cost',
'uom_id': cls.env.ref('uom.product_uom_dozen').id,
'lst_price': 200.0,
'property_account_income_id': cls.copy_account(cls.company_data['default_account_revenue']).id,
'property_account_expense_id': cls.copy_account(cls.company_data['default_account_expense']).id,
'taxes_id': [Command.set((cls.tax_sale_a + cls.tax_sale_b).ids)],
'supplier_taxes_id': [Command.set((cls.tax_purchase_a + cls.tax_purchase_b).ids)],
'can_be_expensed': True,
'default_code': 'product_c',
})
# Ensure Invoicing tests products can be expensed and their code is properly set.
(cls.product_a + cls.product_b).write({'can_be_expensed': True})
cls.product_a.default_code = 'product_a'
cls.product_b.default_code = 'product_b'
cls.frozen_today = datetime(year=2022, month=1, day=25, hour=0, minute=0, second=0)
# create expense account
cls.expense_account = cls.env['account.account'].create({
'code': '610010',
'name': 'Expense Account 1'
})
def create_expense_report(self, values=None):
values = values or {}
default_values = {
'name': 'Test Expense Report',
'employee_id': self.expense_employee.id,
'company_id': self.company_data['company'].id,
'expense_line_ids': [Command.create({
'employee_id': self.expense_employee.id,
'product_id': self.product_c.id,
'total_amount_currency': 1000.00,
'tax_ids': [Command.set(self.tax_purchase_a.ids)],
'date': self.frozen_today,
'company_id': self.company_data['company'].id,
'currency_id': self.company_data['currency'].id,
})]
}
return self.env['hr.expense.sheet'].create({**default_values, **values})
def create_expense(self, values=None):
values = values or {}
default_values = {
'employee_id': self.expense_employee.id,
'product_id': self.product_c.id,
'total_amount_currency': 1000.00,
'tax_ids': [Command.set(self.tax_purchase_a.ids)],
'date': self.frozen_today,
'company_id': self.company_data['company'].id,
'currency_id': self.company_data['currency'].id,
}
return self.env['hr.expense'].create({**default_values, **values})
def get_new_payment(self, expense_sheet, amount):
""" Helper to create payments """
ctx = {'active_model': 'account.move', 'active_ids': expense_sheet.account_move_ids.ids}
with freeze_time(self.frozen_today):
payment_register = self.env['account.payment.register'].with_context(**ctx).create({
'amount': amount,
'journal_id': self.company_data['default_journal_bank'].id,
'payment_method_line_id': self.inbound_payment_method_line.id,
})
return payment_register._create_payments()