Merge branch 'master' into Development
This commit is contained in:
@@ -37,7 +37,7 @@ public function index(Request $request): Response
|
||||
->get(['id', 'profile_id', 'sname', 'phone_number']);
|
||||
$templates = \App\Models\SmsTemplate::query()
|
||||
->orderBy('name')
|
||||
->get(['id', 'name']);
|
||||
->get(['id', 'name', 'content']);
|
||||
$segments = \App\Models\Segment::query()
|
||||
->where('active', true)
|
||||
->orderBy('name')
|
||||
@@ -98,6 +98,10 @@ public function show(Package $package, SmsService $sms): Response
|
||||
'start_date' => (string) ($c->start_date ?? ''),
|
||||
'end_date' => (string) ($c->end_date ?? ''),
|
||||
];
|
||||
// Include contract.meta as flattened key-value pairs
|
||||
if (is_array($c->meta) && ! empty($c->meta)) {
|
||||
$vars['contract']['meta'] = $this->flattenMeta($c->meta);
|
||||
}
|
||||
if ($c->account) {
|
||||
$initialRaw = (string) $c->account->initial_amount;
|
||||
$balanceRaw = (string) $c->account->balance_amount;
|
||||
@@ -121,7 +125,7 @@ public function show(Package $package, SmsService $sms): Response
|
||||
if (! $rendered) {
|
||||
$body = isset($payload['body']) ? trim((string) $payload['body']) : '';
|
||||
if ($body !== '') {
|
||||
$rendered = $body;
|
||||
$rendered = $sms->renderContent($body, $vars);
|
||||
} elseif (! empty($payload['template_id'])) {
|
||||
$tpl = \App\Models\SmsTemplate::find((int) $payload['template_id']);
|
||||
if ($tpl) {
|
||||
@@ -157,6 +161,10 @@ public function show(Package $package, SmsService $sms): Response
|
||||
'start_date' => (string) ($c->start_date ?? ''),
|
||||
'end_date' => (string) ($c->end_date ?? ''),
|
||||
];
|
||||
// Include contract.meta as flattened key-value pairs
|
||||
if (is_array($c->meta) && ! empty($c->meta)) {
|
||||
$vars['contract']['meta'] = $this->flattenMeta($c->meta);
|
||||
}
|
||||
if ($c->account) {
|
||||
$initialRaw = (string) $c->account->initial_amount;
|
||||
$balanceRaw = (string) $c->account->balance_amount;
|
||||
@@ -175,7 +183,7 @@ public function show(Package $package, SmsService $sms): Response
|
||||
if ($body !== '') {
|
||||
$preview = [
|
||||
'source' => 'body',
|
||||
'content' => $body,
|
||||
'content' => $sms->renderContent($body, $vars),
|
||||
];
|
||||
} elseif (! empty($payload['template_id'])) {
|
||||
/** @var SmsTemplate|null $tpl */
|
||||
@@ -300,30 +308,39 @@ public function destroy(Package $package): RedirectResponse
|
||||
public function contracts(Request $request, PhoneSelector $selector): \Illuminate\Http\JsonResponse
|
||||
{
|
||||
$request->validate([
|
||||
'segment_id' => ['required', 'integer', 'exists:segments,id'],
|
||||
'segment_id' => ['nullable', 'integer', 'exists:segments,id'],
|
||||
'q' => ['nullable', 'string'],
|
||||
'per_page' => ['nullable', 'integer', 'min:1', 'max:100'],
|
||||
'client_id' => ['nullable', 'integer', 'exists:clients,id'],
|
||||
'only_mobile' => ['nullable', 'boolean'],
|
||||
'only_validated' => ['nullable', 'boolean'],
|
||||
'start_date_from' => ['nullable', 'date'],
|
||||
'start_date_to' => ['nullable', 'date'],
|
||||
'promise_date_from' => ['nullable', 'date'],
|
||||
'promise_date_to' => ['nullable', 'date'],
|
||||
]);
|
||||
|
||||
$segmentId = (int) $request->input('segment_id');
|
||||
$segmentId = $request->input('segment_id') ? (int) $request->input('segment_id') : null;
|
||||
$perPage = (int) ($request->input('per_page') ?? 25);
|
||||
|
||||
$query = Contract::query()
|
||||
->join('contract_segment', function ($j) use ($segmentId) {
|
||||
$j->on('contract_segment.contract_id', '=', 'contracts.id')
|
||||
->where('contract_segment.segment_id', '=', $segmentId)
|
||||
->where('contract_segment.active', true);
|
||||
})
|
||||
->with([
|
||||
'clientCase.person.phones',
|
||||
'clientCase.client.person',
|
||||
'account',
|
||||
])
|
||||
->select('contracts.*')
|
||||
->latest('contracts.id');
|
||||
|
||||
// Optional segment filter
|
||||
if ($segmentId) {
|
||||
$query->join('contract_segment', function ($j) use ($segmentId) {
|
||||
$j->on('contract_segment.contract_id', '=', 'contracts.id')
|
||||
->where('contract_segment.segment_id', '=', $segmentId)
|
||||
->where('contract_segment.active', true);
|
||||
});
|
||||
}
|
||||
|
||||
if ($q = trim((string) $request->input('q'))) {
|
||||
$query->where(function ($w) use ($q) {
|
||||
$w->where('contracts.reference', 'ILIKE', "%{$q}%");
|
||||
@@ -335,6 +352,30 @@ public function contracts(Request $request, PhoneSelector $selector): \Illuminat
|
||||
->where('client_cases.client_id', $clientId);
|
||||
}
|
||||
|
||||
// Date range filters for start_date
|
||||
if ($startDateFrom = $request->input('start_date_from')) {
|
||||
$query->where('contracts.start_date', '>=', $startDateFrom);
|
||||
}
|
||||
|
||||
if ($startDateTo = $request->input('start_date_to')) {
|
||||
$query->where('contracts.start_date', '<=', $startDateTo);
|
||||
}
|
||||
|
||||
// Date range filters for account.promise_date
|
||||
$promiseDateFrom = $request->input('promise_date_from');
|
||||
$promiseDateTo = $request->input('promise_date_to');
|
||||
|
||||
if ($promiseDateFrom || $promiseDateTo) {
|
||||
$query->whereHas('account', function ($q) use ($promiseDateFrom, $promiseDateTo) {
|
||||
if ($promiseDateFrom) {
|
||||
$q->where('promise_date', '>=', $promiseDateFrom);
|
||||
}
|
||||
if ($promiseDateTo) {
|
||||
$q->where('promise_date', '<=', $promiseDateTo);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Optional phone filters
|
||||
if ($request->boolean('only_mobile') || $request->boolean('only_validated')) {
|
||||
$query->whereHas('clientCase.person.phones', function ($q) use ($request) {
|
||||
@@ -359,6 +400,8 @@ public function contracts(Request $request, PhoneSelector $selector): \Illuminat
|
||||
'id' => $contract->id,
|
||||
'uuid' => $contract->uuid,
|
||||
'reference' => $contract->reference,
|
||||
'start_date' => $contract->start_date,
|
||||
'promise_date' => $contract->account?->promise_date,
|
||||
'case' => [
|
||||
'id' => $contract->clientCase?->id,
|
||||
'uuid' => $contract->clientCase?->uuid,
|
||||
@@ -481,4 +524,47 @@ public function storeFromContracts(StorePackageFromContractsRequest $request, Ph
|
||||
|
||||
return back()->with('success', 'Package created from contracts');
|
||||
}
|
||||
|
||||
/**
|
||||
* Flatten nested meta structure into dot-notation key-value pairs.
|
||||
* Extracts 'value' from objects with {title, value, type} structure.
|
||||
* Also creates direct access aliases for nested fields (skipping numeric keys).
|
||||
*/
|
||||
private function flattenMeta(array $meta, string $prefix = ''): array
|
||||
{
|
||||
$result = [];
|
||||
foreach ($meta as $key => $value) {
|
||||
$newKey = $prefix === '' ? $key : "{$prefix}.{$key}";
|
||||
|
||||
if (is_array($value)) {
|
||||
// Check if it's a structured meta entry with 'value' field
|
||||
if (isset($value['value'])) {
|
||||
$result[$newKey] = $value['value'];
|
||||
// If parent key is numeric, also create direct alias without the number
|
||||
if ($prefix !== '' && is_numeric($key)) {
|
||||
$result[$key] = $value['value'];
|
||||
}
|
||||
} else {
|
||||
// Recursively flatten nested arrays
|
||||
$nested = $this->flattenMeta($value, $newKey);
|
||||
$result = array_merge($result, $nested);
|
||||
|
||||
// If current key is numeric, also flatten without it for easier access
|
||||
if (is_numeric($key)) {
|
||||
$directNested = $this->flattenMeta($value, $prefix);
|
||||
foreach ($directNested as $dk => $dv) {
|
||||
// Only add if not already set (prefer first occurrence)
|
||||
if (! isset($result[$dk])) {
|
||||
$result[$dk] = $dv;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$result[$newKey] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user