Skip to content

Commit 3e64ed7

Browse files
authored
Merge pull request #85 from MariazelHdez/test
Update MSFAA received
2 parents d39259f + 9765a49 commit 3e64ed7

File tree

15 files changed

+517
-61
lines changed

15 files changed

+517
-61
lines changed

db/4_functions.sql

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4834,40 +4834,53 @@ BEGIN
48344834
END;
48354835
GO
48364836

4837-
CREATE OR ALTER FUNCTION sfa.fn_get_csl_cert_seq_num
4838-
(
4839-
@FROM_DATE_P DATE,
4840-
@TO_DATE_P DATE
4841-
)
4842-
RETURNS INT AS
4843-
BEGIN
4844-
DECLARE @v_cert_count NUMERIC = 0;
48454837

4846-
SELECT @v_cert_count = count(d.id)
4847-
FROM sfa.funding_request AS fr
4848-
INNER JOIN sfa.disbursement AS d ON fr.id = d.funding_request_id
4849-
INNER JOIN sfa.request_type AS rt ON fr.request_type_id = rt.id
4850-
INNER JOIN (
4851-
SELECT m.msfaa_status, app.academic_year_id, app.id
4852-
FROM sfa.msfaa AS m
4853-
INNER JOIN sfa.application AS app ON app.id = m.application_id
4854-
WHERE app.id = m.application_id
4855-
) AS mhd ON fr.application_id = mhd.id
4856-
WHERE (mhd.msfaa_status = 'Received' OR mhd.academic_year_id <= 2012)
4857-
AND issue_date >= @FROM_DATE_P
4858-
AND issue_date <= @TO_DATE_P
4859-
AND d.due_date IS NOT NULL
4860-
AND d.transaction_number IS NOT NULL
4861-
AND d.csl_cert_seq_number IS NULL
4862-
AND disbursement_type_id IN (3, 4, 5, 7, 9)
4863-
AND fr.request_type_id IN (4, 5, 6, 15, 16, 17, 18, 19, 22, 23, 24, 26, 27, 28, 29, 30, 31, 32, 33, 35, 47)
4864-
AND d.transaction_number IN (
4865-
SELECT d1.transaction_number
4866-
FROM sfa.disbursement AS d1
4867-
INNER JOIN sfa.funding_request AS fr1 ON d1.funding_request_id = fr1.id
4868-
WHERE fr1.request_type_id IN (4, 5))
4869-
4870-
RETURN @v_cert_count;
4838+
CREATE OR ALTER FUNCTION sfa.get_serial_fct(@date_p DATE)
4839+
RETURNS INT
4840+
AS
4841+
BEGIN
4842+
DECLARE @v_fbsn INT;
4843+
4844+
SELECT @v_fbsn = ISNULL(MAX(financial_batch_serial_no), 0) + 1
4845+
FROM disbursement
4846+
WHERE financial_batch_run_date = @date_p;
4847+
4848+
RETURN @v_fbsn;
4849+
END
4850+
GO
4851+
4852+
CREATE OR ALTER FUNCTION sfa.get_fiscal_year_fct(@date_p DATE)
4853+
RETURNS INT
4854+
AS
4855+
BEGIN
4856+
/* Determine the batch year the disbursement is part of */
4857+
DECLARE @fb_year INT = YEAR(@date_p);
4858+
4859+
IF @date_p < DATEFROMPARTS(@fb_year, 04, 01)
4860+
SET @fb_year = @fb_year - 1;
4861+
4862+
RETURN @fb_year;
4863+
END
4864+
GO
4865+
4866+
CREATE OR ALTER FUNCTION sfa.get_batch_group_id_fct (@funding_request_id_p INT)
4867+
RETURNS INT
4868+
AS
4869+
BEGIN
4870+
DECLARE @federal_institution_code NVARCHAR(30);
4871+
4872+
SELECT @federal_institution_code = ic.federal_institution_code
4873+
FROM sfa.funding_request fr
4874+
INNER JOIN sfa.application app ON fr.application_id = app.id
4875+
INNER JOIN sfa.institution_campus ic ON app.institution_campus_id = ic.id
4876+
WHERE fr.id = @funding_request_id_p;
4877+
4878+
IF (@federal_institution_code = 'LVAA') -- 'Yukon Col' OR 'Yukon U'
4879+
BEGIN
4880+
RETURN 1;
4881+
END
4882+
4883+
RETURN 3;
48714884
END
48724885
GO
48734886

@@ -5023,3 +5036,4 @@ FROM sfa.disbursement d
50235036
WHERE fr.application_id = @application_id
50245037
ORDER BY d.due_date DESC;
50255038
GO
5039+

src/api/repositories/assessment/assessment-cslft-repository.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ export class AssessmentCslftRepository extends AssessmentBaseRepository {
152152

153153
async getCalculatedValues(): Promise<void> {
154154

155+
const pDaysDiff = moment.utc(this.assessment.pstudy_end_date).diff(moment.utc(this.assessment.pstudy_start_date), "day");
156+
this.assessment.pstudy_weeks = Math.trunc((pDaysDiff + 1)/7 + .9999);
157+
this.assessment.pstudy_months = Math.trunc((pDaysDiff + 1)/30.44 + .9999);
158+
155159
// Get the study expenses.
156160
this.assessment.uncapped_costs_total = await this.getExpenseAmount(this.application.id, 2);
157161

@@ -1116,6 +1120,7 @@ export class AssessmentCslftRepository extends AssessmentBaseRepository {
11161120

11171121
if (payload.data.id && payload.data.id > 0)
11181122
{
1123+
console.log(payload.data);
11191124
result.data = await this.updateAssessment(payload.data.id, payload.data);
11201125
}
11211126
else
Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
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+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./cheque-req-list-repository";
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { ExpenseRepository } from './../../repositories/expense/expense-repository';
2+
import knex from "knex";
3+
import express, { Request, Response } from "express";
4+
import { body, param } from "express-validator";
5+
import { ReturnValidationErrors } from "../../middleware";
6+
import { DB_CONFIG } from "../../config";
7+
import { AssessmentCslftRepository } from "../../repositories";
8+
import { ChequeReqList } from '../../repositories/cheque_req_list';
9+
10+
const db = knex(DB_CONFIG)
11+
export const chequeReqRouter = express.Router();
12+
13+
chequeReqRouter.post("/",
14+
async (req: Request, res: Response) => {
15+
const { issueDate = "" } = req.body;
16+
17+
if (issueDate?.length !== 10) {
18+
return res.status(404).send();
19+
}
20+
21+
try {
22+
const chequeRequest = new ChequeReqList(db);
23+
24+
const validate = await chequeRequest.validate(issueDate);
25+
26+
console.log(validate);
27+
28+
return res.status(200).json(validate);
29+
} catch (error) {
30+
console.log(error);
31+
return res.status(404).send();
32+
}
33+
});

0 commit comments

Comments
 (0)