private function cancelOrder($data, $old_order_status) { // vmdebug('my data on cancelOrder ',$data); // if(empty($data->invoice_number)) return false; $orderModel = VmModel::getModel('orders'); $orderDetails = $orderModel->getOrder($data->virtuemart_order_id); $calc = $this->getOrderCalc($orderDetails); if (!$calc) { return false; } $invoiceNumber = 'onr_' . $orderDetails['details']['BT']->order_number; $orderModel->createInvoiceNumber($orderDetails['details']['BT'], $invoiceNumber); if (!$invoiceNumber) { vmInfo('No invoice created, no reason to cancel at Avatax'); return false; } if (is_array($invoiceNumber)) { $invoiceNumber = $invoiceNumber[0]; } # if (!function_exists('EnsureIsArray')) { require VMAVALARA_PATH . DS . 'AvaTax.php'; } // include in all Avalara Scripts if (!class_exists('TaxServiceSoap')) { require VMAVALARA_CLASS_PATH . DS . 'TaxServiceSoap.class.php'; } if (!class_exists('CancelTaxRequest')) { require VMAVALARA_CLASS_PATH . DS . 'CancelTaxRequest.class.php'; } $this->newATConfig($calc); $client = new TaxServiceSoap($this->_connectionType); $request = new CancelTaxRequest(); $request->setDocCode($invoiceNumber); $request->setDocType(DocumentType::$SalesInvoice); $request->setCompanyCode($calc['company_code']); // Dashboard Company Code if ($calc['committ'] == 0) { return false; } //CancelCode: Enter D for DocDeleted, or P for PostFailed: [D] //I do not know the difference, I use always D (I assume this means order got deleted, cancelled, or refund) $code = CancelCode::$DocDeleted; $request->setCancelCode($code); try { avadebug('plgVmOnCancelPayment used request', $request); $result = $client->cancelTax($request); if ($result->getResultCode() != "Success") { $msg = ''; foreach ($result->getMessages() as $rmsg) { $msg .= $rmsg->getName() . ": " . $rmsg->getSummary() . "\n"; } vmError($msg); } else { vmInfo('CancelTax ResultCode is: ' . $result->getResultCode()); } } catch (SoapFault $exception) { $msg = "Exception: "; if ($exception) { $msg .= $exception->faultstring; } $msg .= "\n"; $msg .= $client->__getLastRequest() . "\n"; $msg .= $client->__getLastResponse() . "\n"; vmError($msg); } }
public function tax($type, $params = array()) { $this->_connectToAvalara(); $client = new TaxServiceSoap(Configuration::get('AVALARATAX_MODE')); if ($type == 'commit') { $request = new CommitTaxRequest(); } elseif ($type == 'post') { $request = new PostTaxRequest(); $request->setDocDate($params['DocDate']); $request->setTotalAmount($params['TotalAmount']); $request->setTotalTax($params['TotalTax']); } elseif ($type == 'cancel') { $request = new CancelTaxRequest(); if ($params['CancelCode'] == 'D') { $code = CancelCode::$DocDeleted; } elseif ($params['CancelCode'] == 'P') { $code = CancelCode::$PostFailed; } elseif ($params['CancelCode'] == 'V') { $code = CancelCode::$DocVoided; } else { die('Invalid cancel code.'); } $request->setCancelCode($code); } elseif ($type == 'history') { $request = new GetTaxHistoryRequest(); $request->setDetailLevel(DetailLevel::$Document); } if ($type != 'isAuthorized') { $request->setDocCode('Order ' . (int) $params['DocCode']); $request->setDocType(DocumentType::$SalesInvoice); $request->setCompanyCode(Configuration::get('AVALARATAX_COMPANY_CODE')); } $buffer = array(); try { if ($type == 'commit') { $result = $client->commitTax($request); } elseif ($type == 'post') { $result = $client->postTax($request); } elseif ($type == 'cancel') { $result = $client->cancelTax($request); } elseif ($type == 'isAuthorized') { $result = $client->isAuthorized('GetTax'); } elseif ($type == 'history') { $result = $client->getTaxHistory($request); $buffer['Invoice'] = $result->getGetTaxRequest()->getDocCode(); $buffer['Status'] = $result->getGetTaxResult()->getDocStatus(); } $buffer['ResultCode'] = $result->getResultCode(); if ($result->getResultCode() != SeverityLevel::$Success) { foreach ($result->getMessages() as $msg) { $buffer['Messages']['Name'] = Tools::safeOutput($msg->getName()); $buffer['Messages']['Summary'] = Tools::safeOutput($msg->getSummary()); } } } catch (SoapFault $exception) { $buffer['Exception']['FaultString'] = Tools::safeOutput($exception->faultstring); $buffer['Exception']['LastRequest'] = Tools::safeOutput($client->__getLastRequest()); $buffer['Exception']['LastResponse'] = Tools::safeOutput($client->__getLastResponse()); } return $buffer; }
<?php require '../../AvaTax4PHP/AvaTax.php'; // include in all Avalara Scripts require '../Credentials.php'; // where service URL, account, license key are set $STDIN = fopen('php://stdin', 'r'); $client = new TaxServiceSoap('Development'); $request = new CancelTaxRequest(); // Locate Document by Invoice Number (Document Code) echo "Enter Invoice Number (Document Code): "; $input = rtrim(fgets($STDIN)); $request->setDocCode($input); $request->setDocType('SalesInvoice'); $request->setCompanyCode('DEFAULT'); // Dashboard Company Code $request->setDocType('SalesInvoice'); $input = "bogus"; while ($input != "D" && $input != "P") { echo "CancelCode: Enter D for DocDeleted, or P for PostFailed: [D]"; $input = strtoupper(rtrim(fgets($STDIN))); if ($input == '') { $input = 'D'; } } $code = CancelCode::$DocDeleted; if ($input == 'P') { $code = CancelCode::$PostFailed; } $request->setCancelCode($code); try {
function testReconcileTaxHistory() { global $client; $dateTime = new DateTime(); $docCode = "PHPReconcile" . date_format($dateTime, "dmyGis"); $getTaxRequest = $this->CreateTaxRequest($docCode); $getTaxRequest->setCommit(true); $getTaxResult = $client->getTax($getTaxRequest); $this->assertEqual(SeverityLevel::$Success, $getTaxResult->getResultCode()); $this->assertEqual(DocStatus::$Committed, $getTaxResult->getDocStatus()); $reconcileTaxHistoryRequest = new ReconcileTaxHistoryRequest(); $reconcileTaxHistoryRequest->setCompanyCode("DEFAULT"); //request.setReconciled(false); $reconcileTaxHistoryRequest->setStartDate(date_format($dateTime, "Y-m-d")); $reconcileTaxHistoryRequest->setEndDate(date_format($dateTime, "Y-m-d")); $reconcileTaxHistoryRequest->setDocStatus(DocStatus::$Committed); $reconcileTaxHistoryRequest->setLastDocId("0"); $reconcileTaxHistoryRequest->setLastDocCode("0"); $reconcileTaxHistoryRequest->setPageSize(1000); $reconcileTaxHistoryRequest->setDocType(DocumentType::$SalesInvoice); //Calling reconHistory Method $reconcileTaxHistoryResult = $client->reconcileTaxHistory($reconcileTaxHistoryRequest); $this->assertEqual(SeverityLevel::$Success, $reconcileTaxHistoryResult->getResultCode()); $taxResults = $reconcileTaxHistoryResult->getGetTaxResults(); $this->assertTrue(count($taxResults) > 0, "Expected > 0 reconcile records"); $this->assertTrue($reconcileTaxHistoryResult->getRecordCount() >= count($taxResults), "RecordCount has to be equal to or more than number of records fetched"); $found = false; /*do { foreach ($taxResults as $taxResult) { $this->assertEqual(DocStatus::$Committed, $taxResult->getDocStatus()); if (strcmp($taxResult->getDocCode(),$getTaxRequest->getDocCode()) == 0) { $found = true; } } $reconcileTaxHistoryRequest->setLastDocCode($reconcileTaxHistoryResult->getLastDocCode()); $reconcileTaxHistoryResult = $client->reconcileTaxHistory($reconcileTaxHistoryRequest); $taxResults = $reconcileTaxHistoryResult->getGetTaxResults(); } while ( count($taxResults) > 0);*/ //$this->assertTrue($found,"ReconcileCommittedTest doc not found"); //Cancel Tax $cancelRequest = new CancelTaxRequest(); $cancelRequest->setCompanyCode($getTaxRequest->getCompanyCode()); $cancelRequest->setDocCode($getTaxRequest->getDocCode()); $cancelRequest->setDocType($getTaxRequest->getDocType()); $cancelRequest->setCancelCode(CancelCode::$DocDeleted); $cancelResult = $client->cancelTax($cancelRequest); $this->assertEqual(SeverityLevel::$Success, $cancelResult->getResultCode()); }