Example #1
0
 /**
  * retrieve line items
  *
  * @param \Fooman_PdfCustomiser_Helper_Pdf $helper
  * @param                                  $printItem
  * @param null                             $order
  *
  * @internal param $
  * @return void
  * @access   public
  */
 public function prepareLineItems(Fooman_PdfCustomiser_Helper_Pdf $helper, $printItem, $order = null)
 {
     //reset
     $this->_taxTotal = array();
     $this->_taxAmount = array();
     $this->_hiddenTaxAmount = 0;
     $this->_baseHiddenTaxAmount = 0;
     $this->_pdfItems = array();
     $this->_pdfBundleItems = array();
     //prepare settings
     if (Mage::getStoreConfig('tax/sales_display/price', $helper->getStoreId()) == Mage_Tax_Model_Config::DISPLAY_TYPE_BOTH || Mage::getStoreConfig('tax/sales_display/price', $helper->getStoreId()) == Mage_Tax_Model_Config::DISPLAY_TYPE_INCLUDING_TAX) {
         $displayItemTaxInclusive = true;
     } else {
         $displayItemTaxInclusive = false;
     }
     if (Mage::getStoreConfigFlag('sales_pdf/all/allrowtotaltaxinclusive', $helper->getStoreId())) {
         $displaySubtotalTaxInclusive = true;
     } else {
         $displaySubtotalTaxInclusive = false;
     }
     $displayTaxInclusiveHiddenTaxAmount = false;
     if (Mage::getStoreConfig('tax/calculation/apply_after_discount', $helper->getStoreId())) {
         $displayTaxAfterDiscount = true;
     } else {
         $displayTaxAfterDiscount = false;
     }
     if (Mage::getStoreConfig('tax/weee/display_sales', $helper->getStoreId()) <= 2) {
         $displayFptInclusivePrices = true;
     } else {
         $displayFptInclusivePrices = false;
     }
     //loop over all items of the sales object
     foreach ($printItem->getAllItems() as $item) {
         $pdfTemp = array();
         //check if we are printing an order
         if ($item instanceof Mage_Sales_Model_Order_Item) {
             $isOrderItem = true;
             $orderItem = $item;
             $pdfTemp['qty'] = $helper->getPdfQtyAsInt() ? (int) $item->getQtyOrdered() : $item->getQtyOrdered();
             $pdfTemp['qty_ordered'] = $pdfTemp['qty'];
             $pdfTemp['qty_backordered'] = $helper->getPdfQtyAsInt() ? (int) $item->getQtyBackordered() : $item->getQtyBackordered();
         } else {
             $isOrderItem = false;
             $orderItem = $item->getOrderItem();
             $pdfTemp['qty'] = $helper->getPdfQtyAsInt() ? (int) $item->getQty() : $item->getQty();
             $pdfTemp['qty_ordered'] = $helper->getPdfQtyAsInt() ? (int) $orderItem->getQtyOrdered() : $orderItem->getQtyOrdered();
             $pdfTemp['qty_backordered'] = $helper->getPdfQtyAsInt() ? (int) $orderItem->getQtyBackordered() : $orderItem->getQtyBackordered();
         }
         $pdfTemp['qty_detailed'][] = Mage::helper('sales')->__('Ordered') . ' ' . $pdfTemp['qty_ordered'];
         if ($orderItem->getQtyInvoiced() > 0.0001) {
             $pdfTemp['qty_detailed'][] = Mage::helper('sales')->__('Invoiced') . ' ' . ($helper->getPdfQtyAsInt() ? (int) $orderItem->getQtyInvoiced() : $orderItem->getQtyInvoiced());
         }
         if ($orderItem->getQtyShipped() > 0.0001) {
             $pdfTemp['qty_detailed'][] = Mage::helper('sales')->__('Shipped') . ' ' . ($helper->getPdfQtyAsInt() ? (int) $orderItem->getQtyShipped() : $orderItem->getQtyShipped());
         }
         if ($orderItem->getQtyRefunded() > 0.0001) {
             $pdfTemp['qty_detailed'][] = Mage::helper('sales')->__('Refunded') . ' ' . ($helper->getPdfQtyAsInt() ? (int) $orderItem->getQtyRefunded() : $orderItem->getQtyRefunded());
         }
         if ($orderItem->getQtyCanceled() > 0.0001) {
             $pdfTemp['qty_detailed'][] = Mage::helper('sales')->__('Canceled') . ' ' . ($helper->getPdfQtyAsInt() ? (int) $orderItem->getQtyCanceled() : $orderItem->getQtyCanceled());
         }
         if ($orderItem->getQtyBackordered() > 0.0001) {
             $pdfTemp['qty_detailed'][] = Mage::helper('sales')->__('Backordered') . ' ' . ($helper->getPdfQtyAsInt() ? (int) $orderItem->getQtyBackordered() : $orderItem->getQtyBackordered());
         }
         $pdfTemp['item_status'] = $orderItem->getStatus();
         //we generally don't want to display subitems of configurable products etc but we do for bundled
         $type = $orderItem->getProductType();
         $itemId = $orderItem->getItemId();
         $parentType = 'none';
         $parentItemId = $orderItem->getParentItemId();
         $parentProductId = false;
         if ($parentItemId) {
             $parentItem = Mage::getModel('sales/order_item')->load($parentItemId);
             $parentType = $parentItem->getProductType();
             $parentProductId = $parentItem->getProductId();
         }
         //Get item details
         $pdfTemp['itemId'] = $itemId;
         $pdfTemp['productId'] = $orderItem->getProductId();
         $pdfTemp['type'] = $type;
         $pdfTemp['parentType'] = $parentType;
         $pdfTemp['parentItemId'] = $parentItemId;
         $pdfTemp['productDetails'] = $this->getItemNameAndSku($item, $helper);
         $pdfTemp['productOptions'] = $orderItem->getProductOptions();
         $pdfTemp['giftMessage'] = $this->getGiftMessage($orderItem);
         if ($displayItemTaxInclusive) {
             if ($item->getPriceInclTax()) {
                 $pdfTemp['price'] = $item->getPriceInclTax();
             } elseif ($pdfTemp['qty']) {
                 $pdfTemp['price'] = $item->getPrice() + ($item->getTaxAmount() + $item->getHiddenTaxAmount()) / $pdfTemp['qty'];
             } else {
                 $pdfTemp['price'] = $item->getPrice();
             }
         } else {
             $pdfTemp['price'] = $item->getPrice();
         }
         $pdfTemp['discountAmount'] = $item->getDiscountAmount();
         $pdfTemp['taxAmount'] = $item->getTaxAmount();
         if ($displayTaxInclusiveHiddenTaxAmount) {
             $pdfTemp['taxAmount'] += $item->getHiddenTaxAmount();
         }
         $pdfTemp['rowTotal2'] = $item->getRowTotal();
         if ($displayItemTaxInclusive || $displaySubtotalTaxInclusive) {
             if ($item->getRowTotalInclTax()) {
                 $pdfTemp['rowTotal2'] += $item->getTaxAmount();
                 if ($displayTaxAfterDiscount) {
                     $pdfTemp['rowTotal2'] += $item->getHiddenTaxAmount();
                 }
             } else {
                 $pdfTemp['rowTotal2'] += $item->getTaxAmount();
             }
         }
         if ($item->getRowTotalInclTax()) {
             $pdfTemp['rowTotal2'] -= $pdfTemp['discountAmount'];
             $pdfTemp['subtotal'] = $item->getRowTotalInclTax();
         } else {
             $pdfTemp['subtotal'] = $item->getRowTotal() + $item->getTaxAmount();
         }
         if (!$displaySubtotalTaxInclusive) {
             $pdfTemp['subtotal'] -= $item->getTaxAmount();
         }
         if (Mage::helper('tax')->displaySalesPriceInclTax()) {
             $pdfTemp['subtotal2'] = Mage::helper('checkout')->getSubtotalInclTax($item);
         } else {
             $pdfTemp['subtotal2'] = $item->getRowTotal();
         }
         if ($displayFptInclusivePrices) {
             $pdfTemp['weee'] = Mage::helper('weee')->getApplied($item);
             $pdfTemp['price'] += $item->getWeeeTaxAppliedAmount();
             $pdfTemp['rowTotal2'] += $item->getWeeeTaxAppliedAmount();
             $pdfTemp['subtotal'] += $item->getWeeeTaxAppliedAmount();
         } else {
             $pdfTemp['weee'] = false;
         }
         //get item details - BASE
         if ($displayItemTaxInclusive) {
             if ($item->getBasePriceInclTax()) {
                 $pdfTemp['basePrice'] = $item->getBasePriceInclTax();
             } elseif ($pdfTemp['qty']) {
                 $pdfTemp['basePrice'] = $item->getBasePrice() + ($item->getBaseTaxAmount() + $item->getBaseHiddenTaxAmount()) / $pdfTemp['qty'];
             } else {
                 $pdfTemp['basePrice'] = $item->getBasePrice();
             }
         } else {
             $pdfTemp['basePrice'] = $item->getBasePrice();
         }
         $pdfTemp['baseDiscountAmount'] = $item->getBaseDiscountAmount();
         $pdfTemp['baseTaxAmount'] = $item->getBaseTaxAmount();
         if ($displayTaxInclusiveHiddenTaxAmount) {
             $pdfTemp['baseTaxAmount'] += $item->getBaseHiddenTaxAmount();
         }
         $pdfTemp['baseRowTotal2'] = $item->getBaseRowTotal();
         if ($displayItemTaxInclusive || $displaySubtotalTaxInclusive) {
             if ($item->getRowTotalInclTax()) {
                 $pdfTemp['baseRowTotal2'] += $item->getBaseTaxAmount();
                 if ($displayTaxAfterDiscount) {
                     $pdfTemp['baseRowTotal2'] += $item->getBaseHiddenTaxAmount();
                 }
             } else {
                 $pdfTemp['baseRowTotal2'] += $item->getBaseTaxAmount();
             }
         }
         if ($item->getBaseRowTotalInclTax()) {
             $pdfTemp['baseRowTotal2'] -= $pdfTemp['baseDiscountAmount'];
             $pdfTemp['baseSubtotal'] = $item->getBaseRowTotalInclTax();
         } else {
             $pdfTemp['baseSubtotal'] = $item->getBaseRowTotal() + $item->getBaseTaxAmount();
         }
         if (!$displaySubtotalTaxInclusive) {
             $pdfTemp['baseSubtotal'] -= $item->getBaseTaxAmount();
         }
         if (Mage::helper('tax')->displaySalesPriceInclTax()) {
             $pdfTemp['baseSubtotal2'] = Mage::helper('checkout')->getBaseSubtotalInclTax($item);
         } else {
             $pdfTemp['baseSubtotal2'] = $item->getBaseRowTotal();
         }
         if ($displayFptInclusivePrices) {
             $pdfTemp['basePrice'] += $item->getBaseWeeeTaxAppliedAmount();
             $pdfTemp['baseRowTotal2'] += $item->getBaseWeeeTaxAppliedAmount();
             $pdfTemp['baseSubtotal'] += $item->getBaseWeeeTaxAppliedAmount();
         }
         if ($orderItem->getTaxPercent()) {
             $taxPercent = sprintf("%01.4f", $orderItem->getTaxPercent());
         } else {
             $taxPercent = '0.000';
         }
         $pdfTemp['taxPercent'] = sprintf("%01.2f", $taxPercent) . '%';
         $parentFixedPrice = false;
         if ($type == 'bundle') {
             $bundlePdfModel = Mage::getModel('bundle/sales_order_pdf_items_invoice');
             $bundlePdfModel->setItem($orderItem);
             $parentFixedPrice = $bundlePdfModel->isChildCalculated($orderItem);
             if ($parentFixedPrice) {
                 $this->trackItemTax($item, $taxPercent);
             }
         } elseif ($parentType != 'bundle') {
             $this->trackItemTax($item, $taxPercent);
         }
         //prepare image
         $pdfTemp['image'] = false;
         if ($helper->printProductImages()) {
             if ($helper->printProductImages()) {
                 $infoBuyRequest = $orderItem->getProductOptionByCode('info_buyRequest');
                 if (isset($infoBuyRequest['zetaprints-downloaded-previews'])) {
                     $pdfTemp['image'] = current($infoBuyRequest['zetaprints-downloaded-previews']);
                 } else {
                     $pdfTemp['image'] = $this->prepareProductImage($pdfTemp['productId'], $pdfTemp['productDetails']['Sku'], $parentProductId);
                 }
             }
         }
         //collect bundle subitems separately
         if ($type == 'bundle') {
             $bundleHelper = Mage::helper('pdfcustomiser/bundle');
             if ($isOrderItem) {
                 $bundleChildren = $orderItem->getChildrenItems();
             } else {
                 $bundleChildren = $bundleHelper->getChilds($item);
             }
             if ($item instanceof Mage_Sales_Model_Order_Shipment_Item) {
                 $shipSeparate = $bundleHelper->isShipmentSeparately($item);
             } else {
                 $shipSeparate = false;
             }
             foreach ($bundleChildren as $childItem) {
                 if ($childItem->getId() == $item->getId()) {
                     continue;
                 }
                 $selectionAttributes = $bundleHelper->getSelectionAttributes($childItem);
                 $subBundleItem = array();
                 $subBundleItem['price'] = $childItem->getPrice();
                 $subBundleItem['parentItemId'] = $itemId;
                 if (!$childItem instanceof Mage_Sales_Model_Order_Item) {
                     $childOrderItem = $childItem->getOrderItem();
                 } else {
                     $childOrderItem = $childItem;
                 }
                 if ($item instanceof Mage_Sales_Model_Order_Shipment_Item) {
                     if ($shipSeparate) {
                         $subBundleItem['qty'] = $helper->getPdfQtyAsInt() ? (int) $item->getQty() * $selectionAttributes['qty'] : $item->getQty() * $selectionAttributes['qty'];
                     } else {
                         $subBundleItem['qty'] = $helper->getPdfQtyAsInt() ? (int) $childItem->getQty() : $childItem->getQty();
                     }
                     if (!$shipSeparate || $parentFixedPrice) {
                         $pdfTemp['qty'] = '';
                     }
                 } else {
                     if ($parentFixedPrice) {
                         $subBundleItem['qty'] = $helper->getPdfQtyAsInt() ? (int) $selectionAttributes['qty'] : $selectionAttributes['qty'];
                     } elseif ($isOrderItem) {
                         $subBundleItem['qty'] = $helper->getPdfQtyAsInt() ? (int) $childItem->getQtyOrdered() : $childItem->getQtyOrdered();
                     } else {
                         $subBundleItem['qty'] = $helper->getPdfQtyAsInt() ? (int) $childItem->getQty() : $childItem->getQty();
                     }
                 }
                 if ($displayItemTaxInclusive && $subBundleItem['qty'] != 0) {
                     $subBundleItem['price'] = $childItem->getPrice() + ($childItem->getTaxAmount() + $childItem->getHiddenTaxAmount()) / $subBundleItem['qty'];
                 } else {
                     $subBundleItem['price'] = $childItem->getPrice();
                 }
                 $subBundleItem['discountAmount'] = $childOrderItem->getDiscountAmount();
                 $subBundleItem['qty_ordered'] = $helper->getPdfQtyAsInt() ? (int) $childOrderItem->getQtyOrdered() : $childOrderItem->getQtyOrdered();
                 $subBundleItem['qty_backordered'] = $helper->getPdfQtyAsInt() ? (int) $childOrderItem->getQtyBackordered() : $childOrderItem->getQtyBackordered();
                 $subBundleItem['taxAmount'] = $childItem->getTaxAmount() + $childItem->getHiddenTaxAmount();
                 $subBundleItem['rowTotal2'] = $childItem->getRowTotal();
                 if ($displayItemTaxInclusive || $displaySubtotalTaxInclusive) {
                     $subBundleItem['rowTotal2'] += $subBundleItem['taxAmount'];
                     if ($childItem->getRowTotalInclTax()) {
                         $subBundleItem['rowTotal2'] += $childItem->getTaxAmount() - $subBundleItem['discountAmount'];
                         if ($displayTaxAfterDiscount) {
                             $subBundleItem['rowTotal2'] += $childItem->getHiddenTaxAmount();
                         }
                     } else {
                         $subBundleItem['rowTotal2'] += $childItem->getTaxAmount();
                     }
                 }
                 if ($childItem->getRowTotalInclTax()) {
                     $subBundleItem['subtotal'] = $childItem->getRowTotalInclTax();
                 } else {
                     $subBundleItem['subtotal'] = $childItem->getRowTotal() + $childItem->getTaxAmount();
                 }
                 //get item details - BASE
                 if ($displayItemTaxInclusive && $subBundleItem['qty'] != 0) {
                     $subBundleItem['basePrice'] = $childItem->getBasePrice() + ($childItem->getBaseTaxAmount() + $childItem->getBaseHiddenTaxAmount()) / $subBundleItem['qty'];
                 } else {
                     $subBundleItem['basePrice'] = $childItem->getBasePrice();
                 }
                 $subBundleItem['baseDiscountAmount'] = $childOrderItem->getBaseDiscountAmount();
                 $subBundleItem['baseTaxAmount'] = $childItem->getBaseTaxAmount() + $childItem->getBaseHiddenTaxAmount();
                 $subBundleItem['baseRowTotal2'] = $childItem->getBaseRowTotal();
                 if ($displayItemTaxInclusive || $displaySubtotalTaxInclusive) {
                     $subBundleItem['baseRowTotal2'] += $subBundleItem['baseTaxAmount'];
                     if ($childItem->getBaseRowTotalInclTax()) {
                         $subBundleItem['baseRowTotal2'] += $childItem->getBaseTaxAmount() - $subBundleItem['baseDiscountAmount'];
                         if ($displayTaxAfterDiscount) {
                             $subBundleItem['baseRowTotal2'] += $childItem->getBaseHiddenTaxAmount();
                         }
                     } else {
                         $subBundleItem['baseRowTotal2'] += $childItem->getBaseTaxAmount();
                     }
                 }
                 if ($childItem->getBaseRowTotalInclTax()) {
                     $subBundleItem['baseSubtotal'] = $childItem->getBaseRowTotalInclTax();
                 } else {
                     $subBundleItem['baseSubtotal'] = $childItem->getBaseRowTotal() + $childItem->getBaseTaxAmount();
                 }
                 if ($childOrderItem->getTaxPercent()) {
                     $taxPercent = sprintf("%01.4f", $childOrderItem->getTaxPercent());
                 } else {
                     $taxPercent = '0.0000';
                 }
                 if (!$parentFixedPrice) {
                     $this->trackItemTax($childItem, $taxPercent, $orderItem->getTaxPercent());
                 }
                 $subBundleItem['taxPercent'] = $taxPercent;
                 if ($helper->printProductImages()) {
                     $subBundleItem['image'] = $this->prepareProductImage($childItem->getProductId(), false);
                 }
                 $subBundleItem['productDetails'] = $this->getItemNameAndSku($childItem, $helper);
                 if ($selectionAttributes['option_label']) {
                     $subBundleItem['productDetails']['Name'] = "<b>" . $selectionAttributes['option_label'] . "</b>: " . $subBundleItem['productDetails']['Name'];
                 }
                 $transport = new Varien_Object();
                 $transport->setItemData($subBundleItem);
                 Mage::dispatchEvent('fooman_pdfcustomiser_prepare_subbundleitem', array('item' => $childItem, 'transport' => $transport));
                 $this->_pdfBundleItems[$itemId][] = $transport->getItemData();
             }
         }
         if ($parentType != 'bundle') {
             $transport = new Varien_Object();
             $transport->setItemData($pdfTemp);
             Mage::dispatchEvent('fooman_pdfcustomiser_prepare_item', array('item' => $item, 'transport' => $transport));
             $this->_pdfItems[$itemId] = $transport->getItemData();
         }
     }
     $this->_sortBy = $helper->getColumnsSortOrder();
     if ($this->_sortBy) {
         uasort($this->_pdfItems, array($this, 'cmp'));
         //uasort($this->_pdfBundleItems, array($this, 'cmp'));
     }
     $this->prepareTotalTaxes($order, $printItem, $helper);
 }