Code review + add coverage
This commit is contained in:
parent
5577ed7ec0
commit
0d9b82f4f9
@ -11,8 +11,7 @@ class AccountInvoice(models.Model):
|
||||
def action_move_create(self):
|
||||
"""Invoked when validating the invoices."""
|
||||
res = super().action_move_create()
|
||||
for invoice in self:
|
||||
spreads = invoice.invoice_line_ids.mapped('spread_id')
|
||||
spreads = self.mapped('invoice_line_ids.spread_id')
|
||||
spreads.compute_spread_board()
|
||||
spreads.reconcile_spread_moves()
|
||||
return res
|
||||
@ -37,18 +36,18 @@ class AccountInvoice(models.Model):
|
||||
"""Cancel the spread lines and their related moves when
|
||||
the invoice is canceled."""
|
||||
res = super().action_cancel()
|
||||
for invoice_line in self.mapped('invoice_line_ids'):
|
||||
moves = invoice_line.spread_id.line_ids.mapped('move_id')
|
||||
spread_lines = self.mapped('invoice_line_ids.spread_id.line_ids')
|
||||
moves = spread_lines.mapped('move_id')
|
||||
moves.button_cancel()
|
||||
moves.unlink()
|
||||
invoice_line.spread_id.line_ids.unlink()
|
||||
spread_lines.unlink()
|
||||
return res
|
||||
|
||||
@api.model
|
||||
def _refund_cleanup_lines(self, lines):
|
||||
result = super(AccountInvoice, self)._refund_cleanup_lines(lines)
|
||||
result = super()._refund_cleanup_lines(lines)
|
||||
for i, line in enumerate(lines):
|
||||
for name, field in line._fields.items():
|
||||
for name in line._fields.keys():
|
||||
if name == 'spread_id':
|
||||
result[i][2][name] = False
|
||||
break
|
||||
|
@ -402,8 +402,7 @@ class AccountSpread(models.Model):
|
||||
"""Checks whether the spread lines should be calculated.
|
||||
In case checks pass, invoke "def _compute_spread_board()" method.
|
||||
"""
|
||||
for spread in self:
|
||||
if spread.total_amount:
|
||||
for spread in self.filtered(lambda s: s.total_amount):
|
||||
spread._compute_spread_board()
|
||||
|
||||
@api.multi
|
||||
@ -434,35 +433,33 @@ class AccountSpread(models.Model):
|
||||
|
||||
@api.multi
|
||||
def _action_unlink_invoice_line(self):
|
||||
for spread in self:
|
||||
spread_mls = spread.line_ids.mapped('move_id.line_ids')
|
||||
spread_mls = self.mapped('line_ids.move_id.line_ids')
|
||||
spread_mls.remove_move_reconcile()
|
||||
spread._message_post_unlink_invoice_line()
|
||||
spread.write({'invoice_line_ids': [(5, 0, 0)]})
|
||||
self._message_post_unlink_invoice_line()
|
||||
self.write({'invoice_line_ids': [(5, 0, 0)]})
|
||||
|
||||
def _message_post_unlink_invoice_line(self):
|
||||
self.ensure_one()
|
||||
for spread in self:
|
||||
invoice_id = spread.invoice_id.id
|
||||
inv_link = '<a href=# data-oe-model=account.invoice ' \
|
||||
'data-oe-id=%d>%s</a>' % (self.invoice_id.id, _("Invoice"))
|
||||
'data-oe-id=%d>%s</a>' % (invoice_id, _("Invoice"))
|
||||
msg_body = _("Unlinked invoice line '%s' (view %s).") % (
|
||||
self.invoice_line_id.name, inv_link)
|
||||
self.message_post(body=msg_body)
|
||||
spread.invoice_line_id.name, inv_link)
|
||||
spread.message_post(body=msg_body)
|
||||
spread_link = '<a href=# data-oe-model=account.spread ' \
|
||||
'data-oe-id=%d>%s</a>' % (self.id, _("Spread Board"))
|
||||
'data-oe-id=%d>%s</a>' % (spread.id, _("Spread"))
|
||||
msg_body = _("Unlinked '%s' (invoice line %s).") % (
|
||||
spread_link, self.invoice_line_id.name)
|
||||
self.invoice_id.message_post(body=msg_body)
|
||||
spread_link, spread.invoice_line_id.name)
|
||||
spread.invoice_id.message_post(body=msg_body)
|
||||
|
||||
@api.multi
|
||||
def unlink(self):
|
||||
for spread in self:
|
||||
if spread.invoice_line_id:
|
||||
if self.filtered(lambda s: s.invoice_line_id):
|
||||
raise UserError(
|
||||
_('Cannot delete spread(s) that are linked '
|
||||
'to an invoice line.'))
|
||||
posted_line_ids = self.line_ids.filtered(
|
||||
lambda x: x.move_id.state == 'posted')
|
||||
if posted_line_ids:
|
||||
if self.mapped('line_ids.move_id').filtered(
|
||||
lambda m: m.state == 'posted'):
|
||||
raise ValidationError(
|
||||
_('Cannot delete spread(s): there are '
|
||||
'posted Journal Entries.'))
|
||||
@ -528,9 +525,7 @@ class AccountSpread(models.Model):
|
||||
|
||||
@api.multi
|
||||
def create_all_moves(self):
|
||||
for spread in self:
|
||||
for line in spread.line_ids:
|
||||
if not line.move_id:
|
||||
for line in self.mapped('line_ids').filtered(lambda l: not l.move_id):
|
||||
line.create_move()
|
||||
|
||||
@api.depends(
|
||||
|
@ -61,12 +61,12 @@ class AccountInvoiceSpreadLine(models.Model):
|
||||
|
||||
@api.multi
|
||||
def _create_moves(self):
|
||||
created_moves = self.env['account.move']
|
||||
for line in self:
|
||||
if line.move_id:
|
||||
if self.filtered(lambda l: l.move_id):
|
||||
raise UserError(_('This spread line is already linked to a '
|
||||
'journal entry! Please post or delete it.'))
|
||||
|
||||
created_moves = self.env['account.move']
|
||||
for line in self:
|
||||
move_vals = line._prepare_move()
|
||||
move = self.env['account.move'].create(move_vals)
|
||||
|
||||
|
@ -746,3 +746,25 @@ class TestAccountInvoiceSpread(common.TransactionCase):
|
||||
|
||||
with self.assertRaises(ValidationError):
|
||||
self.spread.unlink()
|
||||
|
||||
def test_15_invoice_refund(self):
|
||||
|
||||
self.invoice_line.spread_id = self.spread
|
||||
|
||||
# Validate invoice
|
||||
self.invoice.action_invoice_open()
|
||||
self.assertTrue(self.invoice.invoice_line_ids.mapped('spread_id'))
|
||||
|
||||
# Create a refund for invoice.
|
||||
self.env['account.invoice.refund'].with_context({
|
||||
'active_model': 'account.invoice',
|
||||
'active_ids': [self.invoice.id],
|
||||
'active_id': self.invoice.id
|
||||
}).create(dict(
|
||||
description='Invoice Refund',
|
||||
filter_refund='refund',
|
||||
)).invoice_refund()
|
||||
|
||||
# Invoice lines do not contain the lint to the spread.
|
||||
refund = self.invoice.refund_invoice_ids[0]
|
||||
self.assertFalse(refund.invoice_line_ids.mapped('spread_id'))
|
||||
|
Loading…
Reference in New Issue
Block a user