Skip to content
Permalink
Browse files

account: tests run again

  • Loading branch information...
william-andre committed Mar 14, 2019
1 parent e5584f4 commit 81209be57f4bead81354cb9479bb6a2e4d0165ad
@@ -633,7 +633,7 @@ def invoice_print(self):
return self.env.ref('account.account_invoices').report_action(self)
else:
return self.env.ref('account.account_invoices_without_payment').report_action(self)

@api.multi
def action_reconcile_to_check(self, params):
self.ensure_one()
@@ -985,6 +985,10 @@ def action_invoice_re_open(self):
raise UserError(_('Invoice must be paid in order to set it to register payment.'))
return self.write({'state': 'open'})

@api.multi
def action_register_payment(self):
return self.env['account.payment'].with_context(active_ids=[self.env.context['params']['id']], active_model='account.invoice').action_register_payment()

@api.multi
def action_invoice_cancel(self):
return self.filtered(lambda inv: inv.state != 'cancel').action_cancel()
@@ -1809,7 +1813,7 @@ def _onchange_product_id(self):
self_lang = self
if part.lang:
self_lang = self.with_context(lang=part.lang)

product = self_lang.product_id
account = self.get_invoice_line_account(type, product, fpos, company)
if account:
@@ -172,6 +172,16 @@ def _onchange_journal(self):
return {'domain': {'payment_method_id': [('payment_type', '=', payment_type), ('id', 'in', payment_methods_list)]}}
return {}

@api.onchange('invoice_ids')
def _onchange_invoice_ids(self):
# There should be only one invoice_id
amount = self._compute_payment_amount(self.invoice_ids)
self.amount = abs(amount)
self.payment_type = 'inbound' if amount > 0 else 'outbound'
self.partner_id = self.invoice_ids[0].commercial_partner_id
self.partner_type = MAP_INVOICE_TYPE_PARTNER_TYPE[self.invoice_ids[0].type]
self.communication = self.invoice_ids[0].reference or self.invoice_ids[0].number

@api.onchange('partner_id')
def _onchange_partner_id(self):
if self.invoice_ids and self.invoice_ids[0].partner_bank_id:
@@ -351,12 +361,12 @@ def action_register_payment(self):

return {
'name': _('Register Payment'),
'res_model': 'account.payment.register',
'res_model': len(active_ids) == 1 and 'account.payment' or 'account.payment.register',
'view_type': 'form',
'view_mode': 'form',
'view_id': self.env.ref('account.view_account_payment_form_multi').id,
'view_id': len(active_ids) != 1 and self.env.ref('account.view_account_payment_form_multi').id,
'context': {**self.env.context, **{'default_invoice_ids': [(6, False, active_ids)]}},
'target': 'new',
'target': len(active_ids) == 1 and 'current' or 'new',
'type': 'ir.actions.act_window',
}

@@ -1,5 +1,6 @@
from odoo.addons.account.tests.account_test_classes import AccountingTestCase
from odoo.tests import tagged
from odoo.tests.common import Form
import time


@@ -8,7 +9,7 @@ class TestPayment(AccountingTestCase):

def setUp(self):
super(TestPayment, self).setUp()
self.register_payments_model = self.env['account.payment'].with_context(active_model='account.invoice')
self.register_payments_model = self.env['account.payment.register'].with_context(active_model='account.invoice')
self.payment_model = self.env['account.payment']
self.invoice_model = self.env['account.invoice']
self.invoice_line_model = self.env['account.invoice.line']
@@ -41,6 +42,21 @@ def setUp(self):
self.diff_income_account = self.env['res.users'].browse(self.env.uid).company_id.income_currency_exchange_account_id
self.diff_expense_account = self.env['res.users'].browse(self.env.uid).company_id.expense_currency_exchange_account_id

self.form_payment = Form(self.env['account.payment'])
self.form_payment_register = Form(self.env['account.payment.register'])

class group_invoices:
def __init__(self, model, group):
self.model = model
self.group = group

def __enter__(self):
self.previous_state = self.model.env.user.company_id.payment_group_by_partner
self.model.env.user.company_id.payment_group_by_partner = self.group

