From d4feca33ab72a9b84cbbd60fd8092379c3580820 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 7 Jun 2024 09:15:19 +1000 Subject: [PATCH] Fixes for diffindays with Carbon v3 --- app/Helpers/Invoice/ProRata.php | 22 ++++---- app/Jobs/Ninja/RefundCancelledAccount.php | 2 +- app/Models/Account.php | 2 +- app/Services/Report/ARDetailReport.php | 2 +- .../Subscription/SubscriptionService.php | 26 ++++----- .../Subscription/SubscriptionStatus.php | 2 +- app/Utils/Traits/Recurring/HasRecurrence.php | 2 +- composer.lock | 54 +++++++++---------- tests/Integration/CheckRemindersTest.php | 6 +-- tests/Unit/DatesTest.php | 6 +-- tests/Unit/RefundUnitTest.php | 23 +------- 11 files changed, 63 insertions(+), 84 deletions(-) diff --git a/app/Helpers/Invoice/ProRata.php b/app/Helpers/Invoice/ProRata.php index e012036ad5d4..efb73ac5383a 100644 --- a/app/Helpers/Invoice/ProRata.php +++ b/app/Helpers/Invoice/ProRata.php @@ -30,7 +30,7 @@ class ProRata */ 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); 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 { - $days = $from_date->copy()->diffInDays($to_date); + $days = intval(abs($from_date->copy()->diffInDays($to_date))); $days_in_frequency = $this->getDaysInFrequency($frequency); return round((($days / $days_in_frequency) * $amount), 2); @@ -107,23 +107,23 @@ class ProRata case RecurringInvoice::FREQUENCY_TWO_WEEKS: return 14; case RecurringInvoice::FREQUENCY_FOUR_WEEKS: - return now()->diffInDays(now()->addWeeks(4)); + return intval(abs(now()->diffInDays(now()->addWeeks(4)))); case RecurringInvoice::FREQUENCY_MONTHLY: - return now()->diffInDays(now()->addMonthNoOverflow()); + return intval(abs(now()->diffInDays(now()->addMonthNoOverflow()))); case RecurringInvoice::FREQUENCY_TWO_MONTHS: - return now()->diffInDays(now()->addMonthsNoOverflow(2)); + return intval(abs(now()->diffInDays(now()->addMonthsNoOverflow(2)))); case RecurringInvoice::FREQUENCY_THREE_MONTHS: - return now()->diffInDays(now()->addMonthsNoOverflow(3)); + return intval(abs(now()->diffInDays(now()->addMonthsNoOverflow(3)))); case RecurringInvoice::FREQUENCY_FOUR_MONTHS: - return now()->diffInDays(now()->addMonthsNoOverflow(4)); + return intval(abs(now()->diffInDays(now()->addMonthsNoOverflow(4)))); case RecurringInvoice::FREQUENCY_SIX_MONTHS: - return now()->diffInDays(now()->addMonthsNoOverflow(6)); + return intval(abs(now()->diffInDays(now()->addMonthsNoOverflow(6)))); case RecurringInvoice::FREQUENCY_ANNUALLY: - return now()->diffInDays(now()->addYear()); + return intval(abs(now()->diffInDays(now()->addYear()))); case RecurringInvoice::FREQUENCY_TWO_YEARS: - return now()->diffInDays(now()->addYears(2)); + return intval(abs(now()->diffInDays(now()->addYears(2)))); case RecurringInvoice::FREQUENCY_THREE_YEARS: - return now()->diffInDays(now()->addYears(3)); + return intval(abs(now()->diffInDays(now()->addYears(3)))); default: return 0; } diff --git a/app/Jobs/Ninja/RefundCancelledAccount.php b/app/Jobs/Ninja/RefundCancelledAccount.php index 835531e23dc4..52c1ccaa3cb5 100644 --- a/app/Jobs/Ninja/RefundCancelledAccount.php +++ b/app/Jobs/Ninja/RefundCancelledAccount.php @@ -77,7 +77,7 @@ class RefundCancelledAccount implements ShouldQueue $end_date = Carbon::parse($plan_expires); $now = Carbon::now(); - $days_left = $now->diffInDays($end_date); + $days_left = intval(abs($now->diffInDays($end_date))); $pro_rata_ratio = $days_left / 365; diff --git a/app/Models/Account.php b/app/Models/Account.php index 81d262454b79..7d3c1ac4f681 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -619,7 +619,7 @@ class Account extends BaseModel $plan_expires = Carbon::parse($this->plan_expires); if ($plan_expires->gt(now())) { - $diff = $plan_expires->diffInDays(); + $diff = intval(abs($plan_expires->diffInDays())); if ($diff > 14) { return 0; diff --git a/app/Services/Report/ARDetailReport.php b/app/Services/Report/ARDetailReport.php index 8b937b495de2..44e41c6ee01b 100644 --- a/app/Services/Report/ARDetailReport.php +++ b/app/Services/Report/ARDetailReport.php @@ -124,7 +124,7 @@ class ARDetailReport extends BaseExport $client->present()->name(), $client->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->balance, $client), ]; diff --git a/app/Services/Subscription/SubscriptionService.php b/app/Services/Subscription/SubscriptionService.php index 78beb043bc3f..50f2aa792f89 100644 --- a/app/Services/Subscription/SubscriptionService.php +++ b/app/Services/Subscription/SubscriptionService.php @@ -416,7 +416,7 @@ class SubscriptionService $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(); @@ -441,7 +441,7 @@ class SubscriptionService $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) { $days_in_frequency = $subscription->service()->getDaysInFrequency(); @@ -481,7 +481,7 @@ class SubscriptionService $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(); @@ -543,7 +543,7 @@ class SubscriptionService $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(); @@ -1363,23 +1363,23 @@ class SubscriptionService case RecurringInvoice::FREQUENCY_TWO_WEEKS: return 14; case RecurringInvoice::FREQUENCY_FOUR_WEEKS: - return now()->diffInDays(now()->addWeeks(4)); + return intval(abs(now()->diffInDays(now()->addWeeks(4)))); case RecurringInvoice::FREQUENCY_MONTHLY: - return now()->diffInDays(now()->addMonthNoOverflow()); + return intval(abs(now()->diffInDays(now()->addMonthNoOverflow()))); case RecurringInvoice::FREQUENCY_TWO_MONTHS: - return now()->diffInDays(now()->addMonthsNoOverflow(2)); + return intval(abs(now()->diffInDays(now()->addMonthsNoOverflow(2)))); case RecurringInvoice::FREQUENCY_THREE_MONTHS: - return now()->diffInDays(now()->addMonthsNoOverflow(3)); + return intval(abs(now()->diffInDays(now()->addMonthsNoOverflow(3)))); case RecurringInvoice::FREQUENCY_FOUR_MONTHS: - return now()->diffInDays(now()->addMonthsNoOverflow(4)); + return intval(abs(now()->diffInDays(now()->addMonthsNoOverflow(4)))); case RecurringInvoice::FREQUENCY_SIX_MONTHS: - return now()->diffInDays(now()->addMonthsNoOverflow(6)); + return intval(abs(now()->diffInDays(now()->addMonthsNoOverflow(6)))); case RecurringInvoice::FREQUENCY_ANNUALLY: - return now()->diffInDays(now()->addYear()); + return intval(abs(now()->diffInDays(now()->addYear()))); case RecurringInvoice::FREQUENCY_TWO_YEARS: - return now()->diffInDays(now()->addYears(2)); + return intval(abs(now()->diffInDays(now()->addYears(2)))); case RecurringInvoice::FREQUENCY_THREE_YEARS: - return now()->diffInDays(now()->addYears(3)); + return intval(abs(now()->diffInDays(now()->addYears(3)))); default: return 0; } diff --git a/app/Services/Subscription/SubscriptionStatus.php b/app/Services/Subscription/SubscriptionStatus.php index b976e6e94499..8aa6226bfdbe 100644 --- a/app/Services/Subscription/SubscriptionStatus.php +++ b/app/Services/Subscription/SubscriptionStatus.php @@ -101,7 +101,7 @@ class SubscriptionStatus extends AbstractService $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()); diff --git a/app/Utils/Traits/Recurring/HasRecurrence.php b/app/Utils/Traits/Recurring/HasRecurrence.php index 08728eeaec54..b59fdbe512c4 100644 --- a/app/Utils/Traits/Recurring/HasRecurrence.php +++ b/app/Utils/Traits/Recurring/HasRecurrence.php @@ -58,7 +58,7 @@ trait HasRecurrence //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 ($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(); } diff --git a/composer.lock b/composer.lock index cb5cc61fe863..59743c9d71e4 100644 --- a/composer.lock +++ b/composer.lock @@ -1394,16 +1394,16 @@ }, { "name": "aws/aws-sdk-php", - "version": "3.309.0", + "version": "3.311.1", "source": { "type": "git", "url": "https://github.com/aws/aws-sdk-php.git", - "reference": "9213b2101afa17fe8ea99b8ea2dad85c1b1166a8" + "reference": "90218b9372469babf294f97bdd764c9d47ec8a57" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/9213b2101afa17fe8ea99b8ea2dad85c1b1166a8", - "reference": "9213b2101afa17fe8ea99b8ea2dad85c1b1166a8", + "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/90218b9372469babf294f97bdd764c9d47ec8a57", + "reference": "90218b9372469babf294f97bdd764c9d47ec8a57", "shasum": "" }, "require": { @@ -1483,9 +1483,9 @@ "support": { "forum": "https://forums.aws.amazon.com/forum.jspa?forumID=80", "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", @@ -4450,16 +4450,16 @@ }, { "name": "horstoeko/zugferd", - "version": "v1.0.51", + "version": "v1.0.53", "source": { "type": "git", "url": "https://github.com/horstoeko/zugferd.git", - "reference": "9e036d4a9660638b4f51d2babb397fcff29ef046" + "reference": "939e93ab2e84ec476735e5957f4db7e7d58880c3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/horstoeko/zugferd/zipball/9e036d4a9660638b4f51d2babb397fcff29ef046", - "reference": "9e036d4a9660638b4f51d2babb397fcff29ef046", + "url": "https://api.github.com/repos/horstoeko/zugferd/zipball/939e93ab2e84ec476735e5957f4db7e7d58880c3", + "reference": "939e93ab2e84ec476735e5957f4db7e7d58880c3", "shasum": "" }, "require": { @@ -4519,9 +4519,9 @@ ], "support": { "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", @@ -4838,12 +4838,12 @@ "source": { "type": "git", "url": "https://github.com/invoiceninja/einvoice.git", - "reference": "6fe415424c14b1a0ff38f78dbf743ae93356a469" + "reference": "cb519a2263398febfe51673f3fccd989a4d0ade0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/invoiceninja/einvoice/zipball/6fe415424c14b1a0ff38f78dbf743ae93356a469", - "reference": "6fe415424c14b1a0ff38f78dbf743ae93356a469", + "url": "https://api.github.com/repos/invoiceninja/einvoice/zipball/cb519a2263398febfe51673f3fccd989a4d0ade0", + "reference": "cb519a2263398febfe51673f3fccd989a4d0ade0", "shasum": "" }, "require": { @@ -4884,7 +4884,7 @@ "source": "https://github.com/invoiceninja/einvoice/tree/main", "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", @@ -11025,16 +11025,16 @@ }, { "name": "sentry/sentry", - "version": "4.7.0", + "version": "4.8.0", "source": { "type": "git", "url": "https://github.com/getsentry/sentry-php.git", - "reference": "d6769b2a5e6bf19ed3bbfbf52328ceaf8e6fcb1f" + "reference": "3cf5778ff425a23f2d22ed41b423691d36f47163" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/getsentry/sentry-php/zipball/d6769b2a5e6bf19ed3bbfbf52328ceaf8e6fcb1f", - "reference": "d6769b2a5e6bf19ed3bbfbf52328ceaf8e6fcb1f", + "url": "https://api.github.com/repos/getsentry/sentry-php/zipball/3cf5778ff425a23f2d22ed41b423691d36f47163", + "reference": "3cf5778ff425a23f2d22ed41b423691d36f47163", "shasum": "" }, "require": { @@ -11098,7 +11098,7 @@ ], "support": { "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": [ { @@ -11110,7 +11110,7 @@ "type": "custom" } ], - "time": "2024-04-10T13:22:13+00:00" + "time": "2024-06-05T13:18:43+00:00" }, { "name": "sentry/sentry-laravel", @@ -17871,16 +17871,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.11.3", + "version": "1.11.4", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "e64220a05c1209fc856d58e789c3b7a32c0bb9a5" + "reference": "9100a76ce8015b9aa7125b9171ae3a76887b6c82" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/e64220a05c1209fc856d58e789c3b7a32c0bb9a5", - "reference": "e64220a05c1209fc856d58e789c3b7a32c0bb9a5", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/9100a76ce8015b9aa7125b9171ae3a76887b6c82", + "reference": "9100a76ce8015b9aa7125b9171ae3a76887b6c82", "shasum": "" }, "require": { @@ -17925,7 +17925,7 @@ "type": "github" } ], - "time": "2024-05-31T13:53:37+00:00" + "time": "2024-06-06T12:19:22+00:00" }, { "name": "phpunit/php-code-coverage", diff --git a/tests/Integration/CheckRemindersTest.php b/tests/Integration/CheckRemindersTest.php index 9911349f05bd..e4b2583f6a78 100644 --- a/tests/Integration/CheckRemindersTest.php +++ b/tests/Integration/CheckRemindersTest.php @@ -54,7 +54,7 @@ class CheckRemindersTest extends TestCase $this->invoice->service()->markSent(); $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() @@ -100,7 +100,7 @@ class CheckRemindersTest extends TestCase $this->invoice->service()->markSent(); $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() @@ -120,7 +120,7 @@ class CheckRemindersTest extends TestCase $this->invoice->service()->markSent(); $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() diff --git a/tests/Unit/DatesTest.php b/tests/Unit/DatesTest.php index 3f5ca6e2d4dd..9019d7b445dc 100644 --- a/tests/Unit/DatesTest.php +++ b/tests/Unit/DatesTest.php @@ -186,7 +186,7 @@ class DatesTest extends TestCase $start_date = Carbon::parse($string_date); $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); } @@ -195,9 +195,9 @@ class DatesTest extends TestCase { $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() diff --git a/tests/Unit/RefundUnitTest.php b/tests/Unit/RefundUnitTest.php index 067eeca9ae77..4221527c6604 100644 --- a/tests/Unit/RefundUnitTest.php +++ b/tests/Unit/RefundUnitTest.php @@ -25,29 +25,8 @@ class RefundUnitTest extends TestCase { 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() { - $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'))))); } }