61 lines
1.8 KiB
PHP
61 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace Modules\Accounting\Services;
|
|
|
|
use Modules\Accounting\Entities\AccountingAccTransMapping;
|
|
use Modules\Accounting\Utils\AccountingUtil;
|
|
|
|
class JournalApprovalService
|
|
{
|
|
public function requiresApproval(int $businessId): bool
|
|
{
|
|
if (config('accounting.rollout.journal_approval_required', false)) {
|
|
return true;
|
|
}
|
|
|
|
$settings = app(AccountingUtil::class)->getAccountingSettings($businessId);
|
|
|
|
return ! empty($settings['journal_approval_required']);
|
|
}
|
|
|
|
public function initialStatus(int $businessId): string
|
|
{
|
|
return $this->requiresApproval($businessId) ? 'pending' : 'approved';
|
|
}
|
|
|
|
public function approve(AccountingAccTransMapping $mapping, int $userId): AccountingAccTransMapping
|
|
{
|
|
if ($mapping->approval_status === 'approved') {
|
|
return $mapping;
|
|
}
|
|
|
|
$mapping->approval_status = 'approved';
|
|
$mapping->approved_by = $userId;
|
|
$mapping->approved_at = now();
|
|
$mapping->save();
|
|
|
|
app(FinancialPostingService::class)->postPendingJournalLines($mapping);
|
|
|
|
return $mapping;
|
|
}
|
|
|
|
public function reject(AccountingAccTransMapping $mapping, int $userId): AccountingAccTransMapping
|
|
{
|
|
$mapping->approval_status = 'rejected';
|
|
$mapping->approved_by = $userId;
|
|
$mapping->approved_at = now();
|
|
$mapping->save();
|
|
|
|
\Modules\Accounting\Entities\AccountingPostingLog::where('acc_trans_mapping_id', $mapping->id)
|
|
->where('status', 'pending')
|
|
->update(['status' => 'rejected', 'error_message' => 'Rejected by user '.$userId]);
|
|
|
|
return $mapping;
|
|
}
|
|
|
|
public function isPostable(AccountingAccTransMapping $mapping): bool
|
|
{
|
|
return in_array($mapping->approval_status ?? 'approved', ['approved'], true);
|
|
}
|
|
}
|