22
33namespace App \Livewire ;
44
5+ use App \Enums \Subscription ;
6+ use App \Enums \TeamUserStatus ;
57use App \Models \Team ;
68use Livewire \Component ;
79
@@ -14,17 +16,126 @@ public function mount(Team $team): void
1416 $ this ->team = $ team ;
1517 }
1618
19+ public function addSeats (int $ count = 1 ): void
20+ {
21+ $ owner = $ this ->team ->owner ;
22+ $ subscription = $ owner ->subscription ();
23+
24+ if (! $ subscription ) {
25+ return ;
26+ }
27+
28+ // Determine the correct extra seat price based on subscription interval
29+ $ planPriceId = $ subscription ->stripe_price ;
30+
31+ if (! $ planPriceId ) {
32+ foreach ($ subscription ->items as $ item ) {
33+ if (! Subscription::isExtraSeatPrice ($ item ->stripe_price )) {
34+ $ planPriceId = $ item ->stripe_price ;
35+ break ;
36+ }
37+ }
38+ }
39+
40+ $ isMonthly = $ planPriceId === config ('subscriptions.plans.max.stripe_price_id_monthly ' );
41+ $ interval = $ isMonthly ? 'month ' : 'year ' ;
42+ $ priceId = Subscription::extraSeatStripePriceId ($ interval );
43+
44+ if (! $ priceId ) {
45+ return ;
46+ }
47+
48+ // Check if subscription already has this price item
49+ $ existingItem = $ subscription ->items ->firstWhere ('stripe_price ' , $ priceId );
50+
51+ if ($ existingItem ) {
52+ $ subscription ->incrementAndInvoice ($ count , $ priceId );
53+ } else {
54+ $ subscription ->addPriceAndInvoice ($ priceId , $ count );
55+ }
56+
57+ $ this ->team ->increment ('extra_seats ' , $ count );
58+ $ this ->team ->refresh ();
59+
60+ $ this ->dispatch ('seats-updated ' );
61+ }
62+
63+ public function removeSeats (int $ count = 1 ): void
64+ {
65+ if ($ this ->team ->extra_seats < $ count ) {
66+ return ;
67+ }
68+
69+ // Don't allow removing seats if it would go below occupied count
70+ $ newCapacity = $ this ->team ->totalSeatCapacity () - $ count ;
71+ if ($ newCapacity < $ this ->team ->occupiedSeatCount ()) {
72+ return ;
73+ }
74+
75+ $ owner = $ this ->team ->owner ;
76+ $ subscription = $ owner ->subscription ();
77+
78+ if (! $ subscription ) {
79+ return ;
80+ }
81+
82+ $ planPriceId = $ subscription ->stripe_price ;
83+
84+ if (! $ planPriceId ) {
85+ foreach ($ subscription ->items as $ item ) {
86+ if (! Subscription::isExtraSeatPrice ($ item ->stripe_price )) {
87+ $ planPriceId = $ item ->stripe_price ;
88+ break ;
89+ }
90+ }
91+ }
92+
93+ $ isMonthly = $ planPriceId === config ('subscriptions.plans.max.stripe_price_id_monthly ' );
94+ $ interval = $ isMonthly ? 'month ' : 'year ' ;
95+ $ priceId = Subscription::extraSeatStripePriceId ($ interval );
96+
97+ if (! $ priceId ) {
98+ return ;
99+ }
100+
101+ $ existingItem = $ subscription ->items ->firstWhere ('stripe_price ' , $ priceId );
102+
103+ if ($ existingItem ) {
104+ if ($ existingItem ->quantity <= $ count ) {
105+ $ subscription ->removePrice ($ priceId );
106+ } else {
107+ $ subscription ->decrementQuantity ($ count , $ priceId );
108+ }
109+ }
110+
111+ $ this ->team ->decrement ('extra_seats ' , $ count );
112+ $ this ->team ->refresh ();
113+
114+ $ this ->dispatch ('seats-updated ' );
115+ }
116+
17117 public function render ()
18118 {
19119 $ this ->team ->refresh ();
20120 $ this ->team ->load ('users ' );
21121
22- $ activeMembers = $ this ->team ->users ->where ('status ' , \App \Enums \TeamUserStatus::Active);
23- $ pendingInvitations = $ this ->team ->users ->where ('status ' , \App \Enums \TeamUserStatus::Pending);
122+ $ activeMembers = $ this ->team ->users ->where ('status ' , TeamUserStatus::Active);
123+ $ pendingInvitations = $ this ->team ->users ->where ('status ' , TeamUserStatus::Pending);
124+
125+ $ extraSeatPriceYearly = config ('subscriptions.plans.max.extra_seat_price_yearly ' , 4 );
126+ $ extraSeatPriceMonthly = config ('subscriptions.plans.max.extra_seat_price_monthly ' , 5 );
127+
128+ $ removableSeats = min (
129+ $ this ->team ->extra_seats ,
130+ $ this ->team ->totalSeatCapacity () - $ this ->team ->occupiedSeatCount ()
131+ );
24132
25133 return view ('livewire.team-manager ' , [
26134 'activeMembers ' => $ activeMembers ,
27135 'pendingInvitations ' => $ pendingInvitations ,
136+ 'extraSeatPriceYearly ' => $ extraSeatPriceYearly ,
137+ 'extraSeatPriceMonthly ' => $ extraSeatPriceMonthly ,
138+ 'removableSeats ' => max (0 , $ removableSeats ),
28139 ]);
29140 }
30141}
0 commit comments