Skip to content

Commit

Permalink
NTR: add config for stock reset
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalij Mik committed Nov 21, 2024
1 parent cca6cbc commit 2fba008
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 42 deletions.
7 changes: 5 additions & 2 deletions Components/Basket/Basket.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,11 @@ private function getArticleVoucherType(\Shopware\Models\Order\Basket $basketItem

/** @var Detail $article */
$article = $articles[0];

if (!method_exists($article->getAttribute(), 'getMollieVoucherType')) {
$attribute = $article->getAttribute();
if ($attribute === null) {
return '';
}
if (!method_exists($attribute, 'getMollieVoucherType')) {
$this->logger->warning('Method getMollieVoucherType is not existing in Article Attributes. Please clear your cache!');
return VoucherType::NONE;
}
Expand Down
12 changes: 11 additions & 1 deletion Components/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public function autoResetStock()
{
$valueStr = $this->get('auto_reset_stock', 'no');

return $this->dataTypes->getBoolValue($valueStr);
return $this->dataTypes->getBoolValue($valueStr) && !$this->reduceStockOnPayment();
}

/**
Expand Down Expand Up @@ -359,6 +359,16 @@ public function cancelFailedOrders()
return $this->dataTypes->getBoolValue($valueStr);
}

/**
* @return bool
*/
public function reduceStockOnPayment()
{
$valueStr = $this->get('reduce_stock_on_paid', 'no');

return $this->dataTypes->getBoolValue($valueStr);
}

/**
* @return int
*/
Expand Down
2 changes: 2 additions & 0 deletions Components/Config/ConfigExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public function getShopConfigArray($shop)
'updateOrderStatus' => $this->config->updateOrderStatus(),
'ordersShipOnStatus' => $this->config->getOrdersShipOnStatus(),
'cancelFailedOrders' => $this->config->cancelFailedOrders(),
'reduceStockOnPayment' => $this->config->reduceStockOnPayment(),
'resetInvoiceAndShipping' => $this->config->resetInvoiceAndShipping(),
'autoResetStock' => $this->config->autoResetStock(),
'shippedStatus' => $this->config->getShippedStatus(),
Expand Down Expand Up @@ -212,6 +213,7 @@ private function getHumanReadableLabel($key)
'updateOrderStatus' => 'Update Order Status Automatically Enabled',
'shippedStatus' => 'Order Status For Shipped Orders',
'cancelFailedOrders' => 'Cancel Failed Orders Enabled',
'reduceStockOnPayment' => 'Reduce stock after successful payment',
'resetInvoiceAndShipping' => 'Cancellation Of All Amounts Enabled',
'autoResetStock' => 'Reset Stock On Failed Payment Enabled',
'ordersShipOnStatus' => 'Automatic Shipping Status',
Expand Down
20 changes: 13 additions & 7 deletions Components/Services/StockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MollieShopware\Components\Services;

use Enlight_Components_Db_Adapter_Pdo_Mysql;
use MollieShopware\Components\Config;
use MollieShopware\MollieShopware;
use Psr\Log\LoggerInterface;
use Shopware\Bundle\CartBundle\CartPositionsMode;
Expand All @@ -23,16 +24,23 @@ class StockService
*/
private $db;

/**
* @var Config
*/
private $config;

/**
* @param OrderService $orderService
* @param Enlight_Components_Db_Adapter_Pdo_Mysql $db
* @param LoggerInterface $logger
* @param mixed $config
*/
public function __construct($orderService, $db, $logger)
public function __construct($orderService, $config, $db, $logger)
{
$this->orderService = $orderService;
$this->db = $db;
$this->logger = $logger;
$this->config = $config;
}


Expand All @@ -43,15 +51,13 @@ public function __construct($orderService, $db, $logger)
*/
public function updateOrderStocks($orderId, $reset = true)
{
if (!$this->config->reduceStockOnPayment()) {
$this->logger->debug('Reduce stock on payment config is disabled');
return;
}
$order = $this->orderService->getOrderById($orderId);
$paymentMethodName = $order->getPayment()->getName();

$isMolliePayment = strpos($paymentMethodName, MollieShopware::PAYMENT_PREFIX) !== false;

if (! $isMolliePayment) {
$this->logger->info('Order Payment does not belong to mollie', ['paymentMethodName' => $paymentMethodName]);
return;
}
if ($order->getPaymentStatus()->getId() === Status::PAYMENT_STATE_COMPLETELY_PAID) {
$this->logger->info('Order is already paid, stock updates not needed', ['orderId' => $orderId]);
return;
Expand Down
77 changes: 63 additions & 14 deletions Resources/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,55 @@
</options>
</element>

<element type="select" scope="shop">
<name>reduce_stock_on_paid</name>
<label lang="en">Reduce stock after successful payment</label>
<label lang="de">Lagerbestand nach erfolgreicher Zahlung reduzieren</label>
<label lang="nl-NL">Verminder de voorraad na succesvolle betaling</label>
<label lang="es-ES">Reducir el stock después del pago exitoso</label>
<label lang="fr-FR">Réduire le stock après un paiement réussi</label>
<label lang="it-IT">Ridurre le scorte dopo il pagamento andato a buon fine</label>
<value>yes</value>
<description lang="en">Reduce the stock after successfull pyment. This option make the option "reset stock on failed payment".</description>
<description lang="de">Reduzieren Sie den Lagerbestand nach erfolgreicher Zahlung. Diese Option macht die Option "Lagerbestand bei Fehler zurücksetzen" obsolet.</description>
<description lang="nl-NL">Verminder de voorraad na succesvolle betaling. Deze optie maakt de optie "voorraad terugzetten bij mislukte betaling" overbodig.</description>
<description lang="es-ES">Reduzca el stock después del pago exitoso. Esta opción hace que la opción "restablecer stock en caso de pago fallido" obsoleta.</description>
<description lang="fr-FR">Réduisez le stock après un paiement réussi. Cette option rend obsolète l'option "réinitialiser le stock en cas d'échec de paiement".</description>
<description lang="it-IT">Ridurre lo stock dopo aver effettuato con successo il pagamento. Questa opzione rende obsoleta l'opzione "ripristina lo stock in caso di pagamento non riuscito".</description>
<store>
<option>
<value>inherited</value>
<label lang="en">Inherited</label>
<label lang="de">Vererbt</label>
<label lang="nl-NL">Geërfd</label>
<label lang="es-ES">Heredada</label>
<label lang="fr-FR">Hérité</label>
<label lang="it-IT">Ereditato</label>
</option>
<option>
<value>yes</value>
<label lang="en">Yes</label>
<label lang="de">Ja</label>
<label lang="nl-NL">Ja</label>
<label lang="es-ES">sí</label>
<label lang="fr-FR">Oui</label>
<label lang="it-IT">sì</label>
</option>
<option>
<value>no</value>
<label lang="en">No</label>
<label lang="de">Nein</label>
<label lang="nl-NL">Nee</label>
<label lang="es-ES">no</label>
<label lang="fr-FR">non</label>
<label lang="it-IT">no</label>
</option>
</store>
<options>
<position>17</position>
</options>
</element>


<element type="select" scope="shop">
<name>reset_invoice_shipping</name>
Expand Down Expand Up @@ -622,7 +671,7 @@
</option>
</store>
<options>
<position>17</position>
<position>18</position>
</options>
</element>

Expand Down Expand Up @@ -672,7 +721,7 @@
</option>
</store>
<options>
<position>18</position>
<position>19</position>
</options>
</element>

Expand All @@ -692,7 +741,7 @@
<description lang="it-IT">L'ordine viene automaticamente contrassegnato come spedito in Mollie se viene effettuata una transizione a questo stato.</description>
<store>Shopware.apps.Base.store.OrderStatus</store>
<options>
<position>19</position>
<position>20</position>
</options>
</element>

Expand Down Expand Up @@ -742,7 +791,7 @@
</option>
</store>
<options>
<position>20</position>
<position>21</position>
</options>
</element>

Expand Down Expand Up @@ -791,7 +840,7 @@
</option>
</store>
<options>
<position>21</position>
<position>22</position>
</options>
</element>
<element type="button" required="false">
Expand All @@ -803,7 +852,7 @@
<label lang="fr-FR">--------------------------------------------------------------------------------------- E-Mails ---------------------------------------------------------------------------------------</label>
<label lang="it-IT">--------------------------------------------------------------------------------------- E-Mails ---------------------------------------------------------------------------------------</label>
<options>
<position>22</position>
<position>23</position>
</options>
</element>

Expand Down Expand Up @@ -846,7 +895,7 @@
</option>
</store>
<options>
<position>23</position>
<position>24</position>
</options>
</element>

Expand All @@ -859,7 +908,7 @@
<label lang="fr-FR">--------------------------------------------------------------------------------------- Carte de crédit ---------------------------------------------------------------------------------------</label>
<label lang="it-IT">--------------------------------------------------------------------------------------- Carta di credito ---------------------------------------------------------------------------------------</label>
<options>
<position>24</position>
<position>25</position>
</options>
</element>

Expand All @@ -873,7 +922,7 @@
<label lang="it-IT">Abilita componenti</label>
<value>true</value>
<options>
<position>25</position>
<position>26</position>
</options>
</element>

Expand All @@ -887,7 +936,7 @@
<label lang="it-IT">Abilita lo stile per i componenti</label>
<value>true</value>
<options>
<position>26</position>
<position>27</position>
</options>
</element>

Expand All @@ -900,7 +949,7 @@
<label lang="fr-FR">--------------------------------------------------------------------------------------- Apple Pay ---------------------------------------------------------------------------------------</label>
<label lang="it-IT">--------------------------------------------------------------------------------------- Apple Pay ---------------------------------------------------------------------------------------</label>
<options>
<position>27</position>
<position>28</position>
</options>
</element>

Expand Down Expand Up @@ -947,7 +996,7 @@
<valueField>id</valueField>
<displayField>name</displayField>
<isCustomStore>true</isCustomStore>
<position>28</position>
<position>29</position>
</options>
</element>

Expand All @@ -960,7 +1009,7 @@
<label lang="fr-FR">--------------------------------------------------------------------------------------- Bancaire ---------------------------------------------------------------------------------------</label>
<label lang="it-IT">--------------------------------------------------------------------------------------- Trasferimento bancario ---------------------------------------------------------------------------------------</label>
<options>
<position>29</position>
<position>30</position>
</options>
</element>

Expand All @@ -973,7 +1022,7 @@
<label lang="fr-FR">Délai de paiement (jours)</label>
<label lang="it-IT">Termine di pagamento (Giorni)</label>
<options>
<position>30</position>
<position>31</position>
</options>
</element>

Expand Down
3 changes: 2 additions & 1 deletion Resources/services/components/mixed.xml
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@
</service>

<service id="mollie_shopware.components.services.stock_service" class="MollieShopware\Components\Services\StockService" >
<argument id="mollie_shopware.order_service" type="service"/>
<argument type="service" id="mollie_shopware.order_service"/>
<argument type="service" id="mollie_shopware.config"/>
<argument type="service" id="db"/>
<argument type="service" id="mollie_shopware.logger"/>
</service>
Expand Down
25 changes: 8 additions & 17 deletions Subscriber/StockSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

use Enlight\Event\SubscriberInterface;
use Enlight_Event_EventArgs;
use MollieShopware\Components\Constants\PaymentStatus;
use Enlight_Hook_HookArgs;
use MollieShopware\Components\Services\StockService;
use MollieShopware\Events\Events;
use Shopware\Models\Order\Order;
use Shopware\Models\Order\Status;

class StockSubscriber implements SubscriberInterface
{
Expand All @@ -25,7 +24,7 @@ public static function getSubscribedEvents()
{
return [
'Shopware_Modules_Order_SaveOrder_ProcessDetails' => 'resetStocks',
Events::UPDATE_ORDER_PAYMENT_STATUS => 'increaseStocks'
'sOrder::setPaymentStatus::after' => 'increaseStocks'
];
}

Expand All @@ -35,22 +34,14 @@ public function resetStocks(Enlight_Event_EventArgs $eventArgs)
$this->stockService->updateOrderStocks($orderId);
}

public function increaseStocks(Enlight_Event_EventArgs $eventArgs)
public function increaseStocks(Enlight_Hook_HookArgs $eventArgs)
{
$paymentStatus = $eventArgs->get('molliePaymentStatus');
$successStatus = [
PaymentStatus::MOLLIE_PAYMENT_PAID,
PaymentStatus::MOLLIE_PAYMENT_COMPLETED
];

if (!in_array($paymentStatus, $successStatus)) {
$orderId = (int)$eventArgs->get('orderId');
$paymentStatusId = (int)$eventArgs->get('paymentStatusId');
if ($paymentStatusId !== Status::PAYMENT_STATE_COMPLETELY_PAID) {
return;
}
/**
* @var Order $order
*/
$order = $eventArgs->get('order');

$this->stockService->updateOrderStocks($order->getId(), false);
$this->stockService->updateOrderStocks($orderId, false);
}
}
4 changes: 4 additions & 0 deletions Tests/PHPUnit/Components/Config/ConfigExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class ConfigExporterTest extends TestCase
const TEST_VALUE_TEST_API_KEY_VALIDATION = 'Invalid';

const TEST_VALUE_STATUS_NAME = 'Status';
const TEST_VALUE_REDUCE_STOCK_ON_PAYMENT = false;

/**
* @var EntityManagerInterface
Expand Down Expand Up @@ -74,6 +75,7 @@ public function setUp(): void
'updateOrderStatus' => self::TEST_VALUE_UPDATE_ORDER_STATUS,
'getOrdersShipOnStatus' => self::TEST_VALUE_ORDERS_SHIP_ON_STATUS,
'cancelFailedOrders' => self::TEST_VALUE_CANCEL_FAILED_ORDERS,
'reduceStockOnPayment' => self::TEST_VALUE_REDUCE_STOCK_ON_PAYMENT,
'resetInvoiceAndShipping' => self::TEST_VALUE_RESET_INVOICE_AND_SHIPPING,
'autoResetStock' => self::TEST_VALUE_AUTO_RESET_STOCK,
'getShippedStatus' => self::TEST_VALUE_SHIPPED_STATUS,
Expand Down Expand Up @@ -234,6 +236,7 @@ private function getConfigArrayWithTestValues()
'updateOrderStatus' => self::TEST_VALUE_UPDATE_ORDER_STATUS,
'ordersShipOnStatus' => self::TEST_VALUE_ORDERS_SHIP_ON_STATUS,
'cancelFailedOrders' => self::TEST_VALUE_CANCEL_FAILED_ORDERS,
'reduceStockOnPayment' => self::TEST_VALUE_REDUCE_STOCK_ON_PAYMENT,
'resetInvoiceAndShipping' => self::TEST_VALUE_RESET_INVOICE_AND_SHIPPING,
'autoResetStock' => self::TEST_VALUE_AUTO_RESET_STOCK,
'shippedStatus' => self::TEST_VALUE_SHIPPED_STATUS,
Expand Down Expand Up @@ -263,6 +266,7 @@ private function getHumanReadableConfigTextWithTestValues()
'Update Order Status Automatically Enabled' => self::TEST_VALUE_UPDATE_ORDER_STATUS ? 'Yes' : 'No',
'Automatic Shipping Status' => self::TEST_VALUE_STATUS_NAME,
'Cancel Failed Orders Enabled' => self::TEST_VALUE_CANCEL_FAILED_ORDERS ? 'Yes' : 'No',
'Reduce stock after successful payment' => self::TEST_VALUE_REDUCE_STOCK_ON_PAYMENT ? 'Yes' : 'No',
'Cancellation Of All Amounts Enabled' => self::TEST_VALUE_RESET_INVOICE_AND_SHIPPING ? 'Yes' : 'No',
'Reset Stock On Failed Payment Enabled' => self::TEST_VALUE_AUTO_RESET_STOCK ? 'Yes' : 'No',
'Order Status For Shipped Orders' => self::TEST_VALUE_STATUS_NAME,
Expand Down

0 comments on commit 2fba008

Please sign in to comment.