public function testSyncRemove() { $this->eventObserverMock->expects($this->once())->method('getDataObject')->willReturn($this->salesModelMock); $this->salesModelMock->expects($this->once())->method('getId')->willReturn('sales-id-value'); $this->gridAggregatorMock->expects($this->once())->method('purge')->with('sales-id-value'); $this->unit->execute($this->eventObserverMock); }
public function testProcessRelations() { $this->relationProcessorMock->expects($this->once())->method('processRelation')->with($this->salesModelMock); $this->salesModelMock->expects($this->once())->method('getEventPrefix')->willReturn('sales_event_prefix'); $this->eventManagerMock->expects($this->once())->method('dispatch')->with('sales_event_prefix_process_relation', ['object' => $this->salesModelMock]); $this->entityRelationComposite->processRelations($this->salesModelMock); }
public function testSyncInsert() { $this->eventObserverMock->expects($this->once())->method('getObject')->willReturn($this->salesModelMock); $this->salesModelMock->expects($this->once())->method('getId')->willReturn('sales-id-value'); $this->scopeConfigurationMock->expects($this->once())->method('getValue')->with('dev/grid/async_indexing', 'default', null)->willReturn(false); $this->gridAggregatorMock->expects($this->once())->method('refresh')->with('sales-id-value'); $this->unit->execute($this->eventObserverMock); }
/** * @expectedException \Exception * @expectedExceptionMessage Expected Exception * @throws \Exception */ public function testSaveFailed() { $this->modelMock->expects($this->any())->method('getEventPrefix')->will($this->returnValue('event_prefix')); $this->modelMock->expects($this->any())->method('getEventObject')->will($this->returnValue('event_object')); $this->appResourceMock->expects($this->once())->method('getConnection')->will($this->returnValue($this->connectionMock)); $exception = new \Exception('Expected Exception'); $this->modelMock->expects($this->any())->method('getId')->will($this->throwException($exception)); $this->connectionMock->expects($this->once())->method('beginTransaction'); $this->connectionMock->expects($this->once())->method('rollback'); $this->eventManagerMock->expects($this->once())->method('dispatch')->with('event_prefix_save_attribute_before', ['event_object' => $this->attribute, 'object' => $this->modelMock, 'attribute' => ['attribute']]); $this->attribute->saveAttribute($this->modelMock, 'attribute'); }
/** * {@inheritdoc} */ public function build($type) { if ($this->isPaymentExists() && $this->transactionId !== null) { $transaction = $this->transactionRepository->getByTransactionId($this->transactionId, $this->payment->getId(), $this->order->getId()); if (!$transaction) { $transaction = $this->transactionRepository->create()->setTxnId($this->transactionId); } $transaction->setPaymentId($this->payment->getId())->setPayment($this->payment)->setOrderId($this->order->getId())->setOrder($this->order)->setTxnType($type)->isFailsafe($this->failSafe); if ($this->payment->hasIsTransactionClosed()) { $transaction->setIsClosed((int) $this->payment->getIsTransactionClosed()); } if ($this->transactionAdditionalInfo) { foreach ($this->transactionAdditionalInfo as $key => $value) { $transaction->setAdditionalInformation($key, $value); } } $this->transactionAdditionalInfo = []; $this->payment->setLastTransId($transaction->getTxnId()); $this->payment->setCreatedTransaction($transaction); $this->order->addRelatedObject($transaction); if ($this->document && $this->document instanceof AbstractModel) { $this->document->setTransactionId($transaction->getTxnId()); } return $this->linkWithParentTransaction($transaction); } return null; }
/** * Retrieve data * * @param string $key * @param mixed $index * @return mixed */ public function getData($key = '', $index = null) { if ('cc_number' === $key) { $ccNumber = parent::getData('cc_number'); $ccNumberEnc = parent::getData('cc_number_enc'); if (empty($ccNumber) && !empty($ccNumberEnc)) { $this->setData('cc_number', $this->decrypt($ccNumberEnc)); } } if ('cc_cid' === $key) { $ccCid = parent::getData('cc_cid'); $ccCidEnc = parent::getData('cc_cid_enc'); if (empty($ccCid) && !empty($ccCidEnc)) { $this->setData('cc_cid', $this->decrypt($ccCidEnc)); } } return parent::getData($key, $index); }
/** * Process relations for Shipment * * @param \Magento\Sales\Model\AbstractModel $object * @return void * @throws \Exception */ public function processRelation(\Magento\Sales\Model\AbstractModel $object) { /** @var \Magento\Sales\Model\Order\Shipment $object */ if (null !== $object->getItems()) { foreach ($object->getItems() as $item) { $item->setParentId($object->getId()); $this->shipmentItemResource->save($item); } } if (null !== $object->getTracks()) { foreach ($object->getTracks() as $track) { $this->shipmentTrackResource->save($track); } } if (null !== $object->getComments()) { foreach ($object->getComments() as $comment) { $this->shipmentCommentResource->save($comment); } } }
/** * Process relations for Shipment * * @param \Magento\Sales\Model\AbstractModel $object * @return void * @throws \Exception */ public function processRelation(\Magento\Sales\Model\AbstractModel $object) { /** @var $object \Magento\Sales\Model\Order\Invoice */ if (null !== $object->getItems()) { foreach ($object->getItems() as $item) { /** @var \Magento\Sales\Model\Order\Invoice\Item */ $item->setParentId($object->getId()); $item->setOrderItem($item->getOrderItem()); $this->invoiceItemResource->save($item); } } if (null !== $object->getComments()) { foreach ($object->getComments() as $comment) { /** @var \Magento\Sales\Model\Order\Invoice\Comment */ $this->invoiceCommentResource->save($comment); } } }
/** * Save order related objects * * @return $this */ protected function _afterSave() { if (null !== $this->_addresses) { $this->_addresses->save(); $billingAddress = $this->getBillingAddress(); $attributesForSave = array(); if ($billingAddress && $this->getBillingAddressId() != $billingAddress->getId()) { $this->setBillingAddressId($billingAddress->getId()); $attributesForSave[] = 'billing_address_id'; } $shippingAddress = $this->getShippingAddress(); if ($shippingAddress && $this->getShippigAddressId() != $shippingAddress->getId()) { $this->setShippingAddressId($shippingAddress->getId()); $attributesForSave[] = 'shipping_address_id'; } if (!empty($attributesForSave)) { $this->_getResource()->saveAttribute($this, $attributesForSave); } } if (null !== $this->_items) { $this->_items->save(); } if (null !== $this->_payments) { $this->_payments->save(); } if (null !== $this->_statusHistory) { $this->_statusHistory->save(); } foreach ($this->getRelatedObjects() as $object) { $object->save(); } return parent::_afterSave(); }
/** * @param \Magento\Framework\Model\Context $context * @param \Magento\Framework\Registry $registry * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory * @param AttributeValueFactory $customAttributeFactory * @param \Magento\Sales\Model\ResourceModel\Order\Shipment\Item\CollectionFactory $shipmentItemCollectionFactory * @param \Magento\Sales\Model\ResourceModel\Order\Shipment\Track\CollectionFactory $trackCollectionFactory * @param Shipment\CommentFactory $commentFactory * @param \Magento\Sales\Model\ResourceModel\Order\Shipment\Comment\CollectionFactory $commentCollectionFactory * @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection * @param array $data * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory, AttributeValueFactory $customAttributeFactory, \Magento\Sales\Model\ResourceModel\Order\Shipment\Item\CollectionFactory $shipmentItemCollectionFactory, \Magento\Sales\Model\ResourceModel\Order\Shipment\Track\CollectionFactory $trackCollectionFactory, \Magento\Sales\Model\Order\Shipment\CommentFactory $commentFactory, \Magento\Sales\Model\ResourceModel\Order\Shipment\Comment\CollectionFactory $commentCollectionFactory, \Magento\Sales\Api\OrderRepositoryInterface $orderRepository, \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null, array $data = []) { $this->_shipmentItemCollectionFactory = $shipmentItemCollectionFactory; $this->_trackCollectionFactory = $trackCollectionFactory; $this->_commentFactory = $commentFactory; $this->_commentCollectionFactory = $commentCollectionFactory; $this->orderRepository = $orderRepository; parent::__construct($context, $registry, $extensionFactory, $customAttributeFactory, $resource, $resourceCollection, $data); }
/** * @param \Magento\Framework\Model\Context $context * @param \Magento\Framework\Registry $registry * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory * @param AttributeValueFactory $customAttributeFactory * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection * @param array $data * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory, AttributeValueFactory $customAttributeFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null, array $data = []) { parent::__construct($context, $registry, $extensionFactory, $customAttributeFactory, $resource, $resourceCollection, $data); $this->_storeManager = $storeManager; }
/** * After object save manipulation * * @return $this */ protected function _afterSave() { if (null !== $this->_items) { /** * Save invoice items */ foreach ($this->_items as $item) { $item->setOrderItem($item->getOrderItem()); $item->save(); } } if (null !== $this->_comments) { foreach ($this->_comments as $comment) { $comment->save(); } } return parent::_afterSave(); }
/** * After object save manipulations * * @return $this */ protected function _afterSave() { if (null !== $this->_items) { foreach ($this->_items as $item) { $item->save(); } } if (null !== $this->_tracks) { foreach ($this->_tracks as $track) { $track->save(); } } if (null !== $this->_comments) { foreach ($this->_comments as $comment) { $comment->save(); } } return parent::_afterSave(); }
/** * Insert totals to pdf page * * @param \Zend_Pdf_Page $page * @param \Magento\Sales\Model\AbstractModel $source * @return \Zend_Pdf_Page */ protected function insertTotals($page, $source) { $order = $source->getOrder(); $totals = $this->_getTotalsList(); $lineBlock = array('lines' => array(), 'height' => 15); foreach ($totals as $total) { $total->setOrder($order)->setSource($source); if ($total->canDisplay()) { $total->setFontSize(10); foreach ($total->getTotalsForDisplay() as $totalData) { $lineBlock['lines'][] = array(array('text' => $totalData['label'], 'feed' => 475, 'align' => 'right', 'font_size' => $totalData['font_size'], 'font' => 'bold'), array('text' => $totalData['amount'], 'feed' => 565, 'align' => 'right', 'font_size' => $totalData['font_size'], 'font' => 'bold')); } } } $this->y -= 20; $page = $this->drawLineBlocks($page, array($lineBlock)); return $page; }
/** * @param \Magento\Framework\Model\Context $context * @param \Magento\Framework\Registry $registry * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory * @param AttributeValueFactory $customAttributeFactory * @param \Magento\Sales\Model\Order\ItemFactory $orderItemFactory * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection * @param array $data */ public function __construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory, AttributeValueFactory $customAttributeFactory, \Magento\Sales\Model\Order\ItemFactory $orderItemFactory, \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null, array $data = []) { parent::__construct($context, $registry, $extensionFactory, $customAttributeFactory, $resource, $resourceCollection, $data); $this->_orderItemFactory = $orderItemFactory; }
/** * Create transaction, * prepare its insertion into hierarchy and add its information to payment and comments * * To add transactions and related information, * the following information should be set to payment before processing: * - transaction_id * - is_transaction_closed (optional) - whether transaction should be closed or open (closed by default) * - parent_transaction_id (optional) * - should_close_parent_transaction (optional) - whether to close parent transaction (closed by default) * * If the sales document is specified, it will be linked to the transaction as related for future usage. * Currently transaction ID is set into the sales object * This method writes the added transaction ID into last_trans_id field of the payment object * * To make sure transaction object won't cause trouble before saving, use $failsafe = true * * @param string $type * @param \Magento\Sales\Model\AbstractModel $salesDocument * @param bool $failsafe * @return null|Transaction * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ protected function _addTransaction($type, $salesDocument = null, $failsafe = false) { if ($this->getSkipTransactionCreation()) { $this->unsTransactionId(); return null; } // look for set transaction ids $transactionId = $this->getTransactionId(); if (null !== $transactionId) { // set transaction parameters $transaction = false; if ($this->getOrder()->getId()) { $transaction = $this->_lookupTransaction($transactionId); } if (!$transaction) { $transaction = $this->_transactionFactory->create()->setTxnId($transactionId); } $transaction->setOrderPaymentObject($this)->setTxnType($type)->isFailsafe($failsafe); if ($this->hasIsTransactionClosed()) { $transaction->setIsClosed((int) $this->getIsTransactionClosed()); } //set transaction addition information if ($this->_transactionAdditionalInfo) { foreach ($this->_transactionAdditionalInfo as $key => $value) { $transaction->setAdditionalInformation($key, $value); } $this->_transactionAdditionalInfo = []; } // link with sales entities $this->setLastTransId($transactionId); $this->setCreatedTransaction($transaction); $this->getOrder()->addRelatedObject($transaction); if ($salesDocument && $salesDocument instanceof \Magento\Sales\Model\AbstractModel) { $salesDocument->setTransactionId($transactionId); } // link with parent transaction $parentTransactionId = $this->getParentTransactionId(); if ($parentTransactionId) { $transaction->setParentTxnId($parentTransactionId); if ($this->getShouldCloseParentTransaction()) { $parentTransaction = $this->_lookupTransaction($parentTransactionId); if ($parentTransaction) { if (!$parentTransaction->getIsClosed()) { $parentTransaction->isFailsafe($failsafe)->close(false); } $this->getOrder()->addRelatedObject($parentTransaction); } } } return $transaction; } return null; }
/** * Protect order delete from not admin scope * @return $this */ protected function _beforeDelete() { return parent::_beforeDelete(); }
/** * Before object save manipulations * * @return $this */ protected function _beforeSave() { parent::_beforeSave(); if (!$this->getOrderId() && $this->getOrder()) { $this->setOrderId($this->getOrder()->getId()); $this->setBillingAddressId($this->getOrder()->getBillingAddress()->getId()); } return $this; }
/** * Enforce format of the street field * * @param array|string $key * @param null $value * @return \Magento\Framework\DataObject */ public function setData($key, $value = null) { if (is_array($key)) { $key = $this->implodeStreetField($key); } elseif ($key == OrderAddressInterface::STREET) { $value = $this->implodeStreetValue($value); } return parent::setData($key, $value); }
/** * Add data to the object. * * Retains previous data in the object. * * @param array $data * @return $this */ public function addData(array $data) { if (array_key_exists('number', $data)) { $this->setNumber($data['number']); unset($data['number']); } return parent::addData($data); }
/** * Set order again if required * * @return $this */ public function beforeSave() { parent::beforeSave(); if (!$this->getParentId() && $this->getOrder()) { $this->setParentId($this->getOrder()->getId()); } return $this; }
/** * @param \Magento\Framework\Model\Context $context * @param \Magento\Framework\Registry $registry * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory * @param AttributeValueFactory $customAttributeFactory * @param Creditmemo\Config $creditmemoConfig * @param \Magento\Sales\Model\OrderFactory $orderFactory * @param \Magento\Sales\Model\ResourceModel\Order\Creditmemo\Item\CollectionFactory $cmItemCollectionFactory * @param \Magento\Framework\Math\CalculatorFactory $calculatorFactory * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param Creditmemo\CommentFactory $commentFactory * @param \Magento\Sales\Model\ResourceModel\Order\Creditmemo\Comment\CollectionFactory $commentCollectionFactory * @param PriceCurrencyInterface $priceCurrency * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection * @param array $data * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __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 = []) { $this->_creditmemoConfig = $creditmemoConfig; $this->_orderFactory = $orderFactory; $this->_cmItemCollectionFactory = $cmItemCollectionFactory; $this->_calculatorFactory = $calculatorFactory; $this->_storeManager = $storeManager; $this->_commentFactory = $commentFactory; $this->_commentCollectionFactory = $commentCollectionFactory; $this->priceCurrency = $priceCurrency; parent::__construct($context, $registry, $extensionFactory, $customAttributeFactory, $resource, $resourceCollection, $data); }
/** * Verify data required for saving * * @return $this */ public function beforeSave() { if (!$this->getOrderId() && $this->getOrder()) { $this->setOrderId($this->getOrder()->getId()); } if (!$this->getPaymentId() && $this->getOrder() && $this->getOrder()->getPayment()) { $this->setPaymentId($this->getOrder()->getPayment()->getId()); } // set parent id $this->_verifyPaymentObject(); if (!$this->getId()) { $this->setCreatedAt($this->_dateFactory->create()->gmtDate()); } return parent::beforeSave(); }
/** * After object save manipulation * * @return $this */ protected function _afterSave() { return parent::_afterSave(); }
/** * After save object attribute * * @param AbstractModel $object * @param string $attribute * @return \Magento\Sales\Model\Resource\Attribute */ protected function _afterSaveAttribute(AbstractModel $object, $attribute) { if ($object->getEventObject() && $object->getEventPrefix()) { $this->eventManager->dispatch($object->getEventPrefix() . '_save_attribute_after', [$object->getEventObject() => $this, 'object' => $object, 'attribute' => $attribute]); } return $this; }
/** * Verify data required for saving * * @return $this */ public function beforeSave() { // set parent id $this->_verifyPaymentObject(); if (!$this->getId()) { // We need to set order and payment ids only for new transactions if (null !== $this->_paymentObject) { $this->setPaymentId($this->_paymentObject->getId()); } if (null !== $this->_order) { $this->setOrderId($this->_order->getId()); } $this->setCreatedAt($this->_dateFactory->create()->gmtDate()); } return parent::beforeSave(); }
/** * Before object save * * @return $this */ protected function _beforeSave() { parent::_beforeSave(); if (!$this->getParentId() && $this->getInvoice()) { $this->setParentId($this->getInvoice()->getId()); } return $this; }
/** * @param string $key * @param null|string|int $index * @return mixed */ public function getData($key = '', $index = null) { if ($key == 'total_due') { return $this->getTotalDue(); } if ($key == 'base_total_due') { return $this->getBaseTotalDue(); } return parent::getData($key, $index); }
/** * Initialize dependencies. * * @param \Magento\Framework\Model\Context $context * @param \Magento\Framework\Registry $registry * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory * @param AttributeValueFactory $customAttributeFactory * @param \Magento\Sales\Model\OrderFactory $orderFactory * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository * @param \Magento\Framework\Model\Resource\AbstractResource $resource * @param \Magento\Framework\Data\Collection\Db $resourceCollection * @param array $data * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory, AttributeValueFactory $customAttributeFactory, \Magento\Sales\Model\OrderFactory $orderFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\Framework\Model\Resource\AbstractResource $resource = null, \Magento\Framework\Data\Collection\Db $resourceCollection = null, array $data = []) { parent::__construct($context, $registry, $extensionFactory, $customAttributeFactory, $resource, $resourceCollection, $data); $this->_orderFactory = $orderFactory; $this->_storeManager = $storeManager; $this->productRepository = $productRepository; }