Files
investbrain/database/factories/TransactionFactory.php
T

136 lines
3.5 KiB
PHP
Raw Normal View History

2024-09-06 19:39:04 -05:00
<?php
2025-01-28 17:33:54 -06:00
declare(strict_types=1);
2024-09-06 19:39:04 -05:00
namespace Database\Factories;
use App\Models\Portfolio;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Transaction>
*/
class TransactionFactory extends Factory
{
2025-01-28 17:14:49 -06:00
protected static ?string $transaction_type;
2024-09-06 19:39:04 -05:00
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
2024-09-06 21:59:58 -05:00
$transaction_type = $this->faker->randomElement(['BUY', 'SELL']);
2024-09-06 19:39:04 -05:00
return [
2024-09-06 21:59:58 -05:00
'symbol' => $this->faker->randomElement(['AAPL', 'GOOG', 'AMZN']),
'transaction_type' => $transaction_type,
2025-01-28 17:14:49 -06:00
'portfolio_id' => Portfolio::factory()->create()->id,
2024-09-06 19:39:04 -05:00
'date' => $this->faker->date('Y-m-d'),
2024-09-06 21:59:58 -05:00
'quantity' => 1,
2025-01-28 17:14:49 -06:00
'cost_basis' => $transaction_type == 'BUY'
2024-09-06 21:59:58 -05:00
? $this->faker->randomFloat(2, 10, 500)
2025-01-28 17:14:49 -06:00
: null,
'sale_price' => $transaction_type == 'SELL'
2024-09-06 19:39:04 -05:00
? $this->faker->randomFloat(2, 10, 500)
2025-01-28 17:14:49 -06:00
: null,
2024-09-06 19:39:04 -05:00
];
}
2024-09-09 19:39:38 -05:00
public function yearsAgo(): static
{
return $this->state(fn (array $attributes) => [
2025-04-09 19:25:15 -05:00
'date' => now()->subYears($this->faker->numberBetween(3, 5))->toDateString(),
2024-09-11 22:00:37 -05:00
]);
}
2024-09-15 11:28:13 -05:00
public function lastYear(): static
{
return $this->state(fn (array $attributes) => [
2025-04-09 19:25:15 -05:00
'date' => now()->subYear()->toDateString(),
2024-09-15 11:28:13 -05:00
]);
}
2024-09-11 22:00:37 -05:00
public function lastMonth(): static
{
return $this->state(fn (array $attributes) => [
2025-04-09 19:25:15 -05:00
'date' => now()->subMonth()->toDateString(),
2024-09-09 19:39:38 -05:00
]);
}
2025-07-21 20:28:57 -05:00
public function today(): static
{
return $this->state(fn (array $attributes) => [
'date' => now()->toDateString(),
]);
}
2024-09-15 11:28:13 -05:00
public function recent(): static
{
return $this->state(fn (array $attributes) => [
2025-04-09 19:25:15 -05:00
'date' => now()->subDays($this->faker->numberBetween(3, 14))->toDateString(),
]);
}
public function date($date): static
{
return $this->state(fn (array $attributes) => [
'date' => $date,
2024-09-15 11:28:13 -05:00
]);
}
2024-09-06 19:39:04 -05:00
public function symbol($symbol): static
{
return $this->state(fn (array $attributes) => [
'symbol' => $symbol,
]);
}
2024-09-06 21:59:58 -05:00
public function portfolio($portfolio_id): static
2024-09-06 19:39:04 -05:00
{
return $this->state(fn (array $attributes) => [
'portfolio_id' => $portfolio_id,
]);
}
2025-04-09 19:25:15 -05:00
public function currency($currency): static
{
return $this->state(fn (array $attributes) => [
'currency' => $currency,
]);
}
public function costBasis($cost_basis): static
{
return $this->state(fn (array $attributes) => [
'cost_basis' => $cost_basis,
]);
}
public function salePrice($sale_price): static
{
return $this->state(fn (array $attributes) => [
'sale_price' => $sale_price,
]);
}
2024-09-06 19:39:04 -05:00
public function buy(): static
{
return $this->state(fn (array $attributes) => [
'transaction_type' => 'BUY',
2024-09-06 21:59:58 -05:00
'cost_basis' => $this->faker->randomFloat(2, 10, 500),
2025-01-28 17:14:49 -06:00
'sale_price' => null,
2024-09-06 19:39:04 -05:00
]);
}
public function sell(): static
{
return $this->state(fn (array $attributes) => [
'transaction_type' => 'SELL',
2024-09-06 21:59:58 -05:00
'sale_price' => $this->faker->randomFloat(2, 10, 500),
'cost_basis' => null,
2024-09-06 19:39:04 -05:00
]);
}
}