/**
  * Callback function that filters collection by field "Used" from grid
  *
  * @param AbstractCollection $collection
  * @param Column $column
  * @return void
  */
 public function addIsUsedFilterCallback($collection, $column)
 {
     $filterValue = $column->getFilter()->getCondition();
     $expression = $this->getConnection()->getCheckSql('main_table.times_used > 0', 1, 0);
     $conditionSql = $this->_getConditionSql($expression, $filterValue);
     $collection->getSelect()->where($conditionSql);
 }
Beispiel #2
0
 /**
  * Print credit memos for selected orders
  *
  * @param AbstractCollection $collection
  * @return ResponseInterface|\Magento\Backend\Model\View\Result\Redirect
  */
 protected function massAction(AbstractCollection $collection)
 {
     $resultRedirect = $this->resultRedirectFactory->create();
     $flag = false;
     /** @var \Magento\Sales\Model\Order $order */
     foreach ($collection->getItems() as $order) {
         $creditmemos = $order->getCreditmemosCollection();
         if ($creditmemos->getSize()) {
             $flag = true;
             if (!isset($pdf)) {
                 $pdf = $this->_objectManager->create('Magento\\Sales\\Model\\Order\\Pdf\\Creditmemo')->getPdf($creditmemos);
             } else {
                 $pages = $this->_objectManager->create('Magento\\Sales\\Model\\Order\\Pdf\\Creditmemo')->getPdf($creditmemos);
                 $pdf->pages = array_merge($pdf->pages, $pages->pages);
             }
         }
     }
     if ($flag) {
         $date = $this->_objectManager->get('Magento\\Framework\\Stdlib\\DateTime\\DateTime')->date('Y-m-d_H-i-s');
         return $this->_fileFactory->create('creditmemo' . $date . '.pdf', $pdf->render(), DirectoryList::VAR_DIR, 'application/pdf');
     } else {
         $this->messageManager->addError(__('There are no printable documents related to selected orders.'));
         $resultRedirect->setPath('sales/*/');
         return $resultRedirect;
     }
 }
 /**
  * Set collection to pager
  *
  * @param \Magento\Framework\Data\Collection $collection
  * @return $this
  */
 public function setCollection($collection)
 {
     $this->_collection = $collection;
     $this->_collection->setCurPage($this->getCurrentPage());
     // we need to set pagination only if passed value integer and more that 0
     $limit = (int) $this->getLimit();
     if ($limit) {
         $this->_collection->setPageSize($limit);
     }
     if ($this->getCurrentOrder()) {
         $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
     }
     return $this;
 }
Beispiel #4
0
 /**
  * get select count sql
  *
  * @return \Zend_Db_Select
  */
 public function getSelectCountSql()
 {
     if (!$this->_countSelect instanceof \Zend_Db_Select) {
         $this->setSelectCountSql(parent::getSelectCountSql());
     }
     return $this->_countSelect;
 }
 /**
  * Unserialize additional_information in each item
  *
  * @return $this
  */
 protected function _afterLoad()
 {
     foreach ($this->_items as $item) {
         $this->getResource()->unserializeFields($item);
     }
     return parent::_afterLoad();
 }
Beispiel #6
0
 /**
  * Don't add item to the collection if only fixed are allowed and its carrier is not fixed
  *
  * @param \Magento\Quote\Model\Quote\Address\Rate $rate
  * @return $this
  */
 public function addItem(\Magento\Framework\Object $rate)
 {
     $carrier = $this->_carrierFactory->get($rate->getCarrier());
     if ($this->_allowFixedOnly && (!$carrier || !$carrier->isFixed())) {
         return $this;
     }
     return parent::addItem($rate);
 }
 /**
  * Load collection data
  *
  * @return $this
  */
 public function _beforeLoad()
 {
     if (!$this->getLoadDefault()) {
         $this->setWithoutDefaultFilter();
     }
     $this->addOrder('main_table.name', self::SORT_ORDER_ASC);
     return parent::_beforeLoad();
 }
Beispiel #8
0
 /**
  * Initialize select object
  *
  * @return $this
  */
 protected function _initSelect()
 {
     parent::_initSelect();
     $locale = $this->_localeResolver->getLocale();
     $this->addBindParam(':region_locale', $locale);
     $this->getSelect()->joinLeft(['rname' => $this->_regionNameTable], 'main_table.region_id = rname.region_id AND rname.locale = :region_locale', ['name']);
     return $this;
 }
