Example #1
0
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $totalTax = 0;
     $baseTotalTax = 0;
     foreach ($invoice->getAllItems() as $item) {
         $orderItem = $item->getOrderItem();
         $orderItemTax = $orderItem->getTaxAmount();
         $baseOrderItemTax = $orderItem->getBaseTaxAmount();
         $orderItemQty = $orderItem->getQtyOrdered();
         if ($orderItemTax && $orderItemQty) {
             /**
              * Resolve rounding problems
              */
             if ($item->isLast()) {
                 $tax = $orderItemTax - $orderItem->getTaxInvoiced();
                 $baseTax = $baseOrderItemTax - $orderItem->getBaseTaxInvoiced();
             } else {
                 $tax = $orderItemTax * $item->getQty() / $orderItemQty;
                 $baseTax = $baseOrderItemTax * $item->getQty() / $orderItemQty;
                 $tax = $invoice->getStore()->roundPrice($tax);
                 $baseTax = $invoice->getStore()->roundPrice($baseTax);
             }
             $item->setTaxAmount($tax);
             $item->setBaseTaxAmount($baseTax);
             $totalTax += $tax;
             $baseTotalTax += $baseTax;
         }
     }
     $invoice->setTaxAmount($totalTax);
     $invoice->setBaseTaxAmount($baseTotalTax);
     $invoice->setGrandTotal($invoice->getGrandTotal() + $totalTax);
     $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseTotalTax);
     return $this;
 }
Example #2
0
 /**
  * Collect invoice subtotal
  *
  * @param   Mage_Sales_Model_Order_Invoice $invoice
  * @return  Mage_Sales_Model_Order_Invoice_Total_Subtotal
  */
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $subtotal = 0;
     $baseSubtotal = 0;
     $subtotalInclTax = 0;
     $baseSubtotalInclTax = 0;
     $order = $invoice->getOrder();
     foreach ($invoice->getAllItems() as $item) {
         $item->calcRowTotal();
         if ($item->getOrderItem()->isDummy()) {
             continue;
         }
         $subtotal += $item->getRowTotal();
         $baseSubtotal += $item->getBaseRowTotal();
         $subtotalInclTax += $item->getRowTotalInclTax();
         $baseSubtotalInclTax += $item->getBaseRowTotalInclTax();
     }
     $allowedSubtotal = $order->getSubtotal() - $order->getSubtotalInvoiced();
     $baseAllowedSubtotal = $order->getBaseSubtotal() - $order->getBaseSubtotalInvoiced();
     if ($invoice->isLast()) {
         $subtotal = $allowedSubtotal;
         $baseSubtotal = $baseAllowedSubtotal;
     } else {
         $subtotal = min($allowedSubtotal, $subtotal);
         $baseSubtotal = min($baseAllowedSubtotal, $baseSubtotal);
     }
     $invoice->setSubtotal($subtotal);
     $invoice->setBaseSubtotal($baseSubtotal);
     $invoice->setSubtotalInclTax($subtotalInclTax);
     $invoice->setBaseSubtotalInclTax($baseSubtotalInclTax);
     $invoice->setGrandTotal($invoice->getGrandTotal() + $subtotal);
     $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseSubtotal);
     return $this;
 }
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $baseDiscount = 0;
     $discount = 0;
     foreach ($invoice->getAllItems() as $item) {
         if ($item->getOrderItem()->isDummy()) {
             continue;
         }
         $orderItem = $item->getOrderItem();
         $orderItemDiscount = (double) $orderItem->getAffiliateplusAmount();
         $baseOrderItemDiscount = (double) $orderItem->getBaseAffiliateplusAmount();
         $orderItemQty = $orderItem->getQtyOrdered();
         if ($orderItemDiscount && $orderItemQty) {
             $discount -= $orderItemDiscount * $item->getQty() / $orderItemQty;
             $baseDiscount -= $baseOrderItemDiscount * $item->getQty() / $orderItemQty;
         }
     }
     if (!floatval($baseDiscount)) {
         $order = $invoice->getOrder();
         $baseDiscount = $order->getBaseAffiliateplusDiscount();
         $discount = $order->getAffiliateplusDiscount();
     }
     if (floatval($baseDiscount)) {
         $baseDiscount = Mage::app()->getStore()->roundPrice($baseDiscount);
         $discount = Mage::app()->getStore()->roundPrice($discount);
         $invoice->setBaseAffiliateplusDiscount($baseDiscount);
         $invoice->setAffiliateplusDiscount($discount);
         $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseDiscount);
         $invoice->setGrandTotal($invoice->getGrandTotal() + $discount);
     }
     return $this;
 }
Example #4
0
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $invoice->setGomageGiftWrapAmount(0);
     $invoice->setBaseGomageGiftWrapAmount(0);
     $totalGomageGiftWrapAmount = 0;
     $baseTotalGomageGiftWrapAmount = 0;
     foreach ($invoice->getAllItems() as $item) {
         if ($item->getOrderItem()->isDummy()) {
             continue;
         }
         $orderItem = $item->getOrderItem();
         $orderItemGomageGiftWrap = (double) $orderItem->getGomageGiftWrapAmount();
         $baseOrderItemGomageGiftWrap = (double) $orderItem->getBaseGomageGiftWrapAmount();
         $orderItemQty = $orderItem->getQtyOrdered();
         if ($orderItemGomageGiftWrap && $orderItemQty) {
             $GomageGiftWrap = $orderItemGomageGiftWrap * $item->getQty() / $orderItemQty;
             $baseGomageGiftWrap = $baseOrderItemGomageGiftWrap * $item->getQty() / $orderItemQty;
             $GomageGiftWrap = $invoice->getStore()->roundPrice($GomageGiftWrap);
             $baseGomageGiftWrap = $invoice->getStore()->roundPrice($baseGomageGiftWrap);
             $item->setGomageGiftWrapAmount($GomageGiftWrap);
             $item->setBaseGomageGiftWrapAmount($baseGomageGiftWrap);
             $totalGomageGiftWrapAmount += $GomageGiftWrap;
             $baseTotalGomageGiftWrapAmount += $baseGomageGiftWrap;
         }
     }
     $invoice->setGomageGiftWrapAmount($totalGomageGiftWrapAmount);
     $invoice->setBaseGomageGiftWrapAmount($baseTotalGomageGiftWrapAmount);
     $invoice->setGrandTotal($invoice->getGrandTotal() + $totalGomageGiftWrapAmount);
     $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseTotalGomageGiftWrapAmount);
     return $this;
 }
Example #5
0
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $store = $invoice->getStore();
     $totalTax = 0;
     $baseTotalTax = 0;
     foreach ($invoice->getAllItems() as $item) {
         $orderItem = $item->getOrderItem();
         $orderItemQty = $orderItem->getQtyOrdered();
         if ($orderItemQty) {
             if ($orderItem->isDummy()) {
                 continue;
             }
             $weeeTaxAmount = $item->getWeeeTaxAppliedAmount() * $item->getQty();
             $baseWeeeTaxAmount = $item->getBaseWeeeTaxAppliedAmount() * $item->getQty();
             $item->setWeeeTaxAppliedRowAmount($weeeTaxAmount);
             $item->setBaseWeeeTaxAppliedRowAmount($baseWeeeTaxAmount);
             $newApplied = array();
             $applied = Mage::helper('weee')->getApplied($item);
             foreach ($applied as $one) {
                 $one['base_row_amount'] = $one['base_amount'] * $item->getQty();
                 $one['row_amount'] = $one['amount'] * $item->getQty();
                 $one['base_row_amount_incl_tax'] = $one['base_amount_incl_tax'] * $item->getQty();
                 $one['row_amount_incl_tax'] = $one['amount_incl_tax'] * $item->getQty();
                 $newApplied[] = $one;
             }
             Mage::helper('weee')->setApplied($item, $newApplied);
             $item->setWeeeTaxRowDisposition($item->getWeeeTaxDisposition() * $item->getQty());
             $item->setBaseWeeeTaxRowDisposition($item->getBaseWeeeTaxDisposition() * $item->getQty());
             $totalTax += $weeeTaxAmount;
             $baseTotalTax += $baseWeeeTaxAmount;
         }
     }
     /*
      * Add FPT to totals
      * Notice that we check restriction on allowed tax, because
      * a) for last invoice we don't need to collect FPT - it is automatically collected by subtotal/tax collector,
      * that adds whole remaining (not invoiced) subtotal/tax value, so fpt is automatically included into it
      * b) FPT tax is included into order subtotal/tax value, so after multiple invoices with partial item quantities
      * it can happen that other collector will take some FPT value from shared subtotal/tax order value
      */
     $order = $invoice->getOrder();
     if (Mage::helper('weee')->includeInSubtotal($store)) {
         $allowedSubtotal = $order->getSubtotal() - $order->getSubtotalInvoiced() - $invoice->getSubtotal();
         $allowedBaseSubtotal = $order->getBaseSubtotal() - $order->getBaseSubtotalInvoiced() - $invoice->getBaseSubtotal();
         $totalTax = min($allowedSubtotal, $totalTax);
         $baseTotalTax = min($allowedBaseSubtotal, $baseTotalTax);
         $invoice->setSubtotal($invoice->getSubtotal() + $totalTax);
         $invoice->setBaseSubtotal($invoice->getBaseSubtotal() + $baseTotalTax);
     } else {
         $allowedTax = $order->getTaxAmount() - $order->getTaxInvoiced() - $invoice->getTaxAmount();
         $allowedBaseTax = $order->getBaseTaxAmount() - $order->getBaseTaxInvoiced() - $invoice->getBaseTaxAmount();
         $totalTax = min($allowedTax, $totalTax);
         $baseTotalTax = min($allowedBaseTax, $baseTotalTax);
         $invoice->setTaxAmount($invoice->getTaxAmount() + $totalTax);
         $invoice->setBaseTaxAmount($invoice->getBaseTaxAmount() + $baseTotalTax);
     }
     $invoice->setGrandTotal($invoice->getGrandTotal() + $totalTax);
     $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseTotalTax);
     return $this;
 }
