[IMP] rma_sale: black, isort, prettier

This commit is contained in:
Ernesto Tejeda
2020-10-28 15:32:43 -04:00
parent 51df338cd8
commit 054b291031
15 changed files with 422 additions and 324 deletions

View File

@@ -10,10 +10,7 @@
"author": "Tecnativa, Odoo Community Association (OCA)",
"maintainers": ["ernestotejeda"],
"license": "AGPL-3",
"depends": [
"rma",
"sale_stock",
],
"depends": ["rma", "sale_stock",],
"data": [
"views/assets.xml",
"views/report_rma.xml",

View File

@@ -5,9 +5,8 @@ from odoo.addons.rma.controllers.main import PortalRma
class PortalRma(PortalRma):
def _get_filter_domain(self, kw):
res = super()._get_filter_domain(kw)
if 'sale_id' in kw:
res.append(('order_id', '=', int(kw['sale_id'])))
if "sale_id" in kw:
res.append(("order_id", "=", int(kw["sale_id"])))
return res

View File

@@ -1,42 +1,48 @@
# Copyright 2020 Tecnativa - Ernesto Tejeda
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import http, _
from odoo import _, http
from odoo.exceptions import AccessError, MissingError
from odoo.http import request
from odoo.addons.sale.controllers.portal import CustomerPortal
class CustomerPortal(CustomerPortal):
@http.route(['/my/orders/<int:order_id>/requestrma'], type='http',
auth="public", methods=['POST'], website=True)
@http.route(
["/my/orders/<int:order_id>/requestrma"],
type="http",
auth="public",
methods=["POST"],
website=True,
)
def request_rma(self, order_id, access_token=None, **post):
try:
order_sudo = self._document_check_access('sale.order', order_id,
access_token=access_token)
order_sudo = self._document_check_access(
"sale.order", order_id, access_token=access_token
)
except (AccessError, MissingError):
return request.redirect('/my')
order_obj = request.env['sale.order']
wizard_obj = request.env['sale.order.rma.wizard']
return request.redirect("/my")
order_obj = request.env["sale.order"]
wizard_obj = request.env["sale.order.rma.wizard"]
# Set wizard line vals
mapped_vals = {}
for name, value in post.items():
row, field_name = name.split('-', 1)
row, field_name = name.split("-", 1)
mapped_vals.setdefault(row, {}).update({field_name: value})
# If no operation is filled, no RMA will be created
line_vals = [
(0, 0, vals) for vals in mapped_vals.values()
if vals.get("operation_id")]
(0, 0, vals) for vals in mapped_vals.values() if vals.get("operation_id")
]
# Create wizard an generate rmas
order = order_obj.browse(order_id).sudo()
location_id = order.warehouse_id.rma_loc_id.id
wizard = wizard_obj.with_context(active_id=order_id).create({
'line_ids': line_vals,
'location_id': location_id
})
wizard = wizard_obj.with_context(active_id=order_id).create(
{"line_ids": line_vals, "location_id": location_id}
)
rma = wizard.sudo().create_rma()
for rec in rma:
rec.origin += _(' (Portal)')
rec.origin += _(" (Portal)")
# Add the user as follower of the created RMAs so they can
# later view them.
rma.message_subscribe([request.env.user.partner_id.id])

View File

