Skip to content

Commit 4dcc119

Browse files
authored
Merge pull request #137 from magebitcom/feature/MB-24789
Feature/mb 24789
2 parents 7ef6ccb + b73eda6 commit 4dcc119

File tree

15 files changed

+515
-1
lines changed

15 files changed

+515
-1
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Leanpay\Payment\Block\System\Config;
5+
6+
use Magento\Config\Block\System\Config\Form\Field;
7+
use Magento\Framework\Data\Form\Element\AbstractElement;
8+
9+
class ReadOnlySelect extends Field
10+
{
11+
/**
12+
* Retrieve element HTML markup
13+
*
14+
* @param AbstractElement $element
15+
* @return string
16+
*/
17+
protected function _getElementHtml(AbstractElement $element)
18+
{
19+
$element->setReadonly(true);
20+
return $element->getElementHtml();
21+
}
22+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Leanpay\Payment\Console;
5+
6+
use Magento\Framework\App\State as AppState;
7+
use Leanpay\Payment\Cron\RemoveExpiredPromotions;
8+
use Magento\Framework\Exception\LocalizedException;
9+
use Symfony\Component\Console\Command\Command;
10+
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Output\OutputInterface;
12+
13+
class RemoveExpiredPromotionsCommand extends Command
14+
{
15+
private AppState $appState;
16+
/**
17+
* @var RemoveExpiredPromotions
18+
*/
19+
private $removeExpiredPromotions;
20+
21+
/**
22+
* @param RemoveExpiredPromotions $removeExpiredPromotions
23+
* @param string|null $name
24+
*/
25+
public function __construct(
26+
AppState $appState,
27+
RemoveExpiredPromotions $removeExpiredPromotions,
28+
string $name = null
29+
) {
30+
$this->appState = $appState;
31+
$this->removeExpiredPromotions = $removeExpiredPromotions;
32+
parent::__construct($name);
33+
}
34+
35+
/**
36+
* @param InputInterface $input
37+
* @param OutputInterface $output
38+
* @return int
39+
* @throws LocalizedException
40+
*/
41+
protected function execute(
42+
InputInterface $input,
43+
OutputInterface $output
44+
): int
45+
{
46+
$this->appState->setAreaCode('frontend');
47+
$output->writeln('Removing Expired Promotions Started... ');
48+
$this->removeExpiredPromotions->execute();
49+
$output->writeln('Removing Expired Promotions Completed... ');
50+
return 0;
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
protected function configure(): void
57+
{
58+
$this->setName('leanpay:promotions');
59+
$this->setDescription('Removes expired Leanpay promotions.');
60+
61+
parent::configure();
62+
}
63+
}

Cron/RemoveExpiredPromotions.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Leanpay\Payment\Cron;
5+
6+
use Magento\Store\Api\StoreRepositoryInterface;
7+
use Magento\Catalog\Api\ProductRepositoryInterface;
8+
use Magento\Framework\Api\SearchCriteriaBuilder;
9+
use Psr\Log\LoggerInterface;
10+
11+
class RemoveExpiredPromotions
12+
{
13+
/**
14+
* Leanpay Product Attributes
15+
*/
16+
const LEANPAY_PRODUCT_PRIORITY = 'leanpay_product_priority';
17+
const LEANPAY_PRODUCT_TIME_BASED = 'leanpay_product_time_based';
18+
const LEANPAY_PRODUCT_START_DATE = 'leanpay_product_start_date';
19+
const LEANPAY_PRODUCT_END_DATE = 'leanpay_product_end_date';
20+
const LEANPAY_PRODUCT_VENDOR_CODE = 'leanpay_product_vendor_code';
21+
const LEANPAY_PRODUCT_EXCLUSIVE_INCLUSIVE = 'leanpay_product_exclusive_inclusive';
22+
private StoreRepositoryInterface $storeRepository;
23+
private ProductRepositoryInterface $productRepository;
24+
private SearchCriteriaBuilder $searchCriteriaBuilder;
25+
private LoggerInterface $logger;
26+
27+
public function __construct(StoreRepositoryInterface $storeRepository,
28+
ProductRepositoryInterface $productRepository,
29+
SearchCriteriaBuilder $searchCriteriaBuilder,
30+
LoggerInterface $logger)
31+
{
32+
$this->storeRepository = $storeRepository;
33+
$this->productRepository = $productRepository;
34+
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
35+
$this->logger = $logger;
36+
}
37+
38+
/**
39+
* @return void
40+
*/
41+
public function execute(): void
42+
{
43+
$allStores = $this->storeRepository->getList();
44+
$storeIds = [];
45+
foreach ($allStores as $store) {
46+
$storeIds[] = $store->getId();
47+
}
48+
49+
try {
50+
foreach ($storeIds as $storeId) {
51+
$searchCriteria = $this->searchCriteriaBuilder
52+
->addFilter('store_id', $storeId)
53+
->addFilter(self::LEANPAY_PRODUCT_END_DATE, date('Y-m-d'), 'lt')
54+
->create();
55+
$products = $this->productRepository->getList($searchCriteria)->getItems();
56+
foreach ($products as $product) {
57+
$this->resetLeanpayProductAttributes($product, $storeId);
58+
}
59+
}
60+
} catch (\Exception $e) {
61+
$this->logger->error($e->getMessage());
62+
}
63+
}
64+
65+
/**
66+
* @param $product
67+
* @param $storeId
68+
* @return void
69+
*/
70+
private function resetLeanpayProductAttributes($product, $storeId): void
71+
{
72+
$product->setStoreId($storeId);
73+
$product->setData(self::LEANPAY_PRODUCT_PRIORITY, null);
74+
$product->setData(self::LEANPAY_PRODUCT_TIME_BASED, null);
75+
$product->setData(self::LEANPAY_PRODUCT_START_DATE, null);
76+
$product->setData(self::LEANPAY_PRODUCT_END_DATE, null);
77+
$product->setData(self::LEANPAY_PRODUCT_VENDOR_CODE, null);
78+
$product->setData(self::LEANPAY_PRODUCT_EXCLUSIVE_INCLUSIVE, null);
79+
$product->save();
80+
}
81+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Leanpay\Payment\Model\Attribute\Backend;
5+
6+
use Magento\Eav\Model\Entity\Attribute\Backend\Datetime;
7+
8+
class ModifyEndDate extends Datetime
9+
{
10+
/**
11+
* Prepare date for save in DB
12+
*
13+
* Modify the time part to 23:59:59
14+
*
15+
* @param string|int|\DateTimeInterface $date
16+
* @return string|null
17+
*/
18+
public function formatDate($date): string | null
19+
{
20+
// Use parent method to handle date formatting
21+
$formattedDate = parent::formatDate($date);
22+
23+
if (empty($date)) {
24+
return $formattedDate;
25+
}
26+
27+
// Adjust the time portion to 23:59:59
28+
return $this->modifyTimeToEndOfDay($formattedDate);
29+
}
30+
31+
/**
32+
* Modify time part to 23:59:59
33+
*
34+
* @param string $formattedDate
35+
* @return string
36+
*/
37+
private function modifyTimeToEndOfDay($formattedDate)
38+
{
39+
$dateTime = new \DateTime($formattedDate);
40+
$dateTime->setTime(23, 59, 59);
41+
return $dateTime->format('Y-m-d H:i:s');
42+
}
43+
}

Model/Config/Backend/Cron.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace Leanpay\Payment\Model\Config\Backend;
4+
5+
use Magento\Framework\App\Config\ScopeConfigInterface;
6+
use Magento\Framework\App\Config\Storage\WriterInterface;
7+
8+
class Cron extends \Magento\Framework\App\Config\Value
9+
{
10+
const CRON_STRING_PATH = 'payment/leanpay/promotion_cron';
11+
12+
/**
13+
* @var WriterInterface
14+
*/
15+
private \Magento\Framework\App\Config\Storage\WriterInterface $configWriter;
16+
17+
/**
18+
* @var \Magento\Framework\App\Config\ValueFactory
19+
*/
20+
protected $_configValueFactory;
21+
22+
23+
/**
24+
* @param \Magento\Framework\Model\Context $context
25+
* @param \Magento\Framework\Registry $registry
26+
* @param \Magento\Framework\App\Config\ScopeConfigInterface $config
27+
* @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
28+
* @param \Magento\Framework\App\Config\ValueFactory $configValueFactory
29+
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
30+
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
31+
* @param array $data
32+
*/
33+
public function __construct(
34+
\Magento\Framework\App\Config\Storage\WriterInterface $configWriter,
35+
\Magento\Framework\Model\Context $context,
36+
\Magento\Framework\Registry $registry,
37+
\Magento\Framework\App\Config\ScopeConfigInterface $config,
38+
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
39+
\Magento\Framework\App\Config\ValueFactory $configValueFactory,
40+
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
41+
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
42+
array $data = []
43+
) {
44+
$this->configWriter = $configWriter;
45+
$this->_configValueFactory = $configValueFactory;
46+
parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
47+
}
48+
49+
/**
50+
* Cron settings after save
51+
*
52+
* @return $this
53+
* @throws \Magento\Framework\Exception\LocalizedException
54+
*/
55+
public function afterSave()
56+
{
57+
$groups = $this->getData('groups');
58+
$enabled = 0;
59+
if (isset($groups['aropd']['fields']['module_enabled']['value'])) {
60+
$enabled = (int) $groups['aropd']['fields']['module_enabled']['value'];
61+
}
62+
$time = $this->getValue();
63+
$time = explode(',', $time);
64+
65+
if ($enabled) {
66+
$cronExprArray = [
67+
(int) $time[1], # Minute
68+
(int) $time[0], # Hour
69+
'*', # Day of the Month
70+
'*', # Month of the Year
71+
'*', # Day of the Week
72+
];
73+
$cronExprString = join(' ', $cronExprArray);
74+
} else {
75+
$cronExprString = '';
76+
}
77+
78+
try {
79+
$this->configWriter->save(self::CRON_STRING_PATH, $cronExprString, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0);
80+
} catch (\Exception $e) {
81+
throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t save the Cron expression.'));
82+
}
83+
return parent::afterSave();
84+
}
85+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Leanpay\Payment\Model\Config\Source;
5+
6+
use Magento\Framework\Data\OptionSourceInterface;
7+
8+
/**
9+
* PromotionDataRemovalOptions
10+
*/
11+
class PromotionDataRemovalOptions implements OptionSourceInterface
12+
{
13+
/**
14+
* @return array
15+
*/
16+
public function toOptionArray(): array
17+
{
18+
return [
19+
['value' => 'daily', 'label' => __('Daily')],
20+
];
21+
}
22+
}

0 commit comments

Comments
 (0)