File: D:/HostingSpaces/sdo/sdoschoonmaak.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\PropertyValueSelect;
use App\KommaApp\Kms\Core\Attributes\TextField;
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\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 App\KommaApp\Shop\Orders\Models\Order;
use App\KommaApp\Shop\Orders\Product\OrderedProduct;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Collection as BaseCollection;
use Illuminate\Database\Eloquent\Collection as DatabaseCollection;
/**
* 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 DatabaseCollection $sectionTabItems These must be filled with data. This is something you need to do yourself.
*
* @return mixed
*/
public function saveModel(Model $model = null, DatabaseCollection $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 DatabaseCollection $sectionTabItems A collection containing implementations AbstractSectionTabItem's
* @param Model $model
* @return DatabaseCollection
*/
public function fillAttributesWithData(DatabaseCollection $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->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;
}
/**
* Makes a single orderedProduct based of a product
*
* @param Product $product
* @param Order $order
* @return OrderedProduct
*/
public static function createOrderedProductFromProduct(Product $product, Order $order)
{
$attributes = $product->getAttributes();
unset($attributes['id']);
$orderedProduct = new OrderedProduct($attributes);
$orderedProduct->product()->associate($product);
$orderedProduct->quantity = 1;
$order->orderedProducts()->save($orderedProduct);
return $orderedProduct;
}
}