Skip to content

Commit

Permalink
Merge 3566c60 into 601c9ee
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwojt committed Nov 19, 2019
2 parents 601c9ee + 3566c60 commit 307dbf6
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 33 deletions.
74 changes: 41 additions & 33 deletions crm_claim_rma/models/account_invoice.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,67 @@
# -*- coding: utf-8 -*-
# © 2019 Versada UAB
# © 2017 Techspawn Solutions
# © 2015 Eezee-It, MONK Software, Vauxoo
# © 2013 Camptocamp
# © 2009-2013 Akretion,
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import itertools

from odoo import _, api, exceptions, fields, models


class AccountInvoice(models.Model):

_inherit = "account.invoice"

claim_id = fields.Many2one('crm.claim', string='Claim')

@api.model
def _refund_cleanup_lines(self, lines):
""" Override when from claim to update the quantity and link to the
claim line.
""" Override when from claim to update the quantity, link to the
claim line and return empty list for tax_line_ids.
"""
claim_lines_context = self.env.context.get('claim_line_ids')

# check if is an invoice_line and we are from a claim
if not (self.env.context.get('claim_line_ids') and lines and
lines[0]._name == 'account.invoice.line'):
if not (lines and claim_lines_context):
return super(AccountInvoice, self)._refund_cleanup_lines(lines)

# start by browsing all the lines so that Odoo will correctly prefetch
line_ids = [l[1] for l in self.env.context['claim_line_ids']]
claim_lines = self.env['claim.line'].browse(line_ids)

new_lines = []
for claim_line in claim_lines:
if not claim_line.refund_line_id:
# For each lines replace quantity and add claim_line_id
inv_line = claim_line.invoice_line_id
clean_line = {}
for field_name, field in inv_line._fields.iteritems():
if isinstance(field, fields.Many2one):
clean_line[field_name] = inv_line[field_name].id
elif not isinstance(field, (fields.Many2many,
fields.One2many)):
clean_line[field_name] = inv_line[field_name]
elif field_name == 'invoice_line_tax_id':
tax_ids = inv_line[field_name].ids
clean_line[field_name] = [(6, 0, tax_ids)]
clean_line['quantity'] = claim_line.product_returned_quantity
clean_line['claim_line_id'] = [claim_line.id]

new_lines.append(clean_line)
if not new_lines:
# TODO use custom states to show button of this wizard or
# not instead of raise an error
# We don't want tax_line_ids from origin invoice as new ones
# will be calculated for correct number of lines

if lines[0]._name == 'account.invoice.tax':
return []

claim_lines = self.env['claim.line'].browse([l[1] for l in claim_lines_context])
claim_lines_wo_refund = claim_lines.filtered(
lambda s: not s.refund_line_id).sorted('invoice_line_id')

if not claim_lines_wo_refund:

raise exceptions.UserError(
_('A refund has already been created for this claim !')
_('A refund has already been created for all lines on this claim !')
)

# _refund_cleanup_lines keep original sequence so instead of C/P Odoo code
# we can sort record set and update create values.

invoice_lines = claim_lines_wo_refund.mapped('invoice_line_id').sorted()

# data from res in format (0, 0, {field:value})
refund_invoice_lines_data = super(
AccountInvoice, self)._refund_cleanup_lines(invoice_lines)

for claim_line, invoice_create_values in itertools.izip(
claim_lines_wo_refund,
refund_invoice_lines_data):

create_values = invoice_create_values[2]
create_values.update(
quantity=claim_line.product_returned_quantity,
claim_line_id=claim_line.id,
)
return [(0, 0, l) for l in new_lines]
return refund_invoice_lines_data

@api.model
def _prepare_refund(self, *args, **kwargs):
Expand Down
39 changes: 39 additions & 0 deletions crm_claim_rma/tests/test_claim.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,42 @@ def test_create__with_claim_type(self):
self.assertEqual(supplier_type, claim.claim_type)
self.assertIsNotNone(claim.code)
self.assertTrue(claim.code.startswith('RMA-V/'))

def test_refund_from_claim(self):
invoice_ids = self.env.ref('sale.sale_order_4').action_invoice_create()
invoice = self.env['account.invoice'].browse(invoice_ids[0])
invoice.invoice_validate()
invoice_lines = invoice.mapped('invoice_line_ids')
default_tax = self.env['res.company']._company_default_get('account.tax')
invoice_lines.write(
{'invoice_line_tax_ids': [(4, default_tax.id, 0)]}
)
claim = self.env['crm.claim'].create({
'name': 'Test claim',
'invoice_id': invoice.id
})
claim._onchange_invoice()
claim.claim_line_ids[0].unlink()
refund_wizard = self.env['account.invoice.refund'].with_context({
'invoice_ids': [claim.invoice_id.id],
'claim_line_ids': [(4, line.id, 0) for line in claim.claim_line_ids],
'description': claim.name,
'claim_id': claim.id,
}).create({})
refund = refund_wizard.compute_refund()
refund_invoice = self.env['account.invoice'].search(refund['domain'])

self.assertTrue(refund_invoice)
self.assertEqual(
len(refund_invoice.invoice_line_ids),
len(claim.claim_line_ids)
)
self.assertEqual(
refund_invoice.invoice_line_ids.mapped('invoice_line_tax_ids'),
claim.claim_line_ids.mapped('invoice_line_id.invoice_line_tax_ids'),
)
self.assertEqual(
refund_invoice.invoice_line_ids.mapped('quantity'),
claim.claim_line_ids.mapped('product_returned_quantity'),
)
self.assertTrue(refund_invoice.tax_line_ids[0].tax_id.refund_account_id)

0 comments on commit 307dbf6

Please sign in to comment.