@@ -103,7 +103,9 @@ export class ChequeReqList extends BaseRepository {
103103 `SELECT
104104 s.id AS student_id,
105105 app.id AS application_id,
106- rt.description AS funding_type,
106+ CASE WHEN d.disbursement_type_id = 11 THEN CONCAT(rt.description, ' (WDA)')
107+ WHEN d.disbursement_type_id = 10 THEN CONCAT(rt.description, ' (LMDA)')
108+ ELSE rt.description END AS funding_type,
107109 d.financial_batch_id_year,
108110 d.financial_batch_id,
109111 CASE
@@ -181,9 +183,11 @@ export class ChequeReqList extends BaseRepository {
181183 ) ;
182184
183185 if ( pdfData . length ) {
184- const groupByBatchId = _ . groupBy ( pdfData , "financial_batch_id" ) ;
185-
186- return Object . values ( groupByBatchId ) ;
186+ // Sort by financial_batch_id and financial_coding, then group by composite key
187+ const sorted = _ . orderBy ( pdfData , [ 'financial_batch_id' , 'financial_coding' ] ) ;
188+ return Object . values (
189+ _ . groupBy ( sorted , ( item ) => `${ item . financial_batch_id } _${ item . financial_coding } ` )
190+ ) ;
187191 } else {
188192 return [ ] ;
189193 }
@@ -199,37 +203,47 @@ export class ChequeReqList extends BaseRepository {
199203 isSigned : boolean
200204 ) : Promise < any > {
201205 try {
202- const batchTotal = await this . mainDb
203- . select (
204- "d.financial_batch_id" ,
205- this . mainDb . raw ( `FORMAT(SUM(d.disbursed_amount), 'C') as total` )
206- )
207- . from ( "sfa.funding_request AS fr" )
208- . join ( "sfa.request_type AS rt" , "rt.id" , "=" , "fr.request_type_id" )
209- . join ( "sfa.disbursement AS d" , "fr.id" , "=" , "d.funding_request_id" )
210- . join ( "sfa.application AS app" , "fr.application_id" , "=" , "app.id" )
211- . join ( "sfa.student AS s" , "app.student_id" , "=" , "s.id" )
212- . join ( "SFA.person AS p" , "s.person_id" , "=" , "p.id" )
213- . where ( "d.financial_batch_run_date" , "=" , issueDate )
214- . where ( "d.financial_batch_serial_no" , "=" , serialNo )
215- . whereNotNull ( "d.due_date" )
216- . andWhere (
217- isSigned
218- ? this . mainDb . raw ( `
219- (
220- CASE WHEN rt.batch_group_id = 5 THEN
221- CASE
222- WHEN fr.yea_request_type = 1 THEN 3
223- WHEN fr.yea_request_type = 2 THEN 4
224- ELSE 1
225- END
226- WHEN rt.batch_group_id = 1 THEN sfa.get_batch_group_id_fct(fr.id)
227- ELSE rt.batch_group_id
228- END
229- ) = 3` )
230- : this . mainDb . raw ( "1 = 1" )
231- )
232- . groupBy ( "d.financial_batch_id" ) ;
206+ const batchTotal = await this . mainDb . raw (
207+ `
208+ SELECT
209+ d.financial_batch_id,
210+ sfa.fn_financial_coding(d.id) AS financial_coding,
211+ FORMAT(SUM(d.disbursed_amount), 'C') as total
212+ FROM sfa.funding_request AS fr
213+ JOIN sfa.request_type AS rt ON rt.id = fr.request_type_id
214+ JOIN sfa.disbursement AS d ON fr.id = d.funding_request_id
215+ LEFT JOIN sfa.application a1 ON a1.id = fr.application_id
216+ LEFT JOIN sfa.funding_application a2 ON a2.id = fr.funding_application_id
217+ CROSS APPLY (
218+ SELECT
219+ COALESCE(a1.id, a2.student_id) AS id,
220+ COALESCE(a1.student_id, a2.student_id) AS student_id,
221+ COALESCE(a1.student_number, '') as student_number
222+ ) AS app
223+ JOIN sfa.student AS s ON app.student_id = s.id
224+ JOIN sfa.person AS p ON s.person_id = p.id
225+ WHERE d.financial_batch_run_date = ?
226+ AND d.financial_batch_serial_no = ?
227+ AND d.due_date IS NOT NULL
228+ AND (
229+ ? = 0 OR
230+ (
231+ CASE WHEN rt.batch_group_id = 5 THEN
232+ CASE
233+ WHEN fr.yea_request_type = 1 THEN 3
234+ WHEN fr.yea_request_type = 2 THEN 4
235+ ELSE 1
236+ END
237+ WHEN rt.batch_group_id = 1 THEN sfa.get_batch_group_id_fct(fr.id)
238+ ELSE rt.batch_group_id
239+ END
240+ ) = 3
241+ )
242+ GROUP BY d.financial_batch_id, sfa.fn_financial_coding(d.id)
243+ ORDER BY 1, 2;
244+ ` ,
245+ [ issueDate , serialNo , isSigned ? 1 : 0 ]
246+ ) ;
233247
234248 return batchTotal || [ ] ;
235249 } catch ( error ) {
0 commit comments