ifnex/04_Laravel/database/seeders/SampleDataSeeder.php
Kazem Alghasi a699c2dbee feat(laravel): implement user synchronization and shipment schema
Add new core functionality and database structures to support WordPress
integration and shipment tracking.

- Add `SyncWordPressUsers` Artisan command to synchronize users between
  WordPress and Laravel.
- Create `shipment_items` table migration.
- Fix `discount_codes` enum type by changing `percent` to `percentage`.
- Add `SampleDataSeeder` for testing shipments, transactions, and
  discount codes.
- Update project status documentation.
2026-08-08 04:58:01 +03:30

263 lines
11 KiB
PHP

<?php
namespace Database\Seeders;
use App\Models\Country;
use App\Models\DiscountCode;
use App\Models\Shipment;
use App\Models\ShipmentCarrierMapping;
use App\Models\ShipmentItem;
use App\Models\ShipmentTrackingEvent;
use App\Models\ShippingRate;
use App\Models\User;
use App\Models\Wallet;
use App\Models\WalletTransaction;
use App\Enums\ShipmentDirection;
use App\Enums\ShipmentStatus;
use App\Enums\ShipmentType;
use App\Enums\TransactionStatus;
use App\Enums\TransactionType;
use App\Enums\PaymentGateway;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class SampleDataSeeder extends Seeder
{
public function run(): void
{
DB::transaction(function () {
// Get or create a customer user
$customer = User::firstOrCreate(
['email' => 'customer@ifnex.ir'],
[
'name' => 'مشتری تست',
'password' => bcrypt('password'),
'phone' => '09123456789',
'role' => \App\Enums\UserRole::Customer,
'is_active' => true,
]
);
// Create wallet for customer
$wallet = Wallet::firstOrCreate(
['user_id' => $customer->id],
[
'balance' => 2500000,
'total_deposited' => 5000000,
'total_withdrawn' => 2500000,
]
);
// Sample transactions
$transactions = [
[
'amount' => 5000000,
'type' => TransactionType::DEPOSIT,
'status' => TransactionStatus::COMPLETED,
'gateway' => PaymentGateway::ZARINPAL,
'description' => 'شارژ آنلاین کیف پول',
'gateway_reference_id' => 'REF100001',
'created_at' => now()->subDays(15),
],
[
'amount' => -1500000,
'type' => TransactionType::ORDER_PAYMENT,
'status' => TransactionStatus::COMPLETED,
'gateway' => PaymentGateway::SYSTEM,
'description' => 'پرداخت سفارش #1001',
'created_at' => now()->subDays(10),
],
[
'amount' => -1000000,
'type' => TransactionType::WITHDRAWAL,
'status' => TransactionStatus::COMPLETED,
'gateway' => PaymentGateway::MANUAL,
'description' => 'کسر دستی توسط ادمین',
'admin_notes' => 'بدهی مشتری',
'created_at' => now()->subDays(5),
],
];
foreach ($transactions as $t) {
WalletTransaction::create(array_merge($t, [
'wallet_id' => $wallet->id,
'balance_before' => $wallet->balance - $t['amount'],
'balance_after' => $wallet->balance,
'created_by' => 1,
'approved_by' => 1,
'approved_at' => now()->subDays(15),
]));
}
// Sample discount codes
$discounts = [
[
'code' => 'WELCOME10',
'type' => 'percentage',
'value' => 10,
'min_order_amount' => 500000,
'usage_limit' => 100,
'used_count' => 23,
'is_active' => true,
],
[
'code' => 'SUMMER500',
'type' => 'fixed',
'value' => 500000,
'min_order_amount' => 2000000,
'usage_limit' => 50,
'used_count' => 12,
'is_active' => true,
],
[
'code' => 'VIP20',
'type' => 'percentage',
'value' => 20,
'min_order_amount' => 1000000,
'usage_limit' => 20,
'used_count' => 5,
'is_active' => true,
],
];
foreach ($discounts as $d) {
DiscountCode::create($d);
}
// Sample shipments
$countries = Country::where('is_active', true)->get();
$fromCountry = $countries->where('iso_code', 'IR')->first();
$toCountries = $countries->where('iso_code', '!=', 'IR')->take(5);
$shipments = [];
$awbCounter = 980100000;
foreach ($toCountries as $toCountry) {
$awbCounter++;
$shipment = Shipment::create([
'awb_no' => (string) $awbCounter,
'direction' => ShipmentDirection::Export,
'type' => ShipmentType::Parcel,
'status' => ShipmentStatus::Delivered,
'from_country_id' => $fromCountry->id,
'to_country_id' => $toCountry->id,
'weight' => rand(5, 50) / 10,
'volumetric_weight' => rand(5, 60) / 10,
'chargeable_weight' => rand(5, 55) / 10,
'shipping_price' => rand(50, 200),
'extra_service' => rand(0, 30),
'packing_cost' => rand(0, 100000),
'domestic_pickup' => rand(0, 50000),
'domestic_delivery' => rand(0, 50000),
'warehousing_cost' => rand(0, 200000),
'discount' => rand(0, 50000),
'total_fee' => rand(500000, 3000000),
'sender_name' => 'فرستنده نمونه ' . $awbCounter,
'sender_phone' => '0912' . rand(100000, 999999),
'sender_address' => 'اصفهان، خیابان چهارباغ',
'receiver_name' => 'گیرنده نمونه ' . $toCountry->name,
'receiver_phone' => '+98' . rand(9120000000, 9899999999),
'receiver_address' => $toCountry->name . '، آدرس نمونه',
'created_at' => now()->subDays(rand(1, 30)),
]);
// Add carrier mapping
ShipmentCarrierMapping::create([
'shipment_id' => $shipment->id,
'carrier_code' => ['DHL', 'FEDEX', 'UPS'][rand(0, 2)],
'carrier_tracking_number' => 'TRK' . rand(100000, 999999),
'direction' => 'export',
'delivery_status' => 'delivered',
]);
// Add tracking events
$events = [
['delivery_status' => 'processed', 'description' => 'مرسوله ثبت شد', 'days_ago' => 10],
['delivery_status' => 'picked_up', 'description' => 'از انبار حمل شد', 'days_ago' => 9],
['delivery_status' => 'in_transit', 'description' => 'در حال حمل به مقصد', 'days_ago' => 7],
['delivery_status' => 'out_for_delivery', 'description' => 'برای تحویل در حال ارسال است', 'days_ago' => 3],
['delivery_status' => 'delivered', 'description' => 'تحویل داده شد', 'days_ago' => 2],
];
foreach ($events as $index => $event) {
ShipmentTrackingEvent::create([
'shipment_id' => $shipment->id,
'delivery_status' => $event['delivery_status'],
'event_description' => $event['description'],
'event_date' => now()->subDays($event['days_ago'])->toDateString(),
'event_time' => now()->subDays($event['days_ago'])->toTimeString(),
'location' => $event['days_ago'] > 3 ? 'اصفهان' : $toCountry->name,
'source' => 'manual',
]);
}
// Add shipment items
ShipmentItem::create([
'shipment_id' => $shipment->id,
'name' => 'کالای نمونه ' . $awbCounter,
'hs_code' => '6110.' . rand(10, 99),
'quantity' => rand(1, 10),
'unit_price_usd' => rand(10, 500),
'total_usd' => rand(100, 5000),
]);
$shipments[] = $shipment;
}
// Add one pending shipment
$pendingCountry = $countries->where('iso_code', 'DE')->first();
if ($pendingCountry) {
$shipment = Shipment::create([
'awb_no' => (string) ++$awbCounter,
'direction' => ShipmentDirection::Export,
'type' => ShipmentType::Parcel,
'status' => ShipmentStatus::InTransit,
'from_country_id' => $fromCountry->id,
'to_country_id' => $pendingCountry->id,
'weight' => 12.5,
'volumetric_weight' => 15.0,
'chargeable_weight' => 15.0,
'shipping_price' => 85.00,
'extra_service' => 10.00,
'packing_cost' => 100000,
'total_fee' => 1850000,
'sender_name' => 'فرستنده در حال ارسال',
'sender_phone' => '09351234567',
'sender_address' => 'تهران، خیابان ولیعصر',
'receiver_name' => 'گیرنده آلمانی',
'receiver_phone' => '+491711234567',
'receiver_address' => 'برلین، آدرس نمونه',
'created_at' => now()->subDays(5),
]);
ShipmentCarrierMapping::create([
'shipment_id' => $shipment->id,
'carrier_code' => 'DHL',
'carrier_tracking_number' => 'DHL' . rand(100000, 999999),
'direction' => 'export',
'delivery_status' => 'in_transit',
]);
ShipmentTrackingEvent::create([
'shipment_id' => $shipment->id,
'delivery_status' => 'in_transit',
'event_description' => 'در مرکز بازبسته‌ریزی',
'event_date' => now()->subDays(2)->toDateString(),
'event_time' => now()->subDays(2)->toTimeString(),
'location' => 'استانبول',
'source' => 'manual',
]);
}
$this->command->info('✅ داده‌های نمونه با موفقیت ایجاد شدند:');
$this->command->info(' - 1 کاربر مشتری');
$this->command->info(' - 1 کیف پول با موجودی 2,500,000 ریال');
$this->command->info(' - 3 تراکنش کیف پول');
$this->command->info(' - 3 کد تخفیف فعال');
$this->command->info(' - ' . count($shipments) . ' مرسوله با رویداد ترکینگ');
$this->command->info(' - ' . count($shipments) . ' آیتم گمرکی');
});
}
}