19 function GetGoogleCookie($devmode) {
21 $googleCookie =
"c2FtdWVsQGhvbWVjdS5jb218cVpoMVBFeGVZYlkrTlo5anlUMUwyNWhsYVYrM01XZlUxcW9iOEVXdVpURXdnUi9FLzcvRWprR0FIYWlDSU9rTnI3Zlk3L0tyM3EvcmxPck9nYTFnc1lhMkU5S3Y2d2h6SDJIOHZzT0VNYit0MEo5SEFhYmxKT25xSHdmNkJDZHRETzVENVhVVDB5aWdPam4wQTNWVTNHWWRsd0xLb3F4Vlg4L1FQRmxQRVorREZoS2VvSjRNL053OUdPcVFKcy9VWGc9PXwxNTY3MDk4ODkzfDdMQU56WVl1b0FFQXA4NTZWbGhwMjk2THRPTktqVFcrY3BzaWVkdURzZnZ1Z0ZjOFZmc1N4VzQrbE81R2w4dThUTk9GUWJYZEkxaXRvN3QzTzFkMHN0aDFHOXYrRjczQ2k4NGh3dCt4RnZQbDV3PT0=|1567095293|FEDshz4VY7clGFw5M5z4IT3Afwc=";
23 $googleCookie =
"{$_COOKIE['homecu_dev_oauth2_proxy']}";
36 function RunCurlOdyssey($url, $devmode) {
37 $googleCookie = GetGoogleCookie($devmode);
38 $curlCookies =
"HCUTicket=" . urlencode($_COOKIE[
'HCUTicket']) .
";homecu_dev_oauth2_proxy=$googleCookie";
40 $rawresp = RunCurl($url, $curlCookies);
53 function RunCurlMammoth($url) {
54 $curlCookies =
"HCUTicket=" . urlencode($_COOKIE[
'HCUTicket']);
56 $rawresp = RunCurl($url, $curlCookies);
74 function RunCurl($url, $cookies) {
76 curl_setopt($ch, CURLOPT_URL, $url);
77 curl_setopt($ch, CURLOPT_COOKIE, $cookies);
78 curl_setopt($ch, CURLOPT_USERPWD,
"nobody:no1home");
79 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
80 curl_setopt($ch, CURLOPT_TIMEOUT, 30);
81 curl_setopt($ch, CURLOPT_HEADER, 0);
82 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
84 $rawresp = curl_exec($ch);
86 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
88 $returnArray = array(
"httpCode" => $httpCode,
"rawresp" => $rawresp);
101 function GetLimitInBytes() {
102 $val = trim(ini_get(
"memory_limit"));
103 $last = strtolower($val[strlen($val) - 1]);
129 function GatherBillingInfoChildren($dbh, $sysenv, $staffId) {
132 extract ($sysenv[
"BILL"]);
134 if (!isset($sid) || trim($sid) ==
"") {
135 throw new exception (
"Sid is required.", 1);
138 if (!isset($childType) || trim($childType) ==
"") {
139 throw new exception (
"Child type is required.", 2);
142 $monthFormat =
"Y-m-d|";
143 $timeFormat =
"H:i:s|";
145 if (!isset($reportDate) || trim($reportDate) ==
"") {
146 throw new exception (
"Month is required.", 3);
148 if (!isset($cutoffDate) || trim($cutoffDate) ==
"") {
149 throw new exception (
"Cutoff Date is required.", 4);
151 if (!isset($cutoffTime) || trim($cutoffTime) ==
"") {
152 throw new exception (
"Cutoff time is required.", 5);
155 if (!DateTime::createFromFormat($monthFormat, $reportDate)) {
156 throw new exception (
"Month is invalid.", 6);
158 if (!DateTime::createFromFormat($monthFormat, $cutoffDate)) {
159 throw new exception (
"Cutoff date is invalid.", 7);
161 if (!DateTime::createFromFormat($timeFormat, $cutoffTime)) {
162 throw new exception (
"Cutoff time is invalid.", 8);
165 $startFile = GetStartFile();
166 $contractFile = GetContractFile();
167 $estatementFile = GetEstatementCountFile();
168 $ivrFile = GetIVRCountFile();
169 $ipayFile = GetIPayCountFile();
170 $timetrackFile = GetTimeTrackCountFile();
172 $dateTime =
new DateTime($reportDate);
173 $monthBit = 1 << intval($dateTime->format(
"n")) - 1;
174 $sysenv[
"BILL"][
"monthBit"] = $monthBit;
176 $cutoffDate = str_replace(
"-",
"", $cutoffDate);
177 $date = DateTime::createFromFormat(
"Y-m-d", $reportDate);
178 $date->modify(
"-1 month");
179 $regularReportDate = $date->format(
"Y-m") .
"-01";
180 $ivrStartDate = $date->format(
"m/01/Y");
181 $ivrEndDate = $date->format(
"m/t/Y");
182 $iPayReportDate = $date->format(
"Y_m");
187 if ($sysenv[
"devmode"] == 1) {
189 if (isset($callMy) && $callMy ==
"Y") {
190 $queryResults = GetMonitorBillingList($dbh, $sysenv);
192 $queryResults = GetPotentiallyFromDifferentServer($dbh, $sysenv);
195 $queryResults = GetPotentiallyFromDifferentServer($dbh, $sysenv);
198 $fileArray = array(
"sid" => $sid,
"reportDate" => $reportDate,
"monthBit" => $monthBit,
199 "contracts" => $queryResults,
"ready" =>
true);
200 file_put_contents($contractFile, HCU_JsonEncode($fileArray));
205 $countResults = GetEstatementCounts($cutoffDate, $cutoffTime, $sysenv[
"devmode"]);
207 $countResults[
"sid"] = $sid;
208 $countResults[
"cutoffDate"] = $cutoffDate;
209 $countResults[
"cutoffTime"] = $cutoffTime;
210 $countResults[
"ready"] =
true;
212 file_put_contents($estatementFile, HCU_JsonEncode($countResults));
217 $countResults = GetIVRCounts($ivrStartDate, $ivrEndDate);
219 $countResults[
"sid"] = $sid;
220 $countResults[
"ivrStartDate"] = $ivrStartDate;
221 $countResults[
"ivrEndDate"] = $ivrEndDate;
222 $countResults[
"ready"] =
true;
224 file_put_contents($ivrFile, HCU_JsonEncode($countResults));
229 $countResults = GetIPayCounts ($iPayReportDate, $sysenv[
"devmode"]);
231 $countResults[
"sid"] = $sid;
232 $countResults[
"iPayReportDate"] = $iPayReportDate;
233 $countResults[
"ready"] =
true;
235 file_put_contents($ipayFile, HCU_JsonEncode($countResults));
240 $countResults = GetTimetrackCounts($regularReportDate,
"");
242 $countResults[
"sid"] = $sid;
243 $countResults[
"regularReportDate"] = $regularReportDate;
244 $countResults[
"ready"] =
true;
246 file_put_contents($timetrackFile, HCU_JsonEncode($countResults));
250 throw new exception (
"Child process not found.", 9);
255 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"type" =>
"child");
256 }
catch (exception $e) {
257 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage());
283 function GatherBillingInfo($dbh, $sysenv, $staffId, $mode) {
286 extract ($sysenv[
"BILL"]);
288 $monthFormat =
"Y-m-d|";
289 $timeFormat =
"H:i:s|";
291 if (!isset($reportDate) || trim($reportDate) ==
"") {
292 throw new exception (
"Month is required.", 4);
294 if (!isset($cutoffDate) || trim($cutoffDate) ==
"") {
295 throw new exception (
"Cutoff Date is required.", 5);
297 if (!isset($cutoffTime) || trim($cutoffTime) ==
"") {
298 throw new exception (
"Cutoff time is required.", 6);
301 if (!DateTime::createFromFormat($monthFormat, $reportDate)) {
302 throw new exception (
"Month is invalid.", 1);
304 if (!DateTime::createFromFormat($monthFormat, $cutoffDate)) {
305 throw new exception (
"Cutoff date is invalid.", 2);
307 if (!DateTime::createFromFormat($timeFormat, $cutoffTime)) {
308 throw new exception (
"Cutoff time is invalid.", 3);
312 $startFile = GetStartFile();
313 $contractFile = GetContractFile();
314 $estatementFile = GetEstatementCountFile();
315 $ivrFile = GetIVRCountFile();
316 $ipayFile = GetIPayCountFile();
317 $timetrackFile = GetTimeTrackCountFile();
319 if (file_exists($startFile)) {
320 throw new exception (
"Another run billing process is running.", 9);
323 $sid =
new DateTimeZone(
"UTC");
324 $sid =
new DateTime(
"now", $sid);
325 $sid = $sid->format(
"U");
328 $defaultString = HCU_JsonEncode(array(
"sid" => $sid,
"ready" =>
false));
330 file_put_contents($contractFile, $defaultString);
331 file_put_contents($estatementFile, $defaultString);
332 file_put_contents($ivrFile, $defaultString);
333 file_put_contents($ipayFile, $defaultString);
334 file_put_contents($timetrackFile, $defaultString);
336 $fileArray = array(
"sid" => $sid,
"reportDate" => $reportDate,
"cutoffDate" => $cutoffDate,
"cutoffTime" => $cutoffTime);
337 file_put_contents($startFile, HCU_JsonEncode($fileArray));
339 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"sid" => $sid,
"type" =>
"gather",
"callMy" => $mode ===
"odyssey" ?
"Y" :
"N",
340 "reportDate" => $reportDate,
"cutoffDate" => $cutoffDate,
"cutoffTime" => $cutoffTime);
341 }
catch (exception $e) {
342 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"type" =>
"gather");
361 function CheckBillingInfo($dbh, $sysenv, $staffId) {
364 $sid = $sysenv[
"BILL"][
"sid"];
365 if (!isset($sid) || trim($sid) ==
"") {
366 throw new exception (
"Sid is required.", 1);
369 $results = VerifyFiles($sysenv);
370 $ready = $results[
"status"] ===
"000";
372 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"sid" => $sid,
"ready" => $ready,
"type" =>
"check");
374 }
catch (exception $e) {
375 $sysenv[
"logger"]->error(
"Run billing failed: " . $e->getMessage());
376 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"type" =>
"check");
394 function RunBillingCleanup($dbh, $sysenv, $staffId) {
396 extract ($sysenv[
"BILL"]);
398 if (!isset($sid) || trim($sid) ==
"") {
399 throw new exception (
"Sid is required.", 1);
402 $startFile = GetStartFile();
403 $contractFile = GetContractFile();
404 $estatementFile = GetEstatementCountFile();
405 $ivrFile = GetIVRCountFile();
406 $ipayFile = GetIPayCountFile();
407 $timetrackFile = GetTimeTrackCountFile();
409 $startOnly = $sysenv[
"devmode"] !== 1;
411 if (file_exists($startFile)) {
416 if (file_exists($contractFile)) {
417 unlink($contractFile);
420 if (file_exists($estatementFile)) {
421 unlink($estatementFile);
424 if (file_exists($ivrFile)) {
428 if (file_exists($ipayFile)) {
432 if (file_exists($timetrackFile)) {
433 unlink($timetrackFile);
437 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"type" =>
"cleanup");
438 }
catch (exception $e) {
439 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"type" =>
"cleanup");
455 function VerifyFiles($sysenv) {
457 $startFile = GetStartFile();
458 $contractFile = GetContractFile();
459 $estatementFile = GetEstatementCountFile();
460 $ivrFile = GetIVRCountFile();
461 $ipayFile = GetIPayCountFile();
462 $timetrackFile = GetTimeTrackCountFile();
465 extract ($sysenv[
"BILL"]);
467 if (!isset($sid) || trim($sid) ==
"") {
468 throw new exception (
"Sid is required.", 1);
471 if (!file_exists($startFile)) {
472 throw new exception (
"Start file does not exist.", 2);
475 $contents = file_get_contents($startFile);
476 if ($contents ===
false) {
477 throw new exception (
"Contents do not exist in start file.", 3);
479 $contents = HCU_JsonDecode($contents);
481 $testSid = HCU_array_key_value(
"sid", $contents);
483 if ($testSid !== $sid) {
484 throw new exception (
"Sid doesn't match in start file.", 4);
487 $reportDate = HCU_array_key_value(
"reportDate", $contents);
488 if ($reportDate ===
false) {
489 throw new exception (
"ReportDate is not found.", 20);
492 $data[
"reportDate"] = $reportDate;
494 if (!file_exists($contractFile)) {
495 throw new exception (
"Contract file doesn't exist.", 5);
498 $contents = file_get_contents($contractFile);
499 if ($contents ===
false) {
500 throw new exception (
"Contract file doesn't exist.", 6);
502 $contents = HCU_JsonDecode($contents);
504 $testSid = HCU_array_key_value(
"sid", $contents);
506 if ($testSid !== $sid) {
507 throw new exception (
"Sid doesn't match in contract file.", 7);
510 $contractResults = HCU_array_key_value(
"contracts", $contents);
511 $contractResults = $contractResults ===
false ? array() : $contractResults;
512 $membershipCounts = HCU_array_key_value(
"membershipCounts", $contractResults);
513 $membershipCounts = $membershipCounts ===
false ? array() : $membershipCounts;
514 $contractResults = HCU_array_key_value(
"record", $contractResults);
515 $contractResults = $contractResults ===
false ? array() : $contractResults;
517 $data[
"contractResults"] = $contractResults;
518 $data[
"membershipCounts"] = $membershipCounts;
520 if (!file_exists($estatementFile)) {
521 throw new exception (
"eStatement file doesn't exist.", 8);
524 $contents = file_get_contents($estatementFile);
525 if ($contents ===
false) {
526 throw new exception (
"eStatement file doesn't exist.", 9);
528 $contents = HCU_JsonDecode($contents);
530 $testSid = HCU_array_key_value(
"sid", $contents);
532 if ($testSid !== $sid) {
533 throw new exception (
"Sid doesn't match in eStatement file.", 10);
536 $ready = HCU_array_key_value(
"ready", $contents);
538 throw new exception (
"eStatements are not ready.", 22);
541 $eStatementCounts = HCU_array_key_value(
"counts", $contents);
542 $eStatementCounts = $eStatementCounts ===
false ? array() : $eStatementCounts;
544 $data[
"eStatementCounts"] = $eStatementCounts;
546 if (!file_exists($ivrFile)) {
547 throw new exception (
"IVR file doesn't exist.", 11);
550 $contents = file_get_contents($ivrFile);
551 if ($contents ===
false) {
552 throw new exception (
"IVR file doesn't exist.", 12);
554 $contents = HCU_JsonDecode($contents);
556 $testSid = HCU_array_key_value(
"sid", $contents);
558 if ($testSid !== $sid) {
559 throw new exception (
"Sid doesn't match in IVR file.", 13);
562 $ready = HCU_array_key_value(
"ready", $contents);
564 throw new exception (
"IVRs are not ready.", 23);
567 $ivrCounts = HCU_array_key_value(
"counts", $contents);
568 $ivrCounts = $ivrCounts ===
false ? array() : $ivrCounts;
570 $data[
"ivrCounts"] = $ivrCounts;
572 if (!file_exists($ipayFile)) {
573 throw new exception (
"iPay file doesn't exist.", 14);
576 $contents = file_get_contents($ipayFile);
577 if ($contents ===
false) {
578 throw new exception (
"iPay file doesn't exist.", 15);
580 $contents = HCU_JsonDecode($contents);
582 $testSid = HCU_array_key_value(
"sid", $contents);
584 if ($testSid !== $sid) {
585 throw new exception (
"Sid doesn't match in iPay file.", 16);
588 $ready = HCU_array_key_value(
"ready", $contents);
590 throw new exception (
"iPays are not ready.", 24);
593 $iPayCounts = HCU_array_key_value(
"counts", $contents);
594 $iPayCounts = $iPayCounts ===
false ? array() : $iPayCounts;
596 $data[
"iPayCounts"] = $iPayCounts;
598 if (!file_exists($timetrackFile)) {
599 throw new exception (
"Timetrack file doesn't exist.", 17);
602 $contents = file_get_contents($timetrackFile);
603 if ($contents ===
false) {
604 throw new exception (
"Timetrack file doesn't exist.", 18);
606 $contents = HCU_JsonDecode($contents);
608 $testSid = HCU_array_key_value(
"sid", $contents);
610 if ($testSid !== $sid) {
611 throw new exception (
"Sid doesn't match in timetrack file.", 19);
614 $ready = HCU_array_key_value(
"ready", $contents);
616 throw new exception (
"Timetracks are not ready.", 25);
619 $timetrackCounts = HCU_array_key_value(
"counts", $contents);
620 $timetrackCounts = $timetrackCounts ===
false ? array() : $timetrackCounts;
622 $data[
"timetrackCounts"] = $timetrackCounts;
625 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"data" => $data);
626 }
catch (exception $e) {
627 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage());
646 function ExtractContractsAndCounts($sysenv) {
649 $results = VerifyFiles($sysenv);
650 if ($results[
"status"] !==
"000") {
651 throw new exception ($results[
"error"], $results[
"status"]);
653 extract($results[
"data"]);
656 $countByCU = array();
657 foreach($contractResults as $record) {
658 $cu = strtoupper(trim($record[
"cu"]));
659 if (!isset($countByCU[$cu])) {
660 $countByCU[$cu] = array(
"cuinfo" => array(),
"eStatements" => array(),
"ivr" => array(),
"timetrack" => array(),
"iPay" => array());
664 foreach($membershipCounts as $record) {
665 $cu = strtoupper(trim($record[
"cu"]));
666 if (isset($countByCU[$cu])) {
667 $countByCU[$cu][
"cuinfo"] = $record;
671 foreach($eStatementCounts as $key => $value) {
672 $cu = strtoupper(trim($key));
673 if (isset($countByCU[$cu])) {
674 $countByCU[$cu][
"eStatements"] = $value;
678 foreach($ivrCounts as $record) {
679 $cu = strtoupper(trim($record[
"cu"]));
680 if (isset($countByCU[$cu])) {
683 if (!array_key_exists(
"ivr", $countByCU[$cu])) {
684 $countByCU[$cu][
"ivr"] = array(
"undivided" => $record);
685 unset($countByCU[$cu][
"ivr"][
"DNID"]);
688 $undivided = HCU_array_key_value(
"undivided", $countByCU[$cu][
"ivr"]);
689 $undivided = $undivided ===
false ? array(
"calls" => 0,
"minutes" => 0) : $undivided;
690 $undivided[
"calls"] += $record[
"calls"];
691 $undivided[
"minutes"] += $record[
"minutes"];
692 $countByCU[$cu][
"ivr"][
"undivided"] = $undivided;
695 $aDNID = $record[
"DNID"];
696 unset($record[
"DNID"]);
697 $countByCU[$cu][
"ivr"][$aDNID] = $record;
701 foreach($iPayCounts as $cu => $record) {
702 if (HCU_array_key_exists($cu, $cuIpayMinimumMap)) {
703 $cuIpayRec = $cuIpayMinimumMap[$cu];
704 $record[
"Monthly Minimum"] = HCU_array_key_exists(
"minimum", $cuIpayRec) ? intval($cuIpayRec[
"minimum"]) : 0;
705 $record[
"Per Transaction"] = HCU_array_key_exists(
"transfee", $cuIpayRec) ? floatval($cuIpayRec[
"transfee"]) : 0;
707 $countByCU[$cu][
"iPay"] = $record;
710 $countByCU[
"timetrack"] = $timetrackCounts;
712 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"contractResults" => $contractResults,
"counts" => $countByCU,
"reportDate" => $reportDate);
713 }
catch (exception $e) {
714 $sysenv[
"logger"]->error(
"Run Billing Failed: " . $e->getMessage());
715 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage());
734 function RunBillingMonth($dbh, $sysenv, $staffId) {
737 $sid = HCU_array_key_value(
"sid", $sysenv[
"BILL"]);
739 $results = ExtractContractsAndCounts($sysenv);
740 if ($results[
"status"] !==
"000") {
741 throw new exception (
"Run billing failed to extract values for contracts or counts.", 2);
744 $reportDate = $results[
"reportDate"];
745 $counts = $results[
"counts"];
746 $contractResults = $results[
"contractResults"];
748 $results = StartProcess($dbh, $reportDate);
749 if ($results[
"status"] !==
"000") {
750 throw new exception (
"Process was not started successfully.", 7);
753 foreach($contractResults as $record) {
755 $results = ProcessFeature($dbh, $reportDate, $record, $counts, $staffId);
756 if ($results[
"status"] !=
"000") {
757 throw new exception ($results[
"error"], 9);
761 $results = ProcessTimetrack($dbh, $counts[
"timetrack"], $reportDate);
762 if ($results[
"status"] !==
"000") {
763 throw new exception (
"Processing timetrack failed.", 12);
767 $sql =
"update cubillmonth set processing_flag = 100, last_step_date = now() 768 where billing_date = '" . prep_save($reportDate) .
"' 769 and (last_error_code is null or last_error_code = '') and (last_error_msg is null or last_error_msg = '')";
770 $sth = db_query($sql, $dbh);
772 throw new exception (
"Update query failed.", 10);
776 $updateSQL =
"update cutrack set billed = 'Y' where track_id in (select t.track_id from cutrack t 777 inner join cubillpreinvoice pi on t.billing_feature_id = pi.feature_detail_id 778 and pi.billing_date = '" . prep_save($reportDate) .
"' and billing_part in (0,2))";
780 $sth = db_query($sql, $dbh);
782 throw new exception (
"Update query failed.", 11);
785 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"sid" => $sid,
"type" =>
"run");
787 }
catch (exception $e) {
788 if ($e->getCode() != 2) {
789 $sysenv[
"logger"]->error(
"Run Billing Failed: " . $e->getMessage());
791 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"sid" => $sid,
"type" =>
"run");
805 function StartProcess($dbh, $reportDate) {
808 if (!db_work ($dbh, HOMECU_WORK_BEGIN)) {
809 throw new exception(
"begin query failed.", 1);
812 $sql =
"insert into cubillmonth (billing_date, started_date, last_error_code, last_error_msg) 813 select '$reportDate', now(), '', '' where not exists (select 'FOUND' from cubillmonth where billing_date = '" . prep_save($reportDate) .
"')";
814 $sth = db_query($sql, $dbh);
816 throw new exception (
"cubillmonth insert failed.", 2);
820 $sql =
"delete from cubillpreinvoice 821 where billing_date = '" . prep_save($reportDate) .
"' 822 and not exists (select 'FOUND' from cubillmonth where billing_date = '" . prep_save($reportDate) .
"' and processing_flag > 100)";
823 $sth = db_query($sql, $dbh);
825 throw new exception (
"cubillpreinvoice deletion failed.", 3);
828 $sql =
"update cubillmonth set processing_flag = 10, last_step_date = now(), last_error_code = '', last_error_msg = '' 829 where billing_date = '" . prep_save($reportDate) .
"'";
830 $sth = db_query($sql, $dbh);
832 throw new exception (
"cubillmonth update failed.", 4);
835 $sql =
"update cubillfeaturedetail set processing_flag = 0 where end_date is null or end_date > '" . prep_save($reportDate) .
"'";
836 $sth = db_query($sql, $dbh);
838 throw new exception (
"cubillfeaturedetail update failed.", 5);
841 if (!db_work ($dbh, HOMECU_WORK_COMMIT)) {
842 throw new exception(
"commit query failed.", 7);
845 $returnArray = array(
"status" =>
"000",
"error" =>
"");
846 }
catch (exception $e) {
848 db_work($dbh, HOMECU_WORK_ROLLBACK);
849 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage());
868 function ProcessFeature($dbh, $reportDate, $record, $counts, $staffId) {
870 $cu = strtoupper(trim($record[
"cu"]));
872 $membershipRec = HCU_array_key_value(
"cuinfo", $counts[$cu]);
873 $membershipCount = $membershipRec ===
false || !HCU_array_key_exists(
"membership", $membershipRec) ? 0 : intval($membershipRec[
"membership"]);
874 $assetCount = $membershipRec ===
false || !HCU_array_key_exists(
"assets", $membershipRec) ? 0 : floatval($membershipRec[
"assets"]);
875 $eStatementCount = HCU_array_key_exists(
"eStatements", $counts[$cu]) ? intval($counts[$cu][
"eStatements"]) : 0;
876 $iPayCount = HCU_array_key_exists(
"iPay", $counts[$cu]) ? $counts[$cu][
"iPay"] : array();
877 $ivrCount = HCU_array_key_exists(
"ivr", $counts[$cu]) ? $counts[$cu][
"ivr"] : array();
880 $template = HCU_JsonDecode($record[
"billingTemplate"]);
882 unset($record[
"billingTemplate"]);
884 $record[
"description"] = $template[
"descr"];
885 $record[
"qty1"] = intval($template[
"qty1"]);
886 $record[
"qty2"] = intval($template[
"qty2"]);
887 $record[
"fixed"] = floatval($template[
"fixed"]);
888 $record[
"variable"] = floatval($template[
"variable"]);
889 $record[
"calcId"] = intval($record[
"calcId"]);
890 $record[
"aDNID"] = HCU_array_key_exists(
"aDNID", $template) ? trim($template[
"aDNID"]) :
"";
892 $frequency = $record[
"frequency"];
894 $alreadyAdded =
false;
896 $calcId = $record[
"calcId"];
897 $salesItemId = $record[
"salesItemId"];
902 $results = ProcessFeatureStart($dbh, $reportDate, $record);
903 if ($results[
"status"] !==
"000") {
904 if ($results[
"status"] < 0) {
905 $warnings[] = $results[
"error"];
906 throw new exception ($results[
"error"], -1);
908 throw new exception (
"Process feature start failed.", 2);
917 $calcs = CalcMembership($membershipCount, $record);
918 $results = ProcessFeatureEnd($dbh, $calcs[
"count"], $calcs[
"amount"], $reportDate,
false, $record, $staffId, $frequency);
919 if ($results[
"status"] !==
"000") {
920 throw new exception ($results[
"error"], 4);
924 $results = ProcessFeatureEnd($dbh, $membershipCount, $record[
"variable"] * $membershipCount, $reportDate,
false, $record, $staffId, $frequency);
925 if ($results[
"status"] !==
"000") {
926 throw new exception ($results[
"error"], 5);
931 $calcs = CalcSunset($membershipCount, $record);
932 if ($calcs[
"processIt"]) {
933 $results = ProcessFeatureEnd($dbh, $calcs[
"count"], $calcs[
"amount"], $reportDate,
false, $record, $staffId, $frequency);
934 if ($results[
"status"] !==
"000") {
935 throw new exception ($results[
"error"], 6);
957 $results = ProcessFeatureEnd($dbh, 1, $record[
"fixed"], $reportDate,
false, $record, $staffId, $frequency);
958 if ($results[
"status"] !==
"000") {
959 throw new exception ($results[
"error"], 7);
965 $calcs = CalcEstatements($eStatementCount, $record);
966 if ($calcs[
"warning"] !=
"") {
967 $warnings[] = $calcs[
"warning"];
969 $results = ProcessFeatureEnd($dbh, $calcs[
"count"], $calcs[
"amount"], $reportDate, $calcs[
"dontAddRecord"], $record, $staffId, $frequency);
970 if ($results[
"status"] !==
"000") {
971 throw new exception ($results[
"error"], 8);
978 $calcs = CalcIVR($ivrCount, $record);
979 $results = ProcessFeatureEnd($dbh, $calcs[
"count"], $calcs[
"amount"], $reportDate,
false, $record, $staffId, $frequency);
980 if ($results[
"status"] !==
"000") {
981 throw new exception ($results[
"error"], 9);
984 if ($record[
"calcId"] != 36) {
985 $amount2 = $record[
"fixed"];
986 $record[
"description"] .= $record[
"actualDescription"] .
" Monthly Rate";
988 $results = ProcessFeatureEnd($dbh, 1, $amount2, $reportDate,
false, $record, $staffId, $frequency);
989 if ($results[
"status"] !==
"000") {
990 throw new exception ($results[
"error"], 10);
997 throw new exception (
"Email is unused now.", -13);
1000 $calcs = CalcSSL($assetCount, $record, $cu);
1001 if ($calcs[
"warning"] !=
"") {
1002 $warnings[] = $calcs[
"warning"];
1004 if ($calcs[
"processIt"]) {
1005 $results = ProcessFeatureEnd($dbh, $calcs[
"count"], $calcs[
"amount"], $reportDate,
false, $record, $staffId, $frequency);
1006 if ($results[
"status"] !==
"000") {
1007 throw new exception ($results[
"error"], 11);
1013 $calcs = CalcConditionalMemb($membershipCount, $record);
1014 $results = ProcessFeatureEnd($dbh, $calcs[
"count"], $calcs[
"amount"], $reportDate,
false, $record, $staffId, $frequency);
1015 if ($results[
"status"] !==
"000") {
1016 throw new exception ($results[
"error"], 12);
1026 if (count($iPayCount) > 0) {
1027 $calcs = CalcIpay($iPayCount, $record);
1028 if ($calcs[
"processIt"]) {
1029 $results = ProcessFeatureEnd($dbh, $calcs[
"count"], $calcs[
"amount"], $reportDate,
false, $record, $staffId, $frequency);
1030 if ($results[
"status"] !==
"000") {
1031 throw new exception ($results[
"error"], 6);
1040 throw new exception (
"CalcId doesn't exist! (CALC ID: $calcId, SALES ITEM ID: $salesItemId)", 3);
1044 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"warnings" => $warnings);
1045 }
catch (exception $e) {
1046 if ($e->getCode() < 0) {
1047 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"warnings" => $warnings);
1049 $sql =
"update cubillmonth set last_step_date = now(), last_error_code = 'Error', last_error_msg = '" . prep_save($e->getMessage(), 250)
1050 .
"' where billing_date = '" . prep_save($reportDate) .
"'";
1051 $sth = db_query($sql, $dbh);
1052 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"warnings" => $warnings);
1056 return $returnArray;
1071 function ProcessFeatureStart($dbh, $reportDate, $record) {
1073 $featureDetailId = $record[
"featureDetailId"];
1074 $cu = strtoupper(trim($record[
"cu"]));
1075 $warnings = array();
1077 $sql =
"select pi.id from cubillpreinvoice pi 1078 where pi.processing_flag >= 10 and pi.processing_flag < 20 and pi.feature_detail_id = " . intval($featureDetailId) .
" 1079 and upper(pi.cu_id) = '" . prep_save($cu, 10) .
"' and pi.billing_date = '" . prep_save($reportDate) .
"'";
1080 $sth = db_query($sql, $dbh);
1082 throw new exception (
"Select query failed.", 1);
1085 if (db_num_rows($sth) > 0) {
1086 throw new exception (
"QWBC is running for CU: $cu and feature detail: $featureDetailId", -2);
1089 if (!db_work ($dbh, HOMECU_WORK_BEGIN)) {
1090 throw new exception(
"begin query failed.", 3);
1093 $sql =
"delete from cubillpreinvoice where feature_detail_id = " . intval($featureDetailId) .
" and upper(cu_id) = '" . prep_save($cu, 10) .
"' 1094 and billing_date = '" . prep_save($reportDate) .
"'";
1095 $sth = db_query($sql, $dbh);
1097 throw new exception (
"Delete query failed.", 104);
1100 $sql =
"update cubillfeaturedetail set processing_flag = 1 where id = " . intval($featureDetailId);
1101 $sth = db_query($sql, $dbh);
1103 throw new exception (
"Update query failed.", 105);
1106 if (!db_work ($dbh, HOMECU_WORK_COMMIT)) {
1107 throw new exception(
"commit query failed.", 106);
1110 $returnArray = array(
"status" =>
"000",
"error" =>
"");
1111 }
catch (exception $e) {
1112 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage());
1114 return $returnArray;
1133 function ProcessFeatureEnd($dbh, $count, $amount, $reportDate, $alreadyAdded, $record, $staffId, $frequency) {
1135 $featureDetailId = $record[
"featureDetailId"];
1136 $addendum = DateTime::createFromFormat(
"Y-m-d", $reportDate);
1137 $addendum = $addendum->format(
"M Y");
1138 $cu = trim(strtoupper($record[
"cu"]));
1139 $description = trim($record[
"description"]);
1140 $billingItem = $record[
"billingId"];
1141 $salesItemId = $record[
"salesItemId"];
1142 $billHalf = $record[
"billHalf"] ==
"Y";
1143 $billingStatus = intval($record[
"billingStatus"]);
1144 $partiallyBilledDate = $record[
"partiallyBilledDate"];
1146 if (!$alreadyAdded) {
1150 switch($billingStatus) {
1154 if($partiallyBilledDate ==
"" || $partiallyBilledDate != $reportDate) {
1155 throw new exception (
"Do not create record.", -1);
1158 $description = $record[
"actualDescription"] .
" (50% deposit)";
1160 $amount = $amount / 2.0;
1161 $insertSQL =
"insert into cubillpreinvoice (feature_detail_id, sales_item_id, billing_system_id, cu_id, billing_date, description, 1162 quantity, amount, billing_part) 1163 values (" . intval($featureDetailId) .
", " . intval($salesItemId) .
", '" . prep_save($billingItem, 20)
1164 .
"', '" . prep_save($cu, 10) .
"', '" . prep_save($reportDate) .
"', '" . prep_save($description, 100)
1165 .
"', " . floatval($count) .
", '" . floatval($amount) .
"', 1)";
1166 $sth = db_query($insertSQL, $dbh);
1168 throw new exception (
"Insert query failed.", 2);
1171 $updateFeatureToPendingSQL =
"update cubillfeaturedetail set billing_status = " . ($billingStatus == 0 ?
"1" :
"5")
1172 .
" where id = " . intval($featureDetailId);
1173 $sth = db_query($updateFeatureToPendingSQL, $dbh);
1175 throw new exception (
"Update query failed.", 3);
1180 $insertSQL =
"insert into cubillpreinvoice (feature_detail_id, sales_item_id, billing_system_id, cu_id, billing_date, 1181 description, quantity, amount) 1182 values (" . intval($featureDetailId) .
", " . intval($salesItemId) .
", '" . prep_save($billingItem, 20)
1183 .
"', '" . prep_save($cu, 10) .
"', '" . prep_save($reportDate) .
"', '" . prep_save($description, 100)
1184 .
"', " . floatval($count) .
", '" . floatval($amount) .
"')";
1185 $sth = db_query($insertSQL, $dbh);
1187 throw new exception (
"Insert query failed.", 4);
1191 $findSQL =
"select amount from cubillpreinvoice where feature_detail_id = " . intval($featureDetailId) .
" and billing_part = 1";
1192 $sth = db_query($findSQL, $dbh);
1194 throw new exception (
"Find query failed.", 5);
1196 $findAmount = db_fetch_row($sth, 0)[0];
1197 $amount = $amount - floatval($findAmount);
1199 $description = $record[
"actualDescription"] .
" (remainder)";
1200 $insertSQL =
"insert into cubillpreinvoice (feature_detail_id, sales_item_id, billing_system_id, cu_id, billing_date, 1201 description, quantity, amount, billing_part) 1202 values (" . intval($featureDetailId) .
", " . intval($salesItemId) .
", '" . prep_save($billingItem, 20)
1203 .
"', '" . prep_save($cu, 10) .
"', '" . prep_save($reportDate) .
"', '" . prep_save($description, 100)
1204 .
"', " . floatval($count) .
", '" . floatval($amount) .
"', 2)";
1205 $sth = db_query($insertSQL, $dbh);
1207 throw new exception (
"Insert query failed.", 6);
1212 if ($billingStatus != 4 && $billingStatus != 0) {
1213 switch($frequency) {
1224 if ($record[
"productName"] ==
"ESP" && $description ==
"") {
1225 $description = $record[
"actualDescription"] .
" ($addendum)";
1227 $insertSQL =
"insert into cubillpreinvoice (feature_detail_id, sales_item_id, billing_system_id, cu_id, billing_date, description, quantity, amount) 1228 values (" . intval($featureDetailId) .
", " . intval($salesItemId) .
", '" . prep_save($billingItem, 20)
1229 .
"', '" . prep_save($cu, 10) .
"', '" . prep_save($reportDate) .
"', '" . prep_save($description, 100)
1230 .
"', " . floatval($count) .
", '" . floatval($amount) .
"')";
1231 $sth = db_query($insertSQL, $dbh);
1233 throw new exception (
"Insert query failed.", 7);
1237 if ($partiallyBilledDate !=
"") {
1238 $dateTime =
new DateTime($partiallyBilledDate);
1239 $dateFormat = $dateTime->format(
"m/d/Y");
1240 $description =
"Credit paid $dateFormat";
1241 $amount = 0 - floatval($record[
"partiallyBilledAmount"]);
1243 $insertSQL =
"insert into cubillpreinvoice (feature_detail_id, sales_item_id, billing_system_id, cu_id, billing_date, description, quantity, amount) 1244 values (" . intval($featureDetailId) .
", " . intval($salesItemId) .
", '" . prep_save($billingItem, 20)
1245 .
"', '" . prep_save($cu, 10) .
"', '" . prep_save($reportDate) .
"', '" . prep_save($description, 100)
1246 .
"', 1, '" . floatval($amount) .
"')";
1247 $sth = db_query($insertSQL, $dbh);
1249 throw new exception (
"Insert query failed.", 8);
1256 $setupRecurring = getSetupRecurring();
1257 $additionalUpdate = ($billingStatus == 4 ?
", billing_status = 5" :
"");
1258 $processSQL =
"update cubillfeaturedetail set processing_flag = 20 $additionalUpdate where id = " . intval($featureDetailId);
1259 $sth = db_query($processSQL, $dbh);
1261 throw new exception (
"Update query failed.", 9);
1264 $returnArray = array(
"status" =>
"000",
"error" =>
"");
1266 }
catch (exception $e) {
1267 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getCode());
1269 return $returnArray;
1282 function CalcMembership($membershipCount, $record) {
1283 $calcCount = $membershipCount;
1287 $amount = $record[
"fixed"];
1288 if ( $record[
"qty1"] > 0 ) {
1289 $variableAmount = (int) ceil( ( $calcCount - $record[
"qty2"] ) / $record[
"qty1"] );
1291 $variableAmount = 0;
1294 if ( $variableAmount > 0 ) {
1295 $amount += $variableAmount * $record[
"variable"];
1298 return array(
"amount" => $amount,
"count" => $count);
1311 function CalcSunset($membershipCount, $record) {
1313 if ($record[
"calcId"] == 3) {
1314 $calcCount = $membershipCount;
1315 $count = $calcCount;
1316 if ( $calcCount > 2500 ) {
1317 throw new exception (
"Don't process it", 1);
1322 if ( $count < $record[
"qty1"] ) {
1323 $amount = $record[
"variable"] * $count;
1328 $calcCount = $membershipCount;
1329 $count = $calcCount;
1330 if ( $calcCount <= 2500 ) {
1331 throw new exception (
"Don't process it", 2);
1336 $amount = $record[
"fixed"];
1337 if ( $count >= $record[
"qty1"] && $record[
"qty1"] != 0) {
1338 $variableAmount = (int) ceil( ( $count - $record[
"qty2"] ) / $record[
"qty1"] );
1343 if ( $variableAmount > 0 ) {
1344 $amount += $variableAmount * $record[
"variable"];
1348 $returnArray = array(
"processIt" =>
true,
"amount" => $amount,
"count" => $count);
1349 }
catch (exception $e) {
1350 $returnArray = array(
"processIt" =>
false);
1353 return $returnArray;
1367 function CalcEstatements($eStatementCount, $record) {
1368 $count = intval($eStatementCount);
1369 $bottom = intval($record[
"qty1"]);
1370 $top = intval($record[
"qty2"]);
1371 $cu = $record[
"cu"];
1372 $calcID = $record[
"calcId"];
1373 $rate = floatval($record[
"variable"]);
1377 $dontAddRecord =
false;
1378 $range = $top - $bottom;
1379 if ($top != -1 && $count >= $range + $bottom) {
1384 }
else if ($count < $bottom) {
1385 $warning =
"No record created at this level of eStatements because there isn't enough count. (CALC ID: $calcID, cu: $cu)";
1386 $dontAddRecord =
true;
1389 $count = $count - $bottom;
1394 $amount = $count * $rate;
1396 return array(
"amount" => $amount,
"count" => $count,
"dontAddRecord" => $dontAddRecord,
"warning" => $warning);
1409 function CalcIVR($ivrCounts, $record) {
1412 $ivrCounts = $record[
"aDNID"] !=
"" ? (HCU_array_key_exists($record[
"aDNID"], $ivrCounts) ? $ivrCounts[$record[
"aDNID"]] : array()) :
1413 (HCU_array_key_exists(
"undivided", $ivrCounts) ? $ivrCounts[
"undivided"] : array());
1414 $ivrCountMinutes = HCU_array_key_exists(
"minutes", $ivrCounts) ? $ivrCounts[
"minutes"] : 0;
1415 $ivrCountCalls = HCU_array_key_exists(
"calls", $ivrCounts) ? $ivrCounts[
"calls"] : 0;
1417 switch($record[
"calcId"]) {
1419 $count = $ivrCountMinutes > $record[
"qty1"] ? $record[
"qty1"] : $ivrCountMinutes;
1420 $amount1 = $record[
"variable"] * $count;
1423 $count = $ivrCountMinutes > $record[
"qty1"] ? $ivrCountMinutes - $record[
"qty1"] : 0;
1424 $amount1 = $record[
"variable"] * $count;
1427 $count = $ivrCountMinutes;
1428 $amount1 = $record[
"variable"] * $count;
1431 $count = $ivrCountCalls;
1432 $amount1 = $record[
"variable"] * $count;
1436 return array(
"count" => $count,
"amount" => $amount1);
1453 function ProcessTimetrack($dbh, $timetrackCount, $reportDate) {
1457 $sql =
"select billing_template from cubillsalesitem where id = " . intval($salesItemId);
1458 $sth = db_query($sql, $dbh);
1460 throw new exception (
"Select query failed.", 1);
1463 $timetrackTemplate = db_num_rows($sth) > 0 ? db_fetch_row($sth, 0)[0] :
"";
1464 if ($timetrackTemplate ==
"") {
1465 $regularRate = 85.0;
1468 $timetrackTemplate = HCU_JsonDecode($timetrackTemplate);
1469 $regularRate = floatval($timetrackTemplate[
"fixed"]);
1470 $rushRate = floatval($timetrackTemplate[
"variable"]);
1473 foreach($timetrackCount as $cu => $timetrackRecords) {
1474 $cu = trim(strtoupper($cu));
1475 $sql =
"insert into cubillpreinvoice (feature_detail_id, sales_item_id, billing_system_id, cu_id, billing_date, description, quantity, amount) values ";
1476 $sqlParts = array();
1480 $timetrackBillingItems = getTimetrackBillingItems();
1481 foreach($timetrackRecords as $counts) {
1483 $staffName = $counts[
"staff"];
1485 if ($staffName ==
"test") {
1489 $billAmount = floatval($counts[
"billable"]) * floatval($regularRate);
1490 $rushAmount = floatval($counts[
"rushable"]) * floatval($rushRate);
1491 $billCount = floatval($counts[
"billable"]);
1492 $rushCount = floatval($counts[
"rushable"]);
1496 $staffKey = strtolower($staffName);
1497 $billingItemDescription = HCU_array_key_value($staffKey, $timetrackBillingItems);
1498 $actualDescription = $billingItemDescription !==
false ? trim($billingItemDescription) :
"";
1499 $rushDescription =
"$actualDescription (Rush)";
1501 if($billAmount > 0) {
1502 $sqlParts[] =
"(0, " . intval($salesItemId) .
", '" . prep_save($staffName, 20) .
"', '" . prep_save($cu, 10)
1503 .
"', '" . prep_save($reportDate) .
"', '', " . floatval($billCount) .
", " . floatval($billAmount) .
")";
1507 if ($rushAmount > 0) {
1508 $sqlParts[] =
"(0, " . intval($salesItemId) .
", '" . prep_save($staffName, 20) .
"', '" . prep_save($cu, 10)
1509 .
"', '" . prep_save($reportDate) .
"', '" . prep_save($rushDescription, 100) .
"', " . floatval($rushCount) .
", " . floatval($rushAmount) .
")";
1515 $sql .= implode(
",", $sqlParts);
1516 $sth = db_query($sql, $dbh);
1518 throw new exception (
"Insert query failed.", 2);
1523 $returnArray = array(
"status" =>
"000",
"error" =>
"");
1525 }
catch (exception $e) {
1526 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage());
1528 return $returnArray;
1544 function CalcSSL($assetCount, $record, $cu) {
1545 $rangeFrom = floatval($record[
"qty1"]);
1546 $rangeTo = floatval($record[
"qty2"]);
1547 $cu = $record[
"cu"];
1550 $amount = $record[
"fixed"];
1554 if ($assetCount >= $rangeFrom && ($rangeTo == -1 || $assetCount < $rangeTo)) {
1557 if ($rangeTo != -1) {
1558 $warning =
"SSL record not in range: $rangeFrom to $rangeTo for CU: $cu";
1560 $warning =
"SSL record not created because it is less than $rangeFrom for CU: $cu";
1564 return array(
"processIt" => $processIt,
"count" => $count,
"amount" => $amount,
"warning" => $warning);
1578 function CalcIpay($iPayCount, $record) {
1579 $variableAmount = floatval($record[
"variable"]);
1580 $fixedAmount = floatval($record[
"fixed"]);
1581 $calcId = intval($record[
"calcId"]);
1585 $count = intval($iPayCount[
"Total Transactions Plus"]);
1586 $amount = $variableAmount == $iPayCount[
"Per Transaction"] ? $variableAmount * $count : 0;
1587 $amount = $amount < $iPayCount[
"Monthly Minimum"] ? 0 : $amount;
1590 $count = intval($iPayCount[
"Stop Payment Count"]);
1591 $amount = $variableAmount * $count;
1598 $count = intval($iPayCount[
"NSF Count"]);
1599 $amount = $variableAmount * $count;
1602 $count = intval($iPayCount[
"Check Copies Count"]);
1603 $amount = $variableAmount * $count;
1607 $amount = floatval($iPayCount[
"Per Transaction"]) * intval($iPayCount[
"Total Transactions Plus"]);
1608 $amount = $fixedAmount == $iPayCount[
"Monthly Minimum"] ? ($amount < $fixedAmount ? $fixedAmount : 0) : 0;
1613 $processIt = $amount > 0;
1615 return array(
"processIt" => $processIt,
"count" => $count,
"amount" => $amount);
1629 function CalcConditionalMemb($membershipCount, $record) {
1630 $memberDivide = floatval($record[
"qty1"]);
1631 $ifLess = floatval($record[
"fixed"]);
1632 $ifMore = floatval($record[
"variable"]);
1633 $cu = $record[
"cu"];
1636 $value = $membershipCount <= $memberDivide ? $ifLess : $ifMore;
1638 return array(
"count" => 1,
"amount" => $value);
1652 function GetEstatementCounts($cutoffDate, $cutoffTime, $devmode) {
1656 $q =
"cdate=$cutoffDate&ctime=$cutoffTime";
1659 $odysseyServer =
'my';
1660 $servers = array(
'www3',
'www5',
'www6', $odysseyServer);
1662 foreach ($servers as $ask) {
1664 $isOdyssey = $ask == $odysseyServer;
1666 $cmd =
"https://$ask.homecu.net/monitor/cgi-bin/stmntcount.mp?csv=raw&$q";
1668 $cmd =
"https://$ask.homecu.net/hcuadm/stmntcount.pl?csv=raw&$q";
1671 $results = $isOdyssey ? RunCurlOdyssey($cmd, $devmode) : RunCurlMammoth($cmd);
1672 switch ($results[
"httpCode"]) {
1675 throw new exception (
"Server $ask timed out.", 1);
1678 $rawresp = $results[
"rawresp"];
1681 throw new exception (
"Status not found.", 2);
1689 $lines = explode(
"\r\n",$rawresp);
1691 foreach($lines as $line) {
1692 if (trim($line) !=
"") {
1703 $explodedLine = explode(
"\t", $line);
1705 if ($explodedLine[0] !=
"s") {
1709 $cu = strtolower(trim($explodedLine[1]));
1710 $count = intval($explodedLine[2]);
1713 if (isset($cuList[$cu])) {
1714 $cuList[$cu] += $count;
1716 $cuList[$cu] = $count;
1723 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"counts" => $cuList);
1724 }
catch (exception $e) {
1725 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"counts" => array());
1728 return $returnArray;
1740 function GetIVRCounts($reportStartDate, $reportEndDate) {
1743 $cmd =
"http://ivr.homecu.net/hcuadm/ivr_report?rv=RAW&byDNID=Y&reportstartdate=$reportStartDate&reportenddate=$reportEndDate";
1746 $results = RunCurlMammoth($cmd);
1747 switch ($results[
"httpCode"]) {
1750 throw new exception (
"IVR server timed out.", 1);
1753 $rawresp = $results[
"rawresp"];
1756 throw new exception (
"Status not found.", 2);
1763 $lines = explode(
"\r\n",$rawresp);
1764 foreach($lines as $line) {
1765 if (trim($line) !=
"") {
1772 $explodedLine = explode(
",", $line);
1775 if ( $explodedLine[0] ==
"T" ) {
1779 $cuList[] = array(
"cu" => trim($explodedLine[0]),
"DNID" => $explodedLine[1],
"calls" => $explodedLine[2],
"minutes" => $explodedLine[3] );
1784 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"counts" => $cuList);
1785 }
catch (exception $e) {
1786 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"counts" => array());
1789 return $returnArray;
1801 function GetIPayCounts($iPayReportDate, $devmode) {
1804 $cmd =
"https://my.homecu.net/hcuadm/miPayReport.prg?operation=csv&month=$iPayReportDate";
1807 $results = RunCurlOdyssey($cmd, $devmode);
1808 switch ($results[
"httpCode"]) {
1811 throw new exception (
"Monitor server timed out.", 1);
1814 $rawresp = $results[
"rawresp"];
1817 throw new exception (
"Status not found.", 2);
1823 $lines = explode(
"\n",$rawresp);
1826 foreach($lines as $line) {
1828 if (trim($line) ==
"") {
1834 $header = str_getcsv($line);
1838 $parts = str_getcsv($line);
1841 if ( $parts[0] ==
"E") {
1845 $parts = array_combine($header, $parts);
1847 $cuList [$parts[
"CU Code"]] = $parts;
1851 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"counts" => $cuList);
1852 }
catch (exception $e) {
1853 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"counts" => array());
1856 return $returnArray;
1868 function GetTimetrackCounts($reportDate, $endDate) {
1871 $url =
"https://www.homecu.net/timetrack/main.php?mainAction=report&month=$reportDate";
1873 $url .= $endDate ==
"" ?
"" :
"&endmonth=$endDate";
1875 $results = RunCurlMammoth($url);
1876 switch ($results[
"httpCode"]) {
1879 throw new exception (
"Monitor server timed out.", 1);
1882 $raw = $results[
"rawresp"];
1885 throw new exception (
"Status not found.", 2);
1894 foreach(explode(
"\r\n", $raw) as $line) {
1895 $lineParts = explode(
",", $line);
1896 if ($lineParts[0] ==
"ERROR") {
1897 $errors[] = $lineParts[1];
1898 }
else if ($first) {
1901 $cu = strtolower(trim($lineParts[0]));
1905 if (!isset($counts[$cu])) {
1906 $counts[$cu] = array();
1909 $counts[$cu][] = array(
"staff" => $lineParts[1],
"rushable" => $lineParts[2],
"billable" => $lineParts[3]);
1914 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"counts" => $counts);
1915 }
catch (exception $e) {
1916 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"counts" => array());
1919 return $returnArray;
1928 function GetTimetrackCookie () {
1934 $date =
"1990-01-01";
1935 $userName =
"TEST DUDE";
1937 $ticketDomain =
"www.homecu.net";
1940 $hash = MD5($secret . MD5(join (
':', array($secret, $id, $month, $year, $day, $date, $userName, $ipAddress, $now))));
1941 $cookie =
"c_sid=$id&c_mno=$month&c_syr=$year&c_day=$day&c_date=$date&c_nme=$userName&Cip=$ipAddress&Ctime=$now&Ch=$hash";
1957 function GetMonitorBillingList($dbh, $sysenv) {
1959 extract ($sysenv[
"BILL"]);
1961 $reportDate = isset($reportDate) ? trim($reportDate) :
"";
1962 $monthBit = isset($monthBit) ? trim($monthBit) :
"";
1964 $url =
"https://my.homecu.net/billing/index.prg?operation=getPotentiallyFromDifferentServer&reportDate=$reportDate&monthBit=$monthBit";
1965 $results = RunCurlOdyssey($url, $sysenv[
"devmode"]);
1967 switch ($results[
"httpCode"]) {
1970 throw new exception (
"Monitor server timed out.", 3);
1973 $raw = $results[
"rawresp"];
1976 throw new exception (
"Status not found.", 4);
1979 if (trim($raw) ==
"") {
1980 throw new exception (
"Monitor has no results.", 1);
1983 $returnArray = HCU_JsonDecode($raw);
1984 if (count($returnArray) == 0) {
1985 throw new exception (
"Monitor has no results.", 2);
1987 }
catch (exception $e) {
1988 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"record" => array(),
"membershipCounts" => array());
1990 return $returnArray;
2007 function GetPotentiallyFromDifferentServer($dbh, $sysenv) {
2009 global $SYS_TYPE_CLOSED;
2011 extract ($sysenv[
"BILL"]);
2014 $recordResults = array();
2015 $memberResults = array();
2017 $sql =
"select fd.id \"featureDetailId\", fd.sales_order_detail_id \"detailId\", trim(so.cu_id) \"cu\", trim(sd.prod_id) \"productName\", 2018 trim(si.billing_system_id) \"billingId\", si.calc_id \"calcId\", trim(fd.billing_template) \"billingTemplate\", si.id \"salesItemId\", 2019 trim(si.display_name) \"actualDescription\", fd.bill_half_boolean \"billHalf\", fd.billing_status \"billingStatus\", 2020 fd.partially_billed_date \"partiallyBilledDate\", fd.partially_billed_amount \"partiallyBilledAmount\", trim(p.home_cu_desc) \"prodText\", 2021 trim(ci.name) \"cuName\", fd.frequency \"frequency\", fd.months_to_bill \"monthsToBill\" 2022 from cubillfeaturedetail fd 2023 left join cubillsalesorderdetail sd on fd.sales_order_detail_id = sd.id 2024 left join cubillsalesorder so on sd.sales_order_id = so.id 2025 left join cuprodlist p on sd.prod_id = p.home_cu_code 2026 left join cubillsalesitem si on fd.sales_item_id = si.id 2027 left join cuinfo ci on lower(so.cu_id) = lower(ci.user_name) 2029 (fd.start_date <= '" . prep_save($reportDate) .
"' and (fd.end_date is null or fd.end_date >= '" . prep_save($reportDate) .
"') 2030 and fd.billing_status in (2,3) and fd.frequency between 0 and 2 and (fd.frequency = 0 or fd.months_to_bill & " . intval($monthBit) .
" <> 0)) 2031 or (fd.partially_billed_date is not null and fd.billing_status in (0,4) and fd.partially_billed_date = '" . prep_save($reportDate) .
"')) 2032 order by so.cu_id, si.calc_id";
2034 $sth = db_query($sql, $dbh);
2036 throw new exception (
"Record query failed.", 1);
2038 $recordResults = db_fetch_all($sth);
2039 $recordResults = $recordResults ===
false ? array() : $recordResults;
2041 $sql =
"select trim(user_name) \"cu\", dec_31_mem \"membership\", dec_31_assets \"assets\" from cuinfo";
2042 $sth = db_query($sql, $dbh);
2044 throw new exception (
"Member query failed.", 2);
2046 $memberResults = db_fetch_all($sth);
2047 $memberResults = $memberResults ===
false ? array() : $memberResults;
2049 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"record" => $recordResults,
"membershipCounts" => $memberResults);
2051 }
catch (exception $e) {
2052 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"record" => array(),
"membershipCounts" => array());
2054 return $returnArray;
2068 function DownloadRunBillingFiles($dbh, $sysenv) {
2071 $cmd =
"cd /home/homecu/tmp >/dev/null; 2072 mkdir runBilling >/dev/null; 2073 cp .runBilling* runBilling >/dev/null; 2074 tar -zcvf runBilling.tar.gz runBilling >/dev/null;";
2075 $results = shell_exec($cmd);
2076 if ($results !=
"") {
2077 throw new exception ($results, 1);
2080 $file =
"/home/homecu/tmp/runBilling.tar.gz";
2081 $dir =
"/home/homecu/tmp/runBilling";
2083 if (!file_exists($file)) {
2084 throw new exception (
"File doesn't exist.", 1);
2087 header(
'Content-Description: File Transfer');
2088 header(
'Content-Type: application/octet-stream');
2089 header(
'Content-Disposition: attachment; filename="' . basename($file) .
'"');
2090 header(
'Expires: 0');
2091 header(
'Cache-Control: must-revalidate');
2092 header(
'Pragma: public');
2093 header(
'Content-Length: ' . filesize($file));
2097 }
catch (exception $e) {
2098 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage());
2100 return $returnArray;