feat(fusion_repairs): Bundle 8 - rush service + emergency pricing + parts-ordered workflow

The grumpy-old-customer-with-broken-stairlift scenario. Four real workflows
the office faces every week, with comms baked in so the client never has to
call back asking for status.

NEW MODELS
- fusion.repair.emergency.charge (rate card)
  Per (category, tier) rate with per_tech_multiplier; 5 tiers
  (same_day / next_day / after_hours / weekend / holiday). Each category
  can have its own rates - bed motors need 2 techs, stairlift is single.
  Seeded with realistic Westin rates: stairlift same-day $250, weekend
  $450; porch lift same-day $300; bed same-day $175 with 0.6 multiplier
  (2-tech jobs frequent); powerchair same-day $200.

- fusion.repair.part.order (procurement-facing record)
  One per distinct part the tech needs from the manufacturer. Carries
  description + OEM # + manufacturer + quantity + photos + notes.
  4-state lifecycle: draft -> ordered -> received -> fitted (or
  cancelled). On state transitions:
    draft -> ordered:  email client "ordered, expected by X"
    ordered -> received: email client "arrived, scheduling return visit"
                         + auto-create follow-up dispatch task when ALL
                         outstanding parts on the repair have arrived.

REPAIR.ORDER EXTENSIONS
- Rush fields: x_fc_rush_requested, x_fc_rush_tier,
  x_fc_rush_techs_required, x_fc_rush_surcharge (computed via rate card),
  x_fc_rush_acknowledged_at + x_fc_rush_acknowledged_by_id (audit trail
  proving CS got verbal OK before charging).
- Parts-awaiting fields: x_fc_parts_awaiting + x_fc_parts_eta_date +
  x_fc_part_order_ids One2many + x_fc_part_order_count.

- New methods:
  * action_acknowledge_rush() - one-click "client agreed" with audit.
  * action_squeeze_into_today() - picks the lightest-loaded skilled tech,
    finds their first free 1-hour slot between 9am-6pm, schedules the
    task in it, sends:
      1) live bus.bus push to the tech (sticky notification in their
         web client - so they see it MID-SHIFT)
      2) rush-alert email (force_send=True - this can't wait in the queue)
      3) chatter post on the tech task itself
    Validates against fusion_tasks' time-conflict rule by passing
    force_schedule via context (intake.service honours it).
  * action_view_part_orders() - smart button.

WIZARD EXTENSIONS
- repair.intake.wizard:
  New rush_requested + rush_tier + rush_techs_required + rush_acknowledged
  controls. Live rush_surcharge_preview compute shows CS the price in
  real-time as they change category / tier / tech count. Yellow alert
  reminds CS to read the price to the client BEFORE submitting.

- repair.visit.report.wizard:
  New outcome radio: completed / parts_needed / rescheduled.
  When outcome=parts_needed, needs_parts_line_ids One2many appears for
  the tech to capture each part (description, OEM, manufacturer, qty,
  lead days, notes, photos). On submit each line creates a
  fusion.repair.part.order, the repair flips to x_fc_parts_awaiting=True
  with an ETA, and the client gets the "we found the problem, here's the
  plan" email immediately.

INTAKE SERVICE
- _create_dispatch_task now honours force_schedule (date + time_start +
  time_end) via context so squeeze + auto-redispatch don't crash on
  fusion_tasks' time-window validator.
- _create_single_repair carries rush_requested/tier/techs through to
  the new repair fields.

MAIL TEMPLATES (4 new)
- email_template_rush_tech_alert: red 4px accent, address + phone + the
  $surcharge - what the tech needs to know mid-shift.
- email_template_repair_awaiting_parts: amber accent, "we found the
  problem, parts ordered, return visit ~ETA, no action needed".
- email_template_parts_ordered: blue, per-part confirmation.
- email_template_parts_received: green, "arrived, office will call to
  confirm visit".

UI / NAVIGATION
- Backend wizard: rush controls + live surcharge preview + verbal-OK alert.
- repair.order form: new Rush / Parts notebook tab with all the fields
  + linked part orders list. Two new header buttons (Squeeze into
  Today / Client Agreed to Rush Price). Two new search filters
  (Rush, Awaiting Parts).
- Part Order form: statusbar with the 4 transitions + Cancel; notes +
  photos notebook tabs; full chatter for audit.
- Menus: 'Parts to Order' under root; 'Emergency Surcharges' under
  Configuration.

SECURITY
- 8 new ACL entries (emergency_charge user/manager; part_order
  user/dispatcher/manager/technician; visit_report partline for office
  and field tech). Office sees parts but only managers can edit
  emergency rates.

Verified end-to-end on local westin-v19 - all 4 scenarios green:
  S1 Same-day rush stairlift -> $250 surcharge, ack stamped, squeeze
     assigned garry@ at first free 1h slot today, alert email queued,
     chatter posted.
  S2 Next-day priority bed -> $0 surcharge (no rate seeded for bed
     next_day - office can configure), 4 emails queued (client + office).
  S3 2-tech weekend stairlift -> $675 (450 base + 0.5x base for 2nd tech).
  S4 Parts-needed visit-report -> 2 PART-#### records created, repair
     awaiting_parts=True, ETA=2026-06-06, office activity scheduled,
     client email sent. Marking part ordered -> client mail. Marking
     all parts received -> auto-dispatch follow-up + client mail.

