Akaunting/app/Utilities/Overrider.php

141 lines
5.1 KiB
PHP
Raw Permalink Normal View History

2018-02-20 15:24:17 +00:00
<?php
namespace App\Utilities;
use Akaunting\Money\Money;
2026-03-09 19:43:05 +00:00
use App\Models\Setting\Category;
2018-02-20 15:24:17 +00:00
use App\Models\Setting\Currency;
2026-03-09 19:43:05 +00:00
use Illuminate\Support\Str;
2018-02-20 15:24:17 +00:00
class Overrider
{
public static $company_id;
public static function load($type)
{
// Overrides apply per company
2021-04-15 21:59:43 +00:00
$company_id = company_id();
2018-02-20 15:24:17 +00:00
if (empty($company_id)) {
return;
}
static::$company_id = $company_id;
$method = 'load' . ucfirst($type);
static::$method();
}
protected static function loadSettings()
{
// Timezone
2021-10-05 15:14:22 +00:00
$timezone = setting('localisation.timezone');
if (empty($timezone)) {
2021-10-05 15:46:46 +00:00
$timezone = config('setting.fallback.localisation.timezone');
2021-10-05 15:14:22 +00:00
}
config(['app.timezone' => $timezone]);
2019-01-14 13:36:28 +00:00
date_default_timezone_set(config('app.timezone'));
2018-02-20 15:24:17 +00:00
// Email
2019-11-16 07:21:14 +00:00
$email_protocol = setting('email.protocol', 'mail');
2020-03-04 08:09:28 +00:00
config(['mail.default' => $email_protocol]);
2019-11-16 07:21:14 +00:00
config(['mail.from.name' => setting('company.name')]);
config(['mail.from.address' => setting('company.email')]);
2018-02-20 15:24:17 +00:00
if ($email_protocol == 'sendmail') {
2020-03-16 09:09:21 +00:00
config(['mail.mailers.sendmail.path' => setting('email.sendmail_path')]);
2018-02-20 15:24:17 +00:00
} elseif ($email_protocol == 'smtp') {
2020-03-16 09:09:21 +00:00
config(['mail.mailers.smtp.host' => setting('email.smtp_host')]);
config(['mail.mailers.smtp.port' => setting('email.smtp_port')]);
config(['mail.mailers.smtp.username' => setting('email.smtp_username')]);
config(['mail.mailers.smtp.password' => setting('email.smtp_password')]);
config(['mail.mailers.smtp.encryption' => setting('email.smtp_encryption')]);
2018-02-20 15:24:17 +00:00
}
// Locale
2022-06-01 07:15:55 +00:00
if (! session('locale')) {
$locale = user()->locale ?? setting('default.locale');
app()->setLocale($locale);
2018-02-20 15:24:17 +00:00
}
2019-03-09 16:31:03 +00:00
// Set locale for Money package
2026-03-09 19:43:05 +00:00
Money::setLocale(app()->getLocale());
// Money
config(['money.defaults.currency' => setting('default.currency')]);
2021-05-17 07:57:13 +00:00
// Set app url dynamically if empty
2022-06-01 07:15:55 +00:00
if (! config('app.url')) {
2021-05-17 07:57:13 +00:00
config(['app.url' => url('/')]);
}
2018-02-20 15:24:17 +00:00
}
2026-03-09 19:43:05 +00:00
protected static function loadCategoryTypes()
{
$category = new Category;
$income_types = $category->getIncomeCategoryTypes('string');
$expense_types = $category->getExpenseCategoryTypes('string');
$item_types = $category->getItemCategoryTypes('string');
$other_types = $category->getOtherCategoryTypes('string');
$search_string = config('search-string');
foreach ($search_string as $model => &$model_config) {
$route = $model_config['columns']['category_id']['route'] ?? null;
// Only update category_id routes that point to categories.index
if (!is_array($route) || ($route[0] ?? '') !== 'categories.index' || !isset($route[1])) {
continue;
}
// Longest match first (income,expense must come before income)
$replacements = [
'type:' . Category::INCOME_TYPE . ',' . Category::EXPENSE_TYPE => 'type:' . $income_types . ',' . $expense_types,
'type:' . Category::INCOME_TYPE => 'type:' . $income_types,
'type:' . Category::EXPENSE_TYPE => 'type:' . $expense_types,
'type:' . Category::ITEM_TYPE => 'type:' . $item_types,
'type:' . Category::OTHER_TYPE => 'type:' . $other_types,
];
foreach ($replacements as $search => $replace) {
if (Str::contains($route[1], $search)) {
$model_config['columns']['category_id']['route'][1] = Str::replace($search, $replace, $route[1]);
break;
}
}
}
config(['search-string' => $search_string]);
}
2018-02-20 15:24:17 +00:00
protected static function loadCurrencies()
{
$currencies = Currency::all();
foreach ($currencies as $currency) {
2025-08-01 08:44:54 +00:00
// If currency is not set in config, add it
if (! config("money.currencies.{$currency->code}")) {
config(['money.currencies.' . $currency->code => [
'code' => $currency->code,
'subunit' => 100,
]]);
}
2023-07-10 09:53:43 +00:00
config(['money.currencies.' . $currency->code . '.name' => $currency->name]);
config(['money.currencies.' . $currency->code . '.rate' => $currency->rate]);
config(['money.currencies.' . $currency->code . '.precision' => $currency->precision]);
config(['money.currencies.' . $currency->code . '.symbol' => $currency->symbol]);
config(['money.currencies.' . $currency->code . '.symbol_first' => $currency->symbol_first]);
config(['money.currencies.' . $currency->code . '.decimal_mark' => $currency->decimal_mark]);
config(['money.currencies.' . $currency->code . '.thousands_separator' => $currency->thousands_separator]);
2018-02-20 15:24:17 +00:00
}
// Set currencies with new settings
2023-07-10 09:53:43 +00:00
\Akaunting\Money\Currency::setCurrencies(config('money.currencies'));
2018-02-20 15:24:17 +00:00
}
2019-11-16 07:21:14 +00:00
}