stock control allowing negative numbers

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

stock control allowing negative numbers

Post 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);
}
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: stock control allowing negative numbers

Post 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?
(#10850)
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: stock control allowing negative numbers

Post by jonnyfortis »

what would you suggest to make it less obtuse.

the if statements work
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: stock control allowing negative numbers

Post 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."
(#10850)
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: stock control allowing negative numbers

Post 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?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: stock control allowing negative numbers

Post 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.
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: stock control allowing negative numbers

Post 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);
Post Reply