新增單位管理以及一些功能修正
This commit is contained in:
29
database/migrations/2026_01_08_103000_create_units_table.php
Normal file
29
database/migrations/2026_01_08_103000_create_units_table.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('units', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('name')->unique()->comment('單位名稱');
|
||||
$table->string('code')->nullable()->comment('單位代碼 (如: kg)');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('units');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('products', function (Blueprint $table) {
|
||||
// Drop old string columns
|
||||
$table->dropColumn(['base_unit', 'large_unit', 'purchase_unit']);
|
||||
|
||||
// Add new foreign key columns
|
||||
$table->foreignId('base_unit_id')->nullable()->after('specification')->constrained('units')->nullOnDelete()->comment('基本庫存單位ID');
|
||||
$table->foreignId('large_unit_id')->nullable()->after('base_unit_id')->constrained('units')->nullOnDelete()->comment('大單位ID');
|
||||
$table->foreignId('purchase_unit_id')->nullable()->after('conversion_rate')->constrained('units')->nullOnDelete()->comment('採購單位ID');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('products', function (Blueprint $table) {
|
||||
// Remove foreign keys
|
||||
$table->dropForeign(['base_unit_id']);
|
||||
$table->dropForeign(['large_unit_id']);
|
||||
$table->dropForeign(['purchase_unit_id']);
|
||||
$table->dropColumn(['base_unit_id', 'large_unit_id', 'purchase_unit_id']);
|
||||
|
||||
// Add back string columns (nullable since data is lost)
|
||||
$table->string('base_unit')->nullable()->comment('基本庫存單位 (e.g. g, ml)');
|
||||
$table->string('large_unit')->nullable()->comment('大單位 (e.g. 桶, 箱)');
|
||||
$table->string('purchase_unit')->nullable()->comment('採購單位');
|
||||
});
|
||||
}
|
||||
};
|
||||
36
database/seeders/UnitSeeder.php
Normal file
36
database/seeders/UnitSeeder.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Models\Unit;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class UnitSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
$units = [
|
||||
['name' => '個', 'code' => 'pc'],
|
||||
['name' => '箱', 'code' => 'box'],
|
||||
['name' => '瓶', 'code' => 'btl'],
|
||||
['name' => '包', 'code' => 'pkg'],
|
||||
['name' => '公斤', 'code' => 'kg'],
|
||||
['name' => '公克', 'code' => 'g'],
|
||||
['name' => '公升', 'code' => 'l'],
|
||||
['name' => '毫升', 'code' => 'ml'],
|
||||
['name' => '籃', 'code' => 'bsk'],
|
||||
['name' => '桶', 'code' => 'bucket'],
|
||||
['name' => '罐', 'code' => 'can'],
|
||||
];
|
||||
|
||||
foreach ($units as $unit) {
|
||||
Unit::firstOrCreate(
|
||||
['name' => $unit['name']],
|
||||
['code' => $unit['code']]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user