15 function DeleteSubAccounts($pEnv, $accountnumber, $subaccounts) {
16 $showAddNewAccount =
false;
20 $userId = $pEnv[
"userId"];
23 $allowZeroes = $pEnv[
"allowZeroes"];
24 $disallowMultipleAccounts = $pEnv[
"disallowMultipleAccounts"];
28 $accountnumber = preg_replace(
'/^0+/',
'', trim($accountnumber));
31 if ($subaccounts ==
"") {
32 throw new exception(
"Subaccounts are required.", 1);
34 $subaccounts = hcu_JsonDecode($subaccounts);
35 if (!is_array($subaccounts)) {
36 throw new exception(
"Subaccounts are required.", 2);
38 $forDeletion = array();
39 foreach($subaccounts as $subaccount) {
40 if (!HCU_array_key_exists(
"accounttype", $subaccount) || !HCU_array_key_exists(
"certnumber", $subaccount)
41 || !HCU_array_key_exists(
"recordtype", $subaccount)) {
42 throw new exception(
"Subaccounts are malformed.", 3);
44 $subaccount[
"_action"] =
"delete";
45 $subaccount[
"user_id"] = $userId;
46 $subaccount[
"accountnumber"] = $accountnumber;
47 $accountDelete[] = $subaccount;
50 $datatable =
"useraccounts";
51 $forDeletion = array($datatable => $accountDelete);
53 $sql =
"select email from cuadminusers 54 where user_name = '" . prep_save($Cn, 50) .
"' 55 and cu = '" . prep_save($Cu, 10) .
"'";
56 $sth = db_query($sql, $dbh);
58 throw new exception(
"email query failed.", 4);
60 $email = db_fetch_row($sth)[0];
63 $script =
"userSupportAccessControl.prg";
64 $addr = trim($_SERVER[
"REMOTE_ADDR"]);
65 $vars = array(
"cu" => $Cu);
67 if (!db_work ($dbh, HOMECU_WORK_BEGIN)) {
68 throw new exception(
"begin query failed.", 6);
72 $results = DataUserTableUpdate($dbh, $vars,
null, $forDeletion, $userId,
"ACCESS_DEL", $context,
73 $script,
"A",
"Primary User Access Delete", $Cn, $email, $addr);
74 if ($results ===
false) {
75 throw new exception(
"Deleting from useraccounts table failed.", 105);
80 $forDeletion = array();
83 $datatable =
"cu_alerts";
85 foreach($accountDelete as $subKey => $subArray) {
86 if (HCU_array_key_exists(
'recordtype', $subArray)) {
87 unset($accountDelete[$subKey][
'recordtype']);
89 $accountType = HCU_array_key_value(
'accounttype', $accountDelete[$subKey]);
90 $certNumber = HCU_array_key_value(
'certnumber', $accountDelete[$subKey]);
92 $alertSql =
"select id 93 from cu_alerts where user_id = '" . prep_save(intval($userId)) .
"' 94 AND accountnumber = '" . prep_save($accountnumber, 12) .
"' 95 AND accounttype = '" . prep_save($accountType, 25) .
"' 96 AND certnumber = " . prep_save(intval($certNumber)) .
" ";
97 $sth = db_query($alertSql, $dbh);
100 throw new exception(
"cu_alerts query failed.", 107);
102 if (db_num_rows($sth) > 0) {
103 while ($result = db_fetch_row($sth)) {
104 $alerts[
'id'] = $result[0];
105 $alerts[
"_action"] =
"delete";
106 $alertDelete[] = $alerts;
111 if (isset($alertDelete)) {
112 $forDeletion = array($datatable => $alertDelete);
113 $results = DataUserTableUpdate($dbh, $vars,
null, $forDeletion, $userId,
"ACCESS_DEL", $context,
114 $script,
"A",
"Primary User Access Delete", $Cn, $email, $addr);
115 if ($results ===
false) {
116 throw new exception(
"Deleting from alerts table failed.", 105);
121 $forDeletion = array();
123 $datatable =
"cu_scheduledtxn";
126 foreach($accountDelete as $subKey => $subArray) {
127 $accountType = HCU_array_key_value(
'accounttype', $accountDelete[$subKey]);
129 $schedSql =
"select id, txn_data from cu_scheduledtxn 130 where user_id = '" . prep_save(intval($userId)) .
"' 131 AND CU = '" . prep_save($Cu, 10) .
"' ";
132 $sth = db_query($schedSql, $dbh);
135 throw new exception(
"cu_scheduledtxn query failed.", 107);
138 if (db_num_rows($sth) > 0) {
139 while ($result = db_fetch_assoc($sth, $row++)) {
140 $txnData = HCU_JsonDecode( $result[
"txn_data"],
false );
141 if ($txnData[
'txn'][
'frommember'] == $accountnumber && $txnData[
'txn'][
'fromsuffix'] == $accountType ) {
142 $sched[
'id'] = $result[
'id'];
143 $sched[
"_action"] =
"delete";
144 $schedDelete[] = $sched;
150 if (isset($schedDelete)) {
151 $forDeletion = array($datatable => $schedDelete);
152 $results = DataUserTableUpdate($dbh, $vars,
null, $forDeletion, $userId,
"ACCESS_DEL", $context, $script,
153 "A",
"Primary User Access Delete", $Cn, $email, $addr);
154 if ($results ===
false) {
155 throw new exception(
"Deleting from scheduledtxn table failed.", 105);
159 $sql =
"select 'FOUND' from ${Cu}useraccounts 160 where user_id = " . prep_save(intval($userId)) .
" 161 and accountnumber = '" . prep_save($accountnumber, 12) .
"'";
162 $sth = db_query($sql, $dbh);
165 throw new exception(
"found query failed.", 107);
168 if (db_num_rows($sth) == 0) {
170 if ($disallowMultipleAccounts) {
172 $sql =
"select 'FOUND' from ${Cu}useraccounts where user_id = " . prep_save(intval($userId));
173 $sth = db_query($sql, $dbh);
176 throw new exception(
"found query failed.", 110);
179 $showAddNewAccount = db_num_rows($sth) == 0;
181 $sql =
"delete from ${Cu}memberacctrights 182 where user_id = " . prep_save(intval($userId)) .
" 183 and accountnumber = '" . prep_save($accountnumber, 12) .
"'";
184 $sth = db_query($sql, $dbh);
186 throw new exception(
"acct rights delete failed.", 108);
190 if (!db_work($dbh, HOMECU_WORK_COMMIT)) {
191 throw new exception(
"commit work failed.", 109);
194 }
catch(exception $e) {
195 if ($e->getCode() >= 100) {
196 db_work($dbh, HOMECU_WORK_ROLLBACK);
199 return array(
"error" => array($e->getMessage()),
"code" => $e->getCode());
201 return array(
"error" => array(),
"code" => 0,
"showAddNewAccount" => $showAddNewAccount);
217 function GetPrimaryMemberAccounts($dbh, $Cu, $userId, $accountnumber) {
219 $subquery =
"select x.certnumber || trim(recordtype) || trim(x.accountnumber) || trim(x.accounttype) as key, trim(x.accountnumber), 220 trim(x.accounttype), x.certnumber, x.display_name, x.recordtype as type 221 from ${Cu}useraccounts x 223 select b.user_id from ${Cu}user a 224 inner join ${Cu}user b on a.user_id = $userId and a.group_id = b.group_id and b.is_group_primary limit 1) y 225 on x.user_id = y.user_id where trim(x.accountnumber) = '" . prep_save($accountnumber, 12) .
"'";
227 $sql =
"select ua.accountnumber, ua.accounttype, ua.certnumber, ab.deposittype, ua.type, coalesce(ab.description, ua.display_name) as description, 228 ab.may_deposit, ab.may_withdraw 230 select certnumber || 'D' || trim(accountnumber) || trim(accounttype) as key, 231 trim(description), may_deposit, may_withdraw, '' as deposittype, 'D' as type, trim(accounttype) 232 from ${Cu}accountbalance where accountnumber = '" . prep_save($accountnumber, 12) .
"' 234 select '0' || 'T' || trim(accountnumber) || trim(accounttype) || '#' || trim(tomember) as key, 235 trim(description), true, false, deposittype, 'T', trim(accounttype) 236 from ${Cu}crossaccounts where accountnumber = '" . prep_save($accountnumber, 12) .
"' and deposittype <> 'L' 238 select '0' || 'L' || trim(accountnumber) || trim(loannumber) as key, 239 trim(description), may_payment, may_addon, '', 'L', trim(loannumber) 240 from ${Cu}loanbalance where accountnumber = '" . prep_save($accountnumber, 12) .
"' 242 select '0' || 'P' || trim(accountnumber) || trim(accounttype) || '#' || trim(tomember) as key, 243 trim(description), false, false, deposittype, 'P', trim(accounttype) 244 from ${Cu}crossaccounts where accountnumber = '" . prep_save($accountnumber, 12) .
"' and deposittype = 'L' 245 ) as ab(key, description, may_deposit, may_withdraw, deposittype, type, accounttype) 246 right join ($subquery and recordtype in ('D', 'T', 'L', 'P')) as ua(key, accountnumber, accounttype, certnumber, display_name) 247 on ab.key = ua.key order by ab.accounttype";
248 $sth = db_query($sql, $dbh);
251 throw new exception(
"Deposit query failed.", 1);
254 $depositList = array();
257 for($i = 0; $row = db_fetch_assoc($sth, $i); $i++) {
259 $row[
"may_deposit"] = HCU_array_key_exists(
"may_deposit", $row) && isset($row[
"may_deposit"]) ? trim($row[
"may_deposit"]) ==
"t" :
true;
260 $row[
"may_withdraw"] = HCU_array_key_exists(
"may_withdraw", $row) && isset($row[
"may_withdraw"]) ? trim($row[
"may_withdraw"]) ==
"t" :
true;
262 switch($row[
"type"]) {
264 $depositList[] = $row;
267 $row [
"loannumber"] = $row [
"accounttype"];
272 $explode = explode(
"#", $row[
"accounttype"]);
273 $row[
"accounttype"] = $explode[0];
274 $row[
"tomember"] = $explode[1];
279 }
catch(exception $e) {
280 return array(
"code" =>
"666");
282 return array(
"code" =>
"000",
"data" => array(
"accounts" => array(
"deposit" => $depositList,
"loan" => $loanList,
"xa" => $xacList)));
298 function GetAccountFromCore($SYSENV, $accountnumber, $isPrimary) {
300 $live = $SYSENV[
"live"];
301 $logger = $SYSENV[
"logger"];
302 $dbh = $SYSENV[
"dbh"];
304 $allowZeroes = $SYSENV[
"allowZeroes"];
305 $userId = $SYSENV[
"userId"];
311 $accountnumber = preg_replace(
'/^0+/',
'', trim($accountnumber));
316 if ( $live ==
"Y" ) {
317 $SYSENV[
'SYSENV'][
'logger'] = $logger;
318 $SYSENV[
'SYSENV'][
'devmode'] = $SYSENV[
'devmode'];
319 $fullData = $isPrimary ? FindMemberAccounts($SYSENV, array(
"member" => $accountnumber,
"email" =>
"")) :
320 GetPrimaryMemberAccounts($dbh, $Cu, $userId, $accountnumber);
322 if ($fullData[
"code"] ==
"001" || $fullData[
"code"] ==
"999") {
323 $fullData = $isPrimary ? FindMemberAccountsWoMIR($dbh, $Cu, $accountnumber) : GetPrimaryMemberAccounts($dbh, $Cu, $userId, $accountnumber);
326 $fullData = $isPrimary ? SpoofFindMemberAccounts($dbh, $Cu, $accountnumber) : GetPrimaryMemberAccounts($dbh, $Cu, $userId, $accountnumber);
327 if ($fullData[
"code"] ==
"001" || $fullData[
"code"] ==
"999") {
328 $fullData = $isPrimary ? FindMemberAccountsWoMIR($dbh, $Cu, $accountnumber) : GetPrimaryMemberAccounts($dbh, $Cu, $userId, $accountnumber);
332 switch ($fullData[
"code"]) {
334 throw new exception(
"No data was found.", 1);
337 throw new exception($fullData[
"error"], 2);
345 throw new exception(
"There was an internal problem.", 3);
354 if ( isset($fullData[
"data"][
"mir"]) ) {
355 $accountInfo = $fullData[
"data"][
"mir"];
357 $homePhone = !HCU_array_key_exists(
"homephone", $accountInfo) ?
"" : trim($accountInfo[
"homephone"]);
358 $cellPhone = !HCU_array_key_exists(
"cellphone", $accountInfo) ?
"" : trim($accountInfo[
"cellphone"]);
359 $first = !HCU_array_key_exists(
"firstname", $accountInfo) ?
"" : trim($accountInfo[
"firstname"]);
360 $sec = !HCU_array_key_exists(
"middlename", $accountInfo) ?
"" : trim($accountInfo[
"middlename"]);
361 $third = !HCU_array_key_exists(
"lastname", $accountInfo) ?
"" : trim($accountInfo[
"lastname"]);
362 $dob = !HCU_array_key_exists(
"dob", $accountInfo) ?
"" : trim($accountInfo[
"dob"]);
365 if ($dob !=
"" && !DateTime::createFromFormat(
"m/d/Y", $dob)) {
367 throw new exception(
"DOB is invalid.", 5);
370 $theAccountInfo = array(array(
"accountnumber" => $accountnumber,
"name" => trim(
"$first $sec $third"),
"birthday" => $dob,
371 "primaryPhone" => $homePhone !=
"" ? $homePhone : $cellPhone));
373 $theAccountInfo = array(array(
"accountnumber" => $accountnumber,
"name" =>
"",
"birthday" =>
"",
374 "primaryPhone" =>
""));
378 $theAccountInfo =
"";
382 $theSubaccountInfo = array();
386 $alreadyExistsCheck = array();
388 $subaccountInfo = $fullData[
"data"][
"accounts"];
389 if (HCU_array_key_exists(
"deposit", $subaccountInfo)) {
390 foreach($subaccountInfo[
"deposit"] as $thisRow) {
391 $thisRow[
"kendoId"] = $kendoId;
392 $thisRow[
"recordtype"] =
"D";
393 $thisRow[
"isDeposit"] =
true;
395 $canDeposit = !HCU_array_key_exists(
"may_deposit", $thisRow) ? false : $thisRow[
"may_deposit"];
396 $canWithdraw = !HCU_array_key_exists(
"may_withdraw", $thisRow) ? false : $thisRow[
"may_withdraw"];
397 $thisRow[
"canIntDeposit"] = $canDeposit;
398 $thisRow[
"canExtDeposit"] = $canDeposit;
399 $thisRow[
"canIntWithdraw"] = $canWithdraw;
400 $thisRow[
"canExtWithdraw"] = $canWithdraw;
403 $strposAt = strpos($thisRow[
"accounttype"],
"@");
405 $permAccount =
"000";
406 if ($strposAt !==
false) {
408 $permAccount = substr($thisRow[
"accounttype"], $strposAt + 1);
410 $thisRow[
"perm_account_sort"] = $permAccount;
411 $thisRow[
"perm_type_sort"] = $permType;
413 unset($thisRow[
"may_deposit"]);
414 unset($thisRow[
"may_withdraw"]);
415 unset($thisRow[
"may_payment"]);
416 unset($thisRow[
"deposittype"]);
417 $theSubaccountInfo[] = $thisRow;
419 $alreadyExistsCheck[] =
"('" . prep_save($thisRow[
"accounttype"], 25) .
"', 420 " . prep_save(intval($thisRow[
"certnumber"])) .
", 'D', $kendoId)";
424 if (HCU_array_key_exists(
"loan", $subaccountInfo)) {
425 foreach($subaccountInfo[
"loan"] as $thisRow) {
426 $thisRow[
"kendoId"] = $kendoId;
427 $thisRow[
"recordtype"] =
"L";
428 $thisRow[
"isDeposit"] =
false;
429 $thisRow[
"accounttype"] = $thisRow[
"loannumber"];
430 $thisRow[
"certnumber"] = 0;
432 $canDeposit = !HCU_array_key_exists(
"may_payment", $thisRow) ? false : $thisRow[
"may_payment"];
433 $canWithdraw = !HCU_array_key_exists(
"may_addon", $thisRow) ? false : $thisRow[
"may_addon"];
434 $thisRow[
"canIntDeposit"] = $canDeposit;
435 $thisRow[
"canExtDeposit"] = $canDeposit;
436 $thisRow[
"canIntWithdraw"] = $canWithdraw;
437 $thisRow[
"canExtWithdraw"] = $canWithdraw;
440 $strposAt = strpos($thisRow[
"accounttype"],
"@");
442 $permAccount =
"000";
443 if ($strposAt !==
false) {
445 $permAccount = substr($thisRow[
"accounttype"], $strposAt + 1);
447 $thisRow[
"perm_account_sort"] = $permAccount;
448 $thisRow[
"perm_type_sort"] = $permType;
450 unset($thisRow[
"may_payment"]);
451 unset($thisRow[
"deposittype"]);
452 unset($thisRow[
"may_addon"]);
453 unset($thisRow[
"loannumber"]);
454 $theSubaccountInfo[] = $thisRow;
456 $alreadyExistsCheck[] =
"('" . prep_save($thisRow[
"accounttype"], 25) .
"', 457 " . prep_save(intval($thisRow[
"certnumber"])) .
", 'L', $kendoId)";
466 if (HCU_array_key_exists(
"xa", $subaccountInfo) && (GetFlagsetValue(
"CU3_CREATE_ACCESS_CONTROL_FROM_XAC") & $flagset3) == 0 ) {
467 foreach($subaccountInfo[
"xa"] as $thisRow) {
468 $isDeposit = $thisRow[
"deposittype"] !==
"L";
471 $thisRow[
"kendoId"] = $kendoId;
472 $thisRow[
"isDeposit"] = $isDeposit;
473 $thisRow[
"canIntDeposit"] =
true;
474 $thisRow[
"canExtDeposit"] =
false;
475 $thisRow[
"canIntWithdraw"] =
false;
476 $thisRow[
"canExtWithdraw"] =
false;
478 $thisRow[
"certnumber"] = 0;
479 $thisRow[
"accounttype"] .=
"#" . $thisRow[
"tomember"];
480 $thisRow[
"recordtype"] = $isDeposit ?
"T" :
"P";
483 $thisRow[
"perm_account_sort"] = $thisRow[
"tomember"];
484 $thisRow[
"perm_type_sort"] = 2;
486 $theSubaccountInfo[] = $thisRow;
488 $alreadyExistsCheck[] =
"('" . prep_save($thisRow[
"accounttype"], 25) .
"', 489 " . intval($thisRow[
"certnumber"]) .
", '" . $thisRow[
"recordtype"] .
"', $kendoId)";
494 if (count($alreadyExistsCheck) > 0) {
495 $weedOutThese = array();
496 $sql =
"select t.kendoid from ${Cu}useraccounts ua 497 inner join (values " . implode(
",", $alreadyExistsCheck) .
") as t(accounttype, certnumber, recordtype, kendoid) 498 on ua.accounttype = t.accounttype and ua.certnumber = t.certnumber and ua.recordtype = t.recordtype 499 and ua.user_id = " . prep_save(intval($userId)) .
" and ua.accountnumber = '" . prep_save($accountnumber, 12) .
"'";
500 $sth = db_query($sql, $dbh);
503 throw new exception(
"check query failed.", 4);
505 for($i = 0; $row = db_fetch_row($sth, $i); $i++) {
506 $weedOutThese[] = intval($row[0]);
509 $theOfficialSubaccountInfo = array();
510 foreach($theSubaccountInfo as $info) {
511 if (!in_array($info[
"kendoId"], $weedOutThese)) {
512 $theOfficialSubaccountInfo[] = $info;
516 $theOfficialSubaccountInfo = $theSubaccountInfo;
519 }
catch(exception $e) {
520 return array(
"error" => array($e->getMessage()),
"code" => $e->getCode());
522 return array(
"error" => array(),
"code" => 0,
"accountInfo" => $theAccountInfo,
"subaccountInfo" => $theOfficialSubaccountInfo);
546 function CreateSubaccounts($pEnv, $subaccounts, $accountnumber, $latestKendoid) {
547 $dontShowAddNewAccount =
false;
551 $allowZeroes = $pEnv[
"allowZeroes"];
553 $userId = $pEnv[
"userId"];
556 $disallowMultipleAccounts = $pEnv[
"disallowMultipleAccounts"];
560 $accountnumber = preg_replace(
'/^0+/',
'', trim($accountnumber));
562 $accountnumber = trim($accountnumber);
565 if (preg_match(
'/\D/', $accountnumber) === 1) {
566 throw new exception(
"Account number must be a number.", 28);
568 if ($subaccounts ==
"") {
569 throw new exception(
"No subaccounts found.", 1);
571 $subaccounts = hcu_JsonDecode($subaccounts);
572 if (!is_array($subaccounts) || count($subaccounts) == 0) {
573 throw new exception(
"No subaccounts found.", 2);
580 $sql =
"select u.is_group_primary, ma.accountnumber is null, mar.user_id is null 582 left join ${Cu}memberacct ma on trim(ma.accountnumber) = '" . prep_save($accountnumber, 12) .
"' 583 left join ${Cu}memberacctrights mar on u.user_id = mar.user_id and mar.whichright = 'ACCESS' 584 and trim(mar.accountnumber) = '" . prep_save($accountnumber, 12) .
"' 585 where u.user_id = " . prep_save(intval($userId));
586 $sth = db_query($sql, $dbh);
588 throw new exception(
"Primary check query failed.", 8);
590 $row = db_fetch_row($sth);
591 $isPrimaryUser = trim($row[0]) ==
"t";
592 $createMa = $isPrimaryUser && trim($row[1]) ==
"t";
593 $createAccess = $isPrimaryUser && trim($row[2]) ==
"t";
595 if ($disallowMultipleAccounts) {
596 $sql =
"select accountnumber, count(*) as count 597 from ${Cu}useraccounts 598 where user_id = " . prep_save(intval($userId)) .
" 599 group by accountnumber";
600 $sth = db_query($sql, $dbh);
602 throw new exception(
"Found query failed.", 29);
604 $results = db_fetch_all($sth);
605 $hasThisAccount =
false;
606 $hasOtherAccounts =
false;
607 if ($results !==
false) {
608 foreach($results as $result) {
609 trim($result[
"accountnumber"]) == $accountnumber ? $hasThisAccount = $hasThisAccount || $result[
"count"] > 0
610 : $hasOtherAccounts = $hasOtherAccounts || $result[
"count"] > 0;
614 if ($hasOtherAccounts) {
615 throw new exception (
"Cannot add multiple accounts.", 30);
622 $sql =
"select max(display_order) from {$Cu}useraccounts where user_id = 1";
623 $sth = db_query($sql, $dbh);
625 throw new exception(
"display order query failed.", 9);
627 $row = db_fetch_row($sth);
628 $displayOrder = intval($row[0]);
630 $createRecords = array();
631 $memberRecords = array();
632 $newSubaccounts = array();
633 $kendoid = $latestKendoid;
634 foreach($subaccounts as $subaccount) {
637 if ($displayOrder > 0) {
641 if (!HCU_array_key_exists(
"recordtype", $subaccount) || !HCU_array_key_exists(
"accounttype", $subaccount)
642 || !HCU_array_key_exists(
"certnumber", $subaccount)) {
643 throw new exception(
"Missing a required field.", 4);
646 if (!in_array($subaccount[
"recordtype"], array(
"D",
"L",
"T",
"P"))) {
647 throw new exception(
"Recordtype is unrecognized.", 13);
650 $isCrossAccount = in_array($subaccount[
"recordtype"], array(
"T",
"P"));
652 $dontIntRestrictDeposit = HCU_array_key_exists(
"canIntDeposit", $subaccount) ? $subaccount[
"canIntDeposit"] : ($isPrimaryUser ? false :
true);
653 $dontExtRestrictDeposit = HCU_array_key_exists(
"canExtDeposit", $subaccount) ? $subaccount[
"canExtDeposit"] : ($isPrimaryUser ? false :
true);
654 $dontIntRestrictWithdraw = HCU_array_key_exists(
"canExtWithdraw", $subaccount) ? $subaccount[
"canIntWithdraw"] : ($isPrimaryUser ? false :
true);
655 $dontExtRestrictWithdraw = HCU_array_key_exists(
"canExtWithdraw", $subaccount) ? $subaccount[
"canExtWithdraw"] : ($isPrimaryUser ? false :
true);
656 $canIntDeposit = $isPrimaryUser ? $dontIntRestrictDeposit :
false;
657 $canExtDeposit = $isPrimaryUser ? $dontExtRestrictDeposit :
false;
658 $canIntWithdraw = $isPrimaryUser ? $dontIntRestrictWithdraw :
false;
659 $canExtWithdraw = $isPrimaryUser ? $dontExtRestrictWithdraw :
false;
661 $canView = $isPrimaryUser && !$isCrossAccount;
664 if ($subaccount[
"recordtype"] ==
'L') {
665 $canExtWithdraw =
false;
666 $dontExtRestrictWithdraw =
false;
670 $createRecords[] = array(
671 "_action" =>
"create",
"user_id" => $userId,
672 "accountnumber" => $accountnumber,
"accounttype" => $subaccount[
"accounttype"],
673 "certnumber" => intval($subaccount[
"certnumber"]),
"display_name" =>
"",
674 "ext_deposit" => $canExtDeposit,
"ext_withdraw" => $canExtWithdraw,
675 "int_deposit" => $canIntDeposit,
"int_withdraw" => $canIntWithdraw,
676 "view_transactions" => $canView,
"view_balances" => $canView,
677 "recordtype" => $subaccount[
"recordtype"],
678 "display_order" => $displayOrder);
681 $permAccount =
"000";
683 $strposAt = strpos($subaccount[
"accounttype"],
"@");
684 $strposHash = strpos($subaccount[
"accounttype"],
"#");
685 if ( $strposAt !==
false) {
686 $permAccount = substr($subaccount[
"accounttype"], $strposAt + 1);
688 }
else if ( $strposHash !==
false) {
689 $permAccount = substr($subaccount[
"accounttype"], $strposHash + 1);
694 $newSubaccounts[] = array(
695 "accountnumber" => $accountnumber,
696 "accounttype" => $subaccount[
"accounttype"],
697 "certnumber" => intval($subaccount[
"certnumber"]),
698 "display_name" =>
"",
699 "description" => $subaccount[
"description"],
700 "ext_deposit" => $canExtDeposit,
701 "ext_withdraw" => $canExtWithdraw,
702 "restrictExtDeposit" => !$dontExtRestrictDeposit,
703 "restrictIntDeposit" => !$dontIntRestrictDeposit,
704 "int_deposit" => $canIntDeposit,
705 "int_withdraw" => $canIntWithdraw,
706 "restrictExtWithdraw" => !$dontExtRestrictWithdraw,
707 "restrictIntWithdraw" => !$dontIntRestrictWithdraw,
708 "view_transactions" => $canView,
709 "view_balances" => $canView,
710 "restrictViewBalances" => $isCrossAccount,
711 "restrictViewTransactions" => $isCrossAccount || !$canView,
712 "recordtype" => $subaccount[
"recordtype"],
713 "recordTypeFilter" => in_array($subaccount[
"recordtype"], array(
"D",
"T")) ?
"D" :
"L",
714 "display_order" => $displayOrder,
715 "perm_account_sort" => $permAccount,
716 "perm_type_sort" => $permType,
717 "kendoid" => $kendoid ++);
719 $createRecords = array(
"useraccounts" => $createRecords);
721 $sql =
"select email from cuadminusers 722 where user_name = '" . prep_save($Cn, 50) .
"' and cu = '" . prep_save($Cu, 10) .
"'";
723 $sth = db_query($sql, $dbh);
725 throw new exception(
"email query failed.", 5);
727 $email = db_fetch_row($sth)[0];
730 $script =
"userSupportAccessControl.prg";
731 $addr = trim($_SERVER[
"REMOTE_ADDR"]);
732 $vars = array(
"cu" => $Cu);
734 $newAccounts = array();
736 if (!db_work($dbh, HOMECU_WORK_BEGIN)) {
737 throw new exception(
"begin work query failed!", 10);
740 $results = DataUserTableUpdate($dbh, $vars,
null, $createRecords, $userId,
"UA_ADD", $context, $script,
741 "A",
"User Accounts Add", $Cn, $email, $addr);
743 if ($results ===
false) {
744 throw new exception(
"Adding failed.", 106);
747 $dontShowAddNewAccount = $disallowMultipleAccounts && !$hasThisAccount;
752 $memberRecords = array(
"memberacct" => array(array(
"_action" =>
"create",
"accountnumber" => $accountnumber,
"primary_user" => $userId)));
753 $results = DataUserTableUpdate($dbh, $vars,
null, $memberRecords, $userId,
"UM_ADD",
754 $context, $script,
"A",
"User Member Add", $Cn, $email, $addr);
755 if ($results ===
false) {
756 throw new exception(
"Adding failed.", 107);
761 $official = array(
"memberacctrights" => array(
762 array(
"_action" =>
"create",
"accountnumber" => $accountnumber,
"user_id" => $userId,
"whichright" =>
"ACCESS"),
763 array(
"_action" =>
"create",
"accountnumber" => $accountnumber,
"user_id" => $userId,
"whichright" =>
"RDC",
"platform" =>
'["D","A"]'),
764 array(
"_action" =>
"create",
"accountnumber" => $accountnumber,
"user_id" => $userId,
"whichright" =>
"BP",
"platform" =>
'["D","A"]'),
765 array(
"_action" =>
"create",
"accountnumber" => $accountnumber,
"user_id" => $userId,
"whichright" =>
"ES",
"platform" =>
'["D","A"]')
768 $results = DataUserTableUpdate($dbh, $vars,
null, $official, $userId,
"ACCESS_ADD", $context, $script,
769 "A",
"Primary User Access Add", $Cn, $email, $addr);
770 if ( $results ===
false) {
771 throw new exception(
"Adding failed.", 109);
774 $newAccounts[] = array(
"accountnumber" => $accountnumber,
"access" =>
true,
"bpApp" =>
true,
"bpDsk" =>
true,
"esApp" =>
true,
"esDsk" =>
true,
775 "rdcApp" =>
true,
"rdcDsk" =>
true);
778 if (!db_work($dbh, HOMECU_WORK_COMMIT)) {
779 throw new exception(
"commit query failed.", 111);
781 }
catch(exception $e) {
782 if ($e->getCode() >= 100) {
783 db_work($dbh, HOMECU_WORK_ROLLBACK);
785 return array(
"error" => array($e->getMessage()),
"code" => $e->getCode());
787 return array(
"error" => array(),
"code" => 0,
"newAccounts" => $newAccounts,
"newSubaccounts" => $newSubaccounts,
"latestKendoid" => $latestKendoid,
788 "dontShowAddNewAccount" => $dontShowAddNewAccount);
798 function ExpandSettings(&$row, $key) {
799 if ($row[$key] !=
null) {
800 $decoded = hcu_JsonDecode($row[$key]);
801 if (!is_array($decoded)) {
802 throw new exception(
"$key not formatted correctly.", 2);
805 $row[
"${key}Dsk"] = in_array(
"D", $decoded);
806 $row[
"${key}App"] = in_array(
"A", $decoded);
809 $row[
"${key}Dsk"] =
false;
810 $row[
"${key}App"] =
false;
829 function ReadAccessControl($pEnv) {
835 $userId = $pEnv[
"userId"];
836 $allowZeroes = $pEnv[
"allowZeroes"];
837 $disallowMultipleAccounts = $pEnv[
"disallowMultipleAccounts"];
840 $sql =
"select mm.accountnumber, pu.user_id from 841 (select p.user_id, p.group_id from ${Cu}user p 842 inner join ${Cu}user u on p.group_id = u.group_id and p.is_group_primary and u.user_id = " . prep_save(intval($userId)) .
" limit 1) as pu 843 left join (select distinct trim(accountnumber), user_id from ${Cu}useraccounts) as mm(accountnumber, user_id) on pu.user_id = mm.user_id";
845 $sth = db_query($sql, $dbh);
848 throw new exception(
"Access control list failed.", 3);
855 for($i = 0; $row = db_fetch_assoc($sth, $i); $i++) {
857 $isPrimary = $row[
"user_id"] == $userId;
860 if (!is_null($row[
"accountnumber"])) {
861 $accountnumber = $row[
"accountnumber"];
863 $accountnumber = preg_replace(
'/^0+/',
'', trim($accountnumber));
865 $accounts[] = trim(prep_save($accountnumber, 12));
869 $dontShowAddNewAccount = $disallowMultipleAccounts && count($accounts) > 0;
871 $accountData = array();
872 $newAccountData = array();
873 $subaccountData = array();
874 if (count($accounts) > 0) {
876 $sql =
"with rghts as (select trim(accountnumber) as accountnumber, whichright, platform, allowed 877 from ${Cu}memberacctrights where user_id = $userId 878 and whichright in ('ACCESS', 'RDC', 'BP', 'ES')) 879 select ma.accountnumber, access.allowed as access, rdc.platform as rdc, bp.platform as bp, 880 es.platform as es from (values ('" . implode(
"'),('", $accounts) .
"')) as ma(accountnumber) 881 inner join ${Cu}user u on u.user_id = " . prep_save(intval($userId)) .
" 882 left join rghts access on ma.accountnumber = trim(access.accountnumber) and access.whichright = 'ACCESS' 883 left join rghts rdc on ma.accountnumber = rdc.accountnumber and rdc.whichright = 'RDC' 884 left join rghts bp on ma.accountnumber = bp.accountnumber and bp.whichright = 'BP' 885 left join rghts es on ma.accountnumber = es.accountnumber and es.whichright = 'ES'";
887 $sth = db_query($sql, $dbh);
890 throw new exception(
"Right query failed.", 1);
894 for($i = 0; $row = db_fetch_assoc($sth, $i); $i++) {
895 $row[
"access"] = $row[
"access"] ==
"t";
896 ExpandSettings($row,
"rdc");
897 ExpandSettings($row,
"bp");
898 ExpandSettings($row,
"es");
899 $accountData[] = $row;
900 $accounts[] = trim(prep_save($row[
"accountnumber"], 12));
904 $subqueryUA =
"select accountnumber, display_name, ext_withdraw, display_order, view_balances, trim(accounttype) as accounttype, certnumber, 905 trim(recordtype) as recordtype, view_transactions, int_deposit, int_withdraw, ext_deposit, 906 certnumber || trim(recordtype) || trim(accountnumber) || trim(accounttype) as key, 907 case when position('@' in accounttype) <> 0 then 1 908 when position('#' in accounttype) <> 0 then 2 909 else 0 end as perm_type_sort, 910 case when position('@' in accounttype) <> 0 then split_part(accounttype, '@', 2) 911 when position('#' in accounttype) <> 0 then split_part(accounttype, '#', 2) 912 else '000' end as perm_account_sort 913 from ${Cu}useraccounts 914 where trim(accountnumber) in ('" . implode(
"','", $accounts) .
"') and user_id = $userId and trim(recordtype) in ('D', 'L', 'T', 'P')";
916 $subqueryALXB =
"select certnumber || 'D' || trim(accountnumber) || trim(accounttype), trim(description), 917 may_deposit, may_withdraw 918 from ${Cu}accountbalance 920 select '0' || 'T' || trim(accountnumber) || trim(accounttype) || '#' || trim(tomember) as key, 921 trim(description), true, false 922 from ${Cu}crossaccounts where deposittype <> 'L' 924 select '0' || 'L' || trim(accountnumber) || trim(loannumber) as key, 925 trim(description), may_payment, may_addon 926 from ${Cu}loanbalance 928 select '0' || 'P' || trim(accountnumber) || trim(accounttype) || '#' || trim(tomember) as key, 929 trim(description), false, false 930 from ${Cu}crossaccounts where deposittype = 'L'";
932 $sql =
"select ua.*, alxb.description, alxb.key as rec_exists, alxb.allow_deposit, alxb.allow_withdraw 933 from ($subqueryUA) as ua 934 left join ($subqueryALXB) as alxb(key, description, allow_deposit, allow_withdraw) on ua.key = alxb.key 935 order by ua.accountnumber, ua.perm_account_sort, ua.perm_type_sort, ua.display_order";
937 $sth = db_query($sql, $dbh);
940 throw new exception(
"Sub-account query failed.", 2);
943 $usedAccounts = array();
944 $correctLimit = array();
946 for($i = 0; $row = db_fetch_assoc($sth, $i); $i++) {
947 $row[
"kendoid"] = $kendoid++;
948 $row[
"view_balances"] = is_null($row[
"view_balances"]) ? false : trim($row[
"view_balances"]) ==
"t";
949 $row[
"view_transactions"] = is_null($row[
"view_transactions"]) ? false : trim($row[
"view_transactions"]) ==
"t";
950 $row[
"int_deposit"] = is_null($row[
"int_deposit"]) ? false : trim($row[
"int_deposit"]) ==
"t";
951 $row[
"int_withdraw"] = is_null($row[
"int_withdraw"]) ? false : trim($row[
"int_withdraw"]) ==
"t";
952 $row[
"ext_deposit"] = is_null($row[
"ext_deposit"]) ? false : trim($row[
"ext_deposit"]) ==
"t";
953 $row[
"ext_withdraw"] = is_null($row[
"ext_withdraw"]) ? false : trim($row[
"ext_withdraw"]) ==
"t";
955 $row[
"description"] = isset($row[
"description"]) ? trim($row[
"description"]) :
"";
956 $row[
"display_name"] = isset($row[
"display_name"]) ? trim($row[
"display_name"]) :
"";
957 $row[
"display_name"] = $row[
"display_name"] ==
"" || $row[
"display_name"] == $row[
"description"] ?
"" : $row[
"display_name"];
958 $row[
"accountnumber"] = isset($row[
"accountnumber"]) ? trim($row[
"accountnumber"]) :
"";
961 $row[
"restrictViewBalances"] =
false;
962 $row[
"restrictViewTransactions"] = !$row[
"view_balances"];
963 $row[
"restrictIntDeposit"] =
false;
964 $row[
"restrictExtDeposit"] =
false;
965 $row[
"restrictIntWithdraw"] =
false;
966 $row[
"restrictExtWithdraw"] =
false;
969 if ($row[
"restrictViewTransactions"] && $row[
"view_transactions"]) {
970 $correctLimitRow[
"view_transactions"] =
false;
971 $row[
"ext_withdraw"] =
false;
974 if ($row[
"restrictViewBalances"] && $row[
"view_balances"]) {
975 $correctLimitRow[
"view_balances"] =
false;
976 $row[
"view_balances"] =
false;
980 if (in_array($row[
"recordtype"], array(
"L"))) {
981 $row[
"restrictExtWithdraw"] =
true;
988 if (in_array($row[
"recordtype"], array(
"T",
"P"))) {
989 $row[
"restrictIntDeposit"] =
false;
990 $row[
"restrictExtDeposit"] =
true;
991 $row[
"restrictIntWithdraw"] =
true;
992 $row[
"restrictExtWithdraw"] =
true;
993 $row[
"restrictViewBalances"] =
true;
994 $row[
"restrictViewTransactions"] =
true;
998 if ($row[
"view_balances"]) {
999 $correctLimitRow[
"view_balances"] =
false;
1000 $row[
"view_balances"] =
false;
1003 if ($row[
"view_transactions"]) {
1004 $correctLimitRow[
"view_transactions"] =
false;
1005 $row[
"view_transactions"] =
false;
1009 if (isset($row[
"rec_exists"])) {
1010 if (in_array($row[
"recordtype"], array(
"D",
"L"))) {
1011 $restrictDeposit = !isset($row[
"allow_deposit"]) ? false : trim($row[
"allow_deposit"]) !=
"t";
1012 $restrictWithdraw = !isset($row[
"allow_withdraw"]) ? false : trim($row[
"allow_withdraw"]) !=
"t";
1013 $row[
"restrictIntDeposit"] = $restrictDeposit;
1014 $row[
"restrictExtDeposit"] = $restrictDeposit;
1015 $row[
"restrictIntWithdraw"] = $restrictWithdraw;
1016 $row[
"restrictExtWithdraw"] = $restrictWithdraw;
1020 if (in_array($row[
"recordtype"], array(
"L"))) {
1021 $row[
"restrictExtWithdraw"] =
true;
1024 $correctLimitRowA = array(
"user_id" => $userId,
"accountnumber" => $row[
"accountnumber"],
"accounttype" => $row[
"accounttype"],
1025 "certnumber" => $row[
"certnumber"],
"recordtype" => $row[
"recordtype"],
"_action" =>
"update");
1027 $correctLimitRow = array();
1028 if ($row[
"restrictIntDeposit"] && $row[
"int_deposit"]) {
1029 $correctLimitRow[
"int_deposit"] =
false;
1030 $row[
"int_deposit"] =
false;
1033 if ($row[
"restrictExtDeposit"] && $row[
"ext_deposit"]) {
1034 $correctLimitRow[
"ext_deposit"] =
false;
1035 $row[
"ext_deposit"] =
false;
1038 if ($row[
"restrictIntWithdraw"] && $row[
"int_withdraw"]) {
1039 $correctLimitRow[
"int_withdraw"] =
false;
1040 $row[
"int_withdraw"] =
false;
1043 if ($row[
"restrictExtWithdraw"] && $row[
"ext_withdraw"]) {
1044 $correctLimitRow[
"ext_withdraw"] =
false;
1045 $row[
"ext_withdraw"] =
false;
1048 if (count($correctLimitRow) > 0) {
1049 $correctLimit[] = array_merge($correctLimitRowA, $correctLimitRow);
1055 $row[
"recordTypeFilter"] = $row[
"recordtype"] ==
"T" ?
"D" : ($row[
"recordtype"] ==
"P" ?
"L" : $row[
"recordtype"]);
1058 unset($row[
"rec_exists"]);
1061 $subaccountData[] = $row;
1065 if (count($correctLimit) > 0) {
1066 $sql =
"select email from cuadminusers where user_name = '" . prep_save($Cn, 50) .
"' and cu = '" . prep_save($Cu, 10) .
"'";
1067 $sth = db_query($sql, $dbh);
1069 throw new exception(
"email query failed.", 4);
1071 $email = db_fetch_row($sth)[0];
1074 $script =
"userSupportAccessControl.prg";
1075 $addr = trim($_SERVER[
"REMOTE_ADDR"]);
1076 $vars = array(
"cu" => $Cu);
1078 $results = DataUserTableUpdate($dbh, $vars,
null, array(
"useraccounts" => $correctLimit), $userId,
"USERA_UPD",
1079 $context, $script,
"A",
"Correct Restrictions", $Cn, $email, $addr);
1081 if ($results ===
false) {
1082 throw new exception(
"Correct query failed.", 3);
1087 $returnArray = array(
"error" => array(),
"code" => 0,
"accountData" => $accountData,
"subaccountData" => $subaccountData,
"isPrimary" => $isPrimary,
1088 "latestKendoid" => isset($kendoid) ? $kendoid : 1,
"dontShowAddNewAccount" => $dontShowAddNewAccount);
1089 }
catch(exception $e) {
1090 $returnArray = array(
"error" => array($e->getMessage()),
"code" => $e->getCode());
1092 return $returnArray;
1107 function SaveAccountsAndSubaccounts($pEnv, $accounts, $subaccounts) {
1109 $dbh = $pEnv[
"dbh"];
1112 $userId = $pEnv[
"userId"];
1115 $platforms = array();
1117 $accounts = $accounts ==
null ?
"" : trim($accounts);
1118 if ($accounts !=
"" && $accounts !=
"[]") {
1119 $accounts = hcu_JsonDecode($accounts);
1120 if (!is_array($accounts) || count($accounts) == 0) {
1121 throw new exception(
"Accounts aren't encoded correctly.", 1);
1123 foreach($accounts as $account) {
1124 if (!isset($account[
"accountnumber"])) {
1125 throw new exception(
"Account requires a number.", 2);
1127 if (isset($account[
"access"])) {
1128 $sql =
"select 'FOUND' from ${Cu}memberacctrights 1129 where user_id = " . prep_save(intval($userId)) .
" and accountnumber = '" . prep_save($account[
"accountnumber"], 12) .
"' 1130 and whichright = 'ACCESS'";
1131 $sth = db_query($sql, $dbh);
1133 throw new exception (
"Found SQL failed.", 13);
1135 $action = db_num_rows($sth) > 0 ?
"update" :
"create";
1136 $full[] = array(
"_action" => $action,
"whichright" =>
"ACCESS",
"user_id" => $userId,
"accountnumber" => $account[
"accountnumber"],
1137 "allowed" => $account[
"access"]);
1139 ProcessSettings($account,
"es", $platforms, $codes);
1140 ProcessSettings($account,
"rdc", $platforms, $codes);
1141 ProcessSettings($account,
"bp", $platforms, $codes);
1144 if (count($codes) > 0) {
1145 $sql =
"select v.code, v.accountnumber, mar.user_id as exists 1146 from (values " . implode(
",", $codes) .
") as v (code, accountnumber) 1147 left join ${Cu}memberacctrights mar on v.code = mar.whichright 1148 and v.accountnumber = mar.accountnumber and mar.user_id = " . prep_save(intval($userId));
1149 $sth = db_query($sql, $dbh);
1151 throw new exception(
"Verify SQL failed.", 3);
1154 for($i = 0; $row = db_fetch_assoc($sth, $i); $i++) {
1155 $exists = $row[
"exists"] !=
null && trim($row[
"exists"]) !=
"";
1156 $accountnumber = $row[
"accountnumber"] ==
null ?
"" : trim($row[
"accountnumber"]);
1157 $code = $row[
"code"] ==
null ?
"" : trim($row[
"code"]);
1158 $platform = $platforms[$accountnumber][$code];
1159 $record = array(
"whichright" => $code,
"user_id" => $userId,
"accountnumber" => $accountnumber,
"platform" => HCU_JsonEncode($platform));
1161 if (count($platform) > 0) {
1162 $record[
"_action"] =
"create";
1166 if (count($platform) == 0) {
1167 $record[
"_action"] =
"delete";
1168 unset($record[
"platform"]);
1171 $record[
"_action"] =
"update";
1179 $subUpdates = array();
1180 $subaccounts = is_null($subaccounts) ?
"" : trim($subaccounts);
1181 if ($subaccounts !=
"" && $subaccounts !=
"[]") {
1182 $subaccounts = hcu_JsonDecode($subaccounts);
1183 if (!is_array($subaccounts) || count($subaccounts) == 0) {
1184 throw new exception(
"Sub-account not formed correctly.", 8);
1186 foreach($subaccounts as $row) {
1187 if (!isset($row[
"accounttype"]) || !isset($row[
"certnumber"]) || !isset($row[
"recordtype"]) || !isset($row[
"accountnumber"])) {
1188 throw new exception(
"Sub-account not formed correctly.", 9);
1190 $row[
"_action"] =
"update";
1191 $row[
"user_id"] = $userId;
1192 $subUpdates[] = $row;
1196 if (count($full) > 0 || count($subUpdates) > 0) {
1197 $sql =
"select email from cuadminusers where user_name = '" . prep_save($Cn, 50) .
"' and cu = '" . prep_save($Cu, 10) .
"'";
1198 $sth = db_query($sql, $dbh);
1200 throw new exception(
"email query failed.", 4);
1202 $email = db_fetch_row($sth)[0];
1205 $script =
"userSupportAccessControl.prg";
1206 $addr = trim($_SERVER[
"REMOTE_ADDR"]);
1207 $vars = array(
"cu" => $Cu);
1209 if (!db_work($dbh, HOMECU_WORK_BEGIN)) {
1210 throw new exception(
"Work begin query failed.", 12);
1213 if (count($full) > 0) {
1214 $official = array(
"memberacctrights" => $full);
1215 $results = DataUserTableUpdate($dbh, $vars,
null, $official, $userId,
"MAR_CHG", $context, $script,
1216 "A",
"Member Account Rights Change", $Cn, $email, $addr);
1217 if ( $results ===
false) {
1218 throw new exception(
"change failed.", 107);
1222 if (count($subUpdates) > 0) {
1223 $official = array(
"useraccounts" => $subUpdates);
1225 $results = DataUserTableUpdate($dbh, $vars,
null, $official, $userId,
"UA_UPDATE", $context, $script,
1226 "A",
"User Accounts Update", $Cn, $email, $addr);
1227 if ( $results ===
false) {
1228 throw new exception(
"Sub Updating failed.", 110);
1232 if (!db_work($dbh, HOMECU_WORK_COMMIT)) {
1233 throw new exception(
"Work commit query failed.", 112);
1236 }
catch(exception $e) {
1237 if ($e->getCode() >= 100) {
1238 db_work($dbh, HOMECU_WORK_ROLLBACK);
1240 return array(
"error" => array($e->getMessage()),
"code" => $e->getCode());
1242 return array(
"error" => array(),
"code" => 0);
1254 function ProcessSettings($account, $setting, &$platforms, &$codes) {
1255 $setting = trim($setting);
1256 if (HCU_array_key_exists(
"${setting}Dsk", $account) && HCU_array_key_exists(
"${setting}App", $account)) {
1257 $platform = array();
1258 $account[
"${setting}Dsk"] && $platform[] =
"D";
1259 $account[
"${setting}App"] && $platform[] =
"A";
1261 $code = strtoupper($setting);
1262 $accountnumber = trim($account[
"accountnumber"]);
1263 if (!isset($platforms[$accountnumber])) {
1264 $platforms[$accountnumber] = array();
1266 $platforms[$accountnumber][$code] = $platform;
1267 $codes[] =
"('$code', '" . prep_save($accountnumber, 12) .
"')";