Skip to content

Commit

Permalink
- fixed handling when trying to exceed stock
Browse files Browse the repository at this point in the history
- added iso-messages in frontend
- system messages now list individual products out of stock
  • Loading branch information
bennyborn committed Apr 20, 2021
1 parent 6dead8f commit c33225d
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 43 deletions.
73 changes: 52 additions & 21 deletions classes/SimpleERP.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@
/**
* Contao Open Source CMS
*
* Copyright (c) 2005-2017 Leo Feyer
* Copyright (c) 2005-2021 Leo Feyer
*
* @package Isotope Simple ERP
* @author Benny Born <[email protected]>
* @author Michael Bösherz <[email protected]>
* @license LGPL
* @copyright 2017 numero2 - Agentur für Internetdienstleistungen
* @copyright 2021 numero2 - Agentur für digitales Marketing
*/


namespace numero2\IsotopeSimpleERP;

use Contao\System;
use Isotope\Interfaces\IsotopeProductCollection;
use Isotope\Message;
use Isotope\Model\Config;
use Isotope\Model\Product;
use Isotope\Model\ProductCollection;
use Isotope\Model\ProductCollection\Order;
use Isotope\Model\Product;


class SimpleERP extends \System {
class SimpleERP extends System {


/**
Expand Down Expand Up @@ -67,23 +70,29 @@ public function updateProductCount( IsotopeProductCollection $objOrder ) {
*
* @return boolean
*/
public function checkQtyForCollection( Product $objProduct, $intQuantity, IsotopeProductCollection $objCollection ) {
public function checkQtyForCollection( Product $objProduct, $intQuantity=0, IsotopeProductCollection $objCollection ) {

if( $objProduct->simple_erp_count > 0 ) {

// check if we want to add more than we have in stock
if( $intQuantity > $objProduct->simple_erp_count ) {
return 0;
}

// find product in cart to check if the total quantity exceeds our stock
$oInCart = NULL;
$oInCart = $objCollection->getItemForProduct($objProduct);

if( $oInCart && $oInCart->quantity >= $objProduct->simple_erp_count ) {
return 0;
}
if( $oInCart && ($oInCart->quantity+$intQuantity) >= $objProduct->simple_erp_count ) {

$qtyAddToCart = $objProduct->simple_erp_count-$oInCart->quantity;
$qtyAddToCart = $qtyAddToCart<0?0:$qtyAddToCart;

if( !$qtyAddToCart ) {
Message::addError(sprintf(
$GLOBALS['TL_LANG']['ERR']['simpleErpQuantityNotAvailable']
, $objProduct->getName()
, $objProduct->simple_erp_count
));
}

return $qtyAddToCart;
}
}

return $intQuantity;
Expand All @@ -101,9 +110,20 @@ public function checkQtyForCollection( Product $objProduct, $intQuantity, Isotop
*/
public function updateQtyInCollection($objItem, $arrSet, $objCart) {

if( $objItem->getProduct()->simple_erp_count > 0 ) {
if( array_key_exists('quantity', $arrSet) && $arrSet['quantity'] && $arrSet['quantity'] > $objItem->getProduct()->simple_erp_count ) {
$arrSet['quantity'] = $objItem->getProduct()->simple_erp_count;
$objProduct = null;
$objProduct = $objItem->getProduct();

if( $objProduct->simple_erp_count > 0 ) {

if( array_key_exists('quantity', $arrSet) && $arrSet['quantity'] && $arrSet['quantity'] > $objProduct->simple_erp_count ) {

$arrSet['quantity'] = $objProduct->simple_erp_count;

Message::addError(sprintf(
$GLOBALS['TL_LANG']['ERR']['simpleErpQuantityNotAvailable']
, $objProduct->getName()
, $objProduct->simple_erp_count
));
}
}

Expand All @@ -120,11 +140,22 @@ public function getSystemMessages() {

$this->import('Database');

$objUnavailProducts = NULL;
$objUnavailProducts = $this->Database->prepare("SELECT COUNT(id) as num_unavail FROM ".Product::getTable()." WHERE simple_erp_disable_on_zero = '1' AND simple_erp_count = '0'")->execute();
$aMessages = [];

$oProducts = null;
$oProducts = Product::findBy(['published=?','simple_erp_count=?'],[1,'0']);

if( $objUnavailProducts->num_unavail ) {
return '<p class="tl_error">' . sprintf($GLOBALS['TL_LANG']['MSC']['simple_erp_non_avail'], $objUnavailProducts->num_unavail) . '</p>';
if( $oProducts ) {

while( $oProducts->next() ) {

$aMessages[] = '<p class="tl_error">' . sprintf(
$GLOBALS['TL_LANG']['MSC']['simpleErpProductOutOfStock']
, $oProducts->name
) . '</p>';
}
}

return implode('',$aMessages);
}
}
}
5 changes: 3 additions & 2 deletions config/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
/**
* Contao Open Source CMS
*
* Copyright (c) 2005-2017 Leo Feyer
* Copyright (c) 2005-2021 Leo Feyer
*
* @package Isotope Simple ERP
* @author Benny Born <[email protected]>
* @author Michael Bösherz <[email protected]>
* @license LGPL
* @copyright 2017 numero2 - Agentur für Internetdienstleistungen
* @copyright 2021 numero2 - Agentur für digitales Marketing
*/


Expand Down
15 changes: 8 additions & 7 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
/**
* Contao Open Source CMS
*
* Copyright (c) 2005-2017 Leo Feyer
* Copyright (c) 2005-2021 Leo Feyer
*
* @package Isotope Simple ERP
* @author Benny Born <[email protected]>
* @author Michael Bösherz <[email protected]>
* @license LGPL
* @copyright 2017 numero2 - Agentur für Internetdienstleistungen
* @copyright 2021 numero2 - Agentur für digitales Marketing
*/


/**
* Hooks
* HOOKS
*/
$GLOBALS['ISO_HOOKS']['postCheckout'][] = array('numero2\IsotopeSimpleERP\SimpleERP', 'updateProductCount');
$GLOBALS['ISO_HOOKS']['addProductToCollection'][] = array('numero2\IsotopeSimpleERP\SimpleERP', 'checkQtyForCollection');
$GLOBALS['ISO_HOOKS']['updateItemInCollection'][] = array('numero2\IsotopeSimpleERP\SimpleERP','updateQtyInCollection');
$GLOBALS['TL_HOOKS']['getSystemMessages'][] = array('numero2\IsotopeSimpleERP\SimpleERP', 'getSystemMessages');
$GLOBALS['ISO_HOOKS']['postCheckout'][] = ['numero2\IsotopeSimpleERP\SimpleERP', 'updateProductCount'];
$GLOBALS['ISO_HOOKS']['addProductToCollection'][] = ['numero2\IsotopeSimpleERP\SimpleERP', 'checkQtyForCollection'];
$GLOBALS['ISO_HOOKS']['updateItemInCollection'][] = ['numero2\IsotopeSimpleERP\SimpleERP','updateQtyInCollection'];
$GLOBALS['TL_HOOKS']['getSystemMessages'][] = ['numero2\IsotopeSimpleERP\SimpleERP', 'getSystemMessages'];
16 changes: 9 additions & 7 deletions dca/tl_iso_product.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
/**
* Contao Open Source CMS
*
* Copyright (c) 2005-2017 Leo Feyer
* Copyright (c) 2005-2021 Leo Feyer
*
* @package Isotope Simple ERP
* @author Benny Born <[email protected]>
* @author Michael Bösherz <[email protected]>
* @license LGPL
* @copyright 2017 numero2 - Agentur für Internetdienstleistungen
* @copyright 2021 numero2 - Agentur für digitales Marketing
*/


Expand All @@ -18,19 +19,20 @@
$GLOBALS['TL_DCA']['tl_iso_product']['fields']['simple_erp_count'] = array(
'label' => &$GLOBALS['TL_LANG']['tl_iso_product']['simple_erp_count'],
'exclude' => true,
'search' => true,
'filter' => true,
'sorting' => true,
'inputType' => 'text',
'eval' => array( 'mandatory'=>false, 'tl_class'=>'w50' ),
'attributes' => array( 'legend'=>'general_legend', 'multilingual'=>false, 'fe_sorting'=>true ),
'eval' => [ 'mandatory'=>false, 'tl_class'=>'w50' ],
'attributes' => [ 'legend'=>'general_legend', 'multilingual'=>false, 'fe_sorting'=>true ],
'sql' => "varchar(255) NOT NULL default ''",
);

$GLOBALS['TL_DCA']['tl_iso_product']['fields']['simple_erp_disable_on_zero'] = array(
'label' => &$GLOBALS['TL_LANG']['tl_iso_product']['simple_erp_disable_on_zero'],
'exclude' => true,
'filter' => true,
'inputType' => 'checkbox',
'eval' => array( 'tl_class'=>'w50' ),
'attributes' => array( 'legend'=>'general_legend' ),
'eval' => [ 'tl_class'=>'w50' ],
'attributes' => [ 'legend'=>'general_legend' ],
'sql' => "char(1) NOT NULL default ''",
);
10 changes: 7 additions & 3 deletions languages/de/default.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
<xliff version="1.1">
<file datatype="php" original="system/modules/isotope_simple_erp/languages/en/default.php" source-language="en" target-language="de">
<body>
<trans-unit id="MSC.simple_erp_non_avail">
<source>There are %s products currently not available anymore</source>
<target>Sie haben %s Produkt(e) die aktuell nicht mehr verfügbar sind</target>
<trans-unit id="MSC.simpleErpProductOutOfStock">
<source>The product "%s" is currently out of stock</source>
<target>Das Produkt "%s" ist aktuell nicht verfügbar</target>
</trans-unit>
<trans-unit id="ERR.simpleErpQuantityNotAvailable">
<source>The maximum available quantity for "%s" is %s items.</source>
<target>Die maximal verfügbare Bestellmenge für "%s" ist %s Stück.</target>
</trans-unit>
</body>
</file>
Expand Down
8 changes: 5 additions & 3 deletions languages/en/default.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
<xliff version="1.1">
<file datatype="php" original="system/modules/isotope_simple_erp/languages/en/default.php" source-language="en">
<body>
<trans-unit id="MSC.simple_erp_non_avail">
<source>There are %s products currently not available anymore</source>
<target>There are %s products currently not available anymore</target>
<trans-unit id="MSC.simpleErpProductOutOfStock">
<source>The product "%s" is currently out of stock</source>
</trans-unit>
<trans-unit id="ERR.simpleErpQuantityNotAvailable">
<source>The maximum available quantity for "%s" is %s items.</source>
</trans-unit>
</body>
</file>
Expand Down

0 comments on commit c33225d

Please sign in to comment.