[update] TransaksiController, Transaksi.php, Item.php
This commit is contained in:
parent
420cf47f20
commit
cf8f456fb4
@ -14,7 +14,7 @@ class TransaksiController extends Controller
|
|||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
$limit = request()->query('limit', null);
|
$limit = request()->query('limit', null);
|
||||||
$query = Transaksi::with(['kasir', 'sales', 'items.item.produk'])->latest();
|
$query = Transaksi::with(['kasir', 'sales', 'itemTransaksi.item.produk'])->latest();
|
||||||
if ($limit) {
|
if ($limit) {
|
||||||
$query->limit((int)$limit);
|
$query->limit((int)$limit);
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
use App\Models\itemTransaksi;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
|
||||||
class Item extends Model
|
class Item extends Model
|
||||||
@ -14,10 +13,37 @@ class Item extends Model
|
|||||||
'id_produk',
|
'id_produk',
|
||||||
'id_nampan',
|
'id_nampan',
|
||||||
'is_sold',
|
'is_sold',
|
||||||
|
'kode_item', // ✅ ditambahkan agar bisa diisi otomatis
|
||||||
];
|
];
|
||||||
|
|
||||||
protected $hidden = ['created_at', 'updated_at', 'deleted_at'];
|
protected $hidden = ['created_at', 'updated_at', 'deleted_at'];
|
||||||
|
|
||||||
|
// ✅ Auto-generate kode_item setiap kali create
|
||||||
|
protected static function boot()
|
||||||
|
{
|
||||||
|
parent::boot();
|
||||||
|
|
||||||
|
static::creating(function ($item) {
|
||||||
|
$prefix = 'ITM';
|
||||||
|
$date = now()->format('Ymd');
|
||||||
|
|
||||||
|
// Cari item terakhir yg dibuat hari ini
|
||||||
|
$lastItem = self::whereDate('created_at', now()->toDateString())
|
||||||
|
->orderBy('id', 'desc')
|
||||||
|
->first();
|
||||||
|
|
||||||
|
$number = 1;
|
||||||
|
if ($lastItem && $lastItem->kode_item) {
|
||||||
|
// Ambil 4 digit terakhir dari kode_item
|
||||||
|
$lastNumber = intval(substr($lastItem->kode_item, -4));
|
||||||
|
$number = $lastNumber + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format: ITM202509090001
|
||||||
|
$item->kode_item = $prefix . $date . str_pad($number, 4, '0', STR_PAD_LEFT);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public function produk()
|
public function produk()
|
||||||
{
|
{
|
||||||
return $this->belongsTo(Produk::class, 'id_produk');
|
return $this->belongsTo(Produk::class, 'id_produk');
|
||||||
|
@ -9,7 +9,9 @@ class Transaksi extends Model
|
|||||||
{
|
{
|
||||||
/** @use HasFactory<\Database\Factories\TransaksiFactory> */
|
/** @use HasFactory<\Database\Factories\TransaksiFactory> */
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
|
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
|
'kode_transaksi', // ✅ Tambahin kolom kode transaksi
|
||||||
'id_kasir',
|
'id_kasir',
|
||||||
'id_sales',
|
'id_sales',
|
||||||
'nama_sales',
|
'nama_sales',
|
||||||
@ -23,6 +25,24 @@ class Transaksi extends Model
|
|||||||
|
|
||||||
protected $hidden = ['updated_at', 'deleted_at'];
|
protected $hidden = ['updated_at', 'deleted_at'];
|
||||||
|
|
||||||
|
// ✅ Auto-generate kode_transaksi saat create
|
||||||
|
protected static function boot()
|
||||||
|
{
|
||||||
|
parent::boot();
|
||||||
|
|
||||||
|
// Setelah transaksi berhasil dibuat (sudah punya ID)
|
||||||
|
static::created(function ($transaksi) {
|
||||||
|
if (!$transaksi->kode_transaksi) {
|
||||||
|
$prefix = "TRS";
|
||||||
|
$date = $transaksi->created_at->format('Ymd');
|
||||||
|
$number = str_pad($transaksi->id, 4, '0', STR_PAD_LEFT);
|
||||||
|
|
||||||
|
$transaksi->kode_transaksi = $prefix . $date . $number;
|
||||||
|
$transaksi->save();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public function kasir()
|
public function kasir()
|
||||||
{
|
{
|
||||||
return $this->belongsTo(User::class, 'id_kasir');
|
return $this->belongsTo(User::class, 'id_kasir');
|
||||||
|
@ -4,37 +4,46 @@ namespace Database\Factories;
|
|||||||
|
|
||||||
use App\Models\Sales;
|
use App\Models\Sales;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
|
use App\Models\Transaksi;
|
||||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
|
||||||
/**
|
|
||||||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Transaksi>
|
|
||||||
*/
|
|
||||||
class TransaksiFactory extends Factory
|
class TransaksiFactory extends Factory
|
||||||
{
|
{
|
||||||
/**
|
protected $model = Transaksi::class;
|
||||||
* Define the model's default state.
|
|
||||||
*
|
|
||||||
* @return array<string, mixed>
|
|
||||||
*/
|
|
||||||
public function definition(): array
|
public function definition(): array
|
||||||
{
|
{
|
||||||
|
|
||||||
$sales = Sales::inRandomOrder()->first();
|
$sales = Sales::inRandomOrder()->first();
|
||||||
$kasir = User::inRandomOrder()->first();
|
$kasir = User::inRandomOrder()->first();
|
||||||
|
|
||||||
$date = $this->faker->dateTimeBetween('-3 months');
|
$date = $this->faker->dateTimeBetween('-3 months');
|
||||||
$ongkos_bikin = $this->faker->numberBetween(8, 12) * 10000;
|
$ongkos_bikin = $this->faker->numberBetween(8, 12) * 10000;
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'id_kasir' => $kasir?->id,
|
'id_kasir' => $kasir?->id,
|
||||||
'id_sales' => $sales?->id,
|
'id_sales' => $sales?->id,
|
||||||
'nama_sales' => $sales?->nama,
|
'nama_sales' => $sales?->nama,
|
||||||
|
'kode_transaksi' => 'bwabwa' . $this->faker->unique()->numberBetween(1, 9999), // temporary, will be updated in configure()
|
||||||
'nama_pembeli' => $this->faker->name(),
|
'nama_pembeli' => $this->faker->name(),
|
||||||
'no_hp' => $this->faker->phoneNumber(),
|
'no_hp' => $this->faker->phoneNumber(),
|
||||||
'alamat' => $this->faker->address(),
|
'alamat' => $this->faker->address(),
|
||||||
'ongkos_bikin' => $ongkos_bikin,
|
'ongkos_bikin' => $ongkos_bikin,
|
||||||
'total_harga' => $ongkos_bikin,
|
'total_harga' => $ongkos_bikin,
|
||||||
'created_at' => $date,
|
'created_at' => $date,
|
||||||
'updated_at' => $date,
|
'updated_at' => $date,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function configure()
|
||||||
|
{
|
||||||
|
return $this->afterCreating(function (Transaksi $transaksi) {
|
||||||
|
// generate kode transaksi TRS202509090001
|
||||||
|
$prefix = "TRS";
|
||||||
|
$date = $transaksi->created_at->format('Ymd');
|
||||||
|
$number = str_pad($transaksi->id, 4, '0', STR_PAD_LEFT);
|
||||||
|
|
||||||
|
$transaksi->kode_transaksi = $prefix . $date . $number;
|
||||||
|
$transaksi->save();
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,33 +0,0 @@
|
|||||||
<?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('personal_access_tokens', function (Blueprint $table) {
|
|
||||||
$table->id();
|
|
||||||
$table->morphs('tokenable');
|
|
||||||
$table->text('name');
|
|
||||||
$table->string('token', 64)->unique();
|
|
||||||
$table->text('abilities')->nullable();
|
|
||||||
$table->timestamp('last_used_at')->nullable();
|
|
||||||
$table->timestamp('expires_at')->nullable()->index();
|
|
||||||
$table->timestamps();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reverse the migrations.
|
|
||||||
*/
|
|
||||||
public function down(): void
|
|
||||||
{
|
|
||||||
Schema::dropIfExists('personal_access_tokens');
|
|
||||||
}
|
|
||||||
};
|
|
@ -0,0 +1,25 @@
|
|||||||
|
<?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()
|
||||||
|
{
|
||||||
|
Schema::table('items', function (Blueprint $table) {
|
||||||
|
$table->string('kode_item')->unique()->after('id');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::table('items', function (Blueprint $table) {
|
||||||
|
$table->dropColumn('kode_item');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
@ -0,0 +1,27 @@
|
|||||||
|
<?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()
|
||||||
|
{
|
||||||
|
Schema::table('transaksis', function (Blueprint $table) {
|
||||||
|
$table->string('kode_transaksi')->unique()->after('id');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::table('transaksis', function (Blueprint $table) {
|
||||||
|
$table->dropColumn('kode_transaksi');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
};
|
@ -20,7 +20,7 @@ class DatabaseSeeder extends Seeder
|
|||||||
public function run(): void
|
public function run(): void
|
||||||
{
|
{
|
||||||
User::factory()->create([
|
User::factory()->create([
|
||||||
'nama' => 'iwan',
|
'nama' => 'andre',
|
||||||
'role' => 'owner',
|
'role' => 'owner',
|
||||||
'password' => bcrypt('123123'),
|
'password' => bcrypt('123123'),
|
||||||
]);
|
]);
|
||||||
|
Loading…
Reference in New Issue
Block a user