Example #6
0
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $invoice->setDiscountAmount(0);
     $invoice->setBaseDiscountAmount(0);
     $totalDiscountAmount = 0;
     $baseTotalDiscountAmount = 0;
     /**
      * Checking if shipping discount was added in previous invoices.
      * So basically if we have invoice with positive discount and it
      * was not canceled we don't add shipping discount to this one.
      */
     $addShippingDicount = true;
     foreach ($invoice->getOrder()->getInvoiceCollection() as $previusInvoice) {
         if ($previusInvoice->getDiscountAmount()) {
             $addShippingDicount = false;
         }
     }
     if ($addShippingDicount) {
         $totalDiscountAmount = $totalDiscountAmount + $invoice->getOrder()->getShippingDiscountAmount();
         $baseTotalDiscountAmount = $baseTotalDiscountAmount + $invoice->getOrder()->getBaseShippingDiscountAmount();
     }
     /** @var $item Mage_Sales_Model_Order_Invoice_Item */
     foreach ($invoice->getAllItems() as $item) {
         $orderItem = $item->getOrderItem();
         if ($orderItem->isDummy()) {
             continue;
         }
         $orderItemDiscount = (double) $orderItem->getDiscountAmount();
         $baseOrderItemDiscount = (double) $orderItem->getBaseDiscountAmount();
         $orderItemQty = $orderItem->getQtyOrdered();
         if ($orderItemDiscount && $orderItemQty) {
             /**
              * Resolve rounding problems
              *
              * We dont want to include the weee discount amount as the right amount
              * is added when calculating the taxes.
              *
              * Also the subtotal is without weee
              */
             $discount = $orderItemDiscount - $orderItem->getDiscountInvoiced();
             $baseDiscount = $baseOrderItemDiscount - $orderItem->getBaseDiscountInvoiced();
             if (!$item->isLast()) {
                 $activeQty = $orderItemQty - $orderItem->getQtyInvoiced();
                 $discount = $invoice->roundPrice($discount / $activeQty * $item->getQty(), 'regular', true);
                 $baseDiscount = $invoice->roundPrice($baseDiscount / $activeQty * $item->getQty(), 'base', true);
             }
             $item->setDiscountAmount($discount);
             $item->setBaseDiscountAmount($baseDiscount);
             $totalDiscountAmount += $discount;
             $baseTotalDiscountAmount += $baseDiscount;
         }
     }
     $invoice->setDiscountAmount(-$totalDiscountAmount);
     $invoice->setBaseDiscountAmount(-$baseTotalDiscountAmount);
     $invoice->setGrandTotal($invoice->getGrandTotal() - $totalDiscountAmount);
     $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() - $baseTotalDiscountAmount);
     return $this;
 }
Example #7
0
 /**
  * Collect invoice subtotal
  *
  * @param   Mage_Sales_Model_Order_Invoice $invoice
  * @return  Mage_Sales_Model_Order_Invoice_Total_Subtotal
  */
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $subtotal = 0;
     $baseSubtotal = 0;
     $subtotalInclTax = 0;
     $baseSubtotalInclTax = 0;
     $order = $invoice->getOrder();
     foreach ($invoice->getAllItems() as $item) {
         if ($item->getOrderItem()->isDummy()) {
             continue;
         }
         $item->calcRowTotal();
         $subtotal += $item->getRowTotal();
         $baseSubtotal += $item->getBaseRowTotal();
         $subtotalInclTax += $item->getRowTotalInclTax();
         $baseSubtotalInclTax += $item->getBaseRowTotalInclTax();
     }
     $allowedSubtotal = $order->getSubtotal() - $order->getSubtotalInvoiced();
     $baseAllowedSubtotal = $order->getBaseSubtotal() - $order->getBaseSubtotalInvoiced();
     $allowedSubtotalInclTax = $allowedSubtotal + $order->getHiddenTaxAmount() + $order->getTaxAmount() - $order->getTaxInvoiced() - $order->getHiddenTaxInvoiced();
     $baseAllowedSubtotalInclTax = $baseAllowedSubtotal + $order->getBaseHiddenTaxAmount() + $order->getBaseTaxAmount() - $order->getBaseTaxInvoiced() - $order->getBaseHiddenTaxInvoiced();
     /**
      * Check if shipping tax calculation is included to current invoice.
      */
     $includeShippingTax = true;
     foreach ($invoice->getOrder()->getInvoiceCollection() as $previousInvoice) {
         if ($previousInvoice->getShippingAmount() && !$previousInvoice->isCanceled()) {
             $includeShippingTax = false;
             break;
         }
     }
     if ($includeShippingTax) {
         $allowedSubtotalInclTax -= $order->getShippingTaxAmount();
         $baseAllowedSubtotalInclTax -= $order->getBaseShippingTaxAmount();
     } else {
         $allowedSubtotalInclTax += $order->getShippingHiddenTaxAmount();
         $baseAllowedSubtotalInclTax += $order->getBaseShippingHiddenTaxAmount();
     }
     if ($invoice->isLast()) {
         $subtotal = $allowedSubtotal;
         $baseSubtotal = $baseAllowedSubtotal;
         $subtotalInclTax = $allowedSubtotalInclTax;
         $baseSubtotalInclTax = $baseAllowedSubtotalInclTax;
     } else {
         $subtotal = min($allowedSubtotal, $subtotal);
         $baseSubtotal = min($baseAllowedSubtotal, $baseSubtotal);
         $subtotalInclTax = min($allowedSubtotalInclTax, $subtotalInclTax);
         $baseSubtotalInclTax = min($baseAllowedSubtotalInclTax, $baseSubtotalInclTax);
     }
     $invoice->setSubtotal($subtotal);
     $invoice->setBaseSubtotal($baseSubtotal);
     $invoice->setSubtotalInclTax($subtotalInclTax);
     $invoice->setBaseSubtotalInclTax($baseSubtotalInclTax);
     $invoice->setGrandTotal($invoice->getGrandTotal() + $subtotal);
     $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseSubtotal);
     return $this;
 }
 /**
  * Collect gift wrapping tax totals
  *
  * @param Mage_Sales_Model_Order_Invoice $invoice
  * @return Enterprise_GiftWrapping_Model_Total_Invoice_Tax_Giftwrapping
  */
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $order = $invoice->getOrder();
     /**
      * Wrapping for items
      */
     $invoiced = 0;
     $baseInvoiced = 0;
     foreach ($invoice->getAllItems() as $invoiceItem) {
         if (!$invoiceItem->getQty() || $invoiceItem->getQty() == 0) {
             continue;
         }
         $orderItem = $invoiceItem->getOrderItem();
         if ($orderItem->getGwId() && $orderItem->getGwBaseTaxAmount() && $orderItem->getGwBaseTaxAmount() != $orderItem->getGwBaseTaxAmountInvoiced()) {
             $orderItem->setGwBaseTaxAmountInvoiced($orderItem->getGwBaseTaxAmount());
             $orderItem->setGwTaxAmountInvoiced($orderItem->getGwTaxAmount());
             $baseInvoiced += $orderItem->getGwBaseTaxAmount();
             $invoiced += $orderItem->getGwTaxAmount();
         }
     }
     if ($invoiced > 0 || $baseInvoiced > 0) {
         $order->setGwItemsBaseTaxInvoiced($order->getGwItemsBaseTaxInvoiced() + $baseInvoiced);
         $order->setGwItemsTaxInvoiced($order->getGwItemsTaxInvoiced() + $invoiced);
         $invoice->setGwItemsBaseTaxAmount($baseInvoiced);
         $invoice->setGwItemsTaxAmount($invoiced);
     }
     /**
      * Wrapping for order
      */
     if ($order->getGwId() && $order->getGwBaseTaxAmount() && $order->getGwBaseTaxAmount() != $order->getGwBaseTaxAmountInvoiced()) {
         $order->setGwBaseTaxAmountInvoiced($order->getGwBaseTaxAmount());
         $order->setGwTaxAmountInvoiced($order->getGwTaxAmount());
         $invoice->setGwBaseTaxAmount($order->getGwBaseTaxAmount());
         $invoice->setGwTaxAmount($order->getGwTaxAmount());
     }
     /**
      * Printed card
      */
     if ($order->getGwAddCard() && $order->getGwCardBaseTaxAmount() && $order->getGwCardBaseTaxAmount() != $order->getGwCardBaseTaxInvoiced()) {
         $order->setGwCardBaseTaxInvoiced($order->getGwCardBaseTaxAmount());
         $order->setGwCardTaxInvoiced($order->getGwCardTaxAmount());
         $invoice->setGwCardBaseTaxAmount($order->getGwCardBaseTaxAmount());
         $invoice->setGwCardTaxAmount($order->getGwCardTaxAmount());
     }
     if (!$invoice->isLast()) {
         $baseTaxAmount = $invoice->getGwItemsBaseTaxAmount() + $invoice->getGwBaseTaxAmount() + $invoice->getGwCardBaseTaxAmount();
         $taxAmount = $invoice->getGwItemsTaxAmount() + $invoice->getGwTaxAmount() + $invoice->getGwCardTaxAmount();
         $invoice->setBaseTaxAmount($invoice->getBaseTaxAmount() + $baseTaxAmount);
         $invoice->setTaxAmount($invoice->getTaxAmount() + $taxAmount);
         $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseTaxAmount);
         $invoice->setGrandTotal($invoice->getGrandTotal() + $taxAmount);
     }
     return $this;
 }
