ldc-supportweb/database/factories/InvoiceFactory.php

142 lines
4.3 KiB
PHP
Raw Permalink Normal View History

2022-08-30 13:21:07 +00:00
<?php
namespace Database\Factories;
2023-08-14 17:48:26 +00:00
use App\Enums\Invoices\TaskTypeEnum;
use App\Enums\Invoices\InvoiceStatusEnum;
2022-08-30 13:21:07 +00:00
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;
2023-08-14 17:48:26 +00:00
/**
* 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;
2022-08-30 13:21:07 +00:00
/**
* Define the model's default state.
*
2023-08-14 17:48:26 +00:00
* @return void
2022-08-30 13:21:07 +00:00
*/
2023-08-14 17:48:26 +00:00
private function customData(): void
2022-08-30 13:21:07 +00:00
{
\mt_srand();
2023-08-14 17:48:26 +00:00
$lines = \mt_rand(2, 9);
2022-08-30 13:21:07 +00:00
for ($i = 0; $i < $lines; $i++) {
$days = 0.125 * $this->faker->numberBetween(1, 4);
$type = \collect(TaskTypeEnum::cases())->random();
2022-08-30 13:21:07 +00:00
$amount = 0;
switch ($type) {
case TaskTypeEnum::web:
2022-08-30 13:21:07 +00:00
$amount = $days * $type->price;
break;
case TaskTypeEnum::seo:
2022-08-30 13:21:07 +00:00
$amount = $days * $type->price;
break;
case TaskTypeEnum::package:
2022-08-30 13:21:07 +00:00
$amount = $days * (\optional(Package::query()->inRandomOrder()->first())->amount ??
$this->faker->randomFloat(null, 150, 650));
break;
case TaskTypeEnum::package:
2022-08-30 13:21:07 +00:00
$amount = $days * $this->faker->randomFloat(null, 150, 650);
break;
}
2023-08-14 17:48:26 +00:00
$this->totalDays += $days;
$this->total += $amount;
/** @var \App\Enums\Invoices\TaskTypeEnum */
$this->tasks[] = \array_merge(Invoice::getTaskStructure(), [
2022-08-30 13:21:07 +00:00
'desc' => $this->faker->words(9, true),
'days' => $days,
2023-08-14 17:48:26 +00:00
'type' => \collect(TaskTypeEnum::cases())->random()->value,
2022-08-30 13:21:07 +00:00
'amount' => $amount,
'package' => 1
]);
2023-08-14 17:48:26 +00:00
/** Dates */
$this->signedDateBoolean = $this->faker->boolean(25);
$this->sentClientDateBoolean = $this->faker->boolean(25);
$this->sentAccountingDateBoolean = $this->faker->boolean(25);
2022-08-30 13:21:07 +00:00
} //end for
2023-08-14 17:48:26 +00:00
}
2022-08-30 13:21:07 +00:00
2023-08-14 17:48:26 +00:00
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
$this->customData();
2022-08-30 13:21:07 +00:00
return [
'firstname' => $this->faker->firstName,
'lastname' => $this->faker->lastName,
'object' => $this->faker->words(3, true),
// * constructed object
2023-08-14 17:48:26 +00:00
'tasks' => $this->tasks,
2022-08-30 13:21:07 +00:00
// * constructed object result
2023-08-14 17:48:26 +00:00
'total' => $this->total,
2022-08-30 13:21:07 +00:00
// * constructed object result
2023-08-14 17:48:26 +00:00
'total_days' => $this->totalDays,
2022-08-30 13:21:07 +00:00
'comment' => $this->faker->text(\rand(20, 40)),
'show_days' => $this->faker->boolean(75),
2023-08-14 17:48:26 +00:00
'signed_date' =>
$this->signedDateBoolean ?
2022-08-30 13:21:07 +00:00
$this->faker->dateTimeBetween('-1 year', '+1 year') : null,
2023-08-14 17:48:26 +00:00
'sent_client_date' =>
$this->signedDateBoolean && $this->sentClientDateBoolean ?
2022-08-30 13:21:07 +00:00
$this->faker->dateTimeBetween('-1 year', '+1 year') : null,
2023-08-14 17:48:26 +00:00
'sent_accounting_date' =>
$this->signedDateBoolean && $this->sentClientDateBoolean && $this->sentAccountingDateBoolean ?
2022-08-30 13:21:07 +00:00
$this->faker->dateTimeBetween('-1 year', '+1 year') : null,
2023-08-14 17:48:26 +00:00
'status' => $this->signedDateBoolean && $this->sentClientDateBoolean && $this->sentAccountingDateBoolean ?
InvoiceStatusEnum::archived : InvoiceStatusEnum::opened,
2022-08-30 13:21:07 +00:00
];
}
}