PageRenderTime 48ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/app/code/Mage/Sales/Model/Order.php

https://github.com/JackoPlane/magento2
PHP | 2215 lines | 1156 code | 195 blank | 864 comment | 211 complexity | 73bd8e6ed61be2eef51ee16a7a03b4de MD5 | raw file
Possible License(s): CC-BY-SA-3.0

Large files files are truncated, but you can click here to view the full 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@magentocommerce.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.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Sales
  23. * @copyright Copyright (c) 2013 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Order model
  28. *
  29. * Supported events:
  30. * sales_order_load_after
  31. * sales_order_save_before
  32. * sales_order_save_after
  33. * sales_order_delete_before
  34. * sales_order_delete_after
  35. *
  36. * @method Mage_Sales_Model_Resource_Order _getResource()
  37. * @method Mage_Sales_Model_Resource_Order getResource()
  38. * @method string getState()
  39. * @method string getStatus()
  40. * @method Mage_Sales_Model_Order setStatus(string $value)
  41. * @method string getCouponCode()
  42. * @method Mage_Sales_Model_Order setCouponCode(string $value)
  43. * @method string getProtectCode()
  44. * @method Mage_Sales_Model_Order setProtectCode(string $value)
  45. * @method string getShippingDescription()
  46. * @method Mage_Sales_Model_Order setShippingDescription(string $value)
  47. * @method int getIsVirtual()
  48. * @method Mage_Sales_Model_Order setIsVirtual(int $value)
  49. * @method int getStoreId()
  50. * @method Mage_Sales_Model_Order setStoreId(int $value)
  51. * @method int getCustomerId()
  52. * @method Mage_Sales_Model_Order setCustomerId(int $value)
  53. * @method float getBaseDiscountAmount()
  54. * @method Mage_Sales_Model_Order setBaseDiscountAmount(float $value)
  55. * @method float getBaseDiscountCanceled()
  56. * @method Mage_Sales_Model_Order setBaseDiscountCanceled(float $value)
  57. * @method float getBaseDiscountInvoiced()
  58. * @method Mage_Sales_Model_Order setBaseDiscountInvoiced(float $value)
  59. * @method float getBaseDiscountRefunded()
  60. * @method Mage_Sales_Model_Order setBaseDiscountRefunded(float $value)
  61. * @method float getBaseGrandTotal()
  62. * @method Mage_Sales_Model_Order setBaseGrandTotal(float $value)
  63. * @method float getBaseShippingAmount()
  64. * @method Mage_Sales_Model_Order setBaseShippingAmount(float $value)
  65. * @method float getBaseShippingCanceled()
  66. * @method Mage_Sales_Model_Order setBaseShippingCanceled(float $value)
  67. * @method float getBaseShippingInvoiced()
  68. * @method Mage_Sales_Model_Order setBaseShippingInvoiced(float $value)
  69. * @method float getBaseShippingRefunded()
  70. * @method Mage_Sales_Model_Order setBaseShippingRefunded(float $value)
  71. * @method float getBaseShippingTaxAmount()
  72. * @method Mage_Sales_Model_Order setBaseShippingTaxAmount(float $value)
  73. * @method float getBaseShippingTaxRefunded()
  74. * @method Mage_Sales_Model_Order setBaseShippingTaxRefunded(float $value)
  75. * @method float getBaseSubtotal()
  76. * @method Mage_Sales_Model_Order setBaseSubtotal(float $value)
  77. * @method float getBaseSubtotalCanceled()
  78. * @method Mage_Sales_Model_Order setBaseSubtotalCanceled(float $value)
  79. * @method float getBaseSubtotalInvoiced()
  80. * @method Mage_Sales_Model_Order setBaseSubtotalInvoiced(float $value)
  81. * @method float getBaseSubtotalRefunded()
  82. * @method Mage_Sales_Model_Order setBaseSubtotalRefunded(float $value)
  83. * @method float getBaseTaxAmount()
  84. * @method Mage_Sales_Model_Order setBaseTaxAmount(float $value)
  85. * @method float getBaseTaxCanceled()
  86. * @method Mage_Sales_Model_Order setBaseTaxCanceled(float $value)
  87. * @method float getBaseTaxInvoiced()
  88. * @method Mage_Sales_Model_Order setBaseTaxInvoiced(float $value)
  89. * @method float getBaseTaxRefunded()
  90. * @method Mage_Sales_Model_Order setBaseTaxRefunded(float $value)
  91. * @method float getBaseToGlobalRate()
  92. * @method Mage_Sales_Model_Order setBaseToGlobalRate(float $value)
  93. * @method float getBaseToOrderRate()
  94. * @method Mage_Sales_Model_Order setBaseToOrderRate(float $value)
  95. * @method float getBaseTotalCanceled()
  96. * @method Mage_Sales_Model_Order setBaseTotalCanceled(float $value)
  97. * @method float getBaseTotalInvoiced()
  98. * @method Mage_Sales_Model_Order setBaseTotalInvoiced(float $value)
  99. * @method float getBaseTotalInvoicedCost()
  100. * @method Mage_Sales_Model_Order setBaseTotalInvoicedCost(float $value)
  101. * @method float getBaseTotalOfflineRefunded()
  102. * @method Mage_Sales_Model_Order setBaseTotalOfflineRefunded(float $value)
  103. * @method float getBaseTotalOnlineRefunded()
  104. * @method Mage_Sales_Model_Order setBaseTotalOnlineRefunded(float $value)
  105. * @method float getBaseTotalPaid()
  106. * @method Mage_Sales_Model_Order setBaseTotalPaid(float $value)
  107. * @method float getBaseTotalQtyOrdered()
  108. * @method Mage_Sales_Model_Order setBaseTotalQtyOrdered(float $value)
  109. * @method float getBaseTotalRefunded()
  110. * @method Mage_Sales_Model_Order setBaseTotalRefunded(float $value)
  111. * @method float getDiscountAmount()
  112. * @method Mage_Sales_Model_Order setDiscountAmount(float $value)
  113. * @method float getDiscountCanceled()
  114. * @method Mage_Sales_Model_Order setDiscountCanceled(float $value)
  115. * @method float getDiscountInvoiced()
  116. * @method Mage_Sales_Model_Order setDiscountInvoiced(float $value)
  117. * @method float getDiscountRefunded()
  118. * @method Mage_Sales_Model_Order setDiscountRefunded(float $value)
  119. * @method float getGrandTotal()
  120. * @method Mage_Sales_Model_Order setGrandTotal(float $value)
  121. * @method float getShippingAmount()
  122. * @method Mage_Sales_Model_Order setShippingAmount(float $value)
  123. * @method float getShippingCanceled()
  124. * @method Mage_Sales_Model_Order setShippingCanceled(float $value)
  125. * @method float getShippingInvoiced()
  126. * @method Mage_Sales_Model_Order setShippingInvoiced(float $value)
  127. * @method float getShippingRefunded()
  128. * @method Mage_Sales_Model_Order setShippingRefunded(float $value)
  129. * @method float getShippingTaxAmount()
  130. * @method Mage_Sales_Model_Order setShippingTaxAmount(float $value)
  131. * @method float getShippingTaxRefunded()
  132. * @method Mage_Sales_Model_Order setShippingTaxRefunded(float $value)
  133. * @method float getStoreToBaseRate()
  134. * @method Mage_Sales_Model_Order setStoreToBaseRate(float $value)
  135. * @method float getStoreToOrderRate()
  136. * @method Mage_Sales_Model_Order setStoreToOrderRate(float $value)
  137. * @method float getSubtotal()
  138. * @method Mage_Sales_Model_Order setSubtotal(float $value)
  139. * @method float getSubtotalCanceled()
  140. * @method Mage_Sales_Model_Order setSubtotalCanceled(float $value)
  141. * @method float getSubtotalInvoiced()
  142. * @method Mage_Sales_Model_Order setSubtotalInvoiced(float $value)
  143. * @method float getSubtotalRefunded()
  144. * @method Mage_Sales_Model_Order setSubtotalRefunded(float $value)
  145. * @method float getTaxAmount()
  146. * @method Mage_Sales_Model_Order setTaxAmount(float $value)
  147. * @method float getTaxCanceled()
  148. * @method Mage_Sales_Model_Order setTaxCanceled(float $value)
  149. * @method float getTaxInvoiced()
  150. * @method Mage_Sales_Model_Order setTaxInvoiced(float $value)
  151. * @method float getTaxRefunded()
  152. * @method Mage_Sales_Model_Order setTaxRefunded(float $value)
  153. * @method float getTotalCanceled()
  154. * @method Mage_Sales_Model_Order setTotalCanceled(float $value)
  155. * @method float getTotalInvoiced()
  156. * @method Mage_Sales_Model_Order setTotalInvoiced(float $value)
  157. * @method float getTotalOfflineRefunded()
  158. * @method Mage_Sales_Model_Order setTotalOfflineRefunded(float $value)
  159. * @method float getTotalOnlineRefunded()
  160. * @method Mage_Sales_Model_Order setTotalOnlineRefunded(float $value)
  161. * @method float getTotalPaid()
  162. * @method Mage_Sales_Model_Order setTotalPaid(float $value)
  163. * @method float getTotalQtyOrdered()
  164. * @method Mage_Sales_Model_Order setTotalQtyOrdered(float $value)
  165. * @method float getTotalRefunded()
  166. * @method Mage_Sales_Model_Order setTotalRefunded(float $value)
  167. * @method int getCanShipPartially()
  168. * @method Mage_Sales_Model_Order setCanShipPartially(int $value)
  169. * @method int getCanShipPartiallyItem()
  170. * @method Mage_Sales_Model_Order setCanShipPartiallyItem(int $value)
  171. * @method int getCustomerIsGuest()
  172. * @method Mage_Sales_Model_Order setCustomerIsGuest(int $value)
  173. * @method int getCustomerNoteNotify()
  174. * @method Mage_Sales_Model_Order setCustomerNoteNotify(int $value)
  175. * @method int getBillingAddressId()
  176. * @method Mage_Sales_Model_Order setBillingAddressId(int $value)
  177. * @method int getCustomerGroupId()
  178. * @method Mage_Sales_Model_Order setCustomerGroupId(int $value)
  179. * @method int getEditIncrement()
  180. * @method Mage_Sales_Model_Order setEditIncrement(int $value)
  181. * @method int getEmailSent()
  182. * @method Mage_Sales_Model_Order setEmailSent(int $value)
  183. * @method int getForcedShipmentWithInvoice()
  184. * @method Mage_Sales_Model_Order setForcedShipmentWithInvoice(int $value)
  185. * @method int getGiftMessageId()
  186. * @method Mage_Sales_Model_Order setGiftMessageId(int $value)
  187. * @method int getPaymentAuthExpiration()
  188. * @method Mage_Sales_Model_Order setPaymentAuthExpiration(int $value)
  189. * @method int getPaypalIpnCustomerNotified()
  190. * @method Mage_Sales_Model_Order setPaypalIpnCustomerNotified(int $value)
  191. * @method int getQuoteAddressId()
  192. * @method Mage_Sales_Model_Order setQuoteAddressId(int $value)
  193. * @method int getQuoteId()
  194. * @method Mage_Sales_Model_Order setQuoteId(int $value)
  195. * @method int getShippingAddressId()
  196. * @method Mage_Sales_Model_Order setShippingAddressId(int $value)
  197. * @method float getAdjustmentNegative()
  198. * @method Mage_Sales_Model_Order setAdjustmentNegative(float $value)
  199. * @method float getAdjustmentPositive()
  200. * @method Mage_Sales_Model_Order setAdjustmentPositive(float $value)
  201. * @method float getBaseAdjustmentNegative()
  202. * @method Mage_Sales_Model_Order setBaseAdjustmentNegative(float $value)
  203. * @method float getBaseAdjustmentPositive()
  204. * @method Mage_Sales_Model_Order setBaseAdjustmentPositive(float $value)
  205. * @method float getBaseShippingDiscountAmount()
  206. * @method Mage_Sales_Model_Order setBaseShippingDiscountAmount(float $value)
  207. * @method float getBaseSubtotalInclTax()
  208. * @method Mage_Sales_Model_Order setBaseSubtotalInclTax(float $value)
  209. * @method Mage_Sales_Model_Order setBaseTotalDue(float $value)
  210. * @method float getPaymentAuthorizationAmount()
  211. * @method Mage_Sales_Model_Order setPaymentAuthorizationAmount(float $value)
  212. * @method float getShippingDiscountAmount()
  213. * @method Mage_Sales_Model_Order setShippingDiscountAmount(float $value)
  214. * @method float getSubtotalInclTax()
  215. * @method Mage_Sales_Model_Order setSubtotalInclTax(float $value)
  216. * @method Mage_Sales_Model_Order setTotalDue(float $value)
  217. * @method float getWeight()
  218. * @method Mage_Sales_Model_Order setWeight(float $value)
  219. * @method string getCustomerDob()
  220. * @method Mage_Sales_Model_Order setCustomerDob(string $value)
  221. * @method string getIncrementId()
  222. * @method Mage_Sales_Model_Order setIncrementId(string $value)
  223. * @method string getAppliedRuleIds()
  224. * @method Mage_Sales_Model_Order setAppliedRuleIds(string $value)
  225. * @method string getBaseCurrencyCode()
  226. * @method Mage_Sales_Model_Order setBaseCurrencyCode(string $value)
  227. * @method string getCustomerEmail()
  228. * @method Mage_Sales_Model_Order setCustomerEmail(string $value)
  229. * @method string getCustomerFirstname()
  230. * @method Mage_Sales_Model_Order setCustomerFirstname(string $value)
  231. * @method string getCustomerLastname()
  232. * @method Mage_Sales_Model_Order setCustomerLastname(string $value)
  233. * @method string getCustomerMiddlename()
  234. * @method Mage_Sales_Model_Order setCustomerMiddlename(string $value)
  235. * @method string getCustomerPrefix()
  236. * @method Mage_Sales_Model_Order setCustomerPrefix(string $value)
  237. * @method string getCustomerSuffix()
  238. * @method Mage_Sales_Model_Order setCustomerSuffix(string $value)
  239. * @method string getCustomerTaxvat()
  240. * @method Mage_Sales_Model_Order setCustomerTaxvat(string $value)
  241. * @method string getDiscountDescription()
  242. * @method Mage_Sales_Model_Order setDiscountDescription(string $value)
  243. * @method string getExtCustomerId()
  244. * @method Mage_Sales_Model_Order setExtCustomerId(string $value)
  245. * @method string getExtOrderId()
  246. * @method Mage_Sales_Model_Order setExtOrderId(string $value)
  247. * @method string getGlobalCurrencyCode()
  248. * @method Mage_Sales_Model_Order setGlobalCurrencyCode(string $value)
  249. * @method string getHoldBeforeState()
  250. * @method Mage_Sales_Model_Order setHoldBeforeState(string $value)
  251. * @method string getHoldBeforeStatus()
  252. * @method Mage_Sales_Model_Order setHoldBeforeStatus(string $value)
  253. * @method string getOrderCurrencyCode()
  254. * @method Mage_Sales_Model_Order setOrderCurrencyCode(string $value)
  255. * @method string getOriginalIncrementId()
  256. * @method Mage_Sales_Model_Order setOriginalIncrementId(string $value)
  257. * @method string getRelationChildId()
  258. * @method Mage_Sales_Model_Order setRelationChildId(string $value)
  259. * @method string getRelationChildRealId()
  260. * @method Mage_Sales_Model_Order setRelationChildRealId(string $value)
  261. * @method string getRelationParentId()
  262. * @method Mage_Sales_Model_Order setRelationParentId(string $value)
  263. * @method string getRelationParentRealId()
  264. * @method Mage_Sales_Model_Order setRelationParentRealId(string $value)
  265. * @method string getRemoteIp()
  266. * @method Mage_Sales_Model_Order setRemoteIp(string $value)
  267. * @method string getShippingMethod()
  268. * @method Mage_Sales_Model_Order setShippingMethod(string $value)
  269. * @method string getStoreCurrencyCode()
  270. * @method Mage_Sales_Model_Order setStoreCurrencyCode(string $value)
  271. * @method string getStoreName()
  272. * @method Mage_Sales_Model_Order setStoreName(string $value)
  273. * @method string getXForwardedFor()
  274. * @method Mage_Sales_Model_Order setXForwardedFor(string $value)
  275. * @method string getCustomerNote()
  276. * @method Mage_Sales_Model_Order setCustomerNote(string $value)
  277. * @method string getCreatedAt()
  278. * @method Mage_Sales_Model_Order setCreatedAt(string $value)
  279. * @method string getUpdatedAt()
  280. * @method Mage_Sales_Model_Order setUpdatedAt(string $value)
  281. * @method int getTotalItemCount()
  282. * @method Mage_Sales_Model_Order setTotalItemCount(int $value)
  283. * @method int getCustomerGender()
  284. * @method Mage_Sales_Model_Order setCustomerGender(int $value)
  285. * @method float getHiddenTaxAmount()
  286. * @method Mage_Sales_Model_Order setHiddenTaxAmount(float $value)
  287. * @method float getBaseHiddenTaxAmount()
  288. * @method Mage_Sales_Model_Order setBaseHiddenTaxAmount(float $value)
  289. * @method float getShippingHiddenTaxAmount()
  290. * @method Mage_Sales_Model_Order setShippingHiddenTaxAmount(float $value)
  291. * @method float getBaseShippingHiddenTaxAmnt()
  292. * @method Mage_Sales_Model_Order setBaseShippingHiddenTaxAmnt(float $value)
  293. * @method float getHiddenTaxInvoiced()
  294. * @method Mage_Sales_Model_Order setHiddenTaxInvoiced(float $value)
  295. * @method float getBaseHiddenTaxInvoiced()
  296. * @method Mage_Sales_Model_Order setBaseHiddenTaxInvoiced(float $value)
  297. * @method float getHiddenTaxRefunded()
  298. * @method Mage_Sales_Model_Order setHiddenTaxRefunded(float $value)
  299. * @method float getBaseHiddenTaxRefunded()
  300. * @method Mage_Sales_Model_Order setBaseHiddenTaxRefunded(float $value)
  301. * @method float getShippingInclTax()
  302. * @method Mage_Sales_Model_Order setShippingInclTax(float $value)
  303. * @method float getBaseShippingInclTax()
  304. * @method Mage_Sales_Model_Order setBaseShippingInclTax(float $value)
  305. *
  306. * @category Mage
  307. * @package Mage_Sales
  308. * @author Magento Core Team <core@magentocommerce.com>
  309. */
  310. class Mage_Sales_Model_Order extends Mage_Sales_Model_Abstract
  311. {
  312. const ENTITY = 'order';
  313. /**
  314. * XML configuration paths
  315. */
  316. const XML_PATH_EMAIL_TEMPLATE = 'sales_email/order/template';
  317. const XML_PATH_EMAIL_GUEST_TEMPLATE = 'sales_email/order/guest_template';
  318. const XML_PATH_EMAIL_IDENTITY = 'sales_email/order/identity';
  319. const XML_PATH_EMAIL_COPY_TO = 'sales_email/order/copy_to';
  320. const XML_PATH_EMAIL_COPY_METHOD = 'sales_email/order/copy_method';
  321. const XML_PATH_EMAIL_ENABLED = 'sales_email/order/enabled';
  322. const XML_PATH_UPDATE_EMAIL_TEMPLATE = 'sales_email/order_comment/template';
  323. const XML_PATH_UPDATE_EMAIL_GUEST_TEMPLATE = 'sales_email/order_comment/guest_template';
  324. const XML_PATH_UPDATE_EMAIL_IDENTITY = 'sales_email/order_comment/identity';
  325. const XML_PATH_UPDATE_EMAIL_COPY_TO = 'sales_email/order_comment/copy_to';
  326. const XML_PATH_UPDATE_EMAIL_COPY_METHOD = 'sales_email/order_comment/copy_method';
  327. const XML_PATH_UPDATE_EMAIL_ENABLED = 'sales_email/order_comment/enabled';
  328. /**
  329. * Order states
  330. */
  331. const STATE_NEW = 'new';
  332. const STATE_PENDING_PAYMENT = 'pending_payment';
  333. const STATE_PROCESSING = 'processing';
  334. const STATE_COMPLETE = 'complete';
  335. const STATE_CLOSED = 'closed';
  336. const STATE_CANCELED = 'canceled';
  337. const STATE_HOLDED = 'holded';
  338. const STATE_PAYMENT_REVIEW = 'payment_review';
  339. /**
  340. * Order statuses
  341. */
  342. const STATUS_FRAUD = 'fraud';
  343. /**
  344. * Order flags
  345. */
  346. const ACTION_FLAG_CANCEL = 'cancel';
  347. const ACTION_FLAG_HOLD = 'hold';
  348. const ACTION_FLAG_UNHOLD = 'unhold';
  349. const ACTION_FLAG_EDIT = 'edit';
  350. const ACTION_FLAG_CREDITMEMO= 'creditmemo';
  351. const ACTION_FLAG_INVOICE = 'invoice';
  352. const ACTION_FLAG_REORDER = 'reorder';
  353. const ACTION_FLAG_SHIP = 'ship';
  354. const ACTION_FLAG_COMMENT = 'comment';
  355. /**
  356. * Report date types
  357. */
  358. const REPORT_DATE_TYPE_CREATED = 'created';
  359. const REPORT_DATE_TYPE_UPDATED = 'updated';
  360. /*
  361. * Identifier for history item
  362. */
  363. const HISTORY_ENTITY_NAME = 'order';
  364. protected $_eventPrefix = 'sales_order';
  365. protected $_eventObject = 'order';
  366. protected $_addresses = null;
  367. protected $_items = null;
  368. protected $_payments = null;
  369. protected $_statusHistory = null;
  370. protected $_invoices;
  371. protected $_tracks;
  372. protected $_shipments;
  373. protected $_creditmemos;
  374. protected $_relatedObjects = array();
  375. protected $_orderCurrency = null;
  376. protected $_baseCurrency = null;
  377. /**
  378. * Array of action flags for canUnhold, canEdit, etc.
  379. *
  380. * @var array
  381. */
  382. protected $_actionFlag = array();
  383. /**
  384. * Flag: if after order placing we can send new email to the customer.
  385. *
  386. * @var bool
  387. */
  388. protected $_canSendNewEmailFlag = true;
  389. /*
  390. * Identifier for history item
  391. *
  392. * @var string
  393. */
  394. protected $_historyEntityName = self::HISTORY_ENTITY_NAME;
  395. /**
  396. * Initialize resource model
  397. */
  398. protected function _construct()
  399. {
  400. $this->_init('Mage_Sales_Model_Resource_Order');
  401. }
  402. /**
  403. * Clear order object data
  404. *
  405. * @param string $key data key
  406. * @return Mage_Sales_Model_Order
  407. */
  408. public function unsetData($key=null)
  409. {
  410. parent::unsetData($key);
  411. if (is_null($key)) {
  412. $this->_items = null;
  413. }
  414. return $this;
  415. }
  416. /**
  417. * Retrieve can flag for action (edit, unhold, etc..)
  418. *
  419. * @param string $action
  420. * @return boolean|null
  421. */
  422. public function getActionFlag($action)
  423. {
  424. if (isset($this->_actionFlag[$action])) {
  425. return $this->_actionFlag[$action];
  426. }
  427. return null;
  428. }
  429. /**
  430. * Set can flag value for action (edit, unhold, etc...)
  431. *
  432. * @param string $action
  433. * @param boolean $flag
  434. * @return Mage_Sales_Model_Order
  435. */
  436. public function setActionFlag($action, $flag)
  437. {
  438. $this->_actionFlag[$action] = (boolean) $flag;
  439. return $this;
  440. }
  441. /**
  442. * Return flag for order if it can sends new email to customer.
  443. *
  444. * @return bool
  445. */
  446. public function getCanSendNewEmailFlag()
  447. {
  448. return $this->_canSendNewEmailFlag;
  449. }
  450. /**
  451. * Set flag for order if it can sends new email to customer.
  452. *
  453. * @param bool $flag
  454. * @return Mage_Sales_Model_Order
  455. */
  456. public function setCanSendNewEmailFlag($flag)
  457. {
  458. $this->_canSendNewEmailFlag = (boolean) $flag;
  459. return $this;
  460. }
  461. /**
  462. * Load order by system increment identifier
  463. *
  464. * @param string $incrementId
  465. * @return Mage_Sales_Model_Order
  466. */
  467. public function loadByIncrementId($incrementId)
  468. {
  469. return $this->loadByAttribute('increment_id', $incrementId);
  470. }
  471. /**
  472. * Load order by custom attribute value. Attribute value should be unique
  473. *
  474. * @param string $attribute
  475. * @param string $value
  476. * @return Mage_Sales_Model_Order
  477. */
  478. public function loadByAttribute($attribute, $value)
  479. {
  480. $this->load($value, $attribute);
  481. return $this;
  482. }
  483. /**
  484. * Retrieve store model instance
  485. *
  486. * @return Mage_Core_Model_Store
  487. */
  488. public function getStore()
  489. {
  490. $storeId = $this->getStoreId();
  491. if ($storeId) {
  492. return Mage::app()->getStore($storeId);
  493. }
  494. return Mage::app()->getStore();
  495. }
  496. /**
  497. * Retrieve order cancel availability
  498. *
  499. * @return bool
  500. */
  501. public function canCancel()
  502. {
  503. if ($this->canUnhold()) { // $this->isPaymentReview()
  504. return false;
  505. }
  506. $allInvoiced = true;
  507. foreach ($this->getAllItems() as $item) {
  508. if ($item->getQtyToInvoice()) {
  509. $allInvoiced = false;
  510. break;
  511. }
  512. }
  513. if ($allInvoiced) {
  514. return false;
  515. }
  516. $state = $this->getState();
  517. if ($this->isCanceled() || $state === self::STATE_COMPLETE || $state === self::STATE_CLOSED) {
  518. return false;
  519. }
  520. if ($this->getActionFlag(self::ACTION_FLAG_CANCEL) === false) {
  521. return false;
  522. }
  523. /**
  524. * Use only state for availability detect
  525. */
  526. /*foreach ($this->getAllItems() as $item) {
  527. if ($item->getQtyToCancel()>0) {
  528. return true;
  529. }
  530. }
  531. return false;*/
  532. return true;
  533. }
  534. /**
  535. * Getter whether the payment can be voided
  536. * @return bool
  537. */
  538. public function canVoidPayment()
  539. {
  540. if ($this->canUnhold() || $this->isPaymentReview()) {
  541. return false;
  542. }
  543. $state = $this->getState();
  544. if ($this->isCanceled() || $state === self::STATE_COMPLETE || $state === self::STATE_CLOSED) {
  545. return false;
  546. }
  547. return $this->getPayment()->canVoid(new Varien_Object);
  548. }
  549. /**
  550. * Retrieve order invoice availability
  551. *
  552. * @return bool
  553. */
  554. public function canInvoice()
  555. {
  556. if ($this->canUnhold() || $this->isPaymentReview()) {
  557. return false;
  558. }
  559. $state = $this->getState();
  560. if ($this->isCanceled() || $state === self::STATE_COMPLETE || $state === self::STATE_CLOSED) {
  561. return false;
  562. }
  563. if ($this->getActionFlag(self::ACTION_FLAG_INVOICE) === false) {
  564. return false;
  565. }
  566. foreach ($this->getAllItems() as $item) {
  567. if ($item->getQtyToInvoice()>0 && !$item->getLockedDoInvoice()) {
  568. return true;
  569. }
  570. }
  571. return false;
  572. }
  573. /**
  574. * Retrieve order credit memo (refund) availability
  575. *
  576. * @return bool
  577. */
  578. public function canCreditmemo()
  579. {
  580. if ($this->hasForcedCanCreditmemo()) {
  581. return $this->getForcedCanCreditmemo();
  582. }
  583. if ($this->canUnhold() || $this->isPaymentReview()) {
  584. return false;
  585. }
  586. if ($this->isCanceled() || $this->getState() === self::STATE_CLOSED) {
  587. return false;
  588. }
  589. /**
  590. * We can have problem with float in php (on some server $a=762.73;$b=762.73; $a-$b!=0)
  591. * for this we have additional diapason for 0
  592. * TotalPaid - contains amount, that were not rounded.
  593. */
  594. if (abs($this->getStore()->roundPrice($this->getTotalPaid()) - $this->getTotalRefunded()) < .0001) {
  595. return false;
  596. }
  597. if ($this->getActionFlag(self::ACTION_FLAG_EDIT) === false) {
  598. return false;
  599. }
  600. return true;
  601. }
  602. /**
  603. * Retrieve order hold availability
  604. *
  605. * @return bool
  606. */
  607. public function canHold()
  608. {
  609. $state = $this->getState();
  610. if ($this->isCanceled() || $this->isPaymentReview()
  611. || $state === self::STATE_COMPLETE || $state === self::STATE_CLOSED || $state === self::STATE_HOLDED) {
  612. return false;
  613. }
  614. if ($this->getActionFlag(self::ACTION_FLAG_HOLD) === false) {
  615. return false;
  616. }
  617. return true;
  618. }
  619. /**
  620. * Retrieve order unhold availability
  621. *
  622. * @return bool
  623. */
  624. public function canUnhold()
  625. {
  626. if ($this->getActionFlag(self::ACTION_FLAG_UNHOLD) === false || $this->isPaymentReview()) {
  627. return false;
  628. }
  629. return $this->getState() === self::STATE_HOLDED;
  630. }
  631. /**
  632. * Check if comment can be added to order history
  633. *
  634. * @return bool
  635. */
  636. public function canComment()
  637. {
  638. if ($this->getActionFlag(self::ACTION_FLAG_COMMENT) === false) {
  639. return false;
  640. }
  641. return true;
  642. }
  643. /**
  644. * Retrieve order shipment availability
  645. *
  646. * @return bool
  647. */
  648. public function canShip()
  649. {
  650. if ($this->canUnhold() || $this->isPaymentReview()) {
  651. return false;
  652. }
  653. if ($this->getIsVirtual() || $this->isCanceled()) {
  654. return false;
  655. }
  656. if ($this->getActionFlag(self::ACTION_FLAG_SHIP) === false) {
  657. return false;
  658. }
  659. foreach ($this->getAllItems() as $item) {
  660. if ($item->getQtyToShip()>0 && !$item->getIsVirtual()
  661. && !$item->getLockedDoShip())
  662. {
  663. return true;
  664. }
  665. }
  666. return false;
  667. }
  668. /**
  669. * Retrieve order edit availability
  670. *
  671. * @return bool
  672. */
  673. public function canEdit()
  674. {
  675. if ($this->canUnhold()) {
  676. return false;
  677. }
  678. $state = $this->getState();
  679. if ($this->isCanceled() || $this->isPaymentReview()
  680. || $state === self::STATE_COMPLETE || $state === self::STATE_CLOSED) {
  681. return false;
  682. }
  683. if (!$this->getPayment()->getMethodInstance()->canEdit()) {
  684. return false;
  685. }
  686. if ($this->getActionFlag(self::ACTION_FLAG_EDIT) === false) {
  687. return false;
  688. }
  689. return true;
  690. }
  691. /**
  692. * Retrieve order reorder availability
  693. *
  694. * @return bool
  695. */
  696. public function canReorder()
  697. {
  698. return $this->_canReorder(false);
  699. }
  700. /**
  701. * Check the ability to reorder ignoring the availability in stock or status of the ordered products
  702. *
  703. * @return bool
  704. */
  705. public function canReorderIgnoreSalable()
  706. {
  707. return $this->_canReorder(true);
  708. }
  709. /**
  710. * Retrieve order reorder availability
  711. *
  712. * @param bool $ignoreSalable
  713. * @return bool
  714. */
  715. protected function _canReorder($ignoreSalable = false)
  716. {
  717. if ($this->canUnhold() || $this->isPaymentReview() || !$this->getCustomerId()) {
  718. return false;
  719. }
  720. $products = array();
  721. foreach ($this->getItemsCollection() as $item) {
  722. $products[] = $item->getProductId();
  723. }
  724. if (!empty($products)) {
  725. /*
  726. * @TODO ACPAOC: Use product collection here, but ensure that product
  727. * is loaded with order store id, otherwise there'll be problems with isSalable()
  728. * for configurables, bundles and other composites
  729. *
  730. */
  731. /*
  732. $productsCollection = Mage::getModel('Mage_Catalog_Model_Product')->getCollection()
  733. ->setStoreId($this->getStoreId())
  734. ->addIdFilter($products)
  735. ->addAttributeToSelect('status')
  736. ->load();
  737. foreach ($productsCollection as $product) {
  738. if (!$product->isSalable()) {
  739. return false;
  740. }
  741. }
  742. */
  743. foreach ($products as $productId) {
  744. $product = Mage::getModel('Mage_Catalog_Model_Product')
  745. ->setStoreId($this->getStoreId())
  746. ->load($productId);
  747. if (!$product->getId() || (!$ignoreSalable && !$product->isSalable())) {
  748. return false;
  749. }
  750. }
  751. }
  752. if ($this->getActionFlag(self::ACTION_FLAG_REORDER) === false) {
  753. return false;
  754. }
  755. return true;
  756. }
  757. /**
  758. * Check whether the payment is in payment review state
  759. * In this state order cannot be normally processed. Possible actions can be:
  760. * - accept or deny payment
  761. * - fetch transaction information
  762. *
  763. * @return bool
  764. */
  765. public function isPaymentReview()
  766. {
  767. return $this->getState() === self::STATE_PAYMENT_REVIEW;
  768. }
  769. /**
  770. * Check whether payment can be accepted or denied
  771. *
  772. * @return bool
  773. */
  774. public function canReviewPayment()
  775. {
  776. return $this->isPaymentReview() && $this->getPayment()->canReviewPayment();
  777. }
  778. /**
  779. * Check whether there can be a transaction update fetched for payment in review state
  780. *
  781. * @return bool
  782. */
  783. public function canFetchPaymentReviewUpdate()
  784. {
  785. return $this->isPaymentReview() && $this->getPayment()->canFetchTransactionInfo();
  786. }
  787. /**
  788. * Retrieve order configuration model
  789. *
  790. * @return Mage_Sales_Model_Order_Config
  791. */
  792. public function getConfig()
  793. {
  794. return Mage::getSingleton('Mage_Sales_Model_Order_Config');
  795. }
  796. /**
  797. * Place order payments
  798. *
  799. * @return Mage_Sales_Model_Order
  800. */
  801. protected function _placePayment()
  802. {
  803. $this->getPayment()->place();
  804. return $this;
  805. }
  806. /**
  807. * Retrieve order payment model object
  808. *
  809. * @return Mage_Sales_Model_Order_Payment
  810. */
  811. public function getPayment()
  812. {
  813. foreach ($this->getPaymentsCollection() as $payment) {
  814. if (!$payment->isDeleted()) {
  815. return $payment;
  816. }
  817. }
  818. return false;
  819. }
  820. /**
  821. * Declare order billing address
  822. *
  823. * @param Mage_Sales_Model_Order_Address $address
  824. * @return Mage_Sales_Model_Order
  825. */
  826. public function setBillingAddress(Mage_Sales_Model_Order_Address $address)
  827. {
  828. $old = $this->getBillingAddress();
  829. if (!empty($old)) {
  830. $address->setId($old->getId());
  831. }
  832. $this->addAddress($address->setAddressType('billing'));
  833. return $this;
  834. }
  835. /**
  836. * Declare order shipping address
  837. *
  838. * @param Mage_Sales_Model_Order_Address $address
  839. * @return Mage_Sales_Model_Order
  840. */
  841. public function setShippingAddress(Mage_Sales_Model_Order_Address $address)
  842. {
  843. $old = $this->getShippingAddress();
  844. if (!empty($old)) {
  845. $address->setId($old->getId());
  846. }
  847. $this->addAddress($address->setAddressType('shipping'));
  848. return $this;
  849. }
  850. /**
  851. * Retrieve order billing address
  852. *
  853. * @return Mage_Sales_Model_Order_Address
  854. */
  855. public function getBillingAddress()
  856. {
  857. foreach ($this->getAddressesCollection() as $address) {
  858. if ($address->getAddressType()=='billing' && !$address->isDeleted()) {
  859. return $address;
  860. }
  861. }
  862. return false;
  863. }
  864. /**
  865. * Retrieve order shipping address
  866. *
  867. * @return Mage_Sales_Model_Order_Address|bool
  868. */
  869. public function getShippingAddress()
  870. {
  871. foreach ($this->getAddressesCollection() as $address) {
  872. if ($address->getAddressType() == 'shipping' && !$address->isDeleted()) {
  873. return $address;
  874. }
  875. }
  876. return false;
  877. }
  878. /**
  879. * Order state setter.
  880. * If status is specified, will add order status history with specified comment
  881. * the setData() cannot be overriden because of compatibility issues with resource model
  882. *
  883. * @param string $state
  884. * @param string|bool $status
  885. * @param string $comment
  886. * @param bool $isCustomerNotified
  887. * @return Mage_Sales_Model_Order
  888. */
  889. public function setState($state, $status = false, $comment = '', $isCustomerNotified = null)
  890. {
  891. return $this->_setState($state, $status, $comment, $isCustomerNotified, true);
  892. }
  893. /**
  894. * Order state protected setter.
  895. * By default allows to set any state. Can also update status to default or specified value
  896. * 小omplete and closed states are encapsulated intentionally, see the _checkState()
  897. *
  898. * @param string $state
  899. * @param string|bool $status
  900. * @param string $comment
  901. * @param bool $isCustomerNotified
  902. * @param $shouldProtectState
  903. * @return Mage_Sales_Model_Order
  904. */
  905. protected function _setState($state, $status = false, $comment = '',
  906. $isCustomerNotified = null, $shouldProtectState = false)
  907. {
  908. // attempt to set the specified state
  909. if ($shouldProtectState) {
  910. if ($this->isStateProtected($state)) {
  911. Mage::throwException(
  912. Mage::helper('Mage_Sales_Helper_Data')->__('The Order State "%s" must not be set manually.', $state)
  913. );
  914. }
  915. }
  916. $this->setData('state', $state);
  917. // add status history
  918. if ($status) {
  919. if ($status === true) {
  920. $status = $this->getConfig()->getStateDefaultStatus($state);
  921. }
  922. $this->setStatus($status);
  923. $history = $this->addStatusHistoryComment($comment, false); // no sense to set $status again
  924. $history->setIsCustomerNotified($isCustomerNotified); // for backwards compatibility
  925. }
  926. return $this;
  927. }
  928. /**
  929. * Whether specified state can be set from outside
  930. * @param $state
  931. * @return bool
  932. */
  933. public function isStateProtected($state)
  934. {
  935. if (empty($state)) {
  936. return false;
  937. }
  938. return self::STATE_COMPLETE == $state || self::STATE_CLOSED == $state;
  939. }
  940. /**
  941. * Retrieve label of order status
  942. *
  943. * @return string
  944. */
  945. public function getStatusLabel()
  946. {
  947. return $this->getConfig()->getStatusLabel($this->getStatus());
  948. }
  949. /**
  950. * Add status change information to history
  951. *
  952. * @param string $status
  953. * @param string $comment
  954. * @param bool $isCustomerNotified
  955. * @return Mage_Sales_Model_Order
  956. */
  957. public function addStatusToHistory($status, $comment = '', $isCustomerNotified = false)
  958. {
  959. $history = $this->addStatusHistoryComment($comment, $status)
  960. ->setIsCustomerNotified($isCustomerNotified);
  961. return $this;
  962. }
  963. /*
  964. * Add a comment to order
  965. * Different or default status may be specified
  966. *
  967. * @param string $comment
  968. * @param string $status
  969. * @return Mage_Sales_Order_Status_History
  970. */
  971. public function addStatusHistoryComment($comment, $status = false)
  972. {
  973. if (false === $status) {
  974. $status = $this->getStatus();
  975. } elseif (true === $status) {
  976. $status = $this->getConfig()->getStateDefaultStatus($this->getState());
  977. } else {
  978. $this->setStatus($status);
  979. }
  980. $history = Mage::getModel('Mage_Sales_Model_Order_Status_History')
  981. ->setStatus($status)
  982. ->setComment($comment)
  983. ->setEntityName($this->_historyEntityName);
  984. $this->addStatusHistory($history);
  985. return $history;
  986. }
  987. /**
  988. * Overrides entity id, which will be saved to comments history status
  989. *
  990. * @param string $status
  991. * @return Mage_Sales_Model_Order
  992. */
  993. public function setHistoryEntityName( $entityName )
  994. {
  995. $this->_historyEntityName = $entityName;
  996. return $this;
  997. }
  998. /**
  999. * Place order
  1000. *
  1001. * @return Mage_Sales_Model_Order
  1002. */
  1003. public function place()
  1004. {
  1005. Mage::dispatchEvent('sales_order_place_before', array('order'=>$this));
  1006. $this->_placePayment();
  1007. Mage::dispatchEvent('sales_order_place_after', array('order'=>$this));
  1008. return $this;
  1009. }
  1010. public function hold()
  1011. {
  1012. if (!$this->canHold()) {
  1013. Mage::throwException(Mage::helper('Mage_Sales_Helper_Data')->__('Hold action is not available.'));
  1014. }
  1015. $this->setHoldBeforeState($this->getState());
  1016. $this->setHoldBeforeStatus($this->getStatus());
  1017. $this->setState(self::STATE_HOLDED, true);
  1018. return $this;
  1019. }
  1020. /**
  1021. * Attempt to unhold the order
  1022. *
  1023. * @return Mage_Sales_Model_Order
  1024. * @throws Mage_Core_Exception
  1025. */
  1026. public function unhold()
  1027. {
  1028. if (!$this->canUnhold()) {
  1029. Mage::throwException(Mage::helper('Mage_Sales_Helper_Data')->__('Unhold action is not available.'));
  1030. }
  1031. $this->setState($this->getHoldBeforeState(), $this->getHoldBeforeStatus());
  1032. $this->setHoldBeforeState(null);
  1033. $this->setHoldBeforeStatus(null);
  1034. return $this;
  1035. }
  1036. /**
  1037. * Cancel order
  1038. *
  1039. * @return Mage_Sales_Model_Order
  1040. */
  1041. public function cancel()
  1042. {
  1043. if ($this->canCancel()) {
  1044. $this->getPayment()->cancel();
  1045. $this->registerCancellation();
  1046. Mage::dispatchEvent('order_cancel_after', array('order' => $this));
  1047. }
  1048. return $this;
  1049. }
  1050. /**
  1051. * Prepare order totals to cancellation
  1052. * @param string $comment
  1053. * @param bool $graceful
  1054. * @return Mage_Sales_Model_Order
  1055. * @throws Mage_Core_Exception
  1056. */
  1057. public function registerCancellation($comment = '', $graceful = true)
  1058. {
  1059. if ($this->canCancel()) {
  1060. $cancelState = self::STATE_CANCELED;
  1061. foreach ($this->getAllItems() as $item) {
  1062. if ($cancelState != self::STATE_PROCESSING && $item->getQtyToRefund()) {
  1063. if ($item->getQtyToShip() > $item->getQtyToCancel()) {
  1064. $cancelState = self::STATE_PROCESSING;
  1065. } else {
  1066. $cancelState = self::STATE_COMPLETE;
  1067. }
  1068. }
  1069. $item->cancel();
  1070. }
  1071. $this->setSubtotalCanceled($this->getSubtotal() - $this->getSubtotalInvoiced());
  1072. $this->setBaseSubtotalCanceled($this->getBaseSubtotal() - $this->getBaseSubtotalInvoiced());
  1073. $this->setTaxCanceled($this->getTaxAmount() - $this->getTaxInvoiced());
  1074. $this->setBaseTaxCanceled($this->getBaseTaxAmount() - $this->getBaseTaxInvoiced());
  1075. $this->setShippingCanceled($this->getShippingAmount() - $this->getShippingInvoiced());
  1076. $this->setBaseShippingCanceled($this->getBaseShippingAmount() - $this->getBaseShippingInvoiced());
  1077. $this->setDiscountCanceled(abs($this->getDiscountAmount()) - $this->getDiscountInvoiced());
  1078. $this->setBaseDiscountCanceled(abs($this->getBaseDiscountAmount()) - $this->getBaseDiscountInvoiced());
  1079. $this->setTotalCanceled($this->getGrandTotal() - $this->getTotalPaid());
  1080. $this->setBaseTotalCanceled($this->getBaseGrandTotal() - $this->getBaseTotalPaid());
  1081. $this->_setState($cancelState, true, $comment);
  1082. } elseif (!$graceful) {
  1083. Mage::throwException(Mage::helper('Mage_Sales_Helper_Data')->__('Order does not allow to be canceled.'));
  1084. }
  1085. return $this;
  1086. }
  1087. /**
  1088. * Retrieve tracking numbers
  1089. *
  1090. * @return array
  1091. */
  1092. public function getTrackingNumbers()
  1093. {
  1094. if ($this->getData('tracking_numbers')) {
  1095. return explode(',', $this->getData('tracking_numbers'));
  1096. }
  1097. return array();
  1098. }
  1099. /**
  1100. * Return model of shipping carrier
  1101. *
  1102. * @return bool|float|Mage_Shipping_Model_Carrier_Abstract
  1103. */
  1104. public function getShippingCarrier()
  1105. {
  1106. $carrierModel = $this->getData('shipping_carrier');
  1107. if (is_null($carrierModel)) {
  1108. $carrierModel = false;
  1109. /**
  1110. * $method - carrier_method
  1111. */
  1112. $method = $this->getShippingMethod(true);
  1113. if ($method instanceof Varien_Object) {
  1114. $className = Mage::getStoreConfig('carriers/' . $method->getCarrierCode() . '/model');
  1115. if ($className) {
  1116. $carrierModel = Mage::getModel($className);
  1117. }
  1118. }
  1119. $this->setData('shipping_carrier', $carrierModel);
  1120. }
  1121. return $carrierModel;
  1122. }
  1123. /**
  1124. * Retrieve shipping method
  1125. *
  1126. * @param bool $asObject return carrier code and shipping method data as object
  1127. * @return string|Varien_Object
  1128. */
  1129. public function getShippingMethod($asObject = false)
  1130. {
  1131. $shippingMethod = parent::getShippingMethod();
  1132. if (!$asObject) {
  1133. return $shippingMethod;
  1134. } else {
  1135. list($carrierCode, $method) = explode('_', $shippingMethod, 2);
  1136. return new Varien_Object(array(
  1137. 'carrier_code' => $carrierCode,
  1138. 'method' => $method
  1139. ));
  1140. }
  1141. }
  1142. /**
  1143. * Send email with order data
  1144. *
  1145. * @return Mage_Sales_Model_Order
  1146. */
  1147. public function sendNewOrderEmail()
  1148. {
  1149. $storeId = $this->getStore()->getId();
  1150. if (!Mage::helper('Mage_Sales_Helper_Data')->canSendNewOrderEmail($storeId)) {
  1151. return $this;
  1152. }
  1153. // Get the destination email addresses to send copies to
  1154. $copyTo = $this->_getEmails(self::XML_PATH_EMAIL_COPY_TO);
  1155. $copyMethod = Mage::getStoreConfig(self::XML_PATH_EMAIL_COPY_METHOD, $storeId);
  1156. $paymentBlockHtml = Mage::helper('Mage_Payment_Helper_Data')->getInfoBlockHtml($this->getPayment(), $storeId);
  1157. // Retrieve corresponding email template id and customer name
  1158. if ($this->getCustomerIsGuest()) {
  1159. $templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_GUEST_TEMPLATE, $storeId);
  1160. $customerName = $this->getBillingAddress()->getName();
  1161. } else {
  1162. $templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE, $storeId);
  1163. $customerName = $this->getCustomerName();
  1164. }
  1165. $mailer = Mage::getModel('Mage_Core_Model_Email_Template_Mailer');
  1166. $emailInfo = Mage::getModel('Mage_Core_Model_Email_Info');
  1167. $emailInfo->addTo($this->getCustomerEmail(), $customerName);
  1168. if ($copyTo && $copyMethod == 'bcc') {
  1169. // Add bcc to customer email
  1170. foreach ($copyTo as $email) {
  1171. $emailInfo->addBcc($email);
  1172. }
  1173. }
  1174. $mailer->addEmailInfo($emailInfo);
  1175. // Email copies are sent as separated emails if their copy method is 'copy'
  1176. if ($copyTo && $copyMethod == 'copy') {
  1177. foreach ($copyTo as $email) {
  1178. $emailInfo = Mage::getModel('Mage_Core_Model_Email_Info');
  1179. $emailInfo->addTo($email);
  1180. $mailer->addEmailInfo($emailInfo);
  1181. }
  1182. }
  1183. // Set all required params and send emails
  1184. $mailer->setSender(Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY, $storeId));
  1185. $mailer->setStoreId($storeId);
  1186. $mailer->setTemplateId($templateId);
  1187. $mailer->setTemplateParams(array(
  1188. 'order' => $this,
  1189. 'billing' => $this->getBillingAddress(),
  1190. 'payment_html' => $paymentBlockHtml
  1191. )
  1192. );
  1193. $mailer->send();
  1194. $this->setEmailSent(true);
  1195. $this->_getResource()->saveAttribute($this, 'email_sent');
  1196. return $this;
  1197. }
  1198. /**
  1199. * Send email with order update information
  1200. *
  1201. * @param boolean $notifyCustomer
  1202. * @param string $comment
  1203. * @return Mage_Sales_Model_Order
  1204. */
  1205. public function sendOrderUpdateEmail($notifyCustomer = true, $comment = '')
  1206. {
  1207. $storeId = $this->getStore()->getId();
  1208. if (!Mage::helper('Mage_Sales_Helper_Data')->canSendOrderCommentEmail($storeId)) {
  1209. return $this;
  1210. }
  1211. // Get the destination email addresses to send copies to
  1212. $copyTo = $this->_getEmails(self::XML_PATH_UPDATE_EMAIL_COPY_TO);
  1213. $copyMethod = Mage::getStoreConfig(self::XML_PATH_UPDATE_EMAIL_COPY_METHOD, $storeId);
  1214. // Check if at least one recepient is found
  1215. if (!$notifyCustomer && !$copyTo) {
  1216. return $this;
  1217. }
  1218. // Retrieve corresponding email template id and customer name
  1219. if ($this->getCustomerIsGuest()) {
  1220. $templateId = Mage::getStoreConfig(self::XML_PATH_UPDATE_EMAIL_GUEST_TEMPLATE, $storeId);
  1221. $customerName = $this->getBillingAddress()->getName();
  1222. } else {
  1223. $templateId = Mage::getStoreConfig(self::XML_PATH_UPDATE_EMAIL_TEMPLATE, $storeId);
  1224. $customerName = $this->getCustomerName();
  1225. }
  1226. $mailer = Mage::getModel('Mage_Core_Model_Email_Template_Mailer');
  1227. if ($notifyCustomer) {
  1228. $emailInfo = Mage::getModel('Mage_Core_Model_Email_Info');
  1229. $emailInfo->addTo($this->getCustomerEmail(), $customerName);
  1230. if ($copyTo && $copyMethod == 'bcc') {
  1231. // Add bcc to customer email
  1232. foreach ($copyTo as $email) {
  1233. $emailInfo->addBcc($email);
  1234. }
  1235. }
  1236. $mailer->addEmailInfo($emailInfo);
  1237. }
  1238. // Email copies are sent as separated emails if their copy method is
  1239. // 'copy' or a customer should not be notified
  1240. if ($copyTo && ($copyMethod == 'copy' || !$notifyCustomer)) {
  1241. foreach ($copyTo as $email) {
  1242. $emailInfo = Mage::getModel('Mage_Core_Model_Email_Info');
  1243. $emailInfo->addTo($email);
  1244. $mailer->addEmailInfo($emailInfo);
  1245. }
  1246. }
  1247. // Set all required params and send emails
  1248. $mailer->setSender(Mage::getStoreConfig(self::XML_PATH_UPDATE_EMAIL_IDENTITY, $storeId));
  1249. $mailer->setStoreId($storeId);
  1250. $mailer->setTemplateId($templateId);
  1251. $mailer->setTemplateParams(array(
  1252. 'order' => $this,
  1253. 'comment' => $comment,
  1254. 'billing' => $this->getBillingAddress()
  1255. )
  1256. );
  1257. $mailer->send();
  1258. return $this;
  1259. }
  1260. protected function _getEmails($configPath)
  1261. {
  1262. $data = Mage::getStoreConfig($configPath, $this->getStoreId());
  1263. if (!empty($data)) {
  1264. return explode(',', $data);
  1265. }
  1266. return false;
  1267. }
  1268. /*********************** ADDRESSES ***************************/
  1269. /**
  1270. * @return Mage_Sales_Model_Resource_Order_Address_Collection
  1271. */
  1272. public function getAddressesCollection()
  1273. {
  1274. if (is_null($this->_addresses)) {
  1275. $this->_addresses = Mage::getResourceModel('Mage_Sales_Model_Resource_Order_Address_Collection')
  1276. ->setOrderFilter($this);
  1277. if ($this->getId()) {
  1278. foreach ($this->_addresses as $address) {
  1279. $address->setOrder($this);
  1280. }
  1281. }
  1282. }
  1283. return $this->_addresses;
  1284. }
  1285. public function getAddressById($addressId)
  1286. {
  1287. foreach ($this->getAddressesCollection() as $address) {
  1288. if ($address->getId()==$addressId) {
  1289. return $address;
  1290. }
  1291. }
  1292. return false;
  1293. }
  1294. public function addAddress(Mage_Sales_Model_Order_Address $address)
  1295. {
  1296. $address->setOrder($this)->setParentId($this->getId());
  1297. if (!$address->getId()) {
  1298. $this->getAddressesCollection()->addItem($address);
  1299. }
  1300. return $this;
  1301. }
  1302. /**
  1303. * @param array $filterByTypes
  1304. * @param bool $nonChildrenOnly
  1305. * @return Mage_Sales_Model_Resource_Order_Item_Collection
  1306. */
  1307. public function getItemsCollection($filterByTypes = array(), $nonChildrenOnly = false)
  1308. {
  1309. if (is_null($this->_items)) {
  1310. $this->_items = Mage::getResourceModel('Mage_Sales_Model_Resource_Order_Item_Collection')
  1311. ->setOrderFilter($this);
  1312. if ($filterByTypes) {
  1313. $this->_items->filterByTypes($filterByTypes);
  1314. }
  1315. if ($nonChildrenOnly) {
  1316. $this->_items->filterByParent();
  1317. }
  1318. if ($this->getId()) {
  1319. foreach ($this->_items as $item) {
  1320. $item->setOrder($this);
  1321. }
  1322. }
  1323. }
  1324. return $this->_items;
  1325. }
  1326. /**
  1327. * Get random items collection with related children
  1328. *
  1329. * @param int $limit
  1330. * @return Mage_Sales_Model_Resource_Order_Item_Collection
  1331. */
  1332. public function getItemsRandomCollection($limit = 1)
  1333. {
  1334. return $this->_getItemsRandomCollection($limit);
  1335. }
  1336. /**
  1337. * Get random items collection without related children
  1338. *
  1339. * @param int $limit
  1340. * @return Mageā€¦

Large files files are truncated, but you can click here to view the full file