Restaurant Management System ( Part 13 End )

Published Date: 23-Feb-2018 | Tags: Laravel 5.5  Bootstrap 3  Project  

For this last tutorial I want to show you how to create dashboard in Admin screen.

Note: I recommend you to check all pervious tutorials before coming to this step. Because some tutorials have relationship with others.

 

Step 1: Create View

Create view home.blade.php in /resources/views.

<div class="row-fluid">
    <h2 class="page-header">Sale Graph Report (15 days)
        <div style="border: 1px double gray;background: whitesmoke;padding: 10px;margin: 0px;text-align: right;float: right">
            $ {{number_format($order['Total']['total'],2)}}
        </div>
    </h2>

    <div id="line"></div>
</div>
<div class="row-fluid">
    <div class="col-md-7">
        <h4 class="page-header">Top 20 Product</h4>
        <table class="table">
            <thead>
            <tr>
                <th> No</th>
                <th> Description</th>
                <th style="text-align: center"> Quantity</th>
            </tr>
            </thead>
            <tbody>
            <?php $i=1;?>
            @foreach($orderDetails as $orderDetail)
                <tr>
                    <td>{{$i++}}</td>
                    <td>{!! $orderDetail->product_id?$orderDetail->description:'<b>'.$orderDetail->description.'</b>'!!}</td>
                    <td align="center">{{$orderDetail->total}}</td>
                </tr>
            @endforeach
            </tbody>
        </table>
    </div>
    <div class="col-md-5">
        <h4 class="page-header">Daily Sale Summary</h4>
        <table class="table">
            <thead>
            <th>Category</th>
            <th style="text-align: right">Net Amount</th>
            </thead>
            <tbody>
            @foreach($sale as $key=>$value)
                <tr>
                    <td>{{$key}}</td>
                    <td align="right">$ {{number_format($value['total'],2)}}</td>
                </tr>
            @endforeach
            </tbody>
        </table>
        <table class="table">
            <thead>
            <th>Period</th>
            <th style="text-align: right">Net Amount</th>
            </thead>
            <tbody>
            @foreach($order as $key=>$value)
                @if($key!='Total')
                    <tr>
                        <td>{{$key}}</td>
                        <td align="right">$ {{number_format($value['total'],2)}}</td>
                    </tr>
                @endif
            @endforeach
            </tbody>
        </table>
    </div>
