예제 #1
0
 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;
 }
$request->setDocCode($input);
$request->setDocDate($input);
$request->setDocType('SalesInvoice');
$request->setCompanyCode('DEFAULT');
//<Your Dashboard Company Code Here>');
echo "Enter Total Invoice Amount: ";
$input = rtrim(fgets($STDIN));
$request->setTotalAmount($input);
echo "Enter Total Invoice Tax: ";
$input = rtrim(fgets($STDIN));
$request->setTotalTax($input);
echo "Enter Doc Date (yyyy-mm-dd): ";
$input = rtrim(fgets($STDIN));
$request->setDocDate($input);
try {
    $result = $client->postTax($request);
    echo 'PostTax ResultCode is: ' . $result->getResultCode() . "\n";
    if ($result->getResultCode() != SeverityLevel::$Success) {
        foreach ($result->getMessages() as $msg) {
            echo $msg->getName() . ": " . $msg->getSummary() . "\n";
        }
    }
} catch (SoapFault $exception) {
    $msg = "Exception: ";
    if ($exception) {
        $msg .= $exception->faultstring;
    }
    echo $msg . "\n";
    echo $client->__getLastRequest() . "\n";
    echo $client->__getLastResponse() . "\n";
}