Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds quantity of accessories and consumables to checkout. #14342

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/Events/CheckoutableCheckedOut.php
Expand Up @@ -12,6 +12,7 @@ class CheckoutableCheckedOut

public $checkoutable;
public $checkedOutTo;
public $quantity;
public $checkedOutBy;
public $note;
public $originalValues;
Expand All @@ -21,11 +22,12 @@ class CheckoutableCheckedOut
*
* @return void
*/
public function __construct($checkoutable, $checkedOutTo, User $checkedOutBy, $note, $originalValues = [])
public function __construct($checkoutable, $checkedOutTo, User $checkedOutBy, $note, $originalValues = [], $quantity=null)
{
$this->checkoutable = $checkoutable;
$this->checkedOutTo = $checkedOutTo;
$this->checkedOutBy = $checkedOutBy;
$this->quantity = $quantity;
$this->note = $note;
$this->originalValues = $originalValues;
}
Expand Down
30 changes: 18 additions & 12 deletions app/Http/Controllers/Accessories/AccessoryCheckoutController.php
Expand Up @@ -72,30 +72,36 @@ public function store(Request $request, $accessoryId)

$this->authorize('checkout', $accessory);


if (!$user = User::find($request->input('assigned_to'))) {
return redirect()->route('accessories.checkout.show', $accessory->id)->with('error', trans('admin/accessories/message.checkout.user_does_not_exist'));
return redirect()->route('accessories.checkout.show', $accessory->id)->with('error', trans('admin/accessories/message.checkout.user_does_not_exist'))->withInput();
}

