ldc-legaulois/app/Models/Order.php
Jérémy Munsch 9b4af94e73
Closes #39 Send Orders as Email to arcadia and keep generated files
-  clean files after one month
- Dispatch emails as database jobs (retry on fail)
- add command to trigger cleanup of old xml files
2023-09-18 18:17:00 +02:00

132 lines
3.7 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;
/**
*
* @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
*/
class Order extends Model
{
use CastsEnums;
/**
* The attributes that should be cast to Enums.
*
* @var array
*/
protected $enumCasts = [
'civility' => ContactFormCivility::class,
'status' => OrderStatus::class,
'payementType' => OrderType::class
];
/**
* The attributes that are mass assignable.
*
* @var string[]
*/
protected $fillable = [
'civility',
'lastname',
'firstname',
'address',
'postcode',
'city',
'email',
'phone'
];
// * RELATIONSHIPS
/**
* The sellItems that belong to the Order
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function sellItems()
{
return $this->belongsToMany(SellItem::class)->withPivot('quantity');
}
/**
* The customer that belong to the Order
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function customer()
{
return $this->belongsTo(Customers::class);
}
/**
* Get the user associated with the Order
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function deliveryAddress()
{
return $this->hasOne(DeliveryAddress::class);
}
// * METHODS
/**
* 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<string,string|integer|float|array<string,integer>>
*/
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()
];
}
}