PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Adminhtml/Block/Sales/Order/Create/Items/Grid.php

https://gitlab.com/blingbang2016/shop
PHP | 483 lines | 244 code | 37 blank | 202 comment | 28 complexity | 503c5f17f260390a04eba4ebb587d691 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magento.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Adminhtml
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Adminhtml sales order create items grid block
  28. *
  29. * @category Mage
  30. * @package Mage_Adminhtml
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Adminhtml_Block_Sales_Order_Create_Items_Grid extends Mage_Adminhtml_Block_Sales_Order_Create_Abstract
  34. {
  35. /**
  36. * Flag to check can items be move to customer storage
  37. *
  38. * @var bool
  39. */
  40. protected $_moveToCustomerStorage = true;
  41. /**
  42. * Class constructor
  43. */
  44. public function __construct()
  45. {
  46. parent::__construct();
  47. $this->setId('sales_order_create_search_grid');
  48. }
  49. /**
  50. * Returns the items
  51. *
  52. * @return array
  53. */
  54. public function getItems()
  55. {
  56. $items = $this->getParentBlock()->getItems();
  57. $oldSuperMode = $this->getQuote()->getIsSuperMode();
  58. $this->getQuote()->setIsSuperMode(false);
  59. foreach ($items as $item) {
  60. // To dispatch inventory event sales_quote_item_qty_set_after, set item qty
  61. $item->setQty($item->getQty());
  62. $stockItem = $item->getProduct()->getStockItem();
  63. if ($stockItem instanceof Mage_CatalogInventory_Model_Stock_Item) {
  64. // This check has been performed properly in Inventory observer, so it has no sense
  65. /*
  66. $check = $stockItem->checkQuoteItemQty($item->getQty(), $item->getQty(), $item->getQty());
  67. $item->setMessage($check->getMessage());
  68. $item->setHasError($check->getHasError());
  69. */
  70. if ($item->getProduct()->getStatus() == Mage_Catalog_Model_Product_Status::STATUS_DISABLED) {
  71. $item->setMessage(Mage::helper('adminhtml')->__('This product is currently disabled.'));
  72. $item->setHasError(true);
  73. }
  74. }
  75. }
  76. $this->getQuote()->setIsSuperMode($oldSuperMode);
  77. return $items;
  78. }
  79. /**
  80. * Returns the session
  81. *
  82. * @return Mage_Persistent_Helper_Session
  83. */
  84. public function getSession()
  85. {
  86. return $this->getParentBlock()->getSession();
  87. }
  88. /**
  89. * Returns the item's calculation price
  90. *
  91. * @param Mage_Sales_Model_Quote_Item $item
  92. * @return float
  93. */
  94. public function getItemEditablePrice($item)
  95. {
  96. return $item->getCalculationPrice() * 1;
  97. }
  98. /**
  99. * Returns the item's original editable price
  100. *
  101. * @param Mage_Sales_Model_Quote_Item $item
  102. * @return float
  103. */
  104. public function getOriginalEditablePrice($item)
  105. {
  106. if ($item->hasOriginalCustomPrice()) {
  107. $result = $item->getOriginalCustomPrice() * 1;
  108. } elseif ($item->hasCustomPrice()) {
  109. $result = $item->getCustomPrice() * 1;
  110. } else {
  111. if (Mage::helper('tax')->priceIncludesTax($this->getStore())) {
  112. $result = $item->getPriceInclTax() * 1;
  113. } else {
  114. $result = $item->getOriginalPrice() * 1;
  115. }
  116. }
  117. return $result;
  118. }
  119. /**
  120. * Returns the item's original price
  121. *
  122. * @param Mage_Sales_Model_Quote_Item $item
  123. * @return double
  124. */
  125. public function getItemOrigPrice($item)
  126. {
  127. return $this->convertPrice($item->getPrice());
  128. }
  129. /**
  130. * Returns whether the item's gift message is available
  131. *
  132. * @param null|Mage_Sales_Model_Quote_Item $item
  133. * @return bool
  134. */
  135. public function isGiftMessagesAvailable($item = null)
  136. {
  137. if (is_null($item)) {
  138. return $this->helper('giftmessage/message')->getIsMessagesAvailable(
  139. 'items', $this->getQuote(), $this->getStore()
  140. );
  141. }
  142. return $this->helper('giftmessage/message')->getIsMessagesAvailable(
  143. 'item', $item, $this->getStore()
  144. );
  145. }
  146. /**
  147. * Returns whether the item is allowed for the gift message
  148. *
  149. * @param Mage_Sales_Model_Quote_Item $item
  150. * @return bool
  151. */
  152. public function isAllowedForGiftMessage($item)
  153. {
  154. return Mage::getSingleton('adminhtml/giftmessage_save')->getIsAllowedQuoteItem($item);
  155. }
  156. /**
  157. * Check if we need display grid totals include tax
  158. *
  159. * @return bool
  160. */
  161. public function displayTotalsIncludeTax()
  162. {
  163. $res = Mage::getSingleton('tax/config')->displayCartSubtotalInclTax($this->getStore())
  164. || Mage::getSingleton('tax/config')->displayCartSubtotalBoth($this->getStore());
  165. return $res;
  166. }
  167. /**
  168. * Returns the subtotal
  169. *
  170. * @return float
  171. */
  172. public function getSubtotal()
  173. {
  174. $address = $this->getQuoteAddress();
  175. if ($this->displayTotalsIncludeTax()) {
  176. if ($address->getSubtotalInclTax()) {
  177. return $address->getSubtotalInclTax();
  178. }
  179. return $address->getSubtotal()+$address->getTaxAmount();
  180. } else {
  181. return $address->getSubtotal();
  182. }
  183. return false;
  184. }
  185. /**
  186. * Returns the subtotal with any discount removed
  187. *
  188. * @return float
  189. */
  190. public function getSubtotalWithDiscount()
  191. {
  192. $address = $this->getQuoteAddress();
  193. if ($this->displayTotalsIncludeTax()) {
  194. $subtotalInclTax = $address->getSubtotal() + $address->getTaxAmount()
  195. + $address->getHiddenTaxAmount() + $this->getDiscountAmount();
  196. return $subtotalInclTax;
  197. } else {
  198. return $address->getSubtotal() + $this->getDiscountAmount();
  199. }
  200. }
  201. /**
  202. * Return whether the catalog prices include tax
  203. *
  204. * @return bool
  205. */
  206. public function getIsPriceInclTax()
  207. {
  208. return Mage::getSingleton('tax/config')->priceIncludesTax($this->getStore());
  209. }
  210. /**
  211. * Returns the discount amount
  212. *
  213. * @return float
  214. */
  215. public function getDiscountAmount()
  216. {
  217. return $this->getQuote()->getShippingAddress()->getDiscountAmount();
  218. }
  219. /**
  220. * Retrieve quote address
  221. *
  222. * @return Mage_Sales_Model_Quote_Address
  223. */
  224. public function getQuoteAddress()
  225. {
  226. if ($this->getQuote()->isVirtual()) {
  227. return $this->getQuote()->getBillingAddress();
  228. }
  229. else {
  230. return $this->getQuote()->getShippingAddress();
  231. }
  232. }
  233. /**
  234. * Define if specified item has already applied custom price
  235. *
  236. * @param Mage_Sales_Model_Quote_Item $item
  237. * @return bool
  238. */
  239. public function usedCustomPriceForItem($item)
  240. {
  241. return $item->hasCustomPrice();
  242. }
  243. /**
  244. * Define if custom price can be applied for specified item
  245. *
  246. * @param Mage_Sales_Model_Quote_Item $item
  247. * @return bool
  248. */
  249. public function canApplyCustomPrice($item)
  250. {
  251. return !$item->isChildrenCalculated();
  252. }
  253. /**
  254. * Returns the string that contains the 'quantity' title
  255. *
  256. * @param Mage_Sales_Model_Quote_Item $item
  257. * @return string
  258. */
  259. public function getQtyTitle($item)
  260. {
  261. $prices = $item->getProduct()->getTierPrice();
  262. if ($prices) {
  263. $info = array();
  264. foreach ($prices as $data) {
  265. $qty = $data['price_qty'] * 1;
  266. $price = $this->convertPrice($data['price']);
  267. $info[] = $this->helper('sales')->__('Buy %s for price %s', $qty, $price);
  268. }
  269. return implode(', ', $info);
  270. }
  271. else {
  272. return $this->helper('sales')->__('Item ordered qty');
  273. }
  274. }
  275. /**
  276. * Returns the HTML string for the tiered pricing
  277. *
  278. * @param Mage_Sales_Model_Quote_Item $item
  279. * @return string
  280. */
  281. public function getTierHtml($item)
  282. {
  283. $html = '';
  284. $prices = $item->getProduct()->getTierPrice();
  285. if ($prices) {
  286. foreach ($prices as $data) {
  287. $qty = $data['price_qty'] * 1;
  288. $price = $this->convertPrice($data['price']);
  289. $info[] = $this->helper('sales')->__('%s for %s', $qty, $price);
  290. }
  291. $html = implode('<br/>', $info);
  292. }
  293. return $html;
  294. }
  295. /**
  296. * Get Custom Options of item
  297. *
  298. * @param Mage_Sales_Model_Quote_Item $item
  299. * @return array
  300. */
  301. public function getCustomOptions(Mage_Sales_Model_Quote_Item $item)
  302. {
  303. $optionStr = '';
  304. $this->_moveToCustomerStorage = true;
  305. $optionIds = $item->getOptionByCode('option_ids');
  306. if ($optionIds) {
  307. foreach (explode(',', $optionIds->getValue()) as $optionId) {
  308. $option = $item->getProduct()->getOptionById($optionId);
  309. if ($option) {
  310. $optionValue = $item->getOptionByCode('option_' . $option->getId())->getValue();
  311. $optionStr .= $option->getTitle() . ':';
  312. $quoteItemOption = $item->getOptionByCode('option_' . $option->getId());
  313. $group = $option->groupFactory($option->getType())
  314. ->setOption($option)
  315. ->setQuoteItemOption($quoteItemOption);
  316. $optionStr .= $group->getEditableOptionValue($quoteItemOption->getValue());
  317. $optionStr .= "\n";
  318. }
  319. }
  320. }
  321. return $optionStr;
  322. }
  323. /**
  324. * Get flag for rights to move items to customer storage
  325. *
  326. * @return bool
  327. */
  328. public function getMoveToCustomerStorage()
  329. {
  330. return $this->_moveToCustomerStorage;
  331. }
  332. /**
  333. * Returns the item's subtotal that includes tax
  334. *
  335. * @param Mage_Sales_Model_Quote_Item $item
  336. * @return string
  337. */
  338. public function displaySubtotalInclTax($item)
  339. {
  340. if ($item->getTaxBeforeDiscount()) {
  341. $tax = $item->getTaxBeforeDiscount();
  342. } else {
  343. $tax = $item->getTaxAmount() ? $item->getTaxAmount() : 0;
  344. }
  345. return $this->formatPrice($item->getRowTotal() + $tax);
  346. }
  347. /**
  348. * Returns the item's original price that includes tax
  349. *
  350. * @param Mage_Sales_Model_Quote_Item $item
  351. * @return double
  352. */
  353. public function displayOriginalPriceInclTax($item)
  354. {
  355. $tax = 0;
  356. if ($item->getTaxPercent()) {
  357. $tax = $item->getPrice() * ($item->getTaxPercent() / 100);
  358. }
  359. return $this->convertPrice($item->getPrice() + ($tax / $item->getQty()));
  360. }
  361. /**
  362. * Returns the item's row total with any discount and also with any tax
  363. *
  364. * @param Mage_Sales_Model_Quote_Item $item
  365. * @return string
  366. */
  367. public function displayRowTotalWithDiscountInclTax($item)
  368. {
  369. $tax = ($item->getTaxAmount() ? $item->getTaxAmount() : 0);
  370. return $this->formatPrice($item->getRowTotal()-$item->getDiscountAmount()+$tax);
  371. }
  372. /**
  373. * Returns the text for the custom price (whether it includes or excludes tax)
  374. *
  375. * @return string
  376. */
  377. public function getInclExclTaxMessage()
  378. {
  379. if (Mage::helper('tax')->priceIncludesTax($this->getStore())) {
  380. return Mage::helper('sales')->__('* - Enter custom price including tax');
  381. } else {
  382. return Mage::helper('sales')->__('* - Enter custom price excluding tax');
  383. }
  384. }
  385. /**
  386. * Returns the store
  387. *
  388. * @return Mage_Core_Model_Store
  389. */
  390. public function getStore()
  391. {
  392. return $this->getQuote()->getStore();
  393. }
  394. /**
  395. * Return html button which calls configure window
  396. *
  397. * @param Mage_Sales_Model_Quote_Item $item
  398. * @return string
  399. */
  400. public function getConfigureButtonHtml($item)
  401. {
  402. $product = $item->getProduct();
  403. $options = array('label' => Mage::helper('sales')->__('Configure'));
  404. if ($product->canConfigure()) {
  405. $options['onclick'] = sprintf('order.showQuoteItemConfiguration(%s)', $item->getId());
  406. } else {
  407. $options['class'] = ' disabled';
  408. $options['title'] = Mage::helper('sales')->__('This product does not have any configurable options');
  409. }
  410. return $this->getLayout()->createBlock('adminhtml/widget_button')
  411. ->setData($options)
  412. ->toHtml();
  413. }
  414. /**
  415. * Get order item extra info block
  416. *
  417. * @param Mage_Sales_Model_Quote_Item $item
  418. * @return Mage_Core_Block_Abstract
  419. */
  420. public function getItemExtraInfo($item)
  421. {
  422. return $this->getLayout()
  423. ->getBlock('order_item_extra_info')
  424. ->setItem($item);
  425. }
  426. /**
  427. * Returns whether moving to wishlist is allowed for this item
  428. *
  429. * @param Mage_Sales_Model_Quote_Item $item
  430. * @return bool
  431. */
  432. public function isMoveToWishlistAllowed($item)
  433. {
  434. return $item->getProduct()->isVisibleInSiteVisibility();
  435. }
  436. /**
  437. * Retrieve collection of customer wishlists
  438. *
  439. * @return Mage_Wishlist_Model_Resource_Wishlist_Collection
  440. */
  441. public function getCustomerWishlists()
  442. {
  443. return Mage::getModel("wishlist/wishlist")->getCollection()
  444. ->filterByCustomerId($this->getCustomerId());
  445. }
  446. }