File: D:/HostingSpaces/ZelfVerkopen/zelfverkopen.nl/app/KommaApp/Shop/Products/Product/ProductService.php
<?php
namespace App\KommaApp\Shop\Products\Product;
use App\KommaApp\Kms\Core\Attributes\Attribute;
use App\KommaApp\Kms\Core\Attributes\Currency;
use App\KommaApp\Kms\Core\Attributes\TextField;
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\Languages\Models\Language;
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 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 DiscountService $discountService */
protected $discountService;
function __construct()
{
$this->forModelName = Product::class;
$this->discountService = \App::make(DiscountServiceInterface::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 $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;
}
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 attributes with data from a model in this way:
*
* 1. First it looks if it needs to get the value from a translationModel.
* If so, gets it, fills the attribute and fills the next attribute if any
* 2. Then it looks if it needs to get the value from it's model.
* If so, gets it, fills the attribute and fills the next attribute if any
* 4. Then it looks if it needs to get the value from the images associated with it.
* If so, gets it, fills the attribute and fills the next attribute if any
* Please notice that if cases 1, 2 and 3 where true the value you may have set with setValue is overwritten.
*
* @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.");
$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;
}
if ($quantityDiscountAttribute && $quantityPriceAttribute) return false; //break out of the complete each loop
}
);
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->getValue());
$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;
}
}