Example #9
0
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $invoice->setDiscountAmount(0);
     $invoice->setBaseDiscountAmount(0);
     $totalDiscountAmount = 0;
     $baseTotalDiscountAmount = 0;
     /**
      * Checking if shipping discount was added in previous invoices.
      * So basically if we have invoice with positive discount and it
      * was not canceled we don't add shipping discount to this one.
      */
     $addShippingDicount = true;
     foreach ($invoice->getOrder()->getInvoiceCollection() as $previusInvoice) {
         if ($previusInvoice->getDiscountAmount()) {
             $addShippingDicount = false;
         }
     }
     if ($addShippingDicount) {
         $totalDiscountAmount = $totalDiscountAmount + $invoice->getOrder()->getShippingDiscountAmount();
         $baseTotalDiscountAmount = $baseTotalDiscountAmount + $invoice->getOrder()->getBaseShippingDiscountAmount();
     }
     foreach ($invoice->getAllItems() as $item) {
         if ($item->getOrderItem()->isDummy()) {
             continue;
         }
         $orderItem = $item->getOrderItem();
         $orderItemDiscount = (double) $orderItem->getDiscountAmount();
         $baseOrderItemDiscount = (double) $orderItem->getBaseDiscountAmount();
         $orderItemQty = $orderItem->getQtyOrdered();
         if ($orderItemDiscount && $orderItemQty) {
             /**
              * Resolve rounding problems
              */
             if ($item->isLast()) {
                 $discount = $orderItemDiscount - $orderItem->getDiscountInvoiced();
                 $baseDiscount = $baseOrderItemDiscount - $orderItem->getBaseDiscountInvoiced();
             } else {
                 $discount = $orderItemDiscount * $item->getQty() / $orderItemQty;
                 $baseDiscount = $baseOrderItemDiscount * $item->getQty() / $orderItemQty;
                 $discount = $invoice->getStore()->roundPrice($discount);
                 $baseDiscount = $invoice->getStore()->roundPrice($baseDiscount);
             }
             $item->setDiscountAmount($discount);
             $item->setBaseDiscountAmount($baseDiscount);
             $totalDiscountAmount += $discount;
             $baseTotalDiscountAmount += $baseDiscount;
         }
     }
     $invoice->setDiscountAmount($totalDiscountAmount);
     $invoice->setBaseDiscountAmount($baseTotalDiscountAmount);
     $invoice->setGrandTotal($invoice->getGrandTotal() - $totalDiscountAmount);
     $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() - $baseTotalDiscountAmount);
     return $this;
 }
Example #10
0
 /**
  * Collect total cost of invoiced items
  *
  * @param Mage_Sales_Model_Order_Invoice $invoice
  * @return Mage_Sales_Model_Order_Invoice_Total_Cost
  */
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $baseInvoiceTotalCost = 0;
     foreach ($invoice->getAllItems() as $item) {
         if (!$item->getHasChildren()) {
             $baseInvoiceTotalCost += $item->getBaseCost() * $item->getQty();
         }
     }
     $invoice->setBaseCost($baseInvoiceTotalCost);
     return $this;
 }
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $totalTax = 0;
     $baseTotalTax = 0;
     foreach ($invoice->getAllItems() as $item) {
         $orderItem = $item->getOrderItem();
         $orderItemTax = $orderItem->getTaxAmount();
         $baseOrderItemTax = $orderItem->getBaseTaxAmount();
         $orderItemQty = $orderItem->getQtyOrdered();
         if ($orderItemTax && $orderItemQty) {
             if ($item->getOrderItem()->isDummy()) {
                 continue;
             }
             /**
              * Resolve rounding problems
              */
             if ($item->isLast()) {
                 $tax = $orderItemTax - $orderItem->getTaxInvoiced();
                 $baseTax = $baseOrderItemTax - $orderItem->getBaseTaxInvoiced();
             } else {
                 $tax = $orderItemTax * $item->getQty() / $orderItemQty;
                 $baseTax = $baseOrderItemTax * $item->getQty() / $orderItemQty;
                 $tax = $invoice->getStore()->roundPrice($tax);
                 $baseTax = $invoice->getStore()->roundPrice($baseTax);
             }
             $item->setTaxAmount($tax);
             $item->setBaseTaxAmount($baseTax);
             $totalTax += $tax;
             $baseTotalTax += $baseTax;
         }
     }
     $includeShippingTax = true;
     /**
      * Check shipping amount in previus invoices
      */
     foreach ($invoice->getOrder()->getInvoiceCollection() as $previusInvoice) {
         if ($previusInvoice->getShippingAmount() && !$previusInvoice->isCanceled()) {
             $includeShippingTax = false;
         }
     }
     if ($includeShippingTax) {
         $totalTax += $invoice->getOrder()->getShippingTaxAmount();
         $baseTotalTax += $invoice->getOrder()->getBaseShippingTaxAmount();
         $invoice->setShippingTaxAmount($invoice->getOrder()->getShippingTaxAmount());
         $invoice->setBaseShippingTaxAmount($invoice->getOrder()->getBaseShippingTaxAmount());
     }
     $invoice->setTaxAmount($totalTax);
     $invoice->setBaseTaxAmount($baseTotalTax);
     $invoice->setGrandTotal($invoice->getGrandTotal() + $totalTax);
     $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseTotalTax);
     return $this;
 }
Example #12
0
 /**
  * Collect invoice subtotal
  *
  * @param   Mage_Sales_Model_Order_Invoice $invoice
  * @return  Mage_Sales_Model_Order_Invoice_Total_Subtotal
  */
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $subtotal = 0;
     $baseSubtotal = 0;
     foreach ($invoice->getAllItems() as $item) {
         $item->calcRowTotal();
         $subtotal += $item->getRowTotal();
         $baseSubtotal += $item->getBaseRowTotal();
     }
     $invoice->setSubtotal($subtotal);
     $invoice->setBaseSubtotal($baseSubtotal);
     $invoice->setGrandTotal($invoice->getGrandTotal() + $subtotal);
     $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseSubtotal);
     return $this;
 }
Example #13
0
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $order = $invoice->getOrder();
     //$affiliate = Mage::getModel('credit/creditorder')->load($order->getIncrementId())->getAffiliate();
     $affiliate = 0;
     //$credit = Mage::getModel('credit/creditorder')->load($order->getIncrementId())->getCredit();
     $credit = 0;
     $totalDiscountAmount = $affiliate + $credit;
     $baseTotalDiscountAmount = $affiliate + $credit;
     $items = $invoice->getAllItems();
     if (!count($items)) {
         return $this;
     }
     $invoice->setBaseDiscountAmount($invoice->getBaseDiscountAmount() - $baseTotalDiscountAmount);
     $invoice->setGrandTotal($invoice->getGrandTotal() - $totalDiscountAmount);
     $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() - $baseTotalDiscountAmount);
     return $this;
 }
Example #14
0
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $store = $invoice->getStore();
     $totalTax = 0;
     $baseTotalTax = 0;
     foreach ($invoice->getAllItems() as $item) {
         $orderItem = $item->getOrderItem();
         $orderItemQty = $orderItem->getQtyOrdered();
         if ($orderItemQty) {
             if ($orderItem->isDummy()) {
                 continue;
             }
             $weeeTaxAmount = $item->getWeeeTaxAppliedAmount() * $item->getQty();
             $baseWeeeTaxAmount = $item->getBaseWeeeTaxAppliedAmount() * $item->getQty();
             $item->setWeeeTaxAppliedRowAmount($weeeTaxAmount);
             $item->setBaseWeeeTaxAppliedRowAmount($baseWeeeTaxAmount);
             $newApplied = array();
             $applied = Mage::helper('weee')->getApplied($item);
             foreach ($applied as $one) {
                 $one['base_row_amount'] = $one['base_amount'] * $item->getQty();
                 $one['row_amount'] = $one['amount'] * $item->getQty();
                 $one['base_row_amount_incl_tax'] = $one['base_amount_incl_tax'] * $item->getQty();
                 $one['row_amount_incl_tax'] = $one['amount_incl_tax'] * $item->getQty();
                 $newApplied[] = $one;
             }
             Mage::helper('weee')->setApplied($item, $newApplied);
             $item->setWeeeTaxRowDisposition($item->getWeeeTaxDisposition() * $item->getQty());
             $item->setBaseWeeeTaxRowDisposition($item->getBaseWeeeTaxDisposition() * $item->getQty());
             $totalTax += $weeeTaxAmount;
             $baseTotalTax += $baseWeeeTaxAmount;
         }
     }
     if (Mage::helper('weee')->includeInSubtotal($store)) {
         $invoice->setSubtotal($invoice->getSubtotal() + $totalTax);
         $invoice->setBaseSubtotal($invoice->getBaseSubtotal() + $baseTotalTax);
     } else {
         $invoice->setTaxAmount($invoice->getTaxAmount() + $totalTax);
         $invoice->setBaseTaxAmount($invoice->getBaseTaxAmount() + $baseTotalTax);
     }
     $invoice->setGrandTotal($invoice->getGrandTotal() + $totalTax);
     $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseTotalTax);
     return $this;
 }