$validated = $request->validate([
'assigned_qty' => 'required|numeric|min:1',
]);
$quantity =$request->input('assigned_qty');
// Make sure there is at least one available to checkout
if ($accessory->numRemaining() <= 0){

if ($accessory->numRemaining() <= 0 || $accessory->numRemaining() < $quantity) {
return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.checkout.unavailable'));
}


// Update the accessory data
$accessory->assigned_to = e($request->input('assigned_to'));

$accessory->users()->attach($accessory->id, [
'accessory_id' => $accessory->id,
'created_at' => Carbon::now(),
'user_id' => Auth::id(),
'assigned_to' => $request->get('assigned_to'),
'note' => $request->input('note'),
]);
for ($qty = 0; $qty < $quantity; $qty++) {
$accessory->users()->attach($accessory->id, [
'accessory_id' => $accessory->id,
'created_at' => Carbon::now(),
'user_id' => Auth::id(),
'assigned_to' => $request->get('assigned_to'),
'note' => $request->input('note'),
]);
}
event(new CheckoutableCheckedOut($accessory, $user, Auth::user(), $request->input('note'), [], $quantity ));

DB::table('accessories_users')->where('assigned_to', '=', $accessory->assigned_to)->where('accessory_id', '=', $accessory->id)->first();

event(new CheckoutableCheckedOut($accessory, $user, Auth::user(), $request->input('note')));

// Redirect to the new accessory page
return redirect()->route('accessories.index')->with('success', trans('admin/accessories/message.checkout.success'));
Expand Down
38 changes: 22 additions & 16 deletions app/Http/Controllers/Consumables/ConsumableCheckoutController.php
Expand Up @@ -65,37 +65,43 @@ public function create($id)
*/
public function store(Request $request, $consumableId)
{

if (is_null($consumable = Consumable::with('users')->find($consumableId))) {
return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.not_found'));
}

$this->authorize('checkout', $consumable);

// Check if the user exists
$assigned_to = e($request->input('assigned_to'));
if (is_null($user = User::find($assigned_to))) {
// Redirect to the consumable management page with error
return redirect()->route('consumables.checkout.show', $consumable)->with('error', trans('admin/consumables/message.checkout.user_does_not_exist'))->withInput();
}
$validated = $request->validate([
'assigned_qty' => 'required|numeric|min:1',
]);
$quantity = $request->input('assigned_qty');
Godmartinz marked this conversation as resolved.
Show resolved Hide resolved
// Make sure there is at least one available to checkout
if ($consumable->numRemaining() <= 0) {
if ($consumable->numRemaining() <= 0 || $consumable->numRemaining() < $quantity) {
return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.checkout.unavailable'));
}

$admin_user = Auth::user();
$assigned_to = e($request->input('assigned_to'));

// Check if the user exists
if (is_null($user = User::find($assigned_to))) {
// Redirect to the consumable management page with error
return redirect()->route('consumables.checkout.show', $consumable)->with('error', trans('admin/consumables/message.checkout.user_does_not_exist'))->withInput();
}

// Update the consumable data
$consumable->assigned_to = e($request->input('assigned_to'));

$consumable->users()->attach($consumable->id, [
'consumable_id' => $consumable->id,
'user_id' => $admin_user->id,
'assigned_to' => e($request->input('assigned_to')),
'note' => $request->input('note'),
]);

event(new CheckoutableCheckedOut($consumable, $user, Auth::user(), $request->input('note')));
// Update the consumable data
for ($qty = 0; $qty < $quantity; $qty++) {
$consumable->users()->attach($consumable->id, [
'consumable_id' => $consumable->id,
'user_id' => $admin_user->id,
'assigned_to' => e($request->input('assigned_to')),
'note' => $request->input('note'),
]);
}
event(new CheckoutableCheckedOut($consumable, $user, Auth::user(), $request->input('note'), [], $quantity));

// Redirect to the new consumable page
return redirect()->route('consumables.index')->with('success', trans('admin/consumables/message.checkout.success'));
Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/CheckoutableListener.php
Expand Up @@ -231,7 +231,7 @@ private function getCheckoutNotification($event, $acceptance = null)
}


return new $notificationClass($event->checkoutable, $event->checkedOutTo, $event->checkedOutBy, $acceptance, $event->note);
return new $notificationClass($event->checkoutable, $event->checkedOutTo, $event->checkedOutBy, $acceptance, $event->note, $event->quantity);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion app/Notifications/CheckoutAccessoryNotification.php
Expand Up @@ -24,11 +24,12 @@ class CheckoutAccessoryNotification extends Notification
/**
* Create a new notification instance.
*/
public function __construct(Accessory $accessory, $checkedOutTo, User $checkedOutBy, $acceptance, $note)
public function __construct(Accessory $accessory, $checkedOutTo, User $checkedOutBy, $acceptance, $note, $quantity)
{
$this->item = $accessory;
$this->admin = $checkedOutBy;
$this->note = $note;
$this->quantity = $quantity;
$this->target = $checkedOutTo;
$this->acceptance = $acceptance;
$this->settings = Setting::getSettings();
Expand Down Expand Up @@ -180,6 +181,7 @@ public function toMail()
'admin' => $this->admin,
'note' => $this->note,
'target' => $this->target,
'quantity' => $this->quantity,
'eula' => $eula,
'req_accept' => $req_accept,
'accept_url' => $accept_url,
Expand Down
4 changes: 3 additions & 1 deletion app/Notifications/CheckoutConsumableNotification.php
Expand Up @@ -30,11 +30,12 @@ class CheckoutConsumableNotification extends Notification
*
* @param $params
*/
public function __construct(Consumable $consumable, $checkedOutTo, User $checkedOutBy, $acceptance, $note)
public function __construct(Consumable $consumable, $checkedOutTo, User $checkedOutBy, $acceptance, $note, $quantity)
{
$this->item = $consumable;
$this->admin = $checkedOutBy;
$this->note = $note;
$this->quantity = $quantity;
$this->target = $checkedOutTo;
$this->acceptance = $acceptance;

Expand Down Expand Up @@ -183,6 +184,7 @@ public function toMail()
'item' => $this->item,
'admin' => $this->admin,
'note' => $this->note,
'quantity' => $this->quantity,
'target' => $this->target,
'eula' => $eula,
'req_accept' => $req_accept,
Expand Down
13 changes: 13 additions & 0 deletions resources/views/accessories/checkout.blade.php
Expand Up @@ -53,6 +53,19 @@

@include ('partials.forms.edit.user-select', ['translated_name' => trans('general.select_user'), 'fieldname' => 'assigned_to'])

<div class="form-group {{ $errors->has('assigned_qty') ? ' has-error' : '' }}">
<label for="assigned_qty" class="col-md-3 control-label">
{{ trans('general.qty') }}
</label>
<div class="col-md-2 col-sm-5 col-xs-5">
<input class="form-control required col-md-12" type="number" min="1" name="assigned_qty" id="assigned_qty" value="{{ old('assigned_qty') ?? 1 }}" />
</div>
@if ($errors->first('assigned_qty'))
<div class="col-md-9 col-md-offset-3">
{!! $errors->first('assigned_qty', '<span class="alert-msg" aria-hidden="true"><i class="fas fa-times" aria-hidden="true"></i> :message</span>') !!}
</div>
@endif
</div>

@if ($accessory->requireAcceptance() || $accessory->getEula() || ($snipeSettings->webhook_endpoint!=''))
<div class="form-group notification-callout">
Expand Down
13 changes: 13 additions & 0 deletions resources/views/consumables/checkout.blade.php
Expand Up @@ -40,6 +40,19 @@
<!-- User -->
@include ('partials.forms.edit.user-select', ['translated_name' => trans('general.select_user'), 'fieldname' => 'assigned_to', 'required'=> 'true'])

<div class="form-group {{ $errors->has('assigned_qty') ? ' has-error' : '' }}">
<label for="assigned_qty" class="col-md-3 control-label">
{{ trans('general.qty') }}
</label>
<div class="col-md-2 col-sm-5 col-xs-5">
<input class="form-control required col-md-12" type="number" min="1" name="assigned_qty" id="assigned_qty" value="{{ old('assigned_qty') ?? 1 }}" />
</div>
@if ($errors->first('assigned_qty'))
<div class="col-md-9 col-md-offset-3">
{!! $errors->first('assigned_qty', '<span class="alert-msg" aria-hidden="true"><i class="fas fa-times" aria-hidden="true"></i> :message</span>') !!}
</div>
@endif
</div>

@if ($consumable->requireAcceptance() || $consumable->getEula() || ($snipeSettings->webhook_endpoint!=''))
<div class="form-group notification-callout">
Expand Down
Expand Up @@ -14,6 +14,9 @@
| **{{ trans('mail.checkout_date') }}** | {{ $checkout_date }} |
@endif
| **{{ trans('general.accessory') }}** | {{ $item->name }} |
@if (isset($quantity))
| **{{ trans('general.qty') }}** | {{ $quantity }} |
@endif
@if (isset($item->manufacturer))
| **{{ trans('general.manufacturer') }}** | {{ $item->manufacturer->name }} |
@endif
Expand Down
Expand Up @@ -11,6 +11,9 @@
| **{{ trans('mail.checkout_date') }}** | {{ $checkout_date }} |
@endif
| **{{ trans('general.consumable') }}** | {{ $item->name }} |
@if (isset($quantity))
| **{{ trans('general.qty') }}** | {{ $quantity }} |
@endif
@if (isset($item->manufacturer))
| **{{ trans('general.manufacturer') }}** | {{ $item->manufacturer->name }} |
@endif
Expand Down
14 changes: 12 additions & 2 deletions tests/Feature/Checkouts/AccessoryCheckoutTest.php
Expand Up @@ -2,6 +2,7 @@

namespace Tests\Feature\Checkouts;

use App\Events\CheckoutableCheckedOut;
use App\Models\Accessory;
use App\Models\Actionlog;
use App\Models\User;
Expand Down Expand Up @@ -35,21 +36,28 @@ public function testAccessoryMustBeAvailableWhenCheckingOut()
$this->actingAs(User::factory()->checkoutAccessories()->create())
->post(route('accessories.checkout.store', Accessory::factory()->withoutItemsRemaining()->create()), [
'assigned_to' => User::factory()->create()->id,
'assigned_qty'=> 1,
])
->assertSessionHas('error');
}

public function testAccessoryCanBeCheckedOut()
{
$accessory = Accessory::factory()->create();
$accessory = Accessory::factory()->create(['qty' => 2]);
$user = User::factory()->create();

$this->actingAs(User::factory()->checkoutAccessories()->create())
->post(route('accessories.checkout.store', $accessory), [
'assigned_to' => $user->id,
'assigned_qty'=> 2,
]);

$this->assertTrue($accessory->users->contains($user));
$this->assertEquals(
2,
$accessory->users->filter(function ($u) use ($user) {
return $u->id==$user->id;
})->count()
);
}

public function testUserSentNotificationUponCheckout()
Expand All @@ -62,6 +70,7 @@ public function testUserSentNotificationUponCheckout()
$this->actingAs(User::factory()->checkoutAccessories()->create())
->post(route('accessories.checkout.store', $accessory), [
'assigned_to' => $user->id,
'assigned_qty'=> 1,
]);

Notification::assertSentTo($user, CheckoutAccessoryNotification::class);
Expand All @@ -77,6 +86,7 @@ public function testActionLogCreatedUponCheckout()
->post(route('accessories.checkout.store', $accessory), [
'assigned_to' => $user->id,
'note' => 'oh hi there',
'assigned_qty'=> 1,
]);

$this->assertEquals(
Expand Down
12 changes: 10 additions & 2 deletions tests/Feature/Checkouts/ConsumableCheckoutTest.php
Expand Up @@ -35,6 +35,7 @@ public function testConsumableMustBeAvailableWhenCheckingOut()
$this->actingAs(User::factory()->checkoutConsumables()->create())
->post(route('consumables.checkout.store', Consumable::factory()->withoutItemsRemaining()->create()), [
'assigned_to' => User::factory()->create()->id,
'assigned_qty'=> 1,
])
->assertSessionHas('error');
}
Expand All @@ -47,9 +48,14 @@ public function testConsumableCanBeCheckedOut()
$this->actingAs(User::factory()->checkoutConsumables()->create())
->post(route('consumables.checkout.store', $consumable), [
'assigned_to' => $user->id,
'assigned_qty'=> 3,
]);

$this->assertTrue($user->consumables->contains($consumable));
$this->assertEquals(
3,
$consumable->users->filter(function ($u) use ($user) {
return $u->id === $user->id;
})->count()
);
}

public function testUserSentNotificationUponCheckout()
Expand All @@ -62,6 +68,7 @@ public function testUserSentNotificationUponCheckout()
$this->actingAs(User::factory()->checkoutConsumables()->create())
->post(route('consumables.checkout.store', $consumable), [
'assigned_to' => $user->id,
'assigned_qty'=> 1,
]);

Notification::assertSentTo($user, CheckoutConsumableNotification::class);
Expand All @@ -77,6 +84,7 @@ public function testActionLogCreatedUponCheckout()
->post(route('consumables.checkout.store', $consumable), [
'assigned_to' => $user->id,
'note' => 'oh hi there',
'assigned_qty'=> 1,
]);

$this->assertEquals(
Expand Down