📘 Module 6: Customer & Sales Module
1. Create customers Table & Model
POS පද්ධතියේ පාරිභෝගිකයන්ගේ තොරතුරු ගබඩා කිරීමට table එකක් අවශ්යයි. අපි මේ සඳහා Model සහ Migration එකවර සාදා ගනිමු.
php artisan make:model Customer -m
ඉහත command එකේ ඇති -m flag එක මගින් Customer model එක සමගම ඊට අදාළ migration file එකද සාදා දෙනු ලබයි.
දැන් database/migrations/ තුල සෑදුණු ..._create_customers_table.php file එකට අවශ්ය columns එකතු කරමු.
File: database/migrations/..._create_customers_table.php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('customers', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('phone')->nullable();
$table->timestamps();
});
}
// ...
};
දැන්, table එක database එකේ සෑදීමට migrate command එක run කරන්න.
php artisan migrate
2. Add Customer Form (Conceptual)
නව Customer කෙනෙක් ඇතුලත් කිරීමට අවශ්ය form එක සහ logic එක, Module 5 හිදී Products සඳහා අප සෑදූ CRUD ක්රියාවලියට බොහෝ සෙයින් සමාන වේ.
ඒ සඳහා CustomerController එකක්, Route::resource('customers', ...) එකක්, සහ index.blade.php, create.blade.php වැනි views අවශ්ය වනු ඇත. මෙම කොටසේදී අපි ප්රධාන වශයෙන් database relationships ගැන අවධානය යොමු කරන බැවින්, එම CRUD කොටස ඔබ විසින්ම කිරීමට අභ්යාසයක් ලෙස තබමු.
3. Relationship Between Products, Customers, and Sales
Relational Database එකක වැදගත්ම කොටස වන්නේ tables අතර ඇති සම්බන්ධතාවයයි. අපගේ sales table එක, products සහ customers tables සමග සම්බන්ධ වේ.
- එක් Product එකකට Sales ගණනාවක් (Many) තිබිය හැක. (One-to-Many)
- එක් Customer කෙනෙකුට Sales ගණනාවක් (Many) තිබිය හැක. (One-to-Many)
- එක් Sale එකක් අයිති එක් Product එකකට සහ එක් Customer කෙනෙකුට පමණි. (Belongs To)
මෙම සම්බන්ධතා Laravel Models තුල define කරමු.
Product Model: app/Models/Product.php
// ...
class Product extends Model
{
// ...
public function sales()
{
return $this->hasMany(Sale::class);
}
}
Customer Model: app/Models/Customer.php
// ...
class Customer extends Model
{
// ...
public function sales()
{
return $this->hasMany(Sale::class);
}
}
Sale Model: app/Models/Sale.php (මෙම model එක සාදා නොමැති නම් php artisan make:model Sale මගින් සාදන්න)
// ...
class Sale extends Model
{
// ...
public function product()
{
return $this->belongsTo(Product::class);
}
public function customer()
{
return $this->belongsTo(Customer::class);
}
}
මෙම relationships define කිරීමෙන්, අපට $sale->product->name ලෙස ඉතා පහසුවෙන් sale එකකට අදාළ product එකේ නම වැනි දත්ත ලබාගත හැක.
4. Sinhala Explanation of Relational DB
Relational Database (සම්බන්ධිත දත්ත සමුදාය) යනු කුමක්ද?
Relational Database කියන්නේ දත්ත, එකිනෙකට සම්බන්ධකම් ඇති වගු (tables) වල ගබඩා කරන ක්රමවේදයක්. එකම දේ නැවත නැවත ගබඩා කිරීම (data redundancy) අඩු කර, දත්ත කළමනාකරණය පහසු කිරීම මෙහි ප්රධාන අරමුණයි.
පුස්තකාලයක උදාහරණයෙන් තේරුම් ගනිමු:
හිතන්න පුස්තකාලයක පොත් ලොග් පොත් දෙකක් තියෙනවා.
- Members ලොග් පොත: සාමාජිකයන්ගේ විස්තර (නම, ලිපිනය, ID එක).
- Books ලොග් පොත: පොත් වල විස්තර (පොතේ නම, කතෘ, පොතේ ID එක).
දැන් සාමාජිකයෙක් පොතක් ගෙනියනකොට, ඒක සටහන් කරන්න වෙනම "Borrowed Books" ලොග් පොතක් තියෙනවා. මේ පොතේ, පොත ගෙනියන සාමාජිකයාගේ නම, ලිපිනය, පොතේ නම, කතෘ ඔක්කොම නැවත නැවත ලියනවා වෙනුවට, කෙටි ක්රමයක් පාවිච්චි කරනවා.
ඔවුන් ලියන්නේ "Member ID 101, Book ID 555 අරන් ගියා" කියලා විතරයි.
- Primary Key (ප්රාථමික යතුර): Members ලොග් පොතේ හැම සාමාජිකයෙකුටම තියෙන unique ID එක (e.g., Member ID 101) Primary Key එකක්. එයින් ඒ සාමාජිකයාව නිශ්චිතවම හඳුනාගන්න පුළුවන්.
- Foreign Key (විදේශීය යතුර): "Borrowed Books" ලොග් පොතේ සටහන් කරපු Member ID 101 කියන එක Foreign Key එකක්. එය "Borrowed Books" table එක, "Members" table එකට සම්බන්ධ කරන යතුරයි. මේ ID එක දිහා බලලා, Members ලොග් පොතෙන් ඒ කවුද කියලා හරියටම හොයාගන්න පුළුවන්.
අපේ POS system එකේ, sales table එකේ තියෙන product_id කියන්නේ අන්න ඒ වගේ Foreign Key එකක්. එය products table එකට සම්බන්ධයි.