1+ import { Knex } from "knex" ;
2+ import moment from "moment" ;
3+ import { BaseRepository } from "../base-repository" ;
4+
5+ interface ChequeReqDTO {
6+ request_type_id : number ,
7+ disbursement_id : number ,
8+ disbursed_amount : number ,
9+ bg_id : number ,
10+ issue_date : Date | string ,
11+ f_year : number
12+ } ;
13+
14+ export class ChequeReqList extends BaseRepository {
15+
16+ //private applicationRepo: ApplicationRepository;
17+ //private application: Partial<ApplicationDTO> = {};
18+
19+ constructor ( maindb : Knex < any , unknown > ) {
20+ super ( maindb ) ;
21+ //this.applicationRepo = new ApplicationRepository(maindb);
22+ }
23+
24+ //AfterPForm ()
25+ async validate (
26+ issueDate : string
27+ ) : Promise < any > {
28+
29+ let start_p = '' ;
30+ let serial_no_p = null ;
31+
32+ if ( true ) { //start_p === '0'
33+ //EXEC sfa.assign_cheque_req_batch @issue_date_p = @issue_date_p OUTPUT, @serial_no_p = @serial_no_p OUTPUT;
34+ serial_no_p = await this . assignChequeReqBatch ( issueDate ) ;
35+ console . log ( serial_no_p ) ;
36+
37+ } else {
38+ //:issue_date_p := TO_DATE(SUBSTR(:start_p,1,11),'DD MON RRRR');
39+ //:serial_no_p := TO_NUMBER(SUBSTR(:start_p,13));
40+ }
41+ //:issue_date_str_p := TO_CHAR (:issue_date_p, 'YYYYMMDD'); -- Added for Windows 7 issue with dateCOUNT(d.disbursement_id)
42+ const disburseCount = await this . mainDb ( 'sfa.disbursement' )
43+ . count ( 'id as count' )
44+ . where ( 'financial_batch_run_date' , issueDate )
45+ . where ( 'financial_batch_serial_no' , serial_no_p ) ;
46+
47+ if ( ! disburseCount ?. [ 0 ] . count ) {
48+ return { text : 'There are no new disbursements for this date. This will end the report.' }
49+ }
50+
51+ return serial_no_p ;
52+ }
53+
54+ //assign_cheque_req_batch ()
55+ async assignChequeReqBatch (
56+ issueDate : string
57+ ) : Promise < number | undefined > {
58+ try {
59+ const fb_serial = await this . getScalarValue < number > ( "get_serial_fct" , [
60+ `'${ issueDate } '`
61+ ] ) ;
62+
63+ let disb_list : ChequeReqDTO [ ] = [ ] ; // CURSOR disb_cur
64+ let num_request_type ;
65+ let num_batch ;
66+ let num_bg ;
67+ let int_count ;
68+ let batch_year ;
69+ let ori_disb_sign ;
70+ let disb_sign ;
71+ let fb_seq_name ;
72+ let id_year ;
73+ let ori_fb_year ;
74+
75+ disb_list = await this . getDisburseList ( issueDate ) ?? [ ] ;
76+
77+ num_request_type = null ;
78+ num_bg = null ;
79+ int_count = 0 ;
80+
81+ //for await (const disburse of disb_list) {
82+ for ( let index = 0 ; index < disb_list . length ; index ++ ) {
83+ const disburse = disb_list [ index ] ;
84+
85+ disb_sign = disburse . disbursed_amount < 0
86+ ? 'NEG'
87+ : 'POS' ;
88+
89+ // If this is the first time through or the request type is new or the batch group had changed or the disbursement sign has changed
90+ // or the financial batch year has changed or 25 records have been added to a batch then start a new batch
91+ if (
92+ num_request_type === null ||
93+ num_request_type !== disburse . request_type_id ||
94+ num_bg === null ||
95+ num_bg !== disburse . bg_id ||
96+ ori_disb_sign === null ||
97+ ori_disb_sign !== disb_sign ||
98+ ori_fb_year === null ||
99+ ori_fb_year !== disburse . f_year ||
100+ int_count === 25
101+ ) {
102+ num_request_type = disburse . request_type_id ;
103+ num_bg = disburse . bg_id ;
104+ ori_disb_sign = disb_sign ;
105+ ori_fb_year = disburse . f_year ;
106+ batch_year = parseInt ( disburse . f_year . toString ( ) . substring ( 2 , 4 ) , 10 ) ;
107+
108+ // Select the appropriate sequence name.
109+ if ( disburse . bg_id === 1 ) {
110+ fb_seq_name = 'FB_' + disburse . f_year + '_STA_SEQ' ;
111+ } else if ( disburse . bg_id === 2 ) {
112+ fb_seq_name = 'FB_' + disburse . f_year + '_CSL_SEQ' ;
113+ } else if ( disburse . bg_id === 3 ) {
114+ fb_seq_name = 'FB_' + disburse . f_year + '_ORI_SEQ' ;
115+ } else if ( disburse . bg_id === 4 ) {
116+ fb_seq_name = 'FB_' + disburse . f_year + '_OTHER_SEQ' ;
117+ }
118+
119+ const validateSequence : any = await this . mainDb . raw ( `
120+ SELECT 1
121+ FROM sys.sequences sq
122+ INNER JOIN sys.schemas s ON s.schema_id = sq.schema_id
123+ WHERE sq.name = '${ fb_seq_name } '
124+ AND s.name = 'sfa'
125+ ` ) ;
126+ console . log ( "validateSequence" , validateSequence ) ;
127+
128+ const existSequence = validateSequence ?. length > 0 ;
129+
130+ if ( existSequence ) {
131+
132+ const getNumBatch : any = await this . mainDb
133+ . raw ( `SELECT NEXT VALUE FOR sfa.${ fb_seq_name } AS num_batch` ) ;
134+
135+ num_batch = parseInt ( getNumBatch [ 0 ] . num_batch ) ;
136+ console . log ( "1 NUM_BATCH: " , getNumBatch ) ;
137+ int_count = 1 ;
138+ } else {
139+ const createSequence : any = await this . mainDb . raw ( `
140+ CREATE SEQUENCE sfa.${ fb_seq_name }
141+ START WITH 1
142+ INCREMENT BY 1;
143+ ` ) ;
144+ console . log ( "fb_seq_name:" , fb_seq_name ) ;
145+ console . log ( "SEQUENCE CREADA:" , createSequence ) ;
146+
147+ const getNumBatch : any = await this . mainDb
148+ . raw ( `SELECT NEXT VALUE FOR sfa.${ fb_seq_name } AS num_batch` ) ;
149+
150+ num_batch = parseInt ( getNumBatch [ 0 ] . num_batch ) ;
151+
152+ console . log ( "2 NUM_BATCH: " , getNumBatch ) ;
153+ int_count = 1 ;
154+ }
155+
156+ //Get the next batch number from the sequence
157+ //EXEC('SELECT @num_batch = NEXT VALUE FOR ' + @fb_seq_name, N'@num_batch INT OUTPUT', @num_batch OUTPUT);
158+ //SET @int_count = 1;
159+ } else if ( ( num_request_type === disburse . request_type_id ) && ( int_count !== 25 ) ) {
160+ int_count ++ ;
161+ }
162+
163+ //const updateDisburse = await this.mainDb("sfa.disbursement")
164+ // .where({ id: disburse.disbursement_id })
165+ // .update({
166+ // financial_batch_id: num_batch,
167+ // financial_batch_id_year: batch_year,
168+ // financial_batch_run_date: issueDate,
169+ // financial_batch_serial_no: fb_serial
170+ // });
171+
172+ console . log ( {
173+ id : disburse . disbursement_id ,
174+ financial_batch_id : num_batch ,
175+ financial_batch_id_year : batch_year ,
176+ financial_batch_run_date : issueDate ,
177+ financial_batch_serial_no : fb_serial
178+ } ) ;
179+
180+
181+ }
182+
183+ return fb_serial ;
184+ } catch ( error ) {
185+ console . log ( error ) ;
186+ return undefined ;
187+ }
188+ }
189+
190+ async getDisburseList (
191+ issueDate : string
192+ ) : Promise < ChequeReqDTO [ ] | undefined > {
193+ try {
194+ const disb_list = await this . mainDb ( 'sfa.funding_request as fr' )
195+ . select (
196+ 'fr.request_type_id' ,
197+ 'd.id as disbursement_id' ,
198+ 'd.disbursed_amount' ,
199+ this . mainDb . raw ( `
200+ CASE
201+ WHEN rt.batch_group_id = 5 THEN
202+ CASE fr.yea_request_type
203+ WHEN 1 THEN 3
204+ WHEN 2 THEN 4
205+ ELSE 1
206+ END
207+ ELSE ISNULL(sfa.get_batch_group_id_fct(fr.id), rt.batch_group_id)
208+ END AS bg_id` ) ,
209+ 'd.issue_date' ,
210+ this . mainDb . raw ( 'sfa.get_fiscal_year_fct(d.issue_date) AS f_year' ) ,
211+ )
212+ . innerJoin ( 'sfa.disbursement as d' , 'fr.id' , 'd.funding_request_id' )
213+ . innerJoin ( 'sfa.request_type as rt' , 'fr.request_type_id' , 'rt.id' )
214+ . whereNotNull ( 'rt.batch_group_id' )
215+ . where ( ( builder ) => {
216+ builder . where ( 'd.disbursed_amount' , '<>' , 0 ) . orWhere ( 'fr.request_type_id' , 4 ) ;
217+ } )
218+ . where ( 'd.issue_date' , '<=' , issueDate )
219+ . where ( 'd.issue_date' , '>' , this . mainDb . raw ( "CONVERT(DATE, '20050331', 112)" ) )
220+ . whereNotNull ( 'd.due_date' )
221+ . whereNull ( 'd.financial_batch_id' )
222+ . orderBy ( 'bg_id' )
223+ . orderBy ( 'fr.request_type_id' )
224+ . orderBy ( 'f_year' )
225+ . orderBy ( 'd.disbursed_amount' , 'desc' ) ;
226+
227+ return disb_list ;
228+ } catch ( error ) {
229+ console . log ( error ) ;
230+ return undefined ;
231+ }
232+ }
233+
234+
235+ }
0 commit comments