@@ -8,69 +8,64 @@ class Rma(models.Model):
_inherit = "rma"
order_id = fields.Many2one(
comodel_name='sale.order',
string='Sale Order',
comodel_name="sale.order",
string="Sale Order",
domain="["
" ('partner_id', 'child_of', commercial_partner_id),"
" ('state', 'in', ['sale', 'done']),"
"]",
" ('partner_id', 'child_of', commercial_partner_id),"
" ('state', 'in', ['sale', 'done']),"
"]",
readonly=True,
states={'draft': [('readonly', False)]},
states={"draft": [("readonly", False)]},
)
allowed_picking_ids = fields.Many2many(
comodel_name='stock.picking',
compute="_compute_allowed_picking_ids",
)
picking_id = fields.Many2one(
domain="[('id', 'in', allowed_picking_ids)]",
comodel_name="stock.picking", compute="_compute_allowed_picking_ids",
)
picking_id = fields.Many2one(domain="[('id', 'in', allowed_picking_ids)]",)
allowed_move_ids = fields.Many2many(
comodel_name='sale.order.line',
compute="_compute_allowed_move_ids",
)
move_id = fields.Many2one(
domain="[('id', 'in', allowed_move_ids)]",
comodel_name="sale.order.line", compute="_compute_allowed_move_ids",
)
move_id = fields.Many2one(domain="[('id', 'in', allowed_move_ids)]",)
allowed_product_ids = fields.Many2many(
comodel_name='product.product',
compute="_compute_allowed_product_ids",
)
product_id = fields.Many2one(
domain="[('id', 'in', allowed_product_ids)]",
comodel_name="product.product", compute="_compute_allowed_product_ids",
)
product_id = fields.Many2one(domain="[('id', 'in', allowed_product_ids)]",)
@api.depends('partner_id', 'order_id')
@api.depends("partner_id", "order_id")
def _compute_allowed_picking_ids(self):
domain = [('state', '=', 'done'),
('picking_type_id.code', '=', 'outgoing')]
domain = [("state", "=", "done"), ("picking_type_id.code", "=", "outgoing")]
for rec in self:
# if rec.partner_id:
commercial_partner = rec.partner_id.commercial_partner_id
domain.append(('partner_id', 'child_of', commercial_partner.id))
domain.append(("partner_id", "child_of", commercial_partner.id))
if rec.order_id:
domain.append(('sale_id', '=', rec.order_id.id))
rec.allowed_picking_ids = self.env['stock.picking'].search(domain)
domain.append(("sale_id", "=", rec.order_id.id))
rec.allowed_picking_ids = self.env["stock.picking"].search(domain)
@api.depends('order_id', 'picking_id')
@api.depends("order_id", "picking_id")
def _compute_allowed_move_ids(self):
for rec in self:
if rec.order_id:
order_move = rec.order_id.order_line.mapped('move_ids')
order_move = rec.order_id.order_line.mapped("move_ids")
rec.allowed_move_ids = order_move.filtered(
lambda r: r.picking_id == self.picking_id).ids
lambda r: r.picking_id == self.picking_id
).ids
else:
rec.allowed_move_ids = self.picking_id.move_lines.ids
@api.depends('order_id')
@api.depends("order_id")
def _compute_allowed_product_ids(self):
for rec in self:
if rec.order_id:
order_product = rec.order_id.order_line.mapped('product_id')
order_product = rec.order_id.order_line.mapped("product_id")
rec.allowed_product_ids = order_product.filtered(
lambda r: r.type in ['consu', 'product']).ids
lambda r: r.type in ["consu", "product"]
).ids
else:
rec.allowed_product_ids = self.env['product.product'].search(
[('type', 'in', ['consu', 'product'])]).ids
rec.allowed_product_ids = (
self.env["product.product"]
.search([("type", "in", ["consu", "product"])])
.ids
)
@api.onchange("partner_id")
def _onchange_partner_id(self):
@@ -78,7 +73,7 @@ class Rma(models.Model):
self.order_id = False
return res
@api.onchange('order_id')
@api.onchange("order_id")
def _onchange_order_id(self):
self.product_id = self.picking_id = False

View File