Bumped to 19.0.1.9.1.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
gsinghpal
2026-05-21 01:28:13 -04:00
parent 4f1b7c2df6
commit ebbadb3002
18 changed files with 1367 additions and 8 deletions

View File

@@ -7,6 +7,8 @@ from datetime import date, datetime, timedelta
from dateutil.relativedelta import relativedelta
from markupsafe import Markup
from odoo import api, fields, models, _
from odoo.exceptions import UserError
@@ -163,6 +165,293 @@ class RepairOrder(models.Model):
'long-running repair (M3). Avoids re-posting daily.',
)
# ------------------------------------------------------------------
# Bundle 8: RUSH / EMERGENCY SERVICE
# ------------------------------------------------------------------
x_fc_rush_requested = fields.Boolean(
string='Rush / Emergency Service',
tracking=True,
copy=False,
)
x_fc_rush_tier = fields.Selection(
[
('same_day', 'Same Day'),
('next_day', 'Next Day Priority'),
('after_hours', 'After Hours'),
('weekend', 'Weekend'),
('holiday', 'Statutory Holiday'),
],
string='Rush Tier',
tracking=True,
copy=False,
)
x_fc_rush_techs_required = fields.Integer(
string='Technicians Required',
default=1,
copy=False,
help='Some calls need 2+ techs (e.g. heavy lifting, controller programming '
'plus mechanical). Surcharge scales accordingly.',
)
x_fc_rush_surcharge = fields.Monetary(
string='Rush Surcharge',
currency_field='company_currency_id',
compute='_compute_rush_surcharge',
store=True,
tracking=True,
)
x_fc_rush_acknowledged_at = fields.Datetime(
string='Rush Surcharge Acknowledged',
copy=False,
readonly=True,
help='Stamped when CS records that the client agreed to the rush price.',
)
x_fc_rush_acknowledged_by_id = fields.Many2one(
'res.users',
string='Acknowledged By',
copy=False,
readonly=True,
help='The CS rep who got the verbal OK from the client.',
)
# ------------------------------------------------------------------
# Bundle 8: PARTS AWAITING (when tech can't fix on the first visit)
# ------------------------------------------------------------------
x_fc_parts_awaiting = fields.Boolean(
string='Awaiting Parts',
tracking=True,
copy=False,
index=True,
help='Tech could not complete the repair without ordering parts. '
'Repair stays open; clears automatically when the last linked '
'fusion.repair.part.order moves to "received".',
)
x_fc_parts_eta_date = fields.Date(
string='Parts ETA',
copy=False,
tracking=True,
)
x_fc_part_order_ids = fields.One2many(
'fusion.repair.part.order',
'repair_order_id',
string='Part Orders',
copy=False,
)
x_fc_part_order_count = fields.Integer(
compute='_compute_part_order_count',
string='# Part Orders',
)
@api.depends('x_fc_rush_tier', 'x_fc_rush_techs_required',
'x_fc_repair_category_id')
def _compute_rush_surcharge(self):
Rates = self.env['fusion.repair.emergency.charge'].sudo()
for r in self:
if not r.x_fc_rush_tier or not r.x_fc_repair_category_id:
r.x_fc_rush_surcharge = 0.0
continue
r.x_fc_rush_surcharge = Rates.calculate(
r.x_fc_repair_category_id,
r.x_fc_rush_tier,
r.x_fc_rush_techs_required or 1,
)
@api.depends('x_fc_part_order_ids')
def _compute_part_order_count(self):
for r in self:
r.x_fc_part_order_count = len(r.x_fc_part_order_ids)
def action_view_part_orders(self):
self.ensure_one()
return {
'type': 'ir.actions.act_window',
'name': _('Part Orders'),
'res_model': 'fusion.repair.part.order',
'view_mode': 'list,form',
'domain': [('repair_order_id', '=', self.id)],
'context': {'default_repair_order_id': self.id},
}
def action_acknowledge_rush(self):
"""CS clicks this AFTER getting verbal OK from the client on the rush price."""
for r in self:
r.x_fc_rush_acknowledged_at = fields.Datetime.now()
r.x_fc_rush_acknowledged_by_id = self.env.user
r.message_post(body=Markup(_(
'Rush surcharge of <b>%(amt).2f</b> acknowledged by client '
'(verbal OK to %(rep)s).'
)) % {
'amt': r.x_fc_rush_surcharge,
'rep': self.env.user.name,
})
def action_squeeze_into_today(self):
"""Squeeze this repair into a field tech's existing route today.
Picks the lightest-loaded skilled tech, finds the first free 1-hour
slot in their day, creates / updates the dispatch task to that slot,
and pushes a live bus.bus notification + email so the tech knows
mid-shift.
"""
from datetime import date as _date
today = _date.today()
Task = self.env['fusion.technician.task'].sudo()
for r in self:
tech_id = self._fc_find_lightest_today_tech()
if not tech_id:
raise UserError(_(
'No field-staff users available - mark someone as Field '
'Staff under Settings > Users and try again.'
))
slot_start, slot_end = self._fc_find_free_slot_today(tech_id)
if slot_start is None:
raise UserError(_(
"%s has no free hour left today. Either bump an existing "
"task or schedule for tomorrow instead."
) % self.env['res.users'].sudo().browse(tech_id).name)
existing = r.x_fc_technician_task_ids.filtered(
lambda t: t.status not in ('completed', 'cancelled')
)
vals = {
'technician_id': tech_id,
'scheduled_date': today,
'time_start': slot_start,
'time_end': slot_end,
}
if existing:
task = existing[0]
task.write(vals)
else:
self.env['fusion.repair.intake.service'].sudo() \
.with_context(
force_tech_id=tech_id,
force_schedule={
'scheduled_date': today,
'time_start': slot_start,
'time_end': slot_end,
},
) \
._create_dispatch_task(r)
task = r.x_fc_technician_task_ids[:1]
self._notify_tech_of_rush(task)
r.message_post(body=Markup(_(
'Squeezed into <b>%(name)s</b>\'s route today at '
'<b>%(start).0f:00 - %(end).0f:00</b>; tech notified.'
)) % {
'name': task.technician_id.name or '?',
'start': slot_start,
'end': slot_end,
})
def _fc_find_free_slot_today(self, tech_id):
"""Return (start_float, end_float) for the first free 1-hour window
in this tech's day between 9 AM and 6 PM, or (None, None)."""
from datetime import date as _date
today = _date.today()
Task = self.env['fusion.technician.task'].sudo()
existing = Task.search([
('technician_id', '=', tech_id),
('scheduled_date', '=', today),
('status', 'not in', ('completed', 'cancelled')),
])
# Build a set of busy hours (rounded down to integer hours).
busy = set()
for t in existing:
s = int(t.time_start or 0)
e = int(t.time_end or s + 1)
for h in range(s, max(s + 1, e)):
busy.add(h)
# Scan 9 AM - 5 PM (last slot is 17:00-18:00 inclusive).
for hour in range(9, 18):
if hour not in busy:
return float(hour), float(hour + 1)
return None, None
def _fc_find_lightest_today_tech(self):
"""Return the field-staff user with the fewest scheduled tasks today.
Honors skills filter if this repair has a category.
"""
Users = self.env['res.users'].sudo()
Task = self.env['fusion.technician.task'].sudo()
from datetime import date as _date
today = _date.today()
domain = [
('x_fc_is_field_staff', '=', True),
('active', '=', True),
]
if self.x_fc_repair_category_id:
domain.append(
('x_fc_repair_skills', 'in', [self.x_fc_repair_category_id.id])
)
candidates = Users.search(domain)
if not candidates:
# Fallback: any active field staff (skills filter relaxed).
candidates = Users.search([
('x_fc_is_field_staff', '=', True),
('active', '=', True),
])
if not candidates:
return False
# Pick the one with the fewest scheduled tasks today.
ranked = []
for u in candidates:
count = Task.search_count([
('technician_id', '=', u.id),
('scheduled_date', '=', today),
('status', 'not in', ('completed', 'cancelled')),
])
ranked.append((count, u.id))
ranked.sort()
return ranked[0][1]
def _notify_tech_of_rush(self, task):
"""Send a real-time bus push + email so the tech sees it mid-shift."""
for r in self:
tech = task.technician_id
if not tech:
continue
# 1) bus.bus live push (shows as a sticky in-app notification).
try:
# bus.bus.sendone goes to a specific user channel; the
# web client displays it via the simple_notification service.
self.env['bus.bus']._sendone(
tech.partner_id,
'simple_notification',
{
'type': 'warning',
'title': _('RUSH service added to your route'),
'message': (_('Stairlift / urgent stop at %(client)s. '
'Repair %(name)s. See your tasks.') % {
'client': r.partner_id.name or '',
'name': r.name,
}),
'sticky': True,
},
)
except Exception:
_logger.warning('bus.bus push failed for tech %s', tech.login)
# 2) email (matters if the tech is offline at the moment of squeeze).
tpl = self.env.ref(
'fusion_repairs.email_template_rush_tech_alert',
raise_if_not_found=False,
)
if tpl:
try:
tpl.with_context(tech_email=tech.email or tech.partner_id.email or '') \
.send_mail(r.id, force_send=True, email_values={
'email_to': tech.email or tech.partner_id.email or '',
})
except Exception:
_logger.warning('Rush-alert email failed for repair %s', r.name)
# 3) chatter on the task itself so the tech sees it inline.
task.message_post(body=Markup(_(
'<b>RUSH ADDED to your day:</b> %(client)s - %(name)s. '
'Office squeezed it in.'
)) % {
'client': r.partner_id.name or '?',
'name': r.name,
})
# ------------------------------------------------------------------
# M9 - Margin per repair (revenue - labour cost - parts cost)
# All non-stored computes; surfaced in the M7 analytics dashboard.