104 lines
7.7 KiB
PHP
104 lines
7.7 KiB
PHP
<?php
|
|
|
|
use App\Models\SupplyRecord;
|
|
use App\Models\Tenant;
|
|
use App\Models\User;
|
|
use App\Services\CapitalRegister;
|
|
use App\Services\OperationalOrders;
|
|
use App\Services\OrganizationConfiguration;
|
|
use App\Services\RequisitionWorkflow;
|
|
use App\Services\SupplyPlanning;
|
|
use Illuminate\Contracts\Console\Kernel;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
require dirname(__DIR__).'/vendor/autoload.php';
|
|
$app = require dirname(__DIR__).'/bootstrap/app.php';
|
|
$app->make(Kernel::class)->bootstrap();
|
|
if (($argv[1] ?? '') !== '--initialize-authorized-synthetic' || config('app.demonstration') !== true || DB::connection()->getDatabaseName() !== 'compras_demo_20261129') {
|
|
throw new LogicException('Explicit public synthetic company required.');
|
|
}
|
|
try {
|
|
$admin = User::query()->where('email', 'admin@demo.compras.tempo.co.mz')->sole();
|
|
auth()->guard()->setUser($admin);
|
|
$organization = app(OrganizationConfiguration::class);
|
|
$version = fn () => DB::table('organization_access')->where('tenant_id', $admin->tenant_id)->value('version');
|
|
if (! User::query()->where('email', 'admin_review@demo.compras.tempo.co.mz')->exists()) {
|
|
$bytes = file_get_contents($argv[2]);
|
|
if (! is_string($bytes)) {
|
|
throw new LogicException('Private synthetic administrator credential unavailable.');
|
|
}
|
|
$credential = json_decode($bytes, true, 32, JSON_THROW_ON_ERROR)['admin_review'];
|
|
$profile = DB::table('organization_memberships')->where('tenant_id', $admin->tenant_id)->where('user_id', $admin->id)->value('profile_id');
|
|
$organization->execute($admin, 'user', ['version' => $version(), 'department_id' => $admin->department_id,
|
|
'name' => 'Administração suplente — Demonstração', 'email' => $credential['email'], 'password' => $credential['password'],
|
|
'profile_ids' => [$profile], 'reason' => 'Segundo administrador permanente fictício para preservar o mínimo nativo.']);
|
|
}
|
|
if (Tenant::query()->findOrFail($admin->tenant_id)->rules_provisional) {
|
|
$organization->execute($admin, 'rules', ['version' => $version(), 'first_limit' => '25000.00', 'second_limit' => '150000.00', 'includes_tax' => true,
|
|
'reason' => 'Alçadas exclusivamente ilustrativas da empresa fictícia para apresentação.']);
|
|
}
|
|
$names = ['admin' => 'Administração', 'admin_review' => 'Administração suplente', 'requester' => 'Requisitante', 'head' => 'Chefia',
|
|
'procurement' => 'Compras', 'finance' => 'Finanças — preparação', 'finance_review' => 'Finanças — revisão', 'warehouse' => 'Armazém', 'direction' => 'Direcção', 'auditor' => 'Auditoria'];
|
|
foreach ($names as $key => $name) {
|
|
$user = User::query()->where('tenant_id', $admin->tenant_id)->where('email', $key.'@demo.compras.tempo.co.mz')->sole();
|
|
if ($user->name !== $name.' — Demonstração') {
|
|
$before = $user->only(['id', 'name']);
|
|
$user->name = $name.' — Demonstração';
|
|
$user->save();
|
|
app(RequisitionWorkflow::class)->audit($admin, 'demonstration.label_corrected', null, $before, $user->only(['id', 'name']));
|
|
}
|
|
}
|
|
$reviewer = User::query()->where('email', 'finance_review@demo.compras.tempo.co.mz')->sole();
|
|
$profileId = DB::table('organization_memberships')->where('user_id', $reviewer->id)->value('profile_id');
|
|
$profile = DB::table('organization_profiles')->where('id', $profileId)->first();
|
|
$permissions = json_decode($profile->permissions, true, 32, JSON_THROW_ON_ERROR);
|
|
if (! in_array('supply_review', $permissions, true)) {
|
|
$organization->execute($admin, 'profile', ['version' => $version(), 'id' => $profileId, 'name' => 'Finanças — revisão de demonstração',
|
|
'permissions' => [...$permissions, 'supply_read', 'supply_review'], 'work_contexts' => ['procurement.internal', 'people.internal'],
|
|
'reason' => 'Conferência independente fictícia das políticas de stock na demonstração.']);
|
|
}
|
|
$orders = app(OperationalOrders::class);
|
|
$policy = SupplyRecord::query()->where('kind', 'op_order_policy')->latest('version')->first();
|
|
if ($policy === null) {
|
|
$policy = $orders->configure($admin, ['operation_key' => (string) Str::uuid(), 'version' => 0, 'mode' => 'procurement',
|
|
'reason' => 'Cadastro por Compras quando os locais de utilização não estão abrangidos, apenas para demonstração.']);
|
|
}
|
|
$buyer = User::query()->where('email', 'procurement@demo.compras.tempo.co.mz')->sole();
|
|
auth()->guard()->setUser($buyer);
|
|
foreach (['production' => 'DEMO-OP-001', 'maintenance' => 'DEMO-OM-001'] as $type => $reference) {
|
|
if (! SupplyRecord::query()->where('kind', 'op_order')->where('data->reference', $reference)->exists()) {
|
|
$orders->save($buyer, ['operation_key' => (string) Str::uuid(), 'type' => $type, 'reference' => $reference,
|
|
'department_id' => $buyer->department_id, 'utilisation_location' => 'Local fictício de demonstração', 'description' => 'Ordem fictícia para demonstrar a origem das necessidades de compra.',
|
|
'policy_version' => $policy->version, 'reason' => 'Cadastro sintético pelo agente de Compras conforme a origem seleccionada.']);
|
|
}
|
|
}
|
|
foreach (['project' => 'DEMO-PROJECTO', 'asset' => 'DEMO-ACTIVO'] as $type => $code) {
|
|
if (! SupplyRecord::query()->where('kind', 'capital_register')->where('data->code', $code)->exists()) {
|
|
app(CapitalRegister::class)->save($buyer, ['operation_key' => (string) Str::uuid(), 'version' => 0, 'type' => $type, 'code' => $code,
|
|
'name' => ($type === 'project' ? 'Projecto' : 'Activo').' fictício de demonstração', 'department_id' => $buyer->department_id,
|
|
'reference' => 'Referência ilustrativa própria de Compras', 'state' => 'active', 'reason' => 'Cadastro fictício independente, sem lançamento ou integração SGF.']);
|
|
}
|
|
}
|
|
$planning = app(SupplyPlanning::class);
|
|
$stockPolicy = $planning->latest('policy')->where('stock_item_id', 1)->where('warehouse_id', 1)->first();
|
|
if ($stockPolicy === null) {
|
|
$stockPolicy = $planning->save($buyer, 'policy', ['operation_key' => (string) Str::uuid(), 'version' => 0, 'action' => 'save',
|
|
'reason' => 'Valores ilustrativos de stock mínimo, natureza e utilização na empresa fictícia.',
|
|
'data' => ['stock_item_id' => 1, 'warehouse_id' => 1, 'purpose' => 'internal', 'origin' => 'local', 'safety_milli' => 10000,
|
|
'minimum_stock_milli' => 20000, 'target_milli' => 100000, 'maximum_milli' => 120000, 'daily_milli' => 1000, 'lead_days' => 5,
|
|
'approval_days' => 1, 'horizon_days' => 30, 'minimum_milli' => 10000, 'pack_milli' => 1000,
|
|
'usage_context' => 'Papel consumível, utilização fictícia no escritório.', 'basis' => 'Consumo e limites exclusivamente ilustrativos para apresentação.']]);
|
|
}
|
|
if ($stockPolicy->state !== 'approved') {
|
|
auth()->guard()->setUser($reviewer);
|
|
$stockPolicy = $planning->save($reviewer, 'policy', ['operation_key' => (string) Str::uuid(), 'record_key' => $stockPolicy->record_key,
|
|
'version' => $stockPolicy->version, 'action' => 'approve', 'reason' => 'Conferência independente dos parâmetros fictícios de stock.', 'data' => []]);
|
|
}
|
|
echo json_encode(['state' => 'passed', 'permanent_administrators' => count($organization->permanentAdministrators($admin->tenant_id)),
|
|
'operational_origin' => 'procurement', 'stock_minimum_milli' => $stockPolicy->data['minimum_stock_milli'], 'stock_policy_state' => $stockPolicy->state], JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR).PHP_EOL;
|
|
} catch (Throwable $error) {
|
|
fwrite(STDERR, $error::class.': '.$error->getMessage().PHP_EOL);
|
|
exit(1);
|
|
}
|