Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.0][IMP] account_payment_return: Add return expenses functionality #156

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions account_payment_return/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,19 @@ Another option to fill info is setting references and click match button to
find matches with invoices, move lines or moves. This functionality is extended
by other modules as *account_payment_return_import_sepa_pain*

It's possible to add a commission amount on each line.

Next, press button "Confirm" to create a new move line that removes the
balance from the bank journal and reconcile items together to show payment
history through it.

After confirmation you can access from the payment form view to the move
created.

.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/96/10.0

Known issues / Roadmap
======================

* Add a button to see the created move.
* Allow to add a commission amount on each line.

Bug Tracker
===========

Expand All @@ -58,6 +57,7 @@ Contributors
* Sergio Teruel <sergio.teruel@tecnativa.com>
* Carlos Dauden <carlos.dauden@tecnativa.com>
* David Vidal <david.vidal@tecnativa.com>
* Luis M. Ontalba <luis.martinez@tecnativa.com>

Maintainer
----------
Expand Down
3 changes: 2 additions & 1 deletion account_payment_return/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

{
"name": "Account Payment Returns",
"version": "10.0.1.0.0",
"version": "10.0.1.1.0",
"summary": "Manage the return of your payments",
'license': 'AGPL-3',
"depends": [
Expand All @@ -27,6 +27,7 @@
'security/ir.model.access.csv',
'security/account_payment_return_security.xml',
'views/payment_return_view.xml',
'views/account_journal_view.xml',
'data/ir_sequence_data.xml',
'views/account_invoice_view.xml',
],
Expand Down
1 change: 1 addition & 0 deletions account_payment_return/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
from . import account_invoice
from . import payment_return_reason
from . import account_move
from . import account_journal
17 changes: 17 additions & 0 deletions account_payment_return/models/account_journal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
# Copyright 2017 luis M. Ontalba <luis.martinez@tecnativa.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


class AccountJournal(models.Model):
_inherit = "account.journal"

default_expense_account_id = fields.Many2one(
comodel_name='account.account', string='Default Expense Account',
help='Default account for commission expenses')
default_expense_partner_id = fields.Many2one(
comodel_name="res.partner", string="Default Expense Partner",
domain=[('supplier', '=', True)], help='Default partner for '
'commission expenses')
37 changes: 37 additions & 0 deletions account_payment_return/models/payment_return.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Copyright 2013 Pedro M. Baeza <pedro.baeza@tecnativa.com>
# Copyright 2014 Markus Schneider <markus.schneider@initos.com>
# Copyright 2016 Carlos Dauden <carlos.dauden@tecnativa.com>
# Copyright 2017 Luis M. Ontalba <luis.martinez@tecnativa.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import _, api, fields, models
Expand Down Expand Up @@ -157,6 +158,28 @@ def action_confirm(self):
(move_line | move_line2).reconcile()
return_line.move_line_ids.mapped('matched_debit_ids').write(
{'origin_returned_move_ids': [(6, 0, returned_moves.ids)]})
if return_line.expense_amount:
expense_lines_vals = []
expense_lines_vals.append({
'name': move.ref,
'move_id': move.id,
'debit': 0.0,
'credit': return_line.expense_amount,
'partner_id': return_line.expense_partner_id.id,
'account_id': (return_line.return_id.journal_id.
default_credit_account_id.id),
})
expense_lines_vals.append({
'move_id': move.id,
'debit': return_line.expense_amount,
'name': move.ref,
'credit': 0.0,
'partner_id': return_line.expense_partner_id.id,
'account_id': return_line.expense_account.id,
})
for expense_line_vals in expense_lines_vals:
move_line_obj.with_context(
check_move_validity=False).create(expense_line_vals)
extra_lines_vals = return_line._prepare_extra_move_lines(move)
for extra_line_vals in extra_lines_vals:
move_line_obj.create(extra_line_vals)
Expand Down Expand Up @@ -231,6 +254,13 @@ class PaymentReturnLine(models.Model):
string='Amount',
help="Returned amount. Can be different from the move amount",
digits=dp.get_precision('Account'))
expense_account = fields.Many2one(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put at journal level a default expense account and expense partner, and add an _onchange_expense_amount to bring this data to the fields.

comodel_name='account.account', string='Expense Account')
expense_amount = fields.Float(string='Expense amount')
expense_partner_id = fields.Many2one(
comodel_name="res.partner", string="Expense partner",
domain=[('supplier', '=', True)],
)

@api.multi
def _compute_amount(self):
Expand All @@ -251,6 +281,13 @@ def _get_partner_from_move(self):
def _onchange_move_line(self):
self._compute_amount()

