Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
66.67% covered (warning)
66.67%
10 / 15
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApprovalStateResolver
66.67% covered (warning)
66.67%
10 / 15
33.33% covered (danger)
33.33%
1 / 3
17.33
0.00% covered (danger)
0.00%
0 / 1
 resolve
55.56% covered (warning)
55.56%
5 / 9
0.00% covered (danger)
0.00%
0 / 1
11.30
 amountChangedSinceNotified
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
 isPending
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace App\Services;
4
5/**
6 * Derives the SOP §4 approval-circuit state of a budget from the columns that
7 * already track it, so the banner and the approver pop-ups (Phase 4) all read
8 * one authoritative value instead of re-deriving ad-hoc from `for_approval` /
9 * `x_status` in each place.
10 *
11 * Pure (accepts any object with the relevant properties) → unit-testable
12 * without a database.
13 */
14class ApprovalStateResolver
15{
16    public const BORRADOR = 'borrador';        // draft, below threshold / not yet ready
17
18    public const LISTO = 'listo';              // ready to send, no approval needed
19
20    public const PENDIENTE = 'pendiente';      // approval requested, awaiting approver(s)
21
22    public const APROBADO = 'aprobado';        // approval circuit completed OK
23
24    public const NO_APROBADO = 'no_aprobado';  // rejected by an approver
25
26    /**
27     * Priority (most specific first):
28     *   1. pending      → for_approval set (still waiting on ≥1 approver)
29     *   2. no_aprobado  → a rejection is recorded and nothing is pending
30     *   3. aprobado     → an approval is recorded and nothing is pending
31     *   4. listo        → budget_status_id 11 ("Listo para enviar")
32     *   5. borrador     → everything else
33     */
34    public function resolve(object $q): string
35    {
36        if ($this->isPending($q)) {
37            return self::PENDIENTE;
38        }
39
40        if (($q->rejected_at ?? null) !== null || ($q->rejected_at_v2 ?? null) !== null) {
41            return self::NO_APROBADO;
42        }
43
44        if (($q->approved_at ?? null) !== null || ($q->approved_at_v2 ?? null) !== null) {
45            return self::APROBADO;
46        }
47
48        if ((int) ($q->budget_status_id ?? 0) === 11) {
49            return self::LISTO;
50        }
51
52        return self::BORRADOR;
53    }
54
55    /**
56     * True when the amount has moved since the approver was notified — drives
57     * the "el importe se ha modificado de X a Y" approver pop-up (Phase 4).
58     * Only meaningful while pending.
59     */
60    public function amountChangedSinceNotified(object $q): bool
61    {
62        $avisado = $q->importe_avisado ?? null;
63        if (! $this->isPending($q) || $avisado === null) {
64            return false;
65        }
66
67        return (float) $avisado !== (float) ($q->amount ?? 0);
68    }
69
70    private function isPending(object $q): bool
71    {
72        $forApproval = $q->for_approval ?? null;
73
74        return $forApproval !== null && (int) $forApproval !== 0;
75    }
76}