Files
InvoiceShelf/app/Support/helpers.php
Darko Gjorgjijoski 6d1816bd1b refactor: reorganize app/Services and app/Support by domain
The app/Services/ directory had grown into 22 flat files at the root plus 7 uneven subdirectories — finding anything required scrolling through an alphabetical mix of small CRUD services, infrastructure drivers, and install-time utilities. This commit groups services by domain, folds Backup into a new Storage namespace, and moves framework-infrastructure and install-time helpers out of Services and into Support where they belong.

New Services layout: Documents/ (Invoice, Estimate, RecurringInvoice, Payment, Expense, Transaction, DocumentItem, SerialNumber, Currency — matches the 'Documents' navigation group); Company/ (Company, Member, Invitation); Mail/ (MailConfiguration, CompanyMailConfig); Storage/ (FileDisk, plus Backup folded in). ExchangeRateProviderService moves next to its drivers in ExchangeRate/; FontService moves into Pdf/ where it belongs. CustomerService, ItemService, CustomFieldService stay at the Services root as standalone single-file domains.

Moves to Support/: Hashids/ (library wrapper — not business logic); Setup/ (one-shot install-time utilities — stateless helpers); Pdf/ (ImageUtils, PdfTemplateUtils, plus the existing PdfHtmlSanitizer consolidated into the same subdir). These are all framework infrastructure and stateless utilities — the 'service' label never really fit them.

Namespace declarations in 29 moved files updated to match new paths. 62 consumer files (controllers, other services, tests, database factories, seeders, routes, bootstrap/providers.php) have their use statements rewritten via a literal-string replacement script — no regex meant no risk of half-matching. Three Documents services needed an explicit 'use App\Services\Mail\CompanyMailConfigService' added because the same-namespace short reference they relied on no longer resolves after the split.

Verified: composer dump-autoload, 350 tests pass (850 assertions), vendor/bin/pint clean, npm run build succeeds.
2026-04-11 10:00:00 +02:00

197 lines
4.1 KiB
PHP

<?php
use App\Models\CompanySetting;
use App\Models\Currency;
use App\Models\CustomField;
use App\Models\Setting;
use App\Support\Setup\InstallUtils;
use Illuminate\Support\Str;
/**
* Get company setting
*
* @return string
*/
function get_company_setting($key, $company_id)
{
if (! InstallUtils::isDbCreated()) {
return null;
}
return CompanySetting::getSetting($key, $company_id);
}
/**
* Get app setting
*
* @param $company_id
* @return string
*/
function get_app_setting($key)
{
if (! InstallUtils::isDbCreated()) {
return null;
}
return Setting::getSetting($key);
}
/**
* Get page title
*
* @return string
*/
function get_page_title($company_id)
{
if (! InstallUtils::isDbCreated()) {
return null;
}
$routeName = Route::currentRouteName();
$defaultPageTitle = 'InvoiceShelf - Self Hosted Invoicing Platform';
if ($routeName === 'customer.dashboard') {
$pageTitle = CompanySetting::getSetting('customer_portal_page_title', $company_id);
return $pageTitle ? $pageTitle : $defaultPageTitle;
}
$pageTitle = Setting::getSetting('admin_page_title');
return $pageTitle ? $pageTitle : $defaultPageTitle;
}
/**
* Set Active Path
*
* @param string $active
* @return string
*/
function set_active($path, $active = 'active')
{
return call_user_func_array('Request::is', (array) $path) ? $active : '';
}
/**
* @return mixed
*/
function is_url($path)
{
return call_user_func_array('Request::is', (array) $path);
}
/**
* @return string
*/
function getCustomFieldValueKey(string $type)
{
switch ($type) {
case 'Input':
return 'string_answer';
case 'TextArea':
return 'string_answer';
case 'Phone':
return 'number_answer';
case 'Url':
return 'string_answer';
case 'Number':
return 'number_answer';
case 'Dropdown':
return 'string_answer';
case 'Switch':
return 'boolean_answer';
case 'Date':
return 'date_answer';
case 'Time':
return 'time_answer';
case 'DateTime':
return 'date_time_answer';
default:
return 'string_answer';
}
}
/**
* @return formated_money
*/
function format_money_pdf($money, $currency = null)
{
$money = $money / 100;
if (! $currency) {
$currency = Currency::findOrFail(CompanySetting::getSetting('currency', 1));
}
$format_money = number_format(
$money,
$currency->precision,
$currency->decimal_separator,
$currency->thousand_separator
);
$currency_with_symbol = '';
if ($currency->swap_currency_symbol) {
$currency_with_symbol = $format_money.'<span style="font-family: DejaVu Sans;">'.$currency->symbol.'</span>';
} else {
$currency_with_symbol = '<span style="font-family: DejaVu Sans;">'.$currency->symbol.'</span>'.$format_money;
}
return $currency_with_symbol;
}
/**
* @param $string
* @return string
*/
function clean_slug($model, $title, $id = 0)
{
// Normalize the title
$slug = Str::upper('CUSTOM_'.$model.'_'.Str::slug($title, '_'));
// Get any that could possibly be related.
// This cuts the queries down by doing it once.
$allSlugs = getRelatedSlugs($model, $slug, $id);
// If we haven't used it before then we are all good.
if (! $allSlugs->contains('slug', $slug)) {
return $slug;
}
// Just append numbers like a savage until we find not used.
for ($i = 1; $i <= 10; $i++) {
$newSlug = $slug.'_'.$i;
if (! $allSlugs->contains('slug', $newSlug)) {
return $newSlug;
}
}
throw new Exception('Can not create a unique slug');
}
function getRelatedSlugs($type, $slug, $id = 0)
{
return CustomField::select('slug')->where('slug', 'like', $slug.'%')
->where('model_type', $type)
->where('id', '<>', $id)
->get();
}
function respondJson($error, $message)
{
return response()->json([
'error' => $error,
'message' => $message,
], 422);
}