Summary of completed work: - Complete buyer portal (browse, cart, checkout, orders, invoices) - Complete seller portal (orders, manifests, fleet, picking) - Business onboarding wizards (buyer 4-step, seller 5-step) - Email verification and registration flows - Notification system for buyers and sellers - Payment term surcharges and pickup/delivery workflows - Filament admin resources (Business, Brand, Product, Order, Invoice, User) - 51 migrations executed successfully Renamed Company -> Business throughout codebase for consistency. Unified authentication flows with password reset. Added audit trail and telescope for debugging. Next: Week 4 Data Migration (Days 22-28) - Product migration (883 products from cannabrands_crm) - Company migration (81 buyer companies) - User migration (preserve password hashes) - Order history migration
72 lines
2.0 KiB
PHP
72 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\Business;
|
|
use Illuminate\Console\Command;
|
|
|
|
class BackfillBusinessOwners extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'businesses:backfill-owners';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Backfill owner_user_id for existing businesses based on primary or first user';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle()
|
|
{
|
|
$this->info('Backfilling business owners...');
|
|
|
|
// Get all businesses without an owner set
|
|
$businesses = Business::whereNull('owner_user_id')->get();
|
|
|
|
if ($businesses->isEmpty()) {
|
|
$this->info('No businesses found without an owner.');
|
|
return Command::SUCCESS;
|
|
}
|
|
|
|
$this->info("Found {$businesses->count()} businesses without an owner.");
|
|
|
|
$updated = 0;
|
|
$skipped = 0;
|
|
|
|
foreach ($businesses as $business) {
|
|
// Try to find primary user first
|
|
$primaryUser = $business->users()->wherePivot('is_primary', true)->first();
|
|
|
|
// If no primary user, get the first user
|
|
$owner = $primaryUser ?? $business->users()->first();
|
|
|
|
if ($owner) {
|
|
$business->update(['owner_user_id' => $owner->id]);
|
|
$this->line("✓ Business #{$business->id} ({$business->name}): Set owner to {$owner->first_name} {$owner->last_name} ({$owner->email})");
|
|
$updated++;
|
|
} else {
|
|
$this->warn("✗ Business #{$business->id} ({$business->name}): No users found, skipping");
|
|
$skipped++;
|
|
}
|
|
}
|
|
|
|
$this->newLine();
|
|
$this->info("Backfill complete!");
|
|
$this->info("Updated: {$updated}");
|
|
|
|
if ($skipped > 0) {
|
|
$this->warn("Skipped: {$skipped} (no users associated)");
|
|
}
|
|
|
|
return Command::SUCCESS;
|
|
}
|
|
}
|