akaunting/app/Utilities/helpers.php

236 lines
4.7 KiB
PHP
Raw Normal View History

2019-11-16 10:21:14 +03:00
<?php
2021-04-16 00:59:43 +03:00
use App\Models\Common\Company;
2019-11-16 10:21:14 +03:00
use App\Traits\DateTime;
2021-09-07 10:33:34 +03:00
use App\Traits\Sources;
2020-03-25 20:21:42 +03:00
use App\Utilities\Date;
2019-12-31 02:20:10 +03:00
use App\Utilities\Widgets;
2019-11-16 10:21:14 +03:00
2022-06-13 10:01:46 +03:00
if (! function_exists('user')) {
2019-11-16 10:21:14 +03:00
/**
* Get the authenticated user.
*
* @return \App\Models\Auth\User
*/
function user()
{
2022-06-01 10:15:55 +03:00
return auth()->user();
2019-11-16 10:21:14 +03:00
}
}
2022-06-13 10:01:46 +03:00
if (! function_exists('user_id')) {
2021-06-17 10:59:07 +03:00
/**
* Get id of current user.
*/
2022-07-14 18:42:06 +03:00
function user_id(): int|null
2021-06-17 10:59:07 +03:00
{
2022-06-01 10:15:55 +03:00
return user()?->id;
2021-06-17 10:59:07 +03:00
}
}
2022-06-13 10:01:46 +03:00
if (! function_exists('company_date_format')) {
/**
2021-09-07 10:33:34 +03:00
* Get the date format of company.
*/
2022-07-14 18:42:06 +03:00
function company_date_format(): string
{
$date_time = new class() {
use DateTime;
};
return $date_time->getCompanyDateFormat();
}
}
2022-06-13 10:01:46 +03:00
if (! function_exists('company_date')) {
2019-11-16 10:21:14 +03:00
/**
* Format the given date based on company settings.
*/
2022-07-14 18:42:06 +03:00
function company_date($date): string
2019-11-16 10:21:14 +03:00
{
2021-02-23 22:11:35 +03:00
return Date::parse($date)->format(company_date_format());
2019-11-16 10:21:14 +03:00
}
}
2019-12-31 02:20:10 +03:00
2022-06-13 10:01:46 +03:00
if (! function_exists('show_widget')) {
2019-12-31 02:20:10 +03:00
/**
2020-01-04 11:49:59 +03:00
* Show a widget.
2019-12-31 02:20:10 +03:00
*
* @return string
*/
2020-01-04 11:49:59 +03:00
function show_widget()
2019-12-31 02:20:10 +03:00
{
2020-01-04 11:49:59 +03:00
$arguments = func_get_args();
$model = array_shift($arguments);
return Widgets::show($model, ...$arguments);
2019-12-31 02:20:10 +03:00
}
}
2022-06-13 10:01:46 +03:00
if (! function_exists('company')) {
2021-04-16 00:59:43 +03:00
/**
* Get current/any company model.
*/
2022-07-14 18:42:06 +03:00
function company(int|null $id = null): Company|null
2021-04-16 00:59:43 +03:00
{
$company = null;
if (is_null($id)) {
$company = Company::getCurrent();
}
if (is_numeric($id)) {
$company = Company::find($id);
}
return $company;
}
}
2022-06-13 10:01:46 +03:00
if (! function_exists('company_id')) {
2021-04-16 00:59:43 +03:00
/**
* Get id of current company.
*/
2022-07-14 18:42:06 +03:00
function company_id(): int|null
2021-04-16 00:59:43 +03:00
{
2022-06-01 10:15:55 +03:00
return company()?->id;
2021-04-16 00:59:43 +03:00
}
}
2022-06-13 10:01:46 +03:00
if (! function_exists('should_queue')) {
2021-04-16 00:59:43 +03:00
/**
* Check if queue is enabled.
*/
2022-07-14 18:42:06 +03:00
function should_queue(): bool
{
2021-04-16 00:59:43 +03:00
return config('queue.default') != 'sync';
}
}
2022-06-13 10:01:46 +03:00
if (! function_exists('source_name')) {
2021-09-07 10:33:34 +03:00
/**
* Get the current source.
*/
2022-07-14 18:42:06 +03:00
function source_name(string|null $alias = null): string
2021-09-07 10:33:34 +03:00
{
$tmp = new class() {
use Sources;
};
2021-09-10 00:31:39 +03:00
return $tmp->getSourceName(null, $alias);
2021-09-07 10:33:34 +03:00
}
}
2022-06-13 10:01:46 +03:00
if (! function_exists('cache_prefix')) {
/**
* Cache system added company_id prefix.
*/
2022-07-14 18:42:06 +03:00
function cache_prefix(): string
2021-09-07 10:33:34 +03:00
{
2021-04-16 00:59:43 +03:00
return company_id() . '_';
}
}
2022-06-13 10:01:46 +03:00
if (! function_exists('array_values_recursive')) {
2022-06-01 10:15:55 +03:00
/**
* Get array values recursively.
*/
function array_values_recursive(array $array): array
{
$flat = [];
foreach($array as $value) {
if (is_array($value)) {
$flat = array_merge($flat, array_values_recursive($value));
} else {
$flat[] = $value;
}
}
return $flat;
}
}
2022-06-13 10:01:46 +03:00
if (! function_exists('running_in_queue')) {
/**
* Detect if application is running in queue.
*/
2022-07-14 18:42:06 +03:00
function running_in_queue(): bool
{
return defined('APP_RUNNING_IN_QUEUE') ?? false;
}
}
2022-06-01 10:15:55 +03:00
2022-06-13 10:01:46 +03:00
if (! function_exists('simple_icons')) {
2022-06-01 10:15:55 +03:00
/**
* Get the simple icon content
*/
function simple_icons(string $name): string
{
$path = base_path('vendor/simple-icons/simple-icons/icons/' . $name . '.svg');
return file_get_contents($path);
}
}
2022-06-13 10:01:46 +03:00
if (! function_exists('default_currency')) {
2022-06-01 10:15:55 +03:00
/**
* Get the default currency code
*/
function default_currency(): string
{
return setting('default.currency');
}
}
2022-06-13 10:01:46 +03:00
if (! function_exists('env_is_production')) {
/**
* Determine if the application is in the production environment
*/
function env_is_production(): bool
{
return config('app.env') === 'production';
}
}
if (! function_exists('env_is_development')) {
/**
* Determine if the application is in the development environment
*/
function env_is_development(): bool
{
return config('app.env') === 'development';
}
}
if (! function_exists('env_is_build')) {
/**
* Determine if the application is in the build environment
*/
function env_is_build(): bool
{
return config('app.env') === 'build';
}
}
if (! function_exists('env_is_local')) {
/**
* Determine if the application is in the local environment
*/
function env_is_local(): bool
{
return config('app.env') === 'local';
}
}
if (! function_exists('env_is_testing')) {
/**
* Determine if the application is in the testing environment
*/
function env_is_testing(): bool
{
return config('app.env') === 'testing';
}
}