File: D:/HostingSpaces/SBogers10/werkenbij.komma.pro/app/Komma/Shop/Products/Product/ProductService.php
<?php
namespace App\Komma\Shop\Products\Product;
use App\Komma\Kms\Core\Attributes\Attribute;
use App\Komma\Kms\Core\Attributes\Currency;
use App\Komma\Kms\Core\Attributes\Models\SelectOptionInterface;
use App\Komma\Kms\Core\Attributes\TextField;
use App\Komma\Kms\Core\Tree\NestedSets\Nodes\TreeModel;
use App\Komma\Kms\Core\Sections\SectionService;
use App\Komma\Kms\Core\Sections\SectionTabItem;
use App\Komma\Globalization\Languages\Models\Language;
use App\Komma\Shop\Categories\Kms\CategoryService;
use App\Komma\Shop\Categories\Kms\CategoryServiceInterface;
use App\Komma\Shop\Discounts\Actions\ModifyPriceAction;
use App\Komma\Shop\Discounts\Conditions\QuantityDiscountCondition;
use App\Komma\Shop\Discounts\DiscountService;
use App\Komma\Shop\Discounts\DiscountServiceInterface;
use App\Komma\Shop\Categories\Kms\CategorizableInterface;
use App\Komma\Shop\Properties\Kms\PropertyService;
use App\Komma\Shop\Orders\Models\Order;
use App\Komma\Shop\Orders\Product\OrderedProduct;
use App\Komma\Sites\HasSitesInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Query\Builder;
use Illuminate\Database\Eloquent\Collection as DatabaseCollection;
use Illuminate\Support\Collection;
/**
* Class ProductService
* @package App\Komma\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(CategoryServiceInterface::class);
$this->discountService = app(DiscountServiceInterface::class);
$this->propertyService = app(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 TreeModel|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();
$value = $attribute->getValue();
$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->getTranslationModelForModelByLanguage($model, $language);
if(!$translation) throw new \RuntimeException('Expected to get a product translation. Got none.');
$translation['slug'] = (str_slug($attribute->getValue()));
}
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') {
$this->doAfterSave(function ($model) use($value) {
$this->categoryService->makeChildOfCategoriesIfNotAlready($model, $value);
});
}
if($reference == 'site_id') {
$this->doAfterSave(function ($model) use($value) {
$this->siteService->linkModelToSitesUsingIdCsvString($model, $value);
});
}
break;
}
});
$model->save(); //Save the model
$this->saveModelTranslations($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;
case 'site_id':
/** @var HasSitesInterface $model */
$idString = $this->siteService->getSiteIdsForModel($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;
}
public function getOptionsForSelect(bool $allowNullableSelectOption = false): Collection
{
$selectOptions = collect();
$products = Product::with('translation');
$products = $products->get();
foreach ($products as $product) {
$name = $product->getDisplayName();
if(empty($name)){
if($product->lft == 1) $name = 'Root';
else{ $name = __('kms/products.entity'). ' '. $product->id; }
}
/** @var SelectOptionInterface $selectOption */
$selectOption = (app(SelectOptionInterface::class))
->setContent($name)
->setHtmlContent($name)
->setValue($product->id);
$selectOptions->push($selectOption);
}
return $selectOptions;
}
}