Beispiel #9
0
 /**
  * @inheritdoc
  */
 public function fetchItem()
 {
     $item = parent::fetchItem();
     if ($item) {
         $this->entitySnapshot->registerSnapshot($item);
     }
     return $item;
 }
Beispiel #10
0
 /**
  * Process loaded collection data
  *
  * @return $this
  */
 protected function _afterLoadData()
 {
     parent::_afterLoadData();
     $this->addCustomerTaxClassesToResult();
     $this->addProductTaxClassesToResult();
     $this->addRatesToResult();
     return $this;
 }
Beispiel #11
0
 /**
  * Initialize select, add country iso3 code and region name
  *
  * @return void
  */
 public function _initSelect()
 {
     parent::_initSelect();
     $this->_select->joinLeft(['country_table' => $this->_countryTable], 'country_table.country_id = main_table.dest_country_id', ['dest_country' => 'iso3_code'])->joinLeft(['region_table' => $this->_regionTable], 'region_table.region_id = main_table.dest_region_id', ['dest_region' => 'code']);
     $this->addOrder('dest_country', self::SORT_ORDER_ASC);
     $this->addOrder('dest_region', self::SORT_ORDER_ASC);
     $this->addOrder('dest_zip', self::SORT_ORDER_ASC);
     $this->addOrder('condition_value', self::SORT_ORDER_ASC);
 }
Beispiel #12
0
 /**
  * Initialize select
  *
  * @return $this
  */
 protected function _initSelect()
 {
     parent::_initSelect();
     $this->addFieldToSelect(['path', 'value'])->addFieldToFilter('scope', $this->_scope);
     if ($this->_scopeId !== null) {
         $this->addFieldToFilter('scope_id', $this->_scopeId);
     }
     return $this;
 }
Beispiel #13
0
 /**
  * Initialize select
  *
  * @return $this
  */
 protected function _initSelect()
 {
     parent::_initSelect();
     $this->addFieldToSelect(array('path', 'value'))->addFieldToFilter('scope', $this->_scope);
     if (!is_null($this->_scopeId)) {
         $this->addFieldToFilter('scope_id', $this->_scopeId);
     }
     return $this;
 }
Beispiel #14
0
 /**
  * @param \Magento\Core\Model\EntityFactory $entityFactory
  * @param \Magento\Framework\Logger $logger
  * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  * @param \Magento\Framework\Event\ManagerInterface $eventManager
  * @param \Magento\Framework\Locale\ListsInterface $localeLists
  * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  * @param \Magento\Directory\Model\Resource\CountryFactory $countryFactory
  * @param \Magento\Framework\Stdlib\ArrayUtils $arrayUtils
  * @param \Magento\Framework\Locale\ResolverInterface $localeResolver
  * @param mixed $connection
  * @param \Magento\Framework\Model\Resource\Db\AbstractDb $resource
  */
 public function __construct(\Magento\Core\Model\EntityFactory $entityFactory, \Magento\Framework\Logger $logger, \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Framework\Locale\ListsInterface $localeLists, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Directory\Model\Resource\CountryFactory $countryFactory, \Magento\Framework\Stdlib\ArrayUtils $arrayUtils, \Magento\Framework\Locale\ResolverInterface $localeResolver, $connection = null, \Magento\Framework\Model\Resource\Db\AbstractDb $resource = null)
 {
     parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
     $this->_scopeConfig = $scopeConfig;
     $this->_localeLists = $localeLists;
     $this->_localeResolver = $localeResolver;
     $this->_countryFactory = $countryFactory;
     $this->_arrayUtils = $arrayUtils;
 }
 /**
  * Batch print shipping labels for whole shipments.
  * Push pdf document with shipping labels to user browser
  *
  * @param AbstractCollection $collection
  * @return ResponseInterface|void
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 protected function massAction(AbstractCollection $collection)
 {
     $request = $this->getRequest();
     $ids = $collection->getAllIds();
     $createdFromOrders = !empty($ids);
     $shipments = null;
     $labelsContent = [];
     switch ($request->getParam('massaction_prepare_key')) {
         case 'shipment_ids':
             $ids = $request->getParam('shipment_ids');
             array_filter($ids, 'intval');
             if (!empty($ids)) {
                 $shipments = $this->_objectManager->create('Magento\\Sales\\Model\\Resource\\Order\\Shipment\\Collection')->addFieldToFilter('entity_id', ['in' => $ids]);
             }
             break;
         case 'order_ids':
             $ids = $request->getParam('order_ids');
             array_filter($ids, 'intval');
             if (!empty($ids)) {
                 $shipments = $this->_objectManager->create('Magento\\Sales\\Model\\Resource\\Order\\Shipment\\Collection')->setOrderFilter(['in' => $ids]);
             }
             break;
     }
     if ($shipments && $shipments->getSize()) {
         foreach ($shipments as $shipment) {
             $labelContent = $shipment->getShippingLabel();
             if ($labelContent) {
                 $labelsContent[] = $labelContent;
             }
         }
     }
     if (!empty($labelsContent)) {
         $outputPdf = $this->labelGenerator->combineLabelsPdf($labelsContent);
         return $this->_fileFactory->create('ShippingLabels.pdf', $outputPdf->render(), DirectoryList::VAR_DIR, 'application/pdf');
     }
     if ($createdFromOrders) {
         $this->messageManager->addError(__('There are no shipping labels related to selected orders.'));
         $this->_redirect('sales/order/index');
     } else {
         $this->messageManager->addError(__('There are no shipping labels related to selected shipments.'));
         $this->_redirect('sales/shipment/index');
     }
 }
Beispiel #16
0
 /**
  * Load collection data
  *
  * @param bool $printQuery
  * @param bool $logQuery
  * @return  \Magento\GoogleShopping\Model\Resource\Attribute\Collection
  */
 public function load($printQuery = false, $logQuery = false)
 {
     if ($this->isLoaded()) {
         return $this;
     }
     if ($this->getJoinAttributeSetFlag()) {
         $this->_joinAttributeSet();
     }
     parent::load($printQuery, $logQuery);
     return $this;
 }
