mirror of
https://github.com/ForgeFlow/stock-rma.git
synced 2025-01-21 12:57:49 +02:00
[9.0][FIX] rma_account: refund wizard
This commit is contained in:
@@ -56,10 +56,11 @@ class RmaOrderLine(models.Model):
|
|||||||
index=True,
|
index=True,
|
||||||
readonly=True, states={'draft': [('readonly', False)]},
|
readonly=True, states={'draft': [('readonly', False)]},
|
||||||
)
|
)
|
||||||
refund_line_ids = fields.One2many(comodel_name='account.invoice.line',
|
refund_line_ids = fields.One2many(
|
||||||
inverse_name='rma_line_id',
|
comodel_name='account.invoice.line',
|
||||||
string='Refund Lines',
|
inverse_name='rma_line_id', string='Refund Lines',
|
||||||
copy=False, index=True, readonly=True)
|
copy=False, index=True, readonly=True,
|
||||||
|
)
|
||||||
invoice_id = fields.Many2one('account.invoice', string='Source',
|
invoice_id = fields.Many2one('account.invoice', string='Source',
|
||||||
related='invoice_line_id.invoice_id',
|
related='invoice_line_id.invoice_id',
|
||||||
index=True, readonly=True)
|
index=True, readonly=True)
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# © 2017 Eficent Business and IT Consulting Services S.L.
|
# © 2017 Eficent Business and IT Consulting Services S.L.
|
||||||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
|
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
|
||||||
from openerp import _, api, exceptions, fields, models
|
|
||||||
|
from openerp import _, api, fields, models
|
||||||
|
from openerp.exceptions import ValidationError
|
||||||
import openerp.addons.decimal_precision as dp
|
import openerp.addons.decimal_precision as dp
|
||||||
|
|
||||||
|
|
||||||
@@ -10,12 +12,9 @@ class RmaRefund(models.TransientModel):
|
|||||||
|
|
||||||
@api.model
|
@api.model
|
||||||
def _get_reason(self):
|
def _get_reason(self):
|
||||||
context = dict(self._context or {})
|
active_ids = self.env.context.get('active_ids', False)
|
||||||
active_ids = context.get('active_ids', False)
|
return self.env['rma.order.line'].browse(
|
||||||
if active_ids:
|
active_ids[0]).rma_id.name or ''
|
||||||
rma_lines = self.env['rma.order.line'].browse(active_ids[0])
|
|
||||||
return rma_lines.rma_id.name
|
|
||||||
return ''
|
|
||||||
|
|
||||||
@api.returns('rma.order.line')
|
@api.returns('rma.order.line')
|
||||||
def _prepare_item(self, line):
|
def _prepare_item(self, line):
|
||||||
@@ -37,55 +36,48 @@ class RmaRefund(models.TransientModel):
|
|||||||
lines the supplier field is empty otherwise is the unique line
|
lines the supplier field is empty otherwise is the unique line
|
||||||
supplier.
|
supplier.
|
||||||
"""
|
"""
|
||||||
res = super(RmaRefund, self).default_get(
|
res = super(RmaRefund, self).default_get(fields)
|
||||||
fields)
|
|
||||||
rma_line_obj = self.env['rma.order.line']
|
rma_line_obj = self.env['rma.order.line']
|
||||||
rma_line_ids = self.env.context['active_ids'] or []
|
rma_line_ids = self.env.context['active_ids'] or []
|
||||||
active_model = self.env.context['active_model']
|
active_model = self.env.context['active_model']
|
||||||
|
|
||||||
if not rma_line_ids:
|
if not rma_line_ids:
|
||||||
return res
|
return res
|
||||||
assert active_model == 'rma.order.line', \
|
assert active_model == 'rma.order.line', 'Bad context propagation'
|
||||||
'Bad context propagation'
|
|
||||||
|
|
||||||
items = []
|
items = []
|
||||||
lines = rma_line_obj.browse(rma_line_ids)
|
lines = rma_line_obj.browse(rma_line_ids)
|
||||||
if len(lines.mapped('partner_id')) > 1:
|
if len(lines.mapped('partner_id')) > 1:
|
||||||
raise exceptions.Warning(
|
raise ValidationError(
|
||||||
_('Only RMA lines from the same partner can be processed at '
|
_("Only RMAs from the same partner can be processed at "
|
||||||
'the same time'))
|
"the same time."))
|
||||||
for line in lines:
|
for line in lines:
|
||||||
items.append([0, 0, self._prepare_item(line)])
|
items.append([0, 0, self._prepare_item(line)])
|
||||||
res['item_ids'] = items
|
res['item_ids'] = items
|
||||||
return res
|
return res
|
||||||
|
|
||||||
date_invoice = fields.Date(string='Refund Date',
|
date_invoice = fields.Date(
|
||||||
default=fields.Date.context_today,
|
string='Refund Date',
|
||||||
required=True)
|
default=fields.Date.context_today, required=True,
|
||||||
|
)
|
||||||
date = fields.Date(string='Accounting Date')
|
date = fields.Date(string='Accounting Date')
|
||||||
description = fields.Char(string='Reason', required=True,
|
description = fields.Char(
|
||||||
default=_get_reason)
|
string='Reason', required=True, default=_get_reason,
|
||||||
|
)
|
||||||
item_ids = fields.One2many(
|
item_ids = fields.One2many(
|
||||||
comodel_name='rma.refund.item', inverse_name='wiz_id', string='Items')
|
comodel_name='rma.refund.item',
|
||||||
|
inverse_name='wiz_id', string='Items',
|
||||||
|
)
|
||||||
|
|
||||||
@api.multi
|
@api.multi
|
||||||
def compute_refund(self):
|
def compute_refund(self):
|
||||||
for wizard in self:
|
for wizard in self:
|
||||||
first = self.item_ids[0]
|
first = self.item_ids[0]
|
||||||
values = self._prepare_refund(wizard, first.rma_id)
|
values = self._prepare_refund(wizard, first.line_id)
|
||||||
if len(first.line_id.invoice_address_id):
|
|
||||||
values['partner_id'] = first.line_id.invoice_address_id.id
|
|
||||||
else:
|
|
||||||
values['partner_id'] = first.rma_id.partner_id.id
|
|
||||||
new_refund = self.env['account.invoice'].create(values)
|
new_refund = self.env['account.invoice'].create(values)
|
||||||
for item in self.item_ids:
|
for item in self.item_ids:
|
||||||
refund_line_values = self.prepare_refund_line(item, new_refund)
|
refund_line_values = self.prepare_refund_line(item, new_refund)
|
||||||
self.env['account.invoice.line'].create(
|
self.env['account.invoice.line'].create(
|
||||||
refund_line_values)
|
refund_line_values)
|
||||||
# Put the reason in the chatter
|
|
||||||
subject = _("Invoice refund")
|
|
||||||
body = self.item_ids[0].rma_id.name
|
|
||||||
new_refund.message_post(body=body, subject=subject)
|
|
||||||
return new_refund
|
return new_refund
|
||||||
|
|
||||||
@api.multi
|
@api.multi
|
||||||
@@ -94,12 +86,11 @@ class RmaRefund(models.TransientModel):
|
|||||||
self.env.context['active_ids'])
|
self.env.context['active_ids'])
|
||||||
for line in rma_line_ids:
|
for line in rma_line_ids:
|
||||||
if line.refund_policy == 'no':
|
if line.refund_policy == 'no':
|
||||||
raise exceptions.Warning(
|
raise ValidationError(
|
||||||
_('The operation is not refund for at least one line'))
|
_('The operation is not refund for at least one line'))
|
||||||
if line.state != 'approved':
|
if line.state != 'approved':
|
||||||
raise exceptions.Warning(
|
raise ValidationError(
|
||||||
_('RMA %s is not approved') %
|
_('RMA %s is not approved') % line.name)
|
||||||
line.rma_id.name)
|
|
||||||
new_invoice = self.compute_refund()
|
new_invoice = self.compute_refund()
|
||||||
action = 'action_invoice_tree1' if (
|
action = 'action_invoice_tree1' if (
|
||||||
new_invoice.type in ['out_refund', 'out_invoice']) \
|
new_invoice.type in ['out_refund', 'out_invoice']) \
|
||||||
@@ -118,11 +109,11 @@ class RmaRefund(models.TransientModel):
|
|||||||
else:
|
else:
|
||||||
account = accounts['stock_input']
|
account = accounts['stock_input']
|
||||||
if not account:
|
if not account:
|
||||||
raise exceptions.ValidationError("Accounts are not configure for "
|
raise ValidationError(_(
|
||||||
"this product")
|
"Accounts are not configure for this product."))
|
||||||
values = {
|
values = {
|
||||||
'name': item.rma_id.name,
|
'name': item.line_id.name or item.rma_id.name,
|
||||||
'origin': item.rma_id.name,
|
'origin': item.line_id.name or item.rma_id.name,
|
||||||
'account_id': account.id,
|
'account_id': account.id,
|
||||||
'price_unit': item.line_id.price_unit,
|
'price_unit': item.line_id.price_unit,
|
||||||
'uom_id': item.line_id.uom_id.id,
|
'uom_id': item.line_id.uom_id.id,
|
||||||
@@ -134,40 +125,42 @@ class RmaRefund(models.TransientModel):
|
|||||||
return values
|
return values
|
||||||
|
|
||||||
@api.model
|
@api.model
|
||||||
def _prepare_refund(self, wizard, order):
|
def _prepare_refund(self, wizard, rma_line):
|
||||||
# origin_invoices = self._get_invoice(order)
|
# origin_invoices = self._get_invoice(rma_line)
|
||||||
if order.type == 'customer':
|
if rma_line.type == 'customer':
|
||||||
journal = self.env['account.journal'].search(
|
journal = self.env['account.journal'].search(
|
||||||
[('type', '=', 'sale')], limit=1)
|
[('type', '=', 'sale')], limit=1)
|
||||||
else:
|
else:
|
||||||
journal = self.env['account.journal'].search(
|
journal = self.env['account.journal'].search(
|
||||||
[('type', '=', 'purchase')], limit=1)
|
[('type', '=', 'purchase')], limit=1)
|
||||||
values = {
|
values = {
|
||||||
'name': order.name,
|
'name': rma_line.rma_id.name or rma_line.name,
|
||||||
'origin': order.name,
|
'origin': rma_line.rma_id.name or rma_line.name,
|
||||||
'reference': False,
|
'reference': False,
|
||||||
# 'account_id': account.id,
|
# 'account_id': account.id,
|
||||||
'journal_id': journal.id,
|
'journal_id': journal.id,
|
||||||
'partner_id': order.partner_id.id,
|
'currency_id': rma_line.partner_id.company_id.currency_id.id,
|
||||||
'currency_id': order.partner_id.company_id.currency_id.id,
|
|
||||||
'payment_term_id': False,
|
'payment_term_id': False,
|
||||||
'fiscal_position_id':
|
'fiscal_position_id':
|
||||||
order.partner_id.property_account_position_id.id,
|
rma_line.partner_id.property_account_position_id.id,
|
||||||
|
'state': 'draft',
|
||||||
|
'number': False,
|
||||||
|
'date': wizard.date,
|
||||||
|
'date_invoice': wizard.date_invoice,
|
||||||
|
'partner_id': rma_line.invoice_address_id.id or
|
||||||
|
rma_line.partner_id.id,
|
||||||
}
|
}
|
||||||
team_ids = self.env['crm.team'].search(
|
if self.env.registry.models.get('crm.team', False):
|
||||||
['|', ('user_id', '=', self.env.uid),
|
team_ids = self.env['crm.team'].search(
|
||||||
('member_ids', '=', self.env.uid)], limit=1)
|
['|', ('user_id', '=', self.env.uid),
|
||||||
team_id = team_ids[0] if team_ids else False
|
('member_ids', '=', self.env.uid)], limit=1)
|
||||||
if team_id:
|
team_id = team_ids[0] if team_ids else False
|
||||||
values['team_id'] = team_id.id
|
if team_id:
|
||||||
if order.type == 'customer':
|
values['team_id'] = team_id.id
|
||||||
|
if rma_line.type == 'customer':
|
||||||
values['type'] = 'out_refund'
|
values['type'] = 'out_refund'
|
||||||
else:
|
else:
|
||||||
values['type'] = 'in_refund'
|
values['type'] = 'in_refund'
|
||||||
values['state'] = 'draft'
|
|
||||||
values['number'] = False
|
|
||||||
values['date'] = wizard.date_invoice
|
|
||||||
values['date_invoice'] = wizard.date or wizard.date_invoice
|
|
||||||
return values
|
return values
|
||||||
|
|
||||||
@api.constrains('item_ids')
|
@api.constrains('item_ids')
|
||||||
@@ -175,8 +168,8 @@ class RmaRefund(models.TransientModel):
|
|||||||
def check_unique_invoice_address_id(self):
|
def check_unique_invoice_address_id(self):
|
||||||
addresses = self.item_ids.mapped('invoice_address_id')
|
addresses = self.item_ids.mapped('invoice_address_id')
|
||||||
if len(addresses) > 1:
|
if len(addresses) > 1:
|
||||||
raise exceptions.ValidationError('The invoice address must be the '
|
raise ValidationError(_(
|
||||||
'same for all the lines')
|
"The invoice address must be the same for all the lines."))
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user