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
00028
00029
00030
00031
00032
00033
00034 class Mage_Sales_Model_Order_Item extends Mage_Core_Model_Abstract
00035 {
00036
00037 const STATUS_PENDING = 1;
00038 const STATUS_SHIPPED = 2;
00039 const STATUS_INVOICED = 9;
00040 const STATUS_BACKORDERED = 3;
00041 const STATUS_CANCELED = 5;
00042 const STATUS_PARTIAL = 6;
00043 const STATUS_MIXED = 7;
00044 const STATUS_REFUNDED = 8;
00045
00046 const STATUS_RETURNED = 4;
00047
00048 protected $_eventPrefix = 'sales_order_item';
00049 protected $_eventObject = 'item';
00050
00051 protected static $_statuses = null;
00052
00053
00054
00055
00056
00057
00058 protected $_order = null;
00059 protected $_parentItem = null;
00060 protected $_children = array();
00061
00062
00063
00064
00065 protected function _construct()
00066 {
00067 $this->_init('sales/order_item');
00068 }
00069
00070
00071
00072
00073
00074
00075 protected function _beforeSave()
00076 {
00077 parent::_beforeSave();
00078 if (!$this->getOrderId()) {
00079 $this->setOrderId($this->getOrder()->getId());
00080 }
00081 if ($this->getParentItem()) {
00082 $this->setParentItemId($this->getParentItem()->getId());
00083 }
00084 return $this;
00085 }
00086
00087
00088
00089
00090
00091
00092
00093 public function setParentItem($item)
00094 {
00095 if ($item) {
00096 $this->_parentItem = $item;
00097 $item->setHasChildren(true);
00098 $item->addChildItem($this);
00099 }
00100 return $this;
00101 }
00102
00103
00104
00105
00106
00107
00108 public function getParentItem()
00109 {
00110 return $this->_parentItem;
00111 }
00112
00113
00114
00115
00116
00117
00118 public function canInvoice()
00119 {
00120 return $this->getQtyToInvoice()>0;
00121 }
00122
00123
00124
00125
00126
00127
00128 public function canShip()
00129 {
00130 return $this->getQtyToShip()>0;
00131 }
00132
00133
00134
00135
00136
00137
00138 public function canRefund()
00139 {
00140 return $this->getQtyToRefund()>0;
00141 }
00142
00143
00144
00145
00146
00147
00148 public function getQtyToShip()
00149 {
00150 if ($this->isDummy(true)) {
00151 return 0;
00152 }
00153
00154 $qty = $this->getQtyOrdered()
00155 - $this->getQtyShipped()
00156 - $this->getQtyRefunded()
00157 - $this->getQtyCanceled();
00158 return max($qty, 0);
00159 }
00160
00161
00162
00163
00164
00165
00166 public function getQtyToInvoice()
00167 {
00168 if ($this->isDummy()) {
00169 return 0;
00170 }
00171
00172 $qty = $this->getQtyOrdered()
00173 - $this->getQtyInvoiced()
00174 - $this->getQtyCanceled();
00175 return max($qty, 0);
00176 }
00177
00178
00179
00180
00181
00182
00183 public function getQtyToRefund()
00184 {
00185 if ($this->isDummy()) {
00186 return 0;
00187 }
00188
00189 return max($this->getQtyInvoiced()-$this->getQtyRefunded(), 0);
00190 }
00191
00192
00193
00194
00195
00196
00197 public function getQtyToCancel()
00198 {
00199 $qtyToCancel = min($this->getQtyToInvoice(), $this->getQtyToShip());
00200 return max($qtyToCancel, 0);
00201 }
00202
00203
00204
00205
00206
00207
00208
00209 public function setOrder(Mage_Sales_Model_Order $order)
00210 {
00211 $this->_order = $order;
00212 $this->setOrderId($order->getId());
00213 return $this;
00214 }
00215
00216
00217
00218
00219
00220
00221 public function getOrder()
00222 {
00223 if (is_null($this->_order) && ($orderId = $this->getOrderId())) {
00224 $order = Mage::getModel('sales/order');
00225 $order->load($orderId);
00226 $this->setOrder($order);
00227 }
00228 return $this->_order;
00229 }
00230
00231
00232
00233
00234
00235
00236 public function getStatusId()
00237 {
00238 $backordered = (float)$this->getQtyBackordered();
00239 $canceled = (float)$this->getQtyCanceled();
00240 $invoiced = (float)$this->getQtyInvoiced();
00241 $ordered = (float)$this->getQtyOrdered();
00242 $refunded = (float)$this->getQtyRefunded();
00243 $shipped = (float)$this->getQtyShipped();
00244
00245 $actuallyOrdered = $ordered - $canceled - $refunded;
00246
00247 if (!$invoiced && !$shipped && !$refunded && !$canceled && !$backordered) {
00248 return self::STATUS_PENDING;
00249 }
00250 if ($shipped && !$invoiced && ($actuallyOrdered == $shipped)) {
00251 return self::STATUS_SHIPPED;
00252 }
00253
00254 if ($invoiced && !$shipped && ($actuallyOrdered == $invoiced)) {
00255 return self::STATUS_INVOICED;
00256 }
00257
00258 if ($backordered && ($actuallyOrdered == $backordered) ) {
00259 return self::STATUS_BACKORDERED;
00260 }
00261
00262 if ($refunded && $ordered == $refunded) {
00263 return self::STATUS_REFUNDED;
00264 }
00265
00266 if ($canceled && $ordered == $canceled) {
00267 return self::STATUS_CANCELED;
00268 }
00269
00270 if (max($shipped, $invoiced) < $actuallyOrdered) {
00271 return self::STATUS_PARTIAL;
00272 }
00273
00274 return self::STATUS_MIXED;
00275 }
00276
00277
00278
00279
00280
00281
00282 public function getStatus()
00283 {
00284 return $this->getStatusName($this->getStatusId());
00285 }
00286
00287
00288
00289
00290
00291
00292 public static function getStatusName($statusId)
00293 {
00294 if (is_null(self::$_statuses)) {
00295 self::getStatuses();
00296 }
00297 if (isset(self::$_statuses[$statusId])) {
00298 return self::$_statuses[$statusId];
00299 }
00300 return Mage::helper('sales')->__('Unknown Status');
00301 }
00302
00303
00304
00305
00306
00307
00308 public function cancel()
00309 {
00310 if ($this->getStatusId() !== self::STATUS_CANCELED) {
00311 Mage::dispatchEvent('sales_order_item_cancel', array('item'=>$this));
00312 $this->setQtyCanceled($this->getQtyToCancel());
00313 }
00314 return $this;
00315 }
00316
00317
00318
00319
00320
00321
00322 public static function getStatuses()
00323 {
00324 if (is_null(self::$_statuses)) {
00325 self::$_statuses = array(
00326
00327 self::STATUS_PENDING => Mage::helper('sales')->__('Ordered'),
00328 self::STATUS_SHIPPED => Mage::helper('sales')->__('Shipped'),
00329 self::STATUS_INVOICED => Mage::helper('sales')->__('Invoiced'),
00330 self::STATUS_BACKORDERED => Mage::helper('sales')->__('Backordered'),
00331 self::STATUS_RETURNED => Mage::helper('sales')->__('Returned'),
00332 self::STATUS_REFUNDED => Mage::helper('sales')->__('Refunded'),
00333 self::STATUS_CANCELED => Mage::helper('sales')->__('Canceled'),
00334 self::STATUS_PARTIAL => Mage::helper('sales')->__('Partial'),
00335 self::STATUS_MIXED => Mage::helper('sales')->__('Mixed'),
00336 );
00337 }
00338 return self::$_statuses;
00339 }
00340
00341
00342
00343
00344
00345
00346 public function getOriginalPrice()
00347 {
00348 $price = $this->getData('original_price');
00349 if (is_null($price)) {
00350 return $this->getPrice();
00351 }
00352 return $price;
00353 }
00354
00355
00356
00357
00358
00359
00360
00361 public function setProductOptions(array $options)
00362 {
00363 $this->setData('product_options', serialize($options));
00364 return $this;
00365 }
00366
00367
00368
00369
00370
00371
00372 public function getProductOptions()
00373 {
00374 if ($options = $this->_getData('product_options')) {
00375 return unserialize($options);
00376 }
00377 return array();
00378 }
00379
00380
00381
00382
00383
00384
00385
00386
00387 public function getProductOptionByCode($code=null)
00388 {
00389 $options = $this->getProductOptions();
00390 if (is_null($code)) {
00391 return $options;
00392 }
00393 if (isset($options[$code])) {
00394 return $options[$code];
00395 }
00396 return null;
00397 }
00398
00399
00400
00401
00402
00403
00404 public function getRealProductType()
00405 {
00406 if ($productType = $this->getProductOptionByCode('real_product_type')) {
00407 return $productType;
00408 }
00409 return null;
00410 }
00411
00412
00413
00414
00415
00416
00417 public function addChildItem($item)
00418 {
00419 if ($item instanceof Mage_Sales_Model_Order_Item) {
00420 $this->_children[] = $item;
00421 } else if (is_array($item)) {
00422 $this->_children = array_merge($this->_children, $item);
00423 }
00424 }
00425
00426
00427
00428
00429
00430
00431 public function getChildrenItems() {
00432 return $this->_children;
00433 }
00434
00435
00436
00437
00438
00439
00440
00441 public function isChildrenCalculated() {
00442 if ($parentItem = $this->getParentItem()) {
00443 $options = $parentItem->getProductOptions();
00444 } else {
00445 $options = $this->getProductOptions();
00446 }
00447
00448 if (isset($options['product_calculations']) &&
00449 $options['product_calculations'] == Mage_Catalog_Model_Product_Type_Abstract::CALCULATE_CHILD) {
00450 return true;
00451 }
00452 return false;
00453 }
00454
00455
00456
00457
00458
00459
00460
00461 public function isShipSeparately() {
00462 if ($parentItem = $this->getParentItem()) {
00463 $options = $parentItem->getProductOptions();
00464 } else {
00465 $options = $this->getProductOptions();
00466 }
00467
00468 if (isset($options['shipment_type']) &&
00469 $options['shipment_type'] == Mage_Catalog_Model_Product_Type_Abstract::SHIPMENT_SEPARATELY) {
00470 return true;
00471 }
00472 return false;
00473 }
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483 public function isDummy($shipment = false){
00484 if ($shipment) {
00485 if ($this->getHasChildren() && $this->isShipSeparately()) {
00486 return true;
00487 }
00488
00489 if ($this->getHasChildren() && !$this->isShipSeparately()) {
00490 return false;
00491 }
00492
00493 if ($this->getParentItem() && $this->isShipSeparately()) {
00494 return false;
00495 }
00496
00497 if ($this->getParentItem() && !$this->isShipSeparately()) {
00498 return true;
00499 }
00500 } else {
00501 if ($this->getHasChildren() && $this->isChildrenCalculated()) {
00502 return true;
00503 }
00504
00505 if ($this->getHasChildren() && !$this->isChildrenCalculated()) {
00506 return false;
00507 }
00508
00509 if ($this->getParentItem() && $this->isChildrenCalculated()) {
00510 return false;
00511 }
00512
00513 if ($this->getParentItem() && !$this->isChildrenCalculated()) {
00514 return true;
00515 }
00516 }
00517 return false;
00518 }
00519 }