@@ -10,64 +10,63 @@ class SaleOrder(models.Model):
# RMAs that were created from a sale order
rma_ids = fields.One2many(
comodel_name='rma',
inverse_name='order_id',
string='RMAs',
copy=False,
)
rma_count = fields.Integer(
string='RMA count',
compute='_compute_rma_count',
comodel_name="rma", inverse_name="order_id", string="RMAs", copy=False,
)
rma_count = fields.Integer(string="RMA count", compute="_compute_rma_count",)
def _compute_rma_count(self):
rma_data = self.env['rma'].read_group(
[('order_id', 'in', self.ids)], ['order_id'], ['order_id'])
mapped_data = dict(
[(r['order_id'][0], r['order_id_count']) for r in rma_data])
rma_data = self.env["rma"].read_group(
[("order_id", "in", self.ids)], ["order_id"], ["order_id"]
)
mapped_data = {r["order_id"][0]: r["order_id_count"] for r in rma_data}
for record in self:
record.rma_count = mapped_data.get(record.id, 0)
def action_create_rma(self):
self.ensure_one()
if self.state not in ['sale', 'done']:
raise ValidationError(_("You may only create RMAs from a "
"confirmed or done sale order."))
wizard_obj = self.env['sale.order.rma.wizard']
line_vals = [(0, 0, {
'product_id': data['product'].id,
'quantity': data['quantity'],
'uom_id': data['uom'].id,
'picking_id': data['picking'] and data['picking'].id,
}) for data in self.get_delivery_rma_data()]
wizard = wizard_obj.with_context(active_id=self.id).create({
'line_ids': line_vals,
'location_id': self.warehouse_id.rma_loc_id.id
})
if self.state not in ["sale", "done"]:
raise ValidationError(
_("You may only create RMAs from a " "confirmed or done sale order.")
)
wizard_obj = self.env["sale.order.rma.wizard"]
line_vals = [
(
0,
0,
{
"product_id": data["product"].id,
"quantity": data["quantity"],
"uom_id": data["uom"].id,
"picking_id": data["picking"] and data["picking"].id,
},
)
for data in self.get_delivery_rma_data()
]
wizard = wizard_obj.with_context(active_id=self.id).create(
{"line_ids": line_vals, "location_id": self.warehouse_id.rma_loc_id.id}
)
return {
'name': _('Create RMA'),
'type': 'ir.actions.act_window',
'view_type': 'form',
'view_mode': 'form',
'res_model': 'sale.order.rma.wizard',
'res_id': wizard.id,
'target': 'new',
"name": _("Create RMA"),
"type": "ir.actions.act_window",
"view_type": "form",
"view_mode": "form",
"res_model": "sale.order.rma.wizard",
"res_id": wizard.id,
"target": "new",
}
def action_view_rma(self):
self.ensure_one()
action = self.env.ref('rma.rma_action').read()[0]
action = self.env.ref("rma.rma_action").read()[0]
rma = self.rma_ids
if len(rma) == 1:
action.update(
res_id=rma.id,
view_mode="form",
views=[],
res_id=rma.id, view_mode="form", views=[],
)
else:
action['domain'] = [('id', 'in', rma.ids)]
action["domain"] = [("id", "in", rma.ids)]
# reset context to show all related rma without default filters
action['context'] = {}
action["context"] = {}
return action
def get_delivery_rma_data(self):
@@ -83,19 +82,23 @@ class SaleOrderLine(models.Model):
def get_delivery_move(self):
self.ensure_one()
return self.move_ids.filtered(lambda r: (
self.product_id == r.product_id
and r.state == 'done'
and not r.scrapped
and r.location_dest_id.usage == "customer"
and (not r.origin_returned_move_id
or (r.origin_returned_move_id and r.to_refund))
))
return self.move_ids.filtered(
lambda r: (
self.product_id == r.product_id
and r.state == "done"
and not r.scrapped
and r.location_dest_id.usage == "customer"
and (
not r.origin_returned_move_id
or (r.origin_returned_move_id and r.to_refund)
)
)
)
def prepare_sale_rma_data(self):
self.ensure_one()
product = self.product_id
if self.product_id.type != 'product':
if self.product_id.type != "product":
return {}
moves = self.get_delivery_move()
data = []
@@ -103,20 +106,24 @@ class SaleOrderLine(models.Model):
for move in moves:
qty = move.product_uom_qty
move_dest = move.move_dest_ids.filtered(
lambda r: r.state in ['partially_available',
'assigned', 'done'])
qty -= sum(move_dest.mapped('product_uom_qty'))
data.append({
'product': product,
'quantity': qty,
'uom': move.product_uom,
'picking': move.picking_id,
})
lambda r: r.state in ["partially_available", "assigned", "done"]
)
qty -= sum(move_dest.mapped("product_uom_qty"))
data.append(
{
"product": product,
"quantity": qty,
"uom": move.product_uom,
"picking": move.picking_id,
}
)
else:
data.append({
'product': product,
'quantity': self.qty_delivered,
'uom': self.product_uom,
'picking': False,
})
data.append(
{
"product": product,
"quantity": self.qty_delivered,
"uom": self.product_uom,
"picking": False,
}
)
return data

View File

@@ -8,21 +8,17 @@ class TestRmaSale(SavepointCase):
@classmethod
def setUpClass(cls):
super(TestRmaSale, cls).setUpClass()
cls.res_partner = cls.env['res.partner']
cls.product_product = cls.env['product.product']
cls.sale_order = cls.env['sale.order']
cls.res_partner = cls.env["res.partner"]
cls.product_product = cls.env["product.product"]
cls.sale_order = cls.env["sale.order"]
cls.product_1 = cls.product_product.create({
'name': 'Product test 1',
'type': 'product',
})
cls.product_2 = cls.product_product.create({
'name': 'Product test 2',
'type': 'product',
})
cls.partner = cls.res_partner.create({
'name': 'Partner test',
})
cls.product_1 = cls.product_product.create(
{"name": "Product test 1", "type": "product",}
)
cls.product_2 = cls.product_product.create(
{"name": "Product test 2", "type": "product",}
)
cls.partner = cls.res_partner.create({"name": "Partner test",})
order_form = Form(cls.sale_order)
order_form.partner_id = cls.partner
with order_form.order_line.new() as line_form:
@@ -33,13 +29,14 @@ class TestRmaSale(SavepointCase):
# Maybe other modules create additional lines in the create
# method in sale.order model, so let's find the correct line.
cls.order_line = cls.sale_order.order_line.filtered(
lambda r: r.product_id == cls.product_1)
lambda r: r.product_id == cls.product_1
)
cls.order_out_picking = cls.sale_order.picking_ids
cls.order_out_picking.move_lines.quantity_done = 5
cls.order_out_picking.button_validate()
def test_create_rma_with_so(self):
rma_form = Form(self.env['rma'])
rma_form = Form(self.env["rma"])
rma_form.partner_id = self.partner
rma_form.order_id = self.sale_order
rma_form.product_id = self.product_1
@@ -51,9 +48,9 @@ class TestRmaSale(SavepointCase):
def test_create_rma_from_so(self):
order = self.sale_order
wizard_id = order.action_create_rma()['res_id']
wizard = self.env['sale.order.rma.wizard'].browse(wizard_id)
rma = self.env['rma'].browse(wizard.create_and_open_rma()['res_id'])
wizard_id = order.action_create_rma()["res_id"]
wizard = self.env["sale.order.rma.wizard"].browse(wizard_id)
rma = self.env["rma"].browse(wizard.create_and_open_rma()["res_id"])
self.assertEqual(rma.partner_id, order.partner_id)
self.assertEqual(rma.order_id, order)
self.assertEqual(rma.picking_id, self.order_out_picking)
@@ -61,7 +58,7 @@ class TestRmaSale(SavepointCase):
self.assertEqual(rma.product_id, self.product_1)
self.assertEqual(rma.product_uom_qty, self.order_line.product_uom_qty)
self.assertEqual(rma.product_uom, self.order_line.product_uom)
self.assertEqual(rma.state, 'confirmed')
self.assertEqual(rma.state, "confirmed")
self.assertEqual(
rma.reception_move_id.origin_returned_move_id,
self.order_out_picking.move_lines,
@@ -72,10 +69,7 @@ class TestRmaSale(SavepointCase):
)
# Refund the RMA
user = self.env["res.users"].create(
{
"login": "test_refund_with_so",
"name": "Test",
}
{"login": "test_refund_with_so", "name": "Test",}
)
order.user_id = user.id
rma.action_confirm()

