Skip to content

Commit

Permalink
[IMP]add rma lines to invoices as it is done with PO
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronHForgeFlow committed May 25, 2018
1 parent 89a2f5c commit a107228
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
51 changes: 51 additions & 0 deletions rma_account/models/invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

from odoo import api, fields, models
from odoo.tools.float_utils import float_compare


class AccountInvoice(models.Model):
Expand All @@ -14,9 +15,59 @@ def _compute_rma_count(self):
rmas = self.mapped('invoice_line_ids.rma_line_ids')
inv.rma_count = len(rmas)

def _prepare_invoice_line_from_rma_line(self, line):
qty = line.qty_to_refund
if float_compare(
qty, 0.0, precision_rounding=line.uom_id.rounding) <= 0:
qty = 0.0
# Todo fill taxes from somewhere
invoice_line = self.env['account.invoice.line']
data = {
'purchase_line_id': line.id,
'name': line.name + ': '+line.name,
'origin': line.origin,
'uom_id': line.uom_id.id,
'product_id': line.product_id.id,
'account_id': invoice_line.with_context(
{'journal_id': self.journal_id.id,
'type': 'in_invoice'})._default_account(),
'price_unit': line.company_id.currency_id.with_context(
date=self.date_invoice).compute(
line.price_unit, self.currency_id, round=False),
'quantity': qty,
'discount': 0.0,
'account_analytic_id': line.analytic_account_id.id,
'rma_line_ids': [(4, line.id)],
}
return data

@api.onchange('add_rma_line_id')
def on_change_add_rma_line_id(self):
if not self.add_rma_line_id:
return {}
if not self.partner_id:
self.partner_id = self.add_rma_line_id.partner_id.id

new_line = self.env['account.invoice.line']
if self.add_rma_line_id not in (
self.invoice_line_ids.mapped('rma_line_id')):
data = self._prepare_invoice_line_from_rma_line(
self.add_rma_line_id)
new_line = new_line.new(data)
new_line._set_additional_fields(self)
self.invoice_line_ids += new_line
self.add_rma_line_id = False
return {}

rma_count = fields.Integer(
compute=_compute_rma_count, string='# of RMA')

add_rma_line_id = fields.Many2one(
comodel_name='rma.order.line',
string="Add from RMA line",
ondelete="set null",
help="Create a refund in based on an existing rma_line")

@api.multi
def action_view_rma_supplier(self):
action = self.env.ref('rma.action_rma_supplier_lines')
Expand Down
13 changes: 13 additions & 0 deletions rma_account/models/rma_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,16 @@ def action_view_refunds(self):
result['views'] = [(res and res.id or False, 'form')]
result['res_id'] = invoice_ids[0]
return result

@api.multi
def name_get(self):
res = []
if self.env.context.get('rma'):
for rma in self:
res.append((rma.id, "%s %s qty:%s" % (
rma.name,
rma.product_id.name,
rma.product_qty)))
return res
else:
return super(RmaOrderLine, self).name_get()
36 changes: 36 additions & 0 deletions rma_account/views/invoice_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,42 @@
</record>
</data>

<record id="view_invoice_supplier_rma_form" model="ir.ui.view">
<field name="name">account.invoice.supplier.rma</field>
<field name="model">account.invoice</field>
<field name="inherit_id" ref="account.invoice_supplier_form"/>
<field name="arch" type="xml">
<field name="reference" position="after">
<field name="add_rma_line_id"
context="{'rma': True}"
domain="[('type', '=', 'supplier'),
('partner_id', '=', partner_id)]"
attrs="{'readonly': [('state','not in',['draft'])],
'invisible': ['|', ('state', '=', 'done'),
('type', '=', 'in_invoice')]}" class="oe_edit_only"
options="{'no_create': True}"/>
</field>
</field>
</record>

<record id="view_invoice_customer_rma_form" model="ir.ui.view">
<field name="name">account.invoice.customer.rma</field>
<field name="model">account.invoice</field>
<field name="inherit_id" ref="account.invoice_form"/>
<field name="arch" type="xml">
<field name="payment_term_id" position="after">
<field name="add_rma_line_id"
context="{'rma': True}"
domain="[('type', '=', 'customer'),
('partner_id', '=', partner_id)]"
attrs="{'readonly': [('state','not in',['draft'])],
'invisible': ['|', ('state', '=', 'done'),
('type', '=', 'out_invoice')]}" class="oe_edit_only"
options="{'no_create': True}"/>
</field>
</field>
</record>

<record id="action_invoice_line" model="ir.actions.act_window">
<field name="name">Invoice Line</field>
<field name="res_model">account.invoice.line</field>
Expand Down

0 comments on commit a107228

Please sign in to comment.