diff --git a/rma_account/models/rma_order_line.py b/rma_account/models/rma_order_line.py index e8b9cda8..c1209054 100644 --- a/rma_account/models/rma_order_line.py +++ b/rma_account/models/rma_order_line.py @@ -56,10 +56,11 @@ class RmaOrderLine(models.Model): index=True, readonly=True, states={'draft': [('readonly', False)]}, ) - refund_line_ids = fields.One2many(comodel_name='account.invoice.line', - inverse_name='rma_line_id', - string='Refund Lines', - copy=False, index=True, readonly=True) + refund_line_ids = fields.One2many( + comodel_name='account.invoice.line', + inverse_name='rma_line_id', string='Refund Lines', + copy=False, index=True, readonly=True, + ) invoice_id = fields.Many2one('account.invoice', string='Source', related='invoice_line_id.invoice_id', index=True, readonly=True) diff --git a/rma_account/wizards/rma_refund.py b/rma_account/wizards/rma_refund.py index 04c1b166..6180115e 100644 --- a/rma_account/wizards/rma_refund.py +++ b/rma_account/wizards/rma_refund.py @@ -1,7 +1,9 @@ # -*- coding: utf-8 -*- # © 2017 Eficent Business and IT Consulting Services S.L. # 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 @@ -10,12 +12,9 @@ class RmaRefund(models.TransientModel): @api.model def _get_reason(self): - context = dict(self._context or {}) - active_ids = context.get('active_ids', False) - if active_ids: - rma_lines = self.env['rma.order.line'].browse(active_ids[0]) - return rma_lines.rma_id.name - return '' + active_ids = self.env.context.get('active_ids', False) + return self.env['rma.order.line'].browse( + active_ids[0]).rma_id.name or '' @api.returns('rma.order.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 supplier. """ - res = super(RmaRefund, self).default_get( - fields) + res = super(RmaRefund, self).default_get(fields) rma_line_obj = self.env['rma.order.line'] rma_line_ids = self.env.context['active_ids'] or [] active_model = self.env.context['active_model'] - if not rma_line_ids: return res - assert active_model == 'rma.order.line', \ - 'Bad context propagation' + assert active_model == 'rma.order.line', 'Bad context propagation' items = [] lines = rma_line_obj.browse(rma_line_ids) if len(lines.mapped('partner_id')) > 1: - raise exceptions.Warning( - _('Only RMA lines from the same partner can be processed at ' - 'the same time')) + raise ValidationError( + _("Only RMAs from the same partner can be processed at " + "the same time.")) for line in lines: items.append([0, 0, self._prepare_item(line)]) res['item_ids'] = items return res - date_invoice = fields.Date(string='Refund Date', - default=fields.Date.context_today, - required=True) + date_invoice = fields.Date( + string='Refund Date', + default=fields.Date.context_today, required=True, + ) date = fields.Date(string='Accounting Date') - description = fields.Char(string='Reason', required=True, - default=_get_reason) + description = fields.Char( + string='Reason', required=True, default=_get_reason, + ) 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 def compute_refund(self): for wizard in self: first = self.item_ids[0] - values = self._prepare_refund(wizard, first.rma_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 + values = self._prepare_refund(wizard, first.line_id) new_refund = self.env['account.invoice'].create(values) for item in self.item_ids: refund_line_values = self.prepare_refund_line(item, new_refund) self.env['account.invoice.line'].create( 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 @api.multi @@ -94,12 +86,11 @@ class RmaRefund(models.TransientModel): self.env.context['active_ids']) for line in rma_line_ids: if line.refund_policy == 'no': - raise exceptions.Warning( + raise ValidationError( _('The operation is not refund for at least one line')) if line.state != 'approved': - raise exceptions.Warning( - _('RMA %s is not approved') % - line.rma_id.name) + raise ValidationError( + _('RMA %s is not approved') % line.name) new_invoice = self.compute_refund() action = 'action_invoice_tree1' if ( new_invoice.type in ['out_refund', 'out_invoice']) \ @@ -118,11 +109,11 @@ class RmaRefund(models.TransientModel): else: account = accounts['stock_input'] if not account: - raise exceptions.ValidationError("Accounts are not configure for " - "this product") + raise ValidationError(_( + "Accounts are not configure for this product.")) values = { - 'name': item.rma_id.name, - 'origin': item.rma_id.name, + 'name': item.line_id.name or item.rma_id.name, + 'origin': item.line_id.name or item.rma_id.name, 'account_id': account.id, 'price_unit': item.line_id.price_unit, 'uom_id': item.line_id.uom_id.id, @@ -134,40 +125,42 @@ class RmaRefund(models.TransientModel): return values @api.model - def _prepare_refund(self, wizard, order): - # origin_invoices = self._get_invoice(order) - if order.type == 'customer': + def _prepare_refund(self, wizard, rma_line): + # origin_invoices = self._get_invoice(rma_line) + if rma_line.type == 'customer': journal = self.env['account.journal'].search( [('type', '=', 'sale')], limit=1) else: journal = self.env['account.journal'].search( [('type', '=', 'purchase')], limit=1) values = { - 'name': order.name, - 'origin': order.name, + 'name': rma_line.rma_id.name or rma_line.name, + 'origin': rma_line.rma_id.name or rma_line.name, 'reference': False, # 'account_id': account.id, 'journal_id': journal.id, - 'partner_id': order.partner_id.id, - 'currency_id': order.partner_id.company_id.currency_id.id, + 'currency_id': rma_line.partner_id.company_id.currency_id.id, 'payment_term_id': False, '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( - ['|', ('user_id', '=', self.env.uid), - ('member_ids', '=', self.env.uid)], limit=1) - team_id = team_ids[0] if team_ids else False - if team_id: - values['team_id'] = team_id.id - if order.type == 'customer': + if self.env.registry.models.get('crm.team', False): + team_ids = self.env['crm.team'].search( + ['|', ('user_id', '=', self.env.uid), + ('member_ids', '=', self.env.uid)], limit=1) + team_id = team_ids[0] if team_ids else False + if team_id: + values['team_id'] = team_id.id + if rma_line.type == 'customer': values['type'] = 'out_refund' else: 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 @api.constrains('item_ids') @@ -175,8 +168,8 @@ class RmaRefund(models.TransientModel): def check_unique_invoice_address_id(self): addresses = self.item_ids.mapped('invoice_address_id') if len(addresses) > 1: - raise exceptions.ValidationError('The invoice address must be the ' - 'same for all the lines') + raise ValidationError(_( + "The invoice address must be the same for all the lines.")) return True