ক্লাস নং-২০(পার্ট-১০) invoice,payment recive

ক্লাস নং-২০(পার্ট-১০) invoice,payment recive

Table of contents

No heading

No headings in the article.

আজকের ক্লাসে আমাদের মূলত invoice,payment recive নিয়ে এর সাথে আরো বেশ কিছু বিষয় থাকবে চলুন তাহলে শুরু করি।

তো প্রথমেই আমাদের একটা model লাগবে যেটা আমরা Payment Model নামে নিব।

php artisan make:model Payment -mc

কমান্ড টি রান করলে আমাদের migration and controller create হয়ে যাবে একই সাথে।

এবার প্রথমেই আমাদের payments table এ আসি।

   $table->id();
            $table->float('amount');
            $table->unsignedBigInteger('invoice_id');
            $table->timestamps();

            $table->foreign('invoice_id')->references('id')->on('invoices')->onDelete('cascade');

এবার আমাদের livewire এর frontend এর blade.php file এ আসি।

@if(!empty($selectedCourse))
                <p class="mb-4">Price: ${{number_format($selectedCourse->price, 2)}}</p>

                <div class="mb-4">
                    <input wire:model.lazy="payment" type="number" step=".01" max="{{number_format($selectedCourse->price, 2)}}" class="lms-input" placeholder="Payment now">
                </div>

                @include('components.wire-loading-btn')
            @endif

এবার আমাদের livewire এর admisson এর যে controller আছে সেখানে আমাদের course এ student কে assign করে দেব।

$this->selectedCourse->students()->attach($user->id);

এবার আমাদের payment option create করব।এখানে যেহেতু আমাদের payment টা required না তাই আমরা এটাকে condition দিয়ে তৈরি করব ।

        if(!empty($this->payment)) {
            Payment::create([
                'amount' => $this->payment,
                'invoice_id' => $invoice->id,
            ]);
        }

এবার আমাদের একটা migration চালাতে হবে এবং admisson দিয়ে চেক করতে হবে।

এবং আমাদের যেহেতু amount and invoice_id টা payment model এ যাবে তাই আমরা সেখানে fillable করে দেব।

    protected $fillable =[
        'amount',
        'invoice_id'
    ];

এবার যদি আমরা migration চালিয়ে তারপর আমাদের admisson section এ গিয়ে admisson করানোর চেষ্টা করি তাহলে আমরা দেখব আমরা .

  • -admisson done

  • -invoice created

  • -payment recive

এবার আমাদের একটা ভিউ লাগবে আমাদের এগুলো frontend এ শো করানোর জন্য। তো প্রথমেই আমাদের একটা রাউট তৈরি করতে হবে invoiceController দিয়ে।

    Route::get('/invoices', [InvoiceController::class, 'index'])->name('invoice-index');

এবার আমাদের navigation file এ Invoice name দিয়ে একটা মেনু নিয়ে নেই।

 <x-nav-link :href="route('invoices')" :active="request()->routeIs('invoice-index')">
  {{ __('Invoice') }}
</x-nav-link>

এবার আমাদের এটা frontend এ শো করানোর জন্য একটা livewire index create করতে হবে।

php artisan livewire:make InvoiceIndex

এবার আমাদের navigation e একটা menu লাগবে আমরা lead নামের যে ফোল্ডার আছে সেটাকে কপি করে invoice name দিতে পারি এবং সেই index file নিচের কোড দিয়ে দেই।

<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            {{ __('Invoices') }}
        </h2>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">

                <livewire:invoice-index/>
            </div>
        </div>
    </div>
</x-app-layout>

এবার আমরা আমাদের invoiceindex controller এ গিয়ে আমরা invoice এর সাথে invoice গুলো variable আকারে পাঠিয়ে দেব livewire এর frontend এ।

 $invoices = Invoice::paginate(50);
        return view('livewire.invoice-index', [
            'invoices' => $invoices
        ]);

এবার আমাদের frontend এর জন্য invoice index make করতে হবে frontend এর জন্য।

