-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathRepairRevenueLifecycle.php
More file actions
140 lines (115 loc) · 5.02 KB
/
Copy pathRepairRevenueLifecycle.php
File metadata and controls
140 lines (115 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
namespace Fleetbase\Ledger\Console\Commands;
use Fleetbase\Ledger\Models\Invoice;
use Fleetbase\Ledger\Services\RevenueLifecycleService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class RepairRevenueLifecycle extends Command
{
protected $signature = 'fleetops:repair-revenue-lifecycle
{--apply : Apply repairs instead of reporting only}
{--limit=100 : Maximum records per category to repair in one run}';
protected $description = 'Audit and repair Ledger revenue lifecycle state for deleted or canceled FleetOps orders and invoices';
public function __construct(protected RevenueLifecycleService $revenueLifecycleService)
{
parent::__construct();
}
public function handle(): int
{
$apply = (bool) $this->option('apply');
$limit = max(1, (int) $this->option('limit'));
$this->info($apply ? '[Ledger] Applying revenue lifecycle repairs...' : '[Ledger] Dry run: revenue lifecycle repair audit.');
$orderClass = 'Fleetbase\\FleetOps\\Models\\Order';
if (!class_exists($orderClass)) {
// FleetOps API is a required Composer dependency of Ledger. This
// fallback only protects partially installed runtime environments.
// @codeCoverageIgnoreStart
$this->warn('[Ledger] FleetOps Order model is unavailable; skipping order-linked repairs.');
// @codeCoverageIgnoreEnd
} else {
$this->reportOrders($orderClass, $apply, $limit);
}
$this->reportInvoices($apply, $limit);
$this->reportMissingReversals();
$this->info($apply ? '[Ledger] Repair run complete.' : '[Ledger] Dry run complete. Re-run with --apply to repair eligible records.');
return self::SUCCESS;
}
private function reportOrders(string $orderClass, bool $apply, int $limit): void
{
$query = $orderClass::withTrashed()
->where(function ($query) {
$query->whereNotNull('deleted_at')
->orWhereIn('status', ['canceled', 'cancelled', 'order_canceled']);
});
$total = (clone $query)->count();
$orders = $query
->limit($limit)
->get();
$this->line("[Ledger] Inactive/deleted FleetOps orders found: {$total}");
$this->sample($orders);
if (!$apply) {
return;
}
foreach ($orders as $order) {
$this->revenueLifecycleService->repairOrder($order, 'repair_command');
}
}
private function reportInvoices(bool $apply, int $limit): void
{
$query = Invoice::withTrashed()
->without(['customer', 'items', 'template', 'order.trackingNumber'])
->where(function ($query) {
$query->whereNotNull('deleted_at')
->orWhereIn('status', ['void', 'voided', 'cancelled', 'canceled']);
});
$total = (clone $query)->count();
$invoices = $query
->limit($limit)
->get();
$this->line("[Ledger] Deleted/void/cancelled invoices found: {$total}");
$this->sample($invoices);
if (!$apply) {
return;
}
foreach ($invoices as $invoice) {
$this->revenueLifecycleService->repairInvoice($invoice, 'repair_command');
}
}
private function reportMissingReversals(): void
{
$missing = DB::table('ledger_journals as journals')
->where('journals.type', 'revenue_recognition')
->where('journals.status', 'posted')
->whereNull('journals.deleted_at')
->whereNotExists(function ($query) {
$query->selectRaw('1')
->from('ledger_journals as reversals')
->where('reversals.type', 'revenue_recognition_reversal')
->whereNull('reversals.deleted_at')
->whereRaw("JSON_UNQUOTE(JSON_EXTRACT(reversals.meta, '$.reverses_journal_uuid')) = journals.uuid");
})
->whereExists(function ($query) {
$query->selectRaw('1')
->from('ledger_invoices')
->whereRaw("ledger_invoices.uuid = JSON_UNQUOTE(JSON_EXTRACT(journals.meta, '$.invoice_uuid'))")
->where(function ($invoiceQuery) {
$invoiceQuery->whereNotNull('ledger_invoices.deleted_at')
->orWhereIn('ledger_invoices.status', ['void', 'voided', 'cancelled', 'canceled']);
});
})
->count();
$this->line("[Ledger] Revenue-recognition journals missing reversals for inactive invoices: {$missing}");
}
private function sample($records): void
{
$ids = $records
->take(5)
->map(fn ($record) => $record->public_id ?? $record->uuid)
->filter()
->values()
->all();
if ($ids) {
$this->line('[Ledger] Sample: ' . implode(', ', $ids));
}
}
}