Laravel Project - Add Sales and Purchases Reports

Laravel Project - Add Sales and Purchases Reports

Instructor-svgAl-Mamun Sarkar
Sep 15 , 2020
  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram

এই লেসনে sales report এবং purchases report দেখানো হয়েছে। এবং reports এ date range দিয়ে search দেয়া দেখানো হয়েছে। 

 

Source Code:

Routes:

Route::get('reposts/sales', 		'Reports\SaleReportController@index')->name('reports.sales');
Route::get('reposts/purchases', 	'Reports\PurchaseReportController@index')->name('reports.purchases');

 

SaleReportController.php

namespace App\Http\Controllers\Reports;

use App\Http\Controllers\Controller;
use App\SaleItem;
use Illuminate\Http\Request;

class SaleReportController extends Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->data['main_manu']    = 'Reports';
        $this->data['sub_manu']     = 'Sales';
    }

    public function index( Request $request )
    {
    	$this->data['start_date'] 	= $request->get('start_date', date('Y-m-d'));
    	$this->data['end_date'] 	= $request->get('end_date', date('Y-m-d'));

    	$this->data['sales'] = SaleItem::select( 'sale_items.quantity', 'sale_items.price', 'sale_items.total', 'products.title',  'sale_invoices.challan_no', 'sale_invoices.date')
								    	->join('products', 'sale_items.product_id', '=', 'products.id')
								    	->join('sale_invoices', 'sale_items.sale_invoice_id', '=', 'sale_invoices.id')
								    	->whereBetween('sale_invoices.date', [ $this->data['start_date'], $this->data['end_date'] ])
								    	->get();

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

 

PurchaseReportController.php

namespace App\Http\Controllers\Reports;

use App\Http\Controllers\Controller;
use App\PurchaseItem;
use Illuminate\Http\Request;

class PurchaseReportController extends Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->data['main_manu']    = 'Reports';
        $this->data['sub_manu']     = 'Purchases';
    }

    public function index( Request $request )
    {
    	$this->data['start_date'] 	= $request->get('start_date', date('Y-m-d'));
    	$this->data['end_date'] 	= $request->get('end_date', date('Y-m-d'));

    	$this->data['purchases'] = PurchaseItem::select( 'purchase_items.quantity', 'purchase_items.price', 'purchase_items.total', 'products.title',  'purchase_invoices.challan_no', 'purchase_invoices.date')
								    	->join('products', 'purchase_items.product_id', '=', 'products.id')
								    	->join('purchase_invoices', 'purchase_items.purchase_invoice_id', '=', 'purchase_invoices.id')
								    	->whereBetween('purchase_invoices.date', [ $this->data['start_date'], $this->data['end_date'] ])
								    	->get();

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

 

SaleItem.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class SaleItem extends Model
{

	protected $fillable = [ 'product_id', 'sale_invoice_id', 'price', 'quantity', 'total' ];
    
    public function invoice()
    {
    	return $this->belongsTo(SaleInvoice::class, 'sale_invoice_id', 'id');
    }

    public function product()
    {
    	return $this->belongsTo(Product::class);
    }

}

 

reports/sales.blade.php

@extends('layout.main')

@section('main_content')

	<div class="row clearfix page_header">
		<div class="col-md-4">
			<h2> Sales Report </h2>		
		</div>
		<div class="col-md-8 text-right">
			{!! Form::open([ 'route' => ['reports.sales'], 'method' => 'get' ]) !!}
			<div class="form-row align-items-center">
			    <div class="col-auto">
			      	<label class="sr-only" for="inlineFormInput">Start Date</label>
			      	{{ Form::date('start_date', $start_date, [ 'class'=>'form-control', 'id' => 'date', 'placeholder' => 'Start Date' ]) }}
			    </div>
			    <div class="col-auto">
			      	<label class="sr-only" for="inlineFormInputGroup">End Date</label>
			      	<div class="input-group mb-2">
			        	{{ Form::date('end_date', $end_date, [ 'class'=>'form-control', 'id' => 'date', 'placeholder' => 'End Date' ]) }}
			      	</div>
			    </div>			    
			    <div class="col-auto">
			      	<button type="submit" class="btn btn-primary mb-2">Submit</button>
			    </div>
			</div>
			{!! Form::close() !!}
		</div>
	</div>

	<!-- DataTales Example -->
	  <div class="card shadow mb-4">
	    <div class="card-header py-3">
	      <h6 class="m-0 font-weight-bold text-primary">Sales Report From <strong>{{ $start_date }}</strong> to <strong>{{ $end_date }}</strong> </h6>
	    </div>
	    <div class="card-body">
	      <div class="table-responsive">
	        <table class="table table-striped table-borderless" cellspacing="0">
	          <thead>
	            <tr>
	            	<th>Date</th>
	              	<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->date }} </td>
			            <td> {{ $sale->title }} </td>
			            <td class="text-right"> {{ $sale->quantity }} </td>
			            <td class="text-right"> {{ $sale->price }} </td>
			            <td class="text-right"> {{ $sale->total }} </td>
		            </tr>
	            @endforeach
	          </tbody>

	          <tfoot>
	            <tr>
	            	<th></th>
	              	<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"> {{ $sales->sum('total') }} </th>
	            </tr>
	          </tfoot>

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


@stop

 

reports/purchases.blade.php

@extends('layout.main')

@section('main_content')

	<div class="row clearfix page_header">
		<div class="col-md-4">
			<h2> Purchases Report </h2>		
		</div>
		<div class="col-md-8 text-right">
			{!! Form::open([ 'route' => ['reports.purchases'], 'method' => 'get' ]) !!}
			<div class="form-row align-items-center">
			    <div class="col-auto">
			      	<label class="sr-only" for="inlineFormInput">Start Date</label>
			      	{{ Form::date('start_date', $start_date, [ 'class'=>'form-control', 'id' => 'date', 'placeholder' => 'Start Date' ]) }}
			    </div>
			    <div class="col-auto">
			      	<label class="sr-only" for="inlineFormInputGroup">End Date</label>
			      	<div class="input-group mb-2">
			        	{{ Form::date('end_date', $end_date, [ 'class'=>'form-control', 'id' => 'date', 'placeholder' => 'End Date' ]) }}
			      	</div>
			    </div>			    
			    <div class="col-auto">
			      	<button type="submit" class="btn btn-primary mb-2">Submit</button>
			    </div>
			</div>
			{!! Form::close() !!}
		</div>
	</div>

	<!-- DataTales Example -->
	  <div class="card shadow mb-4">
	    <div class="card-header py-3">
	      <h6 class="m-0 font-weight-bold text-primary">Purchases Report From <strong>{{ $start_date }}</strong> to <strong>{{ $end_date }}</strong> </h6>
	    </div>
	    <div class="card-body">
	      <div class="table-responsive">
	        <table class="table table-striped table-borderless" cellspacing="0">
	          <thead>
	            <tr>
	            	<th>Date</th>
	              	<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->date }} </td>
			            <td> {{ $purchase->title }} </td>
			            <td class="text-right"> {{ $purchase->quantity }} </td>
			            <td class="text-right"> {{ $purchase->price }} </td>
			            <td class="text-right"> {{ $purchase->total }} </td>
		            </tr>
	            @endforeach
	          </tbody>

	          <tfoot>
	            <tr>
	            	<th></th>
	              	<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"> {{ $purchases->sum('total') }} </th>
	            </tr>
	          </tfoot>

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


@stop