*/ class Mage_Sales_Model_Order_Invoice_Item extends Mage_Core_Model_Abstract { protected $_eventPrefix = 'sales_invoice_item'; protected $_eventObject = 'invoice_item'; protected $_invoice = null; protected $_orderItem = null; /** * Initialize resource model */ function _construct() { $this->_init('sales/order_invoice_item'); } /** * Init mapping array of short fields to * its full names * * @return Varien_Object */ protected function _initOldFieldsMap() { $this->_oldFieldsMap = Mage::helper('sales')->getOldFieldMap('invoice_item'); return $this; } /** * Declare invoice instance * * @param Mage_Sales_Model_Order_Invoice $invoice * @return Mage_Sales_Model_Order_Invoice_Item */ public function setInvoice(Mage_Sales_Model_Order_Invoice $invoice) { $this->_invoice = $invoice; return $this; } /** * Retrieve invoice instance * * @return Mage_Sales_Model_Order_Invoice */ public function getInvoice() { return $this->_invoice; } /** * Declare order item instance * * @param Mage_Sales_Model_Order_Item $item * @return Mage_Sales_Model_Order_Invoice_Item */ public function setOrderItem(Mage_Sales_Model_Order_Item $item) { $this->_orderItem = $item; $this->setOrderItemId($item->getId()); return $this; } /** * Retrieve order item instance * * @return Mage_Sales_Model_Order_Item */ public function getOrderItem() { if (is_null($this->_orderItem)) { if ($this->getInvoice()) { $this->_orderItem = $this->getInvoice()->getOrder()->getItemById($this->getOrderItemId()); } else { $this->_orderItem = Mage::getModel('sales/order_item') ->load($this->getOrderItemId()); } } return $this->_orderItem; } /** * Declare qty * * @param float $qty * @return Mage_Sales_Model_Order_Invoice_Item */ public function setQty($qty) { if ($this->getOrderItem()->getIsQtyDecimal()) { $qty = (float) $qty; } else { $qty = (int) $qty; } $qty = $qty > 0 ? $qty : 0; /** * Check qty availability */ $qtyToInvoice = sprintf("%F", $this->getOrderItem()->getQtyToInvoice()); $qty = sprintf("%F", $qty); if ($qty <= $qtyToInvoice || $this->getOrderItem()->isDummy()) { $this->setData('qty', $qty); } else { Mage::throwException( Mage::helper('sales')->__('Invalid qty to invoice item "%s"', $this->getName()) ); } return $this; } /** * Applying qty to order item * * @return Mage_Sales_Model_Order_Invoice_Item */ public function register() { $orderItem = $this->getOrderItem(); $orderItem->setQtyInvoiced($orderItem->getQtyInvoiced()+$this->getQty()); $orderItem->setTaxInvoiced($orderItem->getTaxInvoiced()+$this->getTaxAmount()); $orderItem->setBaseTaxInvoiced($orderItem->getBaseTaxInvoiced()+$this->getBaseTaxAmount()); $orderItem->setHiddenTaxInvoiced($orderItem->getHiddenTaxInvoiced()+$this->getHiddenTaxAmount()); $orderItem->setBaseHiddenTaxInvoiced($orderItem->getBaseHiddenTaxInvoiced()+$this->getBaseHiddenTaxAmount()); $orderItem->setDiscountInvoiced($orderItem->getDiscountInvoiced()+$this->getDiscountAmount()); $orderItem->setBaseDiscountInvoiced($orderItem->getBaseDiscountInvoiced()+$this->getBaseDiscountAmount()); $orderItem->setRowInvoiced($orderItem->getRowInvoiced()+$this->getRowTotal()); $orderItem->setBaseRowInvoiced($orderItem->getBaseRowInvoiced()+$this->getBaseRowTotal()); return $this; } /** * Cancelling invoice item * * @return Mage_Sales_Model_Order_Invoice_Item */ public function cancel() { $orderItem = $this->getOrderItem(); $orderItem->setQtyInvoiced($orderItem->getQtyInvoiced()-$this->getQty()); $orderItem->setTaxInvoiced($orderItem->getTaxInvoiced()-$this->getTaxAmount()); $orderItem->setBaseTaxInvoiced($orderItem->getBaseTaxInvoiced()-$this->getBaseTaxAmount()); $orderItem->setHiddenTaxInvoiced($orderItem->getHiddenTaxInvoiced()-$this->getHiddenTaxAmount()); $orderItem->setBaseHiddenTaxInvoiced($orderItem->getBaseHiddenTaxInvoiced()-$this->getBaseHiddenTaxAmount()); $orderItem->setDiscountInvoiced($orderItem->getDiscountInvoiced()-$this->getDiscountAmount()); $orderItem->setBaseDiscountInvoiced($orderItem->getBaseDiscountInvoiced()-$this->getBaseDiscountAmount()); $orderItem->setRowInvoiced($orderItem->getRowInvoiced()-$this->getRowTotal()); $orderItem->setBaseRowInvoiced($orderItem->getBaseRowInvoiced()-$this->getBaseRowTotal()); return $this; } /** * Invoice item row total calculation * * @return Mage_Sales_Model_Order_Invoice_Item */ public function calcRowTotal() { $invoice = $this->getInvoice(); $orderItem = $this->getOrderItem(); $orderItemQty = $orderItem->getQtyOrdered(); $rowTotal = $orderItem->getRowTotal() - $orderItem->getRowInvoiced(); $baseRowTotal = $orderItem->getBaseRowTotal() - $orderItem->getBaseRowInvoiced(); $rowTotalInclTax = $orderItem->getRowTotalInclTax(); $baseRowTotalInclTax = $orderItem->getBaseRowTotalInclTax(); if (!$this->isLast()) { $availableQty = $orderItemQty - $orderItem->getQtyInvoiced(); $rowTotal = $invoice->roundPrice($rowTotal / $availableQty * $this->getQty()); $baseRowTotal = $invoice->roundPrice($baseRowTotal / $availableQty * $this->getQty(), 'base'); } $this->setRowTotal($rowTotal); $this->setBaseRowTotal($baseRowTotal); if ($rowTotalInclTax && $baseRowTotalInclTax) { $this->setRowTotalInclTax($invoice->roundPrice($rowTotalInclTax / $orderItemQty * $this->getQty(), 'including')); $this->setBaseRowTotalInclTax($invoice->roundPrice($baseRowTotalInclTax / $orderItemQty * $this->getQty(), 'including_base')); } return $this; } /** * Checking if the item is last * * @return bool */ public function isLast() { if ((string)(float)$this->getQty() == (string)(float)$this->getOrderItem()->getQtyToInvoice()) { return true; } return false; } /** * Before object save * * @return Mage_Sales_Model_Order_Invoice_Item */ protected function _beforeSave() { parent::_beforeSave(); if (!$this->getParentId() && $this->getInvoice()) { $this->setParentId($this->getInvoice()->getId()); } return $this; } /** * After object save * * @return Mage_Sales_Model_Order_Invoice_Item */ protected function _afterSave() { if (null ==! $this->_orderItem) { $this->_orderItem->save(); } parent::_afterSave(); return $this; } }