Skip to content

A free open source IT asset/license management system

4.4 KB
Raw Blame
<?php

namespace App\Mail;

use App\Models\Accessory;
use App\Models\Asset;
use App\Models\Location;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailables\Address;
use Illuminate\Mail\Mailables\Attachment;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class CheckoutAccessoryMail extends BaseMailable
{
    use Queueable, SerializesModels;

    private Accessory $item;

    private User $admin;

    private mixed $note;

    private mixed $checkout_qty;

    private mixed $target;

    private mixed $acceptance;

    private bool $firstTimeSending;

    /**
     * Create a new message instance.
     */
    public function __construct(Accessory $accessory, $checkedOutTo, User $checkedOutBy, $acceptance, $note, bool $firstTimeSending = true)
    {
        $this->item = $accessory;
        $this->admin = $checkedOutBy;
        $this->note = $note;
        $this->checkout_qty = $accessory->checkout_qty;
        $this->target = $checkedOutTo;
        $this->acceptance = $acceptance;
        $this->firstTimeSending = $firstTimeSending;
    }

    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        $from = new Address(config('mail.from.address'), config('mail.from.name'));

        return new Envelope(
            from: $from,
            subject: $this->getSubject(),
        );
    }

    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        Log::debug($this->item->getImageUrl());
        $eula = $this->item->getEula();
        $req_accept = $this->item->requireAcceptance();
        $accept_url = is_null($this->acceptance) ? null : route('account.accept.item', $this->acceptance);
        $name = null;

        if ($this->target instanceof User) {
            $name = $this->target->display_name;
        } elseif ($this->target instanceof Asset) {
            $name = $this->target->assignedto?->display_name;
        } elseif ($this->target instanceof Location) {
            $name = $this->target->manager->name;
        }

        return new Content(
            markdown: 'mail.markdown.checkout-accessory',
            with: [
                'item' => $this->item,
                'admin' => $this->admin,
                'note' => $this->note,
                'target' => $name,
                'eula' => $eula,
                'req_accept' => $req_accept,
                'accept_url' => $accept_url,
                'checkout_qty' => $this->checkout_qty,
                'location' => $this->getLocation(),
                'introduction_line' => $this->introductionLine(),
            ],
        );
    }

    private function introductionLine(): string
    {
        if ($this->target instanceof Location) {
            return trans_choice('mail.new_item_checked_location', $this->checkout_qty, ['location' => $this->target->name]);
        }

        if ($this->firstTimeSending && $this->requiresAcceptance()) {
            return trans_choice('mail.new_item_checked_with_acceptance', $this->checkout_qty);
        }

        if ($this->firstTimeSending && ! $this->requiresAcceptance()) {
            return trans_choice('mail.new_item_checked', $this->checkout_qty);
        }

        if (! $this->firstTimeSending && $this->requiresAcceptance()) {
            return trans('mail.recent_item_checked');
        }

        // we shouldn't get here but let's send a default message just in case
        return trans('new_item_checked');
    }

    private function requiresAcceptance(): bool
    {
        return $this->item->requireAcceptance();
    }

    /**
     * Get the attachments for the message.
     *
     * @return array<int, Attachment>
     */
    public function attachments(): array
    {
        return [];
    }

    private function getSubject(): string
    {
        if ($this->firstTimeSending) {
            return trans_choice('mail.Accessory_Checkout_Notification', $this->checkout_qty);
        }

        return trans('mail.unaccepted_asset_reminder');
    }

    private function getLocation(): ?string
    {
        if ($this->target instanceof Asset || $this->target instanceof User) {
            return $this->target->location?->name;
        }

        if ($this->target instanceof Location) {
            return $this->target->name;
        }

        return null;
    }
}