[FIX] hr_expense: create expense report without creating record

Currently, When we create expense report from expense it will
directly create a expense report record so discard button on
top is misleading.

So in this commit, when creating the expense report from the expense
it will not create an record but set the default value in editable
form view with auto computed name based on expense.

Also create report action coming to the two places
1) in top action menu
2) on tree header

So in this commit, removed 'create report' menu from top
action bar.

Added the depends on can_reset and can_approve to compute
the value for displaying the button when record is not saved
from the 'create Expense report' button and default in submit state.
also fix the attachment_number field because expense.id is
NewId_X while passing in default_get of expense_line_ids so count was
0 without saving the expense sheet.

closes odoo/odoo#65104

Taskid: 2359834
Related: odoo/upgrade#1875
Signed-off-by: Yannick Tivisse (yti) <yti@odoo.com>
This commit is contained in:
Bonis Modi
2021-04-06 09:19:44 +00:00
committed by Barad Mahendra
parent 7c9afd09b8
commit d00a381ece
2 changed files with 35 additions and 31 deletions
+32 -15
View File
@@ -2,9 +2,10 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import re
from odoo import api, fields, models, _
from odoo import api, fields, Command, models, _
from odoo.exceptions import UserError, ValidationError
from odoo.tools import email_split, float_is_zero, float_repr
from odoo.tools.misc import clean_context, format_date
from odoo.addons.account.models.account_move import PAYMENT_STATE_SELECTION
@@ -197,7 +198,7 @@ class HrExpense(models.Model):
attachment_data = self.env['ir.attachment'].read_group([('res_model', '=', 'hr.expense'), ('res_id', 'in', self.ids)], ['res_id'], ['res_id'])
attachment = dict((data['res_id'], data['res_id_count']) for data in attachment_data)
for expense in self:
expense.attachment_number = attachment.get(expense.id, 0)
expense.attachment_number = attachment.get(expense._origin.id, 0)
@api.depends('employee_id')
def _compute_is_editable(self):
@@ -385,7 +386,7 @@ Or send your receipts at <a href="mailto:%(email)s?subject=Lunch%%20with%%20cust
'res_id': self.sheet_id.id
}
def _create_sheet_from_expenses(self):
def _get_default_expense_sheet_values(self):
if any(expense.state != 'draft' or expense.sheet_id for expense in self):
raise UserError(_("You cannot report twice the same line!"))
if len(self.mapped('employee_id')) != 1:
@@ -394,24 +395,33 @@ Or send your receipts at <a href="mailto:%(email)s?subject=Lunch%%20with%%20cust
raise UserError(_("You can not create report without category."))
todo = self.filtered(lambda x: x.payment_mode=='own_account') or self.filtered(lambda x: x.payment_mode=='company_account')
sheet = self.env['hr.expense.sheet'].create({
'company_id': self.company_id.id,
'employee_id': self[0].employee_id.id,
'name': todo[0].name if len(todo) == 1 else _('Expense Report'),
'expense_line_ids': [(6, 0, todo.ids)]
})
return sheet
if len(todo) == 1:
expense_name = todo.name
else:
dates = todo.mapped('date')
min_date = format_date(self.env, min(dates))
max_date = format_date(self.env, max(dates))
expense_name = min_date if max_date == min_date else "%s - %s" % (min_date, max_date)
values = {
'default_company_id': self.company_id.id,
'default_employee_id': self[0].employee_id.id,
'default_name': expense_name,
'default_expense_line_ids': [Command.set(todo.ids)],
'default_state': 'submit',
'create': False
}
return values
def action_submit_expenses(self):
sheet = self._create_sheet_from_expenses()
sheet.action_submit_sheet()
context_vals = self._get_default_expense_sheet_values()
return {
'name': _('New Expense Report'),
'type': 'ir.actions.act_window',
'view_mode': 'form',
'res_model': 'hr.expense.sheet',
'target': 'current',
'res_id': sheet.id,
'context': context_vals,
}
def action_get_attachment_view(self):
@@ -921,12 +931,14 @@ class HrExpenseSheet(models.Model):
for sheet in self:
sheet.is_multiple_currency = len(sheet.expense_line_ids.mapped('currency_id')) > 1
@api.depends('employee_id')
def _compute_can_reset(self):
is_expense_user = self.user_has_groups('hr_expense.group_hr_expense_team_approver')
for sheet in self:
sheet.can_reset = is_expense_user if is_expense_user else sheet.employee_id.user_id == self.env.user
@api.depends_context('uid')
@api.depends('employee_id')
def _compute_can_approve(self):
is_approver = self.user_has_groups('hr_expense.group_hr_expense_team_approver, hr_expense.group_hr_expense_user')
is_manager = self.user_has_groups('hr_expense.group_hr_expense_manager')
@@ -962,7 +974,12 @@ class HrExpenseSheet(models.Model):
@api.model
def create(self, vals):
sheet = super(HrExpenseSheet, self.with_context(mail_create_nosubscribe=True, mail_auto_subscribe_no_notify=True)).create(vals)
context = clean_context(self.env.context)
context.update({
'mail_create_nosubscribe': True,
'mail_auto_subscribe_no_notify': True
})
sheet = super(HrExpenseSheet, self.with_context(context)).create(vals)
sheet.activity_update()
return sheet
@@ -1014,7 +1031,7 @@ class HrExpenseSheet(models.Model):
expense_line_ids = self.mapped('expense_line_ids')\
.filtered(lambda r: not float_is_zero(r.total_amount, precision_rounding=(r.currency_id or self.env.company.currency_id).rounding))
res = expense_line_ids.action_move_create()
res = expense_line_ids.with_context(clean_context(self.env.context)).action_move_create()
for sheet in self.filtered(lambda s: not s.accounting_date):
sheet.accounting_date = sheet.account_move_id.date
to_post = self.filtered(lambda sheet: sheet.payment_mode == 'own_account' and sheet.expense_line_ids)
+3 -16
View File
@@ -689,7 +689,7 @@
groups="account.group_account_invoice"/>
<button name="%(hr_expense.hr_expense_refuse_wizard_action)d" states="submit,approve" context="{'hr_expense_refuse_model':'hr.expense.sheet'}" string="Refuse" type="action" groups="hr_expense.group_hr_expense_team_approver" />
<button name="reset_expense_sheets" string="Reset to Draft" type="object" attrs="{'invisible': ['|', ('can_reset', '=', False), ('state', 'not in', ['submit', 'cancel'])]}"/>
<field name="state" widget="statusbar" statusbar_visible="draft,submit,approve,post,done"/>
<field name="state" widget="statusbar" statusbar_visible="draft,submit,approve,post,done" force_save="1"/>
</header>
<sheet>
<div class="oe_button_box" name="button_box">
@@ -734,8 +734,8 @@
<field name="analytic_tag_ids" optional="hide" widget="many2many_tags" groups="analytic.group_analytic_tags"/>
<field name="account_id" optional="hide"/>
<field name="message_unread" invisible="1"/>
<field name="attachment_number" string=" "/>
<button name="action_get_attachment_view" string="View Attachments" type="object" icon="fa-paperclip"/>
<button name="action_get_attachment_view" type="object" icon="fa-paperclip" aria-label="View Attachments" title="View Attachments" class="float-right pr-0"/>
<field name="attachment_number" class="text-left pl-0" nolabel="1"/>
<field name="unit_amount" optional="hide" widget="monetary" options="{'currency_field': 'currency_id'}"/>
<field name="currency_id" optional="hide"/>
<field name="quantity" optional="hide"/>
@@ -1091,19 +1091,6 @@
</field>
</record>
<record id="hr_expense_submit_action_server" model="ir.actions.server">
<field name="name">Create Report</field>
<field name="type">ir.actions.server</field>
<field name="model_id" ref="model_hr_expense"/>
<field name="binding_model_id" ref="model_hr_expense"/>
<field name="binding_view_types">list</field>
<field name="state">code</field>
<field name="code">
if records:
action = records.action_submit_expenses()
</field>
</record>
<menuitem id="menu_hr_expense_root" name="Expenses" sequence="100" web_icon="hr_expense,static/description/icon.png"/>
<menuitem id="menu_hr_expense_my_expenses" name="My Expenses" sequence="1" parent="menu_hr_expense_root" groups="base.group_user"/>