Jump to content

FIFO method for stock valuation


funbinod

Recommended Posts

i'm trying to write some line for stock valuation with FIFO method. in the internet somewhere i found with STACK &/or QUEUE method it might be done. but i couldn't understand how it might be done. can anyone please suggest me if STACK &/or QUEUE are the real solution. if no suggest me something better and if yes help me understand these functions..

tahnks in advance.....

Link to comment
Share on other sites

  • Replies 57
  • Created
  • Last Reply

Top Posters In This Topic

thanks both of u.

i think every account method to be applied in php we need certain data structure also, isn't it @davej? or i might be wrong! :(

 

and @justsomeguy! the link is dead. its not opening.

Link to comment
Share on other sites

All FIFO means is that you have decided to tell the government that for tax purposes the shares of stock that you sold were the most recent ones that you bought. If you bought 100 shares of GOOG every January for the past ten years and then you sell some shares you have to declare the holding period because different holding periods may be subject to different tax rates.

Link to comment
Share on other sites

i'm not calculating SHARE STOCK. i'm calculating INVENTORY STOCK.

and i know the definition and manual procedure of FIFO. i'm trying to find the solution for this on php...

Link to comment
Share on other sites

I think it is the same type of thing. You bought 100 crates of widgets every month for various prices and sold N crates of widgets for various prices. Now you need to declare the value of your current inventory and your profit. For FIFO you presume to have always sold the least recently received crate.

Link to comment
Share on other sites

yes @davej! but from which path, in php, i can reach to that calculation!? question is this!

 

and @justsomeguy! its still not opening for me

 

 

 

Oops! Google Chrome could not connect to matthewturland.com

Try reloading: matthewturland.­com/­2010/­05/­20/­new-­spl-­features-­in-­php-­5-­3/­

Link to comment
Share on other sites

  • 1 month later...

hi all!

am back with some try. please have a look and suggest.

 

what i need to calculate is the amount starting from the last row of purchase table.

if the $cqty is less than or equal to 'qty' on the last row then it should select the 'qty' and 'amt' from it. else if it is greater it should select one more row up (i.e. last two rows) and check if it matches the required case (i.e $cqty <= 'sum(qty)') and if true, select the sum of 'qty' and 'amt'.

 

it should keep on moving upwards until it finds the required condition.

but i couldn't make the loop for this. please help me to do this according the following.....

$sid = 1;$cqty = 5; // this value is just for test and may change with the increase in transactions....$sql = mysqli_query($connect, "select qty, amt from purchase where sid=$sid order by date desc limit 1");while ($result = mysqli_fetch_object($sql)) {	$qty = $result->qty;	if ($cqty <= $qty) {		$amt = $result->amt;		echo $camt;	} else if ($cqty > $qty) {		$sql0 = mysqli_query($connect, "select sum(qty)qty, sum(amt)amt from purchase where sid=$sid order by date desc limit 2");		while ($result0 = mysqli_fetch_object($sql0)) {			$qty = $result0->qty;			if ($cqty <= $qty) {				$amt = $result0->amt;				$rate = $amt / $qty;				echo $amt.':'.$rate.':'.$qty.'<br />';				echo $rate * $cqty.'<br />';			}		}	}}
Link to comment
Share on other sites

because there are multiple rows with different quantity and rate on different dates. and i just want to select the rows descending to meet the condition $cqty<= sum(qty).

let me explain with this.....

purchase table

date qty rate amt

2014-01-01 3 20 60

2014-01-02 5 21 106

2014-01-03 3 22 66

 

now if $cqty is less than 3 i should select only last row, and if it is between 4 and 8, i should select last 2 rows. and if it is between 6 and 11 i should select all three rows and likewise.....

Link to comment
Share on other sites

I don't use MySqli but I started to try to work out the code for it -- but there is a logical problem -- if you are matching the items that have been sold against the items that were purchased then you will also need to record what items have been so processed. For example if you have a purchase line of 1000 widgets and then later you do inventory and only 2 widgets were sold, you would need to record the fact that 998 of the widgets on that purchase line are still in inventory. This gets particularly annoying and confusing if you process that same line again and again over time with very few items getting sold. Each time you will only decrement the "remaining" count for that one line. Here I am thinking that a possible solution might be to add a column to the table that would record the REMAINING cnt of items on that line that have not been sold and are still in inventory, but this introduces some confusion. It might be better to build another table to record this information so that you are not reading and writing to the same table as you process it -- it might be best to create and write to a temporary table. Then afterwards the temporary table could be processed to fill in the REMAINING column of the purchase table.