Example #15
0
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $order = $invoice->getOrder();
     if (!$order->getRewardpointsInvitedBaseDiscount()) {
         return $this;
     }
     $invoice->setRewardpointsInvitedDiscount(0);
     $invoice->setRewardpointsInvitedBaseDiscount(0);
     $totalDiscountAmount = 0;
     $baseTotalDiscountAmount = 0;
     if ($invoice->isLast()) {
         $baseTotalDiscountAmount = $order->getRewardpointsInvitedBaseDiscount();
         $totalDiscountAmount = $order->getRewardpointsInvitedDiscount();
         foreach ($order->getInvoiceCollection() as $existedInvoice) {
             $baseTotalDiscountAmount -= $existedInvoice->getRewardpointsInvitedBaseDiscount();
             $totalDiscountAmount -= $existedInvoice->getRewardpointsInvitedDiscount();
         }
     } else {
         foreach ($invoice->getAllItems() as $item) {
             $orderItem = $item->getOrderItem();
             if ($orderItem->isDummy()) {
                 continue;
             }
             $orderItemDiscount = (double) $orderItem->getRewardpointsInvitedDiscount();
             $baseOrderItemDiscount = (double) $orderItem->getRewardpointsInvitedBaseDiscount();
             $orderItemQty = $orderItem->getQtyOrdered();
             if ($orderItemDiscount && $orderItemQty) {
                 $discount = $invoice->roundPrice($orderItemDiscount / $orderItemQty * $item->getQty(), 'regular', true);
                 $baseDiscount = $invoice->roundPrice($baseOrderItemDiscount / $orderItemQty * $item->getQty(), 'base', true);
                 $item->setRewardpointsInvitedDiscount($discount);
                 $item->setRewardpointsInvitedBaseDiscount($baseDiscount);
                 $totalDiscountAmount += $discount;
                 $baseTotalDiscountAmount += $baseDiscount;
             }
         }
     }
     $invoice->setRewardpointsInvitedDiscount($totalDiscountAmount);
     $invoice->setRewardpointsInvitedBaseDiscount($baseTotalDiscountAmount);
     $invoice->setGrandTotal($invoice->getGrandTotal() - $totalDiscountAmount);
     $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() - $baseTotalDiscountAmount);
     return $this;
 }
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $invoice->setDiscountAmount(0);
     $invoice->setBaseDiscountAmount(0);
     $totalDiscountAmount = 0;
     $baseTotalDiscountAmount = 0;
     foreach ($invoice->getAllItems() as $item) {
         if ($item->getOrderItem()->isDummy()) {
             continue;
         }
         $orderItem = $item->getOrderItem();
         $orderItemDiscount = (double) $orderItem->getDiscountAmount();
         $baseOrderItemDiscount = (double) $orderItem->getBaseDiscountAmount();
         $orderItemQty = $orderItem->getQtyOrdered();
         if ($orderItemDiscount && $orderItemQty) {
             /**
              * Resolve rounding problems
              */
             if ($item->isLast()) {
                 $discount = $orderItemDiscount - $orderItem->getDiscountInvoiced();
                 $baseDiscount = $baseOrderItemDiscount - $orderItem->getBaseDiscountInvoiced();
             } else {
                 $discount = $orderItemDiscount * $item->getQty() / $orderItemQty;
                 $baseDiscount = $baseOrderItemDiscount * $item->getQty() / $orderItemQty;
                 $discount = $invoice->getStore()->roundPrice($discount);
                 $baseDiscount = $invoice->getStore()->roundPrice($baseDiscount);
             }
             $item->setDiscountAmount($discount);
             $item->setBaseDiscountAmount($baseDiscount);
             $totalDiscountAmount += $discount;
             $baseTotalDiscountAmount += $baseDiscount;
         }
     }
     $invoice->setDiscountAmount($totalDiscountAmount);
     $invoice->setBaseDiscountAmount($baseTotalDiscountAmount);
     $invoice->setGrandTotal($invoice->getGrandTotal() - $totalDiscountAmount);
     $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() - $baseTotalDiscountAmount);
     return $this;
 }
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     // Changed By Adam 22/09/2014
     if (!Mage::helper('affiliateplus')->isAffiliateModuleEnabled()) {
         return $this;
     }
     $baseDiscount = 0;
     $discount = 0;
     foreach ($invoice->getAllItems() as $item) {
         if ($item->getOrderItem()->isDummy()) {
             continue;
         }
         $orderItem = $item->getOrderItem();
         $orderItemDiscount = (double) $orderItem->getAffiliateplusAmount();
         $baseOrderItemDiscount = (double) $orderItem->getBaseAffiliateplusAmount();
         $orderItemQty = $orderItem->getQtyOrdered();
         if ($orderItemDiscount && $orderItemQty) {
             $discount -= $orderItemDiscount * $item->getQty() / $orderItemQty;
             $baseDiscount -= $baseOrderItemDiscount * $item->getQty() / $orderItemQty;
         }
     }
     /* Changed By Adam 30/09/2014: to solve the problem: 
      * invoice san pham ko phai affiliate nhung van hien discount
      */
     //        if (!floatval($baseDiscount)) {
     //            $order = $invoice->getOrder();
     //            $baseDiscount = $order->getBaseAffiliateplusDiscount();
     //            $discount = $order->getAffiliateplusDiscount();
     //        }
     if (floatval($baseDiscount)) {
         $baseDiscount = Mage::app()->getStore()->roundPrice($baseDiscount);
         $discount = Mage::app()->getStore()->roundPrice($discount);
         $invoice->setBaseAffiliateplusDiscount($baseDiscount);
         $invoice->setAffiliateplusDiscount($discount);
         $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseDiscount);
         $invoice->setGrandTotal($invoice->getGrandTotal() + $discount);
     }
     return $this;
 }
Example #18
0
 /**
  * @param Mage_Sales_Model_Order_Invoice $invoice
  *
  * @return Bronto_Common_Model_Email_Template_Filter
  */
 protected function _filterInvoice(Mage_Sales_Model_Order_Invoice $invoice)
 {
     if (!in_array('invoice', $this->_filteredObjects)) {
         $index = 1;
         foreach ($invoice->getAllItems() as $item) {
             $_item = $item->getOrderItem();
             if (!$_item->getParentItem()) {
                 $this->_filterOrderItem($_item, $index);
                 $index++;
             }
         }
         // Add Related Content
         $this->_items = $invoice->getAllItems();
         $this->setField('invoiceIncrementId', $invoice->getIncrementId());
         $this->setField('invoiceItems', $this->_filterInvoiceItems($invoice));
         $this->_filteredObjects[] = 'invoice';
     }
     return $this;
 }
 public static function invoiceCalculation(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $params = array();
     /* @var $helper Dutycalculator_Charge_Helper_Data */
     $helper = Mage::helper('dccharge');
     $invoiceItems = $invoice->getAllItems();
     $params['calculation_id'] = $invoice->getOrder()->getDcOrderId();
     $orderShippingAmount = (double) $invoice->getOrder()->getShippingAmount();
     if ($orderShippingAmount) {
         foreach ($invoice->getOrder()->getInvoiceCollection() as $previusInvoice) {
             if ($previusInvoice->getShippingAmount() && $previusInvoice->getDcOrderId() != 0 && !$previusInvoice->isCanceled()) {
                 $orderShippingAmount = 0;
             }
         }
     } else {
         $orderShippingAmount = 0;
     }
     $params['shipping'] = $orderShippingAmount;
     $params['output_currency'] = $invoice->getOrderCurrencyCode();
     $params['cat'] = array();
     $params['qty'] = array();
     $params['reference'] = array();
     $idx = 0;
     $itemsToSend = 0;
     foreach ($invoiceItems as $invoiceItem) {
         $orderItem = $invoiceItem->getOrderItem();
         $product = Mage::getModel('catalog/product')->load($orderItem->getProductId());
         $qty = $invoiceItem->getQty();
         if ($orderItem->getParentItemId() || !$orderItem->getQuoteItemId() || $product->isVirtual() || $qty <= 0) {
             continue;
         }
         $itemsToSend++;
         /* @var $invoiceItem Mage_Sales_Model_Order_Invoice_Item */
         /* @var $product Mage_Catalog_Model_Product */
         $params['qty'][$idx] = (double) $qty;
         $params['reference'][$idx] = $orderItem->getQuoteItemId();
         if ($product->getDcProductId()) {
             $params['cat'][$idx] = $product->getDcProductId();
         } else {
             $params['cat'][$idx] = '';
         }
         $idx++;
     }
     if ($itemsToSend > 0) {
         $rawXml = $helper->sendRequest('invoice_calculation', $params);
         try {
             if (stripos($rawXml, '<?xml') === false) {
                 throw new Exception($rawXml);
             }
             $answer = new SimpleXMLElement($rawXml);
             $answerAttributes = $answer->attributes();
             $dcOrderId = (int) $answerAttributes['id'];
             $totals = current($answer->xpath('total-charges'));
             $items = $answer->xpath('item');
             $result = array();
             $result['dc_order_id'] = $dcOrderId;
             $result['total'] = (double) $totals->total->amount;
             $result['duty'] = (double) $totals->duty->amount;
             $additionalTaxes = $totals->xpath('additional-import-taxes');
             if ($additionalTaxes) {
                 $additionalTaxes = current($additionalTaxes);
                 foreach ($additionalTaxes->tax as $additionalTax) {
                     $result['duty'] += (double) $additionalTax->amount;
                 }
             }
             $result['sales_tax'] = (double) current($totals->xpath('sales-tax'))->amount;
             $result['items'] = array();
             $result['aggregated_items'] = array();
             foreach ($items as $item) {
                 $attributes = $item->attributes();
                 $references = explode(',', (string) $attributes->reference);
                 if (count($references) > 1) {
                     $total = (double) $item->total->amount;
                     $duty = (double) $item->duty->amount;
                     $additionalTaxes = $item->xpath('additional-import-taxes');
                     if ($additionalTaxes) {
                         $additionalTaxes = current($additionalTaxes);
                         foreach ($additionalTaxes->tax as $additionalTax) {
                             $duty += (double) $additionalTax->amount;
                         }
                     }
                     $salesTax = (double) current($item->xpath('sales-tax'))->amount;
                     $result['aggregated_items'][(string) $attributes->reference] = array('items' => $references, 'aggregated_total' => $total, 'aggregated_duty' => $duty, 'aggregated_sales_tax' => $salesTax);
                 } else {
                     $total = (double) $item->total->amount;
                     $duty = (double) $item->duty->amount;
                     $additionalTaxes = $item->xpath('additional-import-taxes');
                     if ($additionalTaxes) {
                         $additionalTaxes = current($additionalTaxes);
                         foreach ($additionalTaxes->tax as $additionalTax) {
                             $duty += (double) $additionalTax->amount;
                         }
                     }
                     $salesTax = (double) current($item->xpath('sales-tax'))->amount;
                     $result['items'][(string) $attributes->reference] = array('total' => $total, 'duty' => $duty, 'sales_tax' => $salesTax);
                 }
             }
             return $result;
         } catch (Exception $ex) {
             $result = array();
             $result['failed_calculation'] = 1;
             $result['dc_order_id'] = 0;
             $result['total'] = 0;
             $result['duty'] = 0;
             $result['sales_tax'] = 0;
             $result['items'] = array();
             $result['aggregated_items'] = array();
             return $result;
         }
     } else {
         return false;
     }
 }
