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