1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

Utils::processVariables count with year overlaps

Works fine with increments and decrements like this ":MONTH+1 :YEAR", ":QUARTER+1 :YEAR", ":MONTH-1 :YEAR" and ":QUARTER-1 :YEAR"

Known issue: Does not work with multiple increments like this one ":MONTH+6 :YEAR, :MONTH+7 :YEAR, :MONTH+8 :YEAR"

Signed-off-by: Kristián Feldsam <feldsam@gmail.com>
This commit is contained in:
Kristián Feldsam 2019-05-01 12:48:42 +02:00
parent 694de222a7
commit 925bcca617
2 changed files with 93 additions and 0 deletions

View File

@ -865,6 +865,7 @@ class Utils
}
$variables = ['MONTH', 'QUARTER', 'YEAR', 'DATE_MONTH', 'DATE_YEAR'];
$yearOverlap = 0;
for ($i = 0; $i < count($variables); $i++) {
$variable = $variables[$i];
$regExp = '/:'.$variable.'[+-]?[\d]*/';
@ -886,6 +887,9 @@ class Utils
$offset = intval($minArray[1]) * -1;
}
$yearOverlap += self::getDateYearOverlap($variable, $offset);
if($variable === 'YEAR') $offset += $yearOverlap;
$locale = $client && $client->language_id ? $client->language->locale : null;
$val = self::getDatePart($variable, $offset, $locale);
$str = str_replace($match, $val, $str);
@ -911,6 +915,20 @@ class Utils
}
}
private static function getDateYearOverlap(string $part, int $offset): int
{
$offset = intval($offset);
switch ($part) {
case 'MONTH':
return self::getMonthYearOverlap($offset);
case 'QUARTER':
return self::getQuarterYearOverlap($offset);
}
return 0;
}
public static function getDateMonth($offset, $locale)
{
$timestamp = time();
@ -979,6 +997,34 @@ class Utils
return 'Q'.$quarter;
}
private static function getMonthYearOverlap(int $offset): int
{
$month = intval(date('n')) - 1;
$month += $offset;
if($month < 0){
$month += 1;
return (((abs($month) / 12 % 12) + 1) * -1);
}
return ($month / 12 % 12);
}
private static function getQuarterYearOverlap(int $offset): int
{
$month = intval(date('n')) - 1;
$quarter = floor(($month + 3) / 3);
$quarter += $offset - 1;
if($quarter < 0){
$quarter += 1;
return (((abs($quarter) / 4 % 4) + 1) * -1);
}
return ($quarter / 4 % 4);
}
private static function getYear($offset)
{
$year = intval(date('Y'));

View File

@ -59,6 +59,7 @@ function copyObject(orig) {
function processVariables(str) {
if (!str) return '';
var variables = ['MONTH','QUARTER','YEAR'];
var yearOverlap = 0;
for (var i=0; i<variables.length; i++) {
var variable = variables[i];
var regexp = new RegExp(':' + variable + '[+-]?[\\d]*', 'g');
@ -74,6 +75,10 @@ function processVariables(str) {
} else if (match.split('-').length > 1) {
offset = parseInt(match.split('-')[1]) * -1;
}
yearOverlap += getDateYearOverlap(variable, offset);
if(variable === 'YEAR') offset += yearOverlap;
str = str.replace(match, getDatePart(variable, offset));
}
}
@ -95,6 +100,20 @@ function getDatePart(part, offset) {
}
}
function getDateYearOverlap(part, offset)
{
offset = parseInt(offset);
switch (part) {
case 'MONTH':
return getMonthYearOverlap(offset);
case 'QUARTER':
return getQuarterYearOverlap(offset);
}
return 0;
}
function getMonth(offset) {
var today = new Date();
var months = [ "January", "February", "March", "April", "May", "June",
@ -125,6 +144,34 @@ function getQuarter(offset) {
return 'Q' + quarter;
}
function getMonthYearOverlap(offset)
{
var today = new Date();
var month = today.getMonth();
month = parseInt(month) + offset;
if(month < 0){
month += 1;
return Math.ceil(((Math.abs(month) / 12 % 12) + 1) * -1);
}
return Math.floor(month / 12 % 12);
}
function getQuarterYearOverlap(offset)
{
var today = new Date();
var quarter = Math.floor((today.getMonth() + 3) / 3);
quarter += offset - 1;
if(quarter < 0){
quarter += 1;
return Math.ceil(((Math.abs(quarter) / 4 % 4) + 1) * -1);
}
return Math.floor(quarter / 4 % 4);
}
// https://gist.github.com/beiyuu/2029907
$.fn.selectRange = function(start, end) {
var e = document.getElementById($(this).attr('id')); // I don't know why... but $(this) don't want to work today :-/