File: /var/www/ipsremont-demo/database/seeds/PartsStorageSeeder.php
<?php
use App\Models\PartsStorage;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class PartsStorageSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
PartsStorage::truncate();
$central = \App\Services\Branch\BranchService::getCentral();
if (!$central) {
$central = \App\Models\Branch::query()->inRandomOrder()->first();
$central->central = 1;
$central->save();
}
\App\Models\Branch::query()->update(
['external_id' => DB::raw('CONCAT(id, "-ext")')]
);
$parts = \App\Models\Part::query()->orderBy('name', 'asc')->get('id')->pluck('id')->toArray();
$central = \App\Services\Branch\BranchService::getCentral();
if (!$central) {
die("Выберите центральное подразделение");
}
$warehouses = \App\Models\Branch::displayed()
->where('id', '<>', $central->id)
->limit(10)
->get();
$services = \App\Models\Service\Service::displayed()
->where('external_warehouse_id', '<>', '')
->limit(10)
->get('external_warehouse_id')->pluck('external_warehouse_id')->toArray();
foreach ($parts as $partId) {
if (in_array($partId, [1, 6, 11]) || $partId >= 15) {
PartsStorage::create([
'part_id' => $partId,
'warehouse_id' => $central->id,
'external_warehouse_id' => $central->external_id,
'amount' => rand(1, 50),
'type' => PartsStorage::branch,
]);
}
if ($partId >= 5) {
foreach ($warehouses as $warehouse) {
PartsStorage::create([
'part_id' => $partId,
'warehouse_id' => $warehouse->id,
'external_warehouse_id' => $warehouse->external_id,
'amount' => rand(1, 50),
'type' => PartsStorage::branch,
]);
}
}
if ($partId >= 10) {
foreach ($services as $service) {
PartsStorage::create([
'part_id' => $partId,
'warehouse_id' => '',
'external_warehouse_id' => $service,
'amount' => rand(1, 50),
'type' => PartsStorage::service,
]);
}
}
}
}
}