54 lines
3.1 KiB
PHP
54 lines
3.1 KiB
PHP
<?php
|
|
|
|
use App\Models\User;
|
|
use App\Services\CommonPaymentFacts;
|
|
use App\Services\ExpenseAllocations;
|
|
use Illuminate\Contracts\Console\Kernel;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Session\ArraySessionHandler;
|
|
use Illuminate\Session\Store;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
require dirname(__DIR__).'/vendor/autoload.php';
|
|
$app = require dirname(__DIR__).'/bootstrap/app.php';
|
|
$app->make(Kernel::class)->bootstrap();
|
|
if (($argv[1] ?? '') !== '--read-only' || config('app.demonstration') !== true || DB::connection()->getDatabaseName() !== 'compras_demo_20261129') {
|
|
throw new LogicException('Explicit synthetic demonstration required.');
|
|
}
|
|
try {
|
|
$result = DB::transaction(function () {
|
|
DB::statement('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY');
|
|
$actor = User::query()->where('email', 'finance_review@demo.compras.tempo.co.mz')->sole();
|
|
auth()->guard()->setUser($actor);
|
|
// Read-only diagnostic fixture: the same configured internal context, with an in-memory session.
|
|
$request = Request::create('https://compras.tempo.co.mz/facturas');
|
|
$session = new Store('finance-readonly', new ArraySessionHandler(120));
|
|
$session->start();
|
|
$session->put('work_context', ['user_id' => $actor->id, 'tenant_id' => $actor->tenant_id, 'key' => 'procurement.internal']);
|
|
$request->setLaravelSession($session);
|
|
$request->setUserResolver(fn () => $actor);
|
|
app()->instance('request', $request);
|
|
$invoice = DB::table('invoices')->sole();
|
|
$allocation = DB::table('ps_allocations')->sole();
|
|
$facts = app(CommonPaymentFacts::class)->source($actor, $allocation->id);
|
|
$category = app(ExpenseAllocations::class)->projection($actor, $allocation->id, 'common_allocation');
|
|
$debit = (int) DB::table('gl_lines')->sum('debit_minor');
|
|
$credit = (int) DB::table('gl_lines')->sum('credit_minor');
|
|
$stock = (int) DB::table('stock_movements')->sum('quantity_milli');
|
|
if ($invoice->status !== 'validated' || (int) $invoice->total_minor !== 100000 || $facts['amount_minor'] !== 40000
|
|
|| $category['state'] !== 'allocated' || $category['allocations'] !== [['category' => 'Material de escritório', 'amount_minor' => '40000']]
|
|
|| $debit !== 100000 || $credit !== 100000 || $stock !== 10000
|
|
|| DB::table('ps_payments')->count() !== 1 || DB::table('external_payments')->count() !== 0) {
|
|
throw new LogicException('Demonstration financial reconciliation failed.');
|
|
}
|
|
|
|
return ['state' => 'passed', 'invoice_id' => $invoice->id, 'invoice_minor' => 100000, 'payment_minor' => 40000, 'remaining_minor' => 60000,
|
|
'allocation_id' => $allocation->id, 'category_attribution' => $category, 'stock_milli' => $stock, 'journal_debit_minor' => $debit,
|
|
'journal_credit_minor' => $credit, 'duplicate_external_payment' => false, 'read_only' => true];
|
|
});
|
|
echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR).PHP_EOL;
|
|
} catch (Throwable $error) {
|
|
fwrite(STDERR, $error::class.': '.$error->getMessage().PHP_EOL);
|
|
exit(1);
|
|
}
|