/** * Apply catalog price rules to product in admin * * @param \Magento\Framework\Event\Observer $observer * @return $this */ public function execute(\Magento\Framework\Event\Observer $observer) { $product = $observer->getEvent()->getProduct(); $storeId = $product->getStoreId(); $date = $this->localeDate->scopeDate($storeId); $key = false; $ruleData = $this->coreRegistry->registry('rule_data'); if ($ruleData) { $wId = $ruleData->getWebsiteId(); $gId = $ruleData->getCustomerGroupId(); $pId = $product->getId(); $key = "{$date->format('Y-m-d H:i:s')}|{$wId}|{$gId}|{$pId}"; } elseif ($product->getWebsiteId() !== null && $product->getCustomerGroupId() !== null) { $wId = $product->getWebsiteId(); $gId = $product->getCustomerGroupId(); $pId = $product->getId(); $key = "{$date->format('Y-m-d H:i:s')}|{$wId}|{$gId}|{$pId}"; } if ($key) { if (!$this->rulePricesStorage->hasRulePrice($key)) { $rulePrice = $this->resourceRuleFactory->create()->getRulePrice($date, $wId, $gId, $pId); $this->rulePricesStorage->setRulePrice($key, $rulePrice); } if ($this->rulePricesStorage->getRulePrice($key) !== false) { $finalPrice = min($product->getData('final_price'), $this->rulePricesStorage->getRulePrice($key)); $product->setFinalPrice($finalPrice); } } return $this; }
/** * Apply catalog price rules to product on frontend * * @param \Magento\Framework\Event\Observer $observer * @return $this */ public function execute(\Magento\Framework\Event\Observer $observer) { $product = $observer->getEvent()->getProduct(); $pId = $product->getId(); $storeId = $product->getStoreId(); if ($observer->hasDate()) { $date = new \DateTime($observer->getEvent()->getDate()); } else { $date = $this->localeDate->scopeDate($storeId); } if ($observer->hasWebsiteId()) { $wId = $observer->getEvent()->getWebsiteId(); } else { $wId = $this->storeManager->getStore($storeId)->getWebsiteId(); } if ($observer->hasCustomerGroupId()) { $gId = $observer->getEvent()->getCustomerGroupId(); } elseif ($product->hasCustomerGroupId()) { $gId = $product->getCustomerGroupId(); } else { $gId = $this->customerSession->getCustomerGroupId(); } $key = "{$date->format('Y-m-d H:i:s')}|{$wId}|{$gId}|{$pId}"; if (!$this->rulePricesStorage->hasRulePrice($key)) { $rulePrice = $this->resourceRuleFactory->create()->getRulePrice($date, $wId, $gId, $pId); $this->rulePricesStorage->setRulePrice($key, $rulePrice); } if ($this->rulePricesStorage->getRulePrice($key) !== false) { $finalPrice = min($product->getData('final_price'), $this->rulePricesStorage->getRulePrice($key)); $product->setFinalPrice($finalPrice); } return $this; }
/** * @param Collection $productCollection * @param bool $printQuery * @param bool $logQuery * @return array */ public function beforeLoad(Collection $productCollection, $printQuery = false, $logQuery = false) { if (!$productCollection->hasFlag('catalog_rule_loaded')) { $connection = $this->resource->getConnection(); $store = $this->storeManager->getStore(); $productCollection->getSelect()->joinLeft(['catalog_rule' => $this->resource->getTableName('catalogrule_product_price')], implode(' AND ', ['catalog_rule.product_id = e.entity_id', $connection->quoteInto('catalog_rule.website_id = ?', $store->getWebsiteId()), $connection->quoteInto('catalog_rule.customer_group_id = ?', $this->customerSession->getCustomerGroupId()), $connection->quoteInto('catalog_rule.rule_date = ?', $this->dateTime->formatDate($this->localeDate->scopeDate($store->getId()), false))]), [CatalogRulePrice::PRICE_CODE => 'rule_price']); $productCollection->setFlag('catalog_rule_loaded', true); } return [$printQuery, $logQuery]; }
/** * Returns catalog rule value * * @return float|boolean */ public function getValue() { if (null === $this->value) { $this->value = $this->resourceRuleFactory->create()->getRulePrice($this->dateTime->scopeDate($this->storeManager->getStore()->getId()), $this->storeManager->getStore()->getWebsiteId(), $this->customerSession->getCustomerGroupId(), $this->product->getId()); $this->value = $this->value ? floatval($this->value) : false; if ($this->value) { $this->value = $this->priceCurrency->convertAndRound($this->value); } } return $this->value; }
/** * Returns catalog rule value * * @return float|boolean */ public function getValue() { if (null === $this->value) { if ($this->product->hasData(self::PRICE_CODE)) { $this->value = floatval($this->product->getData(self::PRICE_CODE)) ?: false; } else { $this->value = $this->getRuleResource()->getRulePrice($this->dateTime->scopeDate($this->storeManager->getStore()->getId()), $this->storeManager->getStore()->getWebsiteId(), $this->customerSession->getCustomerGroupId(), $this->product->getId()); $this->value = $this->value ? floatval($this->value) : false; if ($this->value) { $this->value = $this->priceCurrency->convertAndRound($this->value); } } } return $this->value; }
/** * Retrieve query for attribute with timezone conversion * * @param string|[] $table * @param string $column * @param null|mixed $from * @param null|mixed $to * @param null|int|string|\Magento\Store\Model\Store $store * @param \Magento\Framework\DB\Adapter\AdapterInterface $connection * @return string */ public function getStoreTZOffsetQuery($table, $column, $from = null, $to = null, $store = null, $connection = null) { if (!$connection) { $connection = $this->getConnection(); } $column = $connection->quoteIdentifier($column); if (null === $from) { $selectOldest = $connection->select()->from($table, ["MIN({$column})"]); $from = $connection->fetchOne($selectOldest); } $periods = $this->_getTZOffsetTransitions($this->_localeDate->scopeDate($store)->format('e'), $from, $to); if (empty($periods)) { return $column; } $query = ""; $periodsCount = count($periods); $i = 0; foreach ($periods as $offset => $timestamps) { $subParts = []; foreach ($timestamps as $ts) { $subParts[] = "({$column} between {$ts['from']} and {$ts['to']})"; } $then = $connection->getDateAddSql($column, $offset, \Magento\Framework\DB\Adapter\AdapterInterface::INTERVAL_SECOND); $query .= ++$i == $periodsCount ? $then : "CASE WHEN " . join(" OR ", $subParts) . " THEN {$then} ELSE "; } return $query . str_repeat('END ', count($periods) - 1); }
/** * Apply catalog price rules to product in admin * * @param EventObserver $observer * @return $this */ public function processAdminFinalPrice($observer) { $product = $observer->getEvent()->getProduct(); $storeId = $product->getStoreId(); $date = $this->_localeDate->scopeDate($storeId); $key = false; $ruleData = $this->_coreRegistry->registry('rule_data'); if ($ruleData) { $wId = $ruleData->getWebsiteId(); $gId = $ruleData->getCustomerGroupId(); $pId = $product->getId(); $key = "{$date->format('Y-m-d H:i:s')}|{$wId}|{$gId}|{$pId}"; } elseif ($product->getWebsiteId() !== null && $product->getCustomerGroupId() !== null) { $wId = $product->getWebsiteId(); $gId = $product->getCustomerGroupId(); $pId = $product->getId(); $key = "{$date->format('Y-m-d H:i:s')}|{$wId}|{$gId}|{$pId}"; } if ($key) { if (!isset($this->_rulePrices[$key])) { $rulePrice = $this->_resourceRuleFactory->create()->getRulePrice($date, $wId, $gId, $pId); $this->_rulePrices[$key] = $rulePrice; } if ($this->_rulePrices[$key] !== false) { $finalPrice = min($product->getData('final_price'), $this->_rulePrices[$key]); $product->setFinalPrice($finalPrice); } } return $this; }
/** * Retrieve range expression with timezone conversion adapted for attribute * * @param string $range * @param string $attribute * @param string $tzFrom * @param string $tzTo * @return string * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ protected function _getTZRangeExpressionForAttribute($range, $attribute, $tzFrom = '+00:00', $tzTo = null) { if (null == $tzTo) { $tzTo = $this->_localeDate->scopeDate()->format('P'); } $adapter = $this->getConnection(); $expression = $this->_getRangeExpression($range); $attribute = $adapter->quoteIdentifier($attribute); $periodExpr = $adapter->getDateAddSql($attribute, $tzTo, \Magento\Framework\DB\Adapter\AdapterInterface::INTERVAL_HOUR); return str_replace('{{attribute}}', $periodExpr, $expression); }
/** * Calculate product price based on special price data and price rules * * @param float $basePrice * @param float $specialPrice * @param string $specialPriceFrom * @param string $specialPriceTo * @param bool|float|null $rulePrice * @param mixed|null $wId * @param integer|null $gId * @param int|null $productId * @return float */ public function calculatePrice($basePrice, $specialPrice, $specialPriceFrom, $specialPriceTo, $rulePrice = false, $wId = null, $gId = null, $productId = null) { \Magento\Framework\Profiler::start('__PRODUCT_CALCULATE_PRICE__'); if ($wId instanceof Store) { $sId = $wId->getId(); $wId = $wId->getWebsiteId(); } else { $sId = $this->_storeManager->getWebsite($wId)->getDefaultGroup()->getDefaultStoreId(); } $finalPrice = $basePrice; $finalPrice = $this->calculateSpecialPrice($finalPrice, $specialPrice, $specialPriceFrom, $specialPriceTo, $sId); if ($rulePrice === false) { $date = $this->_localeDate->scopeDate($sId); $rulePrice = $this->_ruleFactory->create()->getRulePrice($date, $wId, $gId, $productId); } if ($rulePrice !== null && $rulePrice !== false) { $finalPrice = min($finalPrice, $rulePrice); } $finalPrice = max($finalPrice, 0); \Magento\Framework\Profiler::stop('__PRODUCT_CALCULATE_PRICE__'); return $finalPrice; }
/** * Get if updated * * @param string $reportCode * @return string|\Magento\Framework\Stdlib\DateTime\DateInterface */ protected function _getUpdatedAt($reportCode) { $flag = $this->_reportsFlagFactory->create()->setReportFlagCode($reportCode)->loadSelf(); return $flag->hasData() ? $this->_localeDate->scopeDate(0, new \Magento\Framework\Stdlib\DateTime\Date($flag->getLastUpdate(), \Magento\Framework\Stdlib\DateTime::DATETIME_INTERNAL_FORMAT), true) : ''; }
/** * Get if updated * * @param string $reportCode * @return string|\DateTime */ protected function _getUpdatedAt($reportCode) { $flag = $this->_reportsFlagFactory->create()->setReportFlagCode($reportCode)->loadSelf(); return $flag->hasData() ? $this->_localeDate->scopeDate(0, $flag->getLastUpdate(), true) : ''; }
/** * Insert order to pdf page * * @param \Zend_Pdf_Page &$page * @param \Magento\Sales\Model\Order $obj * @param bool $putOrderId * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ protected function insertOrder(&$page, $obj, $putOrderId = true) { if ($obj instanceof \Magento\Sales\Model\Order) { $shipment = null; $order = $obj; } elseif ($obj instanceof \Magento\Sales\Model\Order\Shipment) { $shipment = $obj; $order = $shipment->getOrder(); } $this->y = $this->y ? $this->y : 815; $top = $this->y; $page->setFillColor(new \Zend_Pdf_Color_GrayScale(0.45)); $page->setLineColor(new \Zend_Pdf_Color_GrayScale(0.45)); $page->drawRectangle(25, $top, 570, $top - 55); $page->setFillColor(new \Zend_Pdf_Color_GrayScale(1)); $this->setDocHeaderCoordinates([25, $top, 570, $top - 55]); $this->_setFontRegular($page, 10); if ($putOrderId) { $page->drawText(__('Order # ') . $order->getRealOrderId(), 35, $top -= 30, 'UTF-8'); } $page->drawText(__('Order Date: ') . $this->_localeDate->formatDate($this->_localeDate->scopeDate($order->getStore(), $order->getCreatedAt(), true), \IntlDateFormatter::MEDIUM, false), 35, $top -= 15, 'UTF-8'); $top -= 10; $page->setFillColor(new \Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92)); $page->setLineColor(new \Zend_Pdf_Color_GrayScale(0.5)); $page->setLineWidth(0.5); $page->drawRectangle(25, $top, 275, $top - 25); $page->drawRectangle(275, $top, 570, $top - 25); /* Calculate blocks info */ /* Billing Address */ $billingAddress = $this->_formatAddress($this->addressRenderer->format($order->getBillingAddress(), 'pdf')); /* Payment */ $paymentInfo = $this->_paymentData->getInfoBlock($order->getPayment())->setIsSecureMode(true)->toPdf(); $paymentInfo = htmlspecialchars_decode($paymentInfo, ENT_QUOTES); $payment = explode('{{pdf_row_separator}}', $paymentInfo); foreach ($payment as $key => $value) { if (strip_tags(trim($value)) == '') { unset($payment[$key]); } } reset($payment); /* Shipping Address and Method */ if (!$order->getIsVirtual()) { /* Shipping Address */ $shippingAddress = $this->_formatAddress($this->addressRenderer->format($order->getShippingAddress(), 'pdf')); $shippingMethod = $order->getShippingDescription(); } $page->setFillColor(new \Zend_Pdf_Color_GrayScale(0)); $this->_setFontBold($page, 12); $page->drawText(__('Sold to:'), 35, $top - 15, 'UTF-8'); if (!$order->getIsVirtual()) { $page->drawText(__('Ship to:'), 285, $top - 15, 'UTF-8'); } else { $page->drawText(__('Payment Method:'), 285, $top - 15, 'UTF-8'); } $addressesHeight = $this->_calcAddressHeight($billingAddress); if (isset($shippingAddress)) { $addressesHeight = max($addressesHeight, $this->_calcAddressHeight($shippingAddress)); } $page->setFillColor(new \Zend_Pdf_Color_GrayScale(1)); $page->drawRectangle(25, $top - 25, 570, $top - 33 - $addressesHeight); $page->setFillColor(new \Zend_Pdf_Color_GrayScale(0)); $this->_setFontRegular($page, 10); $this->y = $top - 40; $addressesStartY = $this->y; foreach ($billingAddress as $value) { if ($value !== '') { $text = []; foreach ($this->string->split($value, 45, true, true) as $_value) { $text[] = $_value; } foreach ($text as $part) { $page->drawText(strip_tags(ltrim($part)), 35, $this->y, 'UTF-8'); $this->y -= 15; } } } $addressesEndY = $this->y; if (!$order->getIsVirtual()) { $this->y = $addressesStartY; foreach ($shippingAddress as $value) { if ($value !== '') { $text = []; foreach ($this->string->split($value, 45, true, true) as $_value) { $text[] = $_value; } foreach ($text as $part) { $page->drawText(strip_tags(ltrim($part)), 285, $this->y, 'UTF-8'); $this->y -= 15; } } } $addressesEndY = min($addressesEndY, $this->y); $this->y = $addressesEndY; $page->setFillColor(new \Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92)); $page->setLineWidth(0.5); $page->drawRectangle(25, $this->y, 275, $this->y - 25); $page->drawRectangle(275, $this->y, 570, $this->y - 25); $this->y -= 15; $this->_setFontBold($page, 12); $page->setFillColor(new \Zend_Pdf_Color_GrayScale(0)); $page->drawText(__('Payment Method'), 35, $this->y, 'UTF-8'); $page->drawText(__('Shipping Method:'), 285, $this->y, 'UTF-8'); $this->y -= 10; $page->setFillColor(new \Zend_Pdf_Color_GrayScale(1)); $this->_setFontRegular($page, 10); $page->setFillColor(new \Zend_Pdf_Color_GrayScale(0)); $paymentLeft = 35; $yPayments = $this->y - 15; } else { $yPayments = $addressesStartY; $paymentLeft = 285; } foreach ($payment as $value) { if (trim($value) != '') { //Printing "Payment Method" lines $value = preg_replace('/<br[^>]*>/i', "\n", $value); foreach ($this->string->split($value, 45, true, true) as $_value) { $page->drawText(strip_tags(trim($_value)), $paymentLeft, $yPayments, 'UTF-8'); $yPayments -= 15; } } } if ($order->getIsVirtual()) { // replacement of Shipments-Payments rectangle block $yPayments = min($addressesEndY, $yPayments); $page->drawLine(25, $top - 25, 25, $yPayments); $page->drawLine(570, $top - 25, 570, $yPayments); $page->drawLine(25, $yPayments, 570, $yPayments); $this->y = $yPayments - 15; } else { $topMargin = 15; $methodStartY = $this->y; $this->y -= 15; foreach ($this->string->split($shippingMethod, 45, true, true) as $_value) { $page->drawText(strip_tags(trim($_value)), 285, $this->y, 'UTF-8'); $this->y -= 15; } $yShipments = $this->y; $totalShippingChargesText = "(" . __('Total Shipping Charges') . " " . $order->formatPriceTxt($order->getShippingAmount()) . ")"; $page->drawText($totalShippingChargesText, 285, $yShipments - $topMargin, 'UTF-8'); $yShipments -= $topMargin + 10; $tracks = []; if ($shipment) { $tracks = $shipment->getAllTracks(); } if (count($tracks)) { $page->setFillColor(new \Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92)); $page->setLineWidth(0.5); $page->drawRectangle(285, $yShipments, 510, $yShipments - 10); $page->drawLine(400, $yShipments, 400, $yShipments - 10); //$page->drawLine(510, $yShipments, 510, $yShipments - 10); $this->_setFontRegular($page, 9); $page->setFillColor(new \Zend_Pdf_Color_GrayScale(0)); //$page->drawText(__('Carrier'), 290, $yShipments - 7 , 'UTF-8'); $page->drawText(__('Title'), 290, $yShipments - 7, 'UTF-8'); $page->drawText(__('Number'), 410, $yShipments - 7, 'UTF-8'); $yShipments -= 20; $this->_setFontRegular($page, 8); foreach ($tracks as $track) { $maxTitleLen = 45; $endOfTitle = strlen($track->getTitle()) > $maxTitleLen ? '...' : ''; $truncatedTitle = substr($track->getTitle(), 0, $maxTitleLen) . $endOfTitle; $page->drawText($truncatedTitle, 292, $yShipments, 'UTF-8'); $page->drawText($track->getNumber(), 410, $yShipments, 'UTF-8'); $yShipments -= $topMargin - 5; } } else { $yShipments -= $topMargin - 5; } $currentY = min($yPayments, $yShipments); // replacement of Shipments-Payments rectangle block $page->drawLine(25, $methodStartY, 25, $currentY); //left $page->drawLine(25, $currentY, 570, $currentY); //bottom $page->drawLine(570, $currentY, 570, $methodStartY); //right $this->y = $currentY; $this->y -= 15; } }
/** * Retrieve store timezone offset from UTC in the form acceptable by SQL's CONVERT_TZ() * * @param null|mixed $store * @return string */ protected function _getStoreTimezoneUtcOffset($store = null) { return $this->_localeDate->scopeDate($store)->toString(\Zend_Date::GMT_DIFF_SEP); }
/** * Retrieve time shift * * @param string $datetime * @return string */ public function timeShift($datetime) { return $this->_localeDate->scopeDate(null, $datetime, true)->setTimezone(new \DateTimeZone('UTC'))->format('Y-m-d H:i:s'); }
/** * Retrieve store timezone offset from UTC in the form acceptable by SQL's CONVERT_TZ() * * @param null|mixed $store * @return string */ protected function _getStoreTimezoneUtcOffset($store = null) { return $this->_localeDate->scopeDate($store)->format('P'); }
/** * Convert the start datetime (if set) to proper locale/timezone and return * * @return string */ public function exportStartDatetime() { $datetime = $this->getStartDatetime(); if (!$datetime || !$this->_localeDate || !$this->_store) { return ''; } $date = $this->_localeDate->scopeDate($this->_store, strtotime($datetime), true); return $date->toString($this->_localeDate->getDateTimeFormat(TimezoneInterface::FORMAT_TYPE_SHORT)); }
/** * Get object created at date affected with object store timezone * * @return \DateTime */ public function getCreatedAtStoreDate() { return $this->_localeDate->scopeDate($this->getStore(), $this->getCreatedAt(), true); }
/** * Get object created at date affected with object store timezone * * @return \Magento\Framework\Stdlib\DateTime\Date */ public function getCreatedAtStoreDate() { return $this->_localeDate->scopeDate($this->getStore(), $this->dateTime->toTimestamp($this->getCreatedAt()), true); }
/** * Get formated order created date in store timezone * * @param string $format date format type (short|medium|long|full) * @return string */ public function getCreatedAtFormated($format) { return $this->timezone->formatDate($this->timezone->scopeDate($this->getStore(), $this->getCreatedAt(), true), $format, true); }
/** * @param PriceBox $subject * @param string $result * @return string * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterGetCacheKey(PriceBox $subject, $result) { return implode('-', [$result, $this->priceCurrency->getCurrencySymbol(), $this->dateTime->scopeDate($this->scopeResolver->getScope()->getId())->format('Ymd'), $this->scopeResolver->getScope()->getId(), $this->customerSession->getCustomerGroupId()]); }