From c4f900e9af8df9bf8dfc7b5e003b8fc7d7876b2e Mon Sep 17 00:00:00 2001 From: Brady Wetherington Date: Wed, 4 Jan 2023 13:02:53 -0800 Subject: [PATCH] Change license_seat changing method to properly 'true up' license seats --- app/Models/License.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/app/Models/License.php b/app/Models/License.php index d0e6f5c969..b59387a42e 100755 --- a/app/Models/License.php +++ b/app/Models/License.php @@ -123,12 +123,20 @@ class License extends Depreciable static::created(function ($license) { $newSeatCount = $license->getAttributes()['seats']; - return static::adjustSeatCount($license, $oldSeatCount = 0, $newSeatCount); + return static::adjustSeatCount($license, 0, $newSeatCount); }); // However, we listen for updating to be able to prevent the edit if we cannot delete enough seats. static::updating(function ($license) { $newSeatCount = $license->getAttributes()['seats']; - $oldSeatCount = isset($license->getOriginal()['seats']) ? $license->getOriginal()['seats'] : 0; + //$oldSeatCount = isset($license->getOriginal()['seats']) ? $license->getOriginal()['seats'] : 0; + /* + That previous method *did* mostly work, but if you ever managed to get your $license->seats value out of whack + with your actual count of license_seats *records*, you would never manage to get back 'into whack'. + The below method actually grabs a count of existing license_seats records, so it will be more accurate. + This means that if your license_seats are out of whack, you can change the quantity and hit 'save' and it + will manage to 'true up' and make your counts line up correctly. + */ + $oldSeatCount = $license->license_seats_count; return static::adjustSeatCount($license, $oldSeatCount, $newSeatCount); });