Codestyle & bug fixes

This commit is contained in:
Dmitrii Minaev
2019-02-28 01:37:58 +03:00
parent d3c320e56f
commit 16bb784e78
10 changed files with 129 additions and 254 deletions
+101 -87
View File
@@ -25,27 +25,28 @@ class PredefinedKitCheckoutService
* @param User $user checkout target
* @return array Empty array if all ok, else [string_error1, string_error2...]
*/
public function checkout(Request $request, PredefinedKit $kit, User $user) {
public function checkout(Request $request, PredefinedKit $kit, User $user)
{
try {
// Check if the user exists
if (is_null($user) ) {
if (is_null($user)) {
return [trans('admin/users/message.user_not_found')];
}
$errors = [];
$assets_to_add = $this->getAssetsToAdd($kit, $user, $errors);
$license_seats_to_add = $this->getLicenseSeatsToAdd($kit, $user, $errors);
$consumables_to_add = $this->getConsumablesToAdd($kit, $user, $errors);
$accessories_to_add = $this->getAccessoriesToAdd($kit, $user, $errors);
$license_seats_to_add = $this->getLicenseSeatsToAdd($kit, $errors);
$consumables_to_add = $this->getConsumablesToAdd($kit, $errors);
$accessories_to_add = $this->getAccessoriesToAdd($kit, $errors);
if( count($errors) > 0 ) {
if (count($errors) > 0) {
return $errors;
}
$checkout_at = date("Y-m-d H:i:s");
if (($request->filled('checkout_at')) && ($request->get('checkout_at')!= date("Y-m-d"))) {
if (($request->filled('checkout_at')) && ($request->get('checkout_at') != date("Y-m-d"))) {
$checkout_at = $request->get('checkout_at');
}
@@ -58,7 +59,93 @@ class PredefinedKitCheckoutService
$note = e($request->get('note'));
$errors = DB::transaction(
$errors = $this->saveToDb($user, $admin, $checkout_at, $expected_checkin, $errors, $assets_to_add, $license_seats_to_add, $consumables_to_add, $accessories_to_add, $note);
return $errors;
} catch (ModelNotFoundException $e) {
return [$e->getMessage()];
} catch (CheckoutNotAllowed $e) {
return [$e->getMessage()];
}
}
protected function getAssetsToAdd($kit, $user, &$errors)
{
$models = $kit->models()
->with(['assets' => function ($hasMany) {
$hasMany->RTD();
}])
->get();
$assets_to_add = [];
foreach ($models as $model) {
$assets = $model->assets;
$quantity = $model->pivot->quantity;
foreach ($assets as $asset) {
if (
$asset->availableForCheckout()
&& !$asset->is($user)
) {
$this->authorize('checkout', $asset);
$quantity -= 1;
$assets_to_add[] = $asset;
if ($quantity <= 0) {
break;
}
}
}
if ($quantity > 0) {
$errors[] = "Don't have available assets for model " . $model->name . '. Need ' . $model->pivot->quantity . ' assets.'; // TODO: trans
}
}
return $assets_to_add;
}
protected function getLicenseSeatsToAdd($kit, &$errors)
{
$seats_to_add = [];
$licenses = $kit->licenses()
->with('freeSeats')
->get();
foreach ($licenses as $license) {
$quantity = $license->pivot->quantity;
if ($quantity > count($license->freeSeats)) {
$errors[] = "Don't have free seats for license " . $license->name . '. Need ' . $quantity . ' seats.'; // TODO: trans
}
for ($i = 0; $i < $quantity; $i++) {
$seats_to_add[] = $license->freeSeats[$i];
}
}
return $seats_to_add;
}
protected function getConsumablesToAdd($kit, &$errors)
{
// $consumables = $kit->consumables()->withCount('consumableAssignments as consumable_assignments_count')->get();
$consumables = $kit->consumables()->with('users')->get();
foreach ($consumables as $consumable) {
if ($consumable->numRemaining() < $consumable->pivot->quantity) {
$errors[] = "Don't have available consumable " . $consumable->name . '. Need ' . $consumable->pivot->quantity; // TODO: trans
}
}
return $consumables;
}
protected function getAccessoriesToAdd($kit, &$errors)
{
$accessories = $kit->accessories()->with('users')->get();
foreach ($accessories as $accossory) {
if ($accossory->numRemaining() < $accossory->pivot->quantity) {
$errors[] = "Don't have available accossory " . $accossory->name . '. Need ' . $accossory->pivot->quantity; // TODO: trans
}
}
return $accessories;
}
protected function saveToDb($user, $admin, $checkout_at, $expected_checkin, $errors, $assets_to_add, $license_seats_to_add, $consumables_to_add, $accessories_to_add, $note)
{
$errors = DB::transaction(
function () use ($user, $admin, $checkout_at, $expected_checkin, $errors, $assets_to_add, $license_seats_to_add, $consumables_to_add, $accessories_to_add, $note) {
// assets
foreach ($assets_to_add as $asset) {
@@ -74,13 +161,12 @@ class PredefinedKitCheckoutService
$licenseSeat->assigned_to = $user->id;
if ($licenseSeat->save()) {
event(new CheckoutableCheckedOut($licenseSeat, $user, $admin, $note));
}
else {
$errors []= 'Something went wrong saving a license seat';
} else {
$errors[] = 'Something went wrong saving a license seat';
}
}
// consumables
foreach($consumables_to_add as $consumable) {
foreach ($consumables_to_add as $consumable) {
$consumable->assigned_to = $user->id;
$consumable->users()->attach($consumable->id, [
'consumable_id' => $consumable->id,
@@ -90,7 +176,7 @@ class PredefinedKitCheckoutService
event(new CheckoutableCheckedOut($consumable, $user, $admin, $note));
}
//accessories
foreach($accessories_to_add as $accessory) {
foreach ($accessories_to_add as $accessory) {
$accessory->assigned_to = $user->id;
$accessory->users()->attach($accessory->id, [
'accessory_id' => $accessory->id,
@@ -100,81 +186,9 @@ class PredefinedKitCheckoutService
event(new CheckoutableCheckedOut($accessory, $user, $admin, $note));
}
return $errors;
});
return $errors;
} catch (ModelNotFoundException $e) {
return [$e->getMessage()];
} catch (CheckoutNotAllowed $e) {
return [$e->getMessage()];
}
}
protected function getAssetsToAdd($kit, $user, &$errors) {
$models = $kit->models()
->with( ['assets' => function($hasMany) { $hasMany->RTD(); }] )
->get();
$assets_to_add = [];
foreach($models as $model) {
$assets = $model->assets;
$quantity = $model->pivot->quantity;
foreach($assets as $asset) {
if ($asset->availableForCheckout()
&& !$asset->is($user)) {
$this->authorize('checkout', $asset);
$quantity -= 1;
$assets_to_add []= $asset;
if($quantity <= 0) {
break;
}
}
}
if($quantity > 0) {
$errors []= "Don't have available assets for model " . $model->name . '. Need ' . $model->pivot->quantity . ' assets.'; // TODO: trans
}
}
);
return $assets_to_add;
}
protected function getLicenseSeatsToAdd($kit, $user, &$errors) {
$seats_to_add = [];
$licenses = $kit->licenses()
->with('freeSeats')
->get();
foreach($licenses as $license) {
$quantity = $license->pivot->quantity;
if( $quantity > count($license->freeSeats) ) {
$errors []= "Don't have free seats for license " . $license->name . '. Need ' . $quantity . ' seats.'; // TODO: trans
}
for($i=0; $i < $quantity; $i++) {
$seats_to_add []= $license->freeSeats[$i];
}
}
return $seats_to_add;
}
protected function getConsumablesToAdd($kit, $user, &$errors) {
// $consumables = $kit->consumables()->withCount('consumableAssignments as consumable_assignments_count')->get();
$consumables = $kit->consumables()->with('users')->get();
foreach($consumables as $consumable) {
if( $consumable->numRemaining() < $consumable->pivot->quantity ) {
$errors []= "Don't have available consumable " . $consumable->name . '. Need ' . $consumable->pivot->quantity; // TODO: trans
}
}
return $consumables;
}
protected function getAccessoriesToAdd($kit, $user, &$errors) {
$accessories = $kit->accessories()->with('users')->get();
foreach($accessories as $accossory) {
if( $accossory->numRemaining() < $accossory->pivot->quantity ) {
$errors []= "Don't have available accossory " . $accossory->name . '. Need ' . $accossory->pivot->quantity; // TODO: trans
}
}
return $accessories;
return $errors;
}
}