Beispiel #17
0
 /**
  * Set parent items
  *
  * @return $this
  */
 protected function _afterLoad()
 {
     parent::_afterLoad();
     /**
      * Assign parent items
      */
     foreach ($this as $item) {
         if ($item->getParentItemId()) {
             $item->setParentItem($this->getItemById($item->getParentItemId()));
         }
     }
     return $this;
 }
Beispiel #18
0
 /**
  * Hold selected orders
  *
  * @param AbstractCollection $collection
  * @return \Magento\Backend\Model\View\Result\Redirect
  */
 protected function massAction(AbstractCollection $collection)
 {
     $countHoldOrder = 0;
     foreach ($collection->getItems() as $order) {
         if (!$order->canHold()) {
             continue;
         }
         $order->hold();
         $order->save();
         $countHoldOrder++;
     }
     $countNonHoldOrder = $collection->count() - $countHoldOrder;
     if ($countNonHoldOrder && $countHoldOrder) {
         $this->messageManager->addError(__('%1 order(s) were not put on hold.', $countNonHoldOrder));
     } elseif ($countNonHoldOrder) {
         $this->messageManager->addError(__('No order(s) were put on hold.'));
     }
     if ($countHoldOrder) {
         $this->messageManager->addSuccess(__('You have put %1 order(s) on hold.', $countHoldOrder));
     }
     $resultRedirect = $this->resultRedirectFactory->create();
     $resultRedirect->setPath('sales/*/');
     return $resultRedirect;
 }
Beispiel #19
0
 /**
  * Cancel selected orders
  *
  * @param AbstractCollection $collection
  * @return \Magento\Backend\Model\View\Result\Redirect
  */
 protected function massAction(AbstractCollection $collection)
 {
     $countCancelOrder = 0;
     foreach ($collection->getItems() as $order) {
         if (!$order->canCancel()) {
             continue;
         }
         $order->cancel();
         $order->save();
         $countCancelOrder++;
     }
     $countNonCancelOrder = $collection->count() - $countCancelOrder;
     if ($countNonCancelOrder && $countCancelOrder) {
         $this->messageManager->addError(__('%1 order(s) cannot be canceled.', $countNonCancelOrder));
     } elseif ($countNonCancelOrder) {
         $this->messageManager->addError(__('You cannot cancel the order(s).'));
     }
     if ($countCancelOrder) {
         $this->messageManager->addSuccess(__('We canceled %1 order(s).', $countCancelOrder));
     }
     $resultRedirect = $this->resultRedirectFactory->create();
     $resultRedirect->setPath('sales/*/');
     return $resultRedirect;
 }
