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