137 lines
8.4 KiB
PHP
137 lines
8.4 KiB
PHP
<?php
|
|
|
|
use App\Enums\PurchasingRole;
|
|
use App\Http\Controllers\QuotationController;
|
|
use App\Models\CostCenter;
|
|
use App\Models\Quotation;
|
|
use App\Models\Supplier;
|
|
use App\Models\User;
|
|
use App\Services\CommonBudget;
|
|
use App\Services\PurchaseWorkflow;
|
|
use App\Services\QuotationExchange;
|
|
use App\Services\RequisitionWorkflow;
|
|
use Illuminate\Contracts\Console\Kernel;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
require __DIR__.'/../../vendor/autoload.php';
|
|
$app = require __DIR__.'/../../bootstrap/app.php';
|
|
$app->make(Kernel::class)->bootstrap();
|
|
$database = $argv[1] ?? '';
|
|
if (! $app->environment('testing') || DB::getDriverName() !== 'pgsql' || DB::connection()->getDatabaseName() !== $database
|
|
|| ! preg_match('/^compras_receipts_test_[a-f0-9]{12}$/D', $database)) {
|
|
throw new LogicException('Visual fixture is restricted to disposable PostgreSQL QA.');
|
|
}
|
|
require_once __DIR__.'/../Support/ReceiptFixture.php';
|
|
require_once __DIR__.'/../Support/BudgetDistributionSchema.php';
|
|
$f = receiptFixture();
|
|
budgetDistributionFixtureSchema();
|
|
config(['budget-distributions.qa_enabled' => true]);
|
|
$partsMode = getenv('SGC_ORDER_PARTS_QA_ENABLED') === 'true';
|
|
if ($partsMode) {
|
|
(require __DIR__.'/../../database/qa-migrations/2026_11_22_000007_create_budget_order_parts.php')->up();
|
|
(require __DIR__.'/../../database/qa-migrations/2026_11_22_000008_create_purchase_order_sources.php')->up();
|
|
config(['order-parts.qa_enabled' => true]);
|
|
}
|
|
$users = [];
|
|
$roles = ['owner' => PurchasingRole::Requester, 'buyer' => PurchasingRole::Procurement, 'head' => PurchasingRole::DepartmentHead, 'finance' => PurchasingRole::Finance, 'admin' => PurchasingRole::Admin];
|
|
if ($partsMode) {
|
|
$roles += ['receiver' => PurchasingRole::Warehouse, 'finance-reviewer' => PurchasingRole::Finance];
|
|
}
|
|
foreach ($roles as $name => $role) {
|
|
$users[$name] = User::factory()->create(['tenant_id' => $f['tenant']->id, 'department_id' => $f['department']->id,
|
|
'name' => 'QA sintética · '.$role->label(), 'email' => 'qa-'.$name.'@sgc.example.invalid', 'purchasing_role' => $role]);
|
|
}
|
|
$centre = CostCenter::query()->create(['tenant_id' => $f['tenant']->id, 'department_id' => $f['department']->id, 'code' => 'QA-2', 'name' => 'Segundo centro sintético']);
|
|
foreach ([$f['r']->cost_center_id, $centre->id] as $id) {
|
|
auth()->guard()->setUser($users['finance']);
|
|
$budget = app(CommonBudget::class)->for($users['finance'])->create(['cost_center_id' => $id, 'year' => (int) now()->year,
|
|
'kind' => 'operating', 'includes_tax' => true, 'amount_minor' => 100000, 'reason' => 'Dotação exclusivamente sintética da revisão visual.']);
|
|
auth()->guard()->setUser($users['admin']);
|
|
app(CommonBudget::class)->for($users['admin'])->approve($budget);
|
|
}
|
|
$workflow = app(RequisitionWorkflow::class);
|
|
$requests = [];
|
|
$partSuppliers = $partAwards = [];
|
|
foreach ($partsMode ? ['draft', 'award', 'award2', 'foreign'] : ['draft', 'award', 'foreign'] as $purpose) {
|
|
auth()->guard()->setUser($users['owner']);
|
|
$r = $workflow->save($users['owner'], ['operation_key' => (string) Str::uuid(), 'cost_center_id' => $f['r']->cost_center_id,
|
|
'priority' => 'normal', 'needed_by' => today()->addWeek()->toDateString(), 'justification' => 'Necessidade exclusivamente sintética para revisão visual '.$purpose,
|
|
'items' => [['description' => 'Material sintético '.$purpose, 'category' => 'Material', 'purchase_type' => 'consumable', 'quantity' => '1.000', 'unit' => 'cx', 'unit_price' => '100.00', 'tax_rate' => '0.00']]]);
|
|
$rows = array_map(fn ($id) => ['allocation_key' => (string) Str::uuid(), 'line_id' => $r->items()->firstOrFail()->id,
|
|
'centre_id' => $id, 'subtotal' => '50.00', 'total' => '50.00'], [$f['r']->cost_center_id, $centre->id]);
|
|
$r = $workflow->save($users['owner'], ['operation_key' => (string) Str::uuid(), 'process_version' => $r->getAttribute('process_version'),
|
|
'budget_distribution_preparation' => $rows, 'reason' => 'Parcelas explícitas sintéticas para revisão visual.'], $r);
|
|
$requests[$purpose] = $r;
|
|
if ($purpose === 'draft') {
|
|
continue;
|
|
}
|
|
$workflow->submit($users['owner'], $r, (string) Str::uuid(), $r->getAttribute('process_version'));
|
|
while (($r = $r->fresh())->status === 'pending') {
|
|
$step = $r->steps()->where('decision', 'pending')->orderBy('position')->firstOrFail();
|
|
$actor = match ($step->role) {
|
|
'department_head' => $users['head'], 'procurement' => $users['buyer'], 'finance' => $users['finance']
|
|
};
|
|
auth()->guard()->setUser($actor);
|
|
$workflow->decide($actor, $r, 'approved', null, (string) Str::uuid(), $r->getAttribute('process_version'));
|
|
}
|
|
if ($purpose === 'foreign') {
|
|
auth()->guard()->setUser($users['finance']);
|
|
app(QuotationExchange::class)->record($users['finance'], $r, ['operation_id' => (string) Str::uuid(), 'currency' => 'USD',
|
|
'rate' => '1.0000000000', 'quoted_on' => today()->toDateString(), 'source' => 'Referência exclusivamente sintética, sem cotação de mercado.']);
|
|
}
|
|
$quotes = [];
|
|
for ($index = 0; $index < 3; $index++) {
|
|
if ($partsMode && $purpose === 'award2') {
|
|
$supplier = Supplier::query()->findOrFail($partSuppliers[$index]);
|
|
} else {
|
|
$supplier = Supplier::query()->findOrFail($f['order']->supplier_id)->replicate();
|
|
$supplier->name = 'Fornecedor sintético '.$purpose.' '.$index;
|
|
$supplier->nuit = (string) (200000000 + count($requests) * 100 + $index);
|
|
$supplier->created_by = $users['buyer']->id;
|
|
$supplier->save();
|
|
if ($partsMode && $purpose === 'award') {
|
|
$partSuppliers[$index] = $supplier->id;
|
|
}
|
|
}
|
|
$actor = $users['buyer'];
|
|
auth()->guard()->setUser($actor);
|
|
$input = Request::create('/', 'POST', ['supplier_id' => $supplier->id, 'currency' => $purpose === 'foreign' ? 'USD' : 'MZN',
|
|
'reference' => 'QA-'.$purpose.'-'.$index, 'delivery_days' => 5, 'valid_until' => today()->addMonth()->toDateString(), 'payment_terms' => '30 dias',
|
|
'no_conflict' => true, 'items' => [['requisition_item_id' => $r->items()->firstOrFail()->id, 'unit_price' => '100.00']]]);
|
|
$input->setUserResolver(fn () => $actor);
|
|
app(QuotationController::class)->store($input, $r, $workflow);
|
|
$quote = Quotation::query()->where('requisition_id', $r->id)->latest('id')->firstOrFail();
|
|
$quotes[] = $quote;
|
|
foreach ([$users['buyer'], $users['finance']] as $actor) {
|
|
auth()->guard()->setUser($actor);
|
|
$input = Request::create('/', 'POST', ['quality' => '8', 'history' => '5', 'payment' => '8', 'comment' => 'Avaliação exclusivamente sintética.', 'no_conflict' => true]);
|
|
$input->setUserResolver(fn () => $actor);
|
|
app(QuotationController::class)->evaluate($input, $quote, $workflow);
|
|
}
|
|
}
|
|
if ($purpose === 'foreign' || ($partsMode && in_array($purpose, ['award', 'award2'], true))) {
|
|
auth()->guard()->setUser($users['buyer']);
|
|
$award = app(PurchaseWorkflow::class)->select($users['buyer'], $r, ['quotation_id' => $quotes[0]->id,
|
|
'justification' => 'Proposta exclusivamente sintética para revisão visual.', 'delivery_date' => today()->addWeek()->toDateString(),
|
|
'delivery_location' => 'Armazém sintético', 'budget_distribution_preparation' => $rows, 'reason' => 'Imputação explícita sintética da proposta em MZN.']);
|
|
foreach ($award->approvals()->orderBy('position')->get() as $step) {
|
|
$actor = match ($step->role) {
|
|
'department_head' => $users['head'], 'finance' => $users['finance'], 'procurement' => $users['admin']
|
|
};
|
|
auth()->guard()->setUser($actor);
|
|
app(PurchaseWorkflow::class)->decide($actor, $award, 'approved', null);
|
|
}
|
|
auth()->guard()->setUser($users['buyer']);
|
|
if ($purpose === 'foreign') {
|
|
$order = app(PurchaseWorkflow::class)->issue($users['buyer'], $award);
|
|
} else {
|
|
$partAwards[$purpose] = $award->id;
|
|
}
|
|
}
|
|
}
|
|
echo json_encode(['synthetic_only' => true, 'draft' => $requests['draft']->id, 'award' => $requests['award']->id,
|
|
'foreign_order' => $order->id, 'users' => array_map(fn ($user) => $user->email, $users),
|
|
...($partsMode ? ['order_parts_awards' => $partAwards, 'receipt_stock' => $f['stock']->id, 'receipt_warehouse' => $f['warehouse']->id] : [])], JSON_THROW_ON_ERROR).PHP_EOL;
|