Laravel Project - Add Single User Reports

Author: Al-mamun Sarkar Date: 2020-10-04 09:02:19

এই লেসনে আমরা দেখবো কিভাবে একজন User এর Report (Sales Report, Purchases Report, Payments Report, Receipts Report) তৈরি করতে হয়।

 

Source Code:

Route:

Route::get('users/{id}/reports', 					'UserReportsController@reports')->name('user.reports');

UserReportsController.php

<?php

namespace App\Http\Controllers;

use App\Payment;
use App\PurchaseItem;
use App\Receipt;
use App\SaleItem;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class UserReportsController extends Controller
{
    public function reports( $id )
    {
        $this->data['tab_menu'] = 'reports';
        $this->data['user']     = User::findOrFail($id);

        $this->data['sales'] = SaleItem::select( 'products.title', DB::raw( 'SUM(sale_items.quantity) as quantity, AVG(sale_items.price) AS price, SUM(sale_items.total) as total') )
                                        ->join('products', 'sale_items.product_id', '=', 'products.id')
                                        ->join('sale_invoices', 'sale_items.sale_invoice_id', '=', 'sale_invoices.id')
                                        ->where('products.has_stock', 1)
                                        ->where('sale_invoices.user_id', $id)
                                        ->groupBy(['products.id', 'products.title'])
                                        ->get();

        $this->data['purchases'] = PurchaseItem::select( 'products.title', DB::raw( 'SUM(purchase_items.quantity) as quantity, AVG(purchase_items.price) AS price, SUM(purchase_items.total) as total') )
                                        ->join('products', 'purchase_items.product_id', '=', 'products.id')
                                        ->join('purchase_invoices', 'purchase_items.purchase_invoice_id', '=', 'purchase_invoices.id')
                                        ->where('products.has_stock', 1)
                                        ->where('purchase_invoices.user_id', $id)
                                        ->groupBy(['products.id', 'products.title'])
                                        ->get();

		$this->data['receipts'] = Receipt::select('date', DB::raw('SUM(amount) as amount') )
        								->groupBy('date')
        								->where('user_id', $id)
								    	->get();   
		
		$this->data['payments'] = Payment::select('date', DB::raw('SUM(amount) as amount') )
        								->groupBy('date')
        								->where('user_id', $id)
								    	->get();   

        return view('users.reports.reports', $this->data);   
    }
}

 

users/user_layout_content.blade.php

<div class="row clearfix mt-5">
	<div class="col-md-2">
		<div class="nav flex-column nav-pills">
		  <a class="nav-link @if($tab_menu == 'user_info') active @endif " href=" {{ route('users.show', $user->id) }} ">User Info</a>
		  <a class="nav-link @if($tab_menu == 'reports') active @endif "  href="{{ route('user.reports', $user->id) }}">Reports</a>
		  <a class="nav-link @if($tab_menu == 'sales') active @endif "  href="{{ route('user.sales', $user->id) }}">Sales</a>
		  <a class="nav-link @if($tab_menu == 'purchases') active @endif "  href="{{ route('user.purchases', $user->id) }}">Purchases</a>
		  <a class="nav-link @if($tab_menu == 'payments') active @endif "  href="{{ route('user.payments', $user->id) }}">Payments</a>
		  <a class="nav-link @if($tab_menu == 'receipts') active @endif "  href="{{ route('user.receipts', $user->id) }}">Receipts</a>
		</div>
	</div>
	<div class="col-md-10">
		@yield('user_content')
  	</div>
</div>

users/reports/reports.blade.php

@extends('users.user_layout')

