@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
use App\Models\PurchaseOrder;
|
||||
use App\Models\SupplyRecord;
|
||||
use App\Models\User;
|
||||
use App\Services\ImportBankSupport;
|
||||
use App\Services\ImportDossier;
|
||||
use App\Services\ImportEvidence;
|
||||
use App\Services\SupplyPlanning;
|
||||
use Illuminate\Contracts\Console\Kernel;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
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 synthetic demonstration required.');
|
||||
}
|
||||
if (function_exists('posix_geteuid') && posix_geteuid() !== fileowner(storage_path('app/private'))) {
|
||||
throw new LogicException('Run document preparation as the application storage owner.');
|
||||
}
|
||||
try {
|
||||
$actor = User::query()->where('email', 'procurement@demo.compras.tempo.co.mz')->sole();
|
||||
auth()->guard()->setUser($actor);
|
||||
$order = PurchaseOrder::query()->where('requisition_id', 2)->sole();
|
||||
if (SupplyRecord::query()->where('kind', 'import')->where('purchase_order_id', $order->id)->exists()) {
|
||||
throw new LogicException('Do not duplicate the synthetic import documents.');
|
||||
}
|
||||
$protected = ['invoices', 'invoice_items', 'stock_movements', 'budget_entries', 'budget_consumptions', 'ps_payments', 'ps_allocations', 'gl_journals', 'gl_lines'];
|
||||
$frames = [];
|
||||
foreach ($protected as $table) {
|
||||
$frames[$table] = DB::table($table)->orderBy('id')->get()->toJson();
|
||||
}
|
||||
$import = app(SupplyPlanning::class)->save($actor, 'import', ['operation_key' => (string) Str::uuid(), 'version' => 0, 'action' => 'save',
|
||||
'reason' => 'Processo inteiramente fictício para apresentação do acompanhamento documental.',
|
||||
'data' => ['purchase_order_id' => $order->id, 'purpose' => 'internal', 'jurisdiction' => 'MZ — cenário fictício',
|
||||
'origin_country' => 'ZA — cenário fictício', 'dispatch_country' => 'ZA — cenário fictício',
|
||||
'regime' => 'Demonstração documental sem operação externa', 'expected_usable_on' => today()->addWeek()->toDateString()]]);
|
||||
$documents = [];
|
||||
foreach (['commercial_invoice' => 'DEMO-FACTURA-COMERCIAL', 'packing_list' => 'DEMO-EMBALAGEM', 'customs_declaration' => 'DEMO-DU-JUE',
|
||||
'bank_commitment' => 'DEMO-TERMO-BANCARIO', 'bank_support' => 'DEMO-ENTREGA-DOCUMENTAL'] as $type => $reference) {
|
||||
$stream = 'BT /F1 15 Tf 30 730 Td (DEMONSTRACAO - DOCUMENTO FICTICIO) Tj 0 -35 Td ('.$reference.') Tj 0 -35 Td (Sem validade oficial ou submissao externa.) Tj ET';
|
||||
$objects = ['<< /Type /Catalog /Pages 2 0 R >>', '<< /Type /Pages /Kids [3 0 R] /Count 1 >>',
|
||||
'<< /Type /Page /Parent 2 0 R /MediaBox [0 0 595 842] /Resources << /Font << /F1 4 0 R >> >> /Contents 5 0 R >>',
|
||||
'<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>', '<< /Length '.strlen($stream)." >>\nstream\n".$stream."\nendstream"];
|
||||
$pdf = "%PDF-1.4\n";
|
||||
$offsets = [];
|
||||
foreach ($objects as $index => $object) {
|
||||
$offsets[] = strlen($pdf);
|
||||
$pdf .= ($index + 1)." 0 obj\n".$object."\nendobj\n";
|
||||
}
|
||||
$start = strlen($pdf);
|
||||
$pdf .= "xref\n0 6\n0000000000 65535 f \n";
|
||||
foreach ($offsets as $offset) {
|
||||
$pdf .= sprintf("%010d 00000 n \n", $offset);
|
||||
}
|
||||
$pdf .= "trailer\n<< /Size 6 /Root 1 0 R >>\nstartxref\n".$start."\n%%EOF\n";
|
||||
$temporary = tempnam(storage_path('framework/testing'), 'demo-doc-');
|
||||
if ($temporary === false) {
|
||||
throw new LogicException('Synthetic private document staging unavailable.');
|
||||
}
|
||||
try {
|
||||
file_put_contents($temporary, $pdf);
|
||||
$document = app(ImportDossier::class)->record($actor, $import, ['operation_key' => (string) Str::uuid(), 'type' => $type,
|
||||
'reference' => $reference, 'issuer_reference' => 'Entidade fictícia de demonstração', 'issued_on' => today()->toDateString(),
|
||||
'notes' => 'Exemplar inteiramente fictício, sem validade oficial.', 'reason' => 'Documento sintético para apresentar o circuito, sem submissão ao banco ou JUE.'],
|
||||
new UploadedFile($temporary, $reference.'.pdf', 'application/pdf', null, true));
|
||||
if (app(ImportEvidence::class)->bytes($document) !== $pdf) {
|
||||
throw new LogicException('Synthetic original integrity failed.');
|
||||
}
|
||||
$documents[$type] = $document->id;
|
||||
} finally {
|
||||
unlink($temporary);
|
||||
}
|
||||
}
|
||||
$support = app(ImportBankSupport::class)->save($actor, $import, ['operation_key' => (string) Str::uuid(), 'version' => 0,
|
||||
'commitment_document_id' => $documents['bank_commitment'], 'declaration_document_id' => $documents['customs_declaration'],
|
||||
'submission_document_id' => $documents['bank_support'], 'submission_basis' => 'Comprovativo exclusivamente fictício para apresentação.',
|
||||
'submitted_on' => today()->toDateString(), 'support_document_ids' => [$documents['commercial_invoice'], $documents['packing_list']],
|
||||
'requirements_known' => true, 'required_support_types' => ['commercial_invoice', 'packing_list'],
|
||||
'required_support_counts' => ['commercial_invoice' => 1, 'packing_list' => 1],
|
||||
'requirements_basis' => 'Lista ilustrativa, não representa exigências de um banco real.', 'due_on' => today()->addWeek()->toDateString(),
|
||||
'deadline_basis' => 'Prazo fictício da apresentação; não é prazo legal ou bancário.', 'reason' => 'Acompanhamento sintético do DU/JUE e suporte bancário, sem operação externa.']);
|
||||
foreach ($frames as $table => $before) {
|
||||
if (DB::table($table)->orderBy('id')->get()->toJson() !== $before) {
|
||||
throw new LogicException('Documentary presentation changed protected effects: '.$table);
|
||||
}
|
||||
}
|
||||
echo json_encode(['state' => 'passed', 'import_id' => $import->id, 'order_id' => $order->id, 'documents' => $documents,
|
||||
'bank_support_id' => $support->id, 'bank_support_state' => $support->state, 'protected_financial_stock_tables_unchanged' => count($frames),
|
||||
'jue_submission_executed' => false, 'bank_transfer_executed' => false], JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR).PHP_EOL;
|
||||
} catch (Throwable $error) {
|
||||
fwrite(STDERR, $error::class.': '.$error->getMessage().PHP_EOL);
|
||||
exit(1);
|
||||
}
|
||||
Reference in New Issue
Block a user