Example #20
0
 /**
  * Collect total when create Invoice
  * 
  * @param Mage_Sales_Model_Order_Invoice $invoice
  */
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $order = $invoice->getOrder();
     /**
      * update 2.0
      */
     $earnPoint = 0;
     $maxEarn = $order->getRewardpointsEarn();
     $maxEarn -= (int) Mage::getResourceModel('rewardpoints/transaction_collection')->addFieldToFilter('action', 'earning_invoice')->addFieldToFilter('order_id', $order->getId())->getFieldTotal();
     if ($maxEarn >= 0) {
         foreach ($invoice->getAllItems() as $item) {
             $orderItem = $item->getOrderItem();
             if ($orderItem->isDummy()) {
                 continue;
             }
             $itemPoint = (int) $orderItem->getRewardpointsEarn();
             $itemPoint = $itemPoint * $item->getQty() / $orderItem->getQtyOrdered();
             $earnPoint += floor($itemPoint);
         }
         if ($invoice->isLast() || $earnPoint >= $maxEarn) {
             $earnPoint = $maxEarn;
         }
         $invoice->setRewardpointsEarn($earnPoint);
     }
     if ($order->getRewardpointsDiscount() < 0.0001) {
         return;
     }
     if ($invoice->isLast()) {
         $baseDiscount = $order->getRewardpointsBaseDiscount();
         $discount = $order->getRewardpointsDiscount();
         foreach ($order->getInvoiceCollection() as $existedInvoice) {
             if ($baseDiscount > 0.0001) {
                 $baseDiscount -= $existedInvoice->getRewardpointsBaseDiscount();
                 $discount -= $existedInvoice->getRewardpointsDiscount();
             }
         }
     } else {
         $orderTotal = $order->getGrandTotal() + $order->getRewardpointsDiscount();
         $ratio = $invoice->getGrandTotal() / $orderTotal;
         $baseDiscount = $order->getRewardpointsBaseDiscount() * $ratio;
         $discount = $order->getRewardpointsDiscount() * $ratio;
         $maxBaseDiscount = $order->getRewardpointsBaseDiscount();
         $maxDiscount = $order->getRewardpointsDiscount();
         foreach ($order->getInvoiceCollection() as $existedInvoice) {
             if ($maxBaseDiscount > 0.0001) {
                 $maxBaseDiscount -= $existedInvoice->getRewardpointsBaseDiscount();
                 $maxDiscount -= $existedInvoice->getRewardpointsDiscount();
             }
         }
         if ($baseDiscount > $maxBaseDiscount) {
             $baseDiscount = $maxBaseDiscount;
             $discount = $maxDiscount;
         }
     }
     if ($baseDiscount > 0.0001) {
         if ($invoice->getBaseGrandTotal() <= $baseDiscount) {
             $invoice->setRewardpointsBaseDiscount($invoice->getBaseGrandTotal());
             $invoice->setRewardpointsDiscount($invoice->getGrandTotal());
             $invoice->setBaseGrandTotal(0.0);
             $invoice->setGrandTotal(0.0);
         } else {
             $invoice->setRewardpointsBaseDiscount($baseDiscount);
             $invoice->setRewardpointsDiscount($discount);
             $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() - $baseDiscount);
             $invoice->setGrandTotal($invoice->getGrandTotal() - $discount);
         }
     }
 }
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $currencyFrom = Mage::getModel('directory/currency')->load($invoice->getOrderCurrencyCode());
     $currencyTo = $invoice->getStore()->getBaseCurrency();
     /* @var $helper Dutycalculator_Charge_Helper_Data */
     $helper = Mage::helper('dccharge');
     $invoice->setImportDutyTax(0);
     $invoice->setBaseImportDutyTax(0);
     $invoice->setImportDuty(0);
     $invoice->setBaseImportDuty(0);
     $invoice->setSalesTax(0);
     $invoice->setBaseSalesTax(0);
     $invoice->setDeliveryDutyType($invoice->getOrder()->getDeliveryDutyType());
     $invoice->setFailedCalculation($invoice->getOrder()->getFailedCalculation());
     $invoice->setDcOrderId(0);
     foreach ($invoice->getAllItems() as $invoiceItem) {
         $invoiceItem->setImportDutyTax(0);
         $invoiceItem->setBaseImportDutyTax(0);
         $invoiceItem->setImportDuty(0);
         $invoiceItem->setBaseImportDuty(0);
         $invoiceItem->setSalesTax(0);
         $invoiceItem->setBaseSalesTax(0);
     }
     if ($invoice->getOrder()->getDcOrderId()) {
         $result = Dutycalculator_Charge_Model_Importdutytaxes::invoiceCalculation($invoice);
         if ($result) {
             $amountToInvoice = $result['total'];
             $baseAmountToInvoice = $helper->convertPrice($currencyFrom, $currencyTo, $result['total']);
             $invoice->setImportDutyTax($result['total']);
             $invoice->setBaseImportDutyTax($helper->convertPrice($currencyFrom, $currencyTo, $result['total']));
             $invoice->setImportDuty($result['duty']);
             $invoice->setBaseImportDuty($helper->convertPrice($currencyFrom, $currencyTo, $result['duty']));
             $invoice->setSalesTax($result['sales_tax']);
             $invoice->setBaseSalesTax($helper->convertPrice($currencyFrom, $currencyTo, $result['sales_tax']));
             $invoice->setDeliveryDutyType($invoice->getOrder()->getDeliveryDutyType());
             $invoice->setFailedCalculation($invoice->getOrder()->getFailedCalculation());
             $invoice->setDcOrderId($result['dc_order_id']);
             if ($invoice->getOrder()->getDeliveryDutyType() == Dutycalculator_Charge_Helper_Data::DC_DELIVERY_TYPE_DDP) {
                 $invoice->setGrandTotal($invoice->getGrandTotal() + $amountToInvoice);
                 $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseAmountToInvoice);
                 $aggregatedItemsValues = array();
                 foreach ($invoice->getAllItems() as $invoiceItem) {
                     if ($invoiceItem->getOrderItem()->getParentItemId()) {
                         continue;
                     }
                     $id = $invoiceItem->getOrderItem()->getQuoteItemId();
                     if (isset($result['items'][$id])) {
                         $invoiceItem->setImportDutyTax($result['items'][$id]['total']);
                         $invoiceItem->setBaseImportDutyTax($helper->convertPrice($currencyFrom, $currencyTo, $result['items'][$id]['total']));
                         $invoiceItem->setImportDuty($result['items'][$id]['duty']);
                         $invoiceItem->setBaseImportDuty($helper->convertPrice($currencyFrom, $currencyTo, $result['items'][$id]['duty']));
                         $invoiceItem->setSalesTax($result['items'][$id]['sales_tax']);
                         $invoiceItem->setBaseSalesTax($helper->convertPrice($currencyFrom, $currencyTo, $result['items'][$id]['sales_tax']));
                     } else {
                         foreach ($result['aggregated_items'] as $key => $_items) {
                             if (in_array($id, $_items['items'])) {
                                 $aggregatedItemsValues[$key][$id] = $invoiceItem->getRowTotal();
                             }
                         }
                     }
                 }
                 $totals = array();
                 $totalDuty = array();
                 $totalSalesTaxes = array();
                 foreach ($aggregatedItemsValues as $key => $aggregatedItemsValue) {
                     $aggregatedTotal = $result['aggregated_items'][$key]['aggregated_total'];
                     $aggregatedDuty = $result['aggregated_items'][$key]['aggregated_duty'];
                     $aggregatedSalesTax = $result['aggregated_items'][$key]['aggregated_sales_tax'];
                     $totalAggregatedItemsValue = array_sum($aggregatedItemsValue);
                     foreach ($aggregatedItemsValue as $itemId => $value) {
                         $totals[$itemId] = round($value / $totalAggregatedItemsValue * $aggregatedTotal, 2);
                         $totalDuty[$itemId] = round($value / $totalAggregatedItemsValue * $aggregatedDuty, 2);
                         $totalSalesTaxes[$itemId] = round($value / $totalAggregatedItemsValue * $aggregatedSalesTax, 2);
                     }
                 }
                 foreach ($invoice->getAllItems() as $invoiceItem) {
                     if ($invoiceItem->getOrderItem()->getParentItemId()) {
                         continue;
                     }
                     $id = $invoiceItem->getOrderItem()->getQuoteItemId();
                     if (isset($taxes[$id])) {
                         $invoiceItem->setImportDutyTax($totals[$id]);
                         $invoiceItem->setBaseImportDutyTax($helper->convertPrice($currencyFrom, $currencyTo, $totals[$id]));
                         $invoiceItem->setImportDuty($totalDuty[$id]);
                         $invoiceItem->setBaseImportDuty($helper->convertPrice($currencyFrom, $currencyTo, $totalDuty[$id]));
                         $invoiceItem->setSalesTax($totalSalesTaxes[$id]);
                         $invoiceItem->setBaseSalesTax($helper->convertPrice($currencyFrom, $currencyTo, $totalSalesTaxes[$id]));
                     }
                     if ($invoiceItem->getQty() == 0) {
                         $invoiceItem->setImportDutyTax(0);
                         $invoiceItem->setBaseImportDutyTax(0);
                         $invoiceItem->setImportDuty(0);
                         $invoiceItem->setBaseImportDuty(0);
                         $invoiceItem->setSalesTax(0);
                         $invoiceItem->setBaseSalesTax(0);
                     }
                 }
             }
         }
     }
     return $this;
 }
