8 $sharedLibrary = dirname(__FILE__) .
"/../../shared/library";
9 $monLibrary = dirname(__FILE__) .
"/../../monitor/library";
10 $billingLibrary = dirname(__FILE__) .
"/../library";
12 require_once(
"$monLibrary/cu_top.i");
13 require_once(
"$billingLibrary/globals.i");
15 $MY_TICKET =
"15c9ce293bd3f41b761c21635b14fa06";
17 header(
"Content-Type: text/xml" );
20 $xmlString = file_get_contents(
"php://input");
22 ini_set(
"soap.wsdl_cache_enabled",
"0");
27 $mTime = file_exists(
"/tmp/wsdl.xml") ? filemtime(
"/tmp/wsdl.xml") : false;
28 $yesterday = strtotime(
"-1 day");
29 if ($mTime ===
false || $mTime - $yesterday > 86400) {
31 $wfpw = fopen(
"/tmp/wsdl.xml",
"w");
33 curl_setopt($ch, CURLOPT_URL,
"https://test.developer.intuit.com/QBWC/TroubleshootWebServiceFS/Service.asmx?wsdl");
34 curl_setopt($ch, CURLOPT_RETURNTRANSFER,
true);
36 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,
false);
37 curl_setopt($ch, CURLOPT_RETURNTRANSFER,
true);
38 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,
false);
39 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
41 $response = curl_exec($ch);
43 fwrite($wfpw, $response);
50 $WSDL =
"http://developer.intuit.com/uploadedFiles/Support/QBWebConnectorSvc.wsdl";
51 $WSDL =
"https://test.developer.intuit.com/QBWC/TroubleshootWebServiceFS/Service.asmx?wsdl";
52 $WSDL =
"file:///tmp/wsdl.xml";
53 $soapServer =
new SoapServer($WSDL);
60 $soapServer->addFunction(
"serverVersion" );
61 $soapServer->addFunction(
"clientVersion" );
62 $soapServer->addFunction(
"authenticate" );
63 $soapServer->addFunction(
"sendRequestXML" );
64 $soapServer->addFunction(
"receiveResponseXML" );
65 $soapServer->addFunction(
"connectionError" );
66 $soapServer->addFunction(
"getLastError" );
67 $soapServer->addFunction(
"closeConnection" );
81 if (isset($xmlString)) {
82 $soapServer->handle ($xmlString);
90 function clientVersion( $params ) {
91 $reply = array(
"clientVersionResult" =>
"" );
99 function serverVersion( $params ) {
100 $xsi =
"http://www.w3.org/2001/XMLSchema-instance";
101 $soap =
"http://schemas.xmlsoap.org/soap/envelope/";
102 $outputXML =
'<soap:Envelope xmlns:xsi="' . $xsi .
'" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="' . $soap .
'"> 104 <serverVersionResponse xmlns="http://developer.intuit.com/"> 105 <serverVersionResult>Mike Is Grate</serverVersionResult> 106 </serverVersionResponse> 110 $reply = array(
"serverVersionResult" =>
"0.9" );
114 function authenticate($params) {
116 global $MY_TICKET, $dbh, $cookieName;
117 $user = $params->strUserName;
118 $pass = $params->strPassword;
122 $returnResult = array();
123 $returnResult[
"string"][0] =
"";
124 $returnResult[
"string"][1] =
"none";
126 $results = GetCurrentRun($dbh);
127 if ($results[
"status"] !==
"000") {
128 throw new exception ($results[
"error"], 1);
130 $txnDate = $results[
"date"];
132 $returnResult[
"string"][1] =
"nvu";
134 if (!CheckPerm($dbh, $user,
"billing_qbwc", $_SERVER[
'REMOTE_ADDR'])) {
135 throw new exception (
"Permissions failure", 2);
139 $lowerUser = strtolower($user);
140 $sql =
"select user_name, passwd, failedremain from dmsmonitorusers where lower(user_name) = '" . prep_save($lowerUser, 12) .
"'";
141 $sth = db_query($sql, $dbh);
143 throw new exception (
"Select query failed.", 3);
145 $record = db_num_rows($sth) > 0 ? db_fetch_assoc($sth, 0) : false;
146 if ($record ===
false) {
147 throw new exception (
"Invalid Username or Password", 4);
150 $user = $record[
"user_name"];
151 $password = $record[
"passwd"];
152 $failedCount = $record[
"failedremain"];
154 $password = preg_replace(
"/ +$/",
"", $password);
157 if ($password != crypt($pass, $password)) {
161 if ( $failedCount < 0 ) {
164 $sql =
"update dmsmonitorusers set failedremain = " . intval($failedCount) .
", failedlogin = to_char(now(),'YYYY/MM/DD HH24:MI:SS') 165 where lower(user_name) = '" . prep_save($lowerUser, 12) .
"'";
166 $sth = db_query ($sql, $dbh);
168 throw new exception (
"Update query failed.", 7);
171 if ( $failedCount <= 0 ) {
172 throw new exception (
"Account is Locked", 5);
174 throw new exception (
"Invalid Username or Password", 6);
179 $sql =
"update dmsmonitorusers set failedremain = '" . intval($defaultRetries) .
"', priorlogin = lastlogin, lastlogin = to_char(now(),'YYYY/MM/DD HH24:MI:SS') 180 where lower(user_name) = '" . prep_save($lowerUser, 12) .
"'";
181 $sth = db_query($sql, $dbh);
183 throw new exception (
"Update query failed.", 8);
187 $returnResult[
"string"][1] =
"";
193 $ip = $_SERVER[
"REMOTE_ADDR"];
194 $hash = MD5( $MY_TICKET . MD5( join(
":", array( $MY_TICKET, $now, $ip ) ) ) );
195 $ticketString =
"t=$now&i=$ip&h=$hash";
197 $returnResult[
"string"][0] = $ticketString;
200 $sql =
"update cubillmonth set processing_flag = 110, last_step_date = now() where billing_date = '" . prep_save($txnDate) .
"'";
201 $sth = db_query($sql, $dbh);
203 throw new exception (
"Update query failed.", 9);
207 WriteErrorMsg( $dbh, $txnDate,
"",
"");
208 }
catch (exception $e) {
209 WriteErrorMsg( $dbh, $txnDate, $e->getCode(), $e->getMessage());
212 return array(
"authenticateResult" => $returnResult );
220 function GetCurrentRun($dbh) {
222 $sql =
"select billing_date from cubillmonth where started_date is not null and processing_flag in (100,110,120) order by billing_date limit 1";
223 $sth = db_query($sql, $dbh);
225 throw new exception (
"Select query failed.", 1);
227 $date = db_num_rows($sth) > 0 ? db_fetch_row($sth, 0)[0] :
null;
228 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"date" => $date);
229 }
catch (exception $e) {
230 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage());
239 function WriteErrorMsg( $dbh, $runDate, $errorCode, $errorMsg) {
243 $sql =
"update cubillmonth set last_error_code = '" . prep_save($errorCode, 20) .
"', last_error_msg = '" . prep_save($errorMsg, 250)
244 .
"' where billing_date = '" . prep_save($runDate) .
"'";
245 $sth = db_query($sql, $dbh);
247 throw new exception(
"Update query failed.", 1);
250 $returnArray = array(
"status" =>
"000",
"error" =>
"");
251 }
catch (exception $e) {
252 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage());
257 function sendRequestXML($params) {
259 global $dbh, $cookieName;
261 $billingEmailFlag = GetBillingEmailFlag();
263 $sql =
"select upper(cu_id) \"cu\", coalesce(flags, 0) & " . intval($billingEmailFlag) .
" \"flag\" from cubillsalesorder";
264 $sth = db_query($sql, $dbh);
266 throw new exception (
"Select query failed.", 1);
268 $results = db_fetch_all($sth);
269 $results = $results ===
false ? array() : $results;
270 $cuPreferredDeliveryMap = array();
271 foreach($results as $result) {
272 $cuPreferredDeliveryMap[$result[
"cu"]] = $result[
"flag"];
275 $results = GetCurrentRun($dbh);
276 if ($results[
"status"] !==
"000") {
277 throw new exception ($results[
"error"], 2);
280 $txnDate = $results[
"date"];
284 $sql =
"select pi.cu_id \"cu\", pi.description \"description\", pi.billing_system_id \"billingItem\", pi.quantity \"quantity\", pi.amount \"amount\" 285 from cubillpreinvoice pi 286 where pi.billing_date = '" . prep_save($txnDate) .
"' and pi.processing_flag in (0, 10) 287 order by pi.cu_id, pi.billing_system_id";
288 $sth = db_query($sql, $dbh);
290 throw new exception (
"Select query failed.", 3);
292 $recordsToProcess = db_fetch_all($sth);
293 $recordsToProcess = $recordsToProcess ===
false ? array() : $recordsToProcess;
295 if (trim($params->ticket) ==
"") {
296 throw new exception (
"Missing access ticket", 4);
299 if (!CheckValidAccess($params->ticket)) {
300 throw new exception (
"Access failure.", 5);
304 $xmlRequest =
"<?xml version=\"1.0\" encoding=\"utf-8\"?> 305 <?qbxml version=\"11.0\"?> 307 <QBXMLMsgsRq onError=\"stopOnError\"> 312 $invoiceXMLInnards =
"";
315 foreach($recordsToProcess as $record) {
317 $cu = trim(strtolower($record[
"cu"]));
318 $emailCu = isset($cuPreferredDeliveryMap[strtoupper($cu)]) && $cuPreferredDeliveryMap[strtoupper($cu)] != 0;
319 $printCu = $emailCu ?
"false" :
"true";
320 $emailCu = $emailCu ?
"true" :
"false";
321 $emailCuXML =
"<IsToBePrinted>$printCu</IsToBePrinted> 322 <IsToBeEmailed>$emailCu</IsToBeEmailed>";
324 $invoiceXML =
"<InvoiceAddRq> 327 <FullName>$cu</FullName> 329 <TxnDate>$txnDate</TxnDate> 333 <IncludeRetElement>CustomerRef</IncludeRetElement> 337 else if (trim(strtolower($record[
"cu"])) != $cu) {
341 $billingItem = $record[
"billingItem"];
342 $quantity = floatval($record[
"quantity"]);
343 $amount = sprintf(
'%0.2f', $record[
"amount"]);
344 $descLine = $record[
"description"] ==
"" ?
"" :
"<Desc>" . $record[
"description"] .
"</Desc>";
345 $invoiceXMLInnards .=
"<InvoiceLineAdd> 347 <FullName>$billingItem</FullName> 350 <Quantity>$quantity</Quantity> 351 <Amount>$amount</Amount> 356 if ($invoiceXMLInnards !=
"") {
357 $invoiceXML = str_replace(
"#", $invoiceXMLInnards, $invoiceXML);
358 $xmlRequest = str_replace(
"#", $invoiceXML, $xmlRequest);
363 if ($xmlRequest ==
"") {
366 $sql =
"update cubillmonth set processing_flag = 120, last_step_date = now() where billing_date = '" . prep_save($txnDate) .
"'";
367 $sth = db_query($sql, $dbh);
369 throw new exception (
"Update query failed.", 6);
372 $upperCu = strtoupper($cu);
374 $sql =
"update cubillpreinvoice set processing_flag = 10 375 where upper(cu_id) = '" . prep_save($upperCu, 10) .
"' and billing_date = '" . prep_save($txnDate) .
"'";
376 $sth = db_query($sql, $dbh);
378 throw new exception (
"Update query failed.", 7);
382 }
catch (exception $e) {
384 WriteErrorMsg( $dbh, $txnDate, $e->getCode(), $e->getMessage());
386 return array(
"sendRequestXMLResult" => $xmlRequest );
393 function CheckValidAccess( $string ) {
399 parse_str( $string, $parts );
402 $ip = $_SERVER[
"REMOTE_ADDR"];
404 $hash = MD5( $MY_TICKET . MD5( join(
":", array( $MY_TICKET, $parts[
"t"], $parts[
"i"] ) ) ) );
406 if ( $hash == $parts[
"h"] ) {
408 if ( $ip != $parts[
"i"] ) {
413 if ( ($now - $parts[
"t"]) > ( 20 * 60 ) ) {
443 function receiveResponseXML( $params ) {
448 $responseValue = 100;
450 $results = GetCurrentRun($dbh);
451 if ($results[
"status"] !==
"000") {
452 throw new exception ($results[
"error"], 1);
454 $txnDate = $results[
"date"];
456 if (trim($params->ticket) ==
"") {
457 throw new exception (
"Missing access ticket", 2);
460 if (!CheckValidAccess($params->ticket)) {
461 throw new exception (
"Access failure.", 3);
465 $hResult = $params->hresult;
466 $message = $params->message;
468 if ( strlen( $hResult ) > 0 || strlen( $message ) > 0 ) {
469 throw new exception (
"QB Error: $message", 4);
473 $xmlString = $params->response;
475 $xmlObject = simplexml_load_string( $xmlString );
477 if ( strlen( $txnDate ) > 0 ) {
479 $cuProcessed = strtoupper( trim( $xmlObject->QBXMLMsgsRs->InvoiceAddRs->InvoiceRet->CustomerRef->FullName ) );
481 if ( strlen( $cuProcessed ) ) {
483 $sql =
"update cubillpreinvoice set processing_flag = 20 where processing_flag = 10 and upper(cu_id) = '" . prep_save($cuProcessed, 10) .
"' 484 and billing_date = '" . prep_save($txnDate) .
"'";
485 $sth = db_query($sql, $dbh);
487 throw new exception (
"Update query failed.", 5);
491 $statusCode = trim( $xmlObject->QBXMLMsgsRs->InvoiceAddRs[
"statusCode"] );
493 if ( strlen( $statusCode ) ) {
494 $statusMsg = trim( $xmlObject->QBXMLMsgsRs->InvoiceAddRs[
"statusMessage"] );
497 throw new exception (
"QB returned error: $statusCode - $statusMsg", 6);
498 $responseValue = 100;
502 $sql =
"select distinct cu_id \"cu\", processing_flag \"flag\" from cubillpreinvoice where billing_date = '" . prep_save($txnDate) .
"'";
503 $sth = db_query($sql, $dbh);
505 throw new exception (
"Select query failed.", 7);
507 $queryResults = db_fetch_all($sth);
508 $queryResults = $queryResults ===
false ? array() : $queryResults;
513 foreach($queryResults as $record) {
514 $flag = $record[
"flag"];
516 if (in_array($flag, array(0, 10))) {
524 $total = $pending + $complete;
526 $responseValue = floor( 100 * $complete / $total );
527 if ( $responseValue == 100 && $pending > 0 ) {
532 if (!db_work ($dbh, HOMECU_WORK_BEGIN)) {
533 throw new exception(
"begin query failed.", 8);
536 $sql =
"update cubillpreinvoice set processing_flag = 25 where processing_flag = 20 and billing_date = '" . prep_save($txnDate) .
"'";
537 $sth = db_query($sql, $dbh);
539 throw new exception (
"Update query failed.", 109);
542 $sql =
"update cubillmonth set processing_flag = 120, last_step_date = now() where billing_date = '" . prep_save($txnDate) .
"'";
543 $sth = db_query($sql, $dbh);
545 throw new exception (
"Update query failed.", 110);
548 if (!db_work ($dbh, HOMECU_WORK_COMMIT)) {
549 throw new exception(
"commit query failed.", 111);
555 $responseValue = 100;
559 $responseValue = 100;
563 }
catch (exception $e) {
565 if ($e->getCode() > 100) {
566 db_work($dbh, HOMECU_WORK_ROLLBACK);
569 WriteErrorMsg( $dbh, $txnDate, $e->getCode(), $e->getMessage());
572 return array(
"receiveResponseXMLResult" => $responseValue );
575 function connectionError($params) {
580 $xsi =
"http://www.w3.org/2001/XMLSchema-instance";
581 $soap =
"http://schemas.xmlsoap.org/soap/envelope/";
582 $outputXML =
'<soap:Envelope xmlns:xsi="' . $xsi .
'" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="' . $soap .
'"> 584 <connectionErrorResponse xmlns="http://developer.intuit.com/"> 585 <connectionErrorResult>This would be an error string.</connectionErrorResult> 586 </connectionErrorResponse> 591 $sql =
"select billing_date, last_error_code from cubillmonth where started_date is not null and processing_flag IN (110, 120) order by billing_date limit 1";
592 $sth = db_query($sql, $dbh);
594 throw new exception (
"Select query failed.", 1);
596 $lastErrorCode = db_num_rows($sth) > 0 ? db_fetch_row($sth, 0)[1] :
"";
598 }
catch (exception $e) {
599 $lastErrorCode = $e->getMessage();
602 return array(
"connectionErrorResult" => $lastErrorCode );
605 function getLastError($params) {
609 $xsi =
"http://www.w3.org/2001/XMLSchema-instance";
610 $soap =
"http://schemas.xmlsoap.org/soap/envelope/";
611 $outputXML =
'<soap:Envelope xmlns:xsi="' . $xsi .
'" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="' . $soap .
'"> 613 <getLastErrorResponse xmlns="http://developer.intuit.com/"> 614 <getLastErrorResult>$lastErrorCode</getLastErrorResult> 615 </getLastErrorResponse> 620 $sql =
"select billing_date, last_error_code, last_error_msg from cubillmonth where started_date is not null and processing_flag in (110, 120) 621 order by billing_date limit 1";
622 $sth = db_query($sql, $dbh);
624 throw new exception (
"Select query failed.", 1);
626 $record = db_num_rows($sth) > 0 ? db_fetch_assoc($sth, 0) : false;
628 $date = $record ===
false ?
"" : trim($record[
"billing_date"]);
629 $code = $record ===
false ?
"" : trim($record[
"last_error_code"]);
630 $msg = $record ===
false ?
"" : trim($record[
"last_error_msg"]);
632 $errorCode = $code ==
"" ?
"" :
"Code: $code";
633 $errorMsg = $msg ==
"" ?
"" :
"- $msg";
634 $errorString = $code ==
"" ?
"" : $errorCode . $errorMsg;
635 }
catch (exception $e) {
636 $errorString =
"Code: {$e->getCode()}- {$e->getMessage()}";
639 return array(
"getLastErrorResult" => $errorString );
642 function closeConnection($params) {
646 $xsi =
"http://www.w3.org/2001/XMLSchema-instance";
647 $soap =
"http://schemas.xmlsoap.org/soap/envelope/";
648 $outputXML =
'<soap:Envelope xmlns:xsi="' . $xsi .
'" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="' . $soap .
'"> 650 <closeConnectionResponse xmlns="http://developer.intuit.com/"> 651 <closeConnectionResult>SUCCESS</closeConnectionResult> 652 </closeConnectionResponse> 657 $sql =
"select billing_date, last_error_code from cubillmonth where started_date is not null and processing_flag in (120) order by billing_date limit 1";
658 $sth = db_query($sql, $dbh);
660 throw new exception (
"Select query failed.", 1);
662 $record = db_num_rows($sth) > 0 ? db_fetch_assoc($sth, 0) : false;
663 $txnDate = $record ===
false ?
"" : trim($record[
"billing_date"]);
664 $errorCode = $record ===
false ?
"" : trim($record[
"last_error_code"]);
666 if (trim($params->ticket) ==
"") {
667 throw new exception (
"Missing access ticket", 2);
670 if (!CheckValidAccess($params->ticket)) {
671 throw new exception (
"Access failure.", 3);
674 if ($txnDate !=
"") {
676 $sql =
"select count(*) as count from cubillpreinvoice where billing_date = '" . prep_save($txnDate) .
"' and processing_flag < 25";
677 $sth = db_query($sql, $dbh);
679 throw new exception (
"Select query failed.", 4);
681 $count = db_num_rows($sth) > 0 ? intval(db_fetch_row($sth, 0)[0]) : 0;
686 if (!db_work ($dbh, HOMECU_WORK_BEGIN)) {
687 throw new exception(
"begin query failed.", 5);
690 $sql =
"update cubillpreinvoice set processing_flag = 99 where processing_flag = 25";
691 $sth = db_query($sql, $dbh);
693 throw new exception (
"Update query failed.", 106);
696 $sql =
"update cubillmonth set processing_flag = 200, last_step_date = now() where billing_date = '" . prep_save($txnDate) .
"'";
697 $sth = db_query($sql, $dbh);
699 throw new exception (
"Update query failed.", 107);
702 if (!db_work ($dbh, HOMECU_WORK_COMMIT)) {
703 throw new exception(
"commit query failed.", 108);
706 $returnResult =
"SUCCESS: $txnDate";
708 $returnResult =
"Incomplete finish";
711 $returnResult =
"Finished with error(s)";
714 }
catch (exception $e) {
716 if ($e->getCode() > 100) {
717 db_work($dbh, HOMECU_WORK_ROLLBACK);
719 $returnResult =
"Finished with error(s)";
721 WriteErrorMsg( $dbh, $txnDate, $e->getCode(), $e->getMessage());
724 return array(
"closeConnectionResult" => $returnResult );