Laravel,如果商店具有相同的组ID

时间:2020-05-29 04:23:41

标签: php laravel

我陷入了一个非常复杂的问题,我有商店,组和订单表 我想显示所有具有相同组ID的商店的所有订单 我尝试过,但得到的是个别商店 请查看屏幕截图,您将了解我的问题

对于分组商店,我认为应该同时显示分组和商店

先谢谢您

// show the last 7 days report
public function getSalesStores()
{
    $weekStart = Carbon::parse('last Sunday')->subdays(7);
    $weekEnd = $weekStart->copy()->adddays(7);

    $allusers = User::getAllUsers();
    $allgroups = storeGroup::getAllGroups();

    $allstores = Storeinfo::select('id', 'store_name', 'site_percent', 'fee_type', 'store_group_id')
        ->whereHas('userorders', function ($query) use ($weekStart, $weekEnd) {
            $query->whereBetween('order_date', [$weekStart, $weekEnd]);
        })
        ->with(['userorders' => function ($query) use ($weekStart, $weekEnd) {
            $query
                ->whereBetween('order_date', [$weekStart, $weekEnd])
                ->whereNotIn('confirmed', ['c', 'a', 'r'])
                ->groupBy('userorders.order_number');
        }])
        ->orderBy('storeinfos.store_name')
        ->get();

        $groupedStores = $allstores->where('store_group_id', '!=', null);


return view('managedashboard.cservicedashboard.sales_orders_bystore.archive', compact('allusers', 'allstores','allgroups','groupedStores'));
}

刀片:

   <table class="table table-striped table-bordered table-hover" id="postTable">
    <p class="tabletitle">Sales Report</p>
    <thead>
    <tr>
        <th class="textcenter">Store</th>
        <th class="textcenter">Total</th>
        <th class="textcenter">our fee</th>
        <th class="textcenter">Pay to store</th>
        <th class="textcenter">Order number</th>
        <th class="textcenter">Date</th>
        <th class="textcenter">Store Name</th>
        <th class="textcenter">Customer Name</th>
    </tr>
</thead>
<tbody>   
    @foreach($groupedStores as $key => $store) 

        @php 
            $class = $key % 2 === 0 ? 'even' : 'odd'; 
            $fee=0; 
        @endphp
    <tr class="{{$class}}">
        <td class="storename" rowspan="{{ $store->userorders->count() + 1 }}">               
                  @foreach($allgroups as $allgroup) 
                        @if(($store->store_group_id)==$allgroup->id) 
                            {{$allgroup->name}}
                          @endif   
                   @endforeach
        </td>
    </tr>
     @foreach($store->userorders as $userorder)

            <tr>
                <td class="textcenter">
                    {{number_format($userorder->totalpayment, 2, '.', ',')}}
                </td>

                <td class="textcenter">

                    @if($store->fee_type=='total') 
                        {{number_format(($userorder->totalpayment * ($store->site_percent / 100)), 2, '.', ',')}} 
                        @else 
                        {{number_format(($userorder->order_subtotal * ($store->site_percent / 100)), 2, '.', ',')}}

                    @endif 

                    @php 
                            if($store->fee_type=='total') { 
                            $fee = $fee + number_format(($userorder->totalpayment * ($store->site_percent/100)), 2, '.', ','); 
                                                 } 
                        else { 
                               $fee = $fee + number_format(($userorder->order_subtotal *
                               ($store->site_percent/100)), 2, '.', ','); 
                              } 
                    @endphp
                </td>

                <td class="textcenter">
                    <!-- Pay to Store -->
                    @if($store->fee_type=='total') 
                        {{number_format(($userorder->totalpayment - ($userorder->totalpayment * ($store->site_percent/100))), 2, '.', ',')}} 

                        @else 
                            {{number_format(($userorder->totalpayment -
                            ($userorder->order_subtotal * ($store->site_percent/100))), 2, '.', ',')}} 
                    @endif
                </td>

                <td class="textcenter">
                    {{$userorder->order_number}}
                </td>

                <td class="textcenter">
                    {{date('D d M, Y', strtotime($userorder->order_date))}}
                </td>

                <!--  Store Name  -->
                <td class="textcenter">
                    @foreach($allusers as $alluser) 
                        @if(($userorder->user_id)==$alluser->id) 
                            {{$alluser->fname}} 
                        @endif
                    @endforeach
                </td>

                <!--  Customer Name  -->
                <td class="textcenter">
                    @foreach($allusers as $alluser) 
                        @if(($userorder->user_id)==$alluser->id) 
                            {{$alluser->fname}} 
                        @endif
                    @endforeach
                </td>
            </tr>


            @endforeach <!-- end orders loop -->

    <tr>
        <td class="totalstyle">TOTAL</td>
        <td class="totalstyle">
            {{number_format($store->userorders->sum('totalpayment'), 2, '.', ',')}}
        </td>

        <td class="totalstyle">
            {{$fee}}
        </td>

        <td class="totalstyle">
            {{number_format($store->userorders->sum('totalpayment')-($fee), 2, '.', ',')}}
        </td>
    </tr>

    @endforeach <!-- end main store loop -->

enter image description here

1 个答案:

答案 0 :(得分:0)

您需要做的就是获取查询数据并相应地重新格式化。然后,您可以使用foreach循环在视图中简单地显示数据。对于模型StoreGroup,Store和Order,您可以执行以下操作。这将以附件的“结果”部分中指定的格式提供u数据。

$storeGroupNames = StoreGroup::query()
    ->pluck('name', 'id');
$data = Order::query()
    ->with('store')
    ->get()
    ->groupBy(function (Order $order) {
        return $order->store->store_group_id;
    })
    ->mapWithKeys(function (Collection $orders, $storeGroupId) use ($storeGroupNames) {
        $storeGroupName = $storeGroupNames->get($storeGroupId);

        return [$storeGroupName => $orders];
    });
dd($data);
相关问题