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] 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);