Files
investbrain/database/factories/TransactionFactory.php
T

71 lines
1.9 KiB
PHP
Raw Normal View History

2024-09-06 19:39:04 -05:00
<?php
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
{
protected static ?string $transaction_type;
/**
* 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,
'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,
'cost_basis' => $transaction_type == 'BUY'
? $this->faker->randomFloat(2, 10, 500)
: null,
'sale_price' => $transaction_type == 'SELL'
2024-09-06 19:39:04 -05:00
? $this->faker->randomFloat(2, 10, 500)
: null,
];
}
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,
]);
}
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),
'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
]);
}
}