ldc-supportweb/app/Models/Invoice.php

314 lines
9.2 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use Kwaadpepper\CrudPolicies\Enums\CrudAction;
use Kwaadpepper\CrudPolicies\Enums\CrudType;
use Kwaadpepper\CrudPolicies\Traits\IsCrudModel;
class Invoice extends Model
{
use HasFactory;
use IsCrudModel;
/**
* Crud label column
*
* @var string
*/
protected $crudLabelColumn = 'ref';
/**
* Crud value column
*
* @var string
*/
protected $crudValueColumn = 'id';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'ref', 'firstname', 'lastname',
'object', 'tasks', 'total', 'total_days',
'comment', 'show_days', 'signed_date',
'sent_client_date', 'sent_accounting_date',
'site_id', 'company_id', 'invoiced_to_id'
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'tasks' => 'array'
];
/**
* Invoice
*
* @param array $attributes
*/
public function __construct(array $attributes = [])
{
self::$editableProperties = [
'ref' => [
'label' => trans('Réference'),
'type' => CrudType::string(),
'nullable' => false,
'actions' => [
CrudAction::viewAny(),
CrudAction::view()
]
],
'firstname' => [
'label' => trans('Prénom'),
'type' => CrudType::string(),
'nullable' => false,
'actions' => [
CrudAction::view(),
CrudAction::create(),
CrudAction::update()
]
],
'lastname' => [
'label' => trans('Nom'),
'type' => CrudType::string(),
'nullable' => false,
'actions' => [
CrudAction::view(),
CrudAction::create(),
CrudAction::update()
]
],
'object' => [
'label' => trans('Object'),
'type' => CrudType::string(),
'nullable' => false,
'actions' => [
// * Truncated on view any
CrudAction::viewAny(),
CrudAction::view(),
CrudAction::create(),
CrudAction::update()
]
],
'tasks' => [
'label' => trans('Tâches'),
'type' => CrudType::json(),
'nullable' => false,
'actions' => [
CrudAction::view(),
CrudAction::create(),
CrudAction::update()
]
],
'total' => [
'label' => trans('Montant total'),
'type' => CrudType::float(),
'nullable' => false,
'actions' => [
CrudAction::view(),
CrudAction::create(),
CrudAction::update()
]
],
'total_days' => [
'label' => trans('Montant total jours'),
'type' => CrudType::float(),
'nullable' => false,
'actions' => [
CrudAction::view(),
CrudAction::create(),
CrudAction::update()
]
],
'comment' => [
'label' => trans('Commentaire'),
'type' => CrudType::text(),
'nullable' => true,
'actions' => [
CrudAction::view(),
CrudAction::create(),
CrudAction::update()
]
],
'show_days' => [
'label' => trans('Afficher les jours'),
'type' => CrudType::boolean(),
'nullable' => false,
'actions' => [
CrudAction::view(),
CrudAction::create(),
CrudAction::update()
]
],
'signed_date' => [
'label' => trans('Date de signature'),
'type' => CrudType::datetime(),
'nullable' => true,
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
CrudAction::create(),
CrudAction::update()
]
],
'sent_client_date' => [
'label' => trans('Date d\'envoi client'),
'type' => CrudType::datetime(),
'nullable' => true,
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
CrudAction::create(),
CrudAction::update()
]
],
'sent_accounting_date' => [
'label' => trans('Date d\'envoi compta'),
'type' => CrudType::datetime(),
'nullable' => true,
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
CrudAction::create(),
CrudAction::update()
]
],
'site_id' => [
'label' => trans('models.classes.sites'),
'type' => CrudType::belongsTo(),
'belongsTo' => Site::class,
'belongsToLabel' => 'site',
'nullable' => false,
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
CrudAction::create(),
CrudAction::update()
]
],
'company_id' => [
'label' => trans('models.classes.companies'),
'type' => CrudType::belongsTo(),
'belongsTo' => Company::class,
'belongsToLabel' => 'companie',
'nullable' => false,
'actions' => [
CrudAction::view(),
CrudAction::create(),
CrudAction::update()
]
],
'invoiced_to_id' => [
'label' => trans('models.classes.companies'),
'type' => CrudType::belongsTo(),
'belongsTo' => Company::class,
'belongsToLabel' => 'entité',
'nullable' => false,
'actions' => [
CrudAction::view(),
CrudAction::create(),
CrudAction::update()
]
],
'created_at' => [
'label' => trans('Crée le'),
'type' => CrudType::datetime(),
'nullable' => false,
'actions' => [
CrudAction::view(),
]
],
'updated_at' => [
'label' => trans('Mis à jour'),
'type' => CrudType::datetime(),
'nullable' => false,
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
]
],
];
parent::__construct($attributes);
}
/**
* The attributes that are mass assignable.
*
* @return void
*/
protected static function booted()
{
static::creating(function (self $model) {
$model->ref = \sprintf(
'%s_%s_%s',
Carbon::now()->isoFormat('Y-m'),
Str::padLeft(Invoice::whereYear('created_at', Carbon::now()->isoFormat('Y'))
->whereMonth('created_at', Carbon::now()->isoFormat('M'))
->count() ?: 1, 4, '0'),
Str::substr(Str::upper(Str::slug(User::query()->inRandomOrder()->first()->name)), 0, 2)
);
});
}
// * METHODS
/**
* Get the tasks line structure skel
*
* @return array<string,string|float>
*/
public static function getTaskStructure(): array
{
return [
// * string
'desc' => '',
// * usigned float
'days' => '',
// * Invoice\TaskType
'type' => '',
// * usigned float
'amount' => '',
];
}
// * RELATIONSHIPS
/**
* BelongsTo Site
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function site()
{
return $this->BelongsTo(Site::class);
}
/**
* BelongsTo Company
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function company()
{
return $this->BelongsTo(Company::class);
}
/**
* BelongsTo Company
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function companyInvoiced()
{
return $this->BelongsTo(Company::class, 'invoiced_to_id');
}
}