View File

@@ -1,8 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<template id="assets_frontend" inherit_id="web.assets_frontend" name="Request RMA">
<xpath expr="//link[last()]" position="after">
<link rel="stylesheet" type="text/scss" href="/rma_sale/static/src/scss/rma_sale.scss"/>
<link
rel="stylesheet"
type="text/scss"
href="/rma_sale/static/src/scss/rma_sale.scss"
/>
</xpath>
</template>
</odoo>

View File

@@ -4,13 +4,13 @@
<xpath expr="//div[@t-if='doc.picking_id']" position="before">
<div t-if="doc.order_id" class="col-auto mw-100 mb-2">
<strong>Sale order:</strong>
<p class="m-0" t-field="doc.order_id"/>
<p class="m-0" t-field="doc.order_id" />
</div>
</xpath>
<xpath expr="//div[@t-if='doc.user_id']" position="before">
<div t-if="doc.operation_id" class="col-auto mw-100 mb-2">
<strong>Requested operation:</strong>
<p class="m-0" t-field="doc.operation_id"/>
<p class="m-0" t-field="doc.operation_id" />
</div>
</xpath>
</template>

View File

@@ -1,19 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2020 Tecnativa - Ernesto Tejeda
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
<odoo>
<record id="rma_view_form" model="ir.ui.view">
<field name="name">rma.view.form</field>
<field name="model">rma</field>
<field name="inherit_id" ref="rma.rma_view_form"/>
<field name="inherit_id" ref="rma.rma_view_form" />
<field name="arch" type="xml">
<field name="partner_invoice_id" position="after">
<field name="order_id" options="{'no_create': True}"/>
<field name="order_id" options="{'no_create': True}" />
</field>
<sheet>
<field name="allowed_picking_ids" invisible="1"/>
<field name="allowed_move_ids" invisible="1"/>
<field name="allowed_product_ids" invisible="1"/>
<field name="allowed_picking_ids" invisible="1" />
<field name="allowed_move_ids" invisible="1" />
<field name="allowed_product_ids" invisible="1" />
</sheet>
</field>
</record>

View File

