HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/shop.komma.nl/app/Orders/Mail/OrderStatusUpdatedCustomer.php
<?php

namespace App\Orders\Mail;

use App\Checkout\CheckoutService;
use App\Invoicing\CreditInvoiceService;
use App\Invoicing\InvoiceService;
use App\Orders\CreditInvoiceNumberSequence;
use App\Orders\Kms\OrderService;
use App\Orders\Models\Order;
use App\Orders\OrderStatus;
use App\Payment\PaymentService;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Lang;
use Komma\KMS\Globalization\RegionInfo;
use Komma\KMS\Globalization\RegionInfoInterface;
use Komma\KMS\Users\Genders;

/**
 * Class OrderStatusUpdatedCustomer
 *
 * A mail telling a customer that his order was updated
 *
 * @package App\Orders\Mail
 */
class OrderStatusUpdatedCustomer extends Mailable
{
    use Queueable, SerializesModels;

    private Order $order;
    private InvoiceService $invoiceService;
    private RegionInfo $regionInfo;
    private PaymentService $paymentService;
    private OrderService $orderService;
    private CreditInvoiceNumberSequence $creditInvoiceNumberGenerator;
    private CreditInvoiceService $creditInvoiceService;
    private CheckoutService $checkoutService;

    /**
     * Create a new message instance.
     *
     * @param Order $order
     * @see OrderStatus
     */
    public function __construct(Order $order)
    {
        $this->invoiceService = new InvoiceService();
        $this->order = $order;
        $this->regionInfo = app(RegionInfoInterface::class);
        $this->paymentService = new PaymentService();
        $this->orderService = new OrderService();
        $this->checkoutService = new CheckoutService();

        $this->creditInvoiceNumberGenerator = app(CreditInvoiceNumberSequence::class);
        $this->creditInvoiceService = app(CreditInvoiceService::class);
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        //Verify translations
        if(Lang::has('shop/orders.mail.'.$this->order->status.'.customer'))
            throw new \RuntimeException('Could not build a mailable for a customers order with an order status of: '.$this->order->status.'. The translations are not present. Most likely by design.');

        //Prepare mail view data
        $data = [
            'order'         => $this->order,
            'vatRateTotals' => $this->checkoutService->getVatRateTotalsFromOrder($this->order),
            'greeting'      => __('form.form.salutation.informal.'.Genders::Unknown.'.with_salutation.short').' '.$this->order->customer->getDisplayName(),
            'body'          => __('KMS::orders.mail.'.$this->order->status.'.customer.body', ['order_number' => $this->order->order_number]),
            'button_text'   => __('KMS::orders.mail.'.$this->order->status.'.customer.button_text'),
            'closure'       => __('KMS::orders.mail.'.$this->order->status.'.customer.closure'),
            'showTotal'     => true,
            'name'          => config('site.company.name'),
            'regionInfo'    => $this->regionInfo
        ];

        //On certain order statuses we include order details.
        $data['show_order_details'] = (
            $this->order->status === OrderStatus::PENDING ||
            $this->order->status === OrderStatus::AWAITING_FULFILLMENT
        );

        //Add the button url to the view parameters
        switch ($this->order->status) {
            case OrderStatus::CANCELED:
                $this->paymentService->getAdapter()->createTransaction($this->order); //Create a new transaction that the user can pay.
                $data['button_url'] = $this->orderService->getManualPaymentUrl($this->order);
                break;
            case OrderStatus::REFUNDED:
            case OrderStatus::DISPUTED:
                if(!$this->order->credit_invoice_number) {
                    $this->order->credit_invoice_number = $this->creditInvoiceNumberGenerator->next();
                    $this->order->save();
                    $this->creditInvoiceService->getForOrderAsPDF($this->order); //Generates the pdf (caches it) and returns it.
                }
                break;
            case OrderStatus::PENDING:
            case OrderStatus::AWAITING_FULFILLMENT:
                $data['button_url'] = url('/');
                break;
        }

        //Set the mail subject
        if($this->order->status !== OrderStatus::PENDING) {
            $this->subject(__('KMS::orders.mail.' . $this->order->status . '.customer.subject', [
                'order_number' => $this->order->order_number,
                'order_status' => strtolower(__('KMS::orders.status.' . $this->order->status)),
            ]));
        } else {
            $this->subject(__('KMS::orders.mail.' . $this->order->status . '.customer.subject', [
                'order_number' => $this->order->order_number,
                'order_status' => strtolower(__('KMS::orders.status.' . $this->order->status)),
            ]));
        }

        //Create the view and add attachments if needed
        $view = $this->view('emails.shop.orderUpdateCustomer', $data);
        if($this->order->status === OrderStatus::PENDING) {
            //Add the invoice
            $view->attachFromStorage($this->invoiceService->getPDFPathFromOrder($this->order, true), $this->order->order_number.'.pdf');
        } else if(in_array($this->order->status, [OrderStatus::DISPUTED, OrderStatus::REFUNDED])) {
            $view->attachFromStorage($this->creditInvoiceService->getPDFPathFromOrder($this->order, true), $this->order->order_number.'.pdf');
        }

        return $view;
    }
}