</div>
</div>
<br/><br/><br/><br/>
<script>
    $('#line').highcharts({
        colors: ['#058DC7', '#50B432', '#cc00cc', 'red', '#cc0033', '#DDDF00', '#64E572', '#24CBE5', '#FFF263', '#FF9655'],
        title: {
            text: 'Sale Summary Report',
            x: -20 //center
        },
        subtitle: {
            text: '',
            x: -20
        },
        xAxis: {
            categories: <?php echo json_encode($iv); ?>
        },
        yAxis: {
            title: {
                text: 'Cash in hand ($)'
            },
            labels: {
                format: '{value:.2f}'
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
        },
        plotOptions: {
            line: {
                dataLabels: {
                    enabled: true,
                    formatter: function () {
                        return Highcharts.numberFormat(this.y, 2);
                    }
                },
                enableMouseTracking: false
            }
        },
        tooltip: {
            headerFormat: '',
            valuePrefix: '$'
        },
        legend: {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'middle',
            borderWidth: 0
        },
        series: [
            {
                name: 'Breakfast',
                marker: {symbol: "circle"},
                data:<?php echo json_encode($morning); ?>
            },
            {
                name: 'Lunch',
                marker: {symbol: "circle"},
                data:<?php echo json_encode($afternoon); ?>
            },
            {
                name: 'Dinner',
                marker: {symbol: "circle"},
                data:<?php echo json_encode($evening); ?>
            },
            {
                name: 'Daily',
                marker: {symbol: "circle"},
                data:<?php echo json_encode($daily); ?>
            }
        ]
    });
</script>

 

Step 2: Create Controller

Create controller HomeController.php in /demo/app/Http/Cotrollers.

<?php

namespace App\Http\Controllers;

use App\ProductCategory;
use App\Order;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;

class HomeController extends Controller
{
    public function index()
    {
        $d1 = strtotime(date("d-M-Y", strtotime("-7 days")));
        $d2 = strtotime(date("d-M-Y"));
        $iv = array();
        $morning = array();
        $afternoon = array();
        $evening = array();
        $daily = array();
        $interval = $d2 - $d1;
        $count = date('d', $interval);
        for ($i = 1; $i <= $count; $i++) {
            $current = gmdate("d-M-Y", strtotime("+$i day", $d1));
            $iv[$i - 1] = intVal(date("d", strtotime($current)));
            $m = Order::join('order_details', 'orders.id', '=', 'order_details.order_id')
                ->select(DB::raw("sum(order_details.quantity * order_details.price*(1-orders.discount/100)) as total"))
                ->where(DB::raw("date(orders.created_at)"), date('Y-m-d', strtotime($current)))
                ->where('orders.status', 'Completed')
                ->where('order_details.deleted_at', NULL)
                ->whereBetween(DB::raw("date_format(orders.created_at,'%H:%i')"), ['00:00', '10:59'])
                ->first();
            $a = Order::join('order_details', 'orders.id', '=', 'order_details.order_id')
                ->select(DB::raw("sum(order_details.quantity * order_details.price*(1-orders.discount/100)) as total"))
                ->where(DB::raw("date(orders.created_at)"), date('Y-m-d', strtotime($current)))
                ->where('orders.status', 'Completed')
                ->where('order_details.deleted_at', NULL)
                ->whereBetween(DB::raw("date_format(orders.created_at,'%H:%i')"), ['11:00', '14:59'])
                ->first();
            $e = Order::join('order_details', 'orders.id', '=', 'order_details.order_id')
                ->select(DB::raw("sum(order_details.quantity * order_details.price*(1-orders.discount/100)) as total"))
                ->where(DB::raw("date(orders.created_at)"), date('Y-m-d', strtotime($current)))
                ->where('orders.status', 'Completed')
                ->where('order_details.deleted_at', NULL)
                ->whereBetween(DB::raw("date_format(orders.created_at,'%H:%i')"), ['15:00', '23:59'])
                ->first();
            $d = Order::join('order_details', 'orders.id', '=', 'order_details.order_id')
                ->select(DB::raw("sum(order_details.quantity * order_details.price*(1-orders.discount/100)) as total"))
                ->where(DB::raw("date(orders.created_at)"), date('Y-m-d', strtotime($current)))
                ->where('orders.status', 'Completed')
                ->where('order_details.deleted_at', NULL)
                ->first();
            $morning[$i - 1] = floatval($m->total ? $m->total : 0);
            $afternoon[$i - 1] = floatval($a->total ? $a->total : 0);
            $evening[$i - 1] = floatval($e->total ? $e->total : 0);
            $daily[$i - 1] = floatval($d->total ? $d->total : 0);
        }
        // Get Daily Sale Summary
        $order['Breakfast'] = Order::join('order_details', 'orders.id', '=', 'order_details.order_id')
            ->select(DB::raw('sum(order_details.quantity * order_details.price - (order_details.quantity * order_details.price*orders.discount/100)) as total'))
            ->where(DB::raw("date(orders.created_at)"), date('Y-m-d'))
            ->whereBetween(DB::raw("date_format(orders.created_at,'%H:%i')"), ['00:00', '10:59'])
            ->where('orders.status', 'Completed')
            ->where('order_details.deleted_at', NULL)
            ->groupBy(DB::raw("date(orders.created_at)"))
            ->first();
        $order['Lunch'] = Order::join('order_details', 'orders.id', '=', 'order_details.order_id')
            ->select(DB::raw('sum(order_details.quantity * order_details.price-(order_details.quantity * order_details.price*orders.discount/100)) as total'))
            ->where(DB::raw("date(orders.created_at)"), date('Y-m-d'))
            ->whereBetween(DB::raw("date_format(orders.created_at,'%H:%i')"), ['11:00', '14:59'])
            ->where('orders.status', 'Completed')
            ->where('order_details.deleted_at', NULL)
            ->groupBy(DB::raw("date(orders.created_at)"))
            ->first();
        $order['Dinner'] = Order::join('order_details', 'orders.id', '=', 'order_details.order_id')
            ->select(DB::raw('sum(order_details.quantity * order_details.price-(order_details.quantity * order_details.price*orders.discount/100)) as total'))
            ->where(DB::raw("date(orders.created_at)"), date('Y-m-d'))
            ->whereBetween(DB::raw("date_format(orders.created_at,'%H:%i')"), ['15:00', '23:59'])
            ->where('orders.status', 'Completed')
            ->where('order_details.deleted_at', NULL)
            ->groupBy(DB::raw("date(orders.created_at)"))
            ->first();
        $order['Total'] = Order::join('order_details', 'orders.id', '=', 'order_details.order_id')
            ->select(DB::raw('sum(order_details.quantity * order_details.price-(order_details.quantity * order_details.price*orders.discount/100)) as total'))
            ->where(DB::raw("date(orders.created_at)"), date('Y-m-d'))
            ->where('orders.status', 'Completed')
            ->where('order_details.deleted_at', NULL)
            ->groupBy(DB::raw("date(orders.created_at)"))
            ->first();
        // sale by food type
        foreach (ProductCategory::orderBy('name')->pluck('name', 'id') as $key => $value) {
            $sale[$value] = Order::join('order_details', 'orders.id', '=', 'order_details.order_id')
                ->join('products', 'products.id', '=', 'order_details.product_id')
                ->select(DB::raw('sum(order_details.quantity * order_details.price-(order_details.quantity * order_details.price*orders.discount/100)) as total'))
                ->where(DB::raw("date(orders.created_at)"), date('Y-m-d'))
                ->where('products.product_category_id', $key)
                ->where('orders.status', 'Completed')
                ->where('order_details.deleted_at', NULL)
                ->groupBy(DB::raw("date(orders.created_at)"))
                ->first();
        }
        $sale['Open'] = Order::join('order_details', 'orders.id', '=', 'order_details.order_id')
            ->select(DB::raw('sum(order_details.quantity * order_details.price-(order_details.quantity * order_details.price*orders.discount/100)) as total'))
            ->where(DB::raw("date(orders.created_at)"), date('Y-m-d', strtotime(Session::get('report_from'))))
            ->whereNull('order_details.product_id')
            ->where('orders.status', 'Completed')
            ->where('order_details.deleted_at', NULL)
            ->groupBy(DB::raw("date(orders.created_at)"))
            ->first();

        // Get detail report
        $orderDetails = Order::join('order_details', 'orders.id', '=', 'order_details.order_id')
            ->select(DB::raw("product_id,description,sum(order_details.quantity) as total,price,sum(order_details.quantity * order_details.price*orders.discount/100) as discount"))
            ->where(DB::raw("date(orders.created_at)"), date('Y-m-d'))
            ->where('orders.status', 'Completed')
            ->where('order_details.deleted_at', NULL)
            ->groupBy('order_details.product_id')
            ->groupBy('order_details.description')
            ->groupBy('order_details.price')
            ->orderBy('total', 'desc')
            ->take(20)
            ->get();
        return view('home', [
            'iv' => $iv,
            'morning' => $morning,
            'afternoon' => $afternoon,
            'evening' => $evening,
            'daily' => $daily,
            'order' => $order,
            'sale' => $sale,
            'orderDetails' => $orderDetails,
        ]);
    }
}

 

Step 3: How to Test?

After login to admin screen, you will be able to access Dashboard option on the left side menu.

dashboard

 

Thank you for taking time to follow it. It is hard to explain. So read the code and try to understand by yourself.

Hope it can help you to have some idea of doing something with your programming skill.

If you cannot make it work, please contact me.


Demo


Related Posts

© 2018 All Rights Reserved.