00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 class Mage_Sales_Model_Order_Creditmemo_Item extends Mage_Core_Model_Abstract
00028 {
00029 protected $_eventPrefix = 'sales_creditmemo_item';
00030 protected $_eventObject = 'creditmemo_item';
00031 protected $_creditmemo = null;
00032 protected $_orderItem = null;
00033
00034
00035
00036
00037 function _construct()
00038 {
00039 $this->_init('sales/order_creditmemo_item');
00040 }
00041
00042
00043
00044
00045
00046
00047
00048 public function setCreditmemo(Mage_Sales_Model_Order_Creditmemo $creditmemo)
00049 {
00050 $this->_creditmemo = $creditmemo;
00051 return $this;
00052 }
00053
00054
00055
00056
00057
00058
00059 public function getCreditmemo()
00060 {
00061 return $this->_creditmemo;
00062 }
00063
00064
00065
00066
00067
00068
00069
00070 public function setOrderItem(Mage_Sales_Model_Order_Item $item)
00071 {
00072 $this->_orderItem = $item;
00073 $this->setOrderItemId($item->getId());
00074 return $this;
00075 }
00076
00077
00078
00079
00080
00081
00082 public function getOrderItem()
00083 {
00084 if (is_null($this->_orderItem)) {
00085 if ($this->getCreditmemo()) {
00086 $this->_orderItem = $this->getCreditmemo()->getOrder()->getItemById($this->getOrderItemId());
00087 }
00088 else {
00089 $this->_orderItem = Mage::getModel('sales/order_item')
00090 ->load($this->getOrderItemId());
00091 }
00092 }
00093 return $this->_orderItem;
00094 }
00095
00096
00097
00098
00099
00100
00101
00102 public function setQty($qty)
00103 {
00104 if ($this->getOrderItem()->getIsQtyDecimal()) {
00105 $qty = (float) $qty;
00106 }
00107 else {
00108 $qty = (int) $qty;
00109 }
00110 $qty = $qty > 0 ? $qty : 0;
00111
00112
00113
00114 if ($qty <= $this->getOrderItem()->getQtyToRefund() || $this->getOrderItem()->isDummy()) {
00115 $this->setData('qty', $qty);
00116 }
00117 else {
00118 Mage::throwException(
00119 Mage::helper('sales')->__('Invalid qty to refund item "%s"', $this->getName())
00120 );
00121 }
00122 return $this;
00123 }
00124
00125
00126
00127
00128
00129
00130 public function register()
00131 {
00132 $this->getOrderItem()->setQtyRefunded(
00133 $this->getOrderItem()->getQtyRefunded()+$this->getQty()
00134 );
00135 return $this;
00136 }
00137
00138 public function cancel()
00139 {
00140 $this->getOrderItem()->setQtyRefunded(
00141 $this->getOrderItem()->getQtyRefunded()-$this->getQty()
00142 );
00143 return $this;
00144 }
00145
00146
00147
00148
00149
00150
00151 public function calcRowTotal()
00152 {
00153 $rowTotal = $this->getOrderItem()->getRowTotal()/$this->getOrderItem()->getQtyOrdered()*$this->getQty();
00154 $baseRowTotal = $this->getOrderItem()->getBaseRowTotal()/$this->getOrderItem()->getQtyOrdered()*$this->getQty();
00155
00156 $this->setRowTotal($this->getCreditmemo()->getStore()->roundPrice($rowTotal));
00157 $this->setBaseRowTotal($this->getCreditmemo()->getStore()->roundPrice($baseRowTotal));
00158 return $this;
00159 }
00160 }