File: D:/HostingSpaces/Eurotools/euro-tools.nl/app/KommaApp/Shop/Products/Product/ProductService.php
<?php
namespace App\KommaApp\Shop\Products\Product;
use App\Helpers\KommaHelpers;
use App\KommaApp\Kms\Core\AbstractTranslatableModel;
use App\KommaApp\Kms\Core\Attributes\Attribute;
use App\KommaApp\Kms\Core\Attributes\Currency;
use App\KommaApp\Kms\Core\Attributes\PropertyValueSelect;
use App\KommaApp\Kms\Core\Attributes\TextField;
use App\KommaApp\Kms\Core\HasImagesInterface;
use App\KommaApp\Kms\Core\Kms;
use App\KommaApp\Kms\Core\KmsInterface;
use App\KommaApp\Kms\Core\NestedSets\Nodes\EloquentNode;
use App\KommaApp\Kms\Core\Sections\SectionService;
use App\KommaApp\Kms\Core\Sections\SectionTabItem;
use App\KommaApp\Kms\SidebarListItem;
use App\KommaApp\Languages\Models\Language;
use App\KommaApp\Shop\Categories\Kms\CategoryService;
use App\KommaApp\Shop\Categories\Kms\CategoryServiceInterface;
use App\KommaApp\Shop\Discounts\Actions\ModifyPriceAction;
use App\KommaApp\Shop\Discounts\Conditions\QuantityDiscountCondition;
use App\KommaApp\Shop\Discounts\DiscountService;
use App\KommaApp\Shop\Discounts\DiscountServiceInterface;
use App\KommaApp\Shop\Categories\Kms\CategorizableInterface;
use App\KommaApp\Shop\Properties\Kms\PropertyService;
use App\KommaApp\Shop\Properties\Models\Property;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Query\Builder;
/**
* Class ProductService
* @package App\KommaApp\Shop\Products
*/
class ProductService extends SectionService implements ProductServiceInterface
{
protected $sortable = false;
/** @var CategoryService $categoryService */
protected $categoryService;
/** @var DiscountService $discountService */
protected $discountService;
/** @var PropertyService $propertyService */
private $propertyService;
function __construct()
{
$this->forModelName = Product::class;
$this->categoryService = \App::make(CategoryServiceInterface::class);
$this->discountService = \App::make(DiscountServiceInterface::class);
$this->propertyService = \App::make(PropertyService::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 EloquentNode|CategorizableInterface $model */
//Process Product and discount Specific attributes
$quantityPrice = null;
$quantity = null;
$sectionTabItems->each(function ($sectionTabItem, $key) use ($model, &$productName, &$quantity, &$quantityPrice) {
/** @var SectionTabItem $sectionTabItem */
$attribute = $sectionTabItem->getAttribute();
$reference = $attribute->getsValueFromReference();
switch ($attribute->getsValueFrom())
{
case Attribute::ValueFromTranslationModel;
//save the slug
if($reference == 'meta_title')
{
/** @var Language $language */
$language = $attribute->getAssociatedLanguage();
if(!$model->exists) $model->save();
$translation = $this->getOrCreateTranslationModelForModel($model, $language);
$translation['slug'] = (str_slug($attribute->getValue()));
$translation->save();
}
break;
case Attribute::ValueFromItself:
if ($reference == 'quantity_price') {
$quantityPrice = $attribute->getValue();
}
if ($quantityPrice == '') {
$quantityPrice = null;
} else {
if ($reference == 'quantity') {
$quantity = $attribute->getValue();
}
}
if ($quantity == '') {
$quantity = null;
}
if($reference == 'category_id') {
if(!$model->exists) $model->save();
$this->categoryService->makeChildOfCategoriesIfNotAlready($model, $sectionTabItem->getAttribute()->getValue());
}
break;
}
});
$model->save(); //Save the model
$this->discountService->manageQuantityDiscountForASpecificDiscountable($model, $quantity, $quantityPrice);
$model = parent::saveModel($model,
$sectionTabItems); //First make sure we have a model and save the attributes in them from the SectionTabItem attributes
//Return the model
return $model;
}
/**
* Fills non-product specific attributes. Product 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)
{
$filledAttributesCollection = parent::fillAttributesWithData($sectionTabItems, $model);
/** @var TextField $quantityDiscountAttribute */
$quantityDiscountAttribute = null;
/** @var Currency $quantityPriceAttribute */
$quantityPriceAttribute = null;
$sectionTabItems->each(
function ($sectionTabItem, $key) use ($model, $filledAttributesCollection, &$quantityDiscountAttribute, &$quantityPriceAttribute) {
/** @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.");
$attribute = $sectionTabItem->getAttribute();
$valueReference = $sectionTabItem->getAttribute()->getsValueFromReference();
switch ($valueReference) {
//Case 1. Check if the translation of the model has a value.
case 'quantity_price':
$quantityPriceAttribute = $sectionTabItem->getAttribute();
break;
case 'quantity':
$quantityDiscountAttribute = $sectionTabItem->getAttribute();
break;
case 'category_id':
/** @var CategorizableInterface $model */
$idString = $this->categoryService->getCategoryIdsForModel($model);
if($idString) $sectionTabItem->getAttribute()->setValue($idString);
break;
}
}
);
if($quantityPriceAttribute && $quantityPriceAttribute)
{
$discount = $this->discountService->getQuantityDiscountForASpecificDiscountable($model);
if($discount)
{
/** @var QuantityDiscountCondition $quantityDiscountCondition */
$quantityDiscountCondition = $discount->getDiscountCondition();
/** @var ModifyPriceAction $modifyPriceAction */
$modifyPriceAction = $discount->getDiscountAction();
$quantityPriceAttribute->setValue($modifyPriceAction->getModificationValue());
$quantityDiscountAttribute->setValue($quantityDiscountCondition->getQuantity());
}
} else {
throw new \RuntimeException('The product section must have an attribute with reference quantity_price and price. But not have them');
}
return $filledAttributesCollection;
}
/**
* Return all products that are not in groups
*
* @return Builder
*/
public function modelsWithoutGroups()
{
$products = $this->models()//Get all product models
->doesntHave('groups')//...that don't have any ProductGroups
->with(['translation']); //...with all productTranslations for the current language id of the current site
return $products;
}
/**
* Returns all models for the sidebar menu in the backend
*
* @return array
*/
public function getModelsForSideBar():array
{
if(isset($this->orderBy)){
if($this->orderReverse) $models = $this->forModelName::orderBy($this->orderBy, 'desc');
else $models = $this->forModelName::orderBy($this->orderBy);
}
else $models = $this->forModelName::orderBy('id');
// If Site isset then we add it to the query, so
$site = $this->kms->getSite();
if($site && $siteId = $this->kms->getSite()->id) {
$models = $models->where('site_id', $siteId);
}
// Get the models
$models = $models->get();
// Preload translation to prevent individual queries
if(is_a(new $this->forModelName, AbstractTranslatableModel::class)){
$models = $models->load('translation');
}
// Preload images to prevent individual queries
if(is_a(new $this->forModelName, HasImagesInterface::class)){
$models = $models->load('images');
}
$sidebarList = [];
foreach ($models as $model) {
$sidebarListItem = new SidebarListItem();
$this->setThumbnail($model);
$this->generateThumbnail($model);
//Set the values for the sidebar
$sidebarListItem->setId($model->id);
$sidebarListItem->setStatus($model->active);
$title = KommaHelpers::str_limit_full_word($model->title, 75);
$sidebarListItem->setName($title);
$sidebarListItem->setThumbnail($model->thumbnail);
$sidebarListItem->setModel($model->setRelations([])->makeHidden(['title','featured', 'active', 'price', 'vpe','inner_box', 'created_at', 'updated_at', 'id', 'stock_keeping_unit']));
$sidebarList[] = $sidebarListItem;
}
return $sidebarList;
}
}