Example #22
0
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $order = $invoice->getOrder();
     if ($order->getCustomercreditDiscount() < 0.0001) {
         return;
     }
     $invoice->setBaseCustomercreditDiscount(0);
     $invoice->setCustomercreditDiscount(0);
     $totalDiscountInvoiced = 0;
     $totalBaseDiscountInvoiced = 0;
     $totalDiscountAmount = 0;
     $totalBaseDiscountAmount = 0;
     $totalHiddenTax = 0;
     $totalBaseHiddenTax = 0;
     $hiddenTaxInvoiced = 0;
     $baseHiddenTaxInvoiced = 0;
     $checkAddShipping = true;
     foreach ($order->getInvoiceCollection() as $previousInvoice) {
         if ($previousInvoice->getCustomercreditDiscount()) {
             $checkAddShipping = false;
             $totalBaseDiscountInvoiced += $previousInvoice->getBaseCustomercreditDiscount();
             $totalDiscountInvoiced += $previousInvoice->getCustomercreditDiscount();
             $hiddenTaxInvoiced += $previousInvoice->getCustomercreditHiddenTax();
             $baseHiddenTaxInvoiced += $previousInvoice->getBaseCustomercreditHiddenTax();
         }
     }
     if ($checkAddShipping) {
         $totalBaseDiscountAmount += $order->getBaseCustomercreditDiscountForShipping();
         $totalDiscountAmount += $order->getCustomercreditDiscountForShipping();
         $totalBaseHiddenTax += $order->getBaseCustomercreditShippingHiddenTax();
         $totalHiddenTax += $order->getCustomercreditShippingHiddenTax();
     }
     if ($invoice->isLast()) {
         $totalBaseDiscountAmount = $order->getBaseCustomercreditDiscount() - $totalBaseDiscountInvoiced;
         $totalDiscountAmount = $order->getCustomercreditDiscount() - $totalDiscountInvoiced;
         $totalHiddenTax = $order->getCustomercreditHiddenTax() - $hiddenTaxInvoiced;
         $totalBaseHiddenTax = $order->getBaseCustomercreditHiddenTax() - $baseHiddenTaxInvoiced;
     } else {
         foreach ($invoice->getAllItems() as $item) {
             $orderItem = $item->getOrderItem();
             if ($orderItem->isDummy()) {
                 continue;
             }
             $baseOrderItemCustomercreditDiscount = (double) $orderItem->getBaseCustomercreditDiscount();
             $orderItemCustomercreditDiscount = (double) $orderItem->getCustomercreditDiscount();
             $baseOrderItemHiddenTax = (double) $orderItem->getBaseCustomercreditHiddenTax();
             $orderItemHiddenTax = (double) $orderItem->getCustomercreditHiddenTax();
             $orderItemQty = $orderItem->getQtyOrdered();
             $invoiceItemQty = $item->getQty();
             if ($baseOrderItemCustomercreditDiscount && $orderItemQty) {
                 if (version_compare(Mage::getVersion(), '1.7.0.0', '>=')) {
                     $totalBaseDiscountAmount += $invoice->roundPrice($baseOrderItemCustomercreditDiscount / $orderItemQty * $invoiceItemQty, 'base', true);
                     $totalDiscountAmount += $invoice->roundPrice($orderItemCustomercreditDiscount / $orderItemQty * $invoiceItemQty, 'regular', true);
                     $totalHiddenTax += $invoice->roundPrice($orderItemHiddenTax / $orderItemQty * $invoiceItemQty, 'regular', true);
                     $totalBaseHiddenTax += $invoice->roundPrice($baseOrderItemHiddenTax / $orderItemQty * $invoiceItemQty, 'base', true);
                 } else {
                     $totalBaseDiscountAmount += $baseOrderItemCustomercreditDiscount / $orderItemQty * $invoiceItemQty;
                     $totalDiscountAmount += $orderItemCustomercreditDiscount / $orderItemQty * $invoiceItemQty;
                     $totalHiddenTax += $orderItemHiddenTax / $orderItemQty * $invoiceItemQty;
                     $totalBaseHiddenTax += $baseOrderItemHiddenTax / $orderItemQty * $invoiceItemQty;
                 }
             }
         }
     }
     $invoice->setBaseCustomercreditDiscount($totalBaseDiscountAmount);
     $invoice->setCustomercreditDiscount($totalDiscountAmount);
     $invoice->setBaseCustomercreditHiddenTax($totalBaseHiddenTax);
     $invoice->setCustomercreditHiddenTax($totalHiddenTax);
     $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() - $totalBaseDiscountAmount + $totalBaseHiddenTax);
     $invoice->setGrandTotal($invoice->getGrandTotal() - $totalDiscountAmount + $totalHiddenTax);
 }
Example #23
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);
 }
Example #24
0
 public function convertInvoiceToShipment(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $convertOrder = new Mage_Sales_Model_Convert_Order();
     $order = $invoice->getOrder();
     $shipment = $convertOrder->toShipment($order);
     $items = $invoice->getAllItems();
     $totalQty = 0;
     if (count($items)) {
         foreach ($items as $eachItem) {
             if ($eachItem->getRowTotal() > 0) {
                 $_eachShippedItem = $convertOrder->itemToShipmentItem($eachItem->getOrderItem());
                 $_eachShippedItem->setQty($eachItem->getQty());
                 $shipment->addItem($_eachShippedItem);
                 $totalQty += $eachItem->getQty();
                 unset($_eachShippedItem);
             }
         }
         $shipment->setTotalQty($totalQty);
     }
     Mage::getModel('core/resource_transaction')->addObject($shipment)->addObject($shipment->getOrder())->save();
     $order->save();
 }
