Page 1 of 1

stock control allowing negative numbers

Posted: Mon Jun 15, 2015 7:42 am
by jonnyfortis
having a issue with an online cart. Items are being sold, being deducted of the stock amount but somehow more are being allowed to be sold and therfore getting getting minus numbers.

To start. I have a statement on the product detail page saying that if the stock is 0 dont show that product in the output

Code: Select all

website_Stock.Stock != 0
then in the checkout i have another statement that checks to quantity and if the request is more the user gets a prompt

in the head

Code: Select all

$XC_invalidItems = "";
$XC_lim = "";
$iCol = array_search("Quantity",${$XCName}["ColNames"]);
$iSrc = array_search("Stock",${$XCName}["ColNames"]);
for ($iVal=0; $iVal<sizeof(${$XCName}["contents"][0]); $iVal++) {
  if (${$XCName}["contents"][$iCol][$iVal] > ${$XCName}["contents"][$iSrc][$iVal]) {
    if ($XC_invalidItems != "") $XC_invalidItems .= "-";
    $XC_invalidItems .= (string) ($iVal+1);
    if ($XC_lim != "") $XC_lim .= "-";
    $XC_lim .= (string) ${$XCName}["contents"][$iSrc][$iVal];
  }
}
if ($XC_invalidItems != "") {
  $XC_invalidItems = "(".$XC_invalidItems.")";
  $XC_lim = "(".$XC_lim.")";
  $x_limErr = "The item(s) $XC_invalidItems exceed the available stock of $XC_lim";
}
i have a form value

Code: Select all

<input type="hidden" name="XC_Limit" value="<?php echo @$x_limErr; ?>" />
then the error echoed

Code: Select all

<?php if (isset($XC_limErr)) echo $XC_limErr; ?>
on the stock update page it is updated like

Code: Select all

if ($paymentStatus == 'Completed')  {  
// UPDATE THE DATABASE      

$oldStock = $row_rsUpdateStock['Stock'];
$stockSold = $row_rsUpdateStock['Quantity'];
$newStock = $oldStock - $stockSold;
$SKU = $row_rsUpdateStock['SKU'];

$UpdateQuery="UPDATE website_Stock SET Stock = '$newStock' WHERE website_Stock.SKU = '$SKU'";

$result = mysql_query($UpdateQuery);
}

Re: stock control allowing negative numbers

Posted: Mon Jun 15, 2015 11:36 am
by Christopher
That code is a little obtuse, but the problem is probably around:

Code: Select all

  if (${$XCName}["contents"][$iCol][$iVal] > ${$XCName}["contents"][$iSrc][$iVal]) {
Have you check each step of those if()'s to make sure it is doing what you want it to do?

Re: stock control allowing negative numbers

Posted: Mon Jun 15, 2015 1:14 pm
by jonnyfortis
what would you suggest to make it less obtuse.

the if statements work

Re: stock control allowing negative numbers

Posted: Mon Jun 15, 2015 4:11 pm
by Christopher
jonnyfortis wrote:what would you suggest to make it less obtuse.
Clearer naming or comments to describe what things like ${$XCName}["contents"][0] are? Just the fact that you have code full of things like ${$XCName}["contents"][0] is troubling...
jonnyfortis wrote:the if statements work
How can the logic work, "but somehow more are being allowed to be sold and therfore getting getting minus numbers."

Re: stock control allowing negative numbers

Posted: Tue Jun 16, 2015 7:54 am
by jonnyfortis
Christopher wrote:
jonnyfortis wrote:what would you suggest to make it less obtuse.
Clearer naming or comments to describe what things like ${$XCName}["contents"][0] are? Just the fact that you have code full of things like ${$XCName}["contents"][0] is troubling...
jonnyfortis wrote:the if statements work
How can the logic work, "but somehow more are being allowed to be sold and therfore getting getting minus numbers."
the trouble is this was pre written script that i purchased a few years ago. and it appears sometimes it works but sometimes slipping through the next and as soon as it gets to -1 then people can keep purchasing

have you got any suggestions how i can redo it?

Re: stock control allowing negative numbers

Posted: Tue Jun 16, 2015 8:10 am
by Celauran
Instead of relying on some convoluted array, you might consider performing a stock lookup on the item when it's being added to the cart and again when the purchase is being made.

Re: stock control allowing negative numbers

Posted: Tue Jun 16, 2015 8:20 am
by jonnyfortis
Celauran wrote:Instead of relying on some convoluted array, you might consider performing a stock lookup on the item when it's being added to the cart and again when the purchase is being made.
I thought this is what i am doing prior to adding the item to the cart. the item is not showing if the stock level is 0 i have now added >=0 to the statement

Code: Select all

$var1_rsProduct = "-1";
if (isset($_GET['ItemID'])) {
  $var1_rsProduct = $_GET['ItemID'];
}

mysql_select_db($database_SS15, $SS15);
$query_rsProduct = sprintf("SELECT * FROM wesbite_Cat, wesbite_products, wesbite_Stock, wesbite_SizeList WHERE wesbite_products.catID = wesbite_Cat.catID AND wesbite_products.ItemID = wesbite_Stock.ItemID AND wesbite_Stock.SizeID = wesbite_SizeList.SizeID AND wesbite_products.ItemID = %s AND wesbite_Stock.Stock >= 0 AND wesbite_Stock.Stock != 0 ORDER BY wesbite_SizeList.SizeID", GetSQLValueString($var1_rsProduct, "int"));
$rsProduct = mysql_query($query_rsProduct, $SS15) or die(mysql_error());
$row_rsProduct = mysql_fetch_assoc($rsProduct);
$totalRows_rsProduct = mysql_num_rows($rsProduct);