partial update transaction work with stock product
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateWorkProductsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('work_products', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('work_id')->constrained('works')->onDelete('cascade');
|
||||
$table->foreignId('product_id')->constrained('products')->onDelete('cascade');
|
||||
$table->decimal('quantity_required', 10, 2)->default(1);
|
||||
$table->text('notes')->nullable();
|
||||
$table->timestamps();
|
||||
|
||||
// Prevent duplicate work-product combinations
|
||||
$table->unique(['work_id', 'product_id']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('work_products');
|
||||
}
|
||||
}
|
||||
77
database/seeders/WorkProductSeeder.php
Normal file
77
database/seeders/WorkProductSeeder.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Models\Work;
|
||||
use App\Models\Product;
|
||||
use App\Models\WorkProduct;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class WorkProductSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
// Get some sample works and products
|
||||
$works = Work::take(3)->get();
|
||||
$products = Product::where('active', true)->take(5)->get();
|
||||
|
||||
if ($works->isEmpty() || $products->isEmpty()) {
|
||||
$this->command->info('Tidak ada data Work atau Product untuk seeding. Silakan buat data tersebut terlebih dahulu.');
|
||||
return;
|
||||
}
|
||||
|
||||
// Sample work-product relationships
|
||||
$workProductData = [
|
||||
[
|
||||
'work_id' => $works->first()->id,
|
||||
'product_id' => $products->first()->id,
|
||||
'quantity_required' => 2.0,
|
||||
'notes' => 'Digunakan untuk pembersihan awal'
|
||||
],
|
||||
[
|
||||
'work_id' => $works->first()->id,
|
||||
'product_id' => $products->skip(1)->first()->id,
|
||||
'quantity_required' => 1.0,
|
||||
'notes' => 'Untuk finishing'
|
||||
],
|
||||
[
|
||||
'work_id' => $works->skip(1)->first()->id,
|
||||
'product_id' => $products->skip(2)->first()->id,
|
||||
'quantity_required' => 3.0,
|
||||
'notes' => 'Komponen utama'
|
||||
],
|
||||
[
|
||||
'work_id' => $works->skip(1)->first()->id,
|
||||
'product_id' => $products->skip(3)->first()->id,
|
||||
'quantity_required' => 0.5,
|
||||
'notes' => 'Pelumas tambahan'
|
||||
],
|
||||
[
|
||||
'work_id' => $works->skip(2)->first()->id,
|
||||
'product_id' => $products->skip(4)->first()->id,
|
||||
'quantity_required' => 1.0,
|
||||
'notes' => 'Standard usage'
|
||||
]
|
||||
];
|
||||
|
||||
foreach ($workProductData as $data) {
|
||||
WorkProduct::firstOrCreate(
|
||||
[
|
||||
'work_id' => $data['work_id'],
|
||||
'product_id' => $data['product_id']
|
||||
],
|
||||
[
|
||||
'quantity_required' => $data['quantity_required'],
|
||||
'notes' => $data['notes']
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
$this->command->info('Work Product relationships seeded successfully!');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user