ldc-supportweb/database/seeders/InvoicingSeeder.php
2022-08-30 09:22:06 +02:00

54 lines
1.7 KiB
PHP

<?php
namespace Database\Seeders;
use App\Models\Company;
use App\Models\Invoice;
use App\Models\Package;
use App\Models\Site;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class InvoicingSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
/** Reset all invoices */
DB::table((new Invoice())->getTable())->truncate();
/** Reset all packages (forfaits) */
Package::all()->map(function (Package $model) {
$model->deleteOrFail();
});
Package::factory(6)->create();
/** Reset all companies */
Company::all()->map(function (Company $model) {
$model->deleteOrFail();
});
Company::factory(6)->create();
Invoice::factory(45)->make()->each(function (Invoice $model) {
$model->ref = \sprintf(
'%s_%s_%s',
Carbon::now()->isoFormat('YYYY-MM'),
Str::padLeft((Invoice::whereYear('created_at', Carbon::now()->isoFormat('YYYY'))
->whereMonth('created_at', Carbon::now()->isoFormat('MM'))
->count() + 1) ?: 1, 4, '0'),
Str::substr(Str::upper(Str::slug(User::query()->inRandomOrder()->first()->name)), 0, 1)
);
$model->site()->associate(Site::query()->inRandomOrder()->first());
$model->company()->associate(Company::query()->inRandomOrder()->first());
$model->companyInvoiced()->associate(Company::query()->inRandomOrder()->first());
$model->saveQuietly();
});
}
}