@@ -1,37 +1,80 @@
<odoo>
<template id="sale_order_portal_template" name="Request RMA" inherit_id="sale.sale_order_portal_template">
<xpath expr="//div[hasclass('o_portal_sale_sidebar')]//div[hasclass('o_download_pdf')]/.." position="after">
<li t-if="sale_order.state in ['sale', 'done']" class="list-group-item flex-grow-1" id="li-request-rma">
<a role="button" class="btn btn-secondary btn-block mb8" data-toggle="modal" data-target="#modal-request-rma" href="#">
<i class="fa fa-reply"/> Request RMAs
<template
id="sale_order_portal_template"
name="Request RMA"
inherit_id="sale.sale_order_portal_template"
>
<xpath
expr="//div[hasclass('o_portal_sale_sidebar')]//div[hasclass('o_download_pdf')]/.."
position="after"
>
<li
t-if="sale_order.state in ['sale', 'done']"
class="list-group-item flex-grow-1"
id="li-request-rma"
>
<a
role="button"
class="btn btn-secondary btn-block mb8"
data-toggle="modal"
data-target="#modal-request-rma"
href="#"
>
<i class="fa fa-reply" /> Request RMAs
</a>
</li>
</xpath>
<xpath expr="//div[@id='modaldecline']" position="after">
<div role="dialog" class="modal fade" id="modal-request-rma">
<div class="modal-dialog" style="max-width: 1000px;">
<form id="form-request-rma" method="POST" t-attf-action="/my/orders/#{sale_order.id}/requestrma?access_token=#{sale_order.access_token}" class="modal-content">
<input type="hidden" name="csrf_token" t-att-value="request.csrf_token()"/>
<form
id="form-request-rma"
method="POST"
t-attf-action="/my/orders/#{sale_order.id}/requestrma?access_token=#{sale_order.access_token}"
class="modal-content"
>
<input
type="hidden"
name="csrf_token"
t-att-value="request.csrf_token()"
/>
<header class="modal-header">
<h4 class="modal-title">Request RMAs</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">&amp;times;</button>
<button
type="button"
class="close"
data-dismiss="modal"
aria-label="Close"
>&amp;times;</button>
</header>
<main class="modal-body" id="modal-body-request-rma">
<div class="alert alert-info mb-2 mb-sm-1" role="alert">
<span>
You're about to perform an RMA request. Our team will process it an will reach you once it's validated. Keep in mind that:
<ul>
<li>Select the product quantity and the requested operation</li>
<li>Use the comment button to add relevant information regarding the RMA, like returned serial numbers or a description of the issue</li>
<li>If no requested operation is set, the RMA won't be correctly fulfilled</li>
<li>You can only return as much product units as you received for this order</li>
<li>The limit will decrease when the units in other RMAs are confirmed</li>
<li>You can send a message in every RMA sent</li>
</ul>
<li
>Select the product quantity and the requested operation</li>
<li
>Use the comment button to add relevant information regarding the RMA, like returned serial numbers or a description of the issue</li>
<li
>If no requested operation is set, the RMA won't be correctly fulfilled</li>
<li
>You can only return as much product units as you received for this order</li>
<li
>The limit will decrease when the units in other RMAs are confirmed</li>
<li
>You can send a message in every RMA sent</li>
</ul>
</span>
</div>
<t t-set="data_list" t-value="sale_order.get_delivery_rma_data()"/>
<t t-set="operations" t-value="sale_order.env['rma.operation'].search([])"/>
<t
t-set="data_list"
t-value="sale_order.get_delivery_rma_data()"
/>
<t
t-set="operations"
t-value="sale_order.env['rma.operation'].search([])"
/>
<table class="table table-sm" id="request-rma-table">
<thead class="bg-100">
<tr>
@@ -47,40 +90,65 @@
<t t-if="data['quantity'] > 0">
<tr>
<td class="text-left">
<span t-esc="data['product'].display_name"/>
<input type="hidden"
t-attf-name="#{data_index}-product_id"
t-att-value="data['product'].id"/>
<span
t-esc="data['product'].display_name"
/>
<input
type="hidden"
t-attf-name="#{data_index}-product_id"
t-att-value="data['product'].id"
/>
</td>
<td class="text-right">
<div id="delivery-rma-qty">
<input type="number"
t-attf-name="#{data_index}-quantity"
class="o_input text-right"
placeholder="0"
min="0"
t-att-max="data['quantity']"
t-att-value="0"
style="max-width: 60px;"/>
<span t-esc="data['uom'].name" groups="uom.group_uom"/>
<input type="hidden"
t-attf-name="#{data_index}-uom_id"
t-att-value="data['uom'].id"/>
<input
type="number"
t-attf-name="#{data_index}-quantity"
class="o_input text-right"
placeholder="0"
min="0"
t-att-max="data['quantity']"
t-att-value="0"
style="max-width: 60px;"
/>
<span
t-esc="data['uom'].name"
groups="uom.group_uom"
/>
<input
type="hidden"
t-attf-name="#{data_index}-uom_id"
t-att-value="data['uom'].id"
/>
</div>
</td>
<td class="text-left">
<span t-if="data['picking']" t-esc="data['picking'].name"/>
<input type="hidden"
t-attf-name="#{data_index}-picking_id"
t-att-value="data['picking'].id"/>
<span
t-if="data['picking']"
t-esc="data['picking'].name"
/>
<input
type="hidden"
t-attf-name="#{data_index}-picking_id"
t-att-value="data['picking'].id"
/>
</td>
<td class="text-left">
<select t-attf-name="#{data_index}-operation_id"
class="form-control">
<select
t-attf-name="#{data_index}-operation_id"
class="form-control"
>
<option value="">---</option>
<t t-foreach="operations" t-as="operation">
<option t-att-value="operation.id">
<t t-esc="operation.name" />
<t
t-foreach="operations"
t-as="operation"
>
<option
t-att-value="operation.id"
>
<t
t-esc="operation.name"
/>
</option>
</t>
</select>
@@ -88,13 +156,18 @@
<td>
<button
class="btn btn-primary fa fa-comments"
type="button" data-toggle="collapse"
type="button"
data-toggle="collapse"
t-attf-data-target="#comment-#{data_index}"
aria-expanded="false"
t-attf-aria-controls="comment-#{data_index}" />
t-attf-aria-controls="comment-#{data_index}"
/>
</td>
</tr>
<tr class="collapse" t-attf-id="comment-#{data_index}">
<tr
class="collapse"
t-attf-id="comment-#{data_index}"
>
<td colspan="5">
<textarea
class="form-control o_website_form_input"
@@ -109,19 +182,35 @@
</table>
</main>
<footer class="modal-footer">
<button type="submit" t-att-id="sale_order.id" class="btn btn-primary"><i class="fa fa-check"></i> Request RMAs</button>
<button type="button" class="btn btn-danger" data-dismiss="modal"><i class="fa fa-times"></i> Cancel</button>
<button
type="submit"
t-att-id="sale_order.id"
class="btn btn-primary"
><i class="fa fa-check" /> Request RMAs</button>
<button
type="button"
class="btn btn-danger"
data-dismiss="modal"
><i class="fa fa-times" /> Cancel</button>
</footer>
</form>
</div>
</div>
</xpath>
</template>
<template id="sale_order_portal_content" name="Show RMAs" inherit_id="sale.sale_order_portal_content">
<template
id="sale_order_portal_content"
name="Show RMAs"
inherit_id="sale.sale_order_portal_content"
>
<xpath expr="//div[@id='introduction']/h2[hasclass('my-0')]" position="inside">
<span t-if="sale_order.rma_count" class="float-right">
<a role="button" t-attf-href="/my/rmas?sale_id=#{sale_order.id}" class="btn btn-sm btn-secondary">
<span class="fa fa-reply" role="img" aria-label="RMA" title="RMA"/>
<a
role="button"
t-attf-href="/my/rmas?sale_id=#{sale_order.id}"
class="btn btn-sm btn-secondary"
>
<span class="fa fa-reply" role="img" aria-label="RMA" title="RMA" />
<span t-esc="sale_order.rma_count" />
<span>RMA</span>
</a>
@@ -135,7 +224,7 @@
<strong>Sale order</strong>
</div>
<div class="col-12 col-sm-8">
<span t-field="rma.order_id"/>
<span t-field="rma.order_id" />
</div>
</div>
</xpath>
@@ -145,7 +234,7 @@
<strong>Requested operation</strong>
</div>
<div class="col-12 col-sm-8">
<span t-field="rma.operation_id"/>
<span t-field="rma.operation_id" />
</div>
</div>
</xpath>

