HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux ip-172-31-42-149 5.15.0-1084-aws #91~20.04.1-Ubuntu SMP Fri May 2 07:00:04 UTC 2025 aarch64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/vhost/disk-apps/qas.sports-crowd.com/app/Order.php
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\User;
use App\OrderState;
use App\Sucursal;
use App\Address;
use App\Core\Order\OrderStatusEnum;
use App\PaymentType;
use App\OrderType;
use App\SalesChannel;
use App\LineBusiness;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Events\NewOrderCreateClient;
use App\Http\Controllers\UtilController;
use App\Http\Controllers\OrderController;

class Order extends Model
{
    use SoftDeletes;
    protected $dates = ['deleted_at'];

    protected $fillable = [
        'id',
        'sync_with_erp'
    ];

    public function client()
    {
        return $this->belongsTo(User::class, 'client_id')->withTrashed();
    }

    public function deliveryMan()
    {
        return $this->belongsTo(User::class, 'delivery_man_id')->with('userInfo')->withTrashed();
    }

    public function admin()
    {
        return $this->belongsTo(User::class, 'admin_id')->withTrashed();
    }

    public function orderState()
    {
        return $this->belongsTo(orderState::class)->select(array('id', 'name'));
    }

    public function sucursal()
    {
        return $this->belongsTo(Sucursal::class, 'sucursal_id')->withTrashed();
    }

    public function sucursalOrigin()
    {
        return $this->belongsTo(Sucursal::class, 'sucursal_origin_id')->withTrashed();
    }

    public function currentSucursal()
    {
        return $this->belongsTo(Sucursal::class, 'current_sucursal_id')->withTrashed();
    }

    public function comment()
    {
        return $this->hasMany(Comment::class);
    }

    public function address()
    {
        return $this->belongsTo(Address::class)->withTrashed();
    }

    public function transferences()
    {
        return $this->hasMany(Transfer::class);
    }

    public function paymentType()
    {
        return $this->belongsTo(PaymentType::class);
    }

    public function orderProducts()
    {
        return $this->hasMany(OrderProduct::class)->with('products', 'orderProductAttributes', 'product');
    }

    public function orderType()
    {
        return $this->belongsTo(OrderType::class);
    }

    public function channel()
    {
        return $this->belongsTo(SalesChannel::class, 'sales_channel_id')->withTrashed();
    }

    public function line()
    {
        return $this->belongsTo(LineBusiness::class, 'line_businesses_id')->withTrashed();
    }

    public function point_sale()
    {
        return $this->belongsTo(PointSale::class);
    }

    public function discountOrderUser()
    {
        return $this->hasMany(DiscountOrderUser::class, 'order_id', 'id')->with('discount');
    }

    public function discountProductUsers()
    {
        return $this->hasMany(DiscountProductUser::class, 'order_id', 'id');
    }

    public function update(array $attributes = [], array $options = [])
    {
        if (parent::getAttribute('gw_state') == OrderStatusEnum::CONFIRMED) {
            // ENVIO EMAIL A CLIENTE
            if (parent::getAttribute('status_notification_email_client') == 'pending') {
                $orderController = new OrderController();
                $order = $orderController->getOrder(parent::getAttribute('id'));
                $emailDeliveryStatus = $this->sendOrderMail(parent::getAttribute('client')->email, $order);
                if ($emailDeliveryStatus) {
                    parent::setAttribute('status_notification_email_client', 'send');
                }
            }
        }
        return parent::update($attributes, $options);
    }

    private function sendOrderMail($email, $order)
    {
        try {
            event(new NewOrderCreateClient($email, $order));
            return true;
        } catch (\Exception $exx) {
            $util = new UtilController();
            $util->logFile($exx);
            return false;
        }
    }
}