File: D:/HostingSpaces/SBogers10/blije-gasten.komma.pro/app/Komma/Shop/Orders/Models/Order.php
<?php
namespace App\Komma\Shop\Orders\Models;
use App\Komma\Kms\Core\Attributes\Models\Traits\HasThumbnailInterface;
use App\Komma\Kms\Core\Attributes\Models\Traits\HasThumbnailTrait;
use App\Komma\Kms\Core\Entities\DisplayNameInterface;
use App\Komma\Kms\Core\Entities\DisplayNameTrait;
use App\Komma\Shop\Orders\OrderStatus;
use App\Komma\Shop\Orders\Product\HasOrderedProducts;
use App\Komma\Shop\Orders\ProductComposite\OrderedProductComposite;
use App\Komma\Shop\Orders\ProductGroup\HasOrderedProductGroups;
use App\Komma\Shop\Orders\ProductGroup\HasOrderedProductGroupsInterface;
use App\Komma\Shop\Orders\Product\HasOrderedProductsInterface;
use App\Komma\Shop\Payment\Transaction;
use App\Komma\Shop\Payment\TransactionStatus;
use App\Komma\Shop\Shipments\Shipment;
use App\Komma\Users\Models\SiteUser;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use InvalidArgumentException;
/**
* App\Komma\Shop\Orders\Models\Order
*
* @property int $id
* @property string $order_number
* @property string $invoice_number
* @property int $user_id
* @property string $status
* @property Carbon $date
* @property int $total_price
* @property int $total_price_ex
* @property string $discount Discount info for the order (not its products) only.
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read \App\Komma\Users\Models\SiteUser $customer
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Shop\Orders\ProductComposite\OrderedProductComposite[] $orderedProductComposites
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Shop\Orders\ProductGroup\OrderedProductGroup[] $orderedGroups
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Shop\Orders\Product\OrderedProduct[] $orderedProducts
* @property-write mixed $customer_info
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Shop\Payment\Transaction[] $transactions
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Shop\Payment\Transaction[] $paidTransactions
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Shop\Payment\Transaction[] $intakeTransactions
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Orders\Models\Order newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Orders\Models\Order newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Orders\Models\Order query()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Orders\Models\Order whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Orders\Models\Order whereDiscount($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Orders\Models\Order whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Orders\Models\Order whereInvoiceAddressId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Orders\Models\Order whereShippingAddressId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Orders\Models\Order whereStatus($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Orders\Models\Order whereTotalPrice($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Orders\Models\Order whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Orders\Models\Order whereUserId($value)
* @mixin \Eloquent
* @property int $invoice_address_id
* @property int $shipping_address_id
* @property string $invoice_company
* @property string $invoice_street
* @property string $invoice_house_number
* @property string $invoice_postal_code
* @property string $invoice_city
* @property string $invoice_telephone
* @property string $invoice_country_iso2
* @property string $shipping_street
* @property string $shipping_house_number
* @property string $shipping_postal_code
* @property string $shipping_city
* @property string $shipping_telephone
* @property int $vat_amount
* @property int $vat_percentage
* @property string $shipping_country_iso2
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Shop\Shipments\Shipment[] $shipments
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Orders\Models\Order whereInvoiceCity($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Orders\Models\Order whereInvoiceCountry($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Orders\Models\Order whereInvoiceHouseNumber($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Orders\Models\Order whereInvoicePostalCode($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Orders\Models\Order whereInvoiceStreet($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Orders\Models\Order whereInvoiceTelephone($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Orders\Models\Order whereOrderNumber($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Orders\Models\Order whereShippingCity($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Orders\Models\Order whereShippingCountry($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Orders\Models\Order whereShippingHouseNumber($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Orders\Models\Order whereShippingPostalCode($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Orders\Models\Order whereShippingStreet($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Orders\Models\Order whereShippingTelephone($value)
*/
class Order extends Model implements HasThumbnailInterface, HasOrderedProductsInterface, DisplayNameInterface
{
protected $name = '';
use DisplayNameTrait;
use HasThumbnailTrait;
use HasOrderedProducts;
// use HasOrderedProductGroups;
/*
* Transient properties on Eloquent models
* These are not saved to database.
*/
protected $class = Order::class;
protected $dates = ['date'];
public function __construct(array $attributes = [])
{
$this->name = ucfirst(__('shop/orders.order'));
parent::__construct($attributes);
}
/**
* Mutates the customer_info attribute
* @param SiteUser $user
*/
public function setCustomerInfoAttribute($user)
{
if (is_numeric($user)) {
$user = SiteUser::find($user);
if (!$user) {
return;
}
}
$formatted = __('kms/site_users.first_name') . ': ' . $user->first_name . PHP_EOL;
$formatted .= __('kms/site_users.last_name') . ': ' . $user->last_name . PHP_EOL;
$formatted .= __('kms/site_users.email') . ': ' . $user->email . PHP_EOL;
$formatted .= '' . PHP_EOL;
$formatted .= __('shop/orders.invoice_address') . ':' . PHP_EOL;
$formatted .= $user->street . ' ' . $user->house_number . ', ' . $user->zip_code . ', ' . $user->city . ', ' . $user->country . PHP_EOL;
$this->attributes['customer_info'] = $formatted;
}
/**
* Returns true if the order has been paid.
*/
public function hasBeenPaid()
{
return (
OrderStatus::AWAITING_FULFILLMENT ||
OrderStatus::AWAITING_SHIPMENT ||
OrderStatus::AWAITING_PICKUP ||
OrderStatus::PARTIALLY_SHIPPED ||
OrderStatus::COMPLETED ||
OrderStatus::SHIPPED ||
OrderStatus::REFUNDED ||
OrderStatus::PARTIALLY_REFUNDED
);
}
// public function customer(): BelongsTo
// {
// return $this->belongsTo(SiteUser::class, 'site_user_id');
// }
//
// public function orderedProductComposites():HasMany
// {
// return $this->HasMany(OrderedProductComposite::class);
// }
/**
* Returns all the shipping address attributes and their values
*
* @return array
*/
public function getShippingAddressAttributes()
{
$attributeNames = ['shipping_street', 'shipping_house_number', 'shipping_postal_code', 'shipping_city', 'shipping_country_iso2'];
$attributes = [];
foreach($attributeNames as $attributeName) $attributes[$attributeName] = $this->getAttribute($attributeName);
return $attributes;
}
/**
* Returns all the invoice address attributes and their values
*
* @return array
*/
public function getInvoiceAddressAttributes()
{
$attributeNames = ['invoice_company', 'invoice_vat_number', 'invoice_street', 'invoice_house_number', 'invoice_postal_code', 'invoice_city', 'invoice_country_iso2'];
$attributes = [];
foreach($attributeNames as $attributeName) $attributes[$attributeName] = $this->getAttribute($attributeName);
return $attributes;
}
/**
* Returns the payment transactions for the order.
* Like successful, canceled, refunded payments
*
* @return HasMany
*/
public function transactions(): HasMany
{
return $this->hasMany(Transaction::class);
}
/**
* Returns the paid transactions for the order.
*
* @return HasMany
*/
public function paidTransactions(): HasMany
{
return $this->hasMany(Transaction::class)
->where('intake' ,'=', false)
->where('status', TransactionStatus::PAYMENT_PAID);
}
/**
* Return the payment transaction for the order related to the intake
*
* @return HasMany
*/
public function intakeTransactions(): HasMany
{
return $this->hasMany(Transaction::class)
->where('intake' ,'=', true)
->orderBy('created_at', 'desc');
}
// /**
// * @return HasMany
// */
// public function shipments(): HasMany
// {
// return $this->hasMany(Shipment::class);
// }
//Mutators
// public function setTotalPriceExAttribute($newValue) {
// //Check that the value is an integer. Else throw an exception. The number needs to get rounded properly first.
// if($newValue - floor($newValue) != 0) throw new InvalidArgumentException('The total_price_ex attribute must be an integer or a floating point number ending with zero decimals or have a decimal of .0');
// $this->attributes['total_price_ex'] = $newValue;
// }
// public function setTotalPriceAttribute($newValue) {
// //Check that the value is an integer. Else throw an exception. The number needs to get rounded properly first.
// if($newValue - floor($newValue) != 0) throw new InvalidArgumentException('The total_price attribute must be an integer or a floating point number ending with zero decimals or have a decimal of .0');
// $this->attributes['total_price'] = $newValue;
// }
// public function setVatAmountAttribute($newValue) {
// //Check that the value is an integer. Else throw an exception. The number needs to get rounded properly first.
// if($newValue - floor($newValue) != 0) throw new InvalidArgumentException('The vat_amount attribute must be an integer or a floating point number ending with zero decimals or have a decimal of .0');
// $this->attributes['vat_amount'] = $newValue;
// }
// public function __get($key)
// {
// if($key == 'name' && isset($this->attributes['id'])) return ucfirst(__('shop/orders.order')).' '.$this->attributes['id'];
// return parent::__get($key);
// }
public function getPaymentAmount()
{
return $this->total + $this->deposit;
}
public function getShortageTotal(): int
{
$shortageTotal = isset($this->shortage_total) ? $this->shortage_total : 0;
if(isset($this->open_fee)) $shortageTotal += $this->open_fee;
return $shortageTotal;
}
public function shouldReturnDeposit(): bool
{
if($this->getShortageTotal() > $this->deposit) return false;
return true;
}
public function getDepositToReturn(): int
{
if($this->getShortageTotal() === 0) return $this->deposit;
if($this->shouldReturnDeposit()) return $this->deposit - $this->getShortageTotal();
else return $this->getShortageTotal() - $this->deposit;
}
/**
* If any of the ordered products has not been handled return false
* Else return true
*
* @return bool
*/
public function orderedProductsHaveBeenHandled(): bool
{
$this->loadMissing('orderedProducts');
foreach ($this->orderedProducts as $orderedProduct)
{
if($orderedProduct->deposit_handled) continue;
return false;
}
return true;
}
}