Jump to content

Bulk Stock Updater


viper151

Recommended Posts

How many times you've been wondering why do I have to go into different products all the time to update the stock? Well for me it's a common problem since I do not resell VPS but dedicated servers so I have to restock manually each time. So I wrote one small addon module that helps me with this procedure. In this addon module only the products you want will show up. Not all of them.

 

Now with WHMCS 4.4.2 it's much easier to use addons but this hasn't been tested to WHMCS 4.4.2

Be aware that I'm not a hard core programmer so the code might be messy and unsecure so feel free to contribute

 

Instructions:

1. So the first thing that you'll need is to create a folder in modules/admin called bulk_stock_updater

2. Create 3 files with names bulk_stock_updater.php,step1.php,step2.php or grab the three files attached and put them in the modules/admin/bulk_stock_updater folder. (I told you I'm not hardcore coder so I had to come up with another way than the if(_post(posted)==Yes) I can find in other addons modules since it wasn't working for me)

3. Configure the 3 files as follows:

4.For bulk_stock_updater.php

   if (!$action)
       include("step1.php");
   elseif ($action=="step2")
       include("step2.php");?>

 

 

 

5. For step1.php you'll have to change some things.

<?php	

if (!defined("WHMCS"))
die("This file cannot be accessed directly");



$query = "SELECT * FROM tblproducts";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
?>

<div class="tablebg" align="center">
<table class="datatable" width="40%" border="0" cellspacing="1" cellpadding="3">
	<tr><th width="120">Server</th><th>Stock</th><th>Quantity</th></tr>
	<?php
	$i=0;
	$n=0;
	while ($i<$num){
		$name=mysql_result($result,$i,"name");
		$stockcontrol=mysql_result($result,$i,"stockcontrol");
		$qty=mysql_result($result,$i,"qty");
		$id=mysql_result($result,$i,"id");?>
		<tr>
		<?php
		if ($stockcontrol=="on"){?>
			<form name="input" action="addonmodules.php?module=bulk_stock_updater&action=step2" method="post">
			<input type="hidden" name="id[]" value="<? echo $id ?>">
			<td><input type="hidden" name="name[]" value="<?php print $name?>"  /><?php print $name?> </td>
			<input type="hidden" name="stockcontrolhidden[]" value="<?php print $stockcontrol?>"  />
			<input type="hidden" name="qtyhidden[]" value="<?php print $qty?>"  />
			<td><select name="stockcontrol[]" style="background-color:red;" ><option value="on">on</option>
			<option value=''>off</option>
			</select> </td>
			<td><input type="text" name="qty[]" value="<?php print $qty?>" size="3"/></td>
			<?php $n = $n + 1;
		} 
		else{
			if (($name=="Server Name 1") &&  ($stockcontrol=="")|| ($name=="Server Name 1") &&  ($stockcontrol==""))
				{?>
				<form name="input" action="addonmodules.php?module=bulk_stock_updater&action=step2" method="post">
				<input type="hidden" name="id[]" value="<? echo $id ?>">
				<td><input type="hidden" name="name[]" value="<?php print $name?>"/><?php print $name?> </td>
				<input type="hidden" name="stockcontrolhidden[]" value="<?php print $stockcontrol?>"  />
				<input type="hidden" name="qtyhidden[]" value="<?php print $qty?>"  />
				<td><select name="stockcontrol[]" ><option value=''>off</option>
				<option value="on">on</option>
				</select> </td>
				<td><input type="text" name="qty[]" value="<?php print $qty?>" size="3"/></td>
				<?php $n = $n + 1;
				}
		}
	$i++;
	?>
	</tr>
	<?php
	}?>
	<tr><td colspan="3">
           <input type="hidden" name="n" value="<?php print $n ?>">
           <input type="submit" value="submit" name="submit">
          </td>	</td></tr>
         </form>
</table>
</div>
<?php echo "Found $n records" ?>	
</body>
</html> 

Ok here where it says if (($name=="Server Name 1") && ($stockcontrol=="")|| ($name=="Server Name 2") && ($stockcontrol=="")) you change the Server Name 1 and 2 with the name of the servers you usually change the stock. and add as many as you want.

Step1 will first check which servers have stock control checked on and at the same time will give you the servers that you want.

 

6. Step 2 will show you the number and the changes you've made.

<html><body>
<?php

$query = "SELECT * FROM tblproducts";
$result=mysql_query($query);
$num=mysql_numrows($result);

?> <?php

$i=0;
$n=0;
while ($i<$num){

$name[$i] = $_POST["name"][$i];
$stockcontrol[$i] = $_POST["stockcontrol"][$i];
$qty[$i] = $_POST["qty"][$i];
$id[$i] = $_POST["id"][$i];

$stockcontrolhidden[$i] = $_POST["stockcontrolhidden"][$i];
$qtyhidden[$i] = $_POST["qtyhidden"][$i];
if ($stockcontrol[$i]==$stockcontrolhidden[$i] && $qty[$i]==$qtyhidden[$i]){}
	else{
	$n++;
		if ($stockcontrol[$i]=="on"){
			echo "$name[$i] stockcontrol was <strong>OFF</strong> and turned to <strong>ON</strong> and quantity was <strong>$qtyhidden[$i]</strong> and became <strong>$qty[$i]</strong>";
			mysql_query("UPDATE tblproducts SET qty='$qty[$i]' ,stockcontrol='on' WHERE id='$id[$i]'");?><br/><?php
		}
		else{
			echo "$name[$i] stockcontrol was <strong>ON</strong> and turned to <strong>OFF</strong> and quantity was <strong>$qtyhidden[$i]</strong> and became <strong>$qty[$i]</strong>";
			mysql_query(" UPDATE tblproducts SET qty='$qty[$i]' ,stockcontrol = ''  WHERE id='$id[$i]'");?><br/><?php
		}
	}


$i = $i + 1;
}
echo "$n records changed";
mysql_close();
?>
<br/>
Redirecting in 2 secs
<meta HTTP-EQUIV="REFRESH" content="2; url=addonmodules.php?module=bulk_stock_updater">

</body></html>

Here make sure to change the redirection time in meta tag (content="2; ) I've set it to 2 seconds since that suits me but for some people might be harsh to check everything in 2 seconds.

 

 

Please feel free to leave a feedback here.

bulk_stock_updater.zip

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated