এই Lesson এ Sales Invoice add করা দেখবো এবং কিভাবে Invoice এ কিভাবে items add করতে হয় তা দেখবো।
Routes:
Route::get('users/{id}/sales', 'UserSalesController@index')->name('user.sales');
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');
UserSalesController.php
namespace App\Http\Controllers;
use App\Http\Requests\InvoiceProductRequest;
use App\Http\Requests\InvoiceRequest;
use App\Product;
use App\SaleInvoice;
use App\SaleItem;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
class UserSalesController extends Controller
{
public function __construct()
{
$this->data['tab_menu'] = 'sales';
}
public function index( $id )
{
$this->data['user'] = User::findOrFail($id);
return view('users.sales.sales', $this->data);
}
public function createInvoice(InvoiceRequest $request, $user_id)
{
$formData = $request->all();
$formData['user_id'] = $user_id;
$formData['admin_id'] = Auth::id();
$invoice = SaleInvoice::create($formData);
return redirect()->route( 'user.sales.invoice_details', ['id' => $user_id, 'invoice_id' => $invoice->id] );
}
public function invoice($user_id, $invoice_id)
{
$this->data['user'] = User::findOrFail($user_id);
$this->data['invoice'] = SaleInvoice::findOrFail($invoice_id);
$this->data['products'] = Product::arrayForSelect();
return view('users.sales.invoice', $this->data);
}
public function addItem(InvoiceProductRequest $request, $user_id, $invoice_id)
{
$formData = $request->all();
$formData['sale_invoice_id'] = $invoice_id;
if( SaleItem::create($formData) ) {
Session::flash('message', 'Item Added Successfully');
}
return redirect()->route( 'user.sales.invoice_details', ['id' => $user_id, 'invoice_id' => $invoice_id] );
}
public function destroyItem($user_id, $invoice_id, $item_id)
{
if( SaleItem::destroy( $item_id ) ) {
Session::flash('message', 'Item Deleted Successfully');
}
return redirect()->route( 'user.sales.invoice_details', ['id' => $user_id, 'invoice_id' => $invoice_id] );
}
public function destroy($user_id, $invoice_id)
{
if( SaleInvoice::destroy($invoice_id) ) {
Session::flash('message', 'Invoice Deleted Successfully');
}
return redirect()->route( 'user.sales', ['id' => $user_id] );
}
}
InvoiceProductRequest.php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class InvoiceProductRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'product_id' => 'numeric|required',
'price' => 'required',
'quantity' => 'required',
'total' => 'required'
];
}
}
app/Admin.php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class Admin extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function payments()
{
return $this->hasMany(Payment::class);
}
public function receipts()
{
return $this->hasMany(Receipt::class);
}
public function sales()
{
return $this->hasMany(SaleInvoice::class);
}
}
app/Product.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = ['title', 'description', 'category_id', 'cost_price', 'price'];
public function category()
{
return $this->belongsTo(Category::class);
}
/**
* Getting array for select option
**/
public static function arrayForSelect() {
$arr = [];
$products = Product::all();
foreach ($products as $product) {
$arr[$product->id] = $product->title;
}
return $arr;
}
}
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);
}
}
app/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);
}
public function product()
{
return $this->belongsTo(Product::class);
}
}
database/migrations/2020_09_06_134357_add_note_to_sales_and_purchase_table.php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddNoteToSalesAndPurchaseTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('sale_invoices', function (Blueprint $table) {
$table->text('note')->nullable()->after('date');
});
Schema::table('purchase_invoices', function (Blueprint $table) {
$table->text('note')->nullable()->after('date');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('sale_invoices', function (Blueprint $table) {
$table->dropColumn('note');
});
Schema::table('purchase_invoices', function (Blueprint $table) {
$table->dropColumn('note');
});
}
}
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>
<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>
{{-- 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 Receipt --}}
<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/sales/sales.blade.php
@extends('users.user_layout')
@section('user_content')
<!-- DataTales Example -->
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary"> Sales of <strong>{{ $user->name }} </strong></h6>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Challen No</th>
<th>Customer</th>
<th>Date</th>
<th>Items</th>
<th>Total</th>
<th class="text-right">Actions</th>
</tr>
</thead>
<tbody>
<?php
$totalItem = 0;
$grandTotal = 0;
?>
@foreach ($user->sales as $sale)
<tr>
<td> {{ $sale->challan_no }} </td>
<td> {{ $user->name }} </td>
<td> {{ $sale->date }} </td>
<td>
<?php
$itemQty = $sale->items()->sum('quantity');
$totalItem += $itemQty;
echo $itemQty;
?>
</td>
<td>
<?php
$total = $sale->items()->sum('total');
$grandTotal += $total;
echo $total;
?>
</td>
<td class="text-right">
<form method="POST" action=" {{ route('user.sales.destroy', ['id' => $user->id, 'invoice_id' => $sale->id ]) }} ">
<a class="btn btn-primary btn-sm" href="{{ route('user.sales.invoice_details', ['id' => $user->id, 'invoice_id' => $sale->id]) }}">
<i class="fa fa-eye"></i>
</a>
@if($itemQty == 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>
<tfoot>
<tr>
<th>Challen No</th>
<th>Customer</th>
<th>Date</th>
<th> {{ $totalItem }} </th>
<th> {{ $grandTotal }} </th>
<th class="text-right">Actions</th>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
@stop
users/sales/invoice.blade.php
@extends('users.user_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 Inoice 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>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> {{ $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>
<tfoot>
<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> {{ $invoice->items()->sum('total') }} </th>
<th></th>
</tfoot>
</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>
@stop