View File

@@ -1,26 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="view_order_form" model="ir.ui.view">
<field name="name">sale.order.form.inherit</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="groups_id" eval="[(4, ref('rma.rma_group_user_own'))]"/>
<field name="inherit_id" ref="sale.view_order_form" />
<field name="groups_id" eval="[(4, ref('rma.rma_group_user_own'))]" />
<field name="arch" type="xml">
<button name="action_done" position="after">
<button name="action_create_rma"
type="object"
string="Create RMA"
states="sale,done"/>
<button
name="action_create_rma"
type="object"
string="Create RMA"
states="sale,done"
/>
</button>
<div name="button_box">
<button name="action_view_rma"
type="object"
class="oe_stat_button"
icon="fa-reply"
attrs="{'invisible': [('rma_count', '=', 0)]}">
<field name="rma_count"
widget="statinfo"
string="RMA"/>
<button
name="action_view_rma"
type="object"
class="oe_stat_button"
icon="fa-reply"
attrs="{'invisible': [('rma_count', '=', 0)]}"
>
<field name="rma_count" widget="statinfo" string="RMA" />
</button>
</div>
</field>

View File

@@ -2,29 +2,30 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import _, api, fields, models
from odoo.addons import decimal_precision as dp
class SaleOrderRmaWizard(models.TransientModel):
_name = "sale.order.rma.wizard"
_description = 'Sale Order Rma Wizard'
_description = "Sale Order Rma Wizard"
def _domain_location_id(self):
rma_loc = self.env['stock.warehouse'].search([]).mapped('rma_loc_id')
return [('id', 'child_of', rma_loc.ids)]
rma_loc = self.env["stock.warehouse"].search([]).mapped("rma_loc_id")
return [("id", "child_of", rma_loc.ids)]
order_id = fields.Many2one(
comodel_name='sale.order',
default=lambda self: self.env.context.get('active_id', False),
comodel_name="sale.order",
default=lambda self: self.env.context.get("active_id", False),
)
line_ids = fields.One2many(
comodel_name='sale.order.line.rma.wizard',
inverse_name='wizard_id',
string='Lines',
comodel_name="sale.order.line.rma.wizard",
inverse_name="wizard_id",
string="Lines",
)
location_id = fields.Many2one(
comodel_name='stock.location',
string='RMA location',
comodel_name="stock.location",
string="RMA location",
domain=_domain_location_id,
default=lambda r: r.order_id.warehouse_id.rma_loc_id.id,
)
@@ -33,19 +34,21 @@ class SaleOrderRmaWizard(models.TransientModel):
self.ensure_one()
lines = self.line_ids.filtered(lambda r: r.quantity > 0.0)
val_list = [line._prepare_rma_values() for line in lines]
rma = self.env['rma'].create(val_list)
rma = self.env["rma"].create(val_list)
# post messages
msg_list = ['<a href="#" data-oe-model="rma" data-oe-id="%d">%s</a>'
% (r.id, r.name) for r in rma]
msg = ', '.join(msg_list)
msg_list = [
'<a href="#" data-oe-model="rma" data-oe-id="%d">%s</a>' % (r.id, r.name)
for r in rma
]
msg = ", ".join(msg_list)
if len(msg_list) == 1:
self.order_id.message_post(body=_(msg + ' has been created.'))
self.order_id.message_post(body=_(msg + " has been created."))
elif len(msg_list) > 1:
self.order_id.message_post(body=_(msg + ' have been created.'))
self.order_id.message_post(body=_(msg + " have been created."))
rma.message_post_with_view(
'mail.message_origin_link',
values={'self': rma, 'origin': self.order_id},
subtype_id=self.env.ref('mail.mt_note').id,
"mail.message_origin_link",
values={"self": rma, "origin": self.order_id},
subtype_id=self.env.ref("mail.mt_note").id,
)
return rma
@@ -54,115 +57,107 @@ class SaleOrderRmaWizard(models.TransientModel):
rma = self.create_rma()
for rec in rma:
rec.action_confirm()
action = self.env.ref('rma.rma_action').read()[0]
action = self.env.ref("rma.rma_action").read()[0]
if len(rma) > 1:
action['domain'] = [('id', 'in', rma.ids)]
action["domain"] = [("id", "in", rma.ids)]
elif rma:
action.update(
res_id=rma.id,
view_mode="form",
view_id=False,
views=False,
res_id=rma.id, view_mode="form", view_id=False, views=False,
)
return action
class SaleOrderLineRmaWizard(models.TransientModel):
_name = "sale.order.line.rma.wizard"
_description = 'Sale Order Line Rma Wizard'
_description = "Sale Order Line Rma Wizard"
wizard_id = fields.Many2one(
comodel_name='sale.order.rma.wizard',
string='Wizard',
)
wizard_id = fields.Many2one(comodel_name="sale.order.rma.wizard", string="Wizard",)
order_id = fields.Many2one(
comodel_name='sale.order',
default=lambda self: self.env['sale.order'].browse(
self.env.context.get('active_id', False)),
comodel_name="sale.order",
default=lambda self: self.env["sale.order"].browse(
self.env.context.get("active_id", False)
),
)
allowed_product_ids = fields.Many2many(
comodel_name='product.product',
compute='_compute_allowed_product_ids'
comodel_name="product.product", compute="_compute_allowed_product_ids"
)
product_id = fields.Many2one(
comodel_name='product.product',
string='Product',
comodel_name="product.product",
string="Product",
required=True,
domain="[('id', 'in', allowed_product_ids)]"
domain="[('id', 'in', allowed_product_ids)]",
)
uom_category_id = fields.Many2one(
comodel_name='uom.category',
related='product_id.uom_id.category_id',
comodel_name="uom.category", related="product_id.uom_id.category_id",
)
quantity = fields.Float(
string='Quantity',
digits=dp.get_precision('Product Unit of Measure'),
string="Quantity",
digits=dp.get_precision("Product Unit of Measure"),
required=True,
)
uom_id = fields.Many2one(
comodel_name='uom.uom',
string='Unit of Measure',
comodel_name="uom.uom",
string="Unit of Measure",
domain="[('category_id', '=', uom_category_id)]",
required=True,
)
allowed_picking_ids = fields.Many2many(
comodel_name='stock.picking',
compute='_compute_allowed_picking_ids'
comodel_name="stock.picking", compute="_compute_allowed_picking_ids"
)
picking_id = fields.Many2one(
comodel_name='stock.picking',
string='Delivery order',
domain="[('id', 'in', allowed_picking_ids)]"
)
move_id = fields.Many2one(
comodel_name='stock.move',
compute='_compute_move_id'
comodel_name="stock.picking",
string="Delivery order",
domain="[('id', 'in', allowed_picking_ids)]",
)
move_id = fields.Many2one(comodel_name="stock.move", compute="_compute_move_id")
operation_id = fields.Many2one(
comodel_name='rma.operation',
string='Requested operation',
comodel_name="rma.operation", string="Requested operation",
)
description = fields.Text()
@api.onchange('product_id')
@api.onchange("product_id")
def onchange_product_id(self):
self.picking_id = False
@api.depends('picking_id')
@api.depends("picking_id")
def _compute_move_id(self):
for record in self:
if record.picking_id:
record.move_id = record.picking_id.move_lines.filtered(
lambda r: (r.sale_line_id.product_id == record.product_id
and r.sale_line_id.order_id == record.order_id))
lambda r: (
r.sale_line_id.product_id == record.product_id
and r.sale_line_id.order_id == record.order_id
)
)
@api.depends('order_id')
@api.depends("order_id")
def _compute_allowed_product_ids(self):
for record in self:
product_ids = record.order_id.order_line.mapped('product_id.id')
product_ids = record.order_id.order_line.mapped("product_id.id")
record.allowed_product_ids = product_ids
@api.depends('product_id')
@api.depends("product_id")
def _compute_allowed_picking_ids(self):
for record in self:
line = record.order_id.order_line.filtered(
lambda r: r.product_id == record.product_id)
record.allowed_picking_ids = line.mapped('move_ids.picking_id')
lambda r: r.product_id == record.product_id
)
record.allowed_picking_ids = line.mapped("move_ids.picking_id")
def _prepare_rma_values(self):
self.ensure_one()
return {
'partner_id': self.order_id.partner_id.id,
'partner_invoice_id': self.order_id.partner_invoice_id.id,
'origin': self.order_id.name,
'company_id': self.order_id.company_id.id,
'location_id': self.wizard_id.location_id.id,
'order_id': self.order_id.id,
'picking_id': self.picking_id.id,
'move_id': self.move_id.id,
'product_id': self.product_id.id,
'product_uom_qty': self.quantity,
'product_uom': self.uom_id.id,
'operation_id': self.operation_id.id,
'description': self.description,
"partner_id": self.order_id.partner_id.id,
"partner_invoice_id": self.order_id.partner_invoice_id.id,
"origin": self.order_id.name,
"company_id": self.order_id.company_id.id,
"location_id": self.wizard_id.location_id.id,
"order_id": self.order_id.id,
"picking_id": self.picking_id.id,
"move_id": self.move_id.id,
"product_id": self.product_id.id,
"product_uom_qty": self.quantity,
"product_uom": self.uom_id.id,
"operation_id": self.operation_id.id,
"description": self.description,
}

