28 extract($POSTED[
"HCUPOST"]);
30 $returnMsgData = Array();
31 switch ($POSTED[
'HCUPOST'][
'action']) {
33 if (!isset($Cu) || !isset($trustid)) {
34 throw new Exception(
'Missing Expected Parameters');
36 # should only be one record, but just in case, limit 1 38 $where= array(
"providermode='$trustid'");
40 # should only be one record, but just in case, limit 1 43 $where[]=
"accountnumber='$member'";
46 if (isset($accounttype))
47 $where[]=
"accounttype='$accounttype'";
49 $where[]=
"user_id=" . intval($userid);
51 $sql =
"select user_id as userid, id as keyid, trim(accountnumber) as accountnumber, trim(accounttype) as accounttype, parms from {$Cu}extkey 52 where " . implode(
" and ", $where) . ($doLimit ?
" limit 1" :
"");
54 $sqlRs = db_query($sql, $dbh);
56 throw new Exception(
"SQL failed ($sql).");
60 while ($dRecord = db_fetch_assoc($sqlRs, $sIdx)) {
67 $payload = $iClass->parms_parse($dRecord[
'parms']); #array(
'dmiloan' => $dmiloan);
68 $returnMsgData[] = $dRecord + $payload;
72 $retStatus_ary[
'homecuData'] = $returnMsgData;
75 $sql=
"select u.user_id, row_number() over(order by lb.accountnumber, lb.loannumber) as rown, lb.accountnumber, lb.loannumber, coalesce(ek.id, 0) as extkeyid from ${Cu}user u 76 inner join ${Cu}loanbalance lb on u.primary_account = lb.accountnumber and lb.loannumber not like '%@%' 77 left join ${Cu}extkey ek on lb.accountnumber = ek.accountnumber and lb.loannumber = ek.accounttype and ek.providermode = '" . prep_save($POSTED[
'HCUPOST'][
'trustid'], 20) .
"'";
79 $sql.=
"where u.user_id=" . intval($userid);
81 $sth= db_query($sql, $dbh);
83 throw new exception(
"DDL query failed!", 1);
84 $loanRecords= array();
85 $accountRecords= array();
86 for($i=0; $record= db_fetch_assoc($sth, $i); $i++)
88 $accountnumber= trim($record[
"accountnumber"]);
89 if (!isset($accountRecords[$accountnumber]))
90 $accountRecords[$accountnumber]= array(
"accountnumber" => $accountnumber);
91 if (!isset($loanRecords[$accountnumber]))
92 $loanRecords[$accountnumber]= array();
93 $loanRecords[$accountnumber][]= array(
"loannumber" => trim($record[
"loannumber"]),
"extkeyid" => intval($record[
"extkeyid"]),
"userid" => $record[
"user_id"]);
95 $retStatus_ary[
"loanData"] = $loanRecords;
96 $retStatus_ary[
"accountData"]= array_values($accountRecords);
106 if (!isset($Cu) || !isset($trustid) || !isset($accountnumber) || !isset($accounttype)) {
108 throw new Exception(
'Missing Expected Parameters');
115 $sql =
"SELECT count(*) as count_rec 116 FROM ${Cu}user where primary_account = '" . prep_save($POSTED[
'HCUPOST'][
'accountnumber'], 12) .
"'";
117 $cntRs = db_query($sql, $dbh);
118 list($recordCount) = db_fetch_array($cntRs);
119 if ($recordCount == 0) {
120 throw new Exception(
'Unable to save entry, Invalid Member Number.');
123 $sql =
"SELECT count(*) as count_rec 124 FROM {$Cu}loanbalance 125 WHERE accountnumber = '" . prep_save($POSTED[
'HCUPOST'][
'accountnumber'], 12) .
"' 126 AND loannumber = '" . prep_save($POSTED[
'HCUPOST'][
'accounttype'], 25) .
"'";
127 $cntRs = db_query($sql, $dbh);
128 list($recordCount) = db_fetch_array($cntRs);
129 if ($recordCount == 0) {
130 throw new Exception(
'Unable to save entry, Invalid HCU Loan Number.');
134 $sql =
"SELECT count(*) as count_rec 136 WHERE accountnumber = '" . prep_save($POSTED[
'HCUPOST'][
'accountnumber'], 12) .
"' 137 AND accounttype = '" . prep_save($POSTED[
'HCUPOST'][
'accounttype'], 25) .
"' 138 AND providermode = '" . prep_save($POSTED[
'HCUPOST'][
'trustid'], 20) .
"'";
139 $cntRs = db_query($sql, $dbh);
140 list($recordCount) = db_fetch_array($cntRs);
141 if ($recordCount > 0) {
142 throw new Exception(
'Unable to save entry, Only one entry per Member / HCU Loan allowed.');
145 $payload = $iClass->parms_validate($POSTED[
'HCUPOST']);
146 if (is_array($payload[errors])) {
147 throw new Exception(json_encode($payload[errors]));
150 $sql =
"INSERT INTO {$Cu}extkey (accountnumber, user_id, providermode, accounttype, parms) 152 '" . prep_save($POSTED[
'HCUPOST'][
'accountnumber'], 12) .
"', 153 '" . prep_save($POSTED[
'HCUPOST'][
'userid']) .
"', 154 '" . prep_save($POSTED[
'HCUPOST'][
'trustid'], 20) .
"', 155 '" . prep_save($POSTED[
'HCUPOST'][
'accounttype'], 25) .
"', 156 '" . prep_save($payload[
'data']) .
"'); 157 select currval('${Cu}extkey_id_seq')";
158 $updRs = db_query($sql, $dbh);
162 throw new Exception(
'A problem occurred, unable to save entry.');
164 list($keyid) = db_fetch_array($updRs,0);
166 $retStatus_ary[
'homecuInfo'][] =
"Entry successfully saved.";
167 $retStatus_ary[
'homecuData'] = $iClass->parms_parse($payload[
'data']);
168 $retStatus_ary[
'homecuData'][
'keyid'] = $keyid;
169 $retStatus_ary[
'homecuData'][
'accountnumber'] = $POSTED[
'HCUPOST'][
'accountnumber'];
170 $retStatus_ary[
'homecuData'][
'accounttype'] = $POSTED[
'HCUPOST'][
'accounttype'];
181 if (!isset($Cu) || !isset($trustid) || !isset($accountnumber) || !isset($accounttype)) {
183 throw new Exception(
'Missing Expected Parameters');
189 $sql =
"SELECT count(*) as count_rec 190 FROM ${Cu}user where primary_account = '" . prep_save($POSTED[
'HCUPOST'][
'accountnumber'], 12) .
"'";
191 $cntRs = db_query($sql, $dbh);
192 list($recordCount) = db_fetch_array($cntRs);
193 if ($recordCount == 0) {
194 throw new Exception(
'Update failed, Invalid Member Number.');
197 $sql =
"SELECT count(*) as count_rec 198 FROM {$Cu}loanbalance 199 WHERE accountnumber = '" . prep_save($POSTED[
'HCUPOST'][
'accountnumber'], 12) .
"' 200 AND loannumber = '" . prep_save($POSTED[
'HCUPOST'][
'accounttype'], 25) .
"'";
201 $cntRs = db_query($sql, $dbh);
202 list($recordCount) = db_fetch_array($cntRs);
203 if ($recordCount == 0) {
204 throw new Exception(
'Update failed, Invalid HCU Loan Number.');
208 $sql =
"SELECT count(*) as count_rec 210 WHERE accountnumber = '" . prep_save($POSTED[
'HCUPOST'][
'accountnumber'], 12) .
"' 211 AND accounttype = '" . prep_save($POSTED[
'HCUPOST'][
'accounttype'], 25) .
"' 212 AND providermode = '" . prep_save($POSTED[
'HCUPOST'][
'trustid'], 20) .
"' 213 AND id <> {$POSTED['HCUPOST']['keyid']}";
214 $cntRs = db_query($sql, $dbh);
215 list($recordCount) = db_fetch_array($cntRs);
216 if ($recordCount > 0) {
217 throw new Exception(
'Update failed, Only one entry per Member / HCU Loan allowed.');
220 $payload = $iClass->parms_validate($POSTED[
'HCUPOST']);
221 if (is_array($payload[errors])) {
222 throw new Exception(implode(
"<br>\n",$payload[errors]));
225 $sql =
"UPDATE {$Cu}extkey SET accountnumber = '" . prep_save($POSTED[
'HCUPOST'][
'accountnumber'], 12) .
"', 226 providermode = '" . prep_save($POSTED[
'HCUPOST'][
'trustid'], 20) .
"', 227 accounttype = '" . prep_save($POSTED[
'HCUPOST'][
'accounttype'], 25) .
"', 228 parms='" . prep_save($payload[
'data']) .
"' 229 WHERE id = {$POSTED['HCUPOST']['keyid']}";
230 $updRs = db_query($sql, $dbh);
233 throw new Exception(
'A problem occurred, update failed.');
234 } elseif (db_affected_rows($updRs) != 1) {
235 throw new Exception(
'Record not found.');
238 $retStatus_ary[
'homecuInfo'][] =
"Entry successfully updated.";
243 $sql =
"DELETE FROM {$Cu}extkey 244 WHERE id = {$POSTED['HCUPOST']['keyid']}";
246 $updRs = db_query($sql, $dbh);
249 throw new Exception(
'A problem occurred. delete failed.');
250 } elseif (db_affected_rows($updRs) != 1) {
251 throw new Exception(
'Record not found. delete failed.');
254 $retStatus_ary[
'homecuInfo'][] =
"Entry successfully deleted.";
260 throw new Exception(
"Unexpected action: {$action}. Action cancelled.");
264 }
catch (Exception $ex) {
265 $exerror = $ex->getMessage();
266 $retStatus_ary[
'homecuErrors'][] = $ex->getMessage();
268 $retStatus_ary[
"type"]= $POSTED[
'HCUPOST'][
'action'];
271 header(
'Content-type: application/json');
273 print json_encode(Array(
"Results" => Array($retStatus_ary)));