Skip to content

Commit

Permalink
[17.0][IMP] fieldservice_euipment_stock: add return order type
Browse files Browse the repository at this point in the history
  • Loading branch information
ilo committed Oct 21, 2024
1 parent 15a55b6 commit f63eb8c
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 7 deletions.
3 changes: 3 additions & 0 deletions fieldservice_equipment_stock/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
],
"data": [
"security/ir.model.access.csv",
"data/fsm_order_type.xml",
"views/fsm_equipment.xml",
"views/fsm_order_view.xml",
"views/fsm_order_type_view.xml",
"views/product_template.xml",
"views/stock_picking_type.xml",
"views/stock_lot.xml",
Expand Down
9 changes: 9 additions & 0 deletions fieldservice_equipment_stock/data/fsm_order_type.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo noupdate="1">

<record id="fsm_order_type_return" model="fsm.order.type">
<field name="name">Return</field>
<field name="internal_type">return</field>
</record>

</odoo>
2 changes: 2 additions & 0 deletions fieldservice_equipment_stock/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
stock_move,
stock_picking_type,
fsm_equipment,
fsm_order,
fsm_order_type,
product_template,
stock_lot,
)
14 changes: 14 additions & 0 deletions fieldservice_equipment_stock/models/fsm_equipment.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,17 @@ def write(self, vals):
if "lot_id" in vals:
equipment.lot_id.fsm_equipment_id = equipment.id
return res

def create_equip_order_return(self):
self.ensure_one()
order_type = self.env.ref("fieldservice_equipment_stock.fsm_order_type_return")
return {

Check warning on line 51 in fieldservice_equipment_stock/models/fsm_equipment.py

View check run for this annotation

Codecov / codecov/patch

fieldservice_equipment_stock/models/fsm_equipment.py#L49-L51

Added lines #L49 - L51 were not covered by tests
"name": "Return Equipment",
"type": "ir.actions.act_window",
"res_model": "fsm.order",
"view_mode": "form",
"context": {
"default_equipment_id": self.id,
"default_type": order_type and order_type.id or False,
},
}
101 changes: 101 additions & 0 deletions fieldservice_equipment_stock/models/fsm_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Copyright 2024 Camptocamp
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import _, api, models
from odoo.exceptions import ValidationError


class FSMOrder(models.Model):
_inherit = "fsm.order"

def _prepare_return_procurement_group_values(self):
return {

Check warning on line 12 in fieldservice_equipment_stock/models/fsm_order.py

View check run for this annotation

Codecov / codecov/patch

fieldservice_equipment_stock/models/fsm_order.py#L12

Added line #L12 was not covered by tests
"name": self.display_name,
"fsm_order_id": self.id,
"move_type": "direct",
}

def _prepare_return_stock_picking_values(self):
source_location_id = self._get_equipment_current_location()
return {

Check warning on line 20 in fieldservice_equipment_stock/models/fsm_order.py

View check run for this annotation

Codecov / codecov/patch

fieldservice_equipment_stock/models/fsm_order.py#L19-L20

Added lines #L19 - L20 were not covered by tests
"picking_type_id": self.type.picking_type_id.id,
"origin": self.display_name,
"location_dest_id": self.type.picking_type_id.default_location_dest_id.id,
"location_id": source_location_id and source_location_id.id,
"fsm_order_id": self.id,
"group_id": self.procurement_group_id.id,
}

def _prepare_return_stock_move_values(self):
source_location_id = self._get_equipment_current_location()
return {

Check warning on line 31 in fieldservice_equipment_stock/models/fsm_order.py

View check run for this annotation

Codecov / codecov/patch

fieldservice_equipment_stock/models/fsm_order.py#L30-L31

Added lines #L30 - L31 were not covered by tests
"name": self.display_name,
"product_id": self.equipment_id.product_id.id,
"product_uom_qty": 1,
"product_uom": self.equipment_id.product_id.uom_id.id,
"location_id": source_location_id.id,
"location_dest_id": self.type.picking_type_id.default_location_dest_id.id,
"group_id": self.procurement_group_id.id,
"fsm_order_id": self.id,
"lot_ids": [(4, self.equipment_id.lot_id.id)]
if self.equipment_id.lot_id
else False,
}

def _get_equipment_current_location(self):
self.ensure_one()

Check warning on line 46 in fieldservice_equipment_stock/models/fsm_order.py

View check run for this annotation

Codecov / codecov/patch

fieldservice_equipment_stock/models/fsm_order.py#L46

Added line #L46 was not covered by tests
if not self.equipment_id:
raise ValidationError(

Check warning on line 48 in fieldservice_equipment_stock/models/fsm_order.py

View check run for this annotation

Codecov / codecov/patch

fieldservice_equipment_stock/models/fsm_order.py#L48

Added line #L48 was not covered by tests
_(
"Cannot create Return Order because "
"order does not have a current equipment."
)
)
if self.equipment_id.location_id:
return (

Check warning on line 55 in fieldservice_equipment_stock/models/fsm_order.py

View check run for this annotation

Codecov / codecov/patch

fieldservice_equipment_stock/models/fsm_order.py#L55

Added line #L55 was not covered by tests
self.equipment_id.location_id
and self.equipment_id.location_id.inventory_location_id
)
elif self.equipment_id.current_location_id:
return (

Check warning on line 60 in fieldservice_equipment_stock/models/fsm_order.py

View check run for this annotation

Codecov / codecov/patch

fieldservice_equipment_stock/models/fsm_order.py#L60

Added line #L60 was not covered by tests
self.equipment_id.current_location_id
and self.equipment_id.current_location_id.inventory_location_id
)
else:
raise ValidationError(

Check warning on line 65 in fieldservice_equipment_stock/models/fsm_order.py

View check run for this annotation

Codecov / codecov/patch

fieldservice_equipment_stock/models/fsm_order.py#L65

Added line #L65 was not covered by tests
_(
"Cannot create Return Order because "
"equipment does not have a current location."
)
)

