diff --git a/src/Billable.php b/src/Billable.php index 0db1279..8303577 100644 --- a/src/Billable.php +++ b/src/Billable.php @@ -318,6 +318,16 @@ public function orderItems() return $this->morphMany(Cashier::$orderItemModel, 'owner'); } + /** + * Get all the Scheduled Order Items for the billable model. + * + * @return \Illuminate\Database\Eloquent\Relations\MorphToMany + */ + public function scheduledOrderItems() + { + return $this->morphToMany(Cashier::$orderItemModel, 'owner', 'subscriptions', 'owner_id', 'scheduled_order_item_id'); + } + /** * Get the balances for the billable model. A separate balance is kept for each currency. * diff --git a/tests/BillableTest.php b/tests/BillableTest.php index 54e3acf..2c035ca 100644 --- a/tests/BillableTest.php +++ b/tests/BillableTest.php @@ -313,4 +313,53 @@ public function testHasActiveSubscriptionsFalse() $this->assertFalse($user->hasActiveSubscription()); } + + public function testGetScheduledOrderItems() + { + $this->withConfiguredPlans(); + $this->withMockedCouponRepository(); // 'test-coupon' + $this->withMockedGetMollieCustomer(3); + $this->withMockedGetMollieMandateAccepted(3); + + $user = $this->getMandatedUser(true, [ + 'mollie_mandate_id' => 'mdt_unique_mandate_id', + 'mollie_customer_id' => 'cst_unique_customer_id', + ]); + $subscription = $user->newSubscription('default', 'monthly-10-1')->create(); + + $this->assertEquals($subscription->scheduledOrderItem->original, $user->scheduledOrderItems[0]->original); + } + + public function testGetTwoScheduledOrderItems() + { + $this->withConfiguredPlans(); + $this->withMockedCouponRepository(); // 'test-coupon' + $this->withMockedGetMollieCustomer(6); + $this->withMockedGetMollieMandateAccepted(6); + + $user = $this->getMandatedUser(true, [ + 'mollie_mandate_id' => 'mdt_unique_mandate_id', + 'mollie_customer_id' => 'cst_unique_customer_id', + ]); + + $subscription1 = $user->newSubscription('default', 'monthly-10-1')->create(); + $subscription2 = $user->newSubscription('default', 'monthly-20-1')->create(); + + $this->assertEquals($subscription1->scheduledOrderItem->original, $user->scheduledOrderItems[0]->original); + $this->assertEquals($subscription2->scheduledOrderItem->original, $user->scheduledOrderItems[1]->original); + } + + public function testGetNoScheduledOrderItems() + { + $this->withConfiguredPlans(); + $this->withMockedCouponRepository(); // 'test-coupon' + + $user = $this->getMandatedUser(true, [ + 'mollie_mandate_id' => 'mdt_unique_mandate_id', + 'mollie_customer_id' => 'cst_unique_customer_id', + ]); + + + $this->assertEquals($user->scheduledOrderItems, $user->orderItems); + } }