akaunting/database/factories/Transaction.php

105 lines
3.2 KiB
PHP
Raw Normal View History

2019-11-18 10:54:26 +03:00
<?php
2020-10-14 17:07:59 +03:00
namespace Database\Factories;
use App\Abstracts\Factory;
use App\Models\Banking\Transaction as Model;
use App\Traits\Transactions;
2022-06-01 10:15:55 +03:00
use App\Utilities\Date;
2020-10-14 17:07:59 +03:00
class Transaction extends Factory
{
2021-02-07 01:12:57 +03:00
use Transactions;
2020-10-14 17:07:59 +03:00
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Model::class;
2022-06-01 10:15:55 +03:00
/**
* The type of the model.
*
* @var string
*/
protected $type = 'income';
2020-10-14 17:07:59 +03:00
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
$types = array_merge($this->getIncomeTypes(), $this->getExpenseTypes());
2022-06-01 10:15:55 +03:00
$this->type = $this->faker->randomElement($types);
2020-10-14 17:07:59 +03:00
2022-06-01 10:15:55 +03:00
$category_type = in_array($this->type, $this->getIncomeTypes()) ? 'income' : 'expense';
2021-02-07 01:09:31 +03:00
2021-02-07 01:12:57 +03:00
return [
'company_id' => $this->company->id,
2022-06-01 10:15:55 +03:00
'type' => $this->type,
'number' => $this->getNextTransactionNumber(),
2021-02-07 01:12:57 +03:00
'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()->$category_type()->get()->random(1)->pluck('id')->first(),
'reference' => $this->faker->text(5),
'payment_method' => setting('default.payment_method'),
2021-09-10 00:31:39 +03:00
'created_from' => 'core::factory',
2021-02-07 01:12:57 +03:00
];
2020-10-14 17:07:59 +03:00
}
/**
* Indicate that the model type is income.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
public function income()
{
2020-10-15 00:08:41 +03:00
return $this->state([
'type' => 'income',
'category_id' => $this->company->categories()->income()->get()->random(1)->pluck('id')->first(),
]);
2020-10-14 17:07:59 +03:00
}
/**
* Indicate that the model type is expense.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
public function expense()
{
2020-10-15 00:08:41 +03:00
return $this->state([
'type' => 'expense',
'category_id' => $this->company->categories()->expense()->get()->random(1)->pluck('id')->first(),
]);
2020-10-14 17:07:59 +03:00
}
2022-06-01 10:15:55 +03:00
/**
* Indicate that the model is recurring.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
public function recurring()
{
return $this->state([
'type' => $this->getRawAttribute('type') . '-recurring',
'number' => $this->getNextTransactionNumber('-recurring'),
'recurring_started_at' => $this->getRawAttribute('paid_at'),
'recurring_frequency' => 'daily',
'recurring_custom_frequency' => 'daily',
'recurring_interval' => '1',
'recurring_limit' => 'date',
'recurring_limit_date' => Date::now()->addDay(7)->format('Y-m-d'),
'disabled_transaction_paid' => "Auto-generated",
'disabled_transaction_number' => "Auto-generated",
'real_type' => $this->getRawAttribute('type'),
]);
}
2020-10-14 17:07:59 +03:00
}