A free open source IT asset/license management system
<?php
namespace App\Models;
use App\Helpers\Helper;
use App\Models\Builders\CheckoutAcceptanceQueryBuilder;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;
use TCPDF;
/**
* @mixin IdeHelperCheckoutAcceptance
*/
class CheckoutAcceptance extends Model
{
use HasFactory, Notifiable, SoftDeletes;
protected $casts = [
'accepted_at' => 'datetime',
'declined_at' => 'datetime',
'alert_on_response_id' => 'integer',
];
/**
* Get the mail recipient from the config
*
* @return mixed|string|null
*/
public function routeNotificationForMail()
{
$settings = Setting::getSettings();
$recipients = array_map('trim', explode(',', $settings->admin_cc_email ?? ''));
return array_values(array_unique(array_filter($recipients)));
}
public function getCheckoutableItemTypeAttribute(): string
{
$type = $this->checkoutable_type;
return match ($type) {
Asset::class => trans('general.asset'),
LicenseSeat::class => trans('general.license'),
Accessory::class => trans('general.accessory'),
Component::class => trans('general.component'),
Consumable::class => trans('general.consumable'),
default => class_basename($type),
};
}
/**
* FMCS tenant check for the acceptance's checkoutable. The row
* itself has no company_id and does not use CompanyableTrait /
* CompanyableChildTrait, so Company::isCurrentUserHasAccess has
* to be routed through the polymorphic parent. LicenseSeat is
* company-less too, so hop up to its license. Bypasses global
* scopes on the lookup so a scope-filtered foreign row still
* resolves for the check (otherwise FMCS admins would see a
* silent true for anything their own scope filters out).
*/
public function isAccessibleToCurrentUser(): bool
{
if (!Company::isFullMultipleCompanySupportEnabled()) {
return true;
}
if (auth()->user()?->isSuperUser()) {
return true;
}
$checkoutable = $this->checkoutable_type::withoutGlobalScopes()->find($this->checkoutable_id);
if ($checkoutable instanceof LicenseSeat) {
$checkoutable = License::withoutGlobalScopes()->find($checkoutable->license_id);
}
return Company::isCurrentUserHasAccess($checkoutable);
}
/**
* Accessor for the checkoutable item's category name.
*
* @return Attribute<string|null, never>
*/
protected function checkoutableCategoryName(): Attribute
{
return Attribute::make(
get: function () {
$item = $this->checkoutable;
if ($item instanceof Asset) {
return $item->model?->category?->name;
}
if ($item instanceof LicenseSeat) {
return $item->license?->category?->name;
}
return $item->category?->name;
},
);
}
/**
* The resource that was is out
*
* @return MorphTo
*/
public function checkoutable()
{
return $this->morphTo();
}
/**
* The user that the checkoutable was checked out to
*
* @return BelongsTo
*/
public function assignedTo()
{
return $this->belongsTo(User::class);
}
/**
* Is this checkout acceptance pending?
*
* @return bool
*/
public function isPending()
{
return $this->accepted_at == null && $this->declined_at == null;
}
/**
* Was the checkoutable checked out to this user?
*
* @return bool
*/
public function isCheckedOutTo(User $user)
{
return $this->assignedTo?->is($user);
}
/**
* Finalize this acceptance as accepted. Returns false when another
* request already finalized the row (accepted or declined). The state
* transition is a compare-and-set UPDATE scoped by whereNull on both
* timestamps, so overlapping requests cannot both run the side effects.
*
* @param string $signature_filename
*/
public function accept($signature_filename, $eula = null, $filename = null, $note = null): bool
{
$now = now();
$claimed = static::query()
->where('id', $this->id)
->whereNull('accepted_at')
->whereNull('declined_at')
->update([
'accepted_at' => $now,
'signature_filename' => $signature_filename,
'stored_eula' => $eula,
'stored_eula_file' => $filename,
'note' => $note,
'updated_at' => $now,
]);
if ($claimed === 0) {
return false;
}
$this->refresh();
$this->checkoutable->acceptedCheckout($this->assignedTo, $signature_filename, $filename);
return true;
}
/**
* Finalize this acceptance as declined. Returns false when another
* request already finalized the row. The qty loop lives here (not in
* the controller) so the per-unit side effects are tied to a winning
* state transition.
*
* @param string $signature_filename
*/
public function decline($signature_filename, $note = null): bool
{
$now = now();
$claimed = static::query()
->where('id', $this->id)
->whereNull('accepted_at')
->whereNull('declined_at')
->update([
'declined_at' => $now,
'signature_filename' => $signature_filename,
'note' => $note,
'updated_at' => $now,
]);
if ($claimed === 0) {
return false;
}
$this->refresh();
$qty = max((int) ($this->qty ?? 1), 1);
for ($i = 0; $i < $qty; $i++) {
$this->checkoutable->declinedCheckout($this->assignedTo, $signature_filename);
}
return true;
}
public function newEloquentBuilder($query): CheckoutAcceptanceQueryBuilder
{
return new CheckoutAcceptanceQueryBuilder($query);
}
/**
* @return Attribute<string, never>
*/
protected function displayCheckoutableType(): Attribute
{
return Attribute::make(
get: fn (mixed $value) => strtolower(str_replace('App\Models\\', '', $this->checkoutable_type)),
);
}
protected function scopeHasFiles(Builder $query)
{
return $query->whereNotNull('signature_filename')->orWhereNotNull('stored_eula_file');
}
public function generateAcceptancePdf($data, $pdf_filename)
{
// set some language dependent data:
$lg = [];
$lg['a_meta_charset'] = 'UTF-8';
$lg['w_page'] = 'page';
$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);
$pdf->setRTL(false);
$pdf->setLanguageArray($lg);
$pdf->SetFontSubsetting(true);
$pdf->SetCreator('Snipe-IT Asset Management System');
$pdf->SetAuthor($data['assigned_to']);
$pdf->SetTitle('Asset Acceptance: '.$data['item_tag']);
$pdf->SetSubject('Asset Acceptance: '.$data['item_tag']);
$pdf->SetKeywords('Snipe-IT, assets, acceptance, eula, tos');
$pdf->SetFont('dejavusans', '', 8, '', true);
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
$pdf->AddPage();
if ($data['logo'] != null) {
$pdf->writeHTML('<img src="@'.$data['logo'].'">', true, 0, true, 0, '');
} else {
$pdf->writeHTML('<h3>'.e($data['site_name']).'</h3><br /><br />', true, 0, true, 0, 'C');
}
$pdf->Ln();
// Check for CJK in the translation string for date. (This is a good proxy for the rest of the document)
Helper::hasRtl(trans('general.date')) ? $pdf->setRTL(true) : $pdf->setRTL(false);
Helper::isCjk(trans('general.date')) ? $pdf->SetFont('cid0cs', '', 9) : $pdf->SetFont('dejavusans', '', 8, '', true);
$pdf->writeHTML(trans('general.date').': '.Helper::getFormattedDateObject(now(), 'datetime', false), true, 0, true, 0, '');
if ($data['company_name'] != null) {
$pdf->writeHTML(trans('general.company').': '.e($data['company_name']), true, 0, true, 0, '');
}
if ($data['item_tag'] != null) {
$pdf->writeHTML(trans('general.asset_tag').': '.e($data['item_tag']), true, 0, true, 0, '');
}
if ($data['item_name'] != null) {
$pdf->writeHTML(trans('general.name').': '.e($data['item_name']), true, 0, true, 0, '');
}
if ($data['item_model'] != null) {
$pdf->writeHTML(trans('general.asset_model').': '.e($data['item_model']), true, 0, true, 0, '');
}
if ($data['item_serial'] != null) {
$pdf->writeHTML(trans('admin/hardware/form.serial').': '.e($data['item_serial']), true, 0, true, 0, '');
}
if (! empty($data['custom_fields']) && is_iterable($data['custom_fields'])) {
foreach ($data['custom_fields'] as $customField) {
$label = $customField['label'] ?? null;
$value = $customField['value'] ?? null;
if (($label !== null) && ($value !== null) && ($value !== '')) {
$pdf->writeHTML(e((string) $label).': '.e((string) $value), true, 0, true, 0, '');
}
}
}
if (($data['qty'] != null) && ($data['qty'] > 1)) {
$pdf->writeHTML(trans('general.qty').': '.e($data['qty']), true, 0, true, 0, '');
}
$pdf->Ln();
$pdf->writeHTML('<hr>', true, 0, true, 0, '');
$pdf->writeHTML(trans('general.assignee').': '.e($data['assigned_to']).($data['employee_num'] ? ' ('.e($data['employee_num']).')' : ''), true, 0, true, 0, '');
if ($data['email'] != null) {
$pdf->writeHTML(trans('general.email').': '.e($data['email']), true, 0, true, 0, '');
}
$pdf->Ln();
$pdf->writeHTML('<hr>', true, 0, true, 0, '');
// $data['eula'] arrives here as pre-rendered, sanitized HTML
// (SnipeModel::getEula routes through sanitizeEulaForRender which
// runs parseEscapedMarkedown + strips img tags before returning).
// The old block-split + parseEscapedMarkedown-per-block path only
// made sense when the input was raw markdown with blank-line block
// boundaries. Running parseEscapedMarkedown on already-rendered
// HTML double-parses: Parsedown sees the block-level tags as text,
// strips them, and re-parses the remaining plaintext, which is why
// #19544 reports the PDF EULA rendering as flat text with lists /
// headings / bold lost. Regression from a434253a94 which added the
// sanitize-at-getEula step in v8.7.0 but did not update this PDF
// path.
//
// The whole EULA gets one RTL/CJK detection pass (looking at the
// rendered HTML is fine for the heuristic - the RTL/CJK codepoints
// aren't affected by the HTML tags around them). Mixed-script EULAs
// pick the whole-document dominant script rather than the per-block
// one, which is a small edge-case regression on installs that had
// an all-Arabic EULA with a Latin heading, but WAY less broken than
// the current double-parse behavior.
Helper::hasRtl($data['eula']) ? $pdf->setRTL(true) : $pdf->setRTL(false);
Helper::isCjk($data['eula']) ? $pdf->SetFont('cid0cs', '', 9) : $pdf->SetFont('dejavusans', '', 8, '', true);
$pdf->writeHTML($data['eula'], true, 0, true, 0, '');
$pdf->Ln();
$pdf->Ln();
$pdf->setRTL(false);
$pdf->Ln();
if ($data['signature'] != null) {
$pdf->writeHTML('<img src="@'.$data['signature'].'">', true, 0, true, 0, '');
$pdf->writeHTML('<hr>', true, 0, true, 0, '');
$pdf->writeHTML(e($data['assigned_to']), true, 0, true, 0, 'C');
$pdf->Ln();
}
Helper::hasRtl(trans('general.notes')) ? $pdf->setRTL(true) : $pdf->setRTL(false);
Helper::isCjk(trans('general.notes')) ? $pdf->SetFont('cid0cs', '', 9) : $pdf->SetFont('dejavusans', '', 8, '', true);
if ($data['note'] != null) {
Helper::isCjk(trans('general.notes')) ? $pdf->SetFont('cid0cs', '', 9) : $pdf->SetFont('dejavusans', '', 8, '', true);
$pdf->writeHTML(trans('general.notes').': '.e($data['note']), true, 0, true, 0, '');
$pdf->Ln();
}
$pdf->writeHTML(trans('general.assigned_date').': '.e($data['check_out_date']), true, 0, true, 0, '');
$pdf->writeHTML(trans('general.accepted_date').': '.e($data['accepted_date']), true, 0, true, 0, '');
return $pdf->Output($pdf_filename, 'S');
}
}