Skip to content

[18.0][FIX] fieldservice_sale: preserve FSM template type#1532

Open
fsmw wants to merge 3 commits into
OCA:18.0from
fsmw:18.0-fix-fieldservice-sale-template-type
Open

[18.0][FIX] fieldservice_sale: preserve FSM template type#1532
fsmw wants to merge 3 commits into
OCA:18.0from
fsmw:18.0-fix-fieldservice-sale-template-type

Conversation

@fsmw
Copy link
Copy Markdown
Contributor

@fsmw fsmw commented Apr 30, 2026

Fixes #1398

Summary

  • Preserve the FSM template on sale-level generated orders when a single template is selected.
  • Copy the template's type to the generated FSM order when the selected templates resolve to one type.
  • Add regression coverage for sale order confirmation creating an FSM order with template and type.

Verification

  • pre-commit run --files fieldservice_sale/models/sale_order.py fieldservice_sale/tests/test_fsm_sale_common.py fieldservice_sale/tests/test_fsm_sale_order.py
  • /home/fsmw/dev/smwlab/.venv/bin/python /home/fsmw/dev/smwlab/OCB/odoo-bin --addons-path=/home/fsmw/dev/smwlab/OCB/odoo/addons,/home/fsmw/dev/smwlab/OCB/addons,/home/fsmw/dev/smwlab/oca-fsmw/field-service -d test_fieldservice_sale_template_type_green3 --init=fieldservice_sale --test-enable --stop-after-init --test-tags /fieldservice_sale --http-port=18073

@OCA-git-bot
Copy link
Copy Markdown
Contributor

Hi @wolfhall, @brian10048, @max3903,
some modules you are maintaining are being modified, check this out!

@fsmw
Copy link
Copy Markdown
Contributor Author

fsmw commented Apr 30, 2026

The current failing test job is unrelated to this PR's fieldservice_sale changes. I reproduced the fieldservice_kanban_info failures on clean upstream/18.0 and opened a separate targeted fix in #1533.

@fsmw
Copy link
Copy Markdown
Contributor Author

fsmw commented May 16, 2026

CI should now be green after #1533 merged (fixes the upstream date-test flakiness). This is a focused [FIX] preserving FSM template types on SO lines. Ready for review when possible!

@rrebollo
Copy link
Copy Markdown
Contributor

@fsmw Please do a rebase — the red tests might turn green afterward.

Copy link
Copy Markdown
Contributor

@rrebollo rrebollo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functional and code review: It's working really well.
From a technical perspective, I would personally suggest refactoring to simplify the template_ids approach. Please consider it.

Comment thread fieldservice_sale/models/sale_order.py Outdated
@@ -78,13 +78,16 @@ def _prepare_fsm_values(self, **kwargs):
template_id = kwargs.get("template_id", False)
template_ids = kwargs.get("template_ids", [template_id])
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did my research. The template_ids keyword argument is misleading. Look at how it's used: the fsm_order_template_id field on sale.order is a many2one, so it always points to a single record — not many.

If You go this way You would need to refactor almost all the method here.

I might be wrong, so please review and consider this. You could even correct it in

def _field_service_generate_sale_fsm_orders(self, new_fsm_sol):
"""
Generate the FSM Order for this sale order if it doesn't exist.
"""
self.ensure_one()
new_fsm_orders = self.env["fsm.order"]
if new_fsm_sol:
fsm_by_sale = self.env["fsm.order"].search(
[("sale_id", "=", self.id), ("sale_line_id", "=", False)]
)
if not fsm_by_sale:
templates = new_fsm_sol.product_id.fsm_order_template_id
vals = self._prepare_fsm_values(
so_id=self.id, template_ids=templates.ids
)
fsm_by_sale = self.env["fsm.order"].sudo().create(vals)
new_fsm_orders |= fsm_by_sale
new_fsm_sol.write({"fsm_order_id": fsm_by_sale.id})
return new_fsm_orders

Comment on lines 80 to 104
@@ -96,6 +99,7 @@ def _prepare_fsm_values(self, **kwargs):
"sale_id": kwargs.get("so_id", False),
"sale_line_id": kwargs.get("sol_id", False),
"template_id": template_id,
"type": order_types.id if len(order_types) == 1 else False,
"company_id": self.company_id.id,
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally, it was way too complicated for no reason at all.

@fsmw fsmw force-pushed the 18.0-fix-fieldservice-sale-template-type branch from 82304de to 44ca041 Compare May 20, 2026 01:01
@OCA-git-bot OCA-git-bot added the mod:fieldservice Module fieldservice label May 20, 2026
Comment thread fieldservice_sale/models/sale_order.py Outdated
vals = self._prepare_fsm_values(so_id=self.id, templates=templates)
fsm_by_sale = self.env["fsm.order"].sudo().create(vals)
new_fsm_orders |= fsm_by_sale
new_fsm_sol.write({"fsm_order_id": fsm_by_sale.id})
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
new_fsm_sol.write({"fsm_order_id": fsm_by_sale.id})
new_fsm_sol.fsm_order_id = fsm_by_sale.id

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mod:fieldservice_sale Module fieldservice_sale mod:fieldservice Module fieldservice series:18.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Template and type are not set on the FSM order created from the sales order

3 participants