Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
snipe committed Jun 30, 2021
2 parents f504d7e + 868419b commit b46e2b5
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 57,147 deletions.
2 changes: 1 addition & 1 deletion ansible/ubuntu/vagrant_playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@
- { regexp: '^DB_PASSWORD=', line: 'DB_PASSWORD=vagrant' }
- { regexp: '^APP_URL=', line: "APP_URL=http://{{ fqdn }}" }
- { regexp: '^APP_ENV=', line: "APP_ENV=development" }
- { regexp: '^APP_DEBUG=', line: "APP_ENV=true" }
- { regexp: '^APP_DEBUG=', line: "APP_DEBUG=true" }
- name: Generate application key
shell: "php {{ app_path }}/artisan key:generate --force"
- name: Artisan Migrate
Expand Down
118 changes: 118 additions & 0 deletions app/Http/Controllers/Api/ComponentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use App\Models\Company;
use App\Models\Component;
use Illuminate\Http\Request;
use App\Events\CheckoutableCheckedIn;
use App\Events\ComponentCheckedIn;
use App\Models\Asset;

class ComponentsController extends Controller
{
Expand Down Expand Up @@ -172,4 +175,119 @@ public function getAssets(Request $request, $id)
$assets = $assets->skip($offset)->take($limit)->get();
return (new ComponentsTransformer)->transformCheckedoutComponents($assets, $total);
}


/**
* Validate and checkout the component.
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* t
* @since [v5.1.8]
* @param Request $request
* @param int $componentId
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function checkout(Request $request, $componentId)
{
// Check if the component exists
if (is_null($component = Component::find($componentId))) {
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/components/message.does_not_exist')));
}

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


if ($component->numRemaining() > $request->get('assigned_qty')) {

if (!$asset = Asset::find($request->input('assigned_to'))) {
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.does_not_exist')));
}

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

$component->assets()->attach($component->id, [
'component_id' => $component->id,
'created_at' => \Carbon::now(),
'assigned_qty' => $request->get('assigned_qty'),
'user_id' => \Auth::id(),
'asset_id' => $request->get('assigned_to')
]);

$component->logCheckout($request->input('note'), $asset);

return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/components/message.checkout.success')));
}

return response()->json(Helper::formatStandardApiResponse('error', null, 'Not enough components remaining: '.$component->numRemaining().' remaining, '.$request->get('assigned_qty').' requested.'));
}

/**
* Validate and store checkin data.
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v5.1.8]
* @param Request $request
* @param $component_asset_id
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function checkin(Request $request, $component_asset_id)
{
if ($component_assets = \DB::table('components_assets')->find($component_asset_id)) {

if (is_null($component = Component::find($component_assets->component_id))) {

return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/components/message.not_found')));
}

$this->authorize('checkin', $component);

$max_to_checkin = $component_assets->assigned_qty;

if ($max_to_checkin > 1) {

$validator = \Validator::make($request->all(), [
"checkin_qty" => "required|numeric|between:1,$max_to_checkin"
]);

if ($validator->fails()) {
return response()->json(Helper::formatStandardApiResponse('error', null, 'Checkin quantity must be between 1 and '.$max_to_checkin));
}
}


// Validation passed, so let's figure out what we have to do here.
$qty_remaining_in_checkout = ($component_assets->assigned_qty - (int)$request->input('checkin_qty', 1));

// We have to modify the record to reflect the new qty that's
// actually checked out.
$component_assets->assigned_qty = $qty_remaining_in_checkout;

\Log::debug($component_asset_id.' - '.$qty_remaining_in_checkout.' remaining in record '.$component_assets->id);

\DB::table('components_assets')->where('id',
$component_asset_id)->update(['assigned_qty' => $qty_remaining_in_checkout]);

// If the checked-in qty is exactly the same as the assigned_qty,
// we can simply delete the associated components_assets record
if ($qty_remaining_in_checkout == 0) {
\DB::table('components_assets')->where('id', '=', $component_asset_id)->delete();
}


$asset = Asset::find($component_assets->asset_id);

event(new CheckoutableCheckedIn($component, $asset, \Auth::user(), $request->input('note'), \Carbon::now()));

return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/components/message.checkin.success')));

}

return response()->json(Helper::formatStandardApiResponse('error', null, 'No matching checkouts for that component join record'));


}

}
Loading

0 comments on commit b46e2b5

Please sign in to comment.