48 lines
1.9 KiB
PHP
48 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace Modules\Accounting\Services;
|
|
|
|
use Modules\Accounting\Domain\FinancialPostingRequest;
|
|
use Modules\Accounting\Entities\AccountingInsurancePremium;
|
|
|
|
class InsurancePremiumService
|
|
{
|
|
public function __construct(protected FinancialPostingService $postingService) {}
|
|
|
|
public function register(array $data, int $businessId, int $userId): AccountingInsurancePremium
|
|
{
|
|
return AccountingInsurancePremium::create(array_merge($data, [
|
|
'business_id' => $businessId,
|
|
'created_by' => $userId,
|
|
'status' => 'pending',
|
|
]));
|
|
}
|
|
|
|
public function markPaid(AccountingInsurancePremium $premium, array $data, int $userId): AccountingInsurancePremium
|
|
{
|
|
if (! empty($data['expense_account_id']) && ! empty($data['bank_account_id'])) {
|
|
$mapping = $this->postingService->post(new FinancialPostingRequest(
|
|
businessId: $premium->business_id,
|
|
userId: $userId,
|
|
sourceType: 'insurance_premium',
|
|
sourceId: $premium->id,
|
|
note: 'Insurance '.$premium->provider_name,
|
|
lines: [
|
|
['accounting_account_id' => (int) $data['expense_account_id'], 'amount' => (float) $premium->amount, 'type' => 'debit'],
|
|
['accounting_account_id' => (int) $data['bank_account_id'], 'amount' => (float) $premium->amount, 'type' => 'credit'],
|
|
],
|
|
operationDate: ($data['paid_date'] ?? now()->toDateString()),
|
|
));
|
|
$premium->journal_mapping_id = $mapping?->id;
|
|
$premium->expense_account_id = $data['expense_account_id'];
|
|
$premium->bank_account_id = $data['bank_account_id'];
|
|
}
|
|
|
|
$premium->status = 'paid';
|
|
$premium->paid_date = $data['paid_date'] ?? now()->toDateString();
|
|
$premium->save();
|
|
|
|
return $premium;
|
|
}
|
|
}
|