|
|
<?php
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
use Illuminate\Console\Command;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\RevenueTracking;
|
|
|
use Carbon\Carbon;
|
|
|
|
|
|
class PopulateAnalyticsData extends Command
|
|
|
{
|
|
|
protected $signature = 'analytics:populate';
|
|
|
protected $description = 'Populate analytics data from existing orders';
|
|
|
|
|
|
public function handle()
|
|
|
{
|
|
|
$this->info('Populating analytics data from existing orders...');
|
|
|
|
|
|
|
|
|
$orders = Order::where('status', '!=', 'cancelled')->get();
|
|
|
|
|
|
foreach ($orders as $order) {
|
|
|
|
|
|
$cartData = json_decode($order->cart_data, true) ?? [];
|
|
|
|
|
|
if (!empty($cartData)) {
|
|
|
foreach ($cartData as $item) {
|
|
|
RevenueTracking::updateOrCreate([
|
|
|
'order_id' => $order->id,
|
|
|
'product_name' => $item['name'] ?? 'Unknown Product'
|
|
|
], [
|
|
|
'amount' => $item['price'] ?? 0,
|
|
|
'cost' => ($item['price'] ?? 0) * 0.7,
|
|
|
'profit_margin' => 0.30,
|
|
|
'quantity' => $item['quantity'] ?? 1,
|
|
|
'created_at' => $order->created_at,
|
|
|
'updated_at' => $order->updated_at
|
|
|
]);
|
|
|
}
|
|
|
} else {
|
|
|
|
|
|
RevenueTracking::updateOrCreate([
|
|
|
'order_id' => $order->id,
|
|
|
'product_name' => 'Order #' . $order->id
|
|
|
], [
|
|
|
'amount' => $order->total_amount,
|
|
|
'cost' => $order->total_amount * 0.7,
|
|
|
'profit_margin' => 0.30,
|
|
|
'quantity' => 1,
|
|
|
'created_at' => $order->created_at,
|
|
|
'updated_at' => $order->updated_at
|
|
|
]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
$this->info('Analytics data populated successfully!');
|
|
|
$this->info('Total revenue tracking entries: ' . RevenueTracking::count());
|
|
|
}
|
|
|
} |