Skip to content

A free open source IT asset/license management system

Improve output by showing covered and declined

Marcus Moore9afc45990b08
diff --git a/app/Actions/Acceptances/RegenerateAcceptancesAction.php b/app/Actions/Acceptances/RegenerateAcceptancesAction.php
index cb1fa9a4f2..a6b89b930a 100644
--- a/app/Actions/Acceptances/RegenerateAcceptancesAction.php
+++ b/app/Actions/Acceptances/RegenerateAcceptancesAction.php
@@ -26,7 +26,7 @@ use Illuminate\Support\Facades\Mail;
*
* @phpstan-type Checkoutable Accessory|Asset|Component|Consumable|LicenseSeat
* @phpstan-type Candidate array{item: Checkoutable, user: User, units: int}
- * @phpstan-type ClassifiedCandidate array{item: Checkoutable, user: User, units: int, qty: int, outcome: string, declined: bool}
+ * @phpstan-type ClassifiedCandidate array{item: Checkoutable, user: User, units: int, qty: int, coverage: int, outcome: string, declined: bool}
*/
class RegenerateAcceptancesAction
{
@@ -307,6 +307,7 @@ class RegenerateAcceptancesAction
return [
...$candidate,
'qty' => $shortfall,
+ 'coverage' => $pendingCoverage,
'declined' => $declined,
'outcome' => match (true) {
$shortfall <= 0 => self::OUTCOME_COVERED,
@@ -317,7 +318,11 @@ class RegenerateAcceptancesAction
}
/**
- * Tallies one classified pair, adding a report row when it is one to re-request.
+ * Tallies one classified pair and records it under the outcome it landed in.
+ *
+ * Every outcome keeps its rows, not just the re-requested ones: a count on its own
+ * tells an operator that a pair was passed over without telling them which pair, and
+ * the ones this run declines to act on are exactly the ones they may need to chase.
*
* @param ClassifiedCandidate $pair
*/
@@ -327,6 +332,7 @@ class RegenerateAcceptancesAction
if ($pair['outcome'] === self::OUTCOME_COVERED) {
$result->alreadyCovered++;
+ $result->coveredRows[] = [...self::pairRow($pair), $pair['coverage']];
return;
}
@@ -337,6 +343,7 @@ class RegenerateAcceptancesAction
if ($pair['outcome'] === self::OUTCOME_DECLINED_EXCLUDED) {
$result->declinedAndExcluded++;
+ $result->declinedRows[] = self::pairRow($pair);
return;
}
@@ -344,14 +351,25 @@ class RegenerateAcceptancesAction
$type = class_basename($pair['item']);
$result->sendCountsByType[$type] = ($result->sendCountsByType[$type] ?? 0) + 1;
- $result->reportRows[] = [
+ $result->reportRows[] = [...self::pairRow($pair), $pair['qty']];
+ }
+
+ /**
+ * The columns every outcome's table shares, identifying the pair and what it holds.
+ * Each caller appends whatever its own outcome has to say about it.
+ *
+ * @param ClassifiedCandidate $pair
+ * @return array{0: int, 1: string, 2: string, 3: string, 4: int, 5: int}
+ */
+ private static function pairRow(array $pair): array
+ {
+ return [
$pair['user']->getKey(),
$pair['user']->present()->fullName,
$pair['item']->present()->name,
- $type,
+ class_basename($pair['item']),
$pair['item']->getKey(),
$pair['units'],
- $pair['qty'],
];
}
@@ -411,10 +429,17 @@ class RegenerateAcceptancesAction
* is asked once, for three items. A holder with no email address still keeps their
* rows — they will see them on /account/accept at their next login, just without the
* nudge — and is recorded on the run instead, for the caller to report.
+ *
+ * The dry-run guard is redundant today and deliberately kept: holders are collected
+ * in `createAcceptance()`, which a dry run never reaches, so this currently has an
+ * empty list to iterate whatever it does. That makes "a preview sends nothing" a
+ * property of where the accumulation happens rather than of anything stated here —
+ * move the accumulation and a preview starts emailing. The guard says the intent out
+ * loud so that move cannot be silent.
*/
private static function notifyHolders(RegenerateAcceptancesResult $result): void
{
- if (! $result->notify) {
+ if (! $result->notify || $result->dryRun) {
return;
}
diff --git a/app/Actions/Acceptances/RegenerateAcceptancesResult.php b/app/Actions/Acceptances/RegenerateAcceptancesResult.php
index 069cbf7122..d8b5366fd0 100644
--- a/app/Actions/Acceptances/RegenerateAcceptancesResult.php
+++ b/app/Actions/Acceptances/RegenerateAcceptancesResult.php
@@ -46,6 +46,22 @@ class RegenerateAcceptancesResult
*/
public array $reportRows = [];
+ /**
+ * The pairs a pending request already covers, which this run left alone. The last
+ * column is what that pending coverage is worth, against the units held beside it.
+ *
+ * @var array<int, array{0: int, 1: string, 2: string, 3: string, 4: int, 5: int, 6: int}>
+ */
+ public array $coveredRows = [];
+
+ /**
+ * The pairs held back by `excludeDeclined`, in the same columns as the others minus
+ * a units-to-re-request that is by definition none.
+ *
+ * @var array<int, array{0: int, 1: string, 2: string, 3: string, 4: int, 5: int}>
+ */
+ public array $declinedRows = [];
+
/**
* How many pairs each checkoutable type contributed.
*
diff --git a/app/Console/Commands/RegenerateAcceptances.php b/app/Console/Commands/RegenerateAcceptances.php
index 939c0a3c67..45b82a35b3 100644
--- a/app/Console/Commands/RegenerateAcceptances.php
+++ b/app/Console/Commands/RegenerateAcceptances.php
@@ -312,7 +312,8 @@ class RegenerateAcceptances extends Command
}
/**
- * Prints what the run re-requested, or under a dry run what it would have.
+ * Prints what the run re-requested, or under a dry run what it would have, followed
+ * by the pairs it passed over and why.
*/
private function printReport(RegenerateAcceptancesResult $result): int
{
@@ -335,8 +336,16 @@ class RegenerateAcceptances extends Command
$this->info('Previously declined: '.$result->previouslyDeclined.'.');
$this->info('Already covered by a pending request: '.$result->alreadyCovered.'.');
+ if ($result->coveredRows !== []) {
+ $this->table(['User ID', 'User', 'Item', 'Item Type', 'Item ID', 'Units currently held', 'Units already pending'], $result->coveredRows);
+ }
+
if ($result->excludeDeclined) {
$this->info('Previously declined and excluded: '.$result->declinedAndExcluded.'.');
+
+ if ($result->declinedRows !== []) {
+ $this->table(['User ID', 'User', 'Item', 'Item Type', 'Item ID', 'Units currently held'], $result->declinedRows);
+ }
}
$this->info($result->dryRun ? 'Nothing was created.' : 'Created: '.$result->created.'.');
diff --git a/tests/Feature/Console/RegenerateAcceptancesTest.php b/tests/Feature/Console/RegenerateAcceptancesTest.php
index 83e6fe3601..2d646198ab 100644
--- a/tests/Feature/Console/RegenerateAcceptancesTest.php
+++ b/tests/Feature/Console/RegenerateAcceptancesTest.php
@@ -666,6 +666,10 @@ class RegenerateAcceptancesTest extends TestCase
$this->artisan('snipeit:regenerate-acceptances', ['--no-interaction' => true])
->expectsOutput('To re-request: 0.')
->expectsOutput('Already covered by a pending request: 1.')
+ ->expectsTable(
+ ['User ID', 'User', 'Item', 'Item Type', 'Item ID', 'Units currently held', 'Units already pending'],
+ [[$holder->id, $holder->present()->fullName, $asset->present()->name, 'Asset', $asset->id, 1, 1]],
+ )
->assertExitCode(0);
}
@@ -681,6 +685,7 @@ class RegenerateAcceptancesTest extends TestCase
$this->artisan('snipeit:regenerate-acceptances', ['--no-interaction' => true])
->expectsOutput('To re-request: 1.')
->expectsOutput('Already covered by a pending request: 0.')
+ ->doesntExpectOutputToContain('Units already pending')
->assertExitCode(0);
}
@@ -750,6 +755,25 @@ class RegenerateAcceptancesTest extends TestCase
->assertExitCode(0);
}
+ public function test_the_covered_table_reports_the_units_the_pending_rows_are_worth(): void
+ {
+ $holder = User::factory()->create();
+ $accessory = Accessory::factory()->create(['category_id' => $this->acceptanceCategory('accessory')->id]);
+ $accessory->checkouts()->createMany([
+ ['assigned_to' => $holder->id, 'assigned_type' => User::class],
+ ['assigned_to' => $holder->id, 'assigned_type' => User::class],
+ ]);
+ $this->acceptanceFor($accessory, $holder)->pending()->create(['qty' => 2]);
+
+ $this->artisan('snipeit:regenerate-acceptances', ['--no-interaction' => true])
+ ->expectsOutput('To re-request: 0.')
+ ->expectsTable(
+ ['User ID', 'User', 'Item', 'Item Type', 'Item ID', 'Units currently held', 'Units already pending'],
+ [[$holder->id, $holder->present()->fullName, $accessory->present()->name, 'Accessory', $accessory->id, 2, 2]],
+ )
+ ->assertExitCode(0);
+ }
+
/**
* `acceptanceHistory()` deliberately over-fetches — it queries every candidate item id
* against every candidate user id, a cross product — so rows belonging to other
@@ -800,6 +824,10 @@ class RegenerateAcceptancesTest extends TestCase
->expectsOutput('To re-request: 0.')
->expectsOutput('Previously declined: 1.')
->expectsOutput('Previously declined and excluded: 1.')
+ ->expectsTable(
+ ['User ID', 'User', 'Item', 'Item Type', 'Item ID', 'Units currently held'],
+ [[$holder->id, $holder->present()->fullName, $asset->present()->name, 'Asset', $asset->id, 1]],
+ )
->assertExitCode(0);
}