Refactor script
This commit is contained in:
69
database/seeders/MerchantSeeder.php
Normal file
69
database/seeders/MerchantSeeder.php
Normal file
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use Illuminate\Database\Seeder;
|
||||
use App\Models\Merchant;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use App\Models\Customer; // Added Customer model
|
||||
|
||||
class MerchantSeeder extends Seeder
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$path = base_path('merchants.json');
|
||||
|
||||
if (!File::exists($path)) {
|
||||
$this->command->error('merchants.json not found!');
|
||||
return;
|
||||
}
|
||||
|
||||
$json = File::get($path);
|
||||
$data = json_decode($json, true);
|
||||
|
||||
if ($data) {
|
||||
// Find Agent Master to assign customers to
|
||||
$agent = \App\Models\Agent::where('agent_id', 'AGT-MASTER')->first();
|
||||
$agentId = $agent ? $agent->id : null;
|
||||
|
||||
if (!$agentId) {
|
||||
$this->command->warn("Agent AGT-MASTER not found. Customers will be unassigned.");
|
||||
}
|
||||
|
||||
$count = 0;
|
||||
foreach ($data as $item) {
|
||||
// Ensure numeric values are properly cast
|
||||
if (isset($item['latitude']))
|
||||
$item['latitude'] = (float) $item['latitude'];
|
||||
if (isset($item['longitude']))
|
||||
$item['longitude'] = (float) $item['longitude'];
|
||||
|
||||
// Consistently use Customer model only
|
||||
// Mapping fields
|
||||
Customer::updateOrCreate(
|
||||
[
|
||||
'name' => $item['name'],
|
||||
// Use lat/lng as composite key to avoid duplicates if name matches but location diff
|
||||
'latitude' => $item['latitude'],
|
||||
'longitude' => $item['longitude']
|
||||
],
|
||||
[
|
||||
'name' => $item['name'],
|
||||
'address' => $item['address'] ?? '',
|
||||
'city' => $item['city'] ?? '',
|
||||
'latitude' => $item['latitude'],
|
||||
'longitude' => $item['longitude'],
|
||||
'category' => $item['category'] ?? 'other', // Map category from json
|
||||
'agent_id' => $agentId, // Assign to agent
|
||||
'owner_name' => 'Owner ' . $item['name'], // Dummy owner
|
||||
'phone' => '0812' . rand(10000000, 99999999), // Dummy phone
|
||||
// pic_sales_id left null for now
|
||||
]
|
||||
);
|
||||
|
||||
$count++;
|
||||
}
|
||||
$this->command->info("Seeded {$count} Customers from merchants.json for Agent: " . ($agent ? 'AGT-MASTER' : 'None'));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user