File size: 2,270 Bytes
70ba896 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
<?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...');
// Get all orders and create revenue tracking entries
$orders = Order::where('status', '!=', 'cancelled')->get();
foreach ($orders as $order) {
// Parse cart data if it exists
$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, // 70% cost, 30% profit
'profit_margin' => 0.30,
'quantity' => $item['quantity'] ?? 1,
'created_at' => $order->created_at,
'updated_at' => $order->updated_at
]);
}
} else {
// If no cart data, create a single entry for the order
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());
}
} |