Fixes for diffindays with Carbon v3

This commit is contained in:
David Bomba 2024-06-07 09:15:19 +10:00
parent 39a0ffc52d
commit d4feca33ab
11 changed files with 63 additions and 84 deletions

View File

@ -30,7 +30,7 @@ class ProRata
*/ */
public function refund(float $amount, Carbon $from_date, Carbon $to_date, int $frequency): float public function refund(float $amount, Carbon $from_date, Carbon $to_date, int $frequency): float
{ {
$days = $from_date->copy()->diffInDays($to_date); $days = intval(abs($from_date->copy()->diffInDays($to_date)));
$days_in_frequency = $this->getDaysInFrequency($frequency); $days_in_frequency = $this->getDaysInFrequency($frequency);
return round((($days / $days_in_frequency) * $amount), 2); return round((($days / $days_in_frequency) * $amount), 2);
@ -48,7 +48,7 @@ class ProRata
*/ */
public function charge(float $amount, Carbon $from_date, Carbon $to_date, int $frequency): float public function charge(float $amount, Carbon $from_date, Carbon $to_date, int $frequency): float
{ {
$days = $from_date->copy()->diffInDays($to_date); $days = intval(abs($from_date->copy()->diffInDays($to_date)));
$days_in_frequency = $this->getDaysInFrequency($frequency); $days_in_frequency = $this->getDaysInFrequency($frequency);
return round((($days / $days_in_frequency) * $amount), 2); return round((($days / $days_in_frequency) * $amount), 2);
@ -107,23 +107,23 @@ class ProRata
case RecurringInvoice::FREQUENCY_TWO_WEEKS: case RecurringInvoice::FREQUENCY_TWO_WEEKS:
return 14; return 14;
case RecurringInvoice::FREQUENCY_FOUR_WEEKS: case RecurringInvoice::FREQUENCY_FOUR_WEEKS:
return now()->diffInDays(now()->addWeeks(4)); return intval(abs(now()->diffInDays(now()->addWeeks(4))));
case RecurringInvoice::FREQUENCY_MONTHLY: case RecurringInvoice::FREQUENCY_MONTHLY:
return now()->diffInDays(now()->addMonthNoOverflow()); return intval(abs(now()->diffInDays(now()->addMonthNoOverflow())));
case RecurringInvoice::FREQUENCY_TWO_MONTHS: case RecurringInvoice::FREQUENCY_TWO_MONTHS:
return now()->diffInDays(now()->addMonthsNoOverflow(2)); return intval(abs(now()->diffInDays(now()->addMonthsNoOverflow(2))));
case RecurringInvoice::FREQUENCY_THREE_MONTHS: case RecurringInvoice::FREQUENCY_THREE_MONTHS:
return now()->diffInDays(now()->addMonthsNoOverflow(3)); return intval(abs(now()->diffInDays(now()->addMonthsNoOverflow(3))));
case RecurringInvoice::FREQUENCY_FOUR_MONTHS: case RecurringInvoice::FREQUENCY_FOUR_MONTHS:
return now()->diffInDays(now()->addMonthsNoOverflow(4)); return intval(abs(now()->diffInDays(now()->addMonthsNoOverflow(4))));
case RecurringInvoice::FREQUENCY_SIX_MONTHS: case RecurringInvoice::FREQUENCY_SIX_MONTHS:
return now()->diffInDays(now()->addMonthsNoOverflow(6)); return intval(abs(now()->diffInDays(now()->addMonthsNoOverflow(6))));
case RecurringInvoice::FREQUENCY_ANNUALLY: case RecurringInvoice::FREQUENCY_ANNUALLY:
return now()->diffInDays(now()->addYear()); return intval(abs(now()->diffInDays(now()->addYear())));
case RecurringInvoice::FREQUENCY_TWO_YEARS: case RecurringInvoice::FREQUENCY_TWO_YEARS:
return now()->diffInDays(now()->addYears(2)); return intval(abs(now()->diffInDays(now()->addYears(2))));
case RecurringInvoice::FREQUENCY_THREE_YEARS: case RecurringInvoice::FREQUENCY_THREE_YEARS:
return now()->diffInDays(now()->addYears(3)); return intval(abs(now()->diffInDays(now()->addYears(3))));
default: default:
return 0; return 0;
} }

View File

@ -77,7 +77,7 @@ class RefundCancelledAccount implements ShouldQueue
$end_date = Carbon::parse($plan_expires); $end_date = Carbon::parse($plan_expires);
$now = Carbon::now(); $now = Carbon::now();
$days_left = $now->diffInDays($end_date); $days_left = intval(abs($now->diffInDays($end_date)));
$pro_rata_ratio = $days_left / 365; $pro_rata_ratio = $days_left / 365;

View File

@ -619,7 +619,7 @@ class Account extends BaseModel
$plan_expires = Carbon::parse($this->plan_expires); $plan_expires = Carbon::parse($this->plan_expires);
if ($plan_expires->gt(now())) { if ($plan_expires->gt(now())) {
$diff = $plan_expires->diffInDays(); $diff = intval(abs($plan_expires->diffInDays()));
if ($diff > 14) { if ($diff > 14) {
return 0; return 0;

View File

@ -124,7 +124,7 @@ class ARDetailReport extends BaseExport
$client->present()->name(), $client->present()->name(),
$client->number, $client->number,
$client->id_number, $client->id_number,
Carbon::parse($invoice->due_date)->diffInDays(now()), intval(abs(Carbon::parse($invoice->due_date)->diffInDays(now()))),
Number::formatMoney($invoice->amount, $client), Number::formatMoney($invoice->amount, $client),
Number::formatMoney($invoice->balance, $client), Number::formatMoney($invoice->balance, $client),
]; ];

View File

@ -416,7 +416,7 @@ class SubscriptionService
$current_date = now(); $current_date = now();
$days_of_subscription_used = $start_date->diffInDays($current_date); $days_of_subscription_used = intval(abs($start_date->diffInDays($current_date)));
$days_in_frequency = $this->getDaysInFrequency(); $days_in_frequency = $this->getDaysInFrequency();
@ -441,7 +441,7 @@ class SubscriptionService
$current_date = now(); $current_date = now();
$days_of_subscription_used = $start_date->diffInDays($current_date); $days_of_subscription_used = intval(abs($start_date->diffInDays($current_date)));
if ($subscription) { if ($subscription) {
$days_in_frequency = $subscription->service()->getDaysInFrequency(); $days_in_frequency = $subscription->service()->getDaysInFrequency();
@ -481,7 +481,7 @@ class SubscriptionService
$current_date = now(); $current_date = now();
$days_of_subscription_used = $start_date->diffInDays($current_date); $days_of_subscription_used = intval(abs($start_date->diffInDays($current_date)));
$days_in_frequency = $invoice->subscription->service()->getDaysInFrequency(); $days_in_frequency = $invoice->subscription->service()->getDaysInFrequency();
@ -543,7 +543,7 @@ class SubscriptionService
$current_date = now(); $current_date = now();
$days_to_charge = $start_date->diffInDays($current_date); $days_to_charge = intval(abs($start_date->diffInDays($current_date)));
$days_in_frequency = $this->getDaysInFrequency(); $days_in_frequency = $this->getDaysInFrequency();
@ -1363,23 +1363,23 @@ class SubscriptionService
case RecurringInvoice::FREQUENCY_TWO_WEEKS: case RecurringInvoice::FREQUENCY_TWO_WEEKS:
return 14; return 14;
case RecurringInvoice::FREQUENCY_FOUR_WEEKS: case RecurringInvoice::FREQUENCY_FOUR_WEEKS:
return now()->diffInDays(now()->addWeeks(4)); return intval(abs(now()->diffInDays(now()->addWeeks(4))));
case RecurringInvoice::FREQUENCY_MONTHLY: case RecurringInvoice::FREQUENCY_MONTHLY:
return now()->diffInDays(now()->addMonthNoOverflow()); return intval(abs(now()->diffInDays(now()->addMonthNoOverflow())));
case RecurringInvoice::FREQUENCY_TWO_MONTHS: case RecurringInvoice::FREQUENCY_TWO_MONTHS:
return now()->diffInDays(now()->addMonthsNoOverflow(2)); return intval(abs(now()->diffInDays(now()->addMonthsNoOverflow(2))));
case RecurringInvoice::FREQUENCY_THREE_MONTHS: case RecurringInvoice::FREQUENCY_THREE_MONTHS:
return now()->diffInDays(now()->addMonthsNoOverflow(3)); return intval(abs(now()->diffInDays(now()->addMonthsNoOverflow(3))));
case RecurringInvoice::FREQUENCY_FOUR_MONTHS: case RecurringInvoice::FREQUENCY_FOUR_MONTHS:
return now()->diffInDays(now()->addMonthsNoOverflow(4)); return intval(abs(now()->diffInDays(now()->addMonthsNoOverflow(4))));
case RecurringInvoice::FREQUENCY_SIX_MONTHS: case RecurringInvoice::FREQUENCY_SIX_MONTHS:
return now()->diffInDays(now()->addMonthsNoOverflow(6)); return intval(abs(now()->diffInDays(now()->addMonthsNoOverflow(6))));
case RecurringInvoice::FREQUENCY_ANNUALLY: case RecurringInvoice::FREQUENCY_ANNUALLY:
return now()->diffInDays(now()->addYear()); return intval(abs(now()->diffInDays(now()->addYear())));
case RecurringInvoice::FREQUENCY_TWO_YEARS: case RecurringInvoice::FREQUENCY_TWO_YEARS:
return now()->diffInDays(now()->addYears(2)); return intval(abs(now()->diffInDays(now()->addYears(2))));
case RecurringInvoice::FREQUENCY_THREE_YEARS: case RecurringInvoice::FREQUENCY_THREE_YEARS:
return now()->diffInDays(now()->addYears(3)); return intval(abs(now()->diffInDays(now()->addYears(3))));
default: default:
return 0; return 0;
} }

View File

@ -101,7 +101,7 @@ class SubscriptionStatus extends AbstractService
$subscription_start_date = Carbon::parse($primary_invoice->date)->startOfDay(); $subscription_start_date = Carbon::parse($primary_invoice->date)->startOfDay();
$days_of_subscription_used = $subscription_start_date->copy()->diffInDays(now()); $days_of_subscription_used = intval(abs($subscription_start_date->copy()->diffInDays(now())));
return 1 - ($days_of_subscription_used / $this->recurring_invoice->subscription->service()->getDaysInFrequency()); return 1 - ($days_of_subscription_used / $this->recurring_invoice->subscription->service()->getDaysInFrequency());

View File

@ -58,7 +58,7 @@ trait HasRecurrence
//If the set date is less than the original date we need to add a month. //If the set date is less than the original date we need to add a month.
//If we are overflowing dates, then we need to diff the dates and ensure it doesn't equal 0 //If we are overflowing dates, then we need to diff the dates and ensure it doesn't equal 0
if ($set_date->lte($date) || $set_date->diffInDays($carbon_date) == 0) { if ($set_date->lte($date) || intval(abs($set_date->diffInDays($carbon_date))) == 0) {
$set_date->addMonthNoOverflow(); $set_date->addMonthNoOverflow();
} }

54
composer.lock generated
View File

@ -1394,16 +1394,16 @@
}, },
{ {
"name": "aws/aws-sdk-php", "name": "aws/aws-sdk-php",
"version": "3.309.0", "version": "3.311.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/aws/aws-sdk-php.git", "url": "https://github.com/aws/aws-sdk-php.git",
"reference": "9213b2101afa17fe8ea99b8ea2dad85c1b1166a8" "reference": "90218b9372469babf294f97bdd764c9d47ec8a57"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/9213b2101afa17fe8ea99b8ea2dad85c1b1166a8", "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/90218b9372469babf294f97bdd764c9d47ec8a57",
"reference": "9213b2101afa17fe8ea99b8ea2dad85c1b1166a8", "reference": "90218b9372469babf294f97bdd764c9d47ec8a57",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -1483,9 +1483,9 @@
"support": { "support": {
"forum": "https://forums.aws.amazon.com/forum.jspa?forumID=80", "forum": "https://forums.aws.amazon.com/forum.jspa?forumID=80",
"issues": "https://github.com/aws/aws-sdk-php/issues", "issues": "https://github.com/aws/aws-sdk-php/issues",
"source": "https://github.com/aws/aws-sdk-php/tree/3.309.0" "source": "https://github.com/aws/aws-sdk-php/tree/3.311.1"
}, },
"time": "2024-06-03T18:10:07+00:00" "time": "2024-06-06T18:05:50+00:00"
}, },
{ {
"name": "bacon/bacon-qr-code", "name": "bacon/bacon-qr-code",
@ -4450,16 +4450,16 @@
}, },
{ {
"name": "horstoeko/zugferd", "name": "horstoeko/zugferd",
"version": "v1.0.51", "version": "v1.0.53",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/horstoeko/zugferd.git", "url": "https://github.com/horstoeko/zugferd.git",
"reference": "9e036d4a9660638b4f51d2babb397fcff29ef046" "reference": "939e93ab2e84ec476735e5957f4db7e7d58880c3"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/horstoeko/zugferd/zipball/9e036d4a9660638b4f51d2babb397fcff29ef046", "url": "https://api.github.com/repos/horstoeko/zugferd/zipball/939e93ab2e84ec476735e5957f4db7e7d58880c3",
"reference": "9e036d4a9660638b4f51d2babb397fcff29ef046", "reference": "939e93ab2e84ec476735e5957f4db7e7d58880c3",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -4519,9 +4519,9 @@
], ],
"support": { "support": {
"issues": "https://github.com/horstoeko/zugferd/issues", "issues": "https://github.com/horstoeko/zugferd/issues",
"source": "https://github.com/horstoeko/zugferd/tree/v1.0.51" "source": "https://github.com/horstoeko/zugferd/tree/v1.0.53"
}, },
"time": "2024-05-31T17:20:07+00:00" "time": "2024-06-05T16:49:22+00:00"
}, },
{ {
"name": "hyvor/php-json-exporter", "name": "hyvor/php-json-exporter",
@ -4838,12 +4838,12 @@
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/invoiceninja/einvoice.git", "url": "https://github.com/invoiceninja/einvoice.git",
"reference": "6fe415424c14b1a0ff38f78dbf743ae93356a469" "reference": "cb519a2263398febfe51673f3fccd989a4d0ade0"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/invoiceninja/einvoice/zipball/6fe415424c14b1a0ff38f78dbf743ae93356a469", "url": "https://api.github.com/repos/invoiceninja/einvoice/zipball/cb519a2263398febfe51673f3fccd989a4d0ade0",
"reference": "6fe415424c14b1a0ff38f78dbf743ae93356a469", "reference": "cb519a2263398febfe51673f3fccd989a4d0ade0",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -4884,7 +4884,7 @@
"source": "https://github.com/invoiceninja/einvoice/tree/main", "source": "https://github.com/invoiceninja/einvoice/tree/main",
"issues": "https://github.com/invoiceninja/einvoice/issues" "issues": "https://github.com/invoiceninja/einvoice/issues"
}, },
"time": "2024-06-04T11:24:45+00:00" "time": "2024-06-05T03:18:50+00:00"
}, },
{ {
"name": "invoiceninja/inspector", "name": "invoiceninja/inspector",
@ -11025,16 +11025,16 @@
}, },
{ {
"name": "sentry/sentry", "name": "sentry/sentry",
"version": "4.7.0", "version": "4.8.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/getsentry/sentry-php.git", "url": "https://github.com/getsentry/sentry-php.git",
"reference": "d6769b2a5e6bf19ed3bbfbf52328ceaf8e6fcb1f" "reference": "3cf5778ff425a23f2d22ed41b423691d36f47163"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/getsentry/sentry-php/zipball/d6769b2a5e6bf19ed3bbfbf52328ceaf8e6fcb1f", "url": "https://api.github.com/repos/getsentry/sentry-php/zipball/3cf5778ff425a23f2d22ed41b423691d36f47163",
"reference": "d6769b2a5e6bf19ed3bbfbf52328ceaf8e6fcb1f", "reference": "3cf5778ff425a23f2d22ed41b423691d36f47163",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -11098,7 +11098,7 @@
], ],
"support": { "support": {
"issues": "https://github.com/getsentry/sentry-php/issues", "issues": "https://github.com/getsentry/sentry-php/issues",
"source": "https://github.com/getsentry/sentry-php/tree/4.7.0" "source": "https://github.com/getsentry/sentry-php/tree/4.8.0"
}, },
"funding": [ "funding": [
{ {
@ -11110,7 +11110,7 @@
"type": "custom" "type": "custom"
} }
], ],
"time": "2024-04-10T13:22:13+00:00" "time": "2024-06-05T13:18:43+00:00"
}, },
{ {
"name": "sentry/sentry-laravel", "name": "sentry/sentry-laravel",
@ -17871,16 +17871,16 @@
}, },
{ {
"name": "phpstan/phpstan", "name": "phpstan/phpstan",
"version": "1.11.3", "version": "1.11.4",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/phpstan/phpstan.git", "url": "https://github.com/phpstan/phpstan.git",
"reference": "e64220a05c1209fc856d58e789c3b7a32c0bb9a5" "reference": "9100a76ce8015b9aa7125b9171ae3a76887b6c82"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/e64220a05c1209fc856d58e789c3b7a32c0bb9a5", "url": "https://api.github.com/repos/phpstan/phpstan/zipball/9100a76ce8015b9aa7125b9171ae3a76887b6c82",
"reference": "e64220a05c1209fc856d58e789c3b7a32c0bb9a5", "reference": "9100a76ce8015b9aa7125b9171ae3a76887b6c82",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -17925,7 +17925,7 @@
"type": "github" "type": "github"
} }
], ],
"time": "2024-05-31T13:53:37+00:00" "time": "2024-06-06T12:19:22+00:00"
}, },
{ {
"name": "phpunit/php-code-coverage", "name": "phpunit/php-code-coverage",

View File

@ -54,7 +54,7 @@ class CheckRemindersTest extends TestCase
$this->invoice->service()->markSent(); $this->invoice->service()->markSent();
$this->invoice->service()->setReminder($settings)->save(); $this->invoice->service()->setReminder($settings)->save();
$this->assertEquals(0, Carbon::now()->addDays(7)->diffInDays($this->invoice->next_send_date)); $this->assertEquals(0, intval(abs(Carbon::now()->addDays(7)->diffInDays($this->invoice->next_send_date))));
} }
public function test_no_reminders_sent_to_paid_invoices() public function test_no_reminders_sent_to_paid_invoices()
@ -100,7 +100,7 @@ class CheckRemindersTest extends TestCase
$this->invoice->service()->markSent(); $this->invoice->service()->markSent();
$this->invoice->service()->setReminder($settings)->save(); $this->invoice->service()->setReminder($settings)->save();
$this->assertEquals(0, Carbon::parse($this->invoice->due_date)->subDays(29)->diffInDays($this->invoice->next_send_date)); $this->assertEquals(0, intval(abs(Carbon::parse($this->invoice->due_date)->subDays(29)->diffInDays($this->invoice->next_send_date))));
} }
public function test_after_due_date_reminder() public function test_after_due_date_reminder()
@ -120,7 +120,7 @@ class CheckRemindersTest extends TestCase
$this->invoice->service()->markSent(); $this->invoice->service()->markSent();
$this->invoice->service()->setReminder($settings)->save(); $this->invoice->service()->setReminder($settings)->save();
$this->assertEquals(0, Carbon::parse($this->invoice->due_date)->addDays(1)->diffInDays($this->invoice->next_send_date)); $this->assertEquals(0, intval(abs(Carbon::parse($this->invoice->due_date)->addDays(1)->diffInDays($this->invoice->next_send_date))));
} }
public function test_turning_off_reminders() public function test_turning_off_reminders()

