ldc-supportweb/app/Models/Invoice.php
2023-08-14 17:48:26 +00:00

255 lines
7.8 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Invoices\TaskTypeEnum;
use App\Enums\Invoices\InvoiceStatusEnum;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use Mccarlosen\LaravelMpdf\LaravelMpdf;
/**
* @property string $ref
* @property string $firstname
* @property string $lastname
* @property string $object
* @property string $tasks
* @property double $total
* @property double $total_days
* @property string $comment
* @property boolean $show_days
* @property date $signed_date
* @property date $sent_client_date
* @property date $sent_accounting_date
* @property Site $site_id
* @property Company $company_id
* @property Company $invoiced_to_id
* @property InvoiceStatusEnum $status
*/
class Invoice extends Model
{
use HasFactory;
/**
* The attributes that are fillable.
*
* @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',
'status',
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'tasks' => 'array',
'signed_date' => 'date',
'sent_client_date' => 'date',
'sent_accounting_date' => 'date',
'status' => InvoiceStatusEnum::class,
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::creating(function (self $model) {
$model->ref = \sprintf(
'%s_%s_%s',
Carbon::now()->isoFormat('YYYY-MM'),
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(Auth::user()->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);
});
$model->status = InvoiceStatusEnum::opened;
});
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);
});
});
static::deleting(function () {
throw new \Error('Action impossible');
});
}
// * 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\TaskTypeEnum value
'type' => TaskTypeEnum::web,
// * 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 \Mccarlosen\LaravelMpdf\LaravelMpdfWrapper */
$mpdf = app('laravel-mpdf');
/** @var \Mccarlosen\LaravelMpdf\LaravelMpdf */
$pdf = $mpdf->loadView(
'bo.pages.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;
}
/**
* Get the invoice status array depending to the dates entered.
*
* @param \App\Models\Invoice $invoice
* @return array
*/
public static function getInvoiceStatusArray(Invoice $invoice): array
{
$invoiceStatusArray = [];
if ($invoice->signed_date && $invoice->sent_client_date && $invoice->sent_accounting_date) {
return array_merge($invoiceStatusArray, [
InvoiceStatusEnum::toArray()['opened'],
InvoiceStatusEnum::toArray()['archived'],
]);
}
if ($invoice->signed_date) {
return array_merge($invoiceStatusArray, [
InvoiceStatusEnum::toArray()['opened']
]);
}
if ($invoice->sent_client_date) {
return array_merge($invoiceStatusArray, [
InvoiceStatusEnum::toArray()['opened'],
InvoiceStatusEnum::toArray()['refused'],
]);
}
return array_merge($invoiceStatusArray, [
InvoiceStatusEnum::toArray()['opened'],
InvoiceStatusEnum::toArray()['canceled'],
]);
}
}