Skip to content

Commit 34e362e

Browse files
[PT-967] Improve cron job functionality (#97)
Co-authored-by: Ivan Pugach <ivan.pugach@ext.mondu.ai>
1 parent 48aebf9 commit 34e362e

File tree

6 files changed

+129
-9
lines changed

6 files changed

+129
-9
lines changed

Helpers/BulkActions.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,34 @@ private function bulkSyncAction($order, $_additionalData)
154154
*/
155155
private function bulkShipAction($order, $additionalData)
156156
{
157+
if ($this->configProvider->getCronOrderStatus() &&
158+
$this->configProvider->getCronOrderStatus() !== $order->getStatus()
159+
) {
160+
$this->monduFileLogger->info(
161+
'Order '. $order->getIncrementId() .
162+
' is not in '. $this->configProvider->getCronOrderStatus(). ' status yet. Skipping',
163+
);
164+
165+
return null;
166+
}
167+
157168
$withLineItems = $additionalData['withLineItems'] ?? false;
158169
$monduLogData = $this->getMonduLogData($order);
170+
171+
if ($monduLogData['mondu_state'] === 'shipped') {
172+
$this->monduFileLogger->info(
173+
'Order '. $order->getIncrementId() .
174+
' is already in state shipped. Skipping',
175+
);
176+
return null;
177+
}
178+
159179
$this->monduFileLogger->info(
160180
'Order ' . $order->getIncrementId() .
161181
' Trying to create invoice, entering shipOrder'
162182
);
163183

164-
if (!$this->configProvider->isInvoiceRequiredForShipping()) {
184+
if (!$this->configProvider->isInvoiceRequiredCron()) {
165185
return $this->shipOrderWithoutInvoices($order);
166186
} elseif ($monduInvoice = $this->shipOrder($monduLogData, $order, $withLineItems)) {
167187
$this->monduFileLogger->info(

Model/Ui/ConfigProvider.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,26 @@ public function isCronEnabled(): bool
179179
return (bool) $this->scopeConfig->getValue('payment/mondu/cron');
180180
}
181181

182+
/**
183+
* Order status for which the Cron job will process the order
184+
*
185+
* @return string
186+
*/
187+
public function getCronOrderStatus(): ?string
188+
{
189+
return $this->scopeConfig->getValue('payment/mondu/cron_status');
190+
}
191+
192+
/**
193+
* Is Invoice required for processing the order by the Cron job
194+
*
195+
* @return bool
196+
*/
197+
public function isInvoiceRequiredCron(): bool
198+
{
199+
return (bool) $this->scopeConfig->getValue('payment/mondu/cron_require_invoice');
200+
}
201+
182202
/**
183203
* Get invoice url for order
184204
*

Setup/Patch/Data/CronJobPatch.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
namespace Mondu\Mondu\Setup\Patch\Data;
3+
4+
use Magento\Framework\Setup\ModuleDataSetupInterface;
5+
use Magento\Framework\Setup\Patch\DataPatchInterface;
6+
7+
class CronJobPatch implements DataPatchInterface
8+
{
9+
/**
10+
* @var ModuleDataSetupInterface
11+
*/
12+
private $moduleDataSetup;
13+
14+
/**
15+
* @param ModuleDataSetupInterface $moduleDataSetup
16+
*/
17+
public function __construct(
18+
ModuleDataSetupInterface $moduleDataSetup
19+
) {
20+
$this->moduleDataSetup = $moduleDataSetup;
21+
}
22+
23+
/**
24+
* @inheritdoc
25+
*/
26+
public function apply()
27+
{
28+
$this->moduleDataSetup->getConnection()->startSetup();
29+
30+
$previousValue = $this->moduleDataSetup->getConnection()->fetchOne('SELECT `value` FROM core_config_data WHERE path = "payment/mondu/require_invoice"');
31+
32+
if ($previousValue) {
33+
$this->moduleDataSetup
34+
->getConnection()
35+
->delete('core_config_data', ['path = "payment/mondu/cron_require_invoice"']);
36+
37+
$this->moduleDataSetup->getConnection()->insert('core_config_data', [
38+
'scope' => 'default',
39+
'scope_id' => 0,
40+
'path' => 'payment/mondu/cron_require_invoice',
41+
'value' => $previousValue,
42+
]);
43+
}
44+
45+
$this->moduleDataSetup->getConnection()->endSetup();
46+
}
47+
48+
/**
49+
* @inheritdoc
50+
*/
51+
public static function getDependencies()
52+
{
53+
return [];
54+
}
55+
56+
/**
57+
* @inheritdoc
58+
*/
59+
public function getAliases()
60+
{
61+
return [];
62+
}
63+
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "mondu_gmbh/magento2-payment",
33
"description": "Mondu payment method for magento 2",
44
"type": "magento2-module",
5-
"version": "2.3.1",
5+
"version": "2.3.2",
66
"license": [
77
"MIT"
88
],

etc/adminhtml/system.xml

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,19 +115,36 @@
115115
<source_model>Magento\Directory\Model\Config\Source\Country</source_model>
116116
<config_path>payment/mondu/specificcountry</config_path>
117117
</field>
118-
<field id="cron" translate="label" type="select" sortOrder="16" showInDefault="1" showInWebsite="1"
119-
showInStore="1">
120-
<label>Enable automatic order processing</label>
121-
<config_path>payment/mondu/cron</config_path>
122-
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
123-
</field>
124118
<field id="debug" translate="label" type="select" sortOrder="17" showInDefault="1" showInWebsite="1"
125119
showInStore="1">
126120
<label>Enable Logs</label>
127121
<config_path>payment/mondu/debug</config_path>
128122
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
129123
</field>
130124
</group>
125+
<group id="monducron" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="0"
126+
showInStore="0">
127+
<label>Cron configuration</label>
128+
129+
<field id="cron" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0"
130+
showInStore="0">
131+
<label>Enable Cron job for sending invoices to Mondu</label>
132+
<config_path>payment/mondu/cron</config_path>
133+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
134+
</field>
135+
136+
<field id="monducron_status" translate="label" type="select" sortOrder="2" showInDefault="1" showInWebsite="0" showInStore="0">
137+
<label>Order status in which order will be processed by the Cron job</label>
138+
<config_path>payment/mondu/cron_status</config_path>
139+
<source_model>Magento\Sales\Model\Config\Source\Order\Status</source_model>
140+
</field>
141+
142+
<field id="monducron_requireinvoice" translate="label" type="select" sortOrder="2" showInDefault="1" showInWebsite="0" showInStore="0">
143+
<label>Require Invoice document for processing orders</label>
144+
<config_path>payment/mondu/cron_require_invoice</config_path>
145+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
146+
</field>
147+
</group>
131148
</group>
132149
</section>
133150
</system>

etc/crontab.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" ?>
22
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
33
<group id="default">
4-
<job instance="Mondu\Mondu\Cron\Cron" method="execute" name="mondu_cron_test">
4+
<job instance="Mondu\Mondu\Cron\Cron" method="execute" name="mondu_cron">
55
<schedule>*/30 * * * *</schedule>
66
</job>
77
</group>

0 commit comments

Comments
 (0)