Exemplo n.º 1
0
 /**
  * Update the status of a queue record and check to confirm the exclusive change
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return bool
  */
 public function changeQueueStatusWithLocking(AbstractModel $object)
 {
     /* @var $object \ClassyLlama\AvaTax\Model\Queue */
     $object->setUpdatedAt($this->dateTime->gmtDate());
     $data = $this->prepareDataForUpdate($object);
     $originalQueueStatus = $object->getOrigData(self::QUEUE_STATUS_FIELD_NAME);
     $originalUpdatedAt = $object->getOrigData(self::UPDATED_AT_FIELD_NAME);
     // A conditional update does a read lock on update so we use the condition on the old
     // queue status here to guarantee that nothing else has modified the status for processing
     $condition = array();
     // update only the queue record identified by Id
     $condition[] = $this->getConnection()->quoteInto($this->getIdFieldName() . '=?', $object->getId());
     // only update the record if it is still pending
     $condition[] = $this->getConnection()->quoteInto(self::QUEUE_STATUS_FIELD_NAME . '=?', $originalQueueStatus);
     // only update the record if nothing else has updated it
     if ($originalUpdatedAt === null) {
         $condition[] = self::UPDATED_AT_FIELD_NAME . ' IS NULL';
     } else {
         $condition[] = $this->getConnection()->quoteInto(self::UPDATED_AT_FIELD_NAME . '=?', $originalUpdatedAt);
     }
     // update the record and get the number of affected records
     $affectedRowCount = $this->getConnection()->update($this->getMainTable(), $data, $condition);
     $result = false;
     if ($affectedRowCount > 0) {
         $object->setHasDataChanges(false);
         $result = true;
     }
     return $result;
 }
Exemplo n.º 2
0
 /**
  * Prepare data to be saved to database
  * @param \Magento\Framework\Model\AbstractModel|\Magento\Framework\Object $object
  *
  * @return $this
  */
 protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
 {
     if ($object->isObjectNew()) {
         $object->setCreatedAt($this->dateTime->formatDate(true));
     }
     $object->setUpdatedAt($this->dateTime->formatDate(true));
     return $this;
 }
Exemplo n.º 3
0
 protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
 {
     if (!$object->getId()) {
         $object->setCreatedAt($this->_date->gmtDate());
     }
     $object->setUpdatedAt($this->_date->gmtDate());
     return $this;
 }
Exemplo n.º 4
0
 /**
  * @param \Magento\Framework\Model\AbstractModel $object
  *
  * @return $this
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
 {
     if ($object->isObjectNew() && !$object->hasCreatedAt()) {
         $object->setCreatedAt($this->_date->gmtDate());
     }
     $object->setUpdatedAt($this->_date->gmtDate());
     return parent::_beforeSave($object);
 }
Exemplo n.º 5
0
 /**
  * Prepare data for save
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return array
  */
 protected function _prepareDataForSave(\Magento\Framework\Model\AbstractModel $object)
 {
     $currentTime = $this->dateTime->now();
     if ((!$object->getId() || $object->isObjectNew()) && !$object->getCreatedAt()) {
         $object->setCreatedAt($currentTime);
     }
     $object->setUpdatedAt($currentTime);
     $data = parent::_prepareDataForSave($object);
     return $data;
 }
Exemplo n.º 6
0
 /**
  * before save callback
  *
  * @param \Magento\Framework\Model\AbstractModel|\Mageplaza\Blog\Model\Topic $object
  * @return $this
  */
 protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
 {
     $object->setUpdatedAt($this->date->date());
     if ($object->isObjectNew()) {
         $object->setCreatedAt($this->date->date());
     }
     //Check Url Key
     if ($object->isObjectNew()) {
         $count = 0;
         $objName = $object->getName();
         if ($object->getUrlKey()) {
             $urlKey = $object->getUrlKey();
         } else {
             $urlKey = $this->generateUrlKey($objName, $count);
         }
         while ($this->checkUrlKey($urlKey)) {
             $count++;
             $urlKey = $this->generateUrlKey($urlKey, $count);
         }
         $object->setUrlKey($urlKey);
     } else {
         $objectId = $object->getId();
         $count = 0;
         $objName = $object->getName();
         if ($object->getUrlKey()) {
             $urlKey = $object->getUrlKey();
         } else {
             $urlKey = $this->generateUrlKey($objName, $count);
         }
         while ($this->checkUrlKey($urlKey, $objectId)) {
             $count++;
             $urlKey = $this->generateUrlKey($urlKey, $count);
         }
         $object->setUrlKey($urlKey);
     }
     return parent::_beforeSave($object);
 }
