3 require_once(__DIR__ .
'/../library/DateConvert.i');
14 function GetSearchInData() {
16 array(
"value" =>
"",
"text" =>
"Select..."),
17 array(
"value" =>
"email",
"text" =>
"Email Address"),
18 array(
"value" =>
"dob",
"text" =>
"Date Of Birth"),
19 array(
"value" =>
"ssn",
"text" =>
"Social Security #"),
20 array(
"value" =>
"name",
"text" =>
"Last Name"),
21 array(
"value" =>
"account",
"text" =>
"Account #"));
29 function GetApplicantSearchInData() {
31 array(
"value" =>
"email",
"text" =>
"Email"),
32 array(
"value" =>
"account",
"text" =>
"Account #"),
33 array(
"value" =>
"username",
"text" =>
"Username")
42 function GetWithStatusData() {
44 array(
"value" =>
"",
"text" =>
"All"),
45 array(
"value" =>
"NoStat",
"text" =>
"Submitted"),
46 array(
"value" =>
"SubErr",
"text" =>
"Submitted w/Errors"),
47 array(
"value" =>
"Approved",
"text" =>
"Approved"),
48 array(
"value" =>
"Rejected",
"text" =>
"Rejected"),
49 array(
"value" =>
"Pending",
"text" =>
"Pending"),
50 array(
"value" =>
"ReqReview",
"text" =>
"Requires Review"));
58 function GetRangeTypeData() {
60 array(
"value" =>
"",
"text" =>
"ALL Applications"),
61 array(
"value" =>
"submit",
"text" =>
"Applications Submitted between"),
62 array(
"value" =>
"start",
"text" =>
"Applications Started between"),
63 array(
"value" =>
"modified",
"text" =>
"Applications Last Modified between"));
71 function GetSortByData() {
73 array(
"value" =>
"startAD",
"text" =>
"Most recently started first"),
74 array(
"value" =>
"startBC",
"text" =>
"Most recently started last"),
75 array(
"value" =>
"statusA",
"text" =>
"Status ordered A-Z"),
76 array(
"value" =>
"statusZ",
"text" =>
"Status ordered Z-A"),
77 array(
"value" =>
"typeA",
"text" =>
"Type ordered A-Z"),
78 array(
"value" =>
"typeZ",
"text" =>
"Type ordered Z-A"),
79 array(
"value" =>
"nameA",
"text" =>
"Name ordered A-Z"),
80 array(
"value" =>
"nameZ",
"text" =>
"Name ordered Z-A"));
88 function GetActionData() {
90 array(
"value" =>
"",
"text" =>
"Action"),
91 array(
"value" =>
"delete",
"text" =>
"Delete"),
92 array(
"value" =>
"view",
"text" =>
"View Application"));
100 function GetFailedLoginAttempts() {
122 function DeleteLoanApps($dbh, $Cu, $parameters) {
125 $results = _ValidateReadLoanAppsParameters($parameters, $isDelete);
126 if ($results[
"status"] !==
"000") {
127 throw new exception ($results[
"error"], 1);
129 $parameters = $results[
"data"];
131 $sql =
"delete from lnappuserresponse where respid in 132 (select r.respid from lnappuserresponse r 133 inner join lnappuser u on r.userid = u.userid 134 where u.cu = '$Cu' and r.respid in " . $parameters[
"deleteIn"] .
")";
137 $sth = db_query($sql, $dbh);
139 throw new exception (
"Delete query failed.", 2);
142 $returnArray = ReadLoanApps($dbh, $Cu, $parameters, $isDelete);
143 $returnArray[
"info"] =
"Loans were deleted successfully.";
144 }
catch (exception $e) {
147 $returnArray = ReadLoanApps($dbh, $Cu, $parameters, $isDelete);
148 $returnArray[
"status"] = $e->getCode();
149 $returnArray[
"error"] = $e->getMessage();
168 function ReadLoanApps($dbh, $Cu, $parameters, $isDelete) {
172 $results = _ValidateReadLoanAppsParameters($parameters,
false);
173 if ($results[
"status"] !==
"000") {
174 throw new exception ($results[
"error"], 1);
176 extract($results[
"data"]);
178 extract($parameters);
183 $tz = GetCreditUnionTimezone($dbh, $Cu);
185 $select =
"set time zone '$tz'; 186 select case when l.respstatus = '000' then 'Submitted' 187 when l.respstatus in ('020','021','022') then 'Submitted With Errors' 188 when l.respstatus = '027' then 'Approved' 189 when l.respstatus = '028' then 'Rejected' 190 when l.respstatus = '026' then 'Pending' 191 when l.respstatus in ('029','030') then 'Requires Review' 192 else '' end as status, 193 trim(l.resplname) || ', ' || trim(l.respfname) || ' ' || l.respmname as name, m.loantitle, l.respid, a.userid, l.loanid, 194 l.respstatus, l.respamt, l.respstarton, l.respdob, l.respssn, l.respmember, lower(a.email) as email, l.respcoreloanappid";
195 $sql =
"from lnappuser a 196 inner join lnappuserresponse l on a.userid = l.userid 197 inner join lnappschemamaster m on l.loanid = m.loanid";
199 $where = array(
"a.cu = '" . prep_save($Cu, 10) .
"'");
201 $where[] =
"a.userid = " . intval($userId);
202 }
else if ($lookFor !=
"") {
205 $where[] =
"lower(a.email) = '" . strtolower(prep_save($lookFor, 50)) .
"'";
208 $where[] =
"l.respdob = '" . prep_save($lookFor, 10) .
"'";
211 $where[] =
"l.respssn = '" . prep_save($lookFor, 11) .
"'";
214 $where[] =
"strpos(lower(l.resplname), '" . strtolower(prep_save($lookFor, 55)) .
"') > 0";
217 $where[] =
"l.respmember = '" . prep_save($lookFor, 15) .
"'";
220 $where[] =
"l.respcoreloanappid = '" . prep_save($lookFor, 15) .
"'";
225 if ($withStatus !=
"") {
226 switch ($withStatus) {
229 $where[] =
"(l.respstatus = '000' or l.respstatus = '0' or l.respstatus is null)";
232 $where[] =
"l.respstatus in ('020','021','022')";
235 $where[] =
"l.respstatus = '027'";
238 $where[] =
"l.respstatus = '028'";
241 $where[] =
"l.respstatus = '026'";
244 $where[] =
"l.respstatus in ('029','030')";
248 # show all loanapps regardless of status 253 if ($rangeType !=
"") {
254 $dateFilterField =
"";
255 switch ($rangeType) {
257 $dateFilterField =
'l.respsubmiton';
260 $dateFilterField =
'l.respstarton';
263 $dateFilterField =
'l.respmodifiedon';
266 if ($dateFilterField !=
"") {
267 if ($fromDate !=
"") {
268 $where[] =
"$dateFilterField >= '" . prep_save($fromDate) .
"'";
271 $where[] =
"$dateFilterField <= '" . prep_save($toDate) .
"'";
276 if (count($where) > 0) {
277 $sql .=
" where " . implode(
" and ", $where);
282 $sortSQL =
"order by l.respstarton desc";
285 $sortSQL =
"order by l.respstarton asc";
288 $sortSQL =
"order by 1 asc";
291 $sortSQL =
"order by 1 desc";
294 $sortSQL =
"order by m.loantitle asc";
297 $sortSQL =
"order by m.loantitle desc";
300 $sortSQL =
"order by 2 asc";
303 $sortSQL =
"order by 2 desc";
310 $limitSQL = $limit == 0 && $offset == 0 ?
"" :
"limit $limit offset $offset";
312 $countSQL =
"select count(*) $sql";
313 $mainSQL =
"$select $sql $sortSQL $limitSQL";
315 $sth = db_query($countSQL, $dbh);
317 throw new exception (
"count query failed.", 2);
319 $results = db_fetch_row($sth, 0);
320 $total = intval($results[0]);
322 $sth = db_query($mainSQL, $dbh);
324 throw new exception (
"main query failed.", 3);
326 $results = db_fetch_all($sth);
327 $results = $results ===
false ? array() : $results;
330 foreach($results as $record) {
331 $gridData[] = array(
"loanappid" => $record[
"respcoreloanappid"],
"start" => $record[
"respstarton"],
"account" => $record[
"respmember"],
332 "status" => $record[
"status"],
"respid" => $record[
"respid"],
"dob" => $record[
"respdob"],
"title" => $record[
"loantitle"],
333 "ssn" => $record[
"respssn"],
"email" => $record[
"email"],
"name" => $record[
"name"],
"userid" => $record[
"userid"]);
336 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"gridData" => array(
"total" => $total,
"data" => $gridData));
337 }
catch (exception $e) {
338 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"gridData" => array(
"total" => 0,
"data" => array()));
354 function _ValidateReadLoanAppsParameters($parameters, $isDelete) {
357 $loanids = HCU_array_key_value(
"loanids", $parameters);
358 $loanids = $loanids ===
false ?
"" : trim($loanids);
359 if ($loanids ==
"") {
360 throw new exception (
"Loan ids are required.", 14);
362 $loanids = HCU_JsonDecode($loanids);
363 if (count($loanids) == 0) {
364 throw new exception (
"Loan ids are required.", 15);
366 foreach($loanids as $i => $loanid) {
367 if (!ctype_digit(strval($loanid))) {
368 throw new exception (
"Loan ids are invalid.", 16);
371 $parameters [
"deleteIn"] =
"(" . implode(
", ", $loanids) .
")";
375 if (HCU_array_key_exists(
"searchIn", $parameters)) {
376 $searchInData = GetSearchInData();
378 foreach($searchInData as $searchInRow) {
379 if ($searchInRow[
"value"] == $parameters[
"searchIn"]) {
385 throw new exception (
"Search in is invalid.", 1);
388 $parameters[
"searchIn"] =
"";
392 if ($parameters[
"searchIn"] !=
"" && HCU_array_key_exists(
"lookFor", $parameters)) {
393 $parameters[
"lookFor"] = trim($parameters[
"lookFor"]);
395 $parameters[
"lookFor"] =
"";
398 switch($parameters[
"searchIn"]) {
402 if (!validateEmail($parameters[
"lookFor"])) {
403 throw new exception (
"Email is invalid.", 3);
409 $parameters[
"lookFor"] = FilterDateForDatabase($parameters[
"lookFor"],
'm/d/Y');
410 if (empty($parameters[
"lookFor"])) {
411 throw new exception (
"Date is invalid.", 4);
415 if (!ValidateSSN($parameters[
"lookFor"])) {
416 throw new exception (
"SSN is invalid.", 5);
421 if (!ctype_digit(strval($parameters[
"lookFor"]))) {
422 throw new exception (
"search string needs to be a number.", 6);
428 if (HCU_array_key_exists(
"withStatus", $parameters)) {
429 $statusData = GetWithStatusData();
431 foreach($statusData as $statusRow) {
432 if ($statusRow[
"value"] == $parameters[
"withStatus"]) {
438 throw new exception (
"Status is invalid.", 2);
441 $parameters[
"withStatus"] =
"";
445 if (HCU_array_key_exists(
"rangeType", $parameters)) {
446 $rangeTypeData = GetRangeTypeData();
448 foreach($rangeTypeData as $rangeTypeRow) {
449 if ($rangeTypeRow[
"value"] == $parameters[
"rangeType"]) {
455 throw new exception (
"Range type is invalid.", 7);
458 $parameters[
"rangeType"] =
"";
462 $parameters[
"fromDate"] = $parameters[
"rangeType"] ==
"" ? false : HCU_array_key_value(
"fromDate", $parameters);
463 $parameters[
"fromDate"] = $parameters[
"fromDate"] ===
false ?
"" : trim($parameters[
"fromDate"]);
464 $parameters[
"toDate"] = $parameters[
"rangeType"] ==
"" ? false : HCU_array_key_value(
"toDate", $parameters);
465 $parameters[
"toDate"] = $parameters[
"toDate"] ===
false ?
"" : trim($parameters[
"toDate"]);
467 if ($parameters[
"fromDate"] ==
"") {
468 if ($parameters[
"rangeType"] !=
"") {
469 throw new exception (
"From date is required.", 8);
472 if (!validateDate($parameters[
"fromDate"])) {
473 throw new exception (
"From date is invalid.", 10);
475 $parameters[
"fromDate"] = ModifyDateForDatabase($parameters[
"fromDate"]);
478 if ($parameters[
"toDate"] ==
"") {
479 if ($parameters[
"rangeType"] !=
"") {
480 throw new exception (
"To date is required.", 8);
483 if (!validateDate($parameters[
"toDate"])) {
484 throw new exception (
"To date is invalid.", 12);
486 $parameters[
"toDate"] = ModifyDateForDatabase($parameters[
"toDate"]);
489 if ($parameters[
"fromDate"] > $parameters[
"toDate"]) {
490 throw new exception (
"To date cannot be before from date.", 11);
494 if (HCU_array_key_exists(
"sortBy", $parameters) && $parameters[
"sortBy"] !=
"") {
495 $sortByData = GetSortByData();
497 foreach($sortByData as $sortByRow) {
498 if ($sortByRow[
"value"] == $parameters[
"sortBy"]) {
504 throw new exception (
"Sort by is invalid.", 13);
507 $parameters[
"sortBy"] =
"startAD";
510 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"data" => $parameters);
511 }
catch (exception $e) {
512 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage());
527 function FilterDateForDatabase($date =
'', $requested_format =
'Y-m-d', $century = 0)
530 return $dc->ConvertDateFromFormat($date, $requested_format, $century);
542 function ModifyDateForDatabase($date) {
543 return substr($date, - 4) .
"-" . substr($date, 0, 2) .
"-" . substr($date, 3, 2);
555 function ValidateSSN($ssn) {
556 $pattern =
'/^\d{3}-\d{2}-\d{4}$/';
557 $result = preg_match($pattern, $ssn);
558 return $result === 1;
574 function ReadApplicantSearch($dbh, $Cu, $parameters) {
576 $results = _ValidateApplicantSearch($parameters);
577 if ($results[
"status"] !==
"000") {
578 throw new exception (
"Search is invalid.", 1);
580 extract($parameters);
582 $select =
"select ln.userid \"lnId\", ln.email \"email\", coalesce(ln.failedloginattempts, 0) \"failedAttempts\", 583 case when ln.userlogintype = 'H' then 'Home Banking' when ln.userlogintype = 'N' then 'Non-Home Banking Member' else 'Loan App User' end \"loginType\", 584 coalesce(trim(u.user_name), '<None>') \"username\", coalesce(trim(ln.session_account), '<None>') \"accountnumber\"";
586 $sql =
"from lnappuser ln 587 left join ${Cu}user u on ln.banking_user_id = u.user_id 588 where ln.cu = '" . prep_save($Cu, 10) .
"'";
590 if ($searchIn ===
"email") {
591 $sql .=
" and lower(trim(ln.email)) like '%" . strtolower(prep_save($lookFor, 50)) .
"%'";
592 }
else if ($searchIn ===
"account") {
593 $sql .=
" and trim(ln.session_account) like '%" . prep_save($lookFor) .
"%'";
594 }
else if ($searchIn ===
"username") {
595 $sql .=
" and lower(trim(u.user_name)) like '%" . strtolower(prep_save($lookFor)) .
"%'";
598 $sortSQL =
"order by ln.userid desc";
599 $limitSQL = $limit == 0 && $offset == 0 ?
"" :
"limit $limit offset $offset";
601 $countSQL =
"select count(*) $sql";
602 $mainSQL =
"$select $sql $sortSQL $limitSQL";
604 $sth = db_query($countSQL, $dbh);
606 throw new exception (
"count query failed.", 2);
608 $results = db_fetch_row($sth, 0);
609 $total = intval($results[0]);
611 $sth = db_query($mainSQL, $dbh);
613 throw new exception (
"main query failed.", 3);
615 $gridData = db_fetch_all($sth);
616 $gridData = $gridData ===
false ? array() : $gridData;
618 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"gridData" => array(
"total" => $total,
"data" => $gridData));
619 }
catch (exception $e) {
620 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"gridData" => array(
"total" => 0,
"data" => array()));
634 function _ValidateApplicantSearch($parameters) {
636 $searchIn = HCU_array_key_value(
"searchIn", $parameters);
637 $lookFor = HCU_array_key_value(
"lookFor", $parameters);
639 if ($searchIn ===
false || $lookFor ===
false) {
640 throw new exception (
"Search is missing parameters.", 1);
644 if (!validateEmail($lookFor)) {
645 throw new exception (
"Email is invalid.", 3);
649 if (!ctype_digit(strval($lookFor))) {
650 throw new exception (
"search string needs to be a number.", 4);
656 throw new exception (
"Search is invalid.", 2);
659 $returnArray = array(
"status" =>
"000",
"error" =>
"");
660 }
catch (exception $e) {
661 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage());
679 function ReadApplicant($dbh, $Cu, $parameters) {
681 $userId = HCU_array_key_value(
"userId", $parameters);
682 if ($userId ===
false) {
683 throw new exception (
"Userid is required.", 1);
686 $sql =
"select ln.userid \"lnId\", ln.email \"email\", coalesce(ln.failedloginattempts, 0) \"failedAttempts\", 687 case when ln.userlogintype = 'H' then 'Home Banking' when ln.userlogintype = 'N' then 'Non-Home Banking Member' else 'Loan App User' end \"loginType\", 688 coalesce(trim(u.user_name), '') \"username\", coalesce(trim(ln.session_account), '') \"accountnumber\", 689 coalesce(trim(ln.confidenceword), '<None>') \"confidence\" 691 left join ${Cu}user u on ln.banking_user_id = u.user_id 692 where ln.cu = '" . prep_save($Cu, 10) .
"' and ln.userid = " . intval($userId);
693 $sth = db_query($sql, $dbh);
695 throw new exception (
"query failed.", 5);
697 if (db_num_rows($sth) <= 0) {
698 throw new exception (
"User not found.", 2);
700 $applicantData = db_fetch_assoc($sth, 0);
702 $applicantData[
"isLocked"] = $applicantData[
"failedAttempts"] >= GetFailedLoginAttempts();
705 $applicantData[
"canChangeLogin"] = $applicantData[
"loginType"] !==
"Home Banking";
707 $sql =
"select a.user_answer \"answer\", m.quest_text \"question\" 708 from lnappuser_questselect a 709 inner join cuquestmaster m on a.questid = m.quest_id 710 where a.userid = " . intval($userId) .
" and m.quest_lang = 'en_US'";
711 $sth = db_query($sql, $dbh);
713 throw new exception (
"query failed.", 3);
715 $results = db_fetch_all($sth);
716 $results = $results ===
false ? array() : $results;
718 $applicantData[
"securityQuestions"] = $results;
720 $sql =
"select count(*) from lnappuserresponse where userid = " . intval($userId);
721 $sth = db_query($sql, $dbh);
723 throw new exception (
"query failed.", 4);
725 if (db_num_rows($sth) <= 0) {
726 throw new exception (
"query failed.", 6);
728 list ($numLoanApps) = db_fetch_row($sth, 0);
730 $applicantData[
"numLoanApps"] = $numLoanApps;
732 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"applicantData" => $applicantData,
"type" =>
"read");
733 }
catch (exception $e) {
734 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"type" =>
"read");
752 function SaveApplicant($dbh, $Cu, $parameters) {
754 $userId = HCU_array_key_value(
"userId", $parameters);
755 if ($userId ===
false) {
756 throw new exception (
"Userid is required.", 4);
759 $lockSetting = HCU_array_key_value(
"lockSetting", $parameters);
760 if ($lockSetting ===
false) {
761 throw new exception (
"Lock setting is required.", 3);
764 $email = HCU_array_key_value(
"email", $parameters);
765 if ($email ===
false) {
766 throw new exception (
"Email is required.", 5);
769 $password = HCU_array_key_value(
"password", $parameters);
770 if ($password ===
false) {
771 throw new exception (
"Password is required.", 6);
774 $confirm = HCU_array_key_value(
"confirm", $parameters);
775 if ($confirm ===
false) {
776 throw new exception (
"Confirm is required.", 7);
779 $sql =
"select ln.userid \"lnId\", lower(coalesce(trim(ln.email), '')) \"email\", coalesce(ln.failedloginattempts, 0) \"failedAttempts\" 781 where ln.userid = " . intval($userId);
782 $sth = db_query($sql, $dbh);
784 throw new exception (
"Query failed.", 1);
786 if (db_num_rows($sth) <= 0) {
787 throw new exception (
"User not found.", 2);
790 list($userId, $originalEmail, $failedAttempts) = db_fetch_row($sth, 0);
793 switch($lockSetting) {
797 $saveList[] =
"failedloginattempts = " . intval(GetFailedLoginAttempts());
800 $saveList[] =
"failedloginattempts = 0";
803 throw new exception (
"Lock setting is not valid.", 8);
807 $email = trim($email);
809 if ($originalEmail ==
"") {
810 throw new exception (
"Please set an email.", 9);
812 }
else if (strtolower($email) !== $originalEmail) {
813 if (!validateEmail($email)) {
814 throw new exception (
"Email is not valid.", 11);
817 $sql =
"select count(*) from lnappuser ln where lower(trim(ln.email)) = '" . strtolower(prep_save($email, 50)) .
"'";
818 $sth = db_query($sql, $dbh);
820 throw new exception (
"Select query failed.", 15);
822 if (db_num_rows($sth) <= 0) {
823 throw new exception (
"Select query failed.", 16);
825 list($dupCount) = db_fetch_row($sth, 0);
827 throw new exception (
"Email needs to be unique.", 17);
829 $saveList[] =
"email = '" . prep_save($email, 50) .
"'";
832 if ($password !==
"") {
833 if ($confirm !== $password) {
834 throw new exception (
"Passwords do not match.", 10);
837 if (strlen($password) < 6 || strlen($password) > 20) {
838 throw new exception (
"Password must be from 6 to 20 characters long.", 12);
841 if ( !(preg_match(
"/\d/",$password) && preg_match(
"/\D/",$password))) {
842 throw new exception (
"Password must contain both number and letter characters,", 13);
845 if (preg_match(
"/['\"]/", $password)) {
846 throw new exception (
"There are invalid characters in password.", 14);
849 $saveList[] =
"pwd = '" . prep_save(password_hash($password, PASSWORD_DEFAULT)) .
"'";
852 if (count($saveList) > 0) {
854 $sql =
"update lnappuser 855 set " . implode(
", ", $saveList) .
" 856 where userid = " . intval($userId) .
" and cu = '" . prep_save($Cu, 10) .
"'";
857 $sth = db_query($sql, $dbh);
859 throw new exception (
"Update query failed.", 18);
863 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"info" =>
"Applicant was updated successfully.",
"type" =>
"update");
864 }
catch (exception $e) {
865 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"type" =>
"update");