vendredi 15 mars 2019

creating conditions for recieving partial delivery of products in laravel

Its my 2nd day of trying to make my "receiving of partial delivery" for my app. I am trying my luck here with you masters.

I have these two tables, the first one is the order_items table,

enter image description here

This table holds the items from the purchase order. and the other table is warehouse1

enter image description here

which hold the partially receive items.

I get the logic, I have to group the warehouse1.order_item_id so that I could get the sum of the items from let say order_number_id=22 which is the id from order_items table. to show you, here's my code doing the GROUP

public function viewdeliveryItems($id)
{
    $partialDeliveries = Warehouse1stocks::where('order_id', '=', $id)
       ->select(
                'order_item_id',
                DB::raw('SUM(stock_in_qty) as stock_in_qty'))->groupBy('order_item_id')->get();
                // DB::raw('SUM(stock_out_qty) as stock_out_qty'))->groupBy('order_item_id')->get();


    $order = Orders::find($id);
    $orderItems = $order->orderItems;


    $warehouse1stocks = Warehouse1stocks::all('order_id','order_item_id','stock_in_qty')->where('order_id','=',$id);

    return view('orders.delivery')->with('order', $order)
                                  ->with('warehouse1stocks', $warehouse1stocks)
                                  ->with('partialDeliveries', $partialDeliveries);
}

in my code you'll see that I group the order_item_id and called also the order items but you can see that their only link is ORDER_ID.

Just to show you the eloquent connection, in Orderitems Model

public function Warehouse1stocks()
{
    return $this->hasMany('App\Warehouse1stocks', 'id', 'order_item_id')->orderBy('created_at', 'DESC');
} 

Warehouse1stocks model

public function orderItems()
{
    return $this->hasMany('App\Orderitems', 'order_id', 'id')->orderBy('created_at', 'DESC');

}

And in my blade, as I mention in my intro, I am doing this in almost 2 days, I am not really a coder that's why I rely most of my codes base from tutorials available in google, I think I tried to many approach and due to desperation I end up with this,

@foreach($order->orderItems as $key=>$orderItem)
<tr>
    <td></td>
    <td></td>

    <td>
    @foreach($partialDeliveries as $partialDelivery)

            @if($partialDelivery->order_item_id == $orderItem->id)
                @php
                $remainingDeliveries = $orderItem->quantity - $partialDelivery->stock_in_qty;
                @endphp                    
            @else
                @php
                $remainingDeliveries = $orderItem->quantity;
                @endphp
            @endif


                {!! Form::number('stock_in_qty[]', $remainingDeliveries, 
                                ['id'=>'stock_in_qty_'.$orderItem->id]) !!} 
    @endforeach
    </td>
    ....
</tr>
@endforeach

**note: at first it has " @if($loop->first)" right below of the first ForEach. I just remove it.

Explanation the first @foreach calls the items from orderItems table and the second calls the item which has the partially received items

from my if condition, if both order_item_id are equal then triggers the computation. if not then just grab the quantity from order_items table.

But doing this does not gave what my expected result.

I know my post is too long but I guess I need to explain more so that hopefully you could understand the scenario.

Masters I am really hoping you can help me with this. Thank you so much in advance!!!

Aucun commentaire:

Enregistrer un commentaire