File: D:/HostingSpaces/blijegasten/blijegasten.be/app/Komma/Shop/Products/Product/ProductController.php
<?php
namespace App\Komma\Shop\Products\Product;
use App\Komma\Kms\Core\Attributes\Attribute;
use App\Komma\Kms\Core\Attributes\QuantityDiscount;
use App\Komma\Kms\Core\SectionController;
use App\Komma\Shop\Categories\Kms\CategoryModelService;
use App\Komma\Shop\Discounts\Actions\ModifyPriceAction;
use App\Komma\Shop\Discounts\Conditions\QuantityDiscountCondition;
use App\Komma\Shop\Discounts\DiscountService;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
class ProductController extends SectionController
{
protected $slug = "products";
protected $classModelName = Product::class;
protected $forTranslationModelName = ProductTranslation::class;
private $categoryService;
/** @var DiscountService */
private $discountService;
public function __construct()
{
$productSection = new ProductSection($this->slug);
parent::__construct($productSection);
$this->categoryService = new CategoryModelService();
$this->discountService = \App::make(DiscountService::class);
$this->modelService = new ProductModelService();
$this->translationService = new ProductTranslationModelService();
}
/**
* A function that MUST delegate to services to accomplish saving.
* It MUST NOT act like a service.
*
* @param Model $model
* @param Collection $attributesByValueFrom
* @return Model
*/
protected function save(Model $model, Collection $attributesByValueFrom = null): Model
{
if($attributesByValueFrom == null) return $model;
$model = parent::save($model, $attributesByValueFrom);
// Manual hack to save the currency price with vat
$price = (int)(floatval(request('Currency-price')) * 100);
$model->price = $price;
$model->save();
//Use the site service
$this->siteService->save($model, $attributesByValueFrom->get(Attribute::ValueFromItself));
//Use the discountService
$discountService = $this->discountService;
$attributesByValueFrom->collapse()->each(function(Attribute $attribute) use(&$model, &$quantity, &$quantityPrice, &$discountService) {
if(!is_a($attribute, QuantityDiscount::class)) return;
/** @var $attribute QuantityDiscount */
$discountService->manageQuantityDiscountForASpecificProductable($model, $attribute->getQuantity(), $attribute->getDiscountPrice());
});
return $model;
}
/**
* A function that MUST delegate to services to accomplish loading.
* It MUST NOT act like a service.
*
* @param Model $model
* @param Collection $attributesByValueFrom
* @return Collection
*/
protected function load(Model $model, Collection $attributesByValueFrom = null): Collection
{
if($attributesByValueFrom == null) return new Collection();
parent::load($model, $attributesByValueFrom);
$discountService = $this->discountService;
$attributesByValueFrom->collapse()->each(function(Attribute $attribute) use(&$model, &$quantity, &$quantityPrice, &$discountService) {
if($attribute->getKey()->getValuePart() == 'price_excluding_vat') {
$attribute->setPriceValue($model->price);
// dd($model->price, $attribute);
}
if(!is_a($attribute, QuantityDiscount::class)) return;
/** @var $attribute QuantityDiscount */
$discount = $discountService->getQuantityDiscountForASpecificDiscountable($model);
if(!$discount) return;
$quantityDiscountCondition = $discount->getDiscountCondition(); /** @var QuantityDiscountCondition $quantityDiscountCondition */
$modifyPriceAction = $discount->getDiscountAction(); /** @var ModifyPriceAction $modifyPriceAction */
$attribute->setValue($quantityDiscountCondition->getQuantity().'|'.$modifyPriceAction->getModificationValue());
});
//Load site
$this->siteService->load($model, $attributesByValueFrom->get(Attribute::ValueFromItself));
//Load quantity discount
$this->discountService->load($model, $attributesByValueFrom->collapse());
return $attributesByValueFrom;
}
}