এই লেসনে sale invoice এর জন্য receipts (payment) add করা দেখবো। এবং User এর Total Sale, Total Receipts, Total Purchase, Total Payments দেখাবো।
Routes:
Route::post('users/{id}/invoices', 'UserSalesController@createInvoice')->name('user.sales.store');
Route::get('users/{id}/invoices/{invoice_id}', 'UserSalesController@invoice')->name('user.sales.invoice_details');
Route::delete('users/{id}/invoices/{invoice_id}', 'UserSalesController@destroy')->name('user.sales.destroy');
Route::post('users/{id}/invoices/{invoice_id}', 'UserSalesController@addItem')->name('user.sales.invoices.add_item');
Route::delete('users/{id}/invoices/{invoice_id}/{item_id}', 'UserSalesController@destroyItem')->name('user.sales.invoices.delete_item');
Route::post('users/{id}/receipts/{invoice_id?}', 'UserReceiptsController@store')->name('user.receipts.store');
UserReceiptsController.php
namespace App\Http\Controllers;
use App\Http\Requests\ReceiptRequest;
use App\Receipt;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
class UserReceiptsController extends Controller
{
public function __construct()
{
$this->data['tab_menu'] = 'receipts';
}
public function index( $id )
{
$this->data['user'] = User::findOrFail($id);
return view('users.receipts.receipts', $this->data);
}
public function store(ReceiptRequest $request, $user_id, $invoice_id = null)
{
$formData = $request->all();
$formData['user_id'] = $user_id;
$formData['admin_id'] = Auth::id();
if ($invoice_id) {
$formData['sale_invoice_id'] = $invoice_id;
}
if( Receipt::create($formData) ) {
Session::flash('message', 'Receipt Added Successfully');
}
if ($invoice_id) {
return redirect()->route( 'user.sales.invoice_details', ['id' => $user_id, 'invoice_id' => $invoice_id] );
} else {
return redirect()->route('user.receipts', ['id' => $user_id]);
}
}
public function destroy($user_id, $receipt_id)
{
if( Receipt::destroy($receipt_id) ) {
Session::flash('message', 'Receipt Deleted Successfully');
}
return redirect()->route('user.receipts', ['id' => $user_id]);
}
}
app/Receipt.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Receipt extends Model
{
protected $fillable = ['date', 'amount', 'note', 'user_id', 'admin_id', 'sale_invoice_id'];
public function admin()
{
return $this->belongsTo(Admin::class);
}
public function invoice()
{
return $this->belongsTo(SaleInvoice::class);
}
}
app/SaleInvoice.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class SaleInvoice extends Model
{
protected $fillable = ['date', 'challan_no', 'note', 'user_id', 'admin_id'];
public function admin()
{
return $this->belongsTo(Admin::class);
}
public function items()
{
return $this->hasMany(SaleItem::class);
}
public function receipts()
{
return $this->hasMany(Receipt::class);
}
}
2020_09_08_135325_add_invoice_id_on_payments_and_receipts.php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddInvoiceIdOnPaymentsAndReceipts extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('receipts', function (Blueprint $table) {
$table->foreignId('sale_invoice_id')->nullable()->after('user_id');
});
Schema::table('payments', function (Blueprint $table) {
$table->foreignId('purchase_invoice_id')->nullable()->after('user_id');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('receipts', function (Blueprint $table) {
$table->dropColumn('sale_invoice_id');
});
Schema::table('payments', function (Blueprint $table) {
$table->dropColumn('purchase_invoice_id');
});
}
}
users/invoice_layout.blade.php
@extends('layout.main')
@section('main_content')
@include('users.user_layout_content')
@stop
users/user_layout.blade.php
@extends('layout.main')
@section('main_content')
<div class="row clearfix page_header">
<div class="col-md-4">
<a class="btn btn-info" href="{{ route('users.index') }}"> <i class="fa fa-arrow-left" aria-hidden="true"></i> Back </a>
</div>
<div class="col-md-8 text-right">
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#newSale">
<i class="fa fa-plus"></i> New Sale
</button>
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#newPurchase">
<i class="fa fa-plus"></i> New Purchase
</button>
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#newPayment">
<i class="fa fa-plus"></i> New Payment
</button>
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#newReceipt">
<i class="fa fa-plus"></i> New Receipt
</button>
</div>
</div>
@include('users.user_layout_content')
{{-- Modal For add new payment --}}
<div class="modal fade" id="newPayment" tabindex="-1" role="dialog" aria-labelledby="newPaymentModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
{!! Form::open([ 'route' => ['user.payments.store', $user->id], 'method' => 'post' ]) !!}
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="newPaymentModalLabel"> New Payments </h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group row">
<label for="date" class="col-sm-3 col-form-label"> Date <span class="text-danger">*</span> </label>
<div class="col-sm-9">
{{ Form::date('date', NULL, [ 'class'=>'form-control', 'id' => 'date', 'placeholder' => 'Date', 'required' ]) }}
</div>
</div>
<div class="form-group row">
<label for="amount" class="col-sm-3 col-form-label">Amount <span class="text-danger">*</span> </label>
<div class="col-sm-9">
{{ Form::text('amount', NULL, [ 'class'=>'form-control', 'id' => 'amount', 'placeholder' => 'Amount', 'required' ]) }}
</div>
</div>
<div class="form-group row">
<label for="note" class="col-sm-3 col-form-label">Note </label>
<div class="col-sm-9">
{{ Form::textarea('note', NULL, [ 'class'=>'form-control', 'id' => 'note', 'rows' => '3', 'placeholder' => 'Note' ]) }}
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
{!! Form::close() !!}
</div>
</div>
{{-- Modal For Receipt --}}
<div class="modal fade" id="newReceipt" tabindex="-1" role="dialog" aria-labelledby="newReceiptModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
{!! Form::open([ 'route' => ['user.receipts.store', $user->id], 'method' => 'post' ]) !!}
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="newReceiptModalLabel"> New Receipts </h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group row">
<label for="date" class="col-sm-3 col-form-label"> Date <span class="text-danger">*</span> </label>
<div class="col-sm-9">
{{ Form::date('date', NULL, [ 'class'=>'form-control', 'id' => 'date', 'placeholder' => 'Date', 'required' ]) }}
</div>
</div>
<div class="form-group row">
<label for="amount" class="col-sm-3 col-form-label">Amount <span class="text-danger">*</span> </label>
<div class="col-sm-9">
{{ Form::text('amount', NULL, [ 'class'=>'form-control', 'id' => 'amount', 'placeholder' => 'Amount', 'required' ]) }}
</div>
</div>
<div class="form-group row">
<label for="note" class="col-sm-3 col-form-label">Note </label>
<div class="col-sm-9">
{{ Form::textarea('note', NULL, [ 'class'=>'form-control', 'id' => 'note', 'rows' => '3', 'placeholder' => 'Note' ]) }}
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
{!! Form::close() !!}
</div>
</div>
{{-- Modal For Sales --}}
<div class="modal fade" id="newSale" tabindex="-1" role="dialog" aria-labelledby="newSaleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
{!! Form::open([ 'route' => ['user.sales.store', $user->id], 'method' => 'post' ]) !!}
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="newSaleModalLabel"> New Sale Invoice </h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group row">
<label for="date" class="col-sm-3 col-form-label"> Date <span class="text-danger">*</span> </label>
<div class="col-sm-9">
{{ Form::date('date', NULL, [ 'class'=>'form-control', 'id' => 'date', 'placeholder' => 'Date', 'required' ]) }}
</div>
</div>
<div class="form-group row">
<label for="challan_no" class="col-sm-3 col-form-label">Challan Number </label>
<div class="col-sm-9">
{{ Form::text('challan_no', NULL, [ 'class'=>'form-control', 'id' => 'challan_no', 'placeholder' => 'Challan Number' ]) }}
</div>
</div>
<div class="form-group row">
<label for="note" class="col-sm-3 col-form-label">Note </label>
<div class="col-sm-9">
{{ Form::textarea('note', NULL, [ 'class'=>'form-control', 'id' => 'note', 'rows' => '3', 'placeholder' => 'Note' ]) }}
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
{!! Form::close() !!}
</div>
</div>
@stop
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 == '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/sales/invoice.blade.php
@extends('users.invoice_layout')
@section('user_content')
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary"> Sales Invoice Details </h6>
</div>
<div class="card-body">
<div class="row clearfix justify-content-md-center">
<div class="col-md-6">
<div class="no_padding no_margin"> <strong>Customer:</strong> {{ $user->name }}</div>
<div class="no_padding no_margin"><strong>Email:</strong> {{ $user->email }}</div>
<div class="no_padding no_margin"><strong>Phone:</strong> {{ $user->phone }}</div>
</div>
<div class="col-md-3"></div>
<div class="col-md-3">
<div class="no_padding no_margin"><strong>Date:</strong> {{ $invoice->date }} </div>
<div class="no_padding no_margin"><strong>Challen No:</strong> {{ $invoice->challan_no }} </div>
</div>
</div>
<div class="invoice_items">
<table class="table table-borderless ">
<thead>
<th>SL</th>
<th>Product</th>
<th>Price</th>
<th>Qty</th>
<th class="text-right">Total</th>
<th class="text-right">-</th>
</thead>
<tbody>
@foreach ($invoice->items as $key => $item)
<tr>
<td> {{ $key+1 }} </td>
<td> {{ $item->product->title }} </td>
<td> {{ $item->price }} </td>
<td> {{ $item->quantity }} </td>
<td class="text-right"> {{ $item->total }} </td>
<td class="text-right">
<form
method="POST"
action=" {{ route('user.sales.invoices.delete_item', ['id' => $user->id, 'invoice_id' => $invoice->id, 'item_id'=> $item->id]) }}
">
@csrf
@method('DELETE')
<button onclick="return confirm('Are you sure?')" type="submit" class="btn btn-danger btn-sm">
<i class="fa fa-trash"></i>
</button>
</form>
</td>
</tr>
@endforeach
</tbody>
<tr>
<th></th>
<th>
<button class="btn btn-info btn-sm" data-toggle="modal" data-target="#newProduct">
<i class="fa fa-plus "></i> Add Product
</button>
</th>
<th colspan="2" class="text-right"> Total: </th>
<th class="text-right"> {{ $toalPayable = $invoice->items()->sum('total') }} </th>
<th></th>
</tr>
<tr>
<th></th>
<th>
<button class="btn btn-primary btn-sm" data-toggle="modal" data-target="#newReceiptForInvoice">
<i class="fa fa-plus "></i> Add Receipt
</button>
</th>
<th colspan="2" class="text-right"> Paid: </th>
<th class="text-right"> {{ $totalPaid = $invoice->receipts()->sum('amount') }} </th>
<th></th>
</tr>
<tr>
<th colspan="4" class="text-right"> Due: </th>
<th class="text-right"> {{ $toalPayable - $totalPaid }} </th>
<th></th>
</tr>
</table>
</div>
</div>
</div>
{{-- Modal For Add new Product --}}
<div class="modal fade" id="newProduct" tabindex="-1" role="dialog" aria-labelledby="newProductModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
{!! Form::open([ 'route' => ['user.sales.invoices.add_item', ['id' => $user->id, 'invoice_id' => $invoice->id] ], 'method' => 'post' ]) !!}
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="newProductModalLabel"> New Sale Invoice </h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group row">
<label for="product" class="col-sm-3 col-form-label text-right">Product <span class="text-danger">*</span> </label>
<div class="col-sm-9">
{{ Form::select('product_id', $products, NULL, [ 'class'=>'form-control', 'id' => 'product', 'required', 'placeholder' => 'Select Product' ]) }}
</div>
</div>
<div class="form-group row">
<label for="price" class="col-sm-3 col-form-label text-right">Unite Price <span class="text-danger">*</span> </label>
<div class="col-sm-9">
{{ Form::text('price', NULL, [ 'class'=>'form-control', 'id' => 'price', 'placeholder' => 'Unite Price', 'required' ]) }}
</div>
</div>
<div class="form-group row">
<label for="quantity" class="col-sm-3 col-form-label text-right">Quantity <span class="text-danger">*</span> </label>
<div class="col-sm-9">
{{ Form::text('quantity', NULL, [ 'class'=>'form-control', 'id' => 'quantity', 'placeholder' => 'Quantity', 'required' ]) }}
</div>
</div>
<div class="form-group row">
<label for="total" class="col-sm-3 col-form-label text-right">Total <span class="text-danger">*</span> </label>
<div class="col-sm-9">
{{ Form::text('total', NULL, [ 'class'=>'form-control', 'id' => 'total', 'placeholder' => 'Total', 'required' ]) }}
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
{!! Form::close() !!}
</div>
</div>
{{-- New Receipt For Invoice --}}
<div class="modal fade" id="newReceiptForInvoice" tabindex="-1" role="dialog" aria-labelledby="newReceiptForInvoiceModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
{!! Form::open([ 'route' => [ 'user.receipts.store', [$user->id, $invoice->id] ], 'method' => 'post' ]) !!}
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="newReceiptForInvoiceModalLabel"> New Receipts For This Invoice </h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group row">
<label for="date" class="col-sm-3 col-form-label"> Date <span class="text-danger">*</span> </label>
<div class="col-sm-9">
{{ Form::date('date', NULL, [ 'class'=>'form-control', 'id' => 'date', 'placeholder' => 'Date', 'required' ]) }}
</div>
</div>
<div class="form-group row">
<label for="amount" class="col-sm-3 col-form-label">Amount <span class="text-danger">*</span> </label>
<div class="col-sm-9">
{{ Form::text('amount', NULL, [ 'class'=>'form-control', 'id' => 'amount', 'placeholder' => 'Amount', 'required' ]) }}
</div>
</div>
<div class="form-group row">
<label for="note" class="col-sm-3 col-form-label">Note </label>
<div class="col-sm-9">
{{ Form::textarea('note', NULL, [ 'class'=>'form-control', 'id' => 'note', 'rows' => '3', 'placeholder' => 'Note' ]) }}
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
{!! Form::close() !!}
</div>
</div>
@stop
users/users.blade.php
@extends('layout.main')
@section('main_content')
<div class="row clearfix page_header">
<div class="col-md-6">
<h2> Users </h2>
</div>
<div class="col-md-6 text-right">
<a class="btn btn-info" href="{{ url('users/create') }}"> <i class="fa fa-plus"></i> New user </a>
</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">Users</h6>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>ID</th>
<th>Group</th>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Address</th>
<th class="text-right">Actions</th>
</tr>
</thead>
<tfoot>
<tr>
<th>ID</th>
<th>Group</th>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Address</th>
<th class="text-right">Actions</th>
</tr>
</tfoot>
<tbody>
@foreach ($users as $user)
<tr>
<td> {{ $user->id }} </td>
<td> {{ optional($user->group)->title }} </td>
<td> {{ $user->name }} </td>
<td> {{ $user->email }} </td>
<td> {{ $user->phone }} </td>
<td> {{ $user->address }} </td>
<td class="text-right">
<form method="POST" action=" {{ route('users.destroy', ['user' => $user->id]) }} ">
<a class="btn btn-primary btn-sm" href="{{ route('users.show', ['user' => $user->id]) }}">
<i class="fa fa-eye"></i>
</a>
<a class="btn btn-primary btn-sm" href="{{ route('users.edit', ['user' => $user->id]) }}">
<i class="fa fa-edit"></i>
</a>
@if(
$user->sales()->count() == 0
&& $user->purchases()->count() == 0
&& $user->receipts()->count() == 0
&& $user->payments()->count() == 0
)
@csrf
@method('DELETE')
<button onclick="return confirm('Are you sure?')" type="submit" class="btn btn-danger btn-sm">
<i class="fa fa-trash"></i>
</button>
@endif
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
@stop
users/show.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">
<?php
$total = 0;
foreach ($user->sales as $sale) {
$total += $sale->items()->sum('total');
}
echo $total;
?>
</div>
</div>
<div class="col-auto">
<i class="fas fa-chart 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 Purchase</div>
<div class="h5 mb-0 font-weight-bold text-gray-800">
<?php
$total = 0;
foreach ($user->purchases as $purchase) {
$total += $purchase->items()->sum('total');
}
echo $total;
?>
</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">{{ $user->receipts()->sum('amount') }}</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"> {{ $user->payments()->sum('amount') }} </div>
</div>
<div class="col-auto">
<i class="fas fa-calendar fa-2x text-gray-300"></i>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary"> {{ $user->name }} </h6>
</div>
<div class="card-body">
<div class="row clearfix justify-content-md-center">
<div class="col-md-8">
<table class="table table-borderless table-striped">
<tr>
<th class="text-right">Group :</th>
<td> {{ $user->group->title }} </td>
</tr>
<tr>
<th class="text-right">Name : </th>
<td> {{ $user->name }} </td>
</tr>
<tr>
<th class="text-right">Eamil : </th>
<td> {{ $user->email }} </td>
</tr>
<tr>
<th class="text-right">Phone : </th>
<td> {{ $user->phone }} </td>
</tr>
<tr>
<th class="text-right">Address : </th>
<td> {{ $user->address }} </td>
</tr>
</table>
</div>
</div>
</div>
</div>
@stop