Skip to content

Commit

Permalink
[IMP] hr_expense: reuse 'account.register.payments' model to pay empl…
Browse files Browse the repository at this point in the history
…oyee expenses as well.
  • Loading branch information
mitali-willdooit committed Jan 19, 2018
1 parent 9c37b68 commit 5f7061b
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 147 deletions.
2 changes: 1 addition & 1 deletion addons/hr_expense/__manifest__.py
Expand Up @@ -33,7 +33,7 @@
'data/hr_expense_data.xml',
'data/hr_expense_sequence.xml',
'wizard/hr_expense_refuse_reason_views.xml',
'wizard/hr_expense_sheet_register_payment.xml',
'wizard/account_register_payments_views.xml',
'views/hr_expense_views.xml',
'security/ir_rule.xml',
'report/hr_expense_report.xml',
Expand Down
2 changes: 1 addition & 1 deletion addons/hr_expense/views/hr_expense_views.xml
Expand Up @@ -377,7 +377,7 @@
<header>
<button name="approve_expense_sheets" states="submit" string="Approve" type="object" groups="hr_expense.group_hr_expense_user" class="oe_highlight o_expense_sheet_approve"/>
<button name="action_sheet_move_create" states="approve" string="Post Journal Entries" type="object" groups="account.group_account_manager" class="oe_highlight o_expense_sheet_post"/>
<button name="%(hr_expense.hr_expense_sheet_register_payment_wizard_action)d" type="action" string="Register Payment" class="oe_highlight o_expense_sheet_pay" attrs="{'invisible': [('state', '!=', 'post')]}" context="{'default_amount': total_amount, 'partner_id': address_id}" groups="account.group_account_manager"/>
<button name="%(hr_expense.account_register_payments_wizard_action)d" type="action" string="Register Payment" class="oe_highlight o_expense_sheet_pay" attrs="{'invisible': [('state', '!=', 'post')]}" context="{'default_amount': total_amount, 'partner_id': address_id}" groups="account.group_account_manager"/>
<button name="reset_expense_sheets" states="cancel" string="Resubmit" type="object"/>
<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_user" />
<field name="state" widget="statusbar" statusbar_visible="draft,submit,approve,post,done"/>
Expand Down
3 changes: 2 additions & 1 deletion addons/hr_expense/wizard/__init__.py
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-

from . import account_register_payments
from . import hr_expense_refuse_reason
from . import hr_expense_sheet_register_payment
75 changes: 75 additions & 0 deletions addons/hr_expense/wizard/account_register_payments.py
@@ -0,0 +1,75 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from werkzeug import url_encode
from odoo import api, fields, models, _


class AccountRegisterPayments(models.TransientModel):
_inherit = "account.register.payments"

expense_sheet_id = fields.Many2one('hr.expense.sheet')

@api.model
def default_get(self, fields):
vals = super(AccountRegisterPayments, self).default_get(fields)
if not vals.get('expense_sheet_id'):
return vals
expense_sheet = self.env['hr.expense.sheet'].browse(vals.get('expense_sheet_id'))
partner = expense_sheet.address_id or expense_sheet.employee_id.address_home_id
vals.update(
partner_type='supplier',
payment_type='outbound',
amount=abs(expense_sheet.total_amount),
currency_id=expense_sheet.currency_id.id,
partner_id=partner.id
)
return vals

def _prepare_expense_payment_vals(self):
partner = self.expense_sheet_id.address_id or self.expense_sheet_id.employee_id.address_home_id
return {
'journal_id': self.journal_id.id,
'payment_method_id': self.payment_method_id.id,
'payment_date': self.payment_date,
'communication': self.communication,
'partner_type': 'supplier',
'payment_type': 'outbound',
'amount': abs(self.expense_sheet_id.total_amount),
'currency_id': self.expense_sheet_id.currency_id.id,
'partner_id': partner.id
}

@api.multi
def get_payments_vals(self):
self.ensure_one()
res = super(AccountRegisterPayments, self).get_payments_vals()
if not self.expense_sheet_id:
return res
return [self._prepare_expense_payment_vals()]

def _create_payments(self):
payment = super(AccountRegisterPayments, self)._create_payments()
if not self.expense_sheet_id:
return payment

# Log the payment in the chatter
msg = _("A payment of %s %s with the reference <a href='/mail/view?%s'>%s</a> related to your expense <i>%s</i> has been made.")
body = (msg % (payment.amount, payment.currency_id.symbol, url_encode({'model': 'account.payment', 'res_id': payment.id}), payment.name, self.expense_sheet_id.name))
self.expense_sheet_id.message_post(body=body)

# Reconcile the payment and the expense, i.e. lookup on the payable account move lines
account_move_lines_to_reconcile = self.env['account.move.line']
for line in payment.move_line_ids + self.expense_sheet_id.account_move_id.line_ids:
if line.account_id.internal_type == 'payable':
account_move_lines_to_reconcile |= line
account_move_lines_to_reconcile.reconcile()
return payment

@api.multi
def create_payments(self):
self.ensure_one()
res = super(AccountRegisterPayments, self).create_payments()
if not self.expense_sheet_id:
return res
return {'type': 'ir.actions.act_window_close'}
42 changes: 42 additions & 0 deletions addons/hr_expense/wizard/account_register_payments_views.xml
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="view_account_payment_from_invoices_inherit_hr_expense" model="ir.ui.view">
<field name="name">account.register.payments.wizard.inherited</field>
<field name="model">account.register.payments</field>
<field name="inherit_id" ref="account.view_account_payment_from_invoices"/>
<field name="mode">primary</field>
<field name="arch" type="xml">
<data>
<field name="partner_id" position="attributes">
<attribute name="invisible">False</attribute>
<attribute name="required">True</attribute>
<attribute name="readonly">True</attribute>
</field>
<field name="journal_id" position="attributes">
<attribute name="default_focus">1</attribute>
</field>
<field name="payment_date" position="after">
<field name="expense_sheet_id" invisible="1"/>
</field>
<field name="currency_id" position="replace"/>
<xpath expr="//field[@name='amount']" position="replace">
<label for="amount"/>
<div name="amount_div" class="o_row">
<field name="amount"/>
<field name="currency_id" options="{'no_create': True, 'no_open': True}" groups="base.group_multi_currency"/>
</div>
</xpath>
</data>
</field>
</record>
<!-- Expenses -->
<record id="account_register_payments_wizard_action" model="ir.actions.act_window">
<field name="name">Register Payment</field>
<field name="res_model">account.register.payments</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="view_id" ref="hr_expense.view_account_payment_from_invoices_inherit_hr_expense"/>
<field name="target">new</field>
<field name="context">{'default_expense_sheet_id': active_id}</field>
</record>
</odoo>
93 changes: 0 additions & 93 deletions addons/hr_expense/wizard/hr_expense_sheet_register_payment.py

This file was deleted.

51 changes: 0 additions & 51 deletions addons/hr_expense/wizard/hr_expense_sheet_register_payment.xml

This file was deleted.

0 comments on commit 5f7061b

Please sign in to comment.