Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PISHPW-232: reset stocks after complete payment #431

Merged
merged 4 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
95 changes: 95 additions & 0 deletions Components/Services/StockService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

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;
use Shopware\Models\Order\Status;

class StockService
{
/**
* @var OrderService
*/
private $orderService;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var Enlight_Components_Db_Adapter_Pdo_Mysql
*/
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, $config, $db, $logger)
{
$this->orderService = $orderService;
$this->db = $db;
$this->logger = $logger;
$this->config = $config;
}


/**
* @param int $orderId
* @param mixed $reset
* @return void
*/
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);

$this->logger->debug('Start to reset the stocks for order', ['orderId' => $orderId]);

foreach ($order->getDetails() as $orderDetail) {
$articleNumber = $orderDetail->getArticleNumber();

if ($orderDetail->getPrice() < 0) {
$this->logger->debug('Price is lower than 0, this product was not updated in first place', ['orderId' => $orderId, 'articleNumber' => $articleNumber, 'priceNumeric' => $orderDetail->getPrice()]);
continue;
}
if (! in_array($orderDetail->getMode(), [CartPositionsMode::PRODUCT, CartPositionsMode::PREMIUM_PRODUCT], true)) {
$this->logger->debug('Order detail does not have a regular product', ['orderId' => $orderId, 'articleNumber' => $articleNumber, 'mode' => $orderDetail->getMode()]);
continue;
}
$quantity = $orderDetail->getQuantity();

$this->runStockUpdate($quantity, $articleNumber, $reset);
}

$this->logger->debug('Resetted all product stocks', ['orderId' => $orderId]);
}

private function runStockUpdate($quantity, $articleNumber, $reset = true)
{
$sqL = 'UPDATE s_articles_details
SET instock = instock + :quantity
WHERE ordernumber = :number';

if ($reset === false) {
$sqL = 'UPDATE s_articles_details
SET instock = instock - :quantity
WHERE ordernumber = :number';
}

$this->db->executeUpdate($sqL, [':quantity' => $quantity, ':number' => $articleNumber]);
}
}
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
7 changes: 7 additions & 0 deletions Resources/services/components/mixed.xml
Original file line number Diff line number Diff line change
Expand Up @@ -201,5 +201,12 @@
<argument type="service" id="mollie_shopware.components.logger"/>
</service>

<service id="mollie_shopware.components.services.stock_service" class="MollieShopware\Components\Services\StockService" >
<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>

</services>
</container>
5 changes: 5 additions & 0 deletions Resources/services/subscriber.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,10 @@
<tag name="shopware.event_subscriber"/>
</service>

<service class="MollieShopware\Subscriber\StockSubscriber" id="mollie_shopware.subscriber.stock_subscriber">
<argument type="service" id="mollie_shopware.components.services.stock_service"/>
<tag name="shopware.event_subscriber"/>
</service>

</services>
</container>
Loading
Loading