@@ -191,6 +191,62 @@ public function postEdit($licenseId = null)
191191 $ license ->purchase_date = e (Input::get ('purchase_date ' ));
192192 $ license ->purchase_cost = e (Input::get ('purchase_cost ' ));
193193
194+ //Are we changing the total number of seats?
195+ if ( $ license ->seats != e (Input::get ('seats ' )))
196+ {
197+ //Determine how many seats we are dealing with
198+ $ difference = e (Input::get ('seats ' )) - $ license ->licenseseats ()->count ();
199+
200+ if ( $ difference < 0 )
201+ {
202+ //Filter out any license which have a user attached;
203+ $ seats = $ license ->licenseseats ->filter (function ($ seat )
204+ {
205+ return is_null ($ seat ->user );
206+ });
207+
208+ //If the remaining collection is as large or larger than the number of seats we want to delete
209+ if ($ seats ->count () >= abs ($ difference ))
210+ {
211+ for ($ i =1 ; $ i <= abs ($ difference ); $ i ++) {
212+ //Delete the appropriate number of seats
213+ $ seats ->first ()->delete ();
214+ }
215+
216+ //Log the deletion of seats to the log
217+ $ logaction = new Actionlog ();
218+ $ logaction ->asset_id = $ license ->id ;
219+ $ logaction ->asset_type = 'software ' ;
220+ $ logaction ->user_id = Sentry::getUser ()->id ;
221+ $ logaction ->note = abs ($ difference )." seats " ;
222+ $ log = $ logaction ->logaction ('delete seats ' );
223+ } else {
224+ // Redirect to the license edit page
225+ return Redirect::to ("admin/licenses/ $ licenseId/edit " )->with ('error ' , Lang::get ('admin/licenses/message.assoc_users ' ));
226+ }
227+ } else {
228+
229+ for ($ i =1 ; $ i <= $ difference ; $ i ++) {
230+ //Create a seat for this license
231+ $ license_seat = new LicenseSeat ();
232+ $ license_seat ->license_id = $ license ->id ;
233+ $ license_seat ->user_id = Sentry::getId ();
234+ $ license_seat ->assigned_to = 0 ;
235+ $ license_seat ->notes = NULL ;
236+ $ license_seat ->save ();
237+ }
238+
239+ //Log the addition of license to the log.
240+ $ logaction = new Actionlog ();
241+ $ logaction ->asset_id = $ license ->id ;
242+ $ logaction ->asset_type = 'software ' ;
243+ $ logaction ->user_id = Sentry::getUser ()->id ;
244+ $ logaction ->note = abs ($ difference )." seats " ;
245+ $ log = $ logaction ->logaction ('add seats ' );
246+ }
247+ $ license ->seats = e (Input::get ('seats ' ));
248+ }
249+
194250 // Was the asset created?
195251 if ($ license ->save ())
196252 {
0 commit comments