ldc-supportweb/database/factories/InvoiceFactory.php
2024-07-22 11:32:03 +02:00

141 lines
4.3 KiB
PHP

<?php
namespace Database\Factories;
use App\Enums\Invoices\TaskTypeEnum;
use App\Enums\Invoices\InvoiceStatusEnum;
use App\Models\Invoice;
use App\Models\Package;
use Illuminate\Database\Eloquent\Factories\Factory;
class InvoiceFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Invoice::class;
/**
* A JSON object, each line has to be contructed using these
* fields description, days, type Invoice\TaskType, amount.
*
* @var array
*/
protected $tasks = [];
/**
* The total calculated using all tasks.
*
* @var float
*/
protected $total = 0;
/**
* The total days calculated using all tasks.
*
* @var float
*/
protected $totalDays = 0;
/**
* The date of the signing.
*
* @var boolean
*/
protected $signedDateBoolean = 0;
/**
* The date of the shipping to the client.
*
* @var boolean
*/
protected $sentClientDateBoolean = 0;
/**
* The date of the shipping to the accounting.
*
* @var boolean
*/
protected $sentAccountingDateBoolean = 0;
/**
* Define the model's default state.
*
* @return void
*/
private function customData(): void
{
\mt_srand();
$lines = \mt_rand(2, 9);
for ($i = 0; $i < $lines; $i++) {
$days = 0.0625 * $this->faker->numberBetween(1, 4);
$type = \collect(TaskTypeEnum::cases())->random();
$amount = 0;
switch ($type) {
case TaskTypeEnum::web:
$amount = $days * $type->price;
break;
case TaskTypeEnum::seo:
$amount = $days * $type->price;
break;
case TaskTypeEnum::package:
$amount = $days * (\optional(Package::query()->inRandomOrder()->first())->amount ??
$this->faker->randomFloat(null, 150, 650));
break;
case TaskTypeEnum::package:
$amount = $days * $this->faker->randomFloat(null, 150, 650);
break;
}
$this->totalDays += $days;
$this->total += $amount;
/** @var \App\Enums\Invoices\TaskTypeEnum */
$this->tasks[] = \array_merge(Invoice::getTaskStructure(), [
'desc' => $this->faker->words(9, true),
'days' => $days,
'type' => \collect(TaskTypeEnum::cases())->random()->value,
'amount' => $amount,
'package' => 1
]);
/** Dates */
$this->signedDateBoolean = $this->faker->boolean(25);
$this->sentClientDateBoolean = $this->faker->boolean(25);
$this->sentAccountingDateBoolean = $this->faker->boolean(25);
} //end for
}
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
$this->customData();
return [
'firstname' => $this->faker->firstName,
'lastname' => $this->faker->lastName,
'object' => $this->faker->words(3, true),
// * constructed object
'tasks' => $this->tasks,
// * constructed object result
'total' => $this->total,
// * constructed object result
'total_days' => $this->totalDays,
'comment' => $this->faker->text(\rand(20, 40)),
'show_days' => $this->faker->boolean(75),
'signed_date' =>
$this->signedDateBoolean ?
$this->faker->dateTimeBetween('-1 year', '+1 year') : null,
'sent_client_date' =>
$this->signedDateBoolean && $this->sentClientDateBoolean ?
$this->faker->dateTimeBetween('-1 year', '+1 year') : null,
'sent_accounting_date' =>
$this->signedDateBoolean && $this->sentClientDateBoolean && $this->sentAccountingDateBoolean ?
$this->faker->dateTimeBetween('-1 year', '+1 year') : null,
'status' => $this->signedDateBoolean && $this->sentClientDateBoolean && $this->sentAccountingDateBoolean ?
InvoiceStatusEnum::archived : InvoiceStatusEnum::opened,
];
}
}