27 lines
931 B
PHP
27 lines
931 B
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Requisition;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class BudgetWorkflow
|
|
{
|
|
/** @return array<string,int>|null */
|
|
public function record(User $user, Requisition $request, string $kind, int $amount = 0, ?int $subtotal = null): ?array
|
|
{
|
|
abort_unless(auth()->id() === $user->id && $user->tenant_id !== null && $request->tenant_id === $user->tenant_id, 403);
|
|
if (DB::connection()->transactionLevel() === 0) {
|
|
throw new \LogicException('Movimentos de orçamento exigem transacção.');
|
|
}
|
|
$adapter = app(CommonBudget::class);
|
|
|
|
return $adapter->execute(fn () => $adapter->for($user)->record(
|
|
$request->id, $request->cost_center_id,
|
|
$request->items()->where('purchase_type', 'equipment')->exists() ? 'investment' : 'operating',
|
|
$kind, $amount, $subtotal,
|
|
));
|
|
}
|
|
}
|