66use Livewire \Component ;
77use App \Models \ServiceCategory ;
88use App \Models \ServiceProvider ;
9+ use App \Models \ContractReminder ;
910use Illuminate \Support \Facades \Auth ;
1011use App \Models \CompanyServiceContract ;
1112use Illuminate \Foundation \Auth \Access \AuthorizesRequests ;
@@ -24,7 +25,7 @@ class ContractManager extends Component
2425 public $ service_provider_id ;
2526 public $ budget ;
2627 public $ start_date ;
27- public $ next_due_date ;
28+ public $ end_date ;
2829 public $ status = 'active ' ;
2930 public $ notes ;
3031
@@ -36,6 +37,17 @@ class ContractManager extends Component
3637 public ?CompanyServiceContract $ editingContract = null ;
3738 public ?CompanyServiceContract $ deletingContract = null ;
3839
40+ // Reminder properties
41+ public bool $ showReminderModal = false ;
42+ public ?int $ contractReminderContractId = null ;
43+ public $ reminder_title , $ reminder_due_date , $ reminder_frequency = 'manual ' ;
44+ public $ reminder_day_of_month , $ reminder_custom_dates = [], $ reminder_months_active = [];
45+ public $ reminder_days_before = 7 , $ reminder_days_after = 0 , $ reminder_notes ;
46+ public string $ reminder_months_active_string = '' ;
47+ public string $ reminder_custom_dates_string = '' ;
48+ public ?int $ reminder_id = null ;
49+
50+
3951 public function mount ()
4052 {
4153 $ this ->companies = Company::where ('business_id ' , Auth::user ()->current_business_id )->orderBy ('name ' )->get ();
@@ -53,14 +65,14 @@ public function updatedServiceCategoryId($categoryId)
5365
5466 public function loadContracts ()
5567 {
56- $ this ->contracts = CompanyServiceContract::with (['company ' , 'provider ' , 'category ' ])
68+ $ this ->contracts = CompanyServiceContract::with (['company ' , 'provider ' , 'category ' , ' reminders ' ])
5769 ->whereHas ('company ' , fn ($ q ) => $ q ->where ('business_id ' , Auth::user ()->current_business_id ))
5870 ->latest ()->get ();
5971 }
6072
6173 public function openCreateModal ()
6274 {
63- $ this ->reset (['company_id ' , 'service_category_id ' , 'service_provider_id ' , 'budget ' , 'start_date ' , 'next_due_date ' , 'status ' , 'notes ' ]);
75+ $ this ->reset (['company_id ' , 'service_category_id ' , 'service_provider_id ' , 'budget ' , 'start_date ' , 'end_date ' , 'status ' , 'notes ' ]);
6476 $ this ->resetValidation ();
6577 $ this ->showCreateModal = true ;
6678 }
@@ -73,7 +85,7 @@ public function create()
7385 'service_provider_id ' => 'required|exists:service_providers,id ' ,
7486 'budget ' => 'nullable|numeric ' ,
7587 'start_date ' => 'nullable|date ' ,
76- 'next_due_date ' => 'nullable|date ' ,
88+ 'end_date ' => 'nullable|date ' ,
7789 'status ' => 'required|in:active,inactive,terminated ' ,
7890 ]);
7991
@@ -83,7 +95,7 @@ public function create()
8395 'service_provider_id ' => $ this ->service_provider_id ,
8496 'budget ' => $ this ->budget ,
8597 'start_date ' => $ this ->start_date ,
86- 'next_due_date ' => $ this ->next_due_date ,
98+ 'end_date ' => $ this ->end_date ,
8799 'status ' => $ this ->status ,
88100 'notes ' => $ this ->notes ,
89101 ]);
@@ -103,7 +115,7 @@ public function edit($id)
103115 $ this ->service_provider_id = $ this ->editingContract ->service_provider_id ;
104116 $ this ->budget = $ this ->editingContract ->budget ;
105117 $ this ->start_date = $ this ->editingContract ->start_date ;
106- $ this ->next_due_date = $ this ->editingContract ->next_due_date ;
118+ $ this ->end_date = $ this ->editingContract ->end_date ;
107119 $ this ->status = $ this ->editingContract ->status ;
108120 $ this ->notes = $ this ->editingContract ->notes ;
109121
@@ -118,7 +130,7 @@ public function update()
118130 'service_provider_id ' => 'required|exists:service_providers,id ' ,
119131 'budget ' => 'nullable|numeric ' ,
120132 'start_date ' => 'nullable|date ' ,
121- 'next_due_date ' => 'nullable|date ' ,
133+ 'end_date ' => 'nullable|date ' ,
122134 'status ' => 'required|in:active,inactive,terminated ' ,
123135 ]);
124136
@@ -130,7 +142,7 @@ public function update()
130142 'service_provider_id ' => $ this ->service_provider_id ,
131143 'budget ' => $ this ->budget ,
132144 'start_date ' => $ this ->start_date ,
133- 'next_due_date ' => $ this ->next_due_date ,
145+ 'end_date ' => $ this ->end_date ,
134146 'status ' => $ this ->status ,
135147 'notes ' => $ this ->notes ,
136148 ]);
@@ -158,6 +170,91 @@ public function delete()
158170 session ()->flash ('success ' , 'Contract deleted. ' );
159171 }
160172
173+ public function openReminderModal ($ contractIdToAttachToReminder )
174+ {
175+ $ this ->reset ([
176+ 'reminder_title ' ,
177+ 'reminder_due_date ' ,
178+ 'reminder_frequency ' ,
179+ 'reminder_day_of_month ' ,
180+ 'reminder_custom_dates ' ,
181+ 'reminder_months_active ' ,
182+ 'reminder_days_before ' ,
183+ 'reminder_days_after ' ,
184+ 'reminder_notes ' ,
185+ 'reminder_id ' ,
186+ ]);
187+
188+ $ this ->contractReminderContractId = $ contractIdToAttachToReminder ;
189+ $ this ->showReminderModal = true ;
190+ }
191+
192+ public function saveReminder ()
193+ {
194+ $ months = collect (explode (', ' , $ this ->reminder_months_active_string ))
195+ ->filter ()
196+ ->map (fn ($ m ) => (int ) trim ($ m ))
197+ ->filter (fn ($ m ) => $ m >= 1 && $ m <= 12 )
198+ ->values ()
199+ ->toArray ();
200+
201+ $ dates = collect (explode (', ' , $ this ->reminder_custom_dates_string ))
202+ ->map (fn ($ d ) => trim ($ d ))
203+ ->filter (function ($ d ) {
204+ try {
205+ return \Carbon \Carbon::createFromFormat ('Y-m-d ' , $ d ) !== false ;
206+ } catch (\Exception $ e ) {
207+ return false ;
208+ }
209+ })
210+ ->values ()
211+ ->toArray ();
212+
213+ $ data = [
214+ 'company_service_contract_id ' => $ this ->contractReminderContractId ,
215+ 'title ' => $ this ->reminder_title ,
216+ 'due_date ' => $ this ->reminder_due_date ,
217+ 'frequency ' => $ this ->reminder_frequency ,
218+ 'day_of_month ' => $ this ->reminder_day_of_month ,
219+ 'custom_dates ' => $ dates ,
220+ 'months_active ' => $ months ,
221+ 'reminder_days_before ' => $ this ->reminder_days_before ,
222+ 'reminder_days_after ' => $ this ->reminder_days_after ,
223+ 'notes ' => $ this ->reminder_notes ,
224+ ];
225+
226+ if ($ this ->reminder_id ) {
227+ ContractReminder::find ($ this ->reminder_id )?->update($ data );
228+ session ()->flash ('success ' , 'Reminder updated. ' );
229+ } else {
230+ ContractReminder::create ($ data );
231+ session ()->flash ('success ' , 'Reminder created. ' );
232+ }
233+
234+ $ this ->reset (['showReminderModal ' , 'reminder_id ' ]);
235+ $ this ->loadContracts ();
236+ }
237+
238+ public function editReminder ($ reminderId )
239+ {
240+ $ reminder = ContractReminder::findOrFail ($ reminderId );
241+
242+ $ this ->reminder_id = $ reminder ->id ;
243+ $ this ->contractReminderContractId = $ reminder ->company_service_contract_id ;
244+
245+ $ this ->reminder_title = $ reminder ->title ;
246+ $ this ->reminder_due_date = $ reminder ->due_date ;
247+ $ this ->reminder_frequency = $ reminder ->frequency ;
248+ $ this ->reminder_day_of_month = $ reminder ->day_of_month ;
249+ $ this ->reminder_custom_dates_string = collect ($ reminder ->custom_dates )->implode (', ' );
250+ $ this ->reminder_months_active_string = collect ($ reminder ->months_active )->implode (', ' );
251+ $ this ->reminder_days_before = $ reminder ->reminder_days_before ;
252+ $ this ->reminder_days_after = $ reminder ->reminder_days_after ;
253+ $ this ->reminder_notes = $ reminder ->notes ;
254+
255+ $ this ->showReminderModal = true ;
256+ }
257+
161258 public function render ()
162259 {
163260 return view ('livewire.company.service.contract-manager ' );
0 commit comments