diff --git a/src/AppBundle/Service/SetService.php b/src/AppBundle/Service/SetService.php index a7f3ddf..285e763 100644 --- a/src/AppBundle/Service/SetService.php +++ b/src/AppBundle/Service/SetService.php @@ -1,11 +1,11 @@ inventoryPartRepository = $inventoryPartRepository; } - - public function getUniqueModelCount(Set $set) { - - } - - public function getModels(Set $set) + /** + * Get array of all known models in the kit(set) with quantity. Ignores colors and groups parts with the same shape. + * [ + * modelNumber => [ + * 'model' => Model, + * 'quantity => int + * ] + * ... + * ]. + * + * @param Set $set + * @param bool $spare If true - add only spare parts, false - add only regular parts, null - add all parts + * + * @return array + */ + public function getModels(Set $set, $spare = null) { $models = []; @@ -34,27 +45,43 @@ /** @var Inventory_Part $inventoryPart */ foreach ($inventoryParts as $inventoryPart) { - $model = $inventoryPart->getPart()->getModel(); - $color = $inventoryPart->getColor(); - if($model) { - $models[$model->getNumber()]['model'] = $model; - - $quantity = 0; - if(isset($models[$model->getNumber()]['colors'][$color->getId()]['quantity'])) { - $quantity = $models[$model->getNumber()]['colors'][$color->getId()]['quantity']; + if ($model = $inventoryPart->getPart()->getModel()) { + if (isset($models[$model->getNumber()])) { + $models[$model->getNumber()]['quantity'] += $inventoryPart->getQuantity(); + } else { + $models[$model->getNumber()] = [ + 'model' => $model, + 'quantity' => $inventoryPart->getQuantity(), + ]; } - - $models[$model->getNumber()]['colors'][$color->getId()] = [ - 'color' => $color, - 'quantity' => $quantity+$inventoryPart->getQuantity() - ]; } } return $models; } - public function getSpareModels(Set $set) + /** + * Get array of all known models in the kit(set). + * [ + * modelNumber => [ + * 'model' => Model, + * 'colors => [ + * colorID => [ + * 'color' => Color, + * 'quantity => int + * ] + * ... + * ] + * ] + * ... + * ]. + * + * @param Set $set + * @param bool $spare If true - add only spare parts, false - add only regular parts, null - add all parts + * + * @return array + */ + public function getModelsWithColors(Set $set, $spare = null) { $models = []; @@ -62,23 +89,78 @@ /** @var Inventory_Part $inventoryPart */ foreach ($inventoryParts as $inventoryPart) { - $model = $inventoryPart->getPart()->getModel(); - $color = $inventoryPart->getColor(); - if($model) { - $models[$model->getNumber()]['model'] = $model; + if ($model = $inventoryPart->getPart()->getModel()) { + $color = $inventoryPart->getColor(); - $quantity = 0; - if(isset($models[$model->getNumber()]['colors'][$color->getId()]['quantity'])) { - $quantity = $models[$model->getNumber()]['colors'][$color->getId()]['quantity']; + if (!isset($models[$model->getNumber()]['model'])) { + $models[$model->getNumber()]['model'] = $model; } - $models[$model->getNumber()]['colors'][$color->getId()] = [ - 'color' => $color, - 'quantity' => $quantity+$inventoryPart->getQuantity() - ]; + if (isset($models[$model->getNumber()]['colors'][$color->getId()])) { + $models[$model->getNumber()]['colors'][$color->getId()]['quantity'] += $inventoryPart->getQuantity(); + } else { + $models[$model->getNumber()]['colors'][$color->getId()] = [ + 'color' => $color, + 'quantity' => $inventoryPart->getQuantity(), + ]; + } } } return $models; } - } \ No newline at end of file + + + /** + * Get array models grouped by color. + * [ + * 'colorID' => [ + * 'color' => Color, + * 'models => [ + * modelNumber => [ + * 'model' => Model, + * 'quantity' => int + * ] + * ... + * ] + * ] + * ... + * ]. + * + * @param Set $set + * @param bool $spare If true - add only spare parts, false - add only regular parts, null - add all parts + * + * @return array + */ + public function getModelsGroupedByColor(Set $set, $spare = null) + { + $colors = []; + + $inventoryParts = $this->inventoryPartRepository->findAllBySetNumber($set->getNumber(), $spare, true); + + /** @var Inventory_Part $inventoryPart */ + foreach ($inventoryParts as $inventoryPart) { + if ($model = $inventoryPart->getPart()->getModel()) { + $color = $inventoryPart->getCOlor(); + + if (!isset($colors[$color->getId()]['color'])) { + $colors[$color->getId()]['color'] = $color; + $colors[$color->getId()]['quantity'] = 0; + } + + $colors[$color->getId()]['quantity'] += $inventoryPart->getQuantity(); + + if (isset($colors[$color->getId()]['models'][$model->getNumber()])) { + $colors[$color->getId()]['models'][$model->getNumber()]['quantity'] += $inventoryPart->getQuantity(); + } else { + $colors[$color->getId()]['models'][$model->getNumber()] = [ + 'model' => $model, + 'quantity' => $inventoryPart->getQuantity(), + ]; + } + } + } + + return $colors; + } + }