示例#1
0
 /**
  * Save order in AvaTax system
  *
  * @see OnePica_AvaTax_Model_Observer_SalesOrderInvoiceSaveAfter::execute()
  * @param Mage_Sales_Model_Order_Invoice $invoice
  * @param OnePica_AvaTax_Model_Records_Queue $queue
  * @return OnePica_AvaTax_Model_Service_Result_Invoice
  */
 public function invoice($invoice, $queue)
 {
     $this->_lines = array();
     $order = $invoice->getOrder();
     $storeId = $order->getStoreId();
     $invoiceDate = $this->_convertGmtDate($invoice->getCreatedAt(), $storeId);
     $orderDate = $this->_convertGmtDate($order->getCreatedAt(), $storeId);
     $statusDate = $this->_convertGmtDate($queue->getUpdatedAt(), $storeId);
     $shippingAddress = $order->getShippingAddress() ? $order->getShippingAddress() : $order->getBillingAddress();
     $this->_request = new GetTaxRequest();
     $this->_request->setDocCode($invoice->getIncrementId());
     $this->_request->setDocType(DocumentType::$SalesInvoice);
     $this->_addGeneralInfo($order);
     $this->_addShipping($invoice);
     $items = $invoice->getItemsCollection();
     $this->_initProductCollection($items);
     $this->_initTaxClassCollection($invoice);
     //Added code for calculating tax for giftwrap items
     $this->_addGwOrderAmount($invoice);
     $this->_addGwItemsAmount($invoice);
     $this->_addGwPrintedCardAmount($invoice);
     $this->_setOriginAddress($order->getStoreId());
     $this->_setDestinationAddress($shippingAddress);
     $this->_request->setDocDate($invoiceDate);
     $this->_request->setPaymentDate($invoiceDate);
     $this->_request->setTaxDate($orderDate);
     $this->_request->setStatusDate($statusDate);
     $configAction = Mage::getStoreConfig('tax/avatax/action', $order->getStoreId());
     $commitAction = OnePica_AvaTax_Model_Service_Abstract_Config::ACTION_CALC_SUBMIT_COMMIT;
     $this->_request->setCommit($configAction == $commitAction ? true : false);
     foreach ($items as $item) {
         /** @var Mage_Sales_Model_Order_Invoice_Item $item */
         $this->_newLine($item);
     }
     $this->_request->setLines($this->_lines);
     //send to AvaTax
     $result = $this->_send($order->getStoreId());
     /** @var OnePica_AvaTax_Model_Service_Result_Invoice $invoiceResult */
     $invoiceResult = Mage::getModel('avatax/service_result_invoice');
     $resultHasError = $result->getResultCode() != SeverityLevel::$Success;
     $invoiceResult->setHasError($resultHasError);
     //if successful
     if (!$resultHasError) {
         $totalTax = $result->getTotalTax();
         $invoiceResult->setTotalTax($totalTax);
         $documentCode = $result->getDocCode();
         $invoiceResult->setDocumentCode($documentCode);
         //if not successful
     } else {
         $messages = array();
         foreach ($result->getMessages() as $message) {
             $messages[] = $message->getSummary();
         }
         $invoiceResult->setErrors($messages);
     }
     return $invoiceResult;
 }
