File: D:/HostingSpaces/netwerkbrabant/netwerkbrabant.nl/app/KommaApp/Products/Kms/ProductService.php
<?php
namespace App\KommaApp\Products\Kms;
use App\Helpers\KommaHelpers;
use App\KommaApp\Routes\Models\Route;
use App\KommaApp\Documents\Kms\DocumentableInterface;
use App\KommaApp\Products\Models\ProductTranslation;
use App\KommaApp\Kms\Core\AbstractTranslatableModel;
use App\KommaApp\Kms\Core\Attributes\Attribute;
use App\KommaApp\Kms\Core\Attributes\Models\SelectOptionInterface;
use App\KommaApp\Kms\Core\HasRoutesInterface;
use App\KommaApp\Kms\Core\NestedSets\Nodes\TreeModel;
use App\KommaApp\Kms\Core\NestedSets\Nodes\TreeModelInterface;
use App\KommaApp\Kms\Core\Sections\SectionService;
use App\KommaApp\Kms\Core\Sections\SectionTabItem;
use App\KommaApp\Kms\SidebarListItem;
use App\KommaApp\Languages\Models\Language;
use App\KommaApp\Products\Models\Product;
use App\KommaApp\Sites\HasSitesInterface;
use App\KommaApp\WeFact\WeFactAPI;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection as BaseCollection;
class ProductService extends SectionService
{
protected $sortable = false;
protected $orderByDisplayName = true;
function __construct()
{
$this->forModelName = Product::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 TreeModel $model */
//Process Product Specific attributes
$sectionTabItems->each(function($sectionTabItem, $key) use($model) {
/** @var SectionTabItem $sectionTabItem */
$attribute = $sectionTabItem->getAttribute();
$reference = $attribute->getsValueFromReference();
switch ($attribute->getsValueFrom())
{
case Attribute::ValueFromItself:
if($reference == 'site_name'){
$model->site_id = $this->siteService->getCurrentSite()->id;
}
break;
}
});
$model->save(); //Save the product
$model = parent::saveModel($model, $sectionTabItems); //First make sure we have a model and save the attributes in them from the SectionTabItem attributes
$this->siteService->linkModelToCurrentSite($model);
$model->price = '€ ' . number_format(WeFactAPI::convertPriceInclToExcl($model->price_amount, $model->vat_amount), 2, ',', '.');
$model->save();
// Create the slug for each translation
foreach($model->translations as $translation){
if(!$translation->exists) continue;
$translation->slug = $this->createOrGetUniqueSlug($translation, $translation->name);
$translation->save();
}
//Return the product
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);
$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();
$value = $attribute->getValue();
$valueReference = $sectionTabItem->getAttribute()->getsValueFromReference();
switch ($valueReference) {
case 'site_name':
$site = $this->siteService->getCurrentSite();
$attribute->setValue($site->name);
break;
case 'wefact_code':
$attribute->setValue($model->wefact_code);
break;
case 'slug':
if(isset($model->translation)) {
$route = Route::where('route', 'products')
->where('language_id', $model->translation->language_id)
->first();
if(!isset($route)) $attribute->setValue('Geen bijbehorende pagina gevonden.');
else $attribute->setValue( '/' . $route->alias . '/' . $model->translation->slug);
}
else $attribute->setValue('Wordt aangemaakt bij opslag.');
break;
}
}
);
return $filledAttributesCollection;
}
}