From 599d8e939d2c317190331980c4c698283328739e Mon Sep 17 00:00:00 2001 From: Jon Waldstein Date: Thu, 14 Dec 2023 12:56:41 -0500 Subject: [PATCH] feature: add revenue compatability with v3 forms and donation model --- .../UpdateRevenueWhenDonationAmountUpdated.php | 11 +++++------ src/Revenue/RevenueServiceProvider.php | 12 +++++++++++- .../UpdateRevenueWhenDonationAmountUpdatedTest.php | 8 +++----- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/Revenue/Listeners/UpdateRevenueWhenDonationAmountUpdated.php b/src/Revenue/Listeners/UpdateRevenueWhenDonationAmountUpdated.php index d5e1ebdd0b..ea2a108e4c 100644 --- a/src/Revenue/Listeners/UpdateRevenueWhenDonationAmountUpdated.php +++ b/src/Revenue/Listeners/UpdateRevenueWhenDonationAmountUpdated.php @@ -11,14 +11,13 @@ class UpdateRevenueWhenDonationAmountUpdated { /** + * @unreleased updated to accept Donation model * @since 2.22.1 - * - * @param int $donationId The ID of the Donation. */ - public function __invoke($donationId) + public function __invoke(Donation $donation) { - give(Revenue::class)->updateRevenueAmount( - Donation::find($donationId) - ); + if ($donation->isDirty('amount')) { + give(Revenue::class)->updateRevenueAmount($donation); + } } } diff --git a/src/Revenue/RevenueServiceProvider.php b/src/Revenue/RevenueServiceProvider.php index 2fbfb1e1bd..963ed0f318 100644 --- a/src/Revenue/RevenueServiceProvider.php +++ b/src/Revenue/RevenueServiceProvider.php @@ -2,6 +2,7 @@ namespace Give\Revenue; +use Give\Donations\Models\Donation; use Give\Framework\Migrations\MigrationsRegister; use Give\Helpers\Hooks; use Give\Revenue\Listeners\DeleteRevenueWhenDonationDeleted; @@ -9,6 +10,7 @@ use Give\Revenue\Migrations\AddPastDonationsToRevenueTable; use Give\Revenue\Migrations\CreateRevenueTable; use Give\Revenue\Migrations\RemoveRevenueForeignKeys; +use Give\Revenue\Repositories\Revenue; use Give\ServiceProviders\ServiceProvider; class RevenueServiceProvider implements ServiceProvider @@ -28,6 +30,7 @@ public function register() /** * @inheritDoc * + * @unreleased added support for givewp_donation_updated and updated give_updated_edited_donation implementation * @since 2.9.0 */ public function boot() @@ -37,7 +40,14 @@ public function boot() Hooks::addAction('delete_post', DeleteRevenueWhenDonationDeleted::class, '__invoke', 10, 1); Hooks::addAction('give_insert_payment', DonationHandler::class, 'handle', 999, 1); Hooks::addAction('give_register_updates', AddPastDonationsToRevenueTable::class, 'register', 10, 1); - Hooks::addAction('give_updated_edited_donation', UpdateRevenueWhenDonationAmountUpdated::class); + Hooks::addAction('givewp_donation_updated', UpdateRevenueWhenDonationAmountUpdated::class); + add_action('give_updated_edited_donation', static function($donationId) { + $donation = Donation::find($donationId); + + if ($donation){ + (new UpdateRevenueWhenDonationAmountUpdated)($donation); + } + }); } /** diff --git a/tests/Unit/Revenue/Listeners/UpdateRevenueWhenDonationAmountUpdatedTest.php b/tests/Unit/Revenue/Listeners/UpdateRevenueWhenDonationAmountUpdatedTest.php index ac33a0a2c4..c3d2d2b64c 100644 --- a/tests/Unit/Revenue/Listeners/UpdateRevenueWhenDonationAmountUpdatedTest.php +++ b/tests/Unit/Revenue/Listeners/UpdateRevenueWhenDonationAmountUpdatedTest.php @@ -3,12 +3,9 @@ namespace Give\Tests\Unit\Revenue\Listeners; use Give\Donations\Models\Donation; -use Give\Donations\ValueObjects\DonationMode; use Give\Donations\ValueObjects\DonationStatus; -use Give\Donors\Models\Donor; use Give\Framework\Database\DB; use Give\Framework\Support\ValueObjects\Money; -use Give\PaymentGateways\Gateways\TestGateway\TestGateway; use Give\Revenue\Listeners\UpdateRevenueWhenDonationAmountUpdated; use Give\Tests\TestCase; use Give\Tests\TestTraits\RefreshDatabase; @@ -21,6 +18,7 @@ class UpdateRevenueWhenDonationAmountUpdatedTest extends TestCase use RefreshDatabase; /** + * @unreleased updated action to accept Donation model * @since 2.20.1 */ public function testRevenueIsUpdatedWhenDonationIsUpdated() @@ -34,7 +32,7 @@ public function testRevenueIsUpdatedWhenDonationIsUpdated() $donation->save(); $listener = new UpdateRevenueWhenDonationAmountUpdated(); - $listener->__invoke($donation->id); + $listener->__invoke($donation); $this->assertEquals( Money::fromDecimal(25.00, 'USD')->formatToMinorAmount(), @@ -43,7 +41,7 @@ public function testRevenueIsUpdatedWhenDonationIsUpdated() } /** - * @param Donation $donation + * @param Donation $donation * * @return int */