Buy/Sell profit tracking via FIFO stack in PHP - php

I need to implement an algorythm that can keep track of profits for a list of individual item transactions and update the 'profit' table. The idea is to fetch items bought and cross-reference them with items sold, purely by order (i.e no individual instancing of individual items exists, there are only IDs for item types). So, say we have a table with this data:
items:
item id item type
1 red box
2 blue box
3 white box
transactions:
item id -- quantity -- transaction_type -- price_unit($) -- time
1 3 buy 20 2015-10-10
3 1 buy 10 2015-10-11
1 1 sell 25 2015-10-12
1 1 sell 20 2015-11-14
3 1 sell 15 2015-11-13
For this example my profit table should look like:
item_id -- quantity -- profit -- timestamp_bought ---timestamp_sold
1 1 (25-20)=5 2015-10-10 2015-10-12
1 1 (20-20)=0 2015-10-10 2015-11-14
3 1 (15-10)=5 2015-10-11 2015-11-13
This is what I have so far. It works until several different buy/sell quantities come into play...
$buy_list = mysqli_query($con, "SELECT * FROM transaction WHERE
transaction_type = 'Buy' ORDER BY time DESC");
$sell_list = mysqli_query($con, "SELECT * FROM transaction WHERE
transaction_type = 'Sell' ORDER BY time DESC");
while($row = mysqli_fetch_array($buy_list))
{
array_push($buy_stack, array(
$row['idtrans'],
$row['iditem'],
$row['quantity'],
$row['time'],
$row['price_unit']));
}
while($row = mysqli_fetch_array($sell_list))
{
array_push($sell_stack, array(
$row['idtrans'],
$row['iditem'],
$row['quantity'],
$row['time'],
$row['price_unit']));
}
$size_buy = sizeof($buy_stack);
$size_sell = sizeof($sell_stack);
for($i=0; $i<=$size_buy-1; $i++) //iterate BUY orders
{
$idtrans_b = $buy_stack[$i][0];
$itemid_b = $buy_stack[$i][1];
$quantity_b = $buy_stack[$i][2];
$time_b = $buy_stack[$i][3];
$price_unit_b = $buy_stack[$i][4];
$quantity_b_calc = $buy_stack[$i][2];
for($k=0; $k<=$size_sell-1; $k++)
{
$idtrans_s = $sell_stack[$k][0];
$itemid_s = $sell_stack[$k][1];
$quantity_s = $sell_stack[$k][2];
$time_s = $sell_stack[$k][3];
$price_unit_s = $sell_stack[$k][4];
if($itemid_s == $itemid_b &&
$time_s > $time_b &&
$quantity_b > 0
&& $quantity_s >= $quantity_b) //match
{
$sell_stack[$k][1] = "done_sell";
$buy_stack[$i][1] = "done_buy";
$profit_unit = $price_unit_s - $price_unit_b;
$profit_ quantity = min($quantity_s, $quantity_b);
$quantity_b = $quantity_b - $profit_quantity;
$time_bought = $buy_stack[$i][3];
$time_sold = $sell_stack[$k][3];
$add_profit = mysqli_query
($con, "INSERT INTO profit (id, transaction_buy, transaction_sell, profit_unit, quantity, timestamp_buy, timestamp_sell)
VALUES ( 'null', '$idtrans_b', '$idtrans_s', '$profit_unit', '$profit_quantity', $time_bought, $time_sold));
}
}
}
I'm stuck to get this working properly. Should I scrap this and do it with stacks instead?

Related

Fifo inventory in codeigniter

I'm new to CodeIgniter. I'm trying to update the quantity of the table based on FIFO. I'm making a stock take page, if we count the physical item less than what the table has, we will reconcile and this will update the inventory table and the discard table. Updating inventory table is easy and I've done but I want to update the discard table's quantity based on FIFO. What I want is for example the quantity of item A in inventory table is 50 but when we count it physically it's 35, so when we reconcile, it should minus 15 from the discard table quantity.
Items
Qty
Date
Item A
5
2022-8-22
Item A
4
2022-8-22
Item A
9
2022-8-22
After update it should be like:
| Items | Qty | Date |
|:--------|:------:| --------:|
| Item A | 0 | 2022-8-22|
| Item A | 0 | 2022-8-22|
| Item A | 3 | 2022-8-22|
Please help me. If you need further clarification, do let me know. Thanks in advance
public function update_complete(){
$stock_diff = $this->input->post('stock_diff');
$counted_stock_value = $this->input->post('counted_stock_value');
$cost_diff = $this->input->post('cost_diff');
$reconcile = $this->input->post('reconcile');
$barcode = $this->input->post('barcode');
$stk_id = $this->input->post('stk_id');
for ($i = 0; $i < count($barcode); $i++)
{
$data = array(
'stocktake_value' => $counted_stock_value[$i],
'stock_diff' => $stock_diff[$i],
'cost_diff' => $cost_diff[$i],
'status' => $reconcile[$i]
);
// Commented for testing
$this->db->where('stk_id', $stk_id);
$this->db->where('barcode', $barcode[$i]);
$this->db->update('stocktake_trans', $data);
$itemResult = $this->item_model->getStockdiscardTransByBarcodeNew($barcode[$i]);
foreach($itemResult as $item)
{
$rest = $stock_diff[$i];
if($rest > 0)
{
if($item['quantity'] >= $rest)
{
$rest = $item['quantity']-=$rest;
//$rest = 0;
echo $rest;
}
else
{
$item['quantity'] = 0;
$rest = $rest - $item['quantity'];
}
$this->db->where('barcode', $barcode[$i]);
$this->db->update('stockdiscard_trans', array('quantity' => $rest));
}
}
}
// Update Stock Take
$data1 = array(
'reconciled_by' => $this->session->userdata('name'),
'reconciled_at' => date('Y-m-d')
);
$this->db->where('id', $stk_id);
$this->db->update('stocktake', $data1);
// Set message
$this->session->set_flashdata('stocktake_reconciled', 'Stock Take has been Reconciled!');
//redirect('stocktake/complete');
}
I understand you want to store the difference between the old stock and the new stock.
You can store the original quantity in a variable, update the product with the new value, and then do the difference between the original quantity and the new quantity. Finally, you store the difference in the discard table.
Is what I understood correct?
Edit 1:
Something like this?
$itemsModel = model('App\Models\itemsModel');
$totalDB = $itemsModel->where('Items', 'Item A')->select('sum(Qty)')->first(); // let say 50
$totalMag = 35;
$rest = $totalDB - $totalMag; // rest 50-35 = 15
$itemResult = $itemsModel->where('Items', 'Item A')->findAll();
foreach($itemResult as $item)
{
if($rest > 0)
{
if($item->Qty >= $rest)
{
$item->Qty -= $rest;
$rest = 0;
}
else
{
$item->Qty = 0;
$rest = $rest - $item->Qty;
}
$item->save();
}
}
Backtesting:
-remaining qty 15
5 >= 15? - FALSE
Qty = 0
Rest = 10
4 >= 10 FALSE
Qty = 0
Rest = 6
9 >= 6 TRUE
Qty = 9-6 = 3
Rest = 0
But you need to manage, for what will happen if the remaining stock of products will be more than 0 and the quantity of products A will be 0.
Lets say we have remaining qty to 19
5 >= 19? False
Qty = 0
Rest = 14
4 >= 14? False
Qty = 0
Rest = 10
9 >= 10? False
Qty = 0
Rest = 1
What will happen for the rest = 1 when all item in our table have 0 Qty?

Subtract sum of two columns of two unrelated tables in mysql database in php

I have two unrelated tables with an amount column. I want to to sum the amount from the two columns and subtract the two sums, all this is done in an API I am working on.
case 'amountleft':
$warehouse = $_POST['warehouse'];
$stmt = $conn->prepare("SELECT SUM(price) AS amount FROM purchases WHERE warehouse = ?");
$stmttwo = $conn->prepare("SELECT SUM(amountrequested) AS amounttwo FROM fundruequest WHERE warehouse = ?");
$stmt->bind_param("s",$warehouse);
$stmttwo->bind_param("s",$warehouse);
$stmt->execute();
$stmttwo->execute();
$stmt->store_result();
$stmttwo->store_result();
if($stmt->num_rows > 0 && $stmttwo->num_rows > 0){
$stmt->bind_result($amount);
$stmttwo->bind_result($amounttwo);
$stmt->fetch();
$stmttwo->fetch();
$newamount = $stmttwo - $stmt;
$newamount = array(
'newamount'=>$newamount
);
$response['error'] = false;
$response['message'] = 'Amount Allocated Gotten';
$response['newamount'] = $newamount;
}else{
$response['error'] = false;
$response['message'] = 'Amount Allocated Not GOtten';
}
break;
The Table structure
purchases table.
ID Price
1 2
2 34
4 12
5 87
fundruequest Table.
ID amountrequested
1 89
2 67
4 32
5 56
I want to sum amount requested and subtract it from the sum of price in purchases table.
the query does not work.
E.g.
SELECT SUM(price) AS amount
FROM
(
SELECT price, warehouse FROM purchases
UNION ALL
SELECT price * -1, warehouse FROM sales
) x
WHERE warehouse = ?;

Displaying Coupons

So I have this MySQL table where an item has a field called isCoupon. The default is set to 0 if the seller does not make the item available for coupon use. Each item is ordered by ID and in the shop, there can be more than one coupon where the item is also available for discount. But how do I make it that those coupons display when the item is included? How do I make it that the buyer gets to pick which coupon to use? And how do I calculate the new price if I put "50% off" on the record instead of 0.5?
Table Coupon
couponCode | items | discount
------------------------------
SAMPLE123 | 1 | 50% off
SAMPLE234 | 2,3 | 40% off
SAMPLE345 | 1,5 | 25% off
Table Item
itemID | isCoupon
-----------------
1 | 1
2 | 1
3 | 1
4 | 0
5 | 1
Thanks!
Try the below. As for the buyer picking to choose which coupon, you should let the buyer select the item first and then go to the checkout page by selecting the coupon.
$num_items = 5;
// the below array is used to store the coupons you can use for the item so you can
// proceed with it to the checkout page, there you can make a select dropdown to let
// the buyer select which coupon to use
$coupon_use = array();
for($i = 0; $i < $num_items; $i++) {
// check for current item
$SQL = "SELECT * FROM item WHERE itemID = '$i'";
$result = mysqli_query($connect, $SQL);
$field = mysqli_fetch_assoc($result);
// check if there is a coupon for the current item
if($field['isCoupon'] == 1) {
$SQL2 = "SELECT * FROM coupon";
$result2 = mysqli_query($connect, $SQL2);
// while method to go through every single coupon
while($field2 = mysqli_fetch_assoc($result2)) {
// if you put commas, you can use the explode method
// to store all the items for that coupon and use it to
// check for that current item
$coupons = explode(',', $field2['items']);
if(in_array($i, $coupons)) {
// do your items have a price?
// salePrice is the new price
// thePrice is the old price you need to get with $field (not $field2)
if($field2['discount'] == '50% off') { $salePrice = $thePrice * 0.5; }
else if($field2['discount'] == '40% off') { $salePrice = $thePrice * 0.6; }
else if($field2['discount'] == '25% off') { $salePrice = $thePrice * 0.75; }
// add the coupon code to the array of coupons available for use
array_push($coupon_use, $field2['couponCode']);
// display the prices
echo 'ITEM #'.$i.'<br><s>$'.$thePrice.'</s><br>$'.$salePrice.'<br><br>';
}
}
}
else { echo 'ITEM #'.$i.'$'.$thePrice; }
}
Now you can store the values of the coupon_use array to the checkout page.

PHP echoing table rows according to an array from another table

I'm having some issues echoing out the results from table rows in accordance to an array of digits stored in a separate table.
I would like to display the books based on the order assigned in the POS TABLE pos column.
$arr = $pos['pos']
$sort = explode(', ', $arr);
$sql = " SELECT * FROM items ";
$result = mysqli_query($db, $sql);
if ($result->num_rows > 0) {
$prods = $result->fetch_assoc();
foreach ( $sort as $pos ) {
echo $prods['qty'][$pos]; // NOT WORKING
}
}
Here are the structure of the tables:
-POS TABLE---------
id: pos:
1 4,1,3,2
-ITEMS TABLE---------
id: qty: name:
1 43 book1
2 23 book2
3 6 book3
4 36 book4

FIFO calculation in php (mysql)

I have two data tables stock_incomes, stock_outcomes and stock_outcomes_fifo (the one I insert pre-calculated data):
stock_incomes (stores leftovers data)
id| Levtv
-----------
7 | 100
8 | 250
9 | 350
stock_outcomes (here is the point)
id| Quantity
--------------
1 | 150*
I have no problem when stock_outcomes.Quantity is less than 100 (min(Id) from stock_incomes, please see my code below) but I have no idea what code to write I could get calculations if outcome is >100. In my example I used 150 and I would like to get data in next table as:
stock_outcomes_fifo (the one I wish to insert pre-calculated data from the previous two tables)
id| IncomeId| OutcomeId| OutcomePart| Leftv
---------------------------------------------
1 | 7 | 1 | 100 | 0
2 | 8 | 1 | 50 | 200
Here is my code with question inside (see last part of the code):
<?php
include_once("config.inc.php");
include_once("db.class.php");
// stock_outcomes
$db = new db($host, $database, $user, $passwd);
$sql = "SELECT * FROM stock_outcomes WHERE Id = '1'";
$mas = $db->get_array($sql);
if($mas) {
foreach ($mas as $k => $v) {
$OutcomeId = $mas[$k]['Id'];
$OutcomeQuantity = $mas[$k]['Quantity'];
}
}
// stock_incomes
$sql = "select * from stock_incomes where Id = (select min(Id) from stock_incomes where Leftv > 0)";
$mas = $db->get_array($sql);
if($mas) {
foreach ($mas as $k => $v) {
$IncomeId = $mas[$k]['Id'];
$IncomeLeftv = $mas[$k]['Leftv'];
}
}
// insert into stock_outcomes_fifo
if ($OutcomeQuantity <= $IncomeLeftv) {
$OutcomePart = $OutcomeQuantity;
$FifoLeftv = $IncomeLeftv - $OutcomeQuantity;
mysql_query("INSERT INTO `stock_outcomes_fifo` (IncomeId,OutcomeId,OutcomePart,Leftv) VALUES ($IncomeId, $OutcomeId, $OutcomePart, $FifoLeftv)");
}
if ($OutcomeQuantity > $IncomeLeftv) {
// I have no idea what php function to use in this case... please give me direction, thank you...
}
?>
The question has been solved, here is the final working code in case someone might need it:
<?php
include_once("config.inc.php");
include_once("db.class.php");
// stock_outcomes
$db = new db($host, $database, $user, $passwd);
$sql = "SELECT * FROM stock_outcomes WHERE Id = '1'";
$mas = $db->get_array($sql);
if($mas){
foreach ($mas as $k=>$v) {
$OutcomeId=$mas[$k]['Id'];
$OutcomeBarCode=$mas[$k]['BarCode'];
$OutcomeQuantity=$mas[$k]['Quantity'];
}
}
/* - Start code */
if ($OutcomeQuantity > 0) {
$sql = "select * from stock_incomes where Leftv > 0 order by id asc";
$mas = $db->get_array($sql);
if ($mas) {
//filing stock_outcomes_fifo
foreach ($mas as $k=>$v) {
$IncomeId = $mas[$k]['Id'];
$IncomeQuantity = $mas[$k]['Quantity'];
$IncomeUnitPrice = $mas[$k]['UnitPrice'];
$IncomeLeftv = $mas[$k]['Leftv'];
$OutcomePart = min($OutcomeQuantity, $IncomeLeftv);
$FifoLeftv = $IncomeLeftv - $OutcomePart;
$FifoCost = $IncomeUnitPrice * $OutcomePart;
mysql_query("INSERT INTO `stock_outcomes_fifo` (BarCode,IncomeId,OutcomeId,OutcomePart,UnitPrice,Leftv,Cost) VALUES ($OutcomeBarCode, $IncomeId, $OutcomeId, $OutcomePart, $IncomeUnitPrice, $FifoLeftv, $FifoCost)");
mysql_query("UPDATE `stock_incomes` SET Leftv = ".$FifoLeftv." WHERE Id = ".$IncomeId);
$OutcomeQuantity -= $OutcomePart;
if ($OutcomeQuantity <= 0) break;
}
$OutcomeCostQuery = "select sum(Cost) as summ from stock_outcomes_fifo where OutcomeId = ".$OutcomeId."";
$OutcomeCost = mysql_query($OutcomeCostQuery);
$OutcomeCostResult = mysql_fetch_array($OutcomeCost);
mysql_query("UPDATE `stock_outcomes` SET Cost = ".$OutcomeCostResult["summ"]." WHERE Id = ".$OutcomeId."");
}
} /* - Finish code */
?>
Please help me let me explain with this.....
purchase table
id purchase_id product_id qty net_unit_cost created_at
-------------------------------------------------------------------------
1 1 1 10 10 2022-10-10
--------------------------------------------------------------------------
2 2 1 20 12 2022-10-10
Sale table
sale_id product_id qty net_unit_price created_at
1 1 11 15 2022-10-10
in this, if i sold '11' units then how can i subtract from the rows to get remaining units? i've to subtract '10' units from first row and '1' unit from second row...

Categories