MOON
Server: Apache
System: Linux e2e-78-16.ssdcloudindia.net 3.10.0-1160.45.1.el7.x86_64 #1 SMP Wed Oct 13 17:20:51 UTC 2021 x86_64
User: imensosw (1005)
PHP: 7.4.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: /home/imensosw/www/mpl.imenso.co/app/Models/Order.php
<?php

namespace App\Models;

use App\Events\TicketCreated;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;
use Laravel\Scout\Searchable;

class Order extends Model
{
    use Searchable;

    protected $hidden = [
        'reference',
    ];

    protected $fillable = [
        'user_id',
        'reference',
    ];

    public function toSearchableArray()
    {
        $array = [
            'id'        => $this->id,
            'user_id'   => $this->user_id,
            'reference' => $this->reference,
            'status_id' => $this->status_id,
            'expired'   => $this->expired,
        ];

        return $array;
    }

    public $event;

    public function items()
    {
        return $this->hasMany(\App\Models\OrderItem::class, 'order_id');
    }

    public function status()
    {
        return $this->belongsTo(\App\Models\OrderStatus::class, 'status_id');
    }

    public function tickets()
    {
        return $this->hasMany(\App\Models\Ticket::class, 'order_id');
    }

    public function owner()
    {
        return $this->belongsTo(\App\Models\User::class, 'user_id');
    }

    public function addItem(OrderItem $item)
    {
        $item->order_id = $this->id;
        $item->save();
    }

    public function getSubtotal()
    {
        $subtotal = 0;

        foreach ($this->items as $item) {
            $subtotal += $item->subtotal;
        }

        return $subtotal;
    }

    public function getTotal()
    {
        $total = 0;

        foreach ($this->items as $item) {
            $total += $item->total;
        }

        return $total;
    }

    public function getTotalBookingFee()
    {
        $booking_fee = 0;

        foreach ($this->items as $item) {
            $booking_fee += $item->booking_fee;
        }

        return $booking_fee;
    }

    public function setExpired()
    {
        $this->expired = 1;
        $this->save();
    }

    public function fulfill()
    {
        if ($this->status_id == 1) {
            if ($this->expired == 0) {
                $this->updateStatus(OrderStatus::findOrFail(2));
                $this->removeReservation();

                Log::info('Payment process - Order '.$this->reference.' fulfilling...');

                $total_tickets = OrderItem::where('order_id', $this->id)->count();

                if ($this->tickets()->count() != $total_tickets) {
                    foreach ($this->items as $item) {
                        switch ($item->orderable_type) {
                            case \App\Models\TicketType::class:
                                for ($i = 1; $i <= $item->quantity; $i++) {
                                    $ticket_type = TicketType::findOrFail($item->orderable_id);
                                    $ticket = Ticket::create([
                                        'event_id'       => $ticket_type->event_id,
                                        'user_id'        => $this->owner->id,
                                        'quantity'       => 1,
                                        'ticket_type_id' => $item->orderable_id,
                                        'order_id'       => $this->id,
                                    ]);

                                    $ticket->generateBarcode();

                                    event(new TicketCreated($ticket));
                                }
                            break;
                        }
                    }

                    Log::info('Payment process - Order '.$this->reference.' fulfilled...');
                } else {
                    Log::info('Payment error - Order '.$this->reference.' tickets already created.');
                }
            } else {
                //Need to notify customer here
                Log::info('Payment error - Order '.$this->reference.' has already expired.');
            }
        } else {
            Log::info('Payment error - Order '.$this->reference.' just tried to fulfill but is already complete.');
            die('This order has always been fulfilled. Please check your ticket wallet.');
        }
    }

    public function removeReservation()
    {
        foreach (ReservedTicket::where('order_id', $this->id)->get() as $ticket) {
            $ticket->remove();
        }
    }

    public function updateStatus(OrderStatus $status)
    {
        $this->status_id = $status->id;
        $this->save();
    }

    public function hasTickets()
    {
        foreach ($this->items as $item) {
            if ($item->orderable_type == \App\Models\TicketType::class) {
                return true;
            }
        }

        return false;
    }

    public function getTicketEvent()
    {
        if (! $this->event) {
            $event_id = 0;

            if ($this->items->count() > 0) {
                foreach ($this->items as $item) {
                    if ($item->orderable_type == \App\Models\TicketType::class) {
                        $ticket_type = TicketType::findOrFail($item->orderable_id);
                        $event_id = $ticket_type->event->id;
                    }
                }

                $this->event = Event::findOrFail($event_id);
            } else {
                return false;
            }
        }

        return $this->event;
    }

    public function getCPMOrderData()
    {
        if (substr($this->getTicketEvent()->getDescriptionExtract(), 0, 48) != '') {
            $desc = substr($this->getTicketEvent()->getDescriptionExtract(), 0, 48);
        } else {
            $desc = substr($this->getTicketEvent()->getName(), 0, 48);
        }

        $data = [
            'orders' => (array) [
                (object) [
                    'shipping' => (array) [
                        (object) [
                            'name'    => $this->owner->getName(),
                            'street'  => $this->owner->address_1,
                            'street2' => $this->owner->address_2,
                            'city'    => $this->owner->city,
                            'state'   => $this->owner->county,
                            'zip'     => $this->owner->postcode,
                            'country' => (is_object($this->owner->country) ? $this->owner->country->iso_code : 'GB'),
                        ],
                    ],
                    'lineitem' => (array) [
                        (object) [
                            'product' => [
                                'sku'         => $this->getTicketEvent()->id,
                                'name'        => $this->getTicketEvent()->getName(),
                                'description' => $desc,
                                'imageurl'    => url($this->getTicketEvent()->getThumbnailSrc()),
                            ],
                            'amount' => $this->getTotal() * 100,
                            'points' => PointAdjustment::calculate([
                                'type'  => 'order',
                                'order' => $this,
                            ], false),
                            'reward'     => 1,
                            'quantity'   => $this->items->first()->quantity,
                            'additional' => (array) [
                                (object) [
                                    'description' => 'Ticket Type',
                                    'price'       => $this->items()->first()->getTicketType(),
                                ],
                                (object) [
                                    'description' => 'Cost',
                                    'price'       => $this->getSubtotal(),
                                ],
                                (object) [
                                    'description' => 'Booking Fee',
                                    'price'       => $this->getTotalBookingFee(),
                                ],
                            ],
                        ],
                    ],
                ],
            ],
        ];

        return json_encode($data);
    }
}