ldc-supportweb/app/Models/Invoice.php
2022-08-30 14:54:53 +02:00

390 lines
12 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Invoice\TaskType;
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;
use Meneses\LaravelMpdf\LaravelMpdf;
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',
'signed_date' => 'date',
'sent_client_date' => 'date',
'sent_accounting_date' => 'date',
];
/**
* 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,
'readonly' => true,
'actions' => [
CrudAction::view(),
]
],
'total_days' => [
'label' => trans('Montant total jours'),
'type' => CrudType::float(),
'nullable' => false,
'readonly' => true,
'actions' => [
CrudAction::view(),
]
],
'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,
'readonly' => true,
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
]
],
'sent_client_date' => [
'label' => trans('Date d\'envoi client'),
'type' => CrudType::datetime(),
'nullable' => true,
'readonly' => true,
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
]
],
'sent_accounting_date' => [
'label' => trans('Date d\'envoi compta'),
'type' => CrudType::datetime(),
'nullable' => true,
'readonly' => true,
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
]
],
'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('Entité à facturer'),
'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('YYYY'),
Str::padLeft((Invoice::whereYear('created_at', Carbon::now()->isoFormat('YYYY'))
->whereMonth('created_at', Carbon::now()->isoFormat('MM'))
->count() + 1) ?: 1, 4, '0'),
Str::substr(Str::upper(Str::slug(User::query()->inRandomOrder()->first()->name)), 0, 1)
);
$model->total_days = collect($model->tasks)
/** @var null|float $previous */
->reduce(function ($previous, array $task) {
return \floatval($task['days']) + \floatval($previous);
});
$model->total = collect($model->tasks)
/** @var null|float $previous */
->reduce(function ($previous, array $task) {
return \floatval($task['amount']) + \floatval($previous);
});
});
static::updating(function (self $model) {
$model->total_days = collect($model->tasks)
/** @var null|float $previous */
->reduce(function ($previous, array $task) {
return \floatval($task['days']) + \floatval($previous);
});
$model->total = collect($model->tasks)
/** @var null|float $previous */
->reduce(function ($previous, array $task) {
return \floatval($task['amount']) + \floatval($previous);
});
});
}
// * 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');
}
// * METHODS
/**
* Get the tasks line structure skel
*
* @return array<string,string|float>
*/
public static function getTaskStructure(): array
{
return [
// * string
'desc' => '',
// * usigned float
'days' => 0,
// * Invoice\TaskType value
'type' => TaskType::web()->value,
// * usigned float
'amount' => 0,
// * package ID if task type package or null
'package' => null
];
}
/**
* Generate Invoice
*
* @return LaravelMpdf
*/
public function generateInvoice(): LaravelMpdf
{
/** @var \Illuminate\Database\Eloquent\Collection<\App\Models\Package> */
$packages = Package::get()->keyBy('id')->all();
/** @var \Meneses\LaravelMpdf\LaravelMpdfWrapper */
$mpdf = app('mpdf.wrapper');
/** @var \Meneses\LaravelMpdf\LaravelMpdf */
$pdf = $mpdf->loadView(
'documents.invoice',
$this->toArray(),
[
'site' => $this->site,
'company' => $this->company,
'companyInvoiced' => $this->companyInvoiced,
'packages' => $packages,
],
[
'mode' => '',
'format' => 'A4',
'default_font_size' => '12',
'default_font' => 'sans-serif',
'margin_left' => 0,
'margin_right' => 0,
'margin_top' => 0,
'margin_bottom' => 0,
'margin_header' => 0,
'margin_footer' => 0,
'orientation' => 'P',
'title' => __('Devis Équipe Web :ref', ['ref' => $this->ref]),
'author' => 'Equipe Web',
'watermark' => '',
'show_watermark' => false,
'watermark_font' => 'sans-serif',
'display_mode' => 'fullpage',
'watermark_text_alpha' => 0.1,
'custom_font_dir' => '',
'custom_font_data' => [],
'auto_language_detection' => false,
'temp_dir' => ini_get('upload_tmp_dir') ? ini_get('upload_tmp_dir') : sys_get_temp_dir(),
'pdfa' => false,
'pdfaauto' => false,
]
);
return $pdf;
}
}