ldc-crm/database/seeders/BrandStatSeeder.php
2021-11-05 14:46:53 +01:00

53 lines
1.7 KiB
PHP

<?php
namespace Database\Seeders;
use App\Models\Brand;
use App\Models\BrandStat;
use Illuminate\Database\Seeder;
use Illuminate\Support\Carbon;
class BrandStatSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
BrandStat::truncate();
// on génère 1 an pour chaque marque
$brands = Brand::all();
/** @var \App\Models\Brand $brand*/
foreach ($brands as $brand) {
$startDay = Carbon::now();
$total = rand(100, 99999);
$active = rand(2, 20) / 100;
$new = rand(10, 30) / 100;
for ($i = 365; $i > 0; $i--) {
$total += floor($i / 2);
// pourcentage active
$pActive = $active + ($i * 100 / 365) / 100 - 0.2;
$pActive = $pActive < 0.3 ? 0.3 : $pActive;
$pInactive = 1 - $pActive;
// pourcentage nouveau
$pNew = $new;
$pOld = 1 - $pNew;
$brandStat = new BrandStat([
'active' => \floor($total * $pActive),
'inactive' => \ceil($total * $pInactive),
'old_accounts' => \ceil($total * $pOld),
'new_accounts' => \floor($total * $pNew),
'total_accounts' => $total,
'total_opt_in' => ceil($total * 0.15) // 15% optin
]);
$brandStat->brand()->associate($brand);
$brandStat->created_at = $startDay;
$brandStat->save();
$startDay = $startDay->subDay();
}
}
}
}