[MIG] hr_expense_vendor: migrate move creation and form view

This commit is contained in:
Cedric Collins
2023-03-14 16:34:46 -05:00
parent 8d79f56550
commit dcd4bab757
3 changed files with 66 additions and 18 deletions

View File

@@ -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]

View File

@@ -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

View File

@@ -16,7 +16,7 @@
<field name="model">hr.expense</field>
<field name="inherit_id" ref="hr_expense.hr_expense_view_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='product_id']" position="after">
<xpath expr="//field[@name='product_id']/parent::div" position="after">
<field name="vendor_id" context="{'res_partner_search_mode': 'supplier'}" groups="account.group_account_user"/>
</xpath>
</field>