Deduct quantity within a PHP loop

Asked

Viewed 42 times

0

I got the following Array():

Array
(
    [0] => stdClass Object
        (
            [inventory_id] => 1
            [product_id] => 1
            [order_id] => 0
            [product_quantity] => 15 <--------
            [notify_quantity] => 1
            [total_price] => 690
            [others_price] => 33.9
            [media_price] => 48.26
            [supplier_id] => 1
            [product_type] => order
            [product_stock_date] => 2019-01-31 11:59:06
            [initial_stock] => 1
            [type] => E
        )

    [1] => stdClass Object
        (
            [inventory_id] => 4
            [product_id] => 1
            [order_id] => 0
            [product_quantity] => 10 <--------
            [notify_quantity] => 1
            [total_price] => 200
            [others_price] => 20
            [media_price] => 22
            [supplier_id] => 2
            [product_type] => purchase
            [product_stock_date] => 2019-02-22 15:22:50
            [initial_stock] => 0
            [type] => E
        )

)`

Note that I have product_quantity = 15 e 10.

I have the $product['qty'] = 20. This is the amount I am requesting at the time of purchase. Ie I have in stock because adding all are 25 in total items.

So, at the time of assembling the loop, I need to deduct then 15 items from the first and 5 items from the second... in this case, there will be only 5 items left in the second array(). The problem is: how to do this?

What I tried to:

foreach($suppliers_products as $s_products){
    $qty = $s_products->product_quantity;
    echo "Qtd total ".$s_products->inventory_id." > ".$qty."<br><br<br>";
    if($qty >= $product['qty']){
        echo "achou total <br>";
    } else {
        $s = $product['qty'] - $qty;
        echo $s." < faltou > <br>"; 
        echo "<br><br><br>faltou ainda <br>";
    }
} 

Whereas $suppliers_products is the array() above.

To deduct in stock, I’m using as a reference PEPS - First that goes in, first out.

1 answer

2


To change the data within the loop you must go through by reference.

foreach ($estoques as &$estoque) {
  // -----------------^
}

And so basically implement the logic you’ve already made:

foreach ($estoques as &$estoque) {

    if ($estoque->quantidade >= $quantidade) {
        $estoque->quantidade -= $quantidade;
        break;
    }

    $quantidade -= $estoque->quantidade;
    $estoque->quantidade = 0;
}

For example:

$estoques = [
    (object) ['id' => 1, 'quantidade' => 15],
    (object) ['id' => 2, 'quantidade' => 10],
];

print_r($estoques);

$quantidade = 20;

foreach ($estoques as &$estoque) {

    if ($estoque->quantidade >= $quantidade) {
        $estoque->quantidade -= $quantidade;
        break;
    }

    $quantidade -= $estoque->quantidade;
    $estoque->quantidade = 0;
}

print_r($estoques);

Initially the array will be:

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [quantidade] => 15
        )

    [1] => stdClass Object
        (
            [id] => 2
            [quantidade] => 10
        )

)

After the loop will be:

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [quantidade] => 0
        )

    [1] => stdClass Object
        (
            [id] => 2
            [quantidade] => 5
        )

)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.