@api.onchange('expense_amount')
def _onchange_expense_amount(self):
if self.expense_amount:
journal = self.return_id.journal_id
self.expense_account = journal.default_expense_account_id
self.expense_partner_id = journal.default_expense_partner_id

@api.multi
def match_invoice(self):
for line in self:
Expand Down
28 changes: 21 additions & 7 deletions account_payment_return/tests/test_payment_return.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright 2015 Pedro M. Baeza <pedro.baeza@tecnativa.com>
# Copyright 2016 Carlos Dauden <carlos.dauden@tecnativa.com>
# Copyright 2017 David Vidal <david.vidal@tecnativa.com>
# Copyright 2017 Luis M. Ontalba <luis.martinez@tecnativa.com>
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

import json
Expand All @@ -20,12 +21,6 @@ def setUpClass(cls):
'type': 'sale',
'update_posted': True,
})
cls.bank_journal = cls.env['account.journal'].create({
'name': 'Test Bank Journal',
'code': 'BANK',
'type': 'bank',
'update_posted': True,
})
cls.account_type = cls.env['account.account.type'].create({
'name': 'Test',
'type': 'receivable',
Expand All @@ -36,6 +31,15 @@ def setUpClass(cls):
'user_type_id': cls.account_type.id,
'reconcile': True,
})
cls.partner_expense = cls.env['res.partner'].create({'name': 'PE'})
cls.bank_journal = cls.env['account.journal'].create({
'name': 'Test Bank Journal',
'code': 'BANK',
'type': 'bank',
'update_posted': True,
'default_expense_account_id': cls.account.id,
'default_expense_partner_id': cls.partner_expense.id,
})
cls.account_income = cls.env['account.account'].create({
'name': 'Test income account',
'code': 'INCOME',
Expand Down Expand Up @@ -80,7 +84,10 @@ def setUpClass(cls):
'line_ids': [
(0, 0, {'partner_id': cls.partner.id,
'move_line_ids': [(6, 0, cls.payment_line.ids)],
'amount': cls.payment_line.credit})]})
'amount': cls.payment_line.credit,
'expense_account': cls.account.id,
'expense_amount': 10.0,
'expense_partner_id': cls.partner.id})]})

def test_confirm_error(self):
self.payment_return.line_ids[0].move_line_ids = False
Expand All @@ -100,11 +107,18 @@ def test_payment_return(self):
self.assertEqual(self.payment_return.state, 'cancelled')
self.payment_return.action_draft()
self.assertEqual(self.payment_return.state, 'draft')
self.payment_return.line_ids[0].expense_amount = 20.0
self.payment_return.line_ids[0]._onchange_expense_amount()
self.payment_return.action_confirm()
self.assertEqual(self.payment_return.state, 'done')
self.assertEqual(self.invoice.state, 'open')
self.assertEqual(self.invoice.residual, self.receivable_line.debit)
self.assertFalse(self.receivable_line.reconciled)
self.assertEqual(self.payment_return.line_ids[0].expense_account,
self.bank_journal.default_expense_account_id)
self.assertEqual(self.payment_return.line_ids[0].expense_partner_id,
self.bank_journal.default_expense_partner_id)
self.assertEqual(len(self.payment_return.move_id.line_ids), 4)
with self.assertRaises(UserError):
self.payment_return.unlink()
self.payment_return.action_cancel()
Expand Down
19 changes: 19 additions & 0 deletions account_payment_return/views/account_journal_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2017 Luis M. Ontalba <luis.martínez@tecnativa.com>
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl-3). -->

<odoo>

<record id="view_account_journal_form" model="ir.ui.view">
<field name="name">account.journal.form</field>
<field name="model">account.journal</field>
<field name="inherit_id" ref="account.view_account_journal_form"/>
<field name="arch" type="xml">
<field name="default_debit_account_id" position="after">
<field name="default_expense_account_id"/>
<field name="default_expense_partner_id"/>
</field>
</field>
</record>

</odoo>
6 changes: 6 additions & 0 deletions account_payment_return/views/payment_return_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<field name="journal_id" domain="[('type', 'in', ['bank', 'cash'])]"/>
<field name="date"/>
<field name="company_id" groups="base.group_multi_company"/>
<field name="move_id" readonly="True"/>
</group>
<notebook colspan="4">
<page string="Lines">
Expand All @@ -42,6 +43,11 @@
('reconciled', '=', True)]"
/>
<field name="amount"/>
<field name="expense_amount" />
<field name="expense_account"
attrs="{'required':[('expense_amount','!=', 0)]}" />
<field name="expense_partner_id"
attrs="{'required':[('expense_account','!=', False)]}"/>
</tree>
</field>
</page>
Expand Down