user11074500
user11074500

Reputation:

How to get remaining quantity against id at runtime?

I Have items and Purchase Items.. If i purchase 100 items and sell 50 items then in this inventory view should show 50 as item stock.

In this code I'm getting only the stock of last item.

I have attached Image of working.How can I get the stock against every item name?

Image

Blade

<div class="card-content collapse show">
    <div class="table-responsive">
        <table 
            class="table table table-striped table-bordered base-style"
            id="myTable"
        >
            <thead class="table table-striped">
                <tr>
                    <th>Item Name</th>
                    <th>Stock</th>
                </tr>
            </thead>
            <tbody>
                @foreach($items as $item)
                   <tr>
                       <td>{{$item->item_name}}</td>
                       <td>{{$allInventory}}</td>
                   </tr>
                @endforeach
            </tbody>
        </table>
    </div>
</div>

Controller

public function inventoryView()
{
    $items = AllItems::where('status', 1)->get(['id','item_name']);

    for ($i = 0; $i < count($items); $i++) {
        $checkQuantity = Purchase::leftJoin('all_items','all_items.id', '=','purchases.item_id')
                             ->where('purchases.item_id', $items[$i]->id)
                             ->sum('quantity');

        $checkSoldQuantity = SaleInvoiceDetail::leftJoin('all_items', 'all_items.id', '=', 'sale_invoice_details.item_id')
                                 ->where('sale_invoice_details.item_id', $items[$i]->id)
                                 ->sum('quantity');

        $allInventory = round($checkQuantity - $checkSoldQuantity);
    }

    return view('pages.inventory-view')
        ->with([
            'allInventory' => $allInventory,
            'items' => $items,
            'checkQuantity' => $checkQuantity,
            'checkSoldQuantity' => $checkSoldQuantity
        ]);
}

Upvotes: 0

Views: 100

Answers (1)

Sunil kumawat
Sunil kumawat

Reputation: 804

The problem is here

$allInventory = round($checkQuantity - $checkSoldQuantity);

Please replace your code and remove the variable ($allInventory,)

$items[$i]['inventory'] = round($checkQuantity - $checkSoldQuantity); 

in the view file, change your for loop

@foreach($items as $item)
        <tr>
           <td>{{$item->item_name}}</td>
           <td>{{$item->inventory}}</td>
        </tr>
 @endforeach

Upvotes: 1

Related Questions