Beispiel #20
0
 /**
  * Fill array of options by item and product
  *
  * @return $this
  */
 protected function _afterLoad()
 {
     parent::_afterLoad();
     foreach ($this as $option) {
         $optionId = $option->getId();
         $itemId = $option->getItemId();
         $productId = $option->getProductId();
         if (isset($this->_optionsByItem[$itemId])) {
             $this->_optionsByItem[$itemId][] = $optionId;
         } else {
             $this->_optionsByItem[$itemId] = [$optionId];
         }
         if (isset($this->_optionsByProduct[$productId])) {
             $this->_optionsByProduct[$productId][] = $optionId;
         } else {
             $this->_optionsByProduct[$productId] = [$optionId];
         }
     }
     return $this;
 }
Beispiel #21
0
 /**
  * Initialize select object
  *
  * @return $this
  */
 protected function _initSelect()
 {
     return parent::_initSelect()->getSelect()->join(array('cp_table' => $this->getTable('catalog_product_entity')), 'main_table.product_id = cp_table.entity_id', array('type_id'));
 }
Beispiel #22
0
 /**
  * Convert items array to array for select options
  *
  * @return array
  */
 public function toOptionArray()
 {
     return parent::_toOptionArray('status_id', 'status_code');
 }
Beispiel #23
0
 /**
  * Add field filter to collection
  * If $attribute is an array will add OR condition with following format:
  * array(
  *     array('attribute'=>'firstname', 'like'=>'test%'),
  *     array('attribute'=>'lastname', 'like'=>'test%'),
  * )
  *
  * @param string $field
  * @param null|string|array $condition
  * @return \Magento\Eav\Model\Entity\Collection\AbstractCollection
  *
  * @see self::_getConditionSql for $condition
  */
 public function addFieldToFilter($field, $condition = null)
 {
     if (isset($this->_fields[$field])) {
         $field = $this->_fields[$field];
     }
     return parent::addFieldToFilter($field, $condition);
 }
Beispiel #24
0
 /**
  * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
  * @param \Psr\Log\LoggerInterface $logger
  * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  * @param \Magento\Framework\Event\ManagerInterface $eventManager
  * @param \Magento\Catalog\Model\Resource\Product\Option\Value\CollectionFactory $optionValueCollectionFactory
  * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  * @param \Zend_Db_Adapter_Abstract $connection
  * @param \Magento\Framework\Model\Resource\Db\AbstractDb $resource
  */
 public function __construct(\Magento\Framework\Data\Collection\EntityFactory $entityFactory, \Psr\Log\LoggerInterface $logger, \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Catalog\Model\Resource\Product\Option\Value\CollectionFactory $optionValueCollectionFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, $connection = null, \Magento\Framework\Model\Resource\Db\AbstractDb $resource = null)
 {
     $this->_optionValueCollectionFactory = $optionValueCollectionFactory;
     $this->_storeManager = $storeManager;
     parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
 }
Beispiel #25
0
 /**
  * Add joins to select
  *
  * @return $this
  */
 protected function _beforeLoad()
 {
     $select = $this->getSelect();
     $connection = $this->getConnection();
     $entityType = $this->getEntityType();
     $this->setItemObjectClass($entityType->getAttributeModel());
     $eaColumns = [];
     $caColumns = [];
     $saColumns = [];
     $eaDescribe = $connection->describeTable($this->getTable('eav_attribute'));
     unset($eaDescribe['attribute_id']);
     foreach (array_keys($eaDescribe) as $columnName) {
         $eaColumns[$columnName] = $columnName;
     }
     $select->join(['ea' => $this->getTable('eav_attribute')], 'main_table.attribute_id = ea.attribute_id', $eaColumns);
     // join additional attribute data table
     $additionalTable = $entityType->getAdditionalAttributeTable();
     if ($additionalTable) {
         $caDescribe = $connection->describeTable($this->getTable($additionalTable));
         unset($caDescribe['attribute_id']);
         foreach (array_keys($caDescribe) as $columnName) {
             $caColumns[$columnName] = $columnName;
         }
         $select->join(['ca' => $this->getTable($additionalTable)], 'main_table.attribute_id = ca.attribute_id', $caColumns);
     }
     // add scope values
     if ($this->_getEavWebsiteTable()) {
         $saDescribe = $connection->describeTable($this->_getEavWebsiteTable());
         unset($saDescribe['attribute_id']);
         foreach (array_keys($saDescribe) as $columnName) {
             if ($columnName == 'website_id') {
                 $saColumns['scope_website_id'] = $columnName;
             } else {
                 if (isset($eaColumns[$columnName])) {
                     $code = sprintf('scope_%s', $columnName);
                     $expression = $connection->getCheckSql('sa.%s IS NULL', 'ea.%s', 'sa.%s');
                     $saColumns[$code] = new \Zend_Db_Expr(sprintf($expression, $columnName, $columnName, $columnName));
                 } elseif (isset($caColumns[$columnName])) {
                     $code = sprintf('scope_%s', $columnName);
                     $expression = $connection->getCheckSql('sa.%s IS NULL', 'ca.%s', 'sa.%s');
                     $saColumns[$code] = new \Zend_Db_Expr(sprintf($expression, $columnName, $columnName, $columnName));
                 }
             }
         }
         $store = $this->getStore();
         $joinWebsiteExpression = $connection->quoteInto('sa.attribute_id = main_table.attribute_id AND sa.website_id = ?', (int) $store->getWebsiteId());
         $select->joinLeft(['sa' => $this->_getEavWebsiteTable()], $joinWebsiteExpression, $saColumns);
     }
     // add store attribute label
     $storeLabelExpr = $connection->getCheckSql('al.value IS NULL', 'ea.frontend_label', 'al.value');
     $joinExpression = $connection->quoteInto('al.attribute_id = main_table.attribute_id AND al.store_id = ?', (int) $store->getId());
     $select->joinLeft(['al' => $this->getTable('eav_attribute_label')], $joinExpression, ['store_label' => $storeLabelExpr]);
     // add entity type filter
     $select->where('ea.entity_type_id = ?', (int) $entityType->getId());
     return parent::_beforeLoad();
 }
