HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/ASmits/kemi.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\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 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;

    function __construct()
    {
        $this->forModelName = Product::class;
        $this->categoryService = \App::make(CategoryServiceInterface::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|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.");

                $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);
                }
            }
        );

        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;
    }
}