fix installment for carry over balance parent account
This commit is contained in:
@@ -651,6 +651,13 @@ class WhatsappController extends BaseController
|
||||
// ignore membership annotation errors
|
||||
}
|
||||
|
||||
// Annotate latest invite delivery state from whatsapp_invites_log.
|
||||
try {
|
||||
$this->annotateWhatsappDeliveryStatuses($classSections, $schoolYear);
|
||||
} catch (\Throwable $e) {
|
||||
// Keep the roster usable even if delivery history cannot be loaded.
|
||||
}
|
||||
|
||||
// 4) (Optional) Teachers, if you keep them in teacher_class
|
||||
try {
|
||||
$tb = $this->db->table('teacher_class tc')
|
||||
@@ -687,6 +694,83 @@ class WhatsappController extends BaseController
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add latest email delivery state for each primary parent/class row.
|
||||
*
|
||||
* Exact class-section logs win. Older consolidated "all classes" logs without
|
||||
* class_section_id are used only as a fallback for the parent/year.
|
||||
*/
|
||||
private function annotateWhatsappDeliveryStatuses(array &$classSections, string $schoolYear): void
|
||||
{
|
||||
if (! $this->db->tableExists('whatsapp_invites_log') || empty($classSections)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$sectionIds = array_keys($classSections);
|
||||
$parentIds = [];
|
||||
foreach ($classSections as $sec) {
|
||||
foreach (($sec['parents'] ?? []) as $p) {
|
||||
$primaryId = (int) ($p['primary_id'] ?? 0);
|
||||
if ($primaryId > 0) {
|
||||
$parentIds[$primaryId] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($sectionIds) || empty($parentIds)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$rows = $this->db->table('whatsapp_invites_log')
|
||||
->select('parent_id, class_section_id, status, error_message, sent_at')
|
||||
->where('school_year', $schoolYear)
|
||||
->whereIn('parent_id', array_keys($parentIds))
|
||||
->groupStart()
|
||||
->whereIn('class_section_id', $sectionIds)
|
||||
->orWhere('class_section_id IS NULL', null, false)
|
||||
->groupEnd()
|
||||
->orderBy('sent_at', 'DESC')
|
||||
->orderBy('id', 'DESC')
|
||||
->get()
|
||||
->getResultArray();
|
||||
|
||||
$exact = [];
|
||||
$fallbackByParent = [];
|
||||
foreach ($rows as $row) {
|
||||
$parentId = (int) ($row['parent_id'] ?? 0);
|
||||
$sectionId = (int) ($row['class_section_id'] ?? 0);
|
||||
if ($parentId <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($sectionId > 0) {
|
||||
$key = $sectionId . ':' . $parentId;
|
||||
if (! isset($exact[$key])) {
|
||||
$exact[$key] = $row;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! isset($fallbackByParent[$parentId])) {
|
||||
$fallbackByParent[$parentId] = $row;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($classSections as $sid => &$sec) {
|
||||
foreach ($sec['parents'] as &$p) {
|
||||
$primaryId = (int) ($p['primary_id'] ?? 0);
|
||||
$log = $exact[$sid . ':' . $primaryId] ?? $fallbackByParent[$primaryId] ?? null;
|
||||
|
||||
$p['delivery_status'] = $log ? strtolower((string) ($log['status'] ?? '')) : 'not_sent';
|
||||
$p['delivery_sent_at'] = $log['sent_at'] ?? null;
|
||||
$p['delivery_error'] = $log['error_message'] ?? null;
|
||||
$p['delivery_class_specific'] = $log && ! empty($log['class_section_id']);
|
||||
}
|
||||
unset($p);
|
||||
}
|
||||
unset($sec);
|
||||
}
|
||||
|
||||
/**
|
||||
* POST: Update WhatsApp group membership flags for a class/parent(s).
|
||||
* Accepts fields:
|
||||
|
||||
Reference in New Issue
Block a user