laravel 8
This commit is contained in:
@ -1,43 +1,74 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Auth\User;
|
||||
use App\Models\Banking\Transaction;
|
||||
use Faker\Generator as Faker;
|
||||
namespace Database\Factories;
|
||||
|
||||
$user = User::first();
|
||||
$company = $user->companies()->first();
|
||||
use App\Abstracts\Factory;
|
||||
use App\Models\Banking\Transaction as Model;
|
||||
use App\Traits\Transactions;
|
||||
|
||||
$factory->define(Transaction::class, function (Faker $faker) use ($company) {
|
||||
setting()->setExtraColumns(['company_id' => $company->id]);
|
||||
class Transaction extends Factory
|
||||
{
|
||||
use Transactions;
|
||||
|
||||
$types = ['income', 'expense'];
|
||||
$type = $faker->randomElement($types);
|
||||
/**
|
||||
* The name of the factory's corresponding model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $model = Model::class;
|
||||
|
||||
return [
|
||||
'company_id' => $company->id,
|
||||
'type' => $type,
|
||||
'account_id' => setting('default.account'),
|
||||
'paid_at' => $faker->dateTimeBetween(now()->startOfYear(), now()->endOfYear())->format('Y-m-d H:i:s'),
|
||||
'amount' => $faker->randomFloat(2, 1, 1000),
|
||||
'currency_code' => setting('default.currency'),
|
||||
'currency_rate' => '1.0',
|
||||
'description' => $faker->text(5),
|
||||
'category_id' => $company->categories()->type($type)->get()->random(1)->pluck('id')->first(),
|
||||
'reference' => $faker->text(5),
|
||||
'payment_method' => setting('default.payment_method'),
|
||||
];
|
||||
});
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function definition()
|
||||
{
|
||||
$types = array_merge($this->getIncomeTypes(), $this->getExpenseTypes());
|
||||
$type = $this->faker->randomElement($types);
|
||||
|
||||
$factory->state(Transaction::class, 'income', function (Faker $faker) use ($company) {
|
||||
return [
|
||||
'type' => 'income',
|
||||
'category_id' => $company->categories()->income()->get()->random(1)->pluck('id')->first(),
|
||||
];
|
||||
});
|
||||
return [
|
||||
'company_id' => $this->company->id,
|
||||
'type' => $type,
|
||||
'account_id' => setting('default.account'),
|
||||
'paid_at' => $this->faker->dateTimeBetween(now()->startOfYear(), now()->endOfYear())->format('Y-m-d H:i:s'),
|
||||
'amount' => $this->faker->randomFloat(2, 1, 1000),
|
||||
'currency_code' => setting('default.currency'),
|
||||
'currency_rate' => '1.0',
|
||||
'description' => $this->faker->text(5),
|
||||
'category_id' => $this->company->categories()->type($type)->get()->random(1)->pluck('id')->first(),
|
||||
'reference' => $this->faker->text(5),
|
||||
'payment_method' => setting('default.payment_method'),
|
||||
];
|
||||
}
|
||||
|
||||
$factory->state(Transaction::class, 'expense', function (Faker $faker) use ($company) {
|
||||
return [
|
||||
'type' => 'expense',
|
||||
'category_id' => $company->categories()->expense()->get()->random(1)->pluck('id')->first(),
|
||||
];
|
||||
});
|
||||
/**
|
||||
* Indicate that the model type is income.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Factories\Factory
|
||||
*/
|
||||
public function income()
|
||||
{
|
||||
return $this->state(function (array $attributes) {
|
||||
return [
|
||||
'type' => 'income',
|
||||
'category_id' => $this->company->categories()->income()->get()->random(1)->pluck('id')->first(),
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the model type is expense.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Factories\Factory
|
||||
*/
|
||||
public function expense()
|
||||
{
|
||||
return $this->state(function (array $attributes) {
|
||||
return [
|
||||
'type' => 'expense',
|
||||
'category_id' => $this->company->categories()->expense()->get()->random(1)->pluck('id')->first(),
|
||||
];
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user