Link to comment
Share on other sites

yes! i had made logic to this also. but there is also the same problem.

lets imagine the same example data i gave above.

 

purchase table

date qty rate amt

2014-01-01 3 20 60

2014-01-02 5 21 106

2014-01-03 3 22 66

in this, if i sold '4' units then how can i subtract from the rows to get remaining units? i've to subtract '3' units from first row and '1' unit from second row. and if i sell '10' units then i need to subtract '3' from first, '5' from second and '2' from third. this should keep on looping. and the problem for me is how can i loop the process till i get the required condition?

i've thought of another table also, but not temporary, to record and update with each purchases and sales. first insert purchase record and after each sales update the rows by subtracting the units. and the problem is same....

i've made logic to the process. but i couldn't make logic with php...

:(

Edited by funbinod
Link to comment
Share on other sites

purchase table

date qty rate amt

2014-01-01 3 20 60

2014-01-02 5 21 106

2014-01-03 3 22 66

If you sold 4 units the table might read...

purchase table

date qty rate amt instock

2014-01-01 3 20 60 0

2014-01-02 5 21 106 4

2014-01-03 3 22 66 3

Later if you sold 3 more units...

purchase table

date qty rate amt instock

2014-01-01 3 20 60 0

2014-01-02 5 21 106 1

2014-01-03 3 22 66 3

Link to comment
Share on other sites

i understood that. it can be done in the same table or on a separate table. but what i'm having trouble is doing this with php. i'm done already with just making logic.. but how can i do this with php...? :(

Link to comment
Share on other sites

Well, here is what I had come up with. It does not include the code to update the table. I think the updates should be done to another table or done later. Also I am not a knowledgeable user of MySQLi, so this code probably contains errors...

$connect = mysqli_connect("url", "user", "password", "database");if (!$connect){    $error = mysqli_connect_error();    $errno = mysqli_connect_errno();    echo "$errno: $errorn";    exit();}$sid = 1;  // this is the stock identifier$cqty = 5; // this is the qty sold$totqty = 0;$totamt = 0;$sql = "select qty, amt, remain from purchase where sid=? and remain!=? order by date desc";$stmt = mysqli_prepare($connect, $sql);if (mysqli_stmt_bind_param($stmt, 'ii', $sid, 0)){  mysqli_stmt_execute($stmt) or die("Error: " . mysqli_error($connect));  $result = mysqli_stmt_get_result($stmt);  while ($row = mysqli_stmt_fetch_object($result) && $totqty<$cqty) {        if ( !is_null($row->remain) ){            if ($row->remain>0 && $row->remain+$totqty > $cqty){            $diff = $cqty - $totqty;             $totqty = $cqty;             $totamt += $diff*$row->amt/$row->qty;            //            //            //            // now you must record the remain count = $row->remain-$diff            //            //            //        }else{             $totqty += $row->remain;             $totamt += $row->remain*$row->amt/$row->qty;            //            //            //            // now you must record the remain=0            //            //            //        }    }elseif ($row->qty+$totqty > $cqty){        $diff = $cqty-$totqty;         $totqty = $cqty;         $totamt += $diff*$row->amt/$row->qty;        //        //        //        // now you must record the remain count = $row->qty-$diff        //        //        //    }else{         $totqty += $row->qty;         $totamt += $row->amt;        //        //        //        // now you must record the remain=0        //        //        //     }  }  if ($cqty != $totqty){      echo 'error: recorded qty purchased is insufficient';  }}else{  echo 'error: could not bind';}mysqli_stmt_close($stmt);mysqli_close($connect);
Link to comment
Share on other sites

thanks for the reply.

but i'm completely beyond understanding what u suggested. sorry! will u please explain the complete process? coz only solving the problem is not my aim. i want to understand it..

Link to comment
Share on other sites

moreover: its saying

Fatal error: Only variables can be passed by reference in E:xampphtdocsactest6.php on line 22

 

for the line

if (mysqli_stmt_bind_param($stmt, 'ii', $sid, 0)){

(sorry i know less about bind param)

Link to comment
Share on other sites

Oh, I was wondering about that. Change it to...

$sql = "select qty, amt, remain from purchase where sid=? and remain>0 order by date desc";$stmt = mysqli_prepare($connect, $sql);if (mysqli_stmt_bind_param($stmt, 'i', $sid)){

Note that this code presumes that the purchase table has a column named remain.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...