📘 Module 7: POS (Billing System) UI

← Back to Course Outline

1. Add "New Sale" Page with Product Dropdown

Billing ਕਰਨ සඳහා නව UI එකක් නිර්මාණය කරමු. පළමුව, sales handle කිරීමට නව Controller එකක් සාදා, ඊට අදාළ route එක සකස් කරමු.

php artisan make:controller SaleController

දැන් routes/web.php file එකට "New Sale" page එක පෙන්වීමට route එකක් එකතු කරමු.


// In routes/web.php
use App\Http\Controllers\SaleController;

// ... other routes
Route::get('/sales/create', [SaleController::class, 'create'])->name('sales.create')->middleware('auth');
            

Controller එකේ create method එක තුල, database එකෙන් සියලුම products ගෙන view එකට pass කරමු.

Controller Logic: app/Http/Controllers/SaleController.php


use App\Models\Product; // Import Product model
use Illuminate\Http\Request;

class SaleController extends Controller
{
    public function create()
    {
        $products = Product::all();
        return view('sales.create', compact('products'));
    }
}
            

2. Select Product, Enter Quantity, Auto-Calculate Total

දැන් view file එක සාදමු. මෙහි product තේරීමට dropdown එකක්, quantity ඇතුලත් කිරීමට input එකක්, සහ JavaScript භාවිතයෙන් මුළු මුදල auto-calculate වන ආකාරය සකස් කරමු.

View: resources/views/sales/create.blade.php


@extends('layouts.app')

@section('content')
<div class="container">
    <h1>New Sale / Billing</h1>
    
    <!-- Product Selection Form -->
    <div class="row mb-3">
        <div class="col-md-4">
            <label for="product" class="form-label">Select Product</label>
            <select id="product" class="form-select">
                <option selected disabled>Choose...</option>
                @foreach($products as $product)
                    <option value="{{ $product->id }}" data-price="{{ $product->price }}">
                        {{ $product->name }}
                    </option>
                @endforeach
            </select>
        </div>
        <div class="col-md-2">
            <label for="quantity" class="form-label">Quantity</label>
            <input type="number" id="quantity" class="form-control" value="1" min="1">
        </div>
        <div class="col-md-2">
            <label for="price" class="form-label">Price</label>
            <input type="text" id="price" class="form-control" readonly>
        </div>
        <div class="col-md-2">
            <label for="total" class="form-label">Total</label>
            <input type="text" id="total" class="form-control" readonly>
        </div>
        <div class="col-md-2 d-flex align-items-end">
            <button class="btn btn-primary w-100">Add to Cart</button>
        </div>
    </div>
    <!-- Cart Display Table -->
</div>

@push('scripts')
<script>
    document.addEventListener('DOMContentLoaded', function () {
        const productSelect = document.getElementById('product');
        const quantityInput = document.getElementById('quantity');
        const priceInput = document.getElementById('price');
        const totalInput = document.getElementById('total');

        function calculateTotal() {
            const selectedOption = productSelect.options[productSelect.selectedIndex];
            if (!selectedOption.value || selectedOption.disabled) {
                priceInput.value = '';
                totalInput.value = '';
                return;
            }
            
            const price = parseFloat(selectedOption.getAttribute('data-price'));
            const quantity = parseInt(quantityInput.value);
            const total = price * quantity;
            
            priceInput.value = price.toFixed(2);
            totalInput.value = total.toFixed(2);
        }

        productSelect.addEventListener('change', calculateTotal);
        quantityInput.addEventListener('input', calculateTotal);
    });
</script>
@endpush
@endsection
            

Layout file එකේ (app.blade.php) </body> tag එකට පෙර @stack('scripts') යන්න එකතු කිරීමට වග බලා ගන්න.

3. Add to Cart (Client-Side Display)

"Add to Cart" button එක click කළ විට, තෝරාගත් භාණ්ඩය පහළින් ඇති table එකකට (cart) එකතු වන ලෙස සකස් කරමු. මෙය server එකට data යැවීමට පෙර, browser එක තුළම (client-side) සිදුවේ.

ඉහත create.blade.php file එකේ "Cart Display Table" comment එකට පහළින් මෙම HTML code එක එකතු කරන්න.


<!-- Cart Display Table -->
<table class="table">
    <thead>
        <tr>
            <th>Product</th>
            <th>Quantity</th>
            <th>Price</th>
            <th>Total</th>
            <th>Action</th>
        </tr>
    </thead>
    <tbody id="cart-body">
        <!-- Cart items will be added here dynamically -->
    </tbody>
</table>
<h3 class="text-end">Grand Total: <span id="grand-total">0.00</span></h3>
            

ඉන්පසු, JavaScript code එක update කර cart එකට item එකතු කිරීමේ functionality එක හදමු.

4. Sinhala Explanation: Form Handling

Form Handling (ෆෝරම හැසිරවීම)

වෙබ් පිටුවක ඇති form එකක් යනු user ගෙන් දත්ත ලබාගෙන server එකට යැවීමට ඇති මාර්ගයයි. මෙම ක්‍රියාවලිය කොටස් දෙකකට බෙදිය හැක.

  • Client-Side (Browser එක තුල): User විසින් form එකේ දත්ත පුරවන විට, JavaScript භාවිතා කර අපට විවිධ දේ කළ හැක. උදාහරණයක් ලෙස, product එකක් තේරූ සැනින් total price එක ගණනය කර පෙන්වීම.
  • Server-Side (Laravel තුල): User විසින් "Submit" button එක click කළ පසු, form එකේ දත්ත server එක වෙතට යවනු ලැබේ. Server එකේදී, Controller එක මගින් එම දත්ත භාරගෙන, ඒවා නිවැරදිදැයි පරීක්ෂා කර (validation), database එකේ save කිරීම වැනි ක්‍රියාවන් සිදු කරයි.

උදාහරණයක්: ඔබ බැංකුවක form එකක් පුරවනවා යැයි සිතන්න.

  • HTML/CSS: ඔබට ලැබෙන හිස් form පත්‍රය.
  • Client-Side JavaScript: ඔබ form එක පුරවන ගමන් calculator එකකින් ගණන් හදා කොටු පිරවීම.
  • Form Submit: ඔබ පිරවූ form එක කවුන්ටරයේ නිලධාරියාට භාර දීම.
  • Server-Side (Laravel): නිලධාරියා form එක භාරගෙන, එහි ඇති සියලු විස්තර නිවැරදිදැයි පරීක්ෂා කර (validation), එය file එකකට දමා (database save) ඔබට ප رسیدයක් ලබා දීම.