2021-03-04 09:38:39 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use App\Enums\ContactFormCivility;
|
|
|
|
|
use App\Enums\OrderStatus;
|
|
|
|
|
use App\Enums\OrderType;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2021-03-04 12:34:17 +01:00
|
|
|
use Illuminate\Support\Carbon;
|
2021-03-04 09:38:39 +01:00
|
|
|
use Kwaadpepper\Enum\Traits\CastsEnums;
|
|
|
|
|
|
2023-09-18 18:17:00 +02:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @property integer $id
|
|
|
|
|
* @property string $ref
|
|
|
|
|
* @property \App\Enums\ContactFormCivility $civility
|
|
|
|
|
* @property \App\Enums\OrderStatus $status
|
|
|
|
|
* @property \App\Enums\OrderType $payementType
|
|
|
|
|
* @property string $lastname
|
|
|
|
|
* @property string $firstname
|
|
|
|
|
* @property string $address
|
|
|
|
|
* @property string $postcode
|
|
|
|
|
* @property string $phone
|
|
|
|
|
* @property string $email
|
|
|
|
|
* @property string $city
|
|
|
|
|
* @property boolean $confirmMailSent
|
|
|
|
|
* @property \Carbon\Carbon $created_at
|
|
|
|
|
* @property \Carbon\Carbon $updated_at
|
|
|
|
|
*
|
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<\App\Models\SellItem> $sellItems
|
|
|
|
|
* @property-read \App\Models\Customers|null $customer
|
|
|
|
|
* @property-read \App\Models\DeliveryAddress|null $deliveryAddress
|
|
|
|
|
* @package App\Models
|
|
|
|
|
*/
|
2021-03-04 09:38:39 +01:00
|
|
|
class Order extends Model
|
|
|
|
|
{
|
|
|
|
|
use CastsEnums;
|
|
|
|
|
|
2023-09-18 18:17:00 +02:00
|
|
|
/**
|
|
|
|
|
* The attributes that should be cast to Enums.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2021-03-04 09:38:39 +01:00
|
|
|
protected $enumCasts = [
|
|
|
|
|
'civility' => ContactFormCivility::class,
|
|
|
|
|
'status' => OrderStatus::class,
|
|
|
|
|
'payementType' => OrderType::class
|
|
|
|
|
];
|
|
|
|
|
|
2023-09-18 18:17:00 +02:00
|
|
|
/**
|
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
|
*
|
|
|
|
|
* @var string[]
|
|
|
|
|
*/
|
2021-03-04 09:38:39 +01:00
|
|
|
protected $fillable = [
|
|
|
|
|
'civility',
|
|
|
|
|
'lastname',
|
|
|
|
|
'firstname',
|
|
|
|
|
'address',
|
|
|
|
|
'postcode',
|
|
|
|
|
'city',
|
|
|
|
|
'email',
|
|
|
|
|
'phone'
|
|
|
|
|
];
|
|
|
|
|
|
2023-09-18 18:17:00 +02:00
|
|
|
// * RELATIONSHIPS
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The sellItems that belong to the Order
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
|
|
|
*/
|
2021-03-04 09:38:39 +01:00
|
|
|
public function sellItems()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(SellItem::class)->withPivot('quantity');
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-18 18:17:00 +02:00
|
|
|
/**
|
|
|
|
|
* The customer that belong to the Order
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
|
*/
|
2021-03-04 09:38:39 +01:00
|
|
|
public function customer()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Customers::class);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-18 18:17:00 +02:00
|
|
|
/**
|
|
|
|
|
* Get the user associated with the Order
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne
|
|
|
|
|
*/
|
2021-03-04 09:38:39 +01:00
|
|
|
public function deliveryAddress()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasOne(DeliveryAddress::class);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-18 18:17:00 +02:00
|
|
|
// * METHODS
|
|
|
|
|
|
2021-03-04 12:34:17 +01:00
|
|
|
/**
|
|
|
|
|
* Get all failed orders old than more one month
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Query\Builder
|
|
|
|
|
*/
|
|
|
|
|
public static function outdatedFailedOrders()
|
|
|
|
|
{
|
|
|
|
|
return Order::where('status', OrderStatus::failed())
|
|
|
|
|
->where('payementType', OrderType::cb())
|
|
|
|
|
->whereDate('updated_at', '<=', Carbon::now()->subMonth()->toDateString());
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-04 09:38:39 +01:00
|
|
|
/**
|
|
|
|
|
* Export the model data to a Formatted Arcadia Data array
|
|
|
|
|
*
|
2023-09-18 18:17:00 +02:00
|
|
|
* @return array<string,string|integer|float|array<string,integer>>
|
2021-03-04 09:38:39 +01:00
|
|
|
*/
|
|
|
|
|
public function exportToOrderMailData(): array
|
|
|
|
|
{
|
|
|
|
|
$sellItems = $this->sellItems()->withPivot('quantity')->get();
|
|
|
|
|
return [
|
|
|
|
|
'civility' => (string)$this->civility,
|
|
|
|
|
'lastname' => $this->lastname,
|
|
|
|
|
'firstname' => $this->firstname,
|
|
|
|
|
'address' => $this->address,
|
|
|
|
|
'postcode' => $this->postcode,
|
|
|
|
|
'city' => $this->city,
|
|
|
|
|
'email' => $this->email,
|
|
|
|
|
'phone' => $this->phone,
|
|
|
|
|
'items' => $sellItems->toArray()
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|