示例#2
0
 /**
  * Save order in AvaTax system
  *
  * @see OnePica_AvaTax_Model_Observer::salesOrderPlaceAfter()
  * @param Mage_Sales_Model_Order_Invoice     $invoice
  * @param OnePica_AvaTax_Model_Records_Queue $queue
  * @return bool
  * @throws OnePica_AvaTax_Exception
  * @throws OnePica_AvaTax_Model_Avatax_Exception_Commitfailure
  * @throws OnePica_AvaTax_Model_Avatax_Exception_Unbalanced
  */
 public function invoice($invoice, $queue)
 {
     $order = $invoice->getOrder();
     $storeId = $order->getStoreId();
     $invoiceDate = $this->_convertGmtDate($invoice->getCreatedAt(), $storeId);
     $orderDate = $this->_convertGmtDate($order->getCreatedAt(), $storeId);
     $statusDate = $this->_convertGmtDate($queue->getUpdatedAt(), $storeId);
     $shippingAddress = $order->getShippingAddress() ? $order->getShippingAddress() : $order->getBillingAddress();
     if (!$shippingAddress) {
         throw new OnePica_AvaTax_Exception($this->__('There is no address attached to this order'));
     }
     $this->_request = new GetTaxRequest();
     $this->_request->setDocCode($invoice->getIncrementId());
     $this->_request->setDocType(DocumentType::$SalesInvoice);
     $this->_addGeneralInfo($order);
     $this->_addShipping($invoice);
     $items = $invoice->getItemsCollection();
     $this->_initProductCollection($items);
     $this->_initTaxClassCollection($invoice);
     //Added code for calculating tax for giftwrap items
     $this->_addGwOrderAmount($invoice);
     $this->_addGwItemsAmount($invoice);
     $this->_addGwPrintedCardAmount($invoice);
     $this->_setOriginAddress($order->getStoreId());
     $this->_setDestinationAddress($shippingAddress);
     $this->_request->setDocDate($invoiceDate);
     $this->_request->setPaymentDate($invoiceDate);
     $this->_request->setTaxDate($orderDate);
     $this->_request->setStatusDate($statusDate);
     $configAction = Mage::getStoreConfig('tax/avatax/action', $order->getStoreId());
     $commitAction = OnePica_AvaTax_Model_Config::ACTION_CALC_SUBMIT_COMMIT;
     $this->_request->setCommit($configAction == $commitAction ? true : false);
     foreach ($items as $item) {
         /** @var Mage_Sales_Model_Order_Invoice_Item $item */
         $this->_newLine($item);
     }
     $this->_request->setLines($this->_lines);
     //send to AvaTax
     $result = $this->_send($order->getStoreId());
     //if successful
     if ($result->getResultCode() == SeverityLevel::$Success) {
         $message = Mage::helper('avatax')->__('Invoice #%s was saved to AvaTax', $result->getDocCode());
         $this->_addStatusHistoryComment($order, $message);
         if ($result->getTotalTax() != $invoice->getBaseTaxAmount()) {
             throw new OnePica_AvaTax_Model_Avatax_Exception_Unbalanced('Collected: ' . $invoice->getBaseTaxAmount() . ', Actual: ' . $result->getTotalTax());
         }
         //if not successful
     } else {
         $messages = array();
         foreach ($result->getMessages() as $message) {
             $messages[] = $message->getSummary();
         }
         throw new OnePica_AvaTax_Model_Avatax_Exception_Commitfailure(implode(' // ', $messages));
     }
     return true;
 }
示例#3
0
 /**
  * Save order's invoice in AvaTax system
  *
  * @see OnePica_AvaTax_Model_Observer_SalesOrderInvoiceSaveAfter::execute()
  * @param Mage_Sales_Model_Order_Invoice $invoice
  * @param OnePica_AvaTax_Model_Records_Queue $queue
  * @return OnePica_AvaTax_Model_Service_Result_Invoice
  */
 public function invoice($invoice, $queue)
 {
     $this->_lines = array();
     $order = $invoice->getOrder();
     $storeId = $order->getStoreId();
     $invoiceDate = $this->_convertGmtDate($invoice->getCreatedAt(), $storeId);
     $orderDate = $this->_convertGmtDate($order->getCreatedAt(), $storeId);
     $shippingAddress = $order->getShippingAddress() ? $order->getShippingAddress() : $order->getBillingAddress();
     // Set up document for request
     $this->_request = $this->_getNewDocumentRequestObject();
     // set up header
     $header = $this->_getRequestHeaderWithMainValues($storeId, $order);
     $header->setDocumentCode($this->_getInvoiceDocumentCode($invoice));
     $header->setTransactionDate($invoiceDate);
     $header->setTaxCalculationDate($orderDate);
     $header->setDefaultLocations($this->_getHeaderDefaultLocations($shippingAddress));
     $this->_request->setHeader($header);
     $this->_addShipping($invoice);
     $items = $invoice->getItemsCollection();
     $this->_initProductCollection($items);
     $this->_initTaxClassCollection($invoice);
     //Added code for calculating tax for giftwrap items
     $this->_addGwOrderAmount($invoice);
     $this->_addGwItemsAmount($invoice);
     $this->_addGwPrintedCardAmount($invoice);
     foreach ($items as $item) {
         /** @var Mage_Sales_Model_Order_Invoice_Item $item */
         $this->_newLine($item);
     }
     $this->_setLinesToRequest();
     //send to AvaTax
     $result = $this->_send($order->getStoreId());
     /** @var OnePica_AvaTax_Model_Service_Result_Invoice $invoiceResult */
     $invoiceResult = Mage::getModel('avatax/service_result_invoice');
     $invoiceResult->setHasError($result->getHasError());
     //if successful
     if (!$result->getHasError()) {
         $totalTax = $result->getCalculatedTaxSummary()->getTotalTax();
         $invoiceResult->setTotalTax($totalTax);
         $documentCode = $result->getHeader()->getDocumentCode();
         $invoiceResult->setDocumentCode($documentCode);
         //if not successful
     } else {
         $invoiceResult->setErrors($result->getErrors());
     }
     return $invoiceResult;
 }
