mirror of
https://github.com/ForgeFlow/stock-rma.git
synced 2025-01-21 12:57:49 +02:00
init branch
This commit is contained in:
committed by
ahenriquez
parent
288ab924f9
commit
70aea6b24b
7
rma_account/models/__init__.py
Normal file
7
rma_account/models/__init__.py
Normal file
@@ -0,0 +1,7 @@
|
||||
# -*- 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 . import rma_order
|
||||
from . import rma_order_line
|
||||
from . import rma_operation
|
||||
from . import invoice
|
||||
87
rma_account/models/invoice.py
Normal file
87
rma_account/models/invoice.py
Normal file
@@ -0,0 +1,87 @@
|
||||
# -*- 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, fields, models
|
||||
|
||||
|
||||
class AccountInvoice(models.Model):
|
||||
|
||||
_inherit = "account.invoice"
|
||||
|
||||
@api.one
|
||||
def _compute_rma_count(self):
|
||||
rma_list = []
|
||||
for invl in self.invoice_line_ids:
|
||||
for rmal in invl.rma_line_ids:
|
||||
rma_list.append(rmal.rma_id.id)
|
||||
self.rma_count = len(list(set(rma_list)))
|
||||
|
||||
rma_count = fields.Integer(compute=_compute_rma_count,
|
||||
string='# of RMA',
|
||||
copy=False)
|
||||
|
||||
@api.multi
|
||||
def action_view_rma_supplier(self):
|
||||
action = self.env.ref('rma.action_rma_supplier')
|
||||
result = action.read()[0]
|
||||
rma_list = []
|
||||
for invl in self.invoice_line_ids:
|
||||
for rmal in invl.rma_line_ids:
|
||||
rma_list.append(rmal.rma_id.id)
|
||||
self.rma_count = len(list(set(rma_list)))
|
||||
# choose the view_mode accordingly
|
||||
if len(rma_list) != 1:
|
||||
result['domain'] = "[('id', 'in', " + \
|
||||
str(rma_list) + ")]"
|
||||
elif len(rma_list) == 1:
|
||||
res = self.env.ref('rma.view_rma_supplier_form', False)
|
||||
result['views'] = [(res and res.id or False, 'form')]
|
||||
result['res_id'] = rma_list[0]
|
||||
return result
|
||||
|
||||
@api.multi
|
||||
def action_view_rma(self):
|
||||
action = self.env.ref('rma.action_rma_customer')
|
||||
result = action.read()[0]
|
||||
rma_list = []
|
||||
for invl in self.invoice_line_ids:
|
||||
for rmal in invl.rma_line_ids:
|
||||
rma_list.append(rmal.rma_id.id)
|
||||
self.rma_count = len(list(set(rma_list)))
|
||||
# choose the view_mode accordingly
|
||||
if len(rma_list) != 1:
|
||||
result['domain'] = "[('id', 'in', " + \
|
||||
str(rma_list) + ")]"
|
||||
elif len(rma_list) == 1:
|
||||
res = self.env.ref('rma.view_rma_form', False)
|
||||
result['views'] = [(res and res.id or False, 'form')]
|
||||
result['res_id'] = rma_list[0]
|
||||
return result
|
||||
|
||||
|
||||
class AccountInvoiceLine(models.Model):
|
||||
|
||||
_inherit = "account.invoice.line"
|
||||
|
||||
@api.multi
|
||||
def _compute_rma_count(self):
|
||||
rma_list = []
|
||||
for invl in self:
|
||||
for rmal in invl.rma_line_ids:
|
||||
rma_list.append(rmal.rma_id.id)
|
||||
invl.rma_count = len(list(set(rma_list)))
|
||||
|
||||
rma_count = fields.Integer(compute=_compute_rma_count,
|
||||
string='# of RMA',
|
||||
copy=False)
|
||||
rma_line_ids = fields.One2many(
|
||||
comodel_name='rma.order.line', inverse_name='invoice_line_id',
|
||||
string="RMA", readonly=True,
|
||||
help="This will contain the RMA lines for the invoice line")
|
||||
|
||||
rma_line_id = fields.Many2one(
|
||||
comodel_name='rma.order.line',
|
||||
string="RMA line refund",
|
||||
ondelete="set null",
|
||||
help="This will contain the rma line that originated the refund line")
|
||||
13
rma_account/models/rma_operation.py
Normal file
13
rma_account/models/rma_operation.py
Normal file
@@ -0,0 +1,13 @@
|
||||
# -*- 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, fields, models
|
||||
|
||||
|
||||
class RmaOperation(models.Model):
|
||||
_inherit = 'rma.operation'
|
||||
|
||||
refund_policy = fields.Selection([
|
||||
('no', 'No refund'), ('ordered', 'Based on Ordered Quantities'),
|
||||
('received', 'Based on Received Quantities')], string="Refund Policy",
|
||||
default='no')
|
||||
145
rma_account/models/rma_order.py
Normal file
145
rma_account/models/rma_order.py
Normal file
@@ -0,0 +1,145 @@
|
||||
# -*- 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, fields, models
|
||||
from openerp.addons import decimal_precision as dp
|
||||
from openerp.exceptions import UserError
|
||||
from dateutil.relativedelta import relativedelta
|
||||
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
class RmaOrder(models.Model):
|
||||
_inherit = "rma.order"
|
||||
|
||||
@api.multi
|
||||
def _compute_invoice_refund_count(self):
|
||||
self.ensure_one()
|
||||
invoice_list = []
|
||||
for line in self.rma_line_ids:
|
||||
for refund in line.refund_line_ids:
|
||||
invoice_list.append(refund.invoice_id.id)
|
||||
self.invoice_refund_count = len(list(set(invoice_list)))
|
||||
|
||||
@api.multi
|
||||
def _compute_invoice_count(self):
|
||||
self.ensure_one()
|
||||
invoice_list = []
|
||||
for line in self.rma_line_ids:
|
||||
if line.invoice_line_id and line.invoice_line_id.id:
|
||||
invoice_list.append(line.invoice_line_id.invoice_id.id)
|
||||
self.invoice_count = len(list(set(invoice_list)))
|
||||
|
||||
add_invoice_id = fields.Many2one('account.invoice', string='Add Invoice',
|
||||
ondelete='set null', readonly=True,
|
||||
states={'draft': [('readonly', False)]})
|
||||
invoice_refund_count = fields.Integer(
|
||||
compute=_compute_invoice_refund_count,
|
||||
string='# of Refunds',
|
||||
copy=False)
|
||||
invoice_count = fields.Integer(compute=_compute_invoice_count,
|
||||
string='# of Incoming Shipments',
|
||||
copy=False)
|
||||
|
||||
def _prepare_rma_line_from_inv_line(self, line):
|
||||
operation = line.product_id.rma_operation_id and \
|
||||
line.product_id.rma_operation_id.id or False
|
||||
if not operation:
|
||||
operation = line.product_id.categ_id.rma_operation_id and \
|
||||
line.product_id.categ_id.rma_operation_id.id or False
|
||||
data = {
|
||||
'invoice_line_id': line.id,
|
||||
'product_id': line.product_id.id,
|
||||
'name': line.name,
|
||||
'origin': line.invoice_id.number,
|
||||
'uom_id': line.uom_id.id,
|
||||
'operation_id': operation,
|
||||
'product_qty': line.quantity,
|
||||
'price_unit': line.invoice_id.currency_id.compute(
|
||||
line.price_unit, line.currency_id, round=False),
|
||||
'rma_id': self._origin.id
|
||||
}
|
||||
return data
|
||||
|
||||
@api.onchange('add_invoice_id')
|
||||
def on_change_invoice(self):
|
||||
if not self.add_invoice_id:
|
||||
return {}
|
||||
if not self.partner_id:
|
||||
self.partner_id = self.add_invoice_id.partner_id.id
|
||||
new_lines = self.env['rma.order.line']
|
||||
for line in self.add_invoice_id.invoice_line_ids:
|
||||
# Load a PO line only once
|
||||
if line in self.rma_line_ids.mapped('invoice_line_id'):
|
||||
continue
|
||||
data = self._prepare_rma_line_from_inv_line(line)
|
||||
new_line = new_lines.new(data)
|
||||
new_lines += new_line
|
||||
|
||||
self.rma_line_ids += new_lines
|
||||
self.date_rma = fields.Datetime.now()
|
||||
self.delivery_address_id = self.add_invoice_id.partner_id.id
|
||||
self.invoice_address_id = self.add_invoice_id.partner_id.id
|
||||
self.add_invoice_id = False
|
||||
return {}
|
||||
|
||||
@api.model
|
||||
def prepare_rma_line(self, origin_rma, rma_id, line):
|
||||
line_values = super(RmaOrder, self).prepare_rma_line(
|
||||
origin_rma, rma_id, line)
|
||||
line_values['invoice_address_id'] = line.invoice_address_id.id
|
||||
return line_values
|
||||
|
||||
@api.model
|
||||
def _prepare_rma_data(self, partner, origin_rma):
|
||||
res = super(RmaOrder, self)._prepare_rma_data(partner, origin_rma)
|
||||
res['invoice_address_id'] = partner.id
|
||||
return res
|
||||
|
||||
@api.multi
|
||||
def action_view_invoice_refund(self):
|
||||
"""
|
||||
This function returns an action that display existing vendor refund
|
||||
bills of given purchase order id.
|
||||
When only one found, show the vendor bill immediately.
|
||||
"""
|
||||
action = self.env.ref('account.action_invoice_tree2')
|
||||
result = action.read()[0]
|
||||
invoice_list = []
|
||||
for line in self.rma_line_ids:
|
||||
for refund in line.refund_line_ids:
|
||||
invoice_list.append(refund.invoice_id.id)
|
||||
invoice_ids = list(set(invoice_list))
|
||||
# choose the view_mode accordingly
|
||||
if len(invoice_ids) != 1:
|
||||
result['domain'] = "[('id', 'in', " + \
|
||||
str(invoice_ids) + ")]"
|
||||
elif len(invoice_ids) == 1:
|
||||
res = self.env.ref('account.invoice_supplier_form', False)
|
||||
result['views'] = [(res and res.id or False, 'form')]
|
||||
result['res_id'] = invoice_ids[0]
|
||||
return result
|
||||
|
||||
@api.multi
|
||||
def action_view_invoice(self):
|
||||
if self.type == "supplier":
|
||||
action = self.env.ref('account.action_invoice_tree2')
|
||||
else:
|
||||
action = self.env.ref('account.action_invoice_tree')
|
||||
result = action.read()[0]
|
||||
invoice_list = []
|
||||
for line in self.rma_line_ids:
|
||||
invoice_list.append(line.invoice_id.id)
|
||||
invoice_ids = list(set(invoice_list))
|
||||
# choose the view_mode accordingly
|
||||
if len(invoice_ids) != 1:
|
||||
result['domain'] = "[('id', 'in', " + \
|
||||
str(invoice_ids) + ")]"
|
||||
elif len(invoice_ids) == 1:
|
||||
if self.type == "supplier":
|
||||
res = self.env.ref('account.invoice_supplier_form', False)
|
||||
else:
|
||||
res = self.env.ref('account.invoice_form', False)
|
||||
result['views'] = [(res and res.id or False, 'form')]
|
||||
result['res_id'] = invoice_ids[0]
|
||||
return result
|
||||
132
rma_account/models/rma_order_line.py
Normal file
132
rma_account/models/rma_order_line.py
Normal file
@@ -0,0 +1,132 @@
|
||||
# -*- 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, fields, models
|
||||
from openerp.addons import decimal_precision as dp
|
||||
from openerp.exceptions import UserError
|
||||
|
||||
|
||||
class RmaOrderLine(models.Model):
|
||||
_inherit = "rma.order.line"
|
||||
|
||||
@api.model
|
||||
def _default_invoice_address(self):
|
||||
partner_id = self.env.context.get('partner_id', False)
|
||||
if partner_id:
|
||||
return self.env['res.partner'].browse(partner_id)
|
||||
return False
|
||||
|
||||
@api.multi
|
||||
@api.depends('refund_line_ids', 'refund_line_ids.invoice_id.state',
|
||||
'refund_policy', 'type')
|
||||
def _compute_qty_refunded(self):
|
||||
for rec in self:
|
||||
rec.qty_refunded = sum(rec.refund_line_ids.filtered(
|
||||
lambda i: i.invoice_id.state in ('open', 'paid')).mapped(
|
||||
'quantity'))
|
||||
|
||||
@api.one
|
||||
@api.depends('refund_line_ids', 'refund_line_ids.invoice_id.state',
|
||||
'refund_policy', 'move_ids', 'move_ids.state', 'type')
|
||||
def _compute_qty_to_refund(self):
|
||||
qty = 0.0
|
||||
if self.refund_policy == 'ordered':
|
||||
qty = self.product_qty - self.qty_refunded
|
||||
elif self.refund_policy == 'received':
|
||||
qty = self.qty_received - self.qty_refunded
|
||||
self.qty_to_refund = qty
|
||||
|
||||
@api.multi
|
||||
def _compute_refund_count(self):
|
||||
for rec in self:
|
||||
rec.refund_count = len(rec.refund_line_ids.mapped('invoice_id'))
|
||||
|
||||
invoice_address_id = fields.Many2one(
|
||||
'res.partner', string='Partner invoice address',
|
||||
default=_default_invoice_address,
|
||||
help="Invoice address for current rma order.")
|
||||
|
||||
refund_count = fields.Integer(compute=_compute_refund_count,
|
||||
string='# of Refunds', copy=False, default=0)
|
||||
|
||||
invoice_line_id = fields.Many2one('account.invoice.line',
|
||||
string='Invoice Line',
|
||||
ondelete='restrict',
|
||||
index=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)
|
||||
refund_policy = fields.Selection([
|
||||
('no', 'No refund'), ('ordered', 'Based on Ordered Quantities'),
|
||||
('received', 'Based on Received Quantities')], string="Refund Policy",
|
||||
required=True, default='no')
|
||||
qty_to_refund = fields.Float(
|
||||
string='Qty To Refund', copy=False,
|
||||
digits=dp.get_precision('Product Unit of Measure'), readonly=True,
|
||||
compute=_compute_qty_to_refund, store=True)
|
||||
qty_refunded = fields.Float(
|
||||
string='Qty Refunded', copy=False,
|
||||
digits=dp.get_precision('Product Unit of Measure'),
|
||||
readonly=True, compute=_compute_qty_refunded, store=True)
|
||||
|
||||
@api.onchange('operation_id')
|
||||
def _onchange_operation_id(self):
|
||||
result = super(RmaOrderLine, self)._onchange_operation_id()
|
||||
if not self.operation_id:
|
||||
return result
|
||||
self.refund_policy = self.operation_id.refund_policy
|
||||
return result
|
||||
|
||||
@api.onchange('invoice_line_id')
|
||||
def _onchange_invoice_line_id(self):
|
||||
result = {}
|
||||
if not self.invoice_line_id:
|
||||
return result
|
||||
self.origin = self.invoice_id.number
|
||||
return result
|
||||
|
||||
@api.multi
|
||||
@api.constrains('invoice_line_id')
|
||||
def _check_duplicated_lines(self):
|
||||
for line in self:
|
||||
matching_inv_lines = self.env['account.invoice.line'].search([(
|
||||
'id', '=', line.invoice_line_id.id)])
|
||||
if len(matching_inv_lines) > 1:
|
||||
raise UserError(
|
||||
_("There's an rma for the invoice line %s "
|
||||
"and invoice %s" %
|
||||
(line.invoice_line_id,
|
||||
line.invoice_line_id.invoice_id)))
|
||||
return {}
|
||||
|
||||
@api.multi
|
||||
def action_view_invoice(self):
|
||||
action = self.env.ref('account.action_invoice_tree')
|
||||
result = action.read()[0]
|
||||
res = self.env.ref('account.invoice_form', False)
|
||||
result['views'] = [(res and res.id or False, 'form')]
|
||||
result['view_id'] = res and res.id or False
|
||||
result['res_id'] = self.invoice_line_id.invoice_id.id
|
||||
|
||||
return result
|
||||
|
||||
@api.multi
|
||||
def action_view_refunds(self):
|
||||
action = self.env.ref('account.action_invoice_tree2')
|
||||
result = action.read()[0]
|
||||
invoice_ids = []
|
||||
for inv_line in self.refund_line_ids:
|
||||
invoice_ids.append(inv_line.invoice_id.id)
|
||||
# choose the view_mode accordingly
|
||||
if len(invoice_ids) != 1:
|
||||
result['domain'] = "[('id', 'in', " + \
|
||||
str(invoice_ids) + ")]"
|
||||
elif len(invoice_ids) == 1:
|
||||
res = self.env.ref('account.invoice_supplier_form', False)
|
||||
result['views'] = [(res and res.id or False, 'form')]
|
||||
result['res_id'] = invoice_ids[0]
|
||||
return result
|
||||
Reference in New Issue
Block a user