Exemplo n.º 7
0
 /**
  * Set date of last update
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return \Magento\Framework\Model\Resource\Db\AbstractDb
  */
 protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
 {
     $object->setUpdatedAt($this->dateTime->formatDate($this->_coreDate->gmtDate()));
     return parent::_beforeSave($object);
 }
Exemplo n.º 8
0
 /**
  * Perform actions after object save
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return $this
  */
 protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
 {
     $connection = $this->getConnection();
     $columns = $connection->describeTable($this->getMainTable());
     if (isset($columns['created_at'], $columns['updated_at'])) {
         $select = $connection->select()->from($this->getMainTable(), ['created_at', 'updated_at'])->where($this->getIdFieldName() . ' = :entity_id');
         $row = $connection->fetchRow($select, [':entity_id' => $object->getId()]);
         if (is_array($row) && isset($row['created_at'], $row['updated_at'])) {
             $object->setCreatedAt($row['created_at']);
             $object->setUpdatedAt($row['updated_at']);
         }
     }
     parent::_afterSave($object);
     return $this;
 }
Exemplo n.º 9
0
 /**
  * @param AbstractModel $object
  * @return $this
  */
 public function _beforeSave(AbstractModel $object)
 {
     $object->setUpdatedAt($this->dateTime->formatDate($this->_date->gmtTimestamp()));
     return $this;
 }
Exemplo n.º 10
0
 /**
  * before save callback
  *
  * @param AbstractModel|\Sample\News\Model\Author $object
  * @return $this
  */
 protected function _beforeSave(AbstractModel $object)
 {
     foreach (['dob'] as $field) {
         $value = !$object->getData($field) ? null : $object->getData($field);
         $object->setData($field, $this->dateTime->formatDate($value));
     }
     $object->setUpdatedAt($this->date->gmtDate());
     if ($object->isObjectNew()) {
         $object->setCreatedAt($this->date->gmtDate());
     }
     $urlKey = $object->getData('url_key');
     if ($urlKey == '') {
         $urlKey = $object->getName();
     }
     $urlKey = $object->formatUrlKey($urlKey);
     $object->setUrlKey($urlKey);
     $validKey = false;
     while (!$validKey) {
         if ($this->getIsUniqueAuthorToStores($object)) {
             $validKey = true;
         } else {
             $parts = explode('-', $urlKey);
             $last = $parts[count($parts) - 1];
             if (!is_numeric($last)) {
                 $urlKey = $urlKey . '-1';
             } else {
                 $suffix = '-' . ($last + 1);
                 unset($parts[count($parts) - 1]);
                 $urlKey = implode('-', $parts) . $suffix;
             }
             $object->setData('url_key', $urlKey);
         }
     }
     return parent::_beforeSave($object);
 }
Exemplo n.º 11
0
 /**
  * {@inheritdoc}
  */
 public function save(\Magento\Framework\Model\AbstractModel $object)
 {
     $object->setUpdatedAt(gmdate('Y-m-d H:i:s'));
     return parent::save($object);
 }
 /**
  * @param \Magento\Sales\Model\Spi\InvoiceResourceInterface $subject
  * @param \Closure $proceed
  *
  *        I include both the extended AbstractModel and implemented Interface here for the IDE's benefit
  * @param \Magento\Framework\Model\AbstractModel|\Magento\Sales\Api\Data\InvoiceInterface $entity
  * @return \Magento\Sales\Model\Spi\InvoiceResourceInterface
  * @throws \Magento\Framework\Exception\CouldNotSaveException
  */
 public function aroundSave(InvoiceResourceInterface $subject, \Closure $proceed, AbstractModel $entity)
 {
     // Check to see if this is a newly created entity and store the determination for later evaluation after
     // the entity is saved via plugin closure. After the entity is saved it will not be listed as new any longer.
     $isObjectNew = $entity->isObjectNew();
     // Save AvaTax extension attributes
     if ($this->avaTaxConfig->isModuleEnabled($entity->getStoreId())) {
         // check to see if any extension attributes exist and set them on the model for saving to the db
         $extensionAttributes = $entity->getExtensionAttributes();
         if ($extensionAttributes && $extensionAttributes->getAvataxIsUnbalanced() !== null) {
             $entity->setData('avatax_is_unbalanced', $extensionAttributes->getAvataxIsUnbalanced());
         }
         if ($extensionAttributes && $extensionAttributes->getBaseAvataxTaxAmount() !== null) {
             $entity->setData('base_avatax_tax_amount', $extensionAttributes->getBaseAvataxTaxAmount());
         }
         // Updating a field to trigger a change to the record when avatax_is_unbalanced and base_avatax_tax_amount
         // are both false or 0 which evaluate the same as null in the isModified check
         if ($extensionAttributes && ($extensionAttributes->getAvataxIsUnbalanced() !== null && ($entity->getOrigData('avatax_is_unbalanced') === null || $extensionAttributes->getAvataxIsUnbalanced() != $entity->getOrigData('avatax_is_unbalanced')) || $extensionAttributes->getBaseAvataxTaxAmount() !== null && ($entity->getOrigData('base_avatax_tax_amount') === null || $extensionAttributes->getBaseAvataxTaxAmount() != $entity->getOrigData('base_avatax_tax_amount')))) {
             $entity->setUpdatedAt($this->dateTime->gmtDate());
         }
     }
     /** @var \Magento\Sales\Model\Spi\InvoiceResourceInterface $resultEntity */
     $resultEntity = $proceed($entity);
     /** @var \Magento\Sales\Model\Order $order */
     $order = $entity->getOrder();
     $isVirtual = $order->getIsVirtual();
     $address = $isVirtual ? $entity->getBillingAddress() : $entity->getShippingAddress();
     $storeId = $entity->getStoreId();
     // Queue the entity to be sent to AvaTax
     if ($this->avaTaxConfig->isModuleEnabled($entity->getStoreId()) && $this->avaTaxConfig->getTaxMode($entity->getStoreId()) == Config::TAX_MODE_ESTIMATE_AND_SUBMIT && $this->avaTaxConfig->isAddressTaxable($address, $storeId)) {
         // Add this entity to the avatax processing queue if this is a new entity
         if ($isObjectNew) {
             /** @var Queue $queue */
             $queue = $this->queueFactory->create();
             $queue->build($entity->getStoreId(), Queue::ENTITY_TYPE_CODE_INVOICE, $entity->getEntityId(), $entity->getIncrementId(), Queue::QUEUE_STATUS_PENDING);
             $queue->save();
             $this->avaTaxLogger->debug(__('Added entity to the queue'), ['queue_id' => $queue->getId(), 'entity_type_code' => Queue::ENTITY_TYPE_CODE_INVOICE, 'entity_id' => $entity->getEntityId()]);
         }
     }
     return $resultEntity;
 }
Exemplo n.º 13
0
 /**
  * before save callback
  *
  * @param \Magento\Framework\Model\AbstractModel|\Mageplaza\Blog\Model\Category $object
  * @return $this
  */
 protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
 {
     $object->setUpdatedAt($this->date->date());
     if ($object->isObjectNew()) {
         $object->setCreatedAt($this->date->date());
     }
     /** @var \Mageplaza\Blog\Model\Category $object */
     parent::_beforeSave($object);
     if (!$object->getChildrenCount()) {
         $object->setChildrenCount(0);
     }
     if ($object->isObjectNew()) {
         if ($object->getPosition() === null) {
             $object->setPosition($this->getMaxPosition($object->getPath()) + 1);
         }
         $path = explode('/', $object->getPath());
         $level = count($path) - ($object->getId() ? 1 : 0);
         $toUpdateChild = array_diff($path, [$object->getId()]);
         if (!$object->hasPosition()) {
             $object->setPosition($this->getMaxPosition(implode('/', $toUpdateChild)) + 1);
         }
         if (!$object->hasLevel()) {
             $object->setLevel($level);
         }
         if (!$object->hasParentId() && $level && !$object->getInitialSetupFlag()) {
             $object->setParentId($path[$level - 1]);
         }
         if (!$object->getId() && !$object->getInitialSetupFlag()) {
             $object->setPath($object->getPath() . '/');
         }
         if (!$object->getInitialSetupFlag()) {
             $this->getConnection()->update($this->getMainTable(), ['children_count' => new \Zend_Db_Expr('children_count+1')], ['category_id IN(?)' => $toUpdateChild]);
         }
     }
     //Check Url Key
     if ($object->isObjectNew()) {
         $count = 0;
         $objName = $object->getName();
         if ($object->getUrlKey()) {
             $urlKey = $object->getUrlKey();
         } else {
             $urlKey = $this->generateUrlKey($objName, $count);
         }
         while ($this->checkUrlKey($urlKey)) {
             $count++;
             $urlKey = $this->generateUrlKey($urlKey, $count);
         }
         $object->setUrlKey($urlKey);
     } else {
         $objectId = $object->getId();
         $count = 0;
         $objName = $object->getName();
         if ($object->getUrlKey()) {
             $urlKey = $object->getUrlKey();
         } else {
             $urlKey = $this->generateUrlKey($objName, $count);
         }
         while ($this->checkUrlKey($urlKey, $objectId)) {
             $count++;
             $urlKey = $this->generateUrlKey($urlKey, $count);
         }
         $object->setUrlKey($urlKey);
     }
     return $this;
 }