Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Creditmemo.php
Go to the documentation of this file.
1 <?php
8 
15 use Magento\Sales\Model\Order\InvoiceFactory;
16 
32 {
33  const STATE_OPEN = 1;
34 
35  const STATE_REFUNDED = 2;
36 
37  const STATE_CANCELED = 3;
38 
39  const REPORT_DATE_TYPE_ORDER_CREATED = 'order_created';
40 
41  const REPORT_DATE_TYPE_REFUND_CREATED = 'refund_created';
42 
48  protected $entityType = 'creditmemo';
49 
53  protected static $_states;
54 
58  protected $_order;
59 
65  protected $_calculators = [];
66 
70  protected $_eventPrefix = 'sales_order_creditmemo';
71 
75  protected $_eventObject = 'creditmemo';
76 
80  protected $_creditmemoConfig;
81 
85  protected $_orderFactory;
86 
91 
96 
100  protected $_storeManager;
101 
105  protected $_commentFactory;
106 
111 
115  protected $priceCurrency;
116 
120  private $invoiceFactory;
121 
141  public function __construct(
142  \Magento\Framework\Model\Context $context,
143  \Magento\Framework\Registry $registry,
144  \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
146  \Magento\Sales\Model\Order\Creditmemo\Config $creditmemoConfig,
147  \Magento\Sales\Model\OrderFactory $orderFactory,
148  \Magento\Sales\Model\ResourceModel\Order\Creditmemo\Item\CollectionFactory $cmItemCollectionFactory,
149  \Magento\Framework\Math\CalculatorFactory $calculatorFactory,
151  \Magento\Sales\Model\Order\Creditmemo\CommentFactory $commentFactory,
152  \Magento\Sales\Model\ResourceModel\Order\Creditmemo\Comment\CollectionFactory $commentCollectionFactory,
154  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
155  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
156  array $data = [],
157  InvoiceFactory $invoiceFactory = null
158  ) {
159  $this->_creditmemoConfig = $creditmemoConfig;
160  $this->_orderFactory = $orderFactory;
161  $this->_cmItemCollectionFactory = $cmItemCollectionFactory;
162  $this->_calculatorFactory = $calculatorFactory;
163  $this->_storeManager = $storeManager;
164  $this->_commentFactory = $commentFactory;
165  $this->_commentCollectionFactory = $commentCollectionFactory;
166  $this->priceCurrency = $priceCurrency;
167  $this->invoiceFactory = $invoiceFactory ?: ObjectManager::getInstance()->get(InvoiceFactory::class);
168  parent::__construct(
169  $context,
170  $registry,
171  $extensionFactory,
173  $resource,
174  $resourceCollection,
175  $data
176  );
177  }
178 
184  protected function _construct()
185  {
186  $this->_init(\Magento\Sales\Model\ResourceModel\Order\Creditmemo::class);
187  }
188 
194  public function getConfig()
195  {
197  }
198 
204  public function getStore()
205  {
206  return $this->getOrder()->getStore();
207  }
208 
215  public function setOrder(\Magento\Sales\Model\Order $order)
216  {
217  $this->_order = $order;
218  $this->setOrderId($order->getId())->setStoreId($order->getStoreId());
219  return $this;
220  }
221 
227  public function getOrder()
228  {
229  if (!$this->_order instanceof \Magento\Sales\Model\Order) {
230  $this->_order = $this->_orderFactory->create()->load($this->getOrderId());
231  }
232  return $this->_order->setHistoryEntityName($this->entityType);
233  }
234 
240  public function getEntityType()
241  {
242  return $this->entityType;
243  }
244 
250  public function getBillingAddress()
251  {
252  return $this->getOrder()->getBillingAddress();
253  }
254 
260  public function getShippingAddress()
261  {
262  return $this->getOrder()->getShippingAddress();
263  }
264 
268  public function getItemsCollection()
269  {
270  $collection = $this->_cmItemCollectionFactory->create()->setCreditmemoFilter($this->getId());
271 
272  if ($this->getId()) {
273  foreach ($collection as $item) {
274  $item->setCreditmemo($this);
275  }
276  }
277  return $collection;
278  }
279 
283  public function getAllItems()
284  {
285  $items = [];
286  foreach ($this->getItems() as $item) {
287  if (!$item->isDeleted()) {
288  $items[] = $item;
289  }
290  }
291  return $items;
292  }
293 
298  public function getItemById($itemId)
299  {
300  foreach ($this->getItemsCollection() as $item) {
301  if ($item->getId() == $itemId) {
302  return $item;
303  }
304  }
305  return false;
306  }
307 
314  public function getItemByOrderId($orderId)
315  {
316  foreach ($this->getItemsCollection() as $item) {
317  if ($item->getOrderItemId() == $orderId) {
318  return $item;
319  }
320  }
321  return false;
322  }
323 
328  public function addItem(\Magento\Sales\Model\Order\Creditmemo\Item $item)
329  {
330  $item->setCreditmemo($this)->setParentId($this->getId())->setStoreId($this->getStoreId());
331  if (!$item->getId()) {
332  $this->setItems(array_merge($this->getItems(), [$item]));
333  }
334  return $this;
335  }
336 
342  public function collectTotals()
343  {
344  foreach ($this->getConfig()->getTotalModels() as $model) {
345  $model->collect($this);
346  }
347  return $this;
348  }
349 
358  public function roundPrice($price, $type = 'regular', $negative = false)
359  {
360  if ($price) {
361  if (!isset($this->_calculators[$type])) {
362  $this->_calculators[$type] = $this->_calculatorFactory->create(['scope' => $this->getStore()]);
363  }
364  $price = $this->_calculators[$type]->deltaRound($price, $negative);
365  }
366  return $price;
367  }
368 
372  public function canRefund()
373  {
374  if ($this->getState() != self::STATE_CANCELED &&
375  $this->getState() != self::STATE_REFUNDED &&
376  $this->getOrder()->getPayment()->canRefund()
377  ) {
378  return true;
379  }
380  return false;
381  }
382 
388  public function getInvoice()
389  {
390  if (!$this->getData('invoice') instanceof \Magento\Sales\Api\Data\InvoiceInterface && $this->getInvoiceId()) {
391  $this->setInvoice($this->invoiceFactory->create()->load($this->getInvoiceId()));
392  }
393  return $this->getData('invoice');
394  }
395 
402  public function setInvoice(Invoice $invoice)
403  {
404  $this->setData('invoice', $invoice);
405  return $this;
406  }
407 
413  public function canCancel()
414  {
415  return $this->getState() == self::STATE_OPEN;
416  }
417 
423  public function canVoid()
424  {
425  return false;
426  }
427 
433  public static function getStates()
434  {
435  if (static::$_states === null) {
436  static::$_states = [
437  self::STATE_OPEN => __('Pending'),
438  self::STATE_REFUNDED => __('Refunded'),
439  self::STATE_CANCELED => __('Canceled'),
440  ];
441  }
442  return static::$_states;
443  }
444 
451  public function getStateName($stateId = null)
452  {
453  if ($stateId === null) {
454  $stateId = $this->getState();
455  }
456 
457  if (static::$_states === null) {
458  static::getStates();
459  }
460  if (isset(static::$_states[$stateId])) {
461  return static::$_states[$stateId];
462  }
463  return __('Unknown State');
464  }
465 
470  public function setShippingAmount($amount)
471  {
473  }
474 
480  {
481  $amount = trim($amount);
482  if (substr($amount, -1) == '%') {
483  $amount = (double)substr($amount, 0, -1);
484  $amount = $this->getOrder()->getGrandTotal() * $amount / 100;
485  }
486 
487  $amount = $this->priceCurrency->round($amount);
488  $this->setData('base_adjustment_positive', $amount);
489 
490  $amount = $this->priceCurrency->round($amount * $this->getOrder()->getBaseToOrderRate());
491  $this->setData('adjustment_positive', $amount);
492  return $this;
493  }
494 
500  {
501  $amount = trim($amount);
502  if (substr($amount, -1) == '%') {
503  $amount = (double)substr($amount, 0, -1);
504  $amount = $this->getOrder()->getGrandTotal() * $amount / 100;
505  }
506 
507  $amount = $this->priceCurrency->round($amount);
508  $this->setData('base_adjustment_negative', $amount);
509 
510  $amount = $this->priceCurrency->round($amount * $this->getOrder()->getBaseToOrderRate());
511  $this->setData('adjustment_negative', $amount);
512  return $this;
513  }
514 
520  public function isLast()
521  {
522  $items = $this->getAllItems();
523  foreach ($items as $item) {
524  if (!$item->isLast()) {
525  return false;
526  }
527  }
528 
529  if (empty($items)) {
530  $order = $this->getOrder();
531  if ($order) {
532  foreach ($order->getItems() as $orderItem) {
533  if ($orderItem->canRefund()) {
534  return false;
535  }
536  }
537  }
538  }
539 
540  return true;
541  }
542 
553  public function addComment($comment, $notify = false, $visibleOnFront = false)
554  {
555  if (!$comment instanceof \Magento\Sales\Model\Order\Creditmemo\Comment) {
556  $comment = $this->_commentFactory->create()->setComment(
557  $comment
558  )->setIsCustomerNotified(
559  $notify
560  )->setIsVisibleOnFront(
561  $visibleOnFront
562  );
563  }
564  $comment->setCreditmemo($this)->setParentId($this->getId())->setStoreId($this->getStoreId());
565  $this->setComments(array_merge($this->getComments(), [$comment]));
566  return $comment;
567  }
568 
574  public function getCommentsCollection($reload = false)
575  {
576  $collection = $this->_commentCollectionFactory->create()->setCreditmemoFilter($this->getId())
577  ->setCreatedAtOrder();
578 
579  if ($this->getId()) {
580  foreach ($collection as $comment) {
581  $comment->setCreditmemo($this);
582  }
583  }
584  return $collection;
585  }
586 
593  public function getFilteredCollectionItems($filter = null)
594  {
595  return $this->getResourceCollection()->getFiltered($filter);
596  }
597 
603  public function getIncrementId()
604  {
605  return $this->getData('increment_id');
606  }
607 
611  public function isValidGrandTotal()
612  {
613  return !($this->getGrandTotal() <= 0 && !$this->getAllowZeroGrandTotal());
614  }
615 
621  public function getItems()
622  {
623  if ($this->getData(CreditmemoInterface::ITEMS) == null) {
624  $this->setData(
626  $this->getItemsCollection()->getItems()
627  );
628  }
629  return $this->getData(CreditmemoInterface::ITEMS);
630  }
631 
637  public function getComments()
638  {
639  if ($this->getData(CreditmemoInterface::COMMENTS) == null) {
640  $this->setData(
642  $this->getCommentsCollection()->getItems()
643  );
644  }
645  return $this->getData(CreditmemoInterface::COMMENTS);
646  }
647 
648  //@codeCoverageIgnoreStart
649 
655  public function getDiscountDescription()
656  {
658  }
659 
663  public function setItems($items)
664  {
665  return $this->setData(CreditmemoInterface::ITEMS, $items);
666  }
667 
673  public function getAdjustment()
674  {
676  }
677 
683  public function getAdjustmentNegative()
684  {
686  }
687 
693  public function getAdjustmentPositive()
694  {
696  }
697 
703  public function getBaseAdjustment()
704  {
706  }
707 
713  public function getBaseAdjustmentNegative()
714  {
716  }
717 
724  public function setBaseAdjustmentNegative($baseAdjustmentNegative)
725  {
726  return $this->setData(CreditmemoInterface::BASE_ADJUSTMENT_NEGATIVE, $baseAdjustmentNegative);
727  }
728 
734  public function getBaseAdjustmentPositive()
735  {
737  }
738 
745  public function setBaseAdjustmentPositive($baseAdjustmentPositive)
746  {
747  return $this->setData(CreditmemoInterface::BASE_ADJUSTMENT_POSITIVE, $baseAdjustmentPositive);
748  }
749 
755  public function getBaseCurrencyCode()
756  {
758  }
759 
765  public function getBaseDiscountAmount()
766  {
768  }
769 
775  public function getBaseGrandTotal()
776  {
778  }
779 
786  {
788  }
789 
795  public function getBaseShippingAmount()
796  {
798  }
799 
806  {
808  }
809 
815  public function getBaseShippingInclTax()
816  {
818  }
819 
825  public function getBaseShippingTaxAmount()
826  {
828  }
829 
835  public function getBaseSubtotal()
836  {
838  }
839 
845  public function getBaseSubtotalInclTax()
846  {
848  }
849 
855  public function getBaseTaxAmount()
856  {
858  }
859 
865  public function getBaseToGlobalRate()
866  {
868  }
869 
875  public function getBaseToOrderRate()
876  {
878  }
879 
885  public function getBillingAddressId()
886  {
888  }
889 
895  public function getCreatedAt()
896  {
898  }
899 
903  public function setCreatedAt($createdAt)
904  {
905  return $this->setData(CreditmemoInterface::CREATED_AT, $createdAt);
906  }
907 
913  public function getCreditmemoStatus()
914  {
916  }
917 
923  public function getDiscountAmount()
924  {
926  }
927 
933  public function getEmailSent()
934  {
936  }
937 
943  public function getGlobalCurrencyCode()
944  {
946  }
947 
953  public function getGrandTotal()
954  {
956  }
957 
964  {
966  }
967 
973  public function getInvoiceId()
974  {
976  }
977 
983  public function getOrderCurrencyCode()
984  {
986  }
987 
993  public function getOrderId()
994  {
995  return $this->getData(CreditmemoInterface::ORDER_ID);
996  }
997 
1003  public function getShippingAddressId()
1004  {
1006  }
1007 
1013  public function getShippingAmount()
1014  {
1016  }
1017 
1024  {
1026  }
1027 
1033  public function getShippingInclTax()
1034  {
1036  }
1037 
1043  public function getShippingTaxAmount()
1044  {
1046  }
1047 
1053  public function getState()
1054  {
1055  return $this->getData(CreditmemoInterface::STATE);
1056  }
1057 
1063  public function getStoreCurrencyCode()
1064  {
1066  }
1067 
1073  public function getStoreId()
1074  {
1075  return $this->getData(CreditmemoInterface::STORE_ID);
1076  }
1077 
1083  public function getStoreToBaseRate()
1084  {
1086  }
1087 
1093  public function getStoreToOrderRate()
1094  {
1096  }
1097 
1103  public function getSubtotal()
1104  {
1105  return $this->getData(CreditmemoInterface::SUBTOTAL);
1106  }
1107 
1113  public function getSubtotalInclTax()
1114  {
1116  }
1117 
1123  public function getTaxAmount()
1124  {
1126  }
1127 
1133  public function getTransactionId()
1134  {
1136  }
1137 
1144  public function setTransactionId($transactionId)
1145  {
1146  return $this->setData(CreditmemoInterface::TRANSACTION_ID, $transactionId);
1147  }
1148 
1154  public function getUpdatedAt()
1155  {
1157  }
1158 
1162  public function setComments($comments)
1163  {
1165  }
1166 
1170  public function setStoreId($id)
1171  {
1172  return $this->setData(CreditmemoInterface::STORE_ID, $id);
1173  }
1174 
1179  {
1181  }
1182 
1186  public function setStoreToOrderRate($rate)
1187  {
1189  }
1190 
1195  {
1197  }
1198 
1202  public function setBaseToOrderRate($rate)
1203  {
1205  }
1206 
1210  public function setGrandTotal($amount)
1211  {
1213  }
1214 
1219  {
1221  }
1222 
1226  public function setSubtotalInclTax($amount)
1227  {
1229  }
1230 
1235  {
1237  }
1238 
1242  public function setStoreToBaseRate($rate)
1243  {
1245  }
1246 
1250  public function setBaseToGlobalRate($rate)
1251  {
1253  }
1254 
1258  public function setBaseAdjustment($baseAdjustment)
1259  {
1260  return $this->setData(CreditmemoInterface::BASE_ADJUSTMENT, $baseAdjustment);
1261  }
1262 
1266  public function setBaseSubtotal($amount)
1267  {
1269  }
1270 
1274  public function setDiscountAmount($amount)
1275  {
1277  }
1278 
1282  public function setSubtotal($amount)
1283  {
1285  }
1286 
1290  public function setAdjustment($adjustment)
1291  {
1292  return $this->setData(CreditmemoInterface::ADJUSTMENT, $adjustment);
1293  }
1294 
1298  public function setBaseGrandTotal($amount)
1299  {
1301  }
1302 
1306  public function setBaseTaxAmount($amount)
1307  {
1309  }
1310 
1315  {
1317  }
1318 
1322  public function setTaxAmount($amount)
1323  {
1325  }
1326 
1330  public function setOrderId($id)
1331  {
1332  return $this->setData(CreditmemoInterface::ORDER_ID, $id);
1333  }
1334 
1338  public function setEmailSent($emailSent)
1339  {
1340  return $this->setData(CreditmemoInterface::EMAIL_SENT, $emailSent);
1341  }
1342 
1346  public function setCreditmemoStatus($creditmemoStatus)
1347  {
1348  return $this->setData(CreditmemoInterface::CREDITMEMO_STATUS, $creditmemoStatus);
1349  }
1350 
1354  public function setState($state)
1355  {
1356  return $this->setData(CreditmemoInterface::STATE, $state);
1357  }
1358 
1362  public function setShippingAddressId($id)
1363  {
1365  }
1366 
1370  public function setBillingAddressId($id)
1371  {
1373  }
1374 
1378  public function setInvoiceId($id)
1379  {
1381  }
1382 
1386  public function setStoreCurrencyCode($code)
1387  {
1389  }
1390 
1394  public function setOrderCurrencyCode($code)
1395  {
1397  }
1398 
1402  public function setBaseCurrencyCode($code)
1403  {
1405  }
1406 
1410  public function setGlobalCurrencyCode($code)
1411  {
1413  }
1414 
1418  public function setIncrementId($id)
1419  {
1421  }
1422 
1426  public function setUpdatedAt($timestamp)
1427  {
1428  return $this->setData(CreditmemoInterface::UPDATED_AT, $timestamp);
1429  }
1430 
1435  {
1437  }
1438 
1443  {
1445  }
1446 
1451  {
1453  }
1454 
1459  {
1461  }
1462 
1466  public function setShippingInclTax($amount)
1467  {
1469  }
1470 
1475  {
1477  }
1478 
1483  {
1485  }
1486 
1492  public function getExtensionAttributes()
1493  {
1494  return $this->_getExtensionAttributes();
1495  }
1496 
1503  public function setExtensionAttributes(\Magento\Sales\Api\Data\CreditmemoExtensionInterface $extensionAttributes)
1504  {
1505  return $this->_setExtensionAttributes($extensionAttributes);
1506  }
1507  //@codeCoverageIgnoreEnd
1508 }
setBaseAdjustmentPositive($baseAdjustmentPositive)
Definition: Creditmemo.php:745
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory, AttributeValueFactory $customAttributeFactory, \Magento\Sales\Model\Order\Creditmemo\Config $creditmemoConfig, \Magento\Sales\Model\OrderFactory $orderFactory, \Magento\Sales\Model\ResourceModel\Order\Creditmemo\Item\CollectionFactory $cmItemCollectionFactory, \Magento\Framework\Math\CalculatorFactory $calculatorFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Sales\Model\Order\Creditmemo\CommentFactory $commentFactory, \Magento\Sales\Model\ResourceModel\Order\Creditmemo\Comment\CollectionFactory $commentCollectionFactory, PriceCurrencyInterface $priceCurrency, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[], InvoiceFactory $invoiceFactory=null)
Definition: Creditmemo.php:141
$orderItem
Definition: order.php:30
$id
Definition: fieldset.phtml:14
_setExtensionAttributes(\Magento\Framework\Api\ExtensionAttributesInterface $extensionAttributes)
setOrder(\Magento\Sales\Model\Order $order)
Definition: Creditmemo.php:215
$order
Definition: order.php:55
$storeManager
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
$price
addItem(\Magento\Sales\Model\Order\Creditmemo\Item $item)
Definition: Creditmemo.php:328
$amount
Definition: order.php:14
$type
Definition: item.phtml:13
setCreditmemoStatus($creditmemoStatus)
$invoice
setBaseAdjustmentNegative($baseAdjustmentNegative)
Definition: Creditmemo.php:724
addComment($comment, $notify=false, $visibleOnFront=false)
Definition: Creditmemo.php:553
roundPrice($price, $type='regular', $negative=false)
Definition: Creditmemo.php:358
setExtensionAttributes(\Magento\Sales\Api\Data\CreditmemoExtensionInterface $extensionAttributes)
$code
Definition: info.phtml:12
$items