File: /var/www/vhost/disk-apps/alq-cali.bikenow.co/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;
}
}
}