def __exit__(self, exception_type, exception_value, traceback):
self.model.env.user.company_id.payment_group_by_partner = self.previous_state

def create_invoice(self, amount=100, type='out_invoice', currency_id=None, partner=None, account_id=None):
""" Returns an open invoice """
invoice = self.invoice_model.create({
@@ -87,14 +103,13 @@ def test_full_payment_process(self):
inv_2 = self.create_invoice(amount=200, currency_id=self.currency_eur_id, partner=self.partner_agrolait.id)

ids = [inv_1.id, inv_2.id]
default_dict = self.payment_model.with_context(active_model='account.invoice', active_ids=ids).default_get(self.payment_model.fields_get_keys())
register_payments = self.payment_model.new({**default_dict, **{
register_payments = self.register_payments_model.with_context(active_ids=ids).create({
'payment_date': time.strftime('%Y') + '-07-15',
'journal_id': self.bank_journal_euro.id,
'payment_method_id': self.payment_method_manual_in.id,
'group_invoices': True,
}})
register_payments.create_payments()
})
with self.group_invoices(self, True):
register_payments.create_payments()
payment = self.payment_model.search([], order="id desc", limit=1)

self.assertAlmostEquals(payment.amount, 300)
@@ -166,18 +181,17 @@ def test_multiple_payments_00(self):
inv_4 = self.create_invoice(amount=50, partner=self.partner_agrolait.id, type='in_invoice')

ids = [inv_1.id, inv_2.id, inv_3.id, inv_4.id]
default_dict = self.payment_model.with_context(active_model='account.invoice', active_ids=ids).default_get(self.payment_model.fields_get_keys())
register_payments = self.payment_model.new({**default_dict, **{
register_payments = self.register_payments_model.with_context(active_ids=ids).create({
'payment_date': time.strftime('%Y') + '-07-15',
'journal_id': self.bank_journal_euro.id,
'payment_method_id': self.payment_method_manual_in.id,
'group_invoices': True,
}})
register_payments.create_payments()
})
with self.group_invoices(self, True):
register_payments.create_payments()
payment_ids = self.payment_model.search([('invoice_ids', 'in', ids)], order="id desc")

self.assertEqual(len(payment_ids), 3)
self.assertAlmostEquals(register_payments.amount, 750)
self.assertAlmostEquals(sum(payment_ids.filtered(lambda r: r.payment_type == 'inbound').mapped('amount')) - sum(payment_ids.filtered(lambda r: r.payment_type == 'outbound').mapped('amount')), 750)

inv_1_2_pay = None
inv_3_pay = None
@@ -223,17 +237,16 @@ def test_partial_payment(self):
# Test Customer Invoice
inv_1 = self.create_invoice(amount=600)
ids = [inv_1.id]
default_dict = self.payment_model.with_context(active_model='account.invoice', active_ids=ids).default_get(self.payment_model.fields_get_keys())
register_payments = self.payment_model.new({**default_dict, **{
'payment_date': time.strftime('%Y') + '-07-15',
'journal_id': self.bank_journal_euro.id,
'payment_method_id': self.payment_method_manual_in.id,
}})
payment_register = Form(self.env['account.payment'].with_context(default_invoice_ids=ids))
with payment_register as pr:
pr.payment_date = time.strftime('%Y') + '-07-15'
pr.journal_id = self.bank_journal_euro
pr.payment_method_id = self.payment_method_manual_in

# Perform the partial payment by setting the amount at 550 instead of 600
register_payments.amount = 550
# Perform the partial payment by setting the amount at 550 instead of 600
pr.amount = 550
payment_register.save()

register_payments.create_payments()
payment_ids = self.payment_model.search([('invoice_ids', 'in', ids)], order="id desc")

self.assertEqual(len(payment_ids), 1)
@@ -249,17 +262,16 @@ def test_partial_payment(self):
# Test Vendor Bill
inv_2 = self.create_invoice(amount=500, type='in_invoice', partner=self.partner_china_exp.id)
ids = [inv_2.id]
default_dict = self.payment_model.with_context(active_model='account.invoice', active_ids=ids).default_get(self.payment_model.fields_get_keys())
register_payments = self.payment_model.new({**default_dict, **{
'payment_date': time.strftime('%Y') + '-07-15',
'journal_id': self.bank_journal_euro.id,
'payment_method_id': self.payment_method_manual_in.id,
}})
payment_register = Form(self.env['account.payment'].with_context(default_invoice_ids=ids))
with payment_register as pr:
pr.payment_date = time.strftime('%Y') + '-07-15'
pr.journal_id = self.bank_journal_euro
pr.payment_method_id = self.payment_method_manual_in

# Perform the partial payment by setting the amount at 300 instead of 500
register_payments.amount = 300
# Perform the partial payment by setting the amount at 300 instead of 500
pr.amount = 300

register_payments.create_payments()
payment_register.save()
payment_ids = self.payment_model.search([('invoice_ids', 'in', ids)], order="id desc")

self.assertEqual(len(payment_ids), 1)
@@ -286,18 +298,17 @@ def test_multiple_receivables(self):
inv_3 = self.create_invoice(amount=300, account_id=account_receivable_id_2)

ids = [inv_1.id, inv_2.id, inv_3.id]
default_dict = self.payment_model.with_context(active_model='account.invoice', active_ids=ids).default_get(self.payment_model.fields_get_keys())
register_payments = self.payment_model.new({**default_dict, **{
register_payments = self.register_payments_model.with_context(active_ids=ids).create({
'payment_date': time.strftime('%Y') + '-07-15',
'journal_id': self.bank_journal_euro.id,
'payment_method_id': self.payment_method_manual_in.id,
'group_invoices': True,
}})
register_payments.create_payments()
})
with self.group_invoices(self, True):
register_payments.create_payments()
payment_ids = self.payment_model.search([('invoice_ids', 'in', ids)], order="id desc")

self.assertEqual(len(payment_ids), 2)
self.assertAlmostEquals(register_payments.amount, 550)
self.assertAlmostEquals(sum(payment_ids.mapped('amount')), 550)

inv_1_pay = payment_ids.filtered(lambda p: p.state == 'posted' and p.invoice_ids and p.invoice_ids == inv_1 + inv_2)
inv_2_pay = payment_ids.filtered(lambda p: p.state == 'posted' and p.invoice_ids and p.invoice_ids == inv_3)
@@ -336,27 +347,25 @@ def test_register_payment_group_invoices(self):

# When grouping invoices, we should have one payment per receivable account
ids1 = [inv_1.id, inv_2.id, inv_3.id, inv_4.id]
default_dict = self.payment_model.with_context(active_model='account.invoice', active_ids=ids1).default_get(self.payment_model.fields_get_keys())
register_payments1 = self.payment_model.new({**default_dict, **{
register_payments1 = self.register_payments_model.with_context(active_ids=ids1).create({
'payment_date': time.strftime('%Y') + '-07-15',
'journal_id': self.bank_journal_euro.id,
'payment_method_id': self.payment_method_manual_in.id,
'group_invoices': True,
}})
register_payments1.create_payments()
})
with self.group_invoices(self, True):
register_payments1.create_payments()
payment_ids1 = self.payment_model.search([('invoice_ids', 'in', ids1)], order="id desc")
self.assertEqual(len(payment_ids1), 3, "3 payments should have been created, one fo each (partner, receivable account).")

# When not grouping, we should have one payment per invoice
ids2 = [inv_5.id, inv_6.id, inv_7.id, inv_8.id]
default_dict = self.payment_model.with_context(active_model='account.invoice', active_ids=ids2).default_get(self.payment_model.fields_get_keys())
register_payments2 = self.payment_model.new({**default_dict, **{
register_payments2 = self.register_payments_model.with_context(active_ids=ids2).create({
'payment_date': time.strftime('%Y') + '-07-15',
'journal_id': self.bank_journal_euro.id,
'payment_method_id': self.payment_method_manual_in.id,
'group_invoices': False,
}})
register_payments2.create_payments()
})
with self.group_invoices(self, False):
register_payments2.create_payments()
payment_ids2 = self.payment_model.search([('invoice_ids', 'in', ids2)], order="id desc")
self.assertEqual(len(payment_ids2), 4, "Not grouping payments should always create a distinct payment per invoice.")

@@ -603,40 +612,3 @@ def test_post_at_bank_reconciliation_payment(self):

# The invoice should now be paid
self.assertEqual(invoice.state, 'paid', "Invoice should be in 'paid' state after having reconciled the two payments with a bank statement")

def test_register_payments_multi_invoices(self):
inv1 = self.create_invoice(amount=40)
inv2 = inv1.copy()
inv2.action_invoice_open()

ids = (inv1 + inv2).ids
default_dict = self.payment_model.with_context(active_model='account.invoice', active_ids=ids).default_get(self.payment_model.fields_get_keys())
batch_payment = self.payment_model.new({**default_dict, **{
'amount': 70,
'partner_id': inv1.partner_id.id,
'journal_id': self.bank_journal_usd.id,
'invoice_ids': [(6, False, (inv1 + inv2).ids)],
'partner_type': 'customer',
'payment_difference_handling': 'reconcile',
'payment_type': 'inbound',
'payment_method_id': self.payment_method_manual_in.id,
'writeoff_account_id': self.account_revenue.id,
'writeoff_label': "why can't we live together",
'group_invoices': True}})

batch_payment.create_payments()
payment_id = self.env['account.payment'].search([('invoice_ids', 'in', ids)], order="id desc")

self.assertEqual(inv1.state, 'paid')
self.assertEqual(inv2.state, 'paid')

receivable_inv1 = inv1.move_id.line_ids.filtered(lambda l: l.account_id.internal_type == 'receivable')
receivable_inv2 = inv2.move_id.line_ids.filtered(lambda l: l.account_id.internal_type == 'receivable')
receivable_payment = payment_id.move_line_ids.filtered(lambda l: l.account_id.internal_type == 'receivable')

self.assertEqual(
receivable_inv1.full_reconcile_id.reconciled_line_ids,
receivable_inv1 + receivable_inv2 + receivable_payment
)
write_off_line = payment_id.move_line_ids.filtered(lambda l: l.account_id == payment_id.writeoff_account_id)
self.assertEqual(write_off_line.debit, 10)
@@ -265,7 +265,7 @@
<form string="Vendor Bill">
<header>
<button name="action_invoice_open" type="object" states="draft" string="Validate" class="oe_highlight" groups="account.group_account_invoice"/>
<button name="%(action_account_invoice_from_list)d" type="action" states="open" string="Register Payment" groups="account.group_account_invoice" class="oe_highlight"/>
<button name="action_register_payment" type="object" states="open" string="Register Payment" groups="account.group_account_invoice" class="oe_highlight"/>
<button name="%(action_account_invoice_refund)d" type='action' string='Ask for a Credit Note' groups="account.group_account_invoice" attrs="{'invisible': ['|',('type', 'in', ['in_refund','out_refund']),('state','not in',('open','in_payment','paid'))]}"/>
<button name="action_invoice_draft" states="cancel" string="Set to Draft" type="object" groups="account.group_account_invoice"/>
<field name="state" widget="statusbar" statusbar_visible="draft,open,paid" />
@@ -418,7 +418,7 @@
<header>
<button name="action_invoice_sent" string="Send &amp; Print" type="object" attrs="{'invisible':['|',('sent','=',True), ('state', 'not in', ('open','in_payment','paid'))]}" class="oe_highlight" groups="base.group_user"/>
<button name="action_invoice_sent" string="Send &amp; Print" type="object" attrs="{'invisible':['|',('sent','=',False), ('state', 'not in', ('open','in_payment','paid'))]}" groups="base.group_user"/>
<button name="%(action_account_invoice_from_list)d" id="account_invoice_payment_btn" type="action"
<button name="action_register_payment" id="account_invoice_payment_btn" type="object"
attrs="{'invisible': [('state', '!=', 'open')]}"
string="Register Payment" groups="account.group_account_invoice" class="oe_highlight"/>
<button name="action_invoice_open" type="object" states="draft" string="Validate" class="oe_highlight o_invoice_validate" groups="account.group_account_invoice"/>

0 comments on commit 81209be

Please sign in to comment.
You can’t perform that action at this time.