View File

@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="sale_order_create_rma_action" model="ir.actions.act_window">
<field name="name">Create RMAs</field>
@@ -7,7 +7,6 @@
<field name="view_mode">form</field>
<field name="target">new</field>
</record>
<record id="=sale_order_rma_wizard_form_view" model="ir.ui.view">
<field name="name">sale.order.rma.wizard.form</field>
<field name="model">sale.order.rma.wizard</field>
@@ -16,34 +15,38 @@
<group>
<field name="line_ids" nolabel="1">
<tree editable="bottom">
<field name="order_id" invisible="1"/>
<field name="allowed_product_ids" invisible="1"/>
<field name="product_id" options="{'no_create': True}"/>
<field name="quantity"/>
<field name="uom_category_id" invisible="1"/>
<field name="uom_id"
groups="uom.group_uom"
options="{'no_create': True}"/>
<field name="allowed_picking_ids" invisible="1"/>
<field name="picking_id" options="{'no_create': True}"/>
<field name="operation_id"/>
<field name="order_id" invisible="1" />
<field name="allowed_product_ids" invisible="1" />
<field name="product_id" options="{'no_create': True}" />
<field name="quantity" />
<field name="uom_category_id" invisible="1" />
<field
name="uom_id"
groups="uom.group_uom"
options="{'no_create': True}"
/>
<field name="allowed_picking_ids" invisible="1" />
<field name="picking_id" options="{'no_create': True}" />
<field name="operation_id" />
</tree>
</field>
</group>
<group>
<field name="location_id"
options="{'no_create': True, 'no_open': True}"
groups="stock.group_stock_multi_locations"
required="1"/>
<field
name="location_id"
options="{'no_create': True, 'no_open': True}"
groups="stock.group_stock_multi_locations"
required="1"
/>
</group>
<footer>
<button name="create_and_open_rma"
string="Accept"
type="object"
class="btn-primary"/>
<button string="Cancel"
class="btn-secondary"
special="cancel" />
<button
name="create_and_open_rma"
string="Accept"
type="object"
class="btn-primary"
/>
<button string="Cancel" class="btn-secondary" special="cancel" />
</footer>
</form>
</field>

View File

@@ -0,0 +1 @@
../../../../rma_sale

6
setup/rma_sale/setup.py Normal file
View File

@@ -0,0 +1,6 @@
import setuptools
setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)