@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
/** Minimal read-only catalogue of the common ledger in this Compras database. */
|
||||
final class RequisitionLedgerAccounts
|
||||
{
|
||||
public function ready(): bool
|
||||
{
|
||||
$db = DB::connection();
|
||||
|
||||
return config('requisition-accounts.qa_enabled') === true && app()->environment('testing')
|
||||
&& (($db->getDriverName() === 'sqlite' && $db->getDatabaseName() === ':memory:')
|
||||
|| ($db->getDriverName() === 'pgsql' && $db->getConfig('host') === '127.0.0.1'
|
||||
&& preg_match('/^compras_receipts_test_[a-f0-9]{12}$/D', $db->getDatabaseName())))
|
||||
&& Schema::hasColumns('gl_accounts', ['id', 'tenant_id', 'company_id', 'unit_id', 'code', 'label', 'currency', 'is_movement']);
|
||||
}
|
||||
|
||||
private function query(User $actor): Builder
|
||||
{
|
||||
abort_unless($this->ready(), 503, 'Plano de contas do núcleo comum indisponível nesta composição.');
|
||||
abort_unless(auth()->id() === $actor->id && $actor->tenant_id !== null, 403);
|
||||
abort_if(app(OrganizationAccess::class)->maintenanceOnly($actor), 403);
|
||||
|
||||
// Standalone Compras uses its existing native company/unit zero context.
|
||||
// Only code/label/currency are exposed; ledger balances, grants and journals are never read.
|
||||
return DB::table('gl_accounts')->where(['tenant_id' => $actor->tenant_id, 'company_id' => 0, 'unit_id' => 0]);
|
||||
}
|
||||
|
||||
/** @return array{id:int,code:string,label:string,currency:string,source_hash:string} */
|
||||
private function row(\stdClass $account): array
|
||||
{
|
||||
$source = ['id' => (int) $account->id, 'tenant_id' => (int) $account->tenant_id, 'company_id' => (int) $account->company_id,
|
||||
'unit_id' => (int) $account->unit_id, 'code' => (string) $account->code, 'label' => (string) $account->label,
|
||||
'currency' => (string) $account->currency, 'is_movement' => (bool) $account->is_movement];
|
||||
|
||||
return ['id' => $source['id'], 'code' => $source['code'], 'label' => $source['label'], 'currency' => $source['currency'],
|
||||
'source_hash' => hash('sha256', json_encode($source, JSON_THROW_ON_ERROR))];
|
||||
}
|
||||
|
||||
/** @return array<string,mixed> */
|
||||
public function options(User $actor, string $search = ''): array
|
||||
{
|
||||
abort_unless(app(OrganizationAccess::class)->canRequest($actor), 403);
|
||||
$query = $this->query($actor)->where('is_movement', true)->where('currency', 'MZN');
|
||||
$search = trim($search);
|
||||
if ($search !== '') {
|
||||
// Literal substring search: user wildcards never widen the catalogue unexpectedly.
|
||||
$search = str_replace(['!', '%', '_'], ['!!', '!%', '!_'], $search);
|
||||
$query->where(fn ($q) => $q->whereRaw("LOWER(code) LIKE LOWER(?) ESCAPE '!'", ['%'.$search.'%'])
|
||||
->orWhereRaw("LOWER(label) LIKE LOWER(?) ESCAPE '!'", ['%'.$search.'%']));
|
||||
}
|
||||
$page = $query->orderBy('code')->orderBy('id')->paginate(20, ['*'], 'accounts_page')->withQueryString();
|
||||
$page->setCollection($page->getCollection()->map(fn ($row) => $this->row($row)));
|
||||
|
||||
return $page->toArray();
|
||||
}
|
||||
|
||||
/** Current movement account, checked again inside the host's company transaction. No account is created.
|
||||
* @return array{id:int,code:string,label:string,currency:string,source_hash:string}
|
||||
*/
|
||||
public function resolve(User $actor, int $id, string $hash): array
|
||||
{
|
||||
$account = $this->query($actor)->where('id', $id)->where('is_movement', true)->where('currency', 'MZN')->first();
|
||||
abort_unless($account !== null, 409, 'Conta indisponível no plano próprio de Compras.');
|
||||
$row = $this->row($account);
|
||||
abort_unless(hash_equals($row['source_hash'], $hash), 409, 'A conta mudou. Confira a referência actual antes de guardar.');
|
||||
|
||||
return $row;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user