<div>
    <table class="w-full table-auto">
        <tr>
            <th class="border px-4 py-2 text-left">ID</th>
            <th class="border px-4 py-2 text-left">User</th>
            <th class="border px-4 py-2 text-left">Due date</th>
            <th class="border px-4 py-2">Amount</th>
            <th class="border px-4 py-2">Paid</th>
            <th class="border px-4 py-2">Due</th>
            <th class="border px-4 py-2">Actions</th>
        </tr>
        @foreach($invoices as $invoice)
            <tr>
                <td class="border px-4 py-2">{{$invoice->id}}</td>
                <td class="border px-4 py-2">{{$invoice->user->name}}</td>
                <td class="border px-4 py-2">{{date('F j, Y', strtotime($invoice->due_date))}}</td>
                <td class="border px-4 py-2 text-center">${{$invoice->amount()['total']}}</td>
                <td class="border px-4 py-2 text-center">${{$invoice->amount()['paid']}}</td>
                <td class="border px-4 py-2 text-center">${{$invoice->amount()['due']}}</td>
                <td class="border px-4 py-2 text-center">
                    <div class="flex items-center justify-center">
                        <a class="mr-1" href="">
                            @include('components.icons.edit')
                        </a>

                        <a class="mr-1" href="{{route('invoice-show', $invoice->id)}}">
                            @include('components.icons.eye')
                        </a>

                        <form class="ml-1" onsubmit="return confirm('Are you sure?');" wire:submit.prevent="invoiceDelete({{$invoice->id}})">
                            <button type="submit">
                                @include('components.icons.trash')
                            </button>
                        </form>
                    </div>
                </td>
            </tr>
        @endforeach
    </table>
</div>

এবার আমরা আমদের invoice model এ ইউজার এর সাথে রিলেশন তৈরি করতে হবে।

    public function user() {
        return $this->belongsTo(User::class, 'user_id');
    }

এবার আসি আমাদের invoice make এর কাজে ।invoice make এর জন্য লারাভেল এ দারুন একটা প্যাক আছে laravel invoice name এর।আমরা খুব সহজেই সেটাকে install করে নিতে পারি।

composer require laraveldaily/laravel-invoices:^3.0

এবার আমাদের একটা রাউট তৈরি করতে হবে।

 Route::get('/invoice/{id}', [InvoiceController::class, 'show'])->name('invoice-show');

এবার আমাদের invoicecontroller এ গিয়ে আমাদের controller make করতে হবে।

<?php

namespace App\Http\Controllers;

use App\Models\Invoice;
use Illuminate\Http\Request;
use LaravelDaily\Invoices\Classes\Buyer;
use LaravelDaily\Invoices\Classes\InvoiceItem;




class InvoiceController extends Controller
{
    //
    public function index(){
        return view('invoice.index');
    }

    public function show($id) {
        $DBinvoice = Invoice::findOrFail($id);

        $customer = new Buyer([
            'name'          => $DBinvoice->user->name,
            'custom_fields' => [
                'email' => $DBinvoice->user->email,
            ],
        ]);

        $items = [];
        foreach($DBinvoice->items as $item) {
            $items[] = (new InvoiceItem())->title($item->name)->pricePerUnit($item->price)->quantity($item->quantity);
        }

        // payments
        foreach($DBinvoice->payments as $payment) {
            $items[] = (new InvoiceItem())->title('Payment')->pricePerUnit(-$payment->amount)->quantity(1);
        }

        $invoice = \LaravelDaily\Invoices\Invoice::make()
            ->buyer($customer)
            ->addItems($items);

        return $invoice->stream();
    }
}

ওপরের কোড টা আমরা laravelDaily থেকে নিয়ে আমাদের মত করে মডিফাই করতে পারব।

এবার যদি আপনি আপনার invoice এ যান তাহলে এটা খুব সুন্দর ভাবে generate হবে ।এর পর আরো মডিফিকেশন আছে যেগুলো আমরা পরবর্তিতে করব।

আশা করি বুঝতে পেরেছেন আর কোন সমস্যা হলে আমাদের discord group তো আছেই সেই সাথে আমাদের youtube channel and facebook group.

video link : https://www.youtube.com/watch?v=eSwAKlQwMnc discord link: https://discord.gg/9qWUUbQ3 facebook group :https://web.facebook.com/groups/1661735757554627/

Happy Learning

___Rakibul Islam___