/** * Set entity * * @param Mage_Sales_Model_Order_Invoice|Mage_Sales_Model_Order_Creditmemo $object * @return $this */ public function setEntity($object) { $this->setEntityId($object->getId()); $this->setEntityIncrementId($object->getIncrementId()); $this->setStoreId($object->getStoreId()); return $this; }
/** * Generate and send a SalesInvoice * * @param Aoe_AvaTax_Model_Api $api * @param Mage_Sales_Model_Order_Invoice $invoice * * @return string * @throws Aoe_AvaTax_Exception * @throws Exception */ public function registerInvoice(Aoe_AvaTax_Model_Api $api, Mage_Sales_Model_Order_Invoice $invoice) { if (!$this->isActive($invoice->getStore())) { return null; } $incrementId = $invoice->getIncrementId(); if (empty($incrementId)) { /* @var $entityType Mage_Eav_Model_Entity_Type */ $entityType = Mage::getModel('eav/entity_type')->loadByCode('invoice'); $invoice->setIncrementId($entityType->fetchNewIncrementId($invoice->getStoreId())); } $result = $api->callGetTaxForInvoice($invoice, true); if ($result['ResultCode'] !== 'Success') { throw new Aoe_AvaTax_Exception($result['ResultCode'], $result['Messages']); } // NB: Ignoring the returned tax data on purpose $invoice->getResource()->beginTransaction(); try { $invoice->setAvataxDocument($result['DocCode']); $invoice->addComment(sprintf('SalesInvoice sent to AvaTax (%s)', $result['DocCode'])); $invoice->save(); $invoice->getOrder()->addStatusHistoryComment(sprintf('SalesInvoice sent to AvaTax (%s)', $result['DocCode'])); $invoice->getOrder()->save(); $invoice->getResource()->commit(); } catch (Exception $e) { $invoice->getResource()->rollBack(); throw $e; } return $result['DocCode']; }
/** * Get gift wrapping tax class id * * @param Mage_Sales_Model_Order_Invoice|Mage_Sales_Model_Order_Creditmemo|Mage_Sales_Model_Quote_Address $object * @return int */ protected function _getGwTaxClassId($object) { if (!$object->getGwPrice() && !$object->getGwItemsPrice() && !$object->getGwPrintedCardPrice()) { return 0; } if ($object instanceof Mage_Sales_Model_Quote_Address) { $storeId = $object->getQuote()->getStoreId(); } else { $storeId = $object->getStoreId(); } return $this->_getWrappingTaxClass($storeId); }
/** * 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); }
/** * Article preparations for PAYMENT_REQUEST, PAYMENT_CHANGE, CONFIRMATION_DELIVER * * @param Mage_Sales_Model_Quote|Mage_Sales_Model_Order|Mage_Sales_Model_Order_Invoice|Mage_Sales_Model_Order_Creditmemo $object * @return array */ public function getArticles($object) { $articles = array(); $articleDiscountAmount = 0; $objectItems = $object->getAllItems(); foreach ($objectItems as $item) { if ($item instanceof Mage_Sales_Model_Order_Item || $item instanceof Mage_Sales_Model_Quote_Item) { $orderItem = $item; } else { $orderItem = Mage::getModel('sales/order_item')->load($item->getOrderItemId()); } $shopProduct = Mage::getModel('catalog/product')->load($orderItem->getProductId()); if (($orderItem->getProductType() !== 'bundle' || $orderItem->getProductType() === 'bundle' && $shopProduct->getPrice() > 0) && $orderItem->getRowTotal() > 0) { $article = array(); $article['articleNumber'] = $item->getSku(); $article['articleName'] = $item->getName(); $article['quantity'] = $object instanceof Mage_Sales_Model_Order ? $item->getQtyOrdered() : $item->getQty(); $article['unitPriceGross'] = $item->getPriceInclTax(); $article['tax'] = $item->getTaxAmount(); $article['taxPercent'] = $orderItem->getTaxPercent(); if ($object instanceof Mage_Sales_Model_Quote) { $article['unitPriceNett'] = $item->getCalculationPrice(); } else { $article['unitPriceNett'] = $item->getPrice(); // netto } $article['discountId'] = ''; if ($item->getDiscountAmount() > 0) { $discount = array(); $discount['articleNumber'] = 'DISCOUNT-' . $item->getSku(); $discount['articleName'] = 'DISCOUNT - ' . $item->getName(); $discount['quantity'] = $article['quantity']; $article['tax'] = $item->getRowTotalInclTax() - $item->getRowTotal(); $discount['tax'] = -1 * ($article['tax'] - $item->getTaxAmount()); $tax = 0; $taxConfig = Mage::getSingleton('tax/config'); if ($taxConfig->priceIncludesTax($object->getStoreId())) { $tax = $discount['tax']; } if (round($discount['tax'], 2) < 0) { $discount['taxPercent'] = $article['taxPercent']; } $discount['unitPriceGross'] = -1 * $item->getDiscountAmount() / $article['quantity']; $discount['discountId'] = $item->getSku(); $articleDiscountAmount = $articleDiscountAmount + $item->getDiscountAmount(); } $articles[] = $article; if ($item->getDiscountAmount() > 0) { // only for sort reason $articles[] = $discount; } } } if ($object instanceof Mage_Sales_Model_Order || $object instanceof Mage_Sales_Model_Order_Invoice || $object instanceof Mage_Sales_Model_Order_Creditmemo) { $shippingObject = $object; if ($object instanceof Mage_Sales_Model_Order_Creditmemo) { $articles = $this->addAdjustments($object, $articles); } } else { $shippingObject = $object->getShippingAddress(); } if ($shippingObject->getShippingAmount() > 0) { if ($object instanceof Mage_Sales_Model_Order_Invoice || $object instanceof Mage_Sales_Model_Order_Shipment || $object instanceof Mage_Sales_Model_Order_Creditmemo) { $shippingDiscountAmount = $shippingObject->getDiscountAmount() - $articleDiscountAmount; $shippingDescription = $object->getOrder()->getShippingDescription(); } else { $shippingDiscountAmount = $shippingObject->getShippingDiscountAmount(); $shippingDescription = $shippingObject->getShippingDescription(); } $article = array(); $article['articleNumber'] = 'SHIPPING'; $article['articleName'] = $shippingDescription; $article['quantity'] = '1'; $article['unitPriceGross'] = $shippingObject->getShippingInclTax(); $article['tax'] = $shippingObject->getShippingTaxAmount(); $shippingTaxPercent = 0; if ($shippingObject->getShippingInclTax() - $shippingObject->getShippingAmount() > 0) { $shippingTaxPercent = ($shippingObject->getShippingInclTax() - $shippingObject->getShippingAmount()) * 100 / $shippingObject->getShippingAmount(); } $article['taxPercent'] = $shippingTaxPercent; $article['discountId'] = ''; if ($shippingDiscountAmount > 0) { $discount = array(); $discount['articleNumber'] = 'SHIPPINGDISCOUNT'; $discount['articleName'] = 'Shipping - Discount'; $discount['quantity'] = 1; $discount['unitPriceGross'] = -1 * $shippingObject->getShippingDiscountAmount(); $article['tax'] = $shippingObject->getShippingInclTax() - $shippingObject->getShippingAmount(); $discount['tax'] = -1 * ($article['tax'] - $shippingObject->getShippingTaxAmount()); $tax = 0; if (Mage::getSingleton('tax/config')->shippingPriceIncludesTax($object->getStoreId())) { $tax = $discount['tax']; } $discount['unitPrice'] = -1 * $shippingObject->getShippingDiscountAmount() - $tax; $discount['totalPrice'] = -1 * $shippingObject->getShippingDiscountAmount() - $tax; $discount['taxPercent'] = 0; if (round($discount['tax'], 2) < 0) { $discount['taxPercent'] = $article['taxPercent']; } $discount['discountId'] = 'SHIPPING'; } $articles[] = $article; if ($shippingDiscountAmount > 0) { // only for sort reason $articles[] = $discount; } } return $articles; }
/** * This is a copy of the mail function in class Mage_Sales_Model_Order_Invoice. * In this case the only recipient is billpay and a comment was added. * * @param Mage_Sales_Model_Order_Invoice $invoice */ protected function sendInvoiceCopy($invoice) { $currentDesign = Mage::getDesign()->setAllGetOld(array('package' => Mage::getStoreConfig('design/package/name', $invoice->getStoreId()), 'store' => $invoice->getStoreId())); $translate = Mage::getSingleton('core/translate'); $translate->setTranslateInline(false); $order = $invoice->getOrder(); $paymentBlock = Mage::helper('payment')->getInfoBlock($order->getPayment())->setIsSecureMode(true); $mailTemplate = Mage::getModel('core/email_template'); if ($order->getCustomerIsGuest()) { $template = Mage::getStoreConfig(Mage_Sales_Model_Order_Invoice::XML_PATH_EMAIL_GUEST_TEMPLATE, $order->getStoreId()); $customerName = $order->getBillingAddress()->getName(); } else { $template = Mage::getStoreConfig(Mage_Sales_Model_Order_Invoice::XML_PATH_EMAIL_TEMPLATE, $order->getStoreId()); $customerName = $order->getCustomerName(); } $mode = $this->getHelper()->getConfigData('account/transaction_mode', $order->getStoreId()); $recipientMail = $mode == Billpay_Helper_Api::TRANSACTION_MODE_TEST ? '*****@*****.**' : '*****@*****.**'; $recipientName = 'Billpay'; $mailTemplate->setDesignConfig(array('area' => 'frontend', 'store' => $order->getStoreId()))->sendTransactional($template, Mage::getStoreConfig(Mage_Sales_Model_Order_Invoice::XML_PATH_EMAIL_IDENTITY, $order->getStoreId()), $recipientMail, $recipientName, array('order' => $order, 'invoice' => $invoice, 'comment' => 'This mail was automatically sent by billpay magento plugin', 'billing' => $order->getBillingAddress(), 'payment_html' => $paymentBlock->toHtml())); $translate->setTranslateInline(true); }