2021-11-06 00:56:57 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Models\Egg;
|
2021-11-07 15:29:18 +00:00
|
|
|
use App\Models\Location;
|
2021-11-06 00:56:57 +00:00
|
|
|
use App\Models\Node;
|
|
|
|
use App\Models\Product;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
|
|
|
|
class ProductController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @description get product locations based on selected egg
|
|
|
|
* @param Request $request
|
|
|
|
* @param Egg $egg
|
|
|
|
* @return Collection|JsonResponse
|
|
|
|
*/
|
|
|
|
public function getNodesBasedOnEgg(Request $request, Egg $egg)
|
|
|
|
{
|
2021-11-07 17:01:23 +00:00
|
|
|
if (is_null($egg->id)) return response()->json('Egg ID is required', '400');
|
2021-11-06 00:56:57 +00:00
|
|
|
|
|
|
|
#get products that include this egg
|
|
|
|
$products = Product::query()->with('nodes')->whereHas('eggs', function (Builder $builder) use ($egg) {
|
|
|
|
$builder->where('id', '=', $egg->id);
|
|
|
|
})->get();
|
|
|
|
|
|
|
|
$nodes = collect();
|
|
|
|
|
|
|
|
#filter unique nodes
|
|
|
|
$products->each(function (Product $product) use ($nodes) {
|
|
|
|
$product->nodes->each(function (Node $node) use ($nodes) {
|
|
|
|
if (!$nodes->contains('id', $node->id) && !$node->disabled) {
|
|
|
|
$nodes->add($node);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return $nodes;
|
|
|
|
}
|
|
|
|
|
2021-11-07 15:29:18 +00:00
|
|
|
/**
|
|
|
|
* @description get product locations based on selected egg
|
|
|
|
* @param Request $request
|
|
|
|
* @param Egg $egg
|
|
|
|
* @return Collection|JsonResponse
|
|
|
|
*/
|
|
|
|
public function getLocationsBasedOnEgg(Request $request, Egg $egg)
|
|
|
|
{
|
|
|
|
$nodes = $this->getNodesBasedOnEgg($request, $egg);
|
|
|
|
$locations = collect();
|
|
|
|
|
|
|
|
//locations
|
|
|
|
$nodes->each(function (Node $node) use ($nodes, $locations) {
|
|
|
|
/** @var Location $location */
|
|
|
|
$location = $node->location;
|
|
|
|
|
2021-11-11 21:05:42 +00:00
|
|
|
if (!$locations->contains('id', $location->id)) {
|
|
|
|
$nodeIds = $nodes->map(function ($node) {
|
2021-11-07 15:29:18 +00:00
|
|
|
return $node->id;
|
|
|
|
});
|
|
|
|
|
|
|
|
$location->nodes = $location->nodes()
|
2021-11-11 21:05:42 +00:00
|
|
|
->whereIn('id', $nodeIds)
|
2021-11-07 15:29:18 +00:00
|
|
|
->get();
|
|
|
|
|
|
|
|
$locations->add($location);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return $locations;
|
|
|
|
}
|
|
|
|
|
2021-11-06 00:56:57 +00:00
|
|
|
/**
|
|
|
|
* @param Node $node
|
2021-11-11 21:05:42 +00:00
|
|
|
* @param Egg $egg
|
2021-11-06 00:56:57 +00:00
|
|
|
* @return Collection|JsonResponse
|
|
|
|
*/
|
2021-11-11 21:05:42 +00:00
|
|
|
public function getProductsBasedOnNode(Egg $egg, Node $node)
|
2021-11-06 00:56:57 +00:00
|
|
|
{
|
2021-11-11 21:05:42 +00:00
|
|
|
if (is_null($egg->id) || is_null($node->id)) return response()->json('node and egg id is required', '400');
|
2021-11-06 00:56:57 +00:00
|
|
|
|
2021-11-11 21:05:42 +00:00
|
|
|
return Product::query()
|
|
|
|
->whereHas('nodes', function (Builder $builder) use ($node) {
|
|
|
|
$builder->where('id', '=', $node->id);
|
|
|
|
})
|
|
|
|
->whereHas('eggs', function (Builder $builder) use ($egg) {
|
|
|
|
$builder->where('id', '=', $egg->id);
|
|
|
|
})
|
|
|
|
->get();
|
2021-11-06 00:56:57 +00:00
|
|
|
}
|
|
|
|
}
|