akaunting/database/factories/Transaction.php

148 lines
4.5 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;
2023-05-17 10:16:49 +03:00
use App\Interfaces\Utility\TransactionNumber;
2020-10-14 17:07:59 +03:00
use App\Models\Banking\Transaction as Model;
2023-03-22 01:33:33 +03:00
use App\Models\Common\Contact;
2020-10-14 17:07:59 +03:00
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,
2023-05-17 10:16:49 +03:00
'number' => $this->getNumber($this->type),
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' => default_currency(),
2021-02-07 01:12:57 +03:00
'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()
{
2023-03-22 01:33:33 +03:00
return $this->state(function (array $attributes): array {
$contacts = Contact::customer()->enabled()->get();
if ($contacts->count()) {
$contact = $contacts->random(1)->first();
} else {
$contact = Contact::factory()->customer()->enabled()->create();
}
return [
'type' => 'income',
2023-05-17 10:16:49 +03:00
'number' => $this->getNumber('income', '', $contact),
2023-03-22 01:33:33 +03:00
'contact_id' => $contact->id,
'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()
{
2023-03-22 01:33:33 +03:00
return $this->state(function (array $attributes): array {
$contacts = Contact::vendor()->enabled()->get();
if ($contacts->count()) {
$contact = $contacts->random(1)->first();
} else {
$contact = Contact::factory()->vendor()->enabled()->create();
}
return [
'type' => 'expense',
2023-05-17 10:16:49 +03:00
'number' => $this->getNumber('expense', '', $contact),
2023-03-22 01:33:33 +03:00
'contact_id' => $contact->id,
'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()
{
2023-05-17 10:16:49 +03:00
$type = $this->getRawAttribute('type') . '-recurring';
2022-06-01 10:15:55 +03:00
return $this->state([
2023-05-17 10:16:49 +03:00
'type' => $type,
'number' => $this->getNumber($type, '-recurring'),
2023-03-14 11:38:57 +03:00
'recurring_started_at' => Date::now()->format('Y-m-d H:i:s'),
2022-06-01 10:15:55 +03:00
'recurring_frequency' => 'daily',
'recurring_custom_frequency' => 'daily',
'recurring_interval' => '1',
'recurring_limit' => 'date',
2023-03-13 23:55:24 +03:00
'recurring_limit_date' => Date::now()->addDay(7)->format('Y-m-d H:i:s'),
2022-06-01 10:15:55 +03:00
'disabled_transaction_paid' => "Auto-generated",
'disabled_transaction_number' => "Auto-generated",
'real_type' => $this->getRawAttribute('type'),
]);
}
2023-01-11 11:55:00 +03:00
/**
* Get transaction number
*
*/
2023-05-17 10:16:49 +03:00
public function getNumber($type, $suffix = '', $contact = null)
2023-01-11 11:55:00 +03:00
{
2023-05-17 10:16:49 +03:00
$utility = app(TransactionNumber::class);
$number = $utility->getNextNumber($type, $suffix, $contact);
2023-03-22 01:33:33 +03:00
2023-05-17 10:16:49 +03:00
$utility->increaseNextNumber($type, $suffix ,$contact);
2023-01-11 11:55:00 +03:00
return $number;
}
2020-10-14 17:07:59 +03:00
}