116 lines
3.7 KiB
PHP
116 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace Database\Factories;
|
|
|
|
use App\Models\Pelanggan;
|
|
use App\Models\Template;
|
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Pelanggan>
|
|
*/
|
|
class PelangganFactory extends Factory
|
|
{
|
|
protected $model = Pelanggan::class;
|
|
|
|
public function definition(): array
|
|
{
|
|
// Get a random template
|
|
$template = Template::inRandomOrder()->first() ?? Template::factory()->create();
|
|
|
|
// Generate form data based on template's form fields
|
|
$formData = $this->generateFormData($template->form['fields'] ?? []);
|
|
|
|
// Generate unique invitation code
|
|
$invitationCode = 'INV-' . strtoupper(Str::random(6));
|
|
while (Pelanggan::where('invitation_code', $invitationCode)->exists()) {
|
|
$invitationCode = 'INV-' . strtoupper(Str::random(6));
|
|
}
|
|
|
|
return [
|
|
'nama_pemesan' => $this->faker->name(),
|
|
'email' => $this->faker->unique()->safeEmail(),
|
|
'no_tlpn' => $this->faker->phoneNumber(),
|
|
'template_id' => $template->id,
|
|
'form' => $formData,
|
|
'harga' => $template->harga,
|
|
'status' => $this->faker->randomElement(['menunggu', 'diterima', 'ditolak']),
|
|
'invitation_code' => $invitationCode,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Generate form data based on template fields
|
|
*
|
|
* @param array $fields
|
|
* @return array
|
|
*/
|
|
private function generateFormData(array $fields): array
|
|
{
|
|
$formData = [];
|
|
|
|
foreach ($fields as $field) {
|
|
$name = $field['name'];
|
|
$type = $field['type'] ?? 'text';
|
|
|
|
switch ($type) {
|
|
case 'text':
|
|
$formData[$name] = $this->generateTextField($name);
|
|
break;
|
|
case 'email':
|
|
$formData[$name] = $this->faker->safeEmail();
|
|
break;
|
|
case 'date':
|
|
$formData[$name] = $this->faker->date('Y-m-d', 'now +1 month');
|
|
break;
|
|
case 'number':
|
|
$formData[$name] = $this->faker->numberBetween(1, 100);
|
|
break;
|
|
case 'textarea':
|
|
$formData[$name] = $this->faker->paragraph();
|
|
break;
|
|
case 'file':
|
|
$formData[$name] = 'files/' . $this->faker->uuid() . '.jpg';
|
|
break;
|
|
default:
|
|
$formData[$name] = $this->faker->word();
|
|
}
|
|
}
|
|
|
|
return $formData;
|
|
}
|
|
|
|
/**
|
|
* Generate text field data based on field name
|
|
*
|
|
* @param string $name
|
|
* @return string
|
|
*/
|
|
private function generateTextField(string $name): string
|
|
{
|
|
if (str_contains($name, 'nama_')) {
|
|
return $this->faker->name();
|
|
}
|
|
if (str_contains($name, 'alamat')) {
|
|
return $this->faker->address();
|
|
}
|
|
if (str_contains($name, 'link_gmaps')) {
|
|
return 'https://maps.google.com/?q=' . $this->faker->latitude() . ',' . $this->faker->longitude();
|
|
}
|
|
if (str_contains($name, 'instagram') || str_contains($name, 'facebook') || str_contains($name, 'twitter')) {
|
|
return 'https://' . str_replace('_', '.', $name) . '/' . $this->faker->userName();
|
|
}
|
|
if (str_contains($name, 'waktu')) {
|
|
return $this->faker->time('H:i');
|
|
}
|
|
if (str_contains($name, 'rekening')) {
|
|
return $this->faker->bankAccountNumber();
|
|
}
|
|
if (str_contains($name, 'link_music')) {
|
|
return 'https://music.example.com/' . $this->faker->uuid();
|
|
}
|
|
|
|
return $this->faker->word();
|
|
}
|
|
} |