@api.model
def create(self, vals):
# if FSM order with type return is created then
# create a picking order
order = super().create(vals)
if order.type.internal_type == "return":
if order.equipment_id and order.type.picking_type_id:
group = self.env["procurement.group"].search(

Check warning on line 79 in fieldservice_equipment_stock/models/fsm_order.py

View check run for this annotation

Codecov / codecov/patch

fieldservice_equipment_stock/models/fsm_order.py#L79

Added line #L79 was not covered by tests
[("fsm_order_id", "=", order.id)]
)
if not group:
values = order._prepare_return_procurement_group_values()
group = self.env["procurement.group"].create(values)
order.procurement_group_id = group and group.id
return_picking_values = order._prepare_return_stock_picking_values()
new_picking = self.env["stock.picking"].create(return_picking_values)
return_move_values = order._prepare_return_stock_move_values()
return_move_values["picking_id"] = new_picking.id
self.env["stock.move"].create(return_move_values)
new_picking.action_confirm()

Check warning on line 91 in fieldservice_equipment_stock/models/fsm_order.py

View check run for this annotation

Codecov / codecov/patch

fieldservice_equipment_stock/models/fsm_order.py#L83-L91

Added lines #L83 - L91 were not covered by tests

elif not order.type.picking_type_id:
raise ValidationError(

Check warning on line 94 in fieldservice_equipment_stock/models/fsm_order.py

View check run for this annotation

Codecov / codecov/patch

fieldservice_equipment_stock/models/fsm_order.py#L94

Added line #L94 was not covered by tests
_(
"Cannot create Return Order because "
"order type does not have a current "
"Picking Type."
)
)
return order
16 changes: 16 additions & 0 deletions fieldservice_equipment_stock/models/fsm_order_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2024 Camptocamp
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


class FsmOrderType(models.Model):
_inherit = "fsm.order.type"

internal_type = fields.Selection(
selection_add=[("return", "Return")],
)
picking_type_id = fields.Many2one(
"stock.picking.type",
domain=[("code", "=", "incoming")],
)
11 changes: 4 additions & 7 deletions fieldservice_equipment_stock/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@

/*
:Author: David Goodger (goodger@python.org)
:Id: $Id: html4css1.css 9511 2024-01-13 09:50:07Z milde $
:Id: $Id: html4css1.css 8954 2022-01-20 10:10:25Z milde $
:Copyright: This stylesheet has been placed in the public domain.

Default cascading style sheet for the HTML output of Docutils.
Despite the name, some widely supported CSS2 features are used.

See https://docutils.sourceforge.io/docs/howto/html-stylesheets.html for how to
customize this style sheet.
Expand Down Expand Up @@ -275,7 +274,7 @@
margin-left: 2em ;
margin-right: 2em }

pre.code .ln { color: gray; } /* line numbers */
pre.code .ln { color: grey; } /* line numbers */
pre.code, code { background-color: #eeeeee }
pre.code .comment, code .comment { color: #5C6576 }
pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
Expand All @@ -301,7 +300,7 @@
span.pre {
white-space: pre }

span.problematic, pre.problematic {
span.problematic {
color: red }

span.section-subtitle {
Expand Down Expand Up @@ -461,9 +460,7 @@ <h2><a class="toc-backref" href="#toc-entry-8">Other credits</a></h2>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#toc-entry-9">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org">
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
</a>
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.</p>
Expand Down
9 changes: 9 additions & 0 deletions fieldservice_equipment_stock/views/fsm_equipment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
<field name="model">fsm.equipment</field>
<field name="inherit_id" ref="fieldservice.fsm_equipment_form_view" />
<field name="arch" type="xml">
<button id="previous_stage" position="before">
<button
id="new_equip_return"
name="create_equip_order_return"
string="Create Return"
class="oe_highlight"
type="object"
/>
</button>
<group id="secondary" position="inside">
<group string="Inventory" groups="stock.group_stock_user">
<field name="product_id" required="1" />
Expand Down
15 changes: 15 additions & 0 deletions fieldservice_equipment_stock/views/fsm_order_type_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>

<!-- Fields Service Orders Form View -->
<record id="fsm_order_type_form_view" model="ir.ui.view">
<field name="model">fsm.order.type</field>
<field name="inherit_id" ref="fieldservice.fsm_order_type_form_view" />
<field name="arch" type="xml">
<field name="internal_type" position="after">
<field name="picking_type_id" invisible="internal_type != 'return'" />
</field>
</field>
</record>

</odoo>
18 changes: 18 additions & 0 deletions fieldservice_equipment_stock/views/fsm_order_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>

<!-- Fields Service Orders Form View -->
<record id="fsm_order_maintenance_form" model="ir.ui.view">
<field name="model">fsm.order</field>
<field name="inherit_id" ref="fieldservice.fsm_order_form" />
<field name="arch" type="xml">
<field name="equipment_id" position="attributes">
<attribute
name="invisible"
>internal_type not in ['repair', 'maintenance', 'return']</attribute>
<attribute name="required">internal_type in ['return']</attribute>
</field>
</field>
</record>

</odoo>

0 comments on commit f63eb8c

Please sign in to comment.