File: D:/HostingSpaces/Lacom/lacom.nl/app/KommaApp/Shop/Products/ProductGroup/ProductGroupService.php
<?php
namespace App\KommaApp\Shop\Products\ProductGroup;
use App\KommaApp\Kms\Core\Attributes\Attribute;
use App\KommaApp\Kms\Core\Attributes\Models\SelectOption;
use App\KommaApp\Kms\Core\Sections\AbstractSectionTabItem;
use App\KommaApp\Kms\Core\Sections\SectionService;
use App\KommaApp\Kms\Core\Sections\SectionTabItem;
use App\KommaApp\Shop\Categories\Kms\CategorizableInterface;
use App\KommaApp\Shop\Categories\Kms\CategoryServiceInterface;
use App\KommaApp\Shop\Products\Product\ProductServiceInterface;
use App\KommaApp\Shop\Products\ProductGroupBehaviour\ProductGroupBehaviorService;
use App\KommaApp\Shop\Products\ProductGroupBehaviour\ProductGroupBehaviour;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
/**
* Class ProductService
* @package App\KommaApp\Shop\Products
*/
class ProductGroupService extends SectionService implements ProductGroupServiceInterface
{
protected $sortable = false;
/** @var ProductServiceInterface $productService */
private $productService;
/** @var ProductGroupBehaviorService $productGroupBehaviourService */
private $productGroupBehaviourService;
/** @var CategoryServiceInterface $categoryService */
private $categoryService;
function __construct()
{
$this->forModelName = ProductGroup::class;
$this->productService = \App::make(ProductServiceInterface::class);
$this->productGroupBehaviourService = new ProductGroupBehaviorService();
$this->categoryService = \App::make(CategoryServiceInterface::class);
parent::__construct();
}
/**
* This method will save an model
*
* @param $model Model or null
* @param Collection $sectionTabItems These must be filled with data. This is something you need to do yourself.
*
* @return mixed
*/
public function saveModel(Model $model = null, Collection $sectionTabItems): Model
{
/** @var ProductGroup $model */
$sectionTabItems->each(function ($sectionTabItem, $key) use ($model) {
/** @var SectionTabItem $sectionTabItem */
$attribute = $sectionTabItem->getAttribute();
$reference = $attribute->getsValueFromReference();
switch ($attribute->getsValueFrom()) {
case Attribute::ValueFromModel:
if ($reference == 'product_group_behaviour_id') {
$behaviourId = $attribute->getValue();
$behaviour = ProductGroupBehaviour::find($behaviourId);
if ($behaviour && $model) {
$model->productGroupBehaviour()->associate($behaviour);
}
}
break;
case Attribute::ValueFromItself:
if($reference == 'category_id') {
if(!$model->exists) $model->save();
$this->categoryService->makeChildOfCategoriesIfNotAlready($model, $sectionTabItem->getAttribute()->getValue());
}
}
});
parent::saveModel($model, $sectionTabItems);
return $model;
}
/**
* Fills non-productGroup specific attributes. ProductGroup specific attributes are processed in the parent
*
*
* @param Collection $sectionTabItems A collection containing implementations AbstractSectionTabItem's
* @param Model $model
* @return Collection
*/
public function fillAttributesWithData(Collection $sectionTabItems, Model $model)
{
//Validate that each Collection item is an instance of AbstractSectionTabItem
$sectionTabItems->every(function ($item, $key) {
if (!is_a($item, AbstractSectionTabItem::class)) {
throw new \InvalidArgumentException("The attributes passed must be a Collection of AbstractSectionTabItem instances but was not");
}
});
$sectionTabItems->each(
function ($sectionTabItem, $key) use ($model) {
/** @var $sectionTabItem SectionTabItem */
if (!is_a($sectionTabItem->getAttribute(), Attribute::class)) {
throw new \InvalidArgumentException("One of the attributes in a AbstractSectionTabItem instance is not but must be an child instance of Attribute.");
}
$valueReference = $sectionTabItem->getAttribute()->getsValueFromReference();
if($valueReference == 'category_id')
{
/** @var CategorizableInterface $model */
$idString = $this->categoryService->getCategoryIdsForModel($model);
if($idString) $sectionTabItem->getAttribute()->setValue($idString);
}
}
);
$filledAttributes = parent::fillAttributesWithData($sectionTabItems, $model);
return $filledAttributes;
}
/**
* @return SelectOption[]
*/
public function getOptionModelsForProductSelect():array
{
$models = [];
$products = $this->productService->models();
$products->each(function ($product) use (&$models) {
$models[] = (new SelectOption())->setValue($product->id)->setContent($product->id)->setHtmlContent($product->title);
});
return $models;
}
/**
* @return SelectOption[]
*/
public function getOptionModelsForBehaviourSelect():array
{
$models = [];
$behaviours = $this->productGroupBehaviourService->models()->get();
$behaviours->each(function ($behaviour) use (&$models) {
$models[] = (new SelectOption())->setValue($behaviour->id)->setContent($behaviour->title)->setHtmlContent(__('shop/products.'.$behaviour->title));
});
return $models;
}
/**
* Return all groups that are not in composites
*
* @return Builder
*/
public function modelsNotInComposites()
{
$groups = $this->models() //Get all group models
->doesntHave('composites') //...that are not in composites
->with(['translation']); //...with all groupTranslations for the current language id of the current site
return $groups;
}
/**
* Returns the price of the items in the group or the entry-level price depending on the groups behaviour.
*
* @param ProductGroup $group
* @return int
*/
public function getPrice(ProductGroup $group):int {
/** @var ProductGroupBehaviour $behaviour */
$behaviour = $group->productGroupBehaviour()->get();
if(!$behaviour) return 0;
$price = false;
switch ($behaviour->title) {
case 'or':
case 'xor':
$price = $group->products()->min('price');
break;
case 'and':
$price = $group->products()->sum('price');
break;
}
if($price == 0) throw new \RuntimeException('The group "'.$group->title.'" did have a non implemented behaviour name: '.$behaviour->title);
}
}