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/SBogers10/zelfverkopen.komma.pro/app/KommaApp/Orders/Models/Order.php
<?php
/**
 *
 *
 * @author      Komma <info@komma.pro>
 * @copyright   (c) 2012-2016, Komma
 */

namespace App\KommaApp\Orders\Models;

use App\KommaApp\Countries\Models\Country;
use App\KommaApp\Customers\Models\Customer;
use App\KommaApp\Kms\Core\NestedSets\Nodes\TranslatableEloquentNode;
use App\KommaApp\Languages\Models\Language;
use App\KommaApp\Products\Models\Product;
use App\KommaApp\Sites\Models\Site;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * Class SaleOrder
 *
 * @package App\KommaApp\SaleOrders\Models
 * @property int id
 * @property int site_id
 * @property int active
 * @property string order_id
 * @property string code_name
 * @property int payment_fail_counter
 * @property-read \App\KommaApp\Customers\Models\Customer $customer
 * @property-read \App\KommaApp\Countries\Models\Country $invoice_country
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Orders\Models\OrderPayment[] $orderPayments
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Products\Models\Product[] $products
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Query\Builder|\App\KommaApp\Orders\Models\Order onlyTrashed()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Query\Builder|\App\KommaApp\Orders\Models\Order withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\KommaApp\Orders\Models\Order withoutTrashed()
 * @mixin \Eloquent
 */
class Order extends Model
{
    use SoftDeletes;

    protected $table = 'orders';
    protected $class = Order::class;

    const ZELFVERKOPING_PREFIX = 'ZELF-';
    const ZELFVERKOPING_STARTING_ID = 2212;

    const ORDER_STATUS_NEW = 0;
    const ORDER_STATUS_PROCESSING = 1;
    const ORDER_STATUS_AWAITING_PAYMENT = 2;
    const ORDER_STATUS_IN_SALE = 3;
    const ORDER_STATUS_CLOSED = 4;
    const ORDER_STATUS_DELETED = 5;
    const ORDER_STATUS_REFUNDED = 6;
    const ORDER_STATUS_CREDIT = 7; //Marks an order as credit invoice

    CONST ORDER_STATUS_ASPERION_OPEN = 1;
    CONST ORDER_STATUS_ASPERION_INVOICED = 2;
    CONST ORDER_STATUS_ASPERION_CANCELED = 3;


    /*
    * Transient properties on Eloquent models
    * These are not saved to database.
    */
    public $thumbnail = false;


    protected $fillable = [
        'status',
        'payment_fail_counter',
        'gender',
        'last_name',
        'zip',
        'house_number',
        'house_number_addition',
        'street',
        'city',
        'country_id',
        'payed'
    ];

    public function getOrderStatusForAsperion()
    {
        switch ($this->attributes['status'])
        {
            case self::ORDER_STATUS_NEW:
            case self::ORDER_STATUS_PROCESSING:
            case self::ORDER_STATUS_AWAITING_PAYMENT:
                return self::ORDER_STATUS_ASPERION_OPEN;
            case self::ORDER_STATUS_IN_SALE:
            case self::ORDER_STATUS_CLOSED:
                if($this->attributes['payed'] == 1) return self::ORDER_STATUS_ASPERION_INVOICED;
                return self::ORDER_STATUS_ASPERION_OPEN;
            default: return self::ORDER_STATUS_ASPERION_OPEN;
        }
    }

    /**
     * A mutator that automatically formats the zip code.
     *
     * @param $zip
     */
    public function setZipAttribute($zip)
    {
        $formatted = $zip;
        $formatted = strtoupper($formatted);
        $formatted = trim($formatted);
        $formatted = str_replace(' ', '', $formatted);

        $this->attributes['zip'] = $formatted;
    }

    /**
     * A accessor that automatically formats the zip code.
     *
     * @param $zip
     * @return mixed|string
     */
    public function getZipAttribute($zip)
    {
        $formatted = $zip;
        $formatted = strtoupper($formatted);
        $formatted = trim($formatted);
        $formatted = str_replace(' ', '', $formatted);

        return $formatted;
    }

    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = ['deleted_at'];

    /**
     * Get the customer of this order
     *
     * @return BelongsTo
     */
    public function customer(): BelongsTo
    {
        return $this->belongsTo(Customer::class, 'customer_id', 'id');
    }

    /**
     * Finds a related order (for credit invoicing)
     *
     * @return HasOne
     */
    public function relatedOrder(): HasOne
    {
        return $this->hasOne(Order::class, 'related_order_id');
    }

    /**
     * Gets the invoice country model for this model
     *
     * @return HasOne
     */
    public function invoice_country(): HasOne
    {
        return $this->hasOne(Country::class);
    }

    public function products(): BelongsToMany
    {
        return $this->belongsToMany(Product::class, 'order_products')
            ->withPivot('price', 'name');
    }

    public function orderPayments():HasMany
    {
        return $this->hasMany(OrderPayment::class);
    }

    public function __get($key)
    {
        if ($key == "title") {

            if(isset($this->customer) && isset($this->customer->customerProperties) && $this->customer->customerProperties->count() != 0) {

                $propertyForSale = $this->customer->customerProperties->first();

                $streetString = $propertyForSale->street . ' ' . $propertyForSale->house_number;
                if($propertyForSale->house_number_addition != '') $streetString .= ' '.$propertyForSale->house_number_addition;

                return $this->order_id . ' | ' . $streetString;
            }
            else{
                return $this->order_id . ' | '.trans('orders.genderTitle.' . $this->gender) . ' ' . $this->last_name;
            }


        }

        return parent::__get($key);
    }

    /**
     * Set status to deleted
     */
    public function delete()
    {
        $this->attributes['status'] = self::ORDER_STATUS_DELETED;
        self::save();
    }
}