From 76a327bb017504806e911e0a84ab8f333cb6965b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Wed, 27 Jan 2021 18:01:47 +0100 Subject: [PATCH 1/6] wip --- app/Services/PdfMaker/Design.php | 2 + app/Utils/Traits/MakesInvoiceValues.php | 74 ++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/app/Services/PdfMaker/Design.php b/app/Services/PdfMaker/Design.php index 68914154da1b..8ad0c506b61a 100644 --- a/app/Services/PdfMaker/Design.php +++ b/app/Services/PdfMaker/Design.php @@ -334,6 +334,8 @@ class Design extends BaseDesign $items = $this->transformLineItems($this->entity->line_items, $type); + info($items); + if (count($items) == 0) { return []; } diff --git a/app/Utils/Traits/MakesInvoiceValues.php b/app/Utils/Traits/MakesInvoiceValues.php index 76fae1e5e15a..b78f3eaf45b4 100644 --- a/app/Utils/Traits/MakesInvoiceValues.php +++ b/app/Utils/Traits/MakesInvoiceValues.php @@ -17,6 +17,7 @@ use App\Models\Invoice; use App\Models\Quote; use App\Utils\Helpers; use App\Utils\Number; +use Illuminate\Support\Str; /** * Class MakesInvoiceValues. @@ -584,8 +585,9 @@ trait MakesInvoiceValues $data[$key][$table_type.'.product_key'] = is_null(optional($item)->product_key) ? $item->item : $item->product_key; $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'] = $item->notes; - $data[$key][$table_type.'.description'] = $item->notes; + + $data[$key][$table_type.'.notes'] = $this->processReservedKeywords($item->notes); + $data[$key][$table_type.'.description'] = $this->processReservedKeywords($item->notes); $data[$key][$table_type . ".{$_table_type}1"] = $helpers->formatCustomFieldValue($this->client->company->custom_fields, "{$_table_type}1", $item->custom_value1, $this->client); @@ -646,6 +648,74 @@ 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 + { + + $replacements = [ + 'literal' => [ + ':MONTH' => now()->localeMonth, + ':YEAR' => now()->year, + ':QUARTER' => 'Q' . now()->quarter, + ], + 'raw' => [ + ':MONTH' => now()->month, + ':YEAR' => now()->year, + ':QUARTER' => now()->quarter, + ], + ]; + + preg_match_all('/:([^:\s]+)/', $value, $m); + + $matches = array_shift($m); + + foreach ($matches as $match) { + if (!Str::contains($match, ['-', '+', '/', '*'])) { + $value = substr_replace($value, $replacements['literal'][$match], strpos($value, $match), strlen($match)); + } + + if (Str::contains($match, ['-', '+', '/', '*'])) { + $operation = preg_match_all('/(?!^-)[+*\/-](\s?-)?/', $match, $_matches); + $_operation = array_shift($_matches)[0]; + + $_value = explode($_operation, $match); // [:MONTH, 4] + + $raw = strtr($_value[0], $replacements['raw']); // :MONTH => 1 + + if ($_operation == '+') { + $calculated = (int)$raw + (int)$_value[1]; // 1 (:MONTH) + 4 + } + + if ($_operation == '-') { + $calculated = (int)$raw - (int)$_value[1]; // 1 (:MONTH) + 4 + } + + if ($_operation == '/') { + $calculated = (int)$raw / (int)$_value[1]; // 1 (:MONTH) + 4 + } + + if ($_operation == '*') { + $calculated = (int)$raw * (int)$_value[1]; // 1 (:MONTH) + 4 + } + + try { + $value = substr_replace($value, 'REPLACED', strpos($value, $match), strlen($_value[0])); + } + catch(\Exception $e) { + return $value; + } + } + } + + return $value; + } + /** * Due to the way we are compiling the blade template we * have no ability to iterate, so in the case From 6d77cce1a9039941b7ca972f25a51611ca0a4107 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Thu, 28 Jan 2021 15:18:18 +0100 Subject: [PATCH 2/6] wip --- app/Utils/Traits/MakesInvoiceValues.php | 42 ++++++++++++++++++------- 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/app/Utils/Traits/MakesInvoiceValues.php b/app/Utils/Traits/MakesInvoiceValues.php index b78f3eaf45b4..c808e05aa554 100644 --- a/app/Utils/Traits/MakesInvoiceValues.php +++ b/app/Utils/Traits/MakesInvoiceValues.php @@ -676,40 +676,58 @@ trait MakesInvoiceValues $matches = array_shift($m); 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 = substr_replace($value, $replacements['literal'][$match], strpos($value, $match), strlen($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($_value[0], $replacements['raw']); // :MONTH => 1 + $raw = strtr($matches->keys()->first(), $replacements['raw']); // :MONTH => 1 - if ($_operation == '+') { - $calculated = (int)$raw + (int)$_value[1]; // 1 (:MONTH) + 4 + $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 == '-') { - $calculated = (int)$raw - (int)$_value[1]; // 1 (:MONTH) + 4 + $output = (int)$raw - (int)$_value[1]; // 1 (:MONTH) - 4 } if ($_operation == '/') { - $calculated = (int)$raw / (int)$_value[1]; // 1 (:MONTH) + 4 + $output = (int)$raw / (int)$_value[1]; // 1 (:MONTH) / 4 } if ($_operation == '*') { - $calculated = (int)$raw * (int)$_value[1]; // 1 (:MONTH) + 4 + $output = (int)$raw * (int)$_value[1]; // 1 (:MONTH) * 4 } - try { - $value = substr_replace($value, 'REPLACED', strpos($value, $match), strlen($_value[0])); - } - catch(\Exception $e) { - return $value; + if ($matches->keys()->first() == ':MONTH') { + $output = \Carbon\Carbon::create()->month($output)->localeMonth; } + + $value = preg_replace( + $target, $output, $value, 1 + ); } } From 119b361297194f5f9b2d275f8d63f995ea040a5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Thu, 28 Jan 2021 15:19:21 +0100 Subject: [PATCH 3/6] wip --- app/Services/PdfMaker/Design.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/Services/PdfMaker/Design.php b/app/Services/PdfMaker/Design.php index 8ad0c506b61a..68914154da1b 100644 --- a/app/Services/PdfMaker/Design.php +++ b/app/Services/PdfMaker/Design.php @@ -334,8 +334,6 @@ class Design extends BaseDesign $items = $this->transformLineItems($this->entity->line_items, $type); - info($items); - if (count($items) == 0) { return []; } From b22825bf17acfee2e2393ebb951a3103f6c37122 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Mon, 22 Feb 2021 17:01:01 +0100 Subject: [PATCH 4/6] wip --- app/Utils/Traits/MakesInvoiceValues.php | 31 +++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/app/Utils/Traits/MakesInvoiceValues.php b/app/Utils/Traits/MakesInvoiceValues.php index c808e05aa554..789d5b352723 100644 --- a/app/Utils/Traits/MakesInvoiceValues.php +++ b/app/Utils/Traits/MakesInvoiceValues.php @@ -655,9 +655,8 @@ trait MakesInvoiceValues * @param string $value * @return string|null */ - private function processReservedKeywords(string $value): ?string + private function processReservedKeywords(string $value): ?string { - $replacements = [ 'literal' => [ ':MONTH' => now()->localeMonth, @@ -671,9 +670,33 @@ trait MakesInvoiceValues ], ]; - preg_match_all('/:([^:\s]+)/', $value, $m); + // First case, with ranges. + preg_match_all('/\[(.*?)]/', $value, $ranges); - $matches = array_shift($m); + $matches = array_shift($ranges); + + foreach ($matches as $match) { + if (!Str::contains($match, '|')) { + continue; + } + + if (Str::contains($match, '|')) { + $replacement = 'PLACEHOLDER FOR REPLACEMENT!'; + $parts = explode('|', $match); + + info($parts); + info($parts[0]); + + $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) { From 7319d073c83636895a71ab36849004c0ce4ab9d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Tue, 23 Feb 2021 14:56:16 +0100 Subject: [PATCH 5/6] wip --- app/Utils/Traits/MakesInvoiceValues.php | 41 +++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/app/Utils/Traits/MakesInvoiceValues.php b/app/Utils/Traits/MakesInvoiceValues.php index 789d5b352723..f573dc93ecf3 100644 --- a/app/Utils/Traits/MakesInvoiceValues.php +++ b/app/Utils/Traits/MakesInvoiceValues.php @@ -17,6 +17,7 @@ use App\Models\Invoice; use App\Models\Quote; use App\Utils\Helpers; use App\Utils\Number; +use Carbon\Carbon; use Illuminate\Support\Str; /** @@ -668,6 +669,13 @@ trait MakesInvoiceValues ':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. @@ -682,10 +690,36 @@ trait MakesInvoiceValues if (Str::contains($match, '|')) { $replacement = 'PLACEHOLDER FOR REPLACEMENT!'; - $parts = explode('|', $match); + $parts = explode('|', $match); // [ '[MONTH', 'MONTH+2]' ] - info($parts); - info($parts[0]); + $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)->format('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)->format('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])->format('F Y'); + } + + $replacement = sprintf('%s to %s', $_left, $_right); $value = preg_replace( sprintf('/%s/', preg_quote($match)), $replacement, $value, 1 @@ -693,6 +727,7 @@ trait MakesInvoiceValues } } + // Second case with more common calculations. preg_match_all('/:([^:\s]+)/', $value, $common); From 81dfb25f5fac48711daa12f4521aacbf14c27f99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Thu, 25 Feb 2021 12:00:01 +0100 Subject: [PATCH 6/6] Use translated format of months based on locale --- app/Utils/Traits/MakesInvoiceValues.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/Utils/Traits/MakesInvoiceValues.php b/app/Utils/Traits/MakesInvoiceValues.php index f573dc93ecf3..c369803ef9d3 100644 --- a/app/Utils/Traits/MakesInvoiceValues.php +++ b/app/Utils/Traits/MakesInvoiceValues.php @@ -656,8 +656,10 @@ trait MakesInvoiceValues * @param string $value * @return string|null */ - private function processReservedKeywords(string $value): ?string + private function processReservedKeywords(string $value): ?string { + Carbon::setLocale($this->client->locale()); + $replacements = [ 'literal' => [ ':MONTH' => now()->localeMonth, @@ -689,7 +691,6 @@ trait MakesInvoiceValues } if (Str::contains($match, '|')) { - $replacement = 'PLACEHOLDER FOR REPLACEMENT!'; $parts = explode('|', $match); // [ '[MONTH', 'MONTH+2]' ] $left = substr($parts[0], 1); // 'MONTH' @@ -700,12 +701,12 @@ trait MakesInvoiceValues continue; } - $_left = Carbon::createFromDate(now()->year, now()->month)->format('F Y'); + $_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)->format('F Y'); + $_right = Carbon::createFromDate(now()->year, now()->month)->translatedFormat('F Y'); } // If right side contains one of math operations, calculate. @@ -716,7 +717,7 @@ trait MakesInvoiceValues $_value = explode($_operation, $right); // [MONTHYEAR, 4] - $_right = Carbon::createFromDate(now()->year, now()->month)->addMonths($_value[1])->format('F Y'); + $_right = Carbon::createFromDate(now()->year, now()->month)->addMonths($_value[1])->translatedFormat('F Y'); } $replacement = sprintf('%s to %s', $_left, $_right);