Package system sms

This commit is contained in:
Simon Pocrnjič
2025-10-26 12:57:09 +01:00
parent 266af6595e
commit 369af34ad4
29 changed files with 2639 additions and 330 deletions
@@ -7,6 +7,7 @@
use App\Models\ClientCase;
use App\Models\Contract;
use App\Models\Document;
use App\Services\Sms\SmsService;
use Exception;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
@@ -1693,6 +1694,7 @@ public function sendSmsToPhone(ClientCase $clientCase, Request $request, int $ph
'template_id' => ['sometimes', 'nullable', 'integer', 'exists:sms_templates,id'],
'profile_id' => ['sometimes', 'nullable', 'integer', 'exists:sms_profiles,id'],
'sender_id' => ['sometimes', 'nullable', 'integer', 'exists:sms_senders,id'],
'contract_uuid' => ['sometimes', 'nullable', 'uuid'],
]);
// Ensure the phone belongs to the person of this case
@@ -1801,4 +1803,93 @@ public function sendSmsToPhone(ClientCase $clientCase, Request $request, int $ph
return back()->with('error', 'SMS ni bil dodan v čakalno vrsto.');
}
}
/**
* Return contracts for the given client case (for SMS dialog dropdown).
*/
public function listContracts(ClientCase $clientCase)
{
$contracts = $clientCase->contracts()
->with('account.type')
->select('id', 'uuid', 'reference', 'active', 'start_date', 'end_date')
->latest('id')
->get()
->map(function ($c) {
/** @var SmsService $sms */
$sms = app(SmsService::class);
$acc = $c->account;
$initialRaw = $acc?->initial_amount !== null ? (string) $acc->initial_amount : null;
$balanceRaw = $acc?->balance_amount !== null ? (string) $acc->balance_amount : null;
return [
'uuid' => $c->uuid,
'reference' => $c->reference,
'active' => (bool) $c->active,
'start_date' => (string) ($c->start_date ?? ''),
'end_date' => (string) ($c->end_date ?? ''),
'account' => $acc ? [
'reference' => $acc->reference,
'type' => $acc->type?->name,
'initial_amount' => $initialRaw !== null ? $sms->formatAmountEu($initialRaw) : null,
'balance_amount' => $balanceRaw !== null ? $sms->formatAmountEu($balanceRaw) : null,
'initial_amount_raw' => $initialRaw,
'balance_amount_raw' => $balanceRaw,
] : null,
];
});
return response()->json(['data' => $contracts]);
}
/**
* Render an SMS template preview with optional contract/account placeholders filled.
*/
public function previewSms(ClientCase $clientCase, Request $request, SmsService $sms)
{
$validated = $request->validate([
'template_id' => ['required', 'integer', 'exists:sms_templates,id'],
'contract_uuid' => ['sometimes', 'nullable', 'uuid'],
]);
/** @var \App\Models\SmsTemplate $template */
$template = \App\Models\SmsTemplate::findOrFail((int) $validated['template_id']);
$vars = [];
$contractUuid = $validated['contract_uuid'] ?? null;
if ($contractUuid) {
// Ensure the contract belongs to this client case
$contract = $clientCase->contracts()->where('uuid', $contractUuid)
->with('account.type')
->first();
if ($contract) {
$vars['contract'] = [
'id' => $contract->id,
'uuid' => $contract->uuid,
'reference' => $contract->reference,
'start_date' => (string) ($contract->start_date ?? ''),
'end_date' => (string) ($contract->end_date ?? ''),
];
if ($contract->account) {
$initialRaw = (string) $contract->account->initial_amount;
$balanceRaw = (string) $contract->account->balance_amount;
$vars['account'] = [
'id' => $contract->account->id,
'reference' => $contract->account->reference,
'initial_amount' => $sms->formatAmountEu($initialRaw),
'balance_amount' => $sms->formatAmountEu($balanceRaw),
'initial_amount_raw' => $initialRaw,
'balance_amount_raw' => $balanceRaw,
'type' => $contract->account->type?->name,
];
}
}
}
$content = $sms->renderContent($template->content, $vars);
return response()->json([
'content' => $content,
'variables' => $vars,
]);
}
}