示例#4
0
 /**
  * Init invoice data
  * @param Mage_Sales_Model_Order_Invoice $invoice
  * @return array
  */
 public function initInvoiceData($invoice)
 {
     $order = $invoice->getOrder();
     $orderCurrencyCode = $order->getOrderCurrencyCode();
     $baseCurrencyCode = $order->getBaseCurrencyCode();
     $this->setTranslationByStoreId($invoice->getStoreId());
     $invoiceData = $this->process($invoice->getData(), $orderCurrencyCode, $baseCurrencyCode);
     $orderData = Mage::getModel('pdfpro/order')->initOrderData($order);
     $invoiceData['order'] = unserialize($orderData);
     $invoiceData['customer'] = $this->getCustomerData(Mage::getModel('customer/customer')->load($order->getCustomerId()));
     $invoiceData['created_at_formated'] = $this->getFormatedDate($invoice->getCreatedAt());
     $invoiceData['updated_at_formated'] = $this->getFormatedDate($invoice->getUpdatedAt());
     $invoiceData['billing'] = $this->getAddressData($invoice->getBillingAddress());
     /*if order is not virtual */
     if (!$order->getIsVirtual()) {
         $invoiceData['shipping'] = $this->getAddressData($invoice->getShippingAddress());
     }
     /*Get Payment Info */
     Mage::getDesign()->setPackageName('default');
     /*Set package to default*/
     $paymentInfo = Mage::helper('payment')->getInfoBlock($order->getPayment())->setIsSecureMode(true)->setArea('adminhtml')->toPdf();
     $paymentInfo = str_replace('{{pdf_row_separator}}', "<br />", $paymentInfo);
     $invoiceData['payment'] = array('code' => $order->getPayment()->getMethodInstance()->getCode(), 'name' => $order->getPayment()->getMethodInstance()->getTitle(), 'info' => $paymentInfo);
     $invoiceData['payment_info'] = $paymentInfo;
     $invoiceData['shipping_description'] = $order->getShippingDescription();
     $invoiceData['totals'] = array();
     $invoiceData['items'] = array();
     /*
      * Get Items information
      */
     foreach ($invoice->getAllItems() as $item) {
         if ($item->getOrderItem()->getParentItem()) {
             continue;
         }
         $itemModel = $this->getItemModel($item);
         if ($item->getOrderItem()->getProductType() == 'bundle') {
             $itemData = array('is_bundle' => 1, 'name' => $item->getName(), 'sku' => $item->getSku());
             if ($itemModel->canShowPriceInfo($item)) {
                 $itemData['price'] = Mage::helper('pdfpro')->currency($item->getPrice(), $orderCurrencyCode);
                 $itemData['qty'] = $item->getQty() * 1;
                 $itemData['tax'] = Mage::helper('pdfpro')->currency($item->getTaxAmount(), $orderCurrencyCode);
                 $itemData['subtotal'] = Mage::helper('pdfpro')->currency($item->getRowTotal(), $orderCurrencyCode);
                 $itemData['row_total'] = Mage::helper('pdfpro')->currency($item->getRowTotalInclTax(), $orderCurrencyCode);
             }
             $itemData['sub_items'] = array();
             $items = $itemModel->getChilds($item);
             foreach ($items as $_item) {
                 $bundleItem = array();
                 $attributes = $itemModel->getSelectionAttributes($_item);
                 // draw SKUs
                 if (!$_item->getOrderItem()->getParentItem()) {
                     continue;
                 }
                 $bundleItem['label'] = $attributes['option_label'];
                 /*Product name */
                 if ($_item->getOrderItem()->getParentItem()) {
                     $name = $itemModel->getValueHtml($_item);
                 } else {
                     $name = $_item->getName();
                 }
                 $bundleItem['value'] = $name;
                 /*$bundleItem['sku']		= $_item->getSku();*/
                 /* price */
                 if ($itemModel->canShowPriceInfo($_item)) {
                     $price = $order->formatPriceTxt($_item->getPrice());
                     $bundleItem['price'] = Mage::helper('pdfpro')->currency($_item->getPrice(), $orderCurrencyCode);
                     $bundleItem['qty'] = $_item->getQty() * 1;
                     $bundleItem['tax'] = Mage::helper('pdfpro')->currency($_item->getTaxAmount(), $orderCurrencyCode);
                     $bundleItem['subtotal'] = Mage::helper('pdfpro')->currency($_item->getRowTotal(), $orderCurrencyCode);
                     $bundleItem['row_total'] = Mage::helper('pdfpro')->currency($_item->getRowTotalInclTax(), $orderCurrencyCode);
                 }
                 $bundleItem = new Varien_Object($bundleItem);
                 Mage::dispatchEvent('ves_pdfpro_data_prepare_after', array('source' => $bundleItem, 'model' => $_item, 'type' => 'item'));
                 $itemData['sub_items'][] = $bundleItem;
             }
         } else {
             $itemData = array('name' => $item->getName(), 'sku' => $item->getSku(), 'price' => Mage::helper('pdfpro')->currency($item->getPrice(), $orderCurrencyCode), 'qty' => $item->getQty() * 1, 'tax' => Mage::helper('pdfpro')->currency($item->getTaxAmount(), $orderCurrencyCode), 'subtotal' => Mage::helper('pdfpro')->currency($item->getRowTotal(), $orderCurrencyCode), 'row_total' => Mage::helper('pdfpro')->currency($item->getRowTotalInclTax(), $orderCurrencyCode));
             $options = $itemModel->getItemOptions($item);
             $itemData['options'] = array();
             if ($options) {
                 foreach ($options as $option) {
                     $optionData = array();
                     $optionData['label'] = strip_tags($option['label']);
                     if ($option['value']) {
                         $printValue = isset($option['print_value']) ? $option['print_value'] : strip_tags($option['value']);
                         $optionData['value'] = $printValue;
                     }
                     $itemData['options'][] = new Varien_Object($optionData);
                 }
             }
         }
         $itemData = new Varien_Object($itemData);
         Mage::dispatchEvent('ves_pdfpro_data_prepare_after', array('source' => $itemData, 'model' => $item, 'type' => 'item'));
         $invoiceData['items'][] = $itemData;
     }
     /*
      * Get Totals information.
      */
     $totals = $this->_getTotalsList($invoice);
     $totalArr = array();
     foreach ($totals as $total) {
         $total->setOrder($order)->setSource($invoice);
         if ($total->canDisplay()) {
             $area = $total->getSourceField() == 'grand_total' ? 'footer' : 'body';
             foreach ($total->getTotalsForDisplay() as $totalData) {
                 $totalArr[$area][] = new Varien_Object(array('label' => $totalData['label'], 'value' => $totalData['amount']));
             }
         }
     }
     $invoiceData['totals'] = new Varien_Object($totalArr);
     $apiKey = Mage::helper('pdfpro')->getApiKey($order->getStoreId(), $order->getCustomerGroupId());
     $invoiceData = new Varien_Object($invoiceData);
     Mage::dispatchEvent('ves_pdfpro_data_prepare_after', array('source' => $invoiceData, 'model' => $invoice, 'type' => 'invoice'));
     $invoiceData = new Varien_Object(array('key' => $apiKey, 'data' => $invoiceData));
     $this->revertTranslation();
     return serialize($invoiceData);
 }