akaunting/app/Traits/Categories.php

66 lines
1.6 KiB
PHP
Raw Permalink Normal View History

2021-02-18 11:54:01 +03:00
<?php
namespace App\Traits;
2022-06-01 10:15:55 +03:00
use App\Models\Setting\Category;
use Illuminate\Support\Facades\Cache;
2021-02-18 11:54:01 +03:00
use Illuminate\Support\Str;
trait Categories
{
2023-03-03 10:11:26 +03:00
public function getCategoryTypes(bool $translate = true): array
2021-02-18 11:54:01 +03:00
{
$types = [];
$configs = config('type.category');
foreach ($configs as $type => $attr) {
$plural_type = Str::plural($type);
$name = $attr['translation']['prefix'] . '.' . $plural_type;
if (!empty($attr['alias'])) {
$name = $attr['alias'] . '::' . $name;
}
2023-03-03 10:11:26 +03:00
$types[$type] = $translate ? trans_choice($name, 1) : $name;
2021-02-18 11:54:01 +03:00
}
return $types;
}
2022-06-01 10:15:55 +03:00
public function getCategoryWithoutChildren(int $id): mixed
2022-06-01 10:15:55 +03:00
{
return Category::getWithoutChildren()->find($id);
}
public function getTransferCategoryId(): mixed
{
// 1 hour set cache for same query
return Cache::remember('transferCategoryId', 60, function () {
return Category::other()->pluck('id')->first();
});
}
public function isTransferCategory(): bool
{
$id = $this->id ?? $this->category->id ?? $this->model->id ?? 0;
return $id == $this->getTransferCategoryId();
2022-06-01 10:15:55 +03:00
}
2022-10-10 17:46:50 +03:00
public function getChildrenCategoryIds($category)
{
$ids = [];
foreach ($category->sub_categories as $sub_category) {
$ids[] = $sub_category->id;
if ($sub_category->sub_categories) {
$ids = array_merge($ids, $this->getChildrenCategoryIds($sub_category));
}
}
return $ids;
}
2021-02-18 11:54:01 +03:00
}