142 \
Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
145 \
Magento\Sales\Model\OrderFactory $orderFactory,
146 \
Magento\Framework\Math\CalculatorFactory $calculatorFactory,
154 $this->_invoiceConfig = $invoiceConfig;
155 $this->_orderFactory = $orderFactory;
156 $this->_calculatorFactory = $calculatorFactory;
157 $this->_invoiceItemCollectionFactory = $invoiceItemCollectionFactory;
158 $this->_invoiceCommentFactory = $invoiceCommentFactory;
159 $this->_commentCollectionFactory = $commentCollectionFactory;
189 $ids = $this->
getCollection()->addAttributeToFilter(
'increment_id', $incrementId)->getAllIds();
193 $this->
load(current($ids));
215 return $this->
getOrder()->getStore();
238 if (!$this->_order instanceof \
Magento\Sales\Model\
Order) {
239 $this->_order = $this->_orderFactory->create()->load($this->
getOrderId());
241 return $this->_order->setHistoryEntityName($this->entityType);
261 return $this->
getOrder()->getBillingAddress();
271 return $this->
getOrder()->getShippingAddress();
291 return $this->
getState() != self::STATE_CANCELED &&
292 $this->
getState() != self::STATE_PAID &&
293 $this->
getOrder()->getPayment()->canCapture();
303 if ($this->
getState() == self::STATE_PAID) {
305 return (
bool)$this->
getOrder()->getPayment()->canVoid();
328 if ($this->
getState() != self::STATE_PAID) {
344 $this->
getOrder()->getPayment()->capture($this);
345 if ($this->getIsPaid()) {
358 if ($this->_wasPayCalled) {
361 $this->_wasPayCalled =
true;
366 $order->getPayment()->pay($this);
369 $invoiceList =
$order->getInvoiceCollection();
371 if (count($invoiceList->getItems()) > 1) {
372 $totalPaid +=
$order->getTotalPaid();
373 $baseTotalPaid +=
$order->getBaseTotalPaid();
375 $order->setTotalPaid($totalPaid);
376 $order->setBaseTotalPaid($baseTotalPaid);
377 $this->_eventManager->dispatch(
'sales_order_invoice_pay', [$this->_eventObject => $this]);
398 $this->
getOrder()->getPayment()->void($this);
414 $order->getPayment()->cancelInvoice($this);
431 $order->setDiscountTaxCompensationInvoiced(
434 $order->setBaseDiscountTaxCompensationInvoiced(
446 $order->setBaseTotalInvoicedCost(
$order->getBaseTotalInvoicedCost() - $this->getBaseCost());
452 $this->
setState(self::STATE_CANCELED);
455 $this->_eventManager->dispatch(
'sales_order_invoice_cancel', [$this->_eventObject => $this]);
483 if (!isset($this->_rounders[
$type])) {
484 $this->_rounders[
$type] = $this->_calculatorFactory->create([
'scope' => $this->
getStore()]);
499 $this->
setItems($this->_invoiceItemCollectionFactory->create()->setInvoiceFilter($this->
getId()));
501 if ($this->
getId()) {
503 $item->setInvoice($this);
517 if (!
$item->isDeleted()) {
531 if (
$item->getId() == $itemId) {
546 if (!$item->getId()) {
559 if (
null === static::$_states) {
561 self::STATE_OPEN =>
__(
'Pending'),
562 self::STATE_PAID =>
__(
'Paid'),
563 self::STATE_CANCELED =>
__(
'Canceled'),
566 return static::$_states;
577 if ($stateId ===
null) {
581 if (
null === static::$_states) {
584 if (isset(static::$_states[$stateId])) {
585 return static::$_states[$stateId];
587 return __(
'Unknown State');
599 public function register()
601 if ($this->
getId()) {
602 throw new \Magento\Framework\Exception\LocalizedException(
__(
'We cannot register an existing invoice'));
606 if (
$item->getQty() > 0) {
609 $item->isDeleted(
true);
614 $captureCase = $this->getRequestedCaptureCase();
617 if ($captureCase == self::CAPTURE_ONLINE) {
619 }
elseif ($captureCase == self::CAPTURE_OFFLINE) {
625 if (!
$order->getPayment()->getIsTransactionPending()) {
640 $order->setDiscountTaxCompensationInvoiced(
643 $order->setBaseDiscountTaxCompensationInvoiced(
655 $order->setBaseTotalInvoicedCost(
$order->getBaseTotalInvoicedCost() + $this->getBaseCost());
658 if (
null === $state) {
662 $this->_eventManager->dispatch(
663 'sales_order_invoice_register',
664 [$this->_eventObject => $this,
'order' =>
$order]
677 if (!
$item->isLast()) {
693 public function addComment($comment, $notify =
false, $visibleOnFront =
false)
696 $comment = $this->_invoiceCommentFactory->create()->setComment(
698 )->setIsCustomerNotified(
700 )->setIsVisibleOnFront(
704 $comment->setInvoice($this)->setStoreId($this->
getStoreId())->setParentId($this->
getId());
705 if (!$comment->getId()) {
708 $this->_hasDataChanges =
true;
719 $comments = $this->_commentCollectionFactory->create()->setInvoiceFilter($this->
getId())
720 ->setCreatedAtOrder();
729 if ($this->
getId()) {
731 $comment->setInvoice($this);
746 $this->_origData =
null;
749 $this->_order =
null;
750 $this->_saveBeforeDestruct =
false;
751 $this->_wasPayCalled =
false;
762 return parent::_beforeSave();
772 return parent::_afterSave();
783 $collection = $this->_invoiceItemCollectionFactory->create()->setInvoiceFilter($this->
getId());
785 $item->setInvoice($this);
800 $collection = $this->_commentCollectionFactory->create()->setInvoiceFilter($this->
getId());
802 $comment->setInvoice($this);
818 return $this->
getData(
'increment_id');
setBaseDiscountAmount($amount)
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory, AttributeValueFactory $customAttributeFactory, \Magento\Sales\Model\Order\Invoice\Config $invoiceConfig, \Magento\Sales\Model\OrderFactory $orderFactory, \Magento\Framework\Math\CalculatorFactory $calculatorFactory, \Magento\Sales\Model\ResourceModel\Order\Invoice\Item\CollectionFactory $invoiceItemCollectionFactory, \Magento\Sales\Model\Order\Invoice\CommentFactory $invoiceCommentFactory, \Magento\Sales\Model\ResourceModel\Order\Invoice\Comment\CollectionFactory $commentCollectionFactory, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
const STORE_CURRENCY_CODE
setBaseShippingInclTax($amount)
setBaseSubtotalInclTax($amount)
setShippingTaxAmount($amount)
_getExtensionAttributes()
const BASE_SHIPPING_TAX_AMOUNT
setExtensionAttributes(\Magento\Sales\Api\Data\InvoiceExtensionInterface $extensionAttributes)
elseif(isset( $params[ 'redirect_parent']))
setDiscountDescription($description)
const BASE_DISCOUNT_TAX_COMPENSATION_AMOUNT
getBaseShippingTaxAmount()
setDiscountAmount($amount)
const BASE_SUBTOTAL_INCL_TAX
getDiscountTaxCompensationAmount()
setGlobalCurrencyCode($code)
const BASE_SHIPPING_AMOUNT
const ORDER_CURRENCY_CODE
loadByIncrementId($incrementId)
setBaseGrandTotal($amount)
const DISCOUNT_TAX_COMPENSATION_AMOUNT
const DISCOUNT_DESCRIPTION
_setExtensionAttributes(\Magento\Framework\Api\ExtensionAttributesInterface $extensionAttributes)
addComment($comment, $notify=false, $visibleOnFront=false)
load($modelId, $field=null)
setShippingAmount($amount)
setOrderCurrencyCode($code)
addItem(\Magento\Sales\Model\Order\Invoice\Item $item)
setBaseDiscountTaxCompensationAmount($amount)
setBaseShippingDiscountTaxCompensationAmnt($amnt)
getBaseShippingDiscountTaxCompensationAmnt()
setDiscountTaxCompensationAmount($amount)
setBaseTotalRefunded($amount)
setStoreCurrencyCode($code)
const SHIPPING_ADDRESS_ID
const SHIPPING_DISCOUNT_TAX_COMPENSATION_AMOUNT
setBaseShippingTaxAmount($amount)
setStoreToBaseRate($rate)
const GLOBAL_CURRENCY_CODE
getBaseDiscountTaxCompensationAmount()
setCanVoidFlag($canVoidFlag)
setBaseToOrderRate($rate)
$_invoiceItemCollectionFactory
$_commentCollectionFactory
const BASE_TOTAL_REFUNDED
setShippingDiscountTaxCompensationAmount($amount)
setBaseTaxAmount($amount)
setBaseToGlobalRate($rate)
getStateName($stateId=null)
setSubtotalInclTax($amount)
const REPORT_DATE_TYPE_INVOICE_CREATED
const SHIPPING_TAX_AMOUNT
const BASE_DISCOUNT_AMOUNT
roundPrice($price, $type='regular', $negative=false)
getCommentsCollection($reload=false)
const BASE_SHIPPING_DISCOUNT_TAX_COMPENSATION_AMNT
setBaseShippingAmount($amount)
getData($key='', $index=null)
const STORE_TO_ORDER_RATE
setData($key, $value=null)
setOrder(\Magento\Sales\Model\Order $order)
setTransactionId($transactionId)
setBaseCurrencyCode($code)
const REPORT_DATE_TYPE_ORDER_CREATED
getShippingDiscountTaxCompensationAmount()
setStoreToOrderRate($rate)
const BASE_TO_GLOBAL_RATE
setIsUsedForRefund($isUsedForRefund)
setShippingAddressId($id)
setShippingInclTax($amount)
const BASE_SHIPPING_INCL_TAX