1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Utils/Helpers.php

388 lines
14 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Utils;
use App\Models\Client;
2020-12-23 11:17:14 +01:00
use App\Utils\Traits\MakesDates;
2021-09-22 10:48:33 +02:00
use Carbon\Carbon;
use Illuminate\Support\Str;
2020-10-28 11:10:49 +01:00
use stdClass;
class Helpers
{
2020-12-23 11:17:14 +01:00
use MakesDates;
2020-10-01 13:41:23 +02:00
public static function sharedEmailVariables(?Client $client, array $settings = null): array
{
if (! $client) {
2020-11-25 15:19:52 +01:00
$elements['signature'] = '';
$elements['settings'] = new stdClass;
$elements['whitelabel'] = true;
2021-03-10 02:51:08 +01:00
$elements['company'] = '';
2020-11-25 15:19:52 +01:00
return $elements;
}
2020-10-01 13:41:23 +02:00
$_settings = is_null($settings) ? $client->getMergedSettings() : $settings;
$elements['signature'] = $_settings->email_signature;
$elements['settings'] = $_settings;
$elements['whitelabel'] = $client->company->account->isPaid() ? true : false;
2021-03-10 02:51:08 +01:00
$elements['company'] = $client->company;
return $elements;
}
2020-12-23 11:17:14 +01:00
/**
* A centralised method to format the custom fields content.
2020-12-25 12:29:42 +01:00
*
* @param mixed|null $custom_fields
2020-12-25 12:29:42 +01:00
* @param mixed $field
* @param mixed $value
* @param \App\Models\Client|null $client
*
* @return null|string
2020-12-23 11:17:14 +01:00
*/
2022-06-06 14:27:17 +02:00
public function formatCustomFieldValue($custom_fields, $field, $value, $entity = null): ?string
2020-12-23 11:17:14 +01:00
{
$custom_field = '';
2023-07-10 10:29:24 +02:00
$quote_or_credit_field = false;
2020-12-23 11:17:14 +01:00
2023-07-10 10:29:24 +02:00
if($custom_fields && stripos($field, 'quote') !== false && property_exists($custom_fields, $field)) {
$custom_field = $custom_fields->{$field};
$custom_field_parts = explode('|', $custom_field);
if (count($custom_field_parts) >= 2) {
$custom_field = $custom_field_parts[1];
}
$quote_or_credit_field = true;
}elseif($custom_fields && stripos($field, 'credit') !== false && property_exists($custom_fields, $field)) {
$custom_field = $custom_fields->{$field};
$custom_field_parts = explode('|', $custom_field);
if (count($custom_field_parts) >= 2) {
$custom_field = $custom_field_parts[1];
}
$quote_or_credit_field = true;
}elseif($custom_fields && stripos($field, 'credit') !== false) {
$field = str_replace("credit", "invoice", $field);
}elseif($custom_fields && stripos($field, 'quote') !== false) {
$field = str_replace("quote", "invoice", $field);
}
if (!$quote_or_credit_field && $custom_fields && property_exists($custom_fields, $field)) {
2020-12-23 11:17:14 +01:00
$custom_field = $custom_fields->{$field};
$custom_field_parts = explode('|', $custom_field);
if (count($custom_field_parts) >= 2) {
$custom_field = $custom_field_parts[1];
}
}
switch ($custom_field) {
case 'date':
2022-06-06 14:27:17 +02:00
return is_null($entity) ? $value : $this->translateDate($value, $entity->date_format(), $entity->locale());
2020-12-23 11:17:14 +01:00
break;
case 'switch':
return trim($value) == 'yes' ? ctrans('texts.yes') : ctrans('texts.no');
break;
default:
return is_null($value) ? '' : $this->processReservedKeywords($value, $entity);
2020-12-23 11:17:14 +01:00
break;
}
}
/**
* A centralised method to make custom field.
2020-12-25 12:29:42 +01:00
* @param mixed|null $custom_fields
* @param mixed $field
*
* @return string
*/
2021-02-20 11:51:33 +01:00
public function makeCustomField($custom_fields, $field): string
{
2023-07-10 10:29:24 +02:00
if ($custom_fields && property_exists($custom_fields, $field)) {
$custom_field = $custom_fields->{$field};
$custom_field_parts = explode('|', $custom_field);
return $custom_field_parts[0];
}
2023-07-10 10:36:42 +02:00
$field = str_replace(["quote","credit"], ["invoice", "invoice"], $field);
2023-07-10 10:29:24 +02:00
if ($custom_fields && property_exists($custom_fields, $field)) {
$custom_field = $custom_fields->{$field};
$custom_field_parts = explode('|', $custom_field);
return $custom_field_parts[0];
}
return '';
}
2021-09-22 10:48:33 +02:00
/**
* Process reserved keywords on PDF.
*
* @param string $value
2022-12-28 09:31:43 +01:00
* @param Client|Company|Vendor $entity
* @param null|Carbon $currentDateTime
* @return null|string
2021-09-22 10:48:33 +02:00
*/
public static function processReservedKeywords(?string $value, $entity, $currentDateTime = null): ?string
2021-09-22 10:48:33 +02:00
{
if (! $value) {
2021-09-22 10:48:33 +02:00
return '';
}
// 04-10-2022 Return Early if no reserved keywords are present, this is a very expensive process
$string_hit = false;
2023-02-16 02:36:09 +01:00
foreach ([':MONTH',':YEAR',':QUARTER',':WEEK'] as $string) {
if (stripos($value, $string) !== false) {
$string_hit = true;
}
}
2023-02-16 02:36:09 +01:00
if (!$string_hit) {
return $value;
2023-02-16 02:36:09 +01:00
}
2022-12-21 02:27:47 +01:00
// 04-10-2022 Return Early if no reserved keywords are present, this is a very expensive process
2022-06-06 05:28:10 +02:00
Carbon::setLocale($entity->locale());
2021-09-22 10:48:33 +02:00
if (!$currentDateTime) {
2022-12-21 02:27:47 +01:00
$currentDateTime = Carbon::now()->timezone($entity->timezone()->name);
}
2021-09-22 10:48:33 +02:00
$replacements = [
'literal' => [
':MONTH_BEFORE' => \sprintf(
'%s %s %s',
$currentDateTime->copy()->subMonth(1)->translatedFormat($entity->date_format()),
ctrans('texts.to'),
$currentDateTime->copy()->subDay(1)->translatedFormat($entity->date_format()),
),
':YEAR_BEFORE' => \sprintf(
'%s %s %s',
$currentDateTime->copy()->subYear(1)->translatedFormat($entity->date_format()),
ctrans('texts.to'),
$currentDateTime->copy()->subDay(1)->translatedFormat($entity->date_format()),
),
':MONTH_AFTER' => \sprintf(
'%s %s %s',
$currentDateTime->translatedFormat($entity->date_format()),
ctrans('texts.to'),
$currentDateTime->copy()->addMonth(1)->subDay(1)->translatedFormat($entity->date_format()),
),
':YEAR_AFTER' => \sprintf(
'%s %s %s',
$currentDateTime->translatedFormat($entity->date_format()),
ctrans('texts.to'),
$currentDateTime->copy()->addYear(1)->subDay(1)->translatedFormat($entity->date_format()),
),
':MONTHYEAR' => \sprintf(
'%s %s',
Carbon::createFromDate($currentDateTime->month)->translatedFormat('F'),
$currentDateTime->year,
),
':MONTH' => Carbon::createFromDate($currentDateTime->year, $currentDateTime->month)->translatedFormat('F'),
':YEAR' => $currentDateTime->year,
':QUARTER' => 'Q'.$currentDateTime->quarter,
2021-09-22 10:48:33 +02:00
':WEEK_BEFORE' => \sprintf(
'%s %s %s',
$currentDateTime->copy()->subDays(7)->translatedFormat($entity->date_format()),
2021-09-22 10:48:33 +02:00
ctrans('texts.to'),
$currentDateTime->copy()->subDays(1)->translatedFormat($entity->date_format())
2021-09-22 10:48:33 +02:00
),
':WEEK_AHEAD' => \sprintf(
'%s %s %s',
$currentDateTime->copy()->addDays(7)->translatedFormat($entity->date_format()),
2021-09-22 10:48:33 +02:00
ctrans('texts.to'),
$currentDateTime->copy()->addDays(13)->translatedFormat($entity->date_format())
2021-09-22 10:48:33 +02:00
),
':WEEK' => \sprintf(
'%s %s %s',
$currentDateTime->translatedFormat($entity->date_format()),
ctrans('texts.to'),
$currentDateTime->copy()->addDays(6)->translatedFormat($entity->date_format())
2021-09-22 10:48:33 +02:00
),
],
'raw' => [
':MONTHYEAR' => $currentDateTime->month,
':MONTH' => $currentDateTime->month,
':YEAR' => $currentDateTime->year,
':QUARTER' => $currentDateTime->quarter,
2021-09-22 10:48:33 +02:00
],
'ranges' => [
'MONTHYEAR' => Carbon::createFromDate($currentDateTime->year, $currentDateTime->month),
2021-09-22 10:48:33 +02:00
],
'ranges_raw' => [
'MONTH' => $currentDateTime->month,
'YEAR' => $currentDateTime->year,
2021-09-22 10:48:33 +02:00
],
];
// First case, with ranges.
preg_match_all('/\[(.*?)]/', $value, $ranges);
$matches = array_shift($ranges);
foreach ($matches as $match) {
if (! Str::contains($match, '|')) {
2021-09-22 10:48:33 +02:00
continue;
}
if (Str::contains($match, '|')) {
$parts = explode('|', $match); // [ '[MONTH', 'MONTH+2]' ]
$left = substr($parts[0], 1); // 'MONTH'
$right = substr($parts[1], 0, -1); // MONTH+2
// If left side is not part of replacements, skip.
if (! array_key_exists($left, $replacements['ranges'])) {
2021-09-22 10:48:33 +02:00
continue;
}
$_left = Carbon::createFromDate($currentDateTime->year, $currentDateTime->month)->translatedFormat('F Y');
2021-09-22 10:48:33 +02:00
$_right = '';
// If right side doesn't have any calculations, replace with raw ranges keyword.
if (! Str::contains($right, ['-', '+', '/', '*'])) {
$_right = Carbon::createFromDate($currentDateTime->year, $currentDateTime->month)->translatedFormat('F Y');
2021-09-22 10:48:33 +02:00
}
// If right side contains one of math operations, calculate.
if (Str::contains($right, ['+'])) {
$operation = preg_match_all('/(?!^-)[+*\/-](\s?-)?/', $right, $_matches);
$_operation = array_shift($_matches)[0]; // + -
$_value = explode($_operation, $right); // [MONTHYEAR, 4]
$_right = Carbon::createFromDate($currentDateTime->year, $currentDateTime->month)->addMonths($_value[1])->translatedFormat('F Y');
2021-09-22 10:48:33 +02:00
}
$replacement = sprintf('%s to %s', $_left, $_right);
$value = preg_replace(
2023-02-16 02:36:09 +01:00
sprintf('/%s/', preg_quote($match)),
$replacement,
$value,
1
2021-09-22 10:48:33 +02:00
);
}
}
// Second case with more common calculations.
preg_match_all('/:([^:\s]+)/', $value, $common);
$matches = array_shift($common);
foreach ($matches as $match) {
$matches = collect($replacements['literal'])->filter(function ($value, $key) use ($match) {
return Str::startsWith($match, $key);
});
if ($matches->count() === 0) {
continue;
}
if (! Str::contains($match, ['-', '+', '/', '*'])) {
2021-09-22 10:48:33 +02:00
$value = preg_replace(
2023-02-16 02:36:09 +01:00
sprintf('/%s/', $matches->keys()->first()),
$replacements['literal'][$matches->keys()->first()],
$value,
1
2021-09-22 10:48:33 +02:00
);
}
if (Str::contains($match, ['-', '+', '/', '*'])) {
$operation = preg_match_all('/(?!^-)[+*\/-](\s?-)?/', $match, $_matches);
$_operation = array_shift($_matches)[0];
$_value = explode($_operation, $match); // [:MONTH, 4]
$raw = strtr($matches->keys()->first(), $replacements['raw']); // :MONTH => 1
$number = $res = preg_replace('/[^0-9]/', '', $_value[1]); // :MONTH+1. || :MONTH+2! => 1 || 2
2021-09-22 10:48:33 +02:00
$target = "/{$matches->keys()->first()}\\{$_operation}{$number}/"; // /:$KEYWORD\\$OPERATION$VALUE => /:MONTH\\+1
$output = (int) $raw + (int) $_value[1];
2021-09-22 10:48:33 +02:00
if ($operation == '+') {
$output = (int) $raw + (int) $_value[1]; // 1 (:MONTH) + 4
2021-09-22 10:48:33 +02:00
}
if ($_operation == '-') {
$output = (int) $raw - (int) $_value[1]; // 1 (:MONTH) - 4
2021-09-22 10:48:33 +02:00
}
if ($_operation == '/' && (int) $_value[1] != 0) {
$output = (int) $raw / (int) $_value[1]; // 1 (:MONTH) / 4
2021-09-22 10:48:33 +02:00
}
if ($_operation == '*') {
$output = (int) $raw * (int) $_value[1]; // 1 (:MONTH) * 4
2021-09-22 10:48:33 +02:00
}
if ($matches->keys()->first() == ':MONTH') {
$output = \Carbon\Carbon::create()->month($output)->translatedFormat('F');
}
if ($matches->keys()->first() == ':MONTHYEAR') {
$final_date = $currentDateTime->copy()->addMonths($output - $currentDateTime->month);
$output = \sprintf(
2023-02-16 02:36:09 +01:00
'%s %s',
$final_date->translatedFormat('F'),
$final_date->year,
);
}
2021-09-22 10:48:33 +02:00
$value = preg_replace(
2023-02-16 02:36:09 +01:00
$target,
$output,
$value,
1
2021-09-22 10:48:33 +02:00
);
}
}
2022-02-22 09:30:12 +01:00
return $value;
2021-09-22 10:48:33 +02:00
}
2021-10-27 14:51:43 +02:00
/**
* Resolve the font from the supported fonts array.
*
* @param string $font
* @return array
2021-10-27 14:51:43 +02:00
*/
public static function resolveFont(string $font = 'Arial'): array
{
2021-10-27 15:03:51 +02:00
return $font
2021-10-27 16:45:50 +02:00
? ['name' => str_replace('_', ' ', $font), 'url' => sprintf('https://fonts.googleapis.com/css2?family=%s&display=swap', str_replace('_', '+', $font))]
2021-10-27 15:03:51 +02:00
: ['name' => 'Arial', 'url' => ''];
2021-10-27 14:51:43 +02:00
}
}