Example #25
0
 /**
  * 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['taxPercent'] = $orderItem->getTaxPercent();
             $article['discountId'] = '';
             if ($item->getDiscountAmount() > 0) {
                 $discount = array();
                 $discount['articleNumber'] = 'DISCOUNT-' . $item->getSku();
                 $discount['articleName'] = 'DISCOUNT - ' . $item->getName();
                 $discount['quantity'] = $article['quantity'];
                 $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->getGwPrice() > 0) {
         $article = array();
         $article['articleNumber'] = 'orderwrapping';
         $article['articleName'] = 'Wrapping Cost Order';
         $article['quantity'] = '1';
         $article['unitPriceGross'] = $object->getGwPrice();
         $article['taxPercent'] = 100 / $object->getGwPrice() * $object->getGwTaxAmount();
         $article['discountId'] = '';
         $articles[] = $article;
     }
     if ($object->getGwItemsPrice() > 0) {
         $article = array();
         $article['articleNumber'] = 'itemswrapping';
         $article['articleName'] = 'Wrapping Cost Items';
         $article['quantity'] = '1';
         $article['unitPriceGross'] = $object->getGwItemsPrice();
         $article['taxPercent'] = 100 / $object->getGwItemsPrice() * $object->getGwItemsTaxAmount();
         $article['discountId'] = '';
         $articles[] = $article;
     }
     if ($object->getGwAddCard() > 0) {
         $article = array();
         $article['articleNumber'] = 'printed_card';
         $article['articleName'] = 'Printed Card';
         $article['quantity'] = '1';
         $article['unitPriceGross'] = $object->getGwCardPrice();
         $article['taxPercent'] = 100 / $object->getGwCardPrice() * $object->getGwCardTaxAmount();
         $article['discountId'] = '';
         $articles[] = $article;
     }
     if (Mage::getEdition() == 'Enterprise') {
         $_cards = Mage::getBlockSingleton('enterprise_giftcardaccount/checkout_cart_total')->getQuoteGiftCards();
         if ($_cards) {
             foreach ($_cards as $card) {
                 $article = array();
                 $article['articleNumber'] = 'gift_card';
                 $article['articleName'] = $card['c'];
                 $article['quantity'] = '1';
                 $article['unitPriceGross'] = -round($card['ba'], 2);
                 $article['taxPercent'] = 0;
                 $article['discountId'] = '';
                 $articles[] = $article;
             }
         }
     }
     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();
         $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();
             $discount['taxPercent'] = 0;
             $discount['discountId'] = 'SHIPPING';
         }
         $articles[] = $article;
         if ($shippingDiscountAmount > 0) {
             // only for sort reason
             $articles[] = $discount;
         }
     }
     if ($object->getRewardCurrencyAmount() > 0) {
         $article = array();
         $article['articleNumber'] = 'REWARDPOINTS';
         $article['articleName'] = 'Reward points';
         $article['quantity'] = '1';
         $article['unitPriceGross'] = -1 * $object->getRewardCurrencyAmount();
         $article['taxPercent'] = 0;
         $article['discountId'] = '';
         $articles[] = $article;
     }
     return $articles;
 }
Example #26
0
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $invoice->setGiftwrapAmount(0);
     $totalAmount = 0;
     $giftwrapPrice = 0;
     $giftcardPrice = 0;
     $giftbox = array();
     $parentItemChildQty = array();
     $order = $invoice->getOrder();
     $tax_info = $order->getFullTaxInfo();
     $rate = $tax_info[0]['percent'];
     //lay ra cac giftbox co trong order
     foreach ($invoice->getAllItems() as $item) {
         $orderItemModel = Mage::getResourceModel('sales/order_item_collection');
         $orderItem = $orderItemModel->addFieldToFilter('item_id', $item->getOrderItemId())->getFirstItem();
         if (!$orderItem->getParentItemId() && $item->getQty() > 0) {
             $quoteItemId = $orderItem->getQuoteItemId();
             $selectionItem = Mage::getResourceModel('giftwrap/selectionitem_collection')->addFieldToFilter('item_id', $quoteItemId)->getFirstItem();
             $selection = Mage::getModel('giftwrap/selection')->load($selectionItem->getSelectionId());
             if ($selection && !$selection->getIsInvoiced()) {
                 if (!in_array($selection->getId(), $giftbox)) {
                     $giftwrapPrice = 0;
                     $giftcardPrice = 0;
                     $totalQtyInBox = 0;
                     $giftbox[] = $selection->getId();
                     $giftwrapPrice += (double) Mage::getModel('giftwrap/giftwrap')->load($selection->getStyleId())->getPrice();
                     if ($selection->getGiftcardId()) {
                         $giftcardPrice += (double) Mage::getModel('giftwrap/giftcard')->load($selection->getGiftcardId())->getPrice();
                     }
                     if ($selection->getCalculateByItem()) {
                         $selectionItems = Mage::getResourceModel('giftwrap/selectionitem_collection')->addFieldToFilter('selection_id', $selection->getId());
                         foreach ($selectionItems as $sItem) {
                             $totalQtyInBox += $sItem->getQty();
                         }
                         $totalAmount += $totalQtyInBox * ($giftwrapPrice + $giftcardPrice);
                     } else {
                         $totalAmount += $giftwrapPrice + $giftcardPrice;
                     }
                 }
             }
         } else {
             $parentItemChildQty[$orderItem->getParentItemId()] += $item->getQty();
         }
     }
     if (count($parentItemChildQty) > 0) {
         foreach ($parentItemChildQty as $orderItemId => $qty) {
             if ($qty == 0) {
                 $quoteItemId = Mage::getModel('sales/order_item')->load($orderItemId)->getQuoteItemId();
                 $selectionItem = Mage::getResourceModel('giftwrap/selectionitem_collection')->addFieldToFilter('item_id', $quoteItemId)->getFirstItem();
                 $selection = Mage::getModel('giftwrap/selection')->load($selectionItem->getSelectionId());
                 $giftwrapPrice = 0;
                 $giftcardPrice = 0;
                 $totalQtyInBox = 0;
                 if (($key = array_search($selection->getId(), $giftbox)) !== false) {
                     unset($giftbox[$key]);
                     $giftwrapPrice += (double) Mage::getModel('giftwrap/giftwrap')->load($selection->getStyleId())->getPrice();
                     if ($selection->getGiftcardId()) {
                         $giftcardPrice += (double) Mage::getModel('giftwrap/giftcard')->load($selection->getGiftcardId())->getPrice();
                     }
                     if ($selection->getCalculateByItem()) {
                         $selectionItems = Mage::getResourceModel('giftwrap/selectionitem_collection')->addFieldToFilter('selection_id', $selection->getId());
                         foreach ($selectionItems as $sItem) {
                             $totalQtyInBox += $sItem->getQty();
                         }
                         $totalAmount -= $totalQtyInBox * ($giftwrapPrice + $giftcardPrice);
                     } else {
                         $totalAmount -= $giftwrapPrice + $giftcardPrice;
                     }
                 }
             }
         }
     }
     $giftbox = array_filter($giftbox);
     $lastItem = $invoice->getItemsCollection()->getLastItem();
     if ($lastItem->getOrderItem()->getParentItemId()) {
         $lastId = $lastItem->getOrderItem()->getParentItemId();
     } else {
         $lastId = $lastItem->getOrderItem()->getId();
     }
     Mage::getSingleton('adminhtml/session')->setSubtotalGiftwrap($totalAmount);
     Mage::getSingleton('adminhtml/session')->setGiftbox($giftbox);
     Mage::getSingleton('adminhtml/session')->setInvoiceLastId($lastId);
     $orderGiftwrapAmount = $totalAmount;
     if (Mage::getStoreConfig('giftwrap/calculation/tax', Mage::app()->getStore(true)->getId())) {
         if ($rate) {
             $orderGiftwrapTax = $totalAmount * $rate;
         }
     }
     if ($orderGiftwrapAmount || $orderGiftwrapAmount == 0) {
         $invoice->setGiftwrapAmount($orderGiftwrapAmount);
         $invoice->setGrandTotal($invoice->getGrandTotal() + $orderGiftwrapAmount);
         $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $orderGiftwrapAmount);
         if ($orderGiftwrapTax) {
             $invoice->setGiftwrapTax($orderGiftwrapTax);
             $invoice->setGrandTotal($invoice->getGrandTotal() + $orderGiftwrapTax);
             $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $orderGiftwrapTax);
         }
     }
     return $this;
 }
Example #27
0
 /**
  * @param Mage_Sales_Model_Order_Invoice $invoice
  *
  * @return Mage_Sales_Model_Order_Invoice_Item[]
  */
 public function getActionableInvoiceItems(Mage_Sales_Model_Order_Invoice $invoice)
 {
     /** @var Mage_Sales_Model_Order_Invoice_Item[] $items */
     $items = $invoice->getAllItems();
     foreach ($items as $k => $item) {
         if ($item->getOrderItem()->isDummy()) {
             unset($items[$k]);
         }
     }
     return $items;
 }
Example #28
0
 /**
  * Collect invoice subtotal
  *
  * @param   Mage_Sales_Model_Order_Invoice $invoice
  * @return  Mage_Sales_Model_Order_Invoice_Total_Subtotal
  */
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     /// Below is the original magento code instead of marked lines
     $subtotal = 0;
     $baseSubtotal = 0;
     $subtotalInclTax = 0;
     $baseSubtotalInclTax = 0;
     $totalWeeeDiscount = 0;
     $totalBaseWeeeDiscount = 0;
     $order = $invoice->getOrder();
     foreach ($invoice->getAllItems() as $item) {
         if ($item->getOrderItem()->isDummy()) {
             continue;
         }
         $item->calcRowTotal();
         $subtotal += $item->getRowTotal();
         $baseSubtotal += $item->getBaseRowTotal();
         $subtotalInclTax += $item->getRowTotalInclTax();
         $baseSubtotalInclTax += $item->getBaseRowTotalInclTax();
         $totalWeeeDiscount += $item->getOrderItem()->getDiscountAppliedForWeeeTax();
         $totalBaseWeeeDiscount += $item->getOrderItem()->getBaseDiscountAppliedForWeeeTax();
     }
     ///// Changes!!! Deducted refunded amount from allowed subtotal
     $allowedSubtotal = $order->getSubtotal() - $order->getSubtotalInvoiced() + $order->getSubtotalRefunded();
     $baseAllowedSubtotal = $order->getBaseSubtotal() - $order->getBaseSubtotalInvoiced() + $order->getBaseSubtotalRefunded();
     $allowedSubtotalInclTax = $allowedSubtotal + $order->getHiddenTaxAmount() + $totalWeeeDiscount + $order->getTaxAmount() - $order->getTaxInvoiced() - $order->getHiddenTaxInvoiced() + $order->getTaxRefunded() + $order->getHiddenTaxRefunded();
     $baseAllowedSubtotalInclTax = $baseAllowedSubtotal + $order->getBaseHiddenTaxAmount() + $totalBaseWeeeDiscount + $order->getBaseTaxAmount() - $order->getBaseTaxInvoiced() - $order->getBaseHiddenTaxInvoiced() + $order->getBaseTaxRefunded() + $order->getBaseHiddenTaxRefunded();
     /**
      * Check if shipping tax calculation is included to current invoice.
      */
     $includeShippingTax = true;
     foreach ($invoice->getOrder()->getInvoiceCollection() as $previousInvoice) {
         if ($previousInvoice->getShippingAmount() && !$previousInvoice->isCanceled()) {
             $includeShippingTax = false;
             break;
         }
     }
     if ($includeShippingTax) {
         $allowedSubtotalInclTax -= $order->getShippingTaxAmount();
         $baseAllowedSubtotalInclTax -= $order->getBaseShippingTaxAmount();
     } else {
         $allowedSubtotalInclTax += $order->getShippingHiddenTaxAmount();
         $baseAllowedSubtotalInclTax += $order->getBaseShippingHiddenTaxAmount();
     }
     if ($invoice->isLast()) {
         $subtotal = $allowedSubtotal;
         $baseSubtotal = $baseAllowedSubtotal;
         $subtotalInclTax = $allowedSubtotalInclTax;
         $baseSubtotalInclTax = $baseAllowedSubtotalInclTax;
     } else {
         $subtotal = min($allowedSubtotal, $subtotal);
         $baseSubtotal = min($baseAllowedSubtotal, $baseSubtotal);
         $subtotalInclTax = min($allowedSubtotalInclTax, $subtotalInclTax);
         $baseSubtotalInclTax = min($baseAllowedSubtotalInclTax, $baseSubtotalInclTax);
     }
     $invoice->setSubtotal($subtotal);
     $invoice->setBaseSubtotal($baseSubtotal);
     $invoice->setSubtotalInclTax($subtotalInclTax);
     $invoice->setBaseSubtotalInclTax($baseSubtotalInclTax);
     $invoice->setGrandTotal($invoice->getGrandTotal() + $subtotal);
     $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseSubtotal);
     return $this;
 }
