ldc-legaulois/app/Models/Order.php
Jérémy Munsch c5e3a834fa phpcbf fix
2021-09-15 14:27:41 +02:00

80 lines
1.9 KiB
PHP

<?php
namespace App\Models;
use App\Enums\ContactFormCivility;
use App\Enums\OrderStatus;
use App\Enums\OrderType;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Kwaadpepper\Enum\Traits\CastsEnums;
class Order extends Model
{
use CastsEnums;
protected $enumCasts = [
'civility' => ContactFormCivility::class,
'status' => OrderStatus::class,
'payementType' => OrderType::class
];
protected $fillable = [
'civility',
'lastname',
'firstname',
'address',
'postcode',
'city',
'email',
'phone'
];
public function sellItems()
{
return $this->belongsToMany(SellItem::class)->withPivot('quantity');
}
public function customer()
{
return $this->belongsTo(Customers::class);
}
public function deliveryAddress()
{
return $this->hasOne(DeliveryAddress::class);
}
/**
* 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());
}
/**
* Export the model data to a Formatted Arcadia Data array
*
* @return array
*/
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()
];
}
}