View File

@ -186,7 +186,7 @@ class DatesTest extends TestCase
$start_date = Carbon::parse($string_date); $start_date = Carbon::parse($string_date);
$current_date = Carbon::parse('2021-06-20'); $current_date = Carbon::parse('2021-06-20');
$diff_in_days = $start_date->diffInDays($current_date); $diff_in_days = intval(abs($start_date->diffInDays($current_date)));
$this->assertEquals(19, $diff_in_days); $this->assertEquals(19, $diff_in_days);
} }
@ -195,9 +195,9 @@ class DatesTest extends TestCase
{ {
$now = Carbon::parse('2020-01-01'); $now = Carbon::parse('2020-01-01');
$x = now()->diffInDays(now()->addDays(7)); $x = intval(abs(now()->diffInDays(now()->addDays(7))));
$this->assertEquals(7, $x); $this->assertEquals(7, intval(abs($x)));
} }
public function testFourteenDaysFromNow() public function testFourteenDaysFromNow()

View File

@ -25,29 +25,8 @@ class RefundUnitTest extends TestCase
{ {
parent::setUp(); parent::setUp();
} }
// public function testProRataRefundMonthly()
// {
// $pro_rata = new ProRata();
// $refund = $pro_rata->refund(10, Carbon::parse('2021-01-01'), Carbon::parse('2021-01-31'), RecurringInvoice::FREQUENCY_MONTHLY);
// $this->assertEquals(9.68, $refund);
// $this->assertEquals(30, Carbon::parse('2021-01-01')->diffInDays(Carbon::parse('2021-01-31')));
// }
// public function testProRataRefundYearly()
// {
// $pro_rata = new ProRata();
// $refund = $pro_rata->refund(10, Carbon::parse('2021-01-01'), Carbon::parse('2021-01-31'), RecurringInvoice::FREQUENCY_ANNUALLY);
// $this->assertEquals(0.82, $refund);
// }
public function testDiffInDays() public function testDiffInDays()
{ {
$this->assertEquals(30, Carbon::parse('2021-01-01')->diffInDays(Carbon::parse('2021-01-31'))); $this->assertEquals(30, intval(abs(Carbon::parse('2021-01-01')->diffInDays(Carbon::parse('2021-01-31')))));
} }
} }