Example #29
0
 /**
  * Collect invoice tax amount
  *
  * @param Mage_Sales_Model_Order_Invoice $invoice
  * @return Mage_Sales_Model_Order_Invoice_Total_Tax
  */
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $totalTax = 0;
     $baseTotalTax = 0;
     $totalHiddenTax = 0;
     $baseTotalHiddenTax = 0;
     $order = $invoice->getOrder();
     /** @var $item Mage_Sales_Model_Order_Invoice_Item */
     foreach ($invoice->getAllItems() as $item) {
         $orderItem = $item->getOrderItem();
         $orderItemQty = $orderItem->getQtyOrdered();
         if (($orderItem->getTaxAmount() || $orderItem->getHiddenTaxAmount()) && $orderItemQty) {
             if ($item->getOrderItem()->isDummy()) {
                 continue;
             }
             /**
              * Resolve rounding problems
              */
             $tax = $orderItem->getTaxAmount() - $orderItem->getTaxInvoiced();
             $baseTax = $orderItem->getBaseTaxAmount() - $orderItem->getBaseTaxInvoiced();
             $hiddenTax = $orderItem->getHiddenTaxAmount() - $orderItem->getHiddenTaxInvoiced();
             $baseHiddenTax = $orderItem->getBaseHiddenTaxAmount() - $orderItem->getBaseHiddenTaxInvoiced();
             if (!$item->isLast()) {
                 $availableQty = $orderItemQty - $orderItem->getQtyInvoiced();
                 $tax = $invoice->roundPrice($tax / $availableQty * $item->getQty());
                 $baseTax = $invoice->roundPrice($baseTax / $availableQty * $item->getQty(), 'base');
                 $hiddenTax = $invoice->roundPrice($hiddenTax / $availableQty * $item->getQty());
                 $baseHiddenTax = $invoice->roundPrice($baseHiddenTax / $availableQty * $item->getQty(), 'base');
             }
             $item->setTaxAmount($tax);
             $item->setBaseTaxAmount($baseTax);
             $item->setHiddenTaxAmount($hiddenTax);
             $item->setBaseHiddenTaxAmount($baseHiddenTax);
             $totalTax += $tax;
             $baseTotalTax += $baseTax;
             $totalHiddenTax += $hiddenTax;
             $baseTotalHiddenTax += $baseHiddenTax;
         }
     }
     if ($this->_canIncludeShipping($invoice)) {
         $totalTax += $order->getShippingTaxAmount();
         $baseTotalTax += $order->getBaseShippingTaxAmount();
         $totalHiddenTax += $order->getShippingHiddenTaxAmount();
         $baseTotalHiddenTax += $order->getBaseShippingHiddenTaxAmount();
         $invoice->setShippingTaxAmount($order->getShippingTaxAmount());
         $invoice->setBaseShippingTaxAmount($order->getBaseShippingTaxAmount());
         $invoice->setShippingHiddenTaxAmount($order->getShippingHiddenTaxAmount());
         $invoice->setBaseShippingHiddenTaxAmount($order->getBaseShippingHiddenTaxAmount());
     }
     $allowedTax = $order->getTaxAmount() - $order->getTaxInvoiced();
     $allowedBaseTax = $order->getBaseTaxAmount() - $order->getBaseTaxInvoiced();
     $allowedHiddenTax = $order->getHiddenTaxAmount() + $order->getShippingHiddenTaxAmount() - $order->getHiddenTaxInvoiced() - $order->getShippingHiddenTaxInvoiced();
     $allowedBaseHiddenTax = $order->getBaseHiddenTaxAmount() + $order->getBaseShippingHiddenTaxAmount() - $order->getBaseHiddenTaxInvoiced() - $order->getBaseShippingHiddenTaxInvoiced();
     if ($invoice->isLast()) {
         $totalTax = $allowedTax;
         $baseTotalTax = $allowedBaseTax;
         $totalHiddenTax = $allowedHiddenTax;
         $baseTotalHiddenTax = $allowedBaseHiddenTax;
     } else {
         $totalTax = min($allowedTax, $totalTax);
         $baseTotalTax = min($allowedBaseTax, $baseTotalTax);
         $totalHiddenTax = min($allowedHiddenTax, $totalHiddenTax);
         $baseTotalHiddenTax = min($allowedBaseHiddenTax, $baseTotalHiddenTax);
     }
     $invoice->setTaxAmount($totalTax);
     $invoice->setBaseTaxAmount($baseTotalTax);
     $invoice->setHiddenTaxAmount($totalHiddenTax);
     $invoice->setBaseHiddenTaxAmount($baseTotalHiddenTax);
     $invoice->setGrandTotal($invoice->getGrandTotal() + $totalTax + $totalHiddenTax);
     $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseTotalTax + $baseTotalHiddenTax);
     return $this;
 }
Example #30
0
 /**
  * Collect total when create Invoice
  * 
  * @param Mage_Sales_Model_Order_Invoice $invoice
  */
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $order = $invoice->getOrder();
     /**
      * update 2.0
      */
     $earnPoint = 0;
     $maxEarn = $order->getRewardpointsEarn();
     $maxEarn -= (int) Mage::getResourceModel('rewardpoints/transaction_collection')->addFieldToFilter('action', 'earning_invoice')->addFieldToFilter('order_id', $order->getId())->getFieldTotal();
     if ($maxEarn >= 0) {
         foreach ($invoice->getAllItems() as $item) {
             $orderItem = $item->getOrderItem();
             if ($orderItem->isDummy()) {
                 continue;
             }
             $itemPoint = (int) $orderItem->getRewardpointsEarn();
             $itemPoint = $itemPoint * $item->getQty() / $orderItem->getQtyOrdered();
             $earnPoint += floor($itemPoint);
         }
         if ($invoice->isLast() || $earnPoint >= $maxEarn) {
             $earnPoint = $maxEarn;
         }
         $invoice->setRewardpointsEarn($earnPoint);
     }
     if ($order->getRewardpointsDiscount() < 0.0001) {
         return;
     }
     $invoice->setRewardpointsDiscount(0);
     $invoice->setRewardpointsBaseDiscount(0);
     $totalDiscountAmount = 0;
     $baseTotalDiscountAmount = 0;
     $totalDiscountInvoiced = 0;
     $baseTotalDiscountInvoiced = 0;
     $hiddenTaxInvoiced = 0;
     $baseHiddenTaxInvoiced = 0;
     $totalHiddenTax = 0;
     $baseTotalHiddenTax = 0;
     /**
      * Checking if shipping discount was added in previous invoices.
      * So basically if we have invoice with positive discount and it
      * was not canceled we don't add shipping discount to this one.
      */
     $addShippingDicount = true;
     foreach ($order->getInvoiceCollection() as $previusInvoice) {
         if ($previusInvoice->getRewardpointsDiscount()) {
             $addShippingDicount = false;
             $totalDiscountInvoiced += $previusInvoice->getRewardpointsDiscount();
             $baseTotalDiscountInvoiced += $previusInvoice->getRewardpointsBaseDiscount();
             $hiddenTaxInvoiced += $previusInvoice->getRewardpointsHiddenTaxAmount();
             $baseHiddenTaxInvoiced += $previusInvoice->getRewardpointsBaseHiddenTaxAmount();
         }
     }
     if ($addShippingDicount) {
         $totalDiscountAmount += $order->getRewardpointsAmount();
         $baseTotalDiscountAmount += $order->getRewardpointsBaseAmount();
         $totalHiddenTax += $order->getRewardpointsShippingHiddenTaxAmount();
         $baseTotalHiddenTax += $order->getRewardpointsBaseShippingHiddenTaxAmount();
     }
     if ($invoice->isLast()) {
         $totalDiscountAmount = $order->getRewardpointsDiscount() - $totalDiscountInvoiced;
         $baseTotalDiscountAmount = $order->getRewardpointsBaseDiscount() - $baseTotalDiscountInvoiced;
         $totalHiddenTax = $order->getRewardpointsHiddenTaxAmount() - $hiddenTaxInvoiced;
         $baseTotalHiddenTax = $order->getRewardpointsBaseHiddenTaxAmount() - $baseHiddenTaxInvoiced;
     } else {
         /** @var $item Mage_Sales_Model_Order_Invoice_Item */
         foreach ($invoice->getAllItems() as $item) {
             $orderItem = $item->getOrderItem();
             if ($orderItem->isDummy()) {
                 continue;
             }
             $orderItemDiscount = (double) $orderItem->getRewardpointsDiscount();
             $baseOrderItemDiscount = (double) $orderItem->getRewardpointsBaseDiscount();
             $orderItemHiddenTax = (double) $orderItem->getRewardpointsHiddenTaxAmount();
             $baseOrderItemHiddenTax = (double) $orderItem->getRewardpointsBaseHiddenTaxAmount();
             $orderItemQty = $orderItem->getQtyOrdered();
             if ($orderItemDiscount && $orderItemQty) {
                 $totalDiscountAmount += $invoice->roundPrice($orderItemDiscount / $orderItemQty * $item->getQty(), 'regular', true);
                 $baseTotalDiscountAmount += $invoice->roundPrice($baseOrderItemDiscount / $orderItemQty * $item->getQty(), 'base', true);
                 $totalHiddenTax += $invoice->roundPrice($orderItemHiddenTax / $orderItemQty * $item->getQty(), 'regular', true);
                 $baseTotalHiddenTax += $invoice->roundPrice($baseOrderItemHiddenTax / $orderItemQty * $item->getQty(), 'base', true);
             }
         }
         $allowedBaseHiddenTax = $order->getRewardpointsBaseHiddenTaxAmount() - $baseHiddenTaxInvoiced;
         $allowedHiddenTax = $order->getRewardpointsHiddenTaxAmount() - $hiddenTaxInvoiced;
         $totalHiddenTax = min($allowedHiddenTax, $totalHiddenTax);
         $baseTotalHiddenTax = min($allowedBaseHiddenTax, $baseTotalHiddenTax);
     }
     $invoice->setRewardpointsDiscount($totalDiscountAmount);
     $invoice->setRewardpointsBaseDiscount($baseTotalDiscountAmount);
     $invoice->setRewardpointsHiddenTaxAmount($totalHiddenTax);
     $invoice->setRewardpointsBaseHiddenTaxAmount($baseTotalHiddenTax);
     $invoice->setGrandTotal($invoice->getGrandTotal() - $totalDiscountAmount + $totalHiddenTax);
     $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() - $baseTotalDiscountAmount + $baseTotalHiddenTax);
     return $this;
 }