50 lines
1.6 KiB
PHP
50 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace Database\Factories;
|
|
|
|
use App\Models\Sales;
|
|
use App\Models\User;
|
|
use App\Models\Transaksi;
|
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
|
|
|
class TransaksiFactory extends Factory
|
|
{
|
|
protected $model = Transaksi::class;
|
|
|
|
public function definition(): array
|
|
{
|
|
$sales = Sales::inRandomOrder()->first();
|
|
$kasir = User::inRandomOrder()->first();
|
|
|
|
$date = $this->faker->dateTimeBetween('-3 months');
|
|
$ongkos_bikin = $this->faker->numberBetween(8, 12) * 10000;
|
|
|
|
return [
|
|
'id_kasir' => $kasir?->id,
|
|
'id_sales' => $sales?->id,
|
|
'nama_sales' => $sales?->nama,
|
|
'kode_transaksi' => 'bwabwa' . $this->faker->unique()->numberBetween(1, 9999), // temporary, will be updated in configure()
|
|
'nama_pembeli' => $this->faker->name(),
|
|
'no_hp' => $this->faker->phoneNumber(),
|
|
'alamat' => $this->faker->address(),
|
|
'ongkos_bikin' => $ongkos_bikin,
|
|
'total_harga' => $ongkos_bikin,
|
|
'created_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();
|
|
});
|
|
}
|
|
}
|