@section('user_content')


	<div class="row">
		
		<!-- Earnings (Monthly) Card Example -->
	    <div class="col-xl-3 col-md-6 mb-4">
	      <div class="card border-left-primary shadow h-100 py-2">
	        <div class="card-body">
	          <div class="row no-gutters align-items-center">
	            <div class="col mr-2">
	              <div class="text-xs font-weight-bold text-primary text-uppercase mb-1"> Total Sales </div>
	              <div class="h5 mb-0 font-weight-bold text-gray-800"> {{ number_format($sales->sum('total'), 2) }} </div>
	            </div>
	            <div class="col-auto">
	              <i class="fas fa-calendar fa-2x text-gray-300"></i>
	            </div>
	          </div>
	        </div>
	      </div>
	    </div>

		<!-- Earnings (Monthly) Card Example -->
	    <div class="col-xl-3 col-md-6 mb-4">
	      <div class="card border-left-primary shadow h-100 py-2">
	        <div class="card-body">
	          <div class="row no-gutters align-items-center">
	            <div class="col mr-2">
	              <div class="text-xs font-weight-bold text-primary text-uppercase mb-1"> Total Purchases </div>
	              <div class="h5 mb-0 font-weight-bold text-gray-800"> {{ number_format($purchases->sum('total'), 2) }} </div>
	            </div>
	            <div class="col-auto">
	              <i class="fas fa-calendar fa-2x text-gray-300"></i>
	            </div>
	          </div>
	        </div>
	      </div>
	    </div>

		<!-- Earnings (Monthly) Card Example -->
	    <div class="col-xl-3 col-md-6 mb-4">
	      <div class="card border-left-primary shadow h-100 py-2">
	        <div class="card-body">
	          <div class="row no-gutters align-items-center">
	            <div class="col mr-2">
	              <div class="text-xs font-weight-bold text-primary text-uppercase mb-1"> Total Receipts </div>
	              <div class="h5 mb-0 font-weight-bold text-gray-800"> {{ number_format($receipts->sum('amount'), 2) }} </div>
	            </div>
	            <div class="col-auto">
	              <i class="fas fa-calendar fa-2x text-gray-300"></i>
	            </div>
	          </div>
	        </div>
	      </div>
	    </div>

		<!-- Earnings (Monthly) Card Example -->
	    <div class="col-xl-3 col-md-6 mb-4">
	      <div class="card border-left-primary shadow h-100 py-2">
	        <div class="card-body">
	          <div class="row no-gutters align-items-center">
	            <div class="col mr-2">
	              <div class="text-xs font-weight-bold text-primary text-uppercase mb-1"> Total Payments </div>
	              <div class="h5 mb-0 font-weight-bold text-gray-800"> {{ number_format($payments->sum('amount'), 2) }} </div>
	            </div>
	            <div class="col-auto">
	              <i class="fas fa-calendar fa-2x text-gray-300"></i>
	            </div>
	          </div>
	        </div>
	      </div>
	    </div>
	    
	</div>

	<!-- Sales Reports -->
  	<div class="card shadow mb-4">
	    <div class="card-header py-3">
	      <h6 class="m-0 font-weight-bold text-primary">Sales Report </h6>
	    </div>
	    <div class="card-body">
	      <div class="table-responsive">
	        <table class="table table-striped table-borderless table-sm" cellspacing="0">
	          <thead>
	            <tr>
	              	<th>Products</th>
	              	<th class="text-right">Quantity</th>
	              	<th class="text-right">Price</th>
	              	<th class="text-right">Total</th>
	            </tr>
	          </thead>
	          
	          <tbody>
	          	@foreach ($sales as $sale)
		            <tr>
			            <td> {{ $sale->title }} </td>
			            <td class="text-right"> {{ $sale->quantity }} </td>
			            <td class="text-right"> {{ number_format($sale->price, 2) }} </td>
			            <td class="text-right"> {{ number_format($sale->total, 2) }} </td>
		            </tr>
	            @endforeach
	          </tbody>

	          <tfoot>
	            <tr>
	              	<th class="text-right">Ttoal Items:</th>
	              	<th class="text-right"> {{ $sales->sum('quantity') }} </th>
	              	<th class="text-right">Total:</th>
	              	<th class="text-right"> {{ number_format($sales->sum('total'), 2) }} </th>
	            </tr>
	          </tfoot>

	        </table>
	      </div>
	    </div>
	  </div>



	  {{-- Purchase Reports --}}
	  <div class="card shadow mb-4">
	    <div class="card-header py-3">
	      <h6 class="m-0 font-weight-bold text-primary">Purchases Report </h6>
	    </div>
	    <div class="card-body">
	      <div class="table-responsive">
	        <table class="table table-striped table-borderless table-sm" cellspacing="0">
	          <thead>
	            <tr>
	              	<th>Products</th>
	              	<th class="text-right">Quantity</th>
	              	<th class="text-right">Price</th>
	              	<th class="text-right">Total</th>
	            </tr>
	          </thead>
	          
	          <tbody>
	          	@foreach ($purchases as $purchase)
		            <tr>
			            <td> {{ $purchase->title }} </td>
			            <td class="text-right"> {{ $purchase->quantity }} </td>
			            <td class="text-right"> {{ number_format($purchase->price, 2) }} </td>
			            <td class="text-right"> {{ number_format($purchase->total, 2) }} </td>
		            </tr>
	            @endforeach
	          </tbody>

	          <tfoot>
	            <tr>
	              	<th class="text-right">Ttoal Items:</th>
	              	<th class="text-right"> {{ $purchases->sum('quantity') }} </th>
	              	<th class="text-right">Total:</th>
	              	<th class="text-right"> {{ number_format($purchases->sum('total'), 2) }} </th>
	            </tr>
	          </tfoot>

	        </table>
	      </div>
	    </div>
	  </div>


	{{-- Receipts Reports --}}
	<div class="card shadow mb-4">
	    <div class="card-header py-3">
	      <h6 class="m-0 font-weight-bold text-primary"> Receipts Report </strong></h6>
	    </div>
	    
	    <div class="card-body">
	    	<div class="table-responsive">
		        <table class="table table-striped table-borderless table-sm" width="100%" cellspacing="0">
		          <thead>
		            <tr>
		              <th>Date</th>
		              <th class="text-right">Total</th>
		            </tr>
		          </thead>
		          
		          <tbody>
		          	@foreach ($receipts as $receipt)
			            <tr>
			              <td> {{ $receipt->date }} </td>
			              <td class="text-right"> {{ number_format($receipt->amount, 2) }} </td>
			            </tr>
		            @endforeach
		          </tbody>

		          <tfoot>
		            <tr>
		              <th>Total</th>
		              <th class="text-right"> {{ number_format($user->receipts()->sum('amount'), 2) }} </th>
		            </tr>
		          </tfoot>
		        </table>
		    </div>
	    </div>
  	</div>

  	{{-- Payment Reports --}}
	 <div class="card shadow mb-4">
	    <div class="card-header py-3">
	      <h6 class="m-0 font-weight-bold text-primary"> Payments Report </strong></h6>
	    </div>
	    
	    <div class="card-body">
	    	<div class="table-responsive">
		        <table class="table table-striped table-borderless table-sm" width="100%" cellspacing="0">
		          <thead>
		            <tr>
		              <th>Date</th>
		              <th class="text-right">Total</th>
		            </tr>
		          </thead>
		          
		          <tbody>
		          	@foreach ($payments as $payment)
			            <tr>
			              <td> {{ $payment->date }} </td>
			              <td class="text-right"> {{ number_format($payment->amount, 2) }} </td>
			            </tr>
		            @endforeach
		          </tbody>

		          <tfoot>
		            <tr>
		              <th>Total</th>
		              <th class="text-right"> {{ number_format($user->payments()->sum('amount'), 2) }} </th>
		            </tr>
		          </tfoot>
		        </table>
		    </div>
	    </div>
  	</div>
  	

@stop