More refactoring

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2024-07-18 01:46:53 +01:00
parent 79a13e3618
commit f56006fb6b
2 changed files with 29 additions and 16 deletions

View file

@ -3,8 +3,6 @@
namespace App\Http\Requests;
use App\Models\Accessory;
use App\Models\Category;
use App\Models\User;
use Illuminate\Support\Facades\Gate;
class AccessoryCheckoutRequest extends ImageUploadRequest
@ -23,10 +21,21 @@ class AccessoryCheckoutRequest extends ImageUploadRequest
if ($this->accessory) {
$this->diff = ($this->accessory->numRemaining() - $this->checkout_qty);
\Log::debug('num remaining in form request: '.$this->accessory->numRemaining());
\Log::debug('accessory qty in form request: '.$this->accessory->qty);
\Log::debug('checkout qty in form request: '.$this->checkout_qty);
\Log::debug('diff in form request: '.$this->diff);
$this->merge([
'checkout_qty' => (int) $this->checkout_qty ?? 1,
'number_remaining_after_checkout' => (int) ($this->accessory->numRemaining() - $this->checkout_qty) ?? 0,
'checkout_qty' => $this->checkout_qty,
'number_remaining_after_checkout' => ($this->accessory->numRemaining() - $this->checkout_qty),
'number_currently_remaining' => $this->accessory->numRemaining(),
'checkout_difference' => $this->diff,
]);
\Log::debug('---------------------------------------------');
}
}
@ -47,14 +56,18 @@ class AccessoryCheckoutRequest extends ImageUploadRequest
'exists:users,id,deleted_at,NULL',
'not_array'
],
'number_remaining_after_checkout' => [
//'gte:checkout_qty',
'min:0',
'required',
'integer',
'min:0',
],
'checkout_qty' => [
'lte:number_remaining_after_checkout',
'integer',
'lte:qty',
'lte:number_currently_remaining',
'min:1',
],
],
);
@ -62,7 +75,7 @@ class AccessoryCheckoutRequest extends ImageUploadRequest
public function messages(): array
{
$messages = ['checkout_qty.lte' => 'There are only '.$this->accessory->qty.'/'.$this->number_remaining_after_checkout.' accessories remaining, trying to check out '.$this->checkout_qty];
$messages = ['checkout_qty.lte' => 'There are only '.$this->accessory->qty.' available accessories, and you are trying to check out '.$this->checkout_qty.', leaving '.$this->number_remaining_after_checkout.' ('.$this->number_currently_remaining.') accessories remaining ('.$this->diff.').'];
return $messages;
}

View file

@ -63,7 +63,7 @@ class Accessory extends SnipeModel
'company_id' => 'integer|nullable',
'min_amt' => 'integer|min:0|nullable',
'purchase_cost' => 'numeric|nullable|gte:0',
'purchase_date' => 'date_format:Y-m-d|nullable',
'purchase_date' => 'date_format:Y-m-d|nullable',
];
@ -338,8 +338,7 @@ class Accessory extends SnipeModel
*/
public function numCheckedOut()
{
\Log::debug('numCheckedOut: '.$this->users_count ?? $this->users->count());
return $this->users_count ?? $this->users->count();
return (int) $this->users_count ?? $this->users->count();
}
@ -347,7 +346,7 @@ class Accessory extends SnipeModel
* Check how many items of an accessory remain.
*
* In order to use this model method, you MUST call withCount('users as users_count')
* on the eloquent query in the controller, otherwise $this->>users_count will be null and
* on the eloquent query in the controller, otherwise $this->users_count will be null and
* bad things happen.
*
* @author [A. Gianotto] [<snipe@snipe.net>]
@ -357,14 +356,15 @@ class Accessory extends SnipeModel
public function numRemaining()
{
$checkedout = $this->numCheckedOut();
$total = $this->qty;
$remaining = $total - $checkedout;
\Log::debug('checked out: '.$checkedout);
$total = $this->qty;
\Log::debug('total: '.$total);
$remaining = $total - $checkedout;
\Log::debug('remaining: '.$remaining);
return (int) $remaining;
return $remaining;
}
/**