50 lines
3.2 KiB
PHP
50 lines
3.2 KiB
PHP
<?php
|
|
|
|
use App\Enums\PurchasingRole;
|
|
use App\Models\ProposalEvaluation;
|
|
use App\Models\PurchaseAward;
|
|
use App\Models\Quotation;
|
|
use App\Models\Supplier;
|
|
use App\Models\User;
|
|
use App\Services\PurchaseWorkflow;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/** Upgrade only a guarded synthetic fixture into a service-approved, issuable award. */
|
|
function issuablePurchaseControlFixture(array $f, User $buyer, User $finance): PurchaseAward
|
|
{
|
|
if (! app()->environment('testing') || ! preg_match('/^compras_receipts_test_[a-f0-9]{12}$/', DB::connection()->getDatabaseName())) {
|
|
throw new LogicException('Fixture proibida fora da base PostgreSQL isolada.');
|
|
}
|
|
auth()->guard()->setUser($buyer);
|
|
DB::table('purchase_awards')->where('id', $f['order']->purchase_award_id)->delete();
|
|
$request = $f['r'];
|
|
$request->update(['approval_snapshot' => ['first_limit_minor' => 2500000, 'second_limit_minor' => 15000000, 'approval_includes_tax' => true, 'rules_provisional' => true]]);
|
|
$head = User::factory()->create(['tenant_id' => $buyer->tenant_id, 'department_id' => $request->department_id, 'purchasing_role' => PurchasingRole::DepartmentHead]);
|
|
$request->steps()->create(['tenant_id' => $buyer->tenant_id, 'position' => 1, 'role' => 'department_head', 'decision' => 'approved', 'approver_id' => $head->id, 'decided_at' => now()]);
|
|
$first = Quotation::query()->where('requisition_id', $request->id)->firstOrFail();
|
|
$supplier = Supplier::query()->findOrFail($first->supplier_id);
|
|
$item = $request->items()->firstOrFail();
|
|
$quotes = [$first];
|
|
for ($index = 1; $index <= 2; $index++) {
|
|
$otherSupplier = $supplier->replicate();
|
|
$otherSupplier->fill(['nuit' => '12345678'.$index, 'name' => 'Fornecedor fictício '.$index])->save();
|
|
$quote = $first->replicate();
|
|
$quote->fill(['supplier_id' => $otherSupplier->id, 'reference' => 'PROP-TEST-'.$index])->save();
|
|
$quotes[] = $quote;
|
|
}
|
|
foreach ($quotes as $index => $quote) {
|
|
$amount = 10000 * ($index + 1);
|
|
$quote->update(['created_by' => $buyer->id, 'subtotal_minor' => $amount, 'total_minor' => $amount]);
|
|
$quote->items()->create(['tenant_id' => $buyer->tenant_id, 'requisition_item_id' => $item->id, 'description' => $item->description, 'quantity' => '1.000', 'unit_price_minor' => $amount, 'tax_rate' => '0', 'subtotal_minor' => $amount, 'tax_minor' => 0, 'total_minor' => $amount]);
|
|
foreach ([$buyer, $finance] as $evaluator) {
|
|
ProposalEvaluation::query()->create(['tenant_id' => $buyer->tenant_id, 'quotation_id' => $quote->id, 'evaluator_id' => $evaluator->id, 'quality' => '8', 'history' => '5', 'payment' => '8', 'no_conflict' => true, 'comment' => 'Avaliação exclusivamente fictícia']);
|
|
}
|
|
}
|
|
$workflow = app(PurchaseWorkflow::class);
|
|
$award = $workflow->select($buyer, $request, ['quotation_id' => $first->id, 'justification' => 'Melhor proposta fictícia, seleccionada pelo serviço.', 'delivery_date' => today()->addWeek()->format('Y-m-d'), 'delivery_location' => 'Armazém fictício']);
|
|
auth()->guard()->setUser($head);
|
|
$workflow->decide($head, $award, 'approved', 'Decisão final exclusivamente fictícia');
|
|
|
|
return $award->refresh();
|
|
}
|