[MOD] Duplicate inventory depletion

Modules, Add-ons and custom code that's more than just a quick hack or Mod.
Post Reply
Martin
Site Admin
Site Admin
Posts: 1854
Joined: Wed Jun 17, 2009 6:30 pm
Location: South Yorkshire UK
Contact:

[MOD] Duplicate inventory depletion

Post by Martin »

I've been promising this mod' for a VERY long time but today has been a catch up and do day so here's the fix.

Note: Having reviewed the code in the last 24 hours I suspect I can break this out into a specific function and use it in other parts of the ordering code so that it will stop duplication of email notifications as well as other problems. It's something I'm going to probably play with in the future so make sure you watch this thread.

Overall this just uses a physical file instead of the database to act as a flag to indicate whether the order has already had its inventory depleted or not. For some reason all attempts to use the database and existing flags in there failed to resolve the problem... I suspect code duplication but couldn't locate it (in over a year!) so this works and I'm using it...



Create a directory/folder called "inventory" in the /cache/ folder and make sure it has the same permissions as the cache folder


Open: /lib/orders.php

Find:

Code: Select all

function DecreaseInventoryFromOrder($orderId)
{
	$order = GetOrder($orderId, false);
	if (!$order) {
		return false;
	}
After, Add:

Code: Select all

// MOD Further attempts to stop multiple inventory updates

	$invStatusDone = false;
	$invLockFile = ISC_CACHE_DIRECTORY .'inventory/'.$orderId;
	
	clearstatcache();
	$invLastLock = file_exists($invLockFile) ? filemtime($invLockFile) : 0;
	
	// Older than 60 seconds, update
	if($invLastLock <= (time() - 60)) {
	
		// Try to get lock
		$fp = fopen(($invLockFile), "w+");
		if(flock($fp, LOCK_EX)) {
			ftruncate($fp, 0);
			fwrite($fp, time());
		}
		// Failed so something else got the lock and is updating inventory
		else {
			$invStatusDone = true;
		}
		// Unlock the file
		@flock($fp, LOCK_UN);
		
		// Close off the file handle
		@fclose($fp);
	}
	else {
		$invStatusDone = true;
	}

	
	/*
	 * If we can't get file lock or the inventory has already been updated in the 
	 * last 60 seconds we return without updating the inventory further
	 * 
	 * This SHOULD help avoid the inventory depletion duplication issue.
	 */
	if($invStatusDone === true) {
		$GLOBALS['ISC_CLASS_LOG']->LogSystemDebug(
			'php', 
			"Inventory status done or being resolved. Repeat depletion (avoided)", 
			"[File ".__FILE__." / Line".__LINE__."] Original code would have repeated inventory depletion <br />\nOrder: Order #{$order['orderid']}");
		return true;
	}
	
// MOD END Further attempts to stop multiple inventory updates	


Payment: If you have been waiting, praying, begging for this fix then please make sure you donate to a suitable charity (Royal British Legion, Red Cross/Crescent, Cancer Research UK... whatever) by way of a pay it forward... This took two years to track down and nail so suitable karmic pass it on would be much appreciated. Thanks...

@Interspire... You can FREELY release this to your customers with PROPER attribution and recognition only.
pbanks
Posts: 8
Joined: Sat Mar 05, 2011 10:35 pm
Location: Ireland

Re: [MOD] Duplicate inventory depletion

Post by pbanks »

Thanks ... that's helped a great deal.
noticz
Posts: 19
Joined: Wed Aug 10, 2011 3:40 am
Location: SLC, Utah

Re: [MOD] Duplicate inventory depletion

Post by noticz »

Simple and easy fix, good job! Another fix would be to just change inventory to update when it is shipped. Since you figured out the fix I might end up switching this to your code since it is kind of a pain. If we have orders waiting to ship on the table and go to update the inventory with one of the same products that is on the table then the inventory is off the second we ship that item.
Post Reply