Beispiel #26
0
 /**
  * Load data
  *
  * @param boolean $printQuery
  * @param boolean $logQuery
  * @return $this
  */
 public function load($printQuery = false, $logQuery = false)
 {
     if ($this->isLoaded()) {
         return $this;
     }
     $this->_eventManager->dispatch('review_review_collection_load_before', ['collection' => $this]);
     parent::load($printQuery, $logQuery);
     if ($this->_addStoreDataFlag) {
         $this->_addStoreData();
     }
     return $this;
 }
Beispiel #27
0
 /**
  * After load processing
  *
  * @return $this
  */
 protected function _afterLoad()
 {
     parent::_afterLoad();
     /**
      * Assign parent items
      */
     foreach ($this as $item) {
         if ($item->getParentItemId()) {
             $item->setParentItem($this->getItemById($item->getParentItemId()));
         }
         if ($this->_quote) {
             $item->setQuote($this->_quote);
         }
     }
     /**
      * Assign options and products
      */
     $this->_assignOptions();
     $this->_assignProducts();
     $this->resetItemsDataChanged();
     return $this;
 }
Beispiel #28
0
 /**
  * Get resource instance
  *
  * @return \Mtf\Util\Generate\Repository\Resource
  */
 public function getResource()
 {
     return parent::getResource();
 }
Beispiel #29
0
 /**
  * After load collection process
  *
  * @return $this
  */
 protected function _afterLoad()
 {
     parent::_afterLoad();
     \Magento\Framework\Profiler::start('TTT1:' . __METHOD__, array('group' => 'TTT1', 'method' => __METHOD__));
     $this->_addProductAttributes();
     \Magento\Framework\Profiler::stop('TTT1:' . __METHOD__);
     \Magento\Framework\Profiler::start('TTT2:' . __METHOD__, array('group' => 'TTT2', 'method' => __METHOD__));
     $this->_addAssociatedProductFilters();
     \Magento\Framework\Profiler::stop('TTT2:' . __METHOD__);
     \Magento\Framework\Profiler::start('TTT3:' . __METHOD__, array('group' => 'TTT3', 'method' => __METHOD__));
     $this->_loadLabels();
     \Magento\Framework\Profiler::stop('TTT3:' . __METHOD__);
     \Magento\Framework\Profiler::start('TTT4:' . __METHOD__, array('group' => 'TTT4', 'method' => __METHOD__));
     $this->_loadPrices();
     \Magento\Framework\Profiler::stop('TTT4:' . __METHOD__);
     return $this;
 }
 /**
  * Delete collection items
  *
  * @param AbstractCollection $collection
  * @return int
  */
 protected function delete(AbstractCollection $collection)
 {
     $count = 0;
     foreach ($collection->getAllIds() as $id) {
         /** @var \Magento\Framework\Model\AbstractModel $model */
         $model = $this->_objectManager->get($this->model);
         $model->load($id);
         $model->delete();
         ++$count;
     }
     return $count;
 }