1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Adminhtml_Block_Sales_Items_Abstract extends Mage_Adminhtml_Block_Template
36: {
37: 38: 39: 40: 41: 42: 43: 44:
45: protected $_itemRenders = array();
46:
47: 48: 49: 50: 51: 52: 53: 54:
55: protected $_columnRenders = array();
56:
57: 58: 59: 60: 61:
62: protected $_canEditQty = null;
63:
64: 65: 66: 67:
68: protected function _construct()
69: {
70: $this->addColumnRender('qty', 'adminhtml/sales_items_column_qty', 'sales/items/column/qty.phtml');
71: $this->addColumnRender('name', 'adminhtml/sales_items_column_name', 'sales/items/column/name.phtml');
72: parent::_construct();
73: }
74:
75: 76: 77: 78: 79: 80: 81: 82:
83: public function addItemRender($type, $block, $template)
84: {
85: $this->_itemRenders[$type] = array(
86: 'block' => $block,
87: 'template' => $template,
88: 'renderer' => null
89: );
90: return $this;
91: }
92:
93: 94: 95: 96: 97: 98: 99: 100:
101: public function addColumnRender($column, $block, $template, $type=null)
102: {
103: if (!is_null($type)) {
104: $column .= '_' . $type;
105: }
106: $this->_columnRenders[$column] = array(
107: 'block' => $block,
108: 'template' => $template,
109: 'renderer' => null
110: );
111: return $this;
112: }
113:
114: 115: 116: 117: 118: 119:
120: public function getItemRenderer($type)
121: {
122: if (!isset($this->_itemRenders[$type])) {
123: $type = 'default';
124: }
125: if (is_null($this->_itemRenders[$type]['renderer'])) {
126: $this->_itemRenders[$type]['renderer'] = $this->getLayout()
127: ->createBlock($this->_itemRenders[$type]['block'])
128: ->setTemplate($this->_itemRenders[$type]['template']);
129: foreach ($this->_columnRenders as $columnType=>$renderer) {
130: $this->_itemRenders[$type]['renderer']->addColumnRender($columnType, $renderer['block'], $renderer['template']);
131: }
132: }
133: return $this->_itemRenders[$type]['renderer'];
134: }
135:
136: 137: 138: 139: 140: 141: 142:
143: public function getColumnRenderer($column, $compositePart='')
144: {
145: if (isset($this->_columnRenders[$column . '_' . $compositePart])) {
146: $column .= '_' . $compositePart;
147: }
148: if (!isset($this->_columnRenders[$column])) {
149: return false;
150: }
151: if (is_null($this->_columnRenders[$column]['renderer'])) {
152: $this->_columnRenders[$column]['renderer'] = $this->getLayout()
153: ->createBlock($this->_columnRenders[$column]['block'])
154: ->setTemplate($this->_columnRenders[$column]['template'])
155: ->setRenderedBlock($this);
156: }
157: return $this->_columnRenders[$column]['renderer'];
158: }
159:
160: 161: 162: 163: 164: 165:
166: public function getItemHtml(Varien_Object $item)
167: {
168: if ($item->getOrderItem()) {
169: $type = $item->getOrderItem()->getProductType();
170: } else {
171: $type = $item->getProductType();
172: }
173:
174: return $this->getItemRenderer($type)
175: ->setItem($item)
176: ->setCanEditQty($this->canEditQty())
177: ->toHtml();
178: }
179:
180: 181: 182: 183: 184: 185:
186: public function (Varien_Object $item)
187: {
188: $extraInfoBlock = $this->getChild('order_item_extra_info');
189: if ($extraInfoBlock) {
190: return $extraInfoBlock
191: ->setItem($item)
192: ->toHtml();
193: }
194: return '';
195: }
196:
197: 198: 199: 200: 201: 202: 203: 204:
205: public function getColumnHtml(Varien_Object $item, $column, $field = null)
206: {
207: if ($item->getOrderItem()) {
208: $block = $this->getColumnRenderer($column, $item->getOrderItem()->getProductType());
209: } else {
210: $block = $this->getColumnRenderer($column, $item->getProductType());
211: }
212:
213: if ($block) {
214: $block->setItem($item);
215: if (!is_null($field)) {
216: $block->setField($field);
217: }
218: return $block->toHtml();
219: }
220: return ' ';
221: }
222:
223: public function getCreditmemo()
224: {
225: return Mage::registry('current_creditmemo');
226: }
227:
228: 229: 230:
231:
232: 233: 234: 235: 236:
237: public function getOrder()
238: {
239: if ($this->hasOrder()) {
240: return $this->getData('order');
241: }
242: if (Mage::registry('current_order')) {
243: return Mage::registry('current_order');
244: }
245: if (Mage::registry('order')) {
246: return Mage::registry('order');
247: }
248: if ($this->getInvoice())
249: {
250: return $this->getInvoice()->getOrder();
251: }
252: if ($this->getCreditmemo())
253: {
254: return $this->getCreditmemo()->getOrder();
255: }
256: if ($this->getItem()->getOrder())
257: {
258: return $this->getItem()->getOrder();
259: }
260:
261: Mage::throwException(Mage::helper('sales')->__('Cannot get order instance'));
262: }
263:
264: 265: 266: 267: 268:
269: public function getPriceDataObject()
270: {
271: $obj = $this->getData('price_data_object');
272: if (is_null($obj)) {
273: return $this->getOrder();
274: }
275: return $obj;
276: }
277:
278: 279: 280: 281: 282: 283: 284: 285:
286: public function displayPriceAttribute($code, $strong = false, $separator = '<br />')
287: {
288: if ($code == 'tax_amount' && $this->getOrder()->getRowTaxDisplayPrecision()) {
289: return $this->displayRoundedPrices(
290: $this->getPriceDataObject()->getData('base_'.$code),
291: $this->getPriceDataObject()->getData($code),
292: $this->getOrder()->getRowTaxDisplayPrecision(),
293: $strong,
294: $separator
295: );
296: } else {
297: return $this->displayPrices(
298: $this->getPriceDataObject()->getData('base_'.$code),
299: $this->getPriceDataObject()->getData($code),
300: $strong,
301: $separator
302: );
303: }
304: }
305:
306: 307: 308: 309: 310: 311: 312: 313: 314:
315: public function displayPrices($basePrice, $price, $strong = false, $separator = '<br />')
316: {
317: return $this->displayRoundedPrices($basePrice, $price, 2, $strong, $separator);
318: }
319:
320: 321: 322: 323: 324: 325: 326: 327: 328: 329:
330: public function displayRoundedPrices($basePrice, $price, $precision=2, $strong = false, $separator = '<br />')
331: {
332: if ($this->getOrder()->isCurrencyDifferent()) {
333: $res = '';
334: $res.= $this->getOrder()->formatBasePricePrecision($basePrice, $precision);
335: $res.= $separator;
336: $res.= $this->getOrder()->formatPricePrecision($price, $precision, true);
337: }
338: else {
339: $res = $this->getOrder()->formatPricePrecision($price, $precision);
340: if ($strong) {
341: $res = '<strong>'.$res.'</strong>';
342: }
343: }
344: return $res;
345: }
346:
347: 348: 349: 350: 351: 352:
353: public function displayPriceInclTax(Varien_Object $item)
354: {
355: $qty = ($item->getQtyOrdered() ? $item->getQtyOrdered() : ($item->getQty() ? $item->getQty() : 1));
356: $baseTax = ($item->getTaxBeforeDiscount() ? $item->getTaxBeforeDiscount() : ($item->getTaxAmount() ? $item->getTaxAmount() : 0));
357: $tax = ($item->getBaseTaxBeforeDiscount() ? $item->getBaseTaxBeforeDiscount() : ($item->getBaseTaxAmount() ? $item->getBaseTaxAmount() : 0));
358:
359: $basePriceTax = 0;
360: $priceTax = 0;
361:
362: if (floatval($qty)) {
363: $basePriceTax = $item->getBasePrice()+$baseTax/$qty;
364: $priceTax = $item->getPrice()+$tax/$qty;
365: }
366:
367: return $this->displayPrices(
368: $this->getOrder()->getStore()->roundPrice($basePriceTax),
369: $this->getOrder()->getStore()->roundPrice($priceTax)
370: );
371: }
372:
373: 374: 375: 376: 377: 378:
379: public function displaySubtotalInclTax($item)
380: {
381: $baseTax = ($item->getTaxBeforeDiscount() ? $item->getTaxBeforeDiscount() : ($item->getTaxAmount() ? $item->getTaxAmount() : 0));
382: $tax = ($item->getBaseTaxBeforeDiscount() ? $item->getBaseTaxBeforeDiscount() : ($item->getBaseTaxAmount() ? $item->getBaseTaxAmount() : 0));
383:
384: return $this->displayPrices(
385: $item->getBaseRowTotal()+$baseTax,
386: $item->getRowTotal()+$tax
387: );
388: }
389:
390: 391: 392: 393: 394: 395:
396: public function displayTaxCalculation(Varien_Object $item)
397: {
398: if ($item->getTaxPercent() && $item->getTaxString() == '') {
399: $percents = array($item->getTaxPercent());
400: } else if ($item->getTaxString()) {
401: $percents = explode(Mage_Tax_Model_Config::CALCULATION_STRING_SEPARATOR, $item->getTaxString());
402: } else {
403: return '0%';
404: }
405:
406: foreach ($percents as &$percent) {
407: $percent = sprintf('%.2f%%', $percent);
408: }
409: return implode(' + ', $percents);
410: }
411:
412: 413: 414: 415: 416: 417:
418: public function displayTaxPercent(Varien_Object $item)
419: {
420: if ($item->getTaxPercent()) {
421: return sprintf('%s%%', $item->getTaxPercent() + 0);
422: } else {
423: return '0%';
424: }
425: }
426:
427: 428: 429:
430:
431: 432: 433: 434: 435:
436: public function canCreateShipment()
437: {
438: foreach ($this->getInvoice()->getAllItems() as $item) {
439: if ($item->getOrderItem()->getQtyToShip()) {
440: return true;
441: }
442: }
443: return false;
444: }
445:
446: 447: 448: 449: 450: 451: 452:
453: public function setCanEditQty($value) {
454: $this->_canEditQty = $value;
455: return $this;
456: }
457:
458: 459: 460: 461: 462:
463: public function canEditQty()
464: {
465: 466: 467:
468: if (!is_null($this->_canEditQty)) {
469: return $this->_canEditQty;
470: }
471:
472: 473: 474: 475:
476: if ($this->getOrder()->getForcedDoShipmentWithInvoice()
477: && ($this->canShipPartially($this->getOrder()) || $this->canShipPartiallyItem($this->getOrder()))
478: ) {
479: return false;
480: }
481: if ($this->getOrder()->getPayment()->canCapture()) {
482: return $this->getOrder()->getPayment()->canCapturePartial();
483: }
484: return true;
485: }
486:
487: public function canCapture()
488: {
489: if (Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/capture')) {
490: return $this->getInvoice()->canCapture();
491: }
492: return false;
493: }
494:
495: public function formatPrice($price)
496: {
497: return $this->getOrder()->formatPrice($price);
498: }
499:
500: 501: 502: 503: 504:
505: public function getSource()
506: {
507: return $this->getInvoice();
508: }
509:
510: 511: 512: 513: 514:
515: public function getInvoice()
516: {
517: return Mage::registry('current_invoice');
518: }
519:
520: 521: 522:
523:
524: public function canReturnToStock() {
525: $canReturnToStock = Mage::getStoreConfig(Mage_CatalogInventory_Model_Stock_Item::XML_PATH_CAN_SUBTRACT);
526: if (Mage::getStoreConfig(Mage_CatalogInventory_Model_Stock_Item::XML_PATH_CAN_SUBTRACT)) {
527: return true;
528: } else {
529: return false;
530: }
531: }
532:
533: 534: 535: 536: 537:
538: public function canReturnItemToStock($item=null) {
539: $canReturnToStock = Mage::getStoreConfig(Mage_CatalogInventory_Model_Stock_Item::XML_PATH_CAN_SUBTRACT);
540: if (!is_null($item)) {
541: if (!$item->hasCanReturnToStock()) {
542: $product = Mage::getModel('catalog/product')->load($item->getOrderItem()->getProductId());
543: if ( $product->getId() && $product->getStockItem()->getManageStock() ) {
544: $item->setCanReturnToStock(true);
545: }
546: else {
547: $item->setCanReturnToStock(false);
548: }
549: }
550: $canReturnToStock = $item->getCanReturnToStock();
551: }
552: return $canReturnToStock;
553: }
554: 555: 556: 557: 558:
559: public function canParentReturnToStock($item = null)
560: {
561: $canReturnToStock = Mage::getStoreConfig(Mage_CatalogInventory_Model_Stock_Item::XML_PATH_CAN_SUBTRACT);
562: if (!is_null($item)) {
563: if ( $item->getCreditmemo()->getOrder()->hasCanReturnToStock() ) {
564: $canReturnToStock = $item->getCreditmemo()->getOrder()->getCanReturnToStock();
565: }
566: } elseif ( $this->getOrder()->hasCanReturnToStock() ) {
567: $canReturnToStock = $this->getOrder()->getCanReturnToStock();
568: }
569: return $canReturnToStock;
570: }
571:
572: 573: 574: 575: 576: 577:
578: public function canShipPartially($order = null)
579: {
580: if (is_null($order) || !$order instanceof Mage_Sales_Model_Order) {
581: $order = Mage::registry('current_shipment')->getOrder();
582: }
583: $value = $order->getCanShipPartially();
584: if (!is_null($value) && !$value) {
585: return false;
586: }
587: return true;
588: }
589:
590: 591: 592: 593: 594: 595:
596: public function canShipPartiallyItem($order = null)
597: {
598: if (is_null($order) || !$order instanceof Mage_Sales_Model_Order) {
599: $order = Mage::registry('current_shipment')->getOrder();
600: }
601: $value = $order->getCanShipPartiallyItem();
602: if (!is_null($value) && !$value) {
603: return false;
604: }
605: return true;
606: }
607:
608: public function isShipmentRegular()
609: {
610: if (!$this->canShipPartiallyItem() || !$this->canShipPartially()) {
611: return false;
612: }
613: return true;
614: }
615:
616: }
617: