mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-10 21:22:58 +01:00
Merge pull request #6696 from beganovich/v5-694
Process reserverd keywords
This commit is contained in:
commit
0d5a5c5722
@ -14,6 +14,8 @@ namespace App\Utils;
|
||||
|
||||
use App\Models\Client;
|
||||
use App\Utils\Traits\MakesDates;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Str;
|
||||
use stdClass;
|
||||
|
||||
class Helpers
|
||||
@ -97,4 +99,166 @@ class Helpers
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Process reserved keywords on PDF.
|
||||
*
|
||||
* @param string $value
|
||||
* @param Client $client
|
||||
* @return null|string
|
||||
*/
|
||||
public static function processReservedKeywords(string $value, Client $client): ?string
|
||||
{
|
||||
Carbon::setLocale($client->locale());
|
||||
|
||||
$replacements = [
|
||||
'literal' => [
|
||||
':MONTH' => Carbon::createFromDate(now()->year, now()->month)->translatedFormat('F'),
|
||||
':YEAR' => now()->year,
|
||||
':QUARTER' => 'Q' . now()->quarter,
|
||||
':WEEK_BEFORE' => \sprintf(
|
||||
'%s %s %s',
|
||||
Carbon::now()->subDays(7)->translatedFormat($client->date_format()),
|
||||
ctrans('texts.to'),
|
||||
Carbon::now()->translatedFormat($client->date_format())
|
||||
),
|
||||
':WEEK_AHEAD' => \sprintf(
|
||||
'%s %s %s',
|
||||
Carbon::now()->addDays(7)->translatedFormat($client->date_format()),
|
||||
ctrans('texts.to'),
|
||||
Carbon::now()->addDays(14)->translatedFormat($client->date_format())
|
||||
),
|
||||
':WEEK' => \sprintf(
|
||||
'%s %s %s',
|
||||
Carbon::now()->translatedFormat($client->date_format()),
|
||||
ctrans('texts.to'),
|
||||
Carbon::now()->addDays(7)->translatedFormat($client->date_format())
|
||||
),
|
||||
],
|
||||
'raw' => [
|
||||
':MONTH' => now()->month,
|
||||
':YEAR' => now()->year,
|
||||
':QUARTER' => now()->quarter,
|
||||
],
|
||||
'ranges' => [
|
||||
'MONTHYEAR' => Carbon::createFromDate(now()->year, now()->month),
|
||||
],
|
||||
'ranges_raw' => [
|
||||
'MONTH' => now()->month,
|
||||
'YEAR' => now()->year,
|
||||
],
|
||||
];
|
||||
|
||||
// First case, with ranges.
|
||||
preg_match_all('/\[(.*?)]/', $value, $ranges);
|
||||
|
||||
$matches = array_shift($ranges);
|
||||
|
||||
foreach ($matches as $match) {
|
||||
if (!Str::contains($match, '|')) {
|
||||
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'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$_left = Carbon::createFromDate(now()->year, now()->month)->translatedFormat('F Y');
|
||||
$_right = '';
|
||||
|
||||
// If right side doesn't have any calculations, replace with raw ranges keyword.
|
||||
if (!Str::contains($right, ['-', '+', '/', '*'])) {
|
||||
$_right = Carbon::createFromDate(now()->year, now()->month)->translatedFormat('F Y');
|
||||
}
|
||||
|
||||
// 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(now()->year, now()->month)->addMonths($_value[1])->translatedFormat('F Y');
|
||||
}
|
||||
|
||||
$replacement = sprintf('%s to %s', $_left, $_right);
|
||||
|
||||
$value = preg_replace(
|
||||
sprintf('/%s/', preg_quote($match)), $replacement, $value, 1
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 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, ['-', '+', '/', '*'])) {
|
||||
$value = preg_replace(
|
||||
sprintf('/%s/', $matches->keys()->first()), $replacements['literal'][$matches->keys()->first()], $value, 1
|
||||
);
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
$target = "/{$matches->keys()->first()}\\{$_operation}{$number}/"; // /:$KEYWORD\\$OPERATION$VALUE => /:MONTH\\+1
|
||||
|
||||
$output = (int) $raw + (int)$_value[1];
|
||||
|
||||
if ($operation == '+') {
|
||||
$output = (int) $raw + (int)$_value[1]; // 1 (:MONTH) + 4
|
||||
}
|
||||
|
||||
if ($_operation == '-') {
|
||||
$output = (int)$raw - (int)$_value[1]; // 1 (:MONTH) - 4
|
||||
}
|
||||
|
||||
if ($_operation == '/' && (int)$_value[1] != 0) {
|
||||
$output = (int)$raw / (int)$_value[1]; // 1 (:MONTH) / 4
|
||||
}
|
||||
|
||||
if ($_operation == '*') {
|
||||
$output = (int)$raw * (int)$_value[1]; // 1 (:MONTH) * 4
|
||||
}
|
||||
|
||||
if ($matches->keys()->first() == ':MONTH') {
|
||||
$output = \Carbon\Carbon::create()->month($output)->translatedFormat('F');
|
||||
}
|
||||
|
||||
$value = preg_replace(
|
||||
$target, $output, $value, 1
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
@ -127,7 +127,7 @@ class HtmlEngine
|
||||
$data['$entity'] = ['value' => '', 'label' => ctrans('texts.invoice')];
|
||||
$data['$number'] = ['value' => $this->entity->number ?: ' ', 'label' => ctrans('texts.invoice_number')];
|
||||
$data['$number_short'] = ['value' => $this->entity->number ?: ' ', 'label' => ctrans('texts.invoice_number_short')];
|
||||
$data['$entity.terms'] = ['value' => $this->entity->terms ?: '', 'label' => ctrans('texts.invoice_terms')];
|
||||
$data['$entity.terms'] = ['value' => Helpers::processReservedKeywords($this->entity->terms, $this->client) ?: '', 'label' => ctrans('texts.invoice_terms')];
|
||||
$data['$terms'] = &$data['$entity.terms'];
|
||||
$data['$view_link'] = ['value' => '<a class="button" href="'.$this->invitation->getLink().'">'.ctrans('texts.view_invoice').'</a>', 'label' => ctrans('texts.view_invoice')];
|
||||
$data['$viewLink'] = &$data['$view_link'];
|
||||
@ -237,7 +237,7 @@ class HtmlEngine
|
||||
$data['$invoice.custom2'] = ['value' => $this->helpers->formatCustomFieldValue($this->company->custom_fields, 'invoice2', $this->entity->custom_value2, $this->client) ?: ' ', 'label' => $this->helpers->makeCustomField($this->company->custom_fields, 'invoice2')];
|
||||
$data['$invoice.custom3'] = ['value' => $this->helpers->formatCustomFieldValue($this->company->custom_fields, 'invoice3', $this->entity->custom_value3, $this->client) ?: ' ', 'label' => $this->helpers->makeCustomField($this->company->custom_fields, 'invoice3')];
|
||||
$data['$invoice.custom4'] = ['value' => $this->helpers->formatCustomFieldValue($this->company->custom_fields, 'invoice4', $this->entity->custom_value4, $this->client) ?: ' ', 'label' => $this->helpers->makeCustomField($this->company->custom_fields, 'invoice4')];
|
||||
$data['$invoice.public_notes'] = ['value' => $this->entity->public_notes ?: '', 'label' => ctrans('texts.public_notes')];
|
||||
$data['$invoice.public_notes'] = ['value' => Helpers::processReservedKeywords($this->entity->public_notes, $this->client) ?: '', 'label' => ctrans('texts.public_notes')];
|
||||
$data['$entity.public_notes'] = &$data['$invoice.public_notes'];
|
||||
$data['$public_notes'] = &$data['$invoice.public_notes'];
|
||||
$data['$notes'] = &$data['$public_notes'];
|
||||
@ -435,7 +435,7 @@ class HtmlEngine
|
||||
$data['$description'] = ['value' => '', 'label' => ctrans('texts.description')];
|
||||
|
||||
//$data['$entity_footer'] = ['value' => $this->client->getSetting("{$this->entity_string}_footer"), 'label' => ''];
|
||||
$data['$entity_footer'] = ['value' => $this->entity->footer, 'label' => ''];
|
||||
$data['$entity_footer'] = ['value' => Helpers::processReservedKeywords($this->entity->footer, $this->client), 'label' => ''];
|
||||
|
||||
$data['$page_size'] = ['value' => $this->settings->page_size, 'label' => ''];
|
||||
$data['$page_layout'] = ['value' => property_exists($this->settings, 'page_layout') ? $this->settings->page_layout : 'Portrait', 'label' => ''];
|
||||
|
@ -294,8 +294,8 @@ trait MakesInvoiceValues
|
||||
$data[$key][$table_type.'.item'] = is_null(optional($item)->item) ? $item->product_key : $item->item;
|
||||
$data[$key][$table_type.'.service'] = is_null(optional($item)->service) ? $item->product_key : $item->service;
|
||||
|
||||
$data[$key][$table_type.'.notes'] = $this->processReservedKeywords($item->notes);
|
||||
$data[$key][$table_type.'.description'] = $this->processReservedKeywords($item->notes);
|
||||
$data[$key][$table_type.'.notes'] = Helpers::processReservedKeywords($item->notes, $this->client);
|
||||
$data[$key][$table_type.'.description'] = Helpers::processReservedKeywords($item->notes, $this->client);
|
||||
|
||||
/* need to test here as this is new - 18/09/2021*/
|
||||
if(!array_key_exists($table_type.'.gross_line_total', $data[$key]))
|
||||
@ -350,168 +350,6 @@ trait MakesInvoiceValues
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process reserved words like :MONTH :YEAR :QUARTER
|
||||
* as well as their operations.
|
||||
*
|
||||
* @param string $value
|
||||
* @return string|null
|
||||
*/
|
||||
private function processReservedKeywords(string $value): ?string
|
||||
{
|
||||
Carbon::setLocale($this->client->locale());
|
||||
|
||||
$replacements = [
|
||||
'literal' => [
|
||||
':MONTH' => Carbon::createFromDate(now()->year, now()->month)->translatedFormat('F'),
|
||||
':YEAR' => now()->year,
|
||||
':QUARTER' => 'Q' . now()->quarter,
|
||||
':WEEK_BEFORE' => \sprintf(
|
||||
'%s %s %s',
|
||||
Carbon::now()->subDays(7)->translatedFormat($this->client->date_format()),
|
||||
ctrans('texts.to'),
|
||||
Carbon::now()->translatedFormat($this->client->date_format())
|
||||
),
|
||||
':WEEK_AHEAD' => \sprintf(
|
||||
'%s %s %s',
|
||||
Carbon::now()->addDays(7)->translatedFormat($this->client->date_format()),
|
||||
ctrans('texts.to'),
|
||||
Carbon::now()->addDays(14)->translatedFormat($this->client->date_format())
|
||||
),
|
||||
':WEEK' => \sprintf(
|
||||
'%s %s %s',
|
||||
Carbon::now()->translatedFormat($this->client->date_format()),
|
||||
ctrans('texts.to'),
|
||||
Carbon::now()->addDays(7)->translatedFormat($this->client->date_format())
|
||||
),
|
||||
],
|
||||
'raw' => [
|
||||
':MONTH' => now()->month,
|
||||
':YEAR' => now()->year,
|
||||
':QUARTER' => now()->quarter,
|
||||
],
|
||||
'ranges' => [
|
||||
'MONTHYEAR' => Carbon::createFromDate(now()->year, now()->month),
|
||||
],
|
||||
'ranges_raw' => [
|
||||
'MONTH' => now()->month,
|
||||
'YEAR' => now()->year,
|
||||
],
|
||||
];
|
||||
|
||||
// First case, with ranges.
|
||||
preg_match_all('/\[(.*?)]/', $value, $ranges);
|
||||
|
||||
$matches = array_shift($ranges);
|
||||
|
||||
foreach ($matches as $match) {
|
||||
if (!Str::contains($match, '|')) {
|
||||
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'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$_left = Carbon::createFromDate(now()->year, now()->month)->translatedFormat('F Y');
|
||||
$_right = '';
|
||||
|
||||
// If right side doesn't have any calculations, replace with raw ranges keyword.
|
||||
if (!Str::contains($right, ['-', '+', '/', '*'])) {
|
||||
$_right = Carbon::createFromDate(now()->year, now()->month)->translatedFormat('F Y');
|
||||
}
|
||||
|
||||
// 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(now()->year, now()->month)->addMonths($_value[1])->translatedFormat('F Y');
|
||||
}
|
||||
|
||||
$replacement = sprintf('%s to %s', $_left, $_right);
|
||||
|
||||
$value = preg_replace(
|
||||
sprintf('/%s/', preg_quote($match)), $replacement, $value, 1
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 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, ['-', '+', '/', '*'])) {
|
||||
$value = preg_replace(
|
||||
sprintf('/%s/', $matches->keys()->first()), $replacements['literal'][$matches->keys()->first()], $value, 1
|
||||
);
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
$target = "/{$matches->keys()->first()}\\{$_operation}{$number}/"; // /:$KEYWORD\\$OPERATION$VALUE => /:MONTH\\+1
|
||||
|
||||
$output = (int) $raw + (int)$_value[1];
|
||||
|
||||
if ($operation == '+') {
|
||||
$output = (int) $raw + (int)$_value[1]; // 1 (:MONTH) + 4
|
||||
}
|
||||
|
||||
if ($_operation == '-') {
|
||||
$output = (int)$raw - (int)$_value[1]; // 1 (:MONTH) - 4
|
||||
}
|
||||
|
||||
if ($_operation == '/' && (int)$_value[1] != 0) {
|
||||
$output = (int)$raw / (int)$_value[1]; // 1 (:MONTH) / 4
|
||||
}
|
||||
|
||||
if ($_operation == '*') {
|
||||
$output = (int)$raw * (int)$_value[1]; // 1 (:MONTH) * 4
|
||||
}
|
||||
|
||||
if ($matches->keys()->first() == ':MONTH') {
|
||||
$output = \Carbon\Carbon::create()->month($output)->translatedFormat('F');
|
||||
}
|
||||
|
||||
$value = preg_replace(
|
||||
$target, $output, $value, 1
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Due to the way we are compiling the blade template we
|
||||
* have no ability to iterate, so in the case
|
||||
|
Loading…
Reference in New Issue
Block a user