mivita/app/Console/Commands/FixPaymentLinkStatus.php
2026-02-20 17:55:06 +01:00

119 lines
3.4 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\ShoppingInstance;
use App\Models\ShoppingPayment;
use Illuminate\Console\Command;
class FixPaymentLinkStatus extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'payment:fix-link-status {--dry-run : Run without making changes}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Fix payment link status for paid orders that have incorrect status in shopping_instances';
/**
* Execute the console command.
*/
public function handle()
{
$isDryRun = $this->option('dry-run');
if ($isDryRun) {
$this->info(' DRY RUN MODE - No changes will be made');
$this->newLine();
}
$this->info('🔎 Searching for payment links with incorrect status...');
$this->newLine();
// Find all ShoppingPayments with identifier that are paid
$paidPayments = ShoppingPayment::whereNotNull('identifier')
->whereHas('shopping_order', function ($query) {
$query->where('paid', 1)
->where('txaction', 'paid');
})
->with(['shopping_order'])
->get();
$this->info("Found {$paidPayments->count()} paid payments with identifiers");
$this->newLine();
$fixed = 0;
$skipped = 0;
$errors = 0;
foreach ($paidPayments as $payment) {
$identifier = $payment->identifier;
// Find the corresponding ShoppingInstance
$instance = ShoppingInstance::where('identifier', $identifier)->first();
if (! $instance) {
$this->warn("⚠️ ShoppingInstance not found for identifier: {$identifier}");
$errors++;
continue;
}
// Check if status needs to be updated
if ($instance->status < 10) {
$oldStatus = $instance->status;
$oldStatusName = $instance->getStatus();
if (! $isDryRun) {
$instance->status = 10; // link_paid
$instance->save();
}
$this->line(sprintf(
'%s Payment #%d: %s → %s (Order #%d, Amount: %s)',
$isDryRun ? '📋' : '✅',
$payment->id,
$oldStatusName." ($oldStatus)",
'link_paid (10)',
$payment->shopping_order_id,
$payment->getPaymentAmount()
));
$fixed++;
} else {
$skipped++;
}
}
$this->newLine();
$this->info('📊 Summary:');
$this->table(
['Status', 'Count'],
[
['Fixed/Would fix', $fixed],
['Already correct', $skipped],
['Errors', $errors],
['Total processed', $paidPayments->count()],
]
);
if ($isDryRun && $fixed > 0) {
$this->newLine();
$this->warn('⚠️ This was a DRY RUN. Run without --dry-run to apply changes.');
}
if (! $isDryRun && $fixed > 0) {
$this->newLine();
$this->info("✨ Successfully updated {$fixed} payment link(s)!");
}
return 0;
}
}