File: D:/HostingSpaces/farmfun/reserveren.farmfun.be/app/Komma/Products/Kms/ProductService.php
<?php
namespace App\Komma\Products\Kms;
use App\Komma\CalendarBlockOuts\Models\CalendarBlockOut;
use App\Komma\Kms\Core\ModelService;
use App\Komma\LocationProducts\Models\LocationProduct;
use App\Komma\Products\Models\Product;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
class ProductService extends ModelService
{
protected $orderByDisplayName = true;
public function __construct()
{
parent::__construct();
$this->setModelClassName(Product::class);
}
/**
* Extends the save model, for saving the price excluding vat
*
* @param Model $model
* @param Collection|null $attributes
* @return Model
*/
public function save(Model $model, Collection $attributes = null): Model
{
$model = parent::save($model, $attributes);
$model->price_each_unit_excluding_vat = calculate_price_excluding_vat((int) $model->price_each_unit, $model->vat_percentage);
$model->price_start_up_excluding_vat = calculate_price_excluding_vat((int) $model->price_start_up, $model->vat_percentage);
$model->save();
return $model;
}
public function destroyForModel(Model $model): Model
{
$model = parent::destroyForModel($model);
// Delete Indirect Relations
LocationProduct::where('product_id', $model->id)->delete();
CalendarBlockOut::where('product_id', $model->id)->delete();
\DB::table('site_products')->where('product_id', $model->id)->delete();
\DB::table('product_category_products')->where('product_id', $model->id)->delete();
return $model;
}
}