| Server IP : 54.233.248.239 / Your IP : 172.28.20.13 Web Server : Apache System : Linux ip-172-28-29-189 6.5.0-1014-aws #14~22.04.1-Ubuntu SMP Thu Feb 15 15:27:06 UTC 2024 x86_64 User : www-data ( 33) PHP Version : 7.2.34-43+ubuntu22.04.1+deb.sury.org+1 Disable Function : 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, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/html/vinumday2_0/app/Events/ |
Upload File : |
<?php
namespace App\Events;
use App\Modules\PedidoEntrega\Models\PedidoEntrega;
use App\Modules\PedidoEntregaHasStatus\Models\PedidoEntregaHasStatus;
use App\Modules\Adega\Models\Adega;
use App\Modules\Empresa\Models\Empresa;
use Notification;
use App\Notifications\NotificacaoAdmin;
use App\User;
use Sentinel;
use App\Services\GeralService;
use Illuminate\Support\Facades\Log;
use DB;
class PedidoEntregaEvent
{
/**
* Listen to the User created event.
*
* @param User $user
* @return void
*/
public function created(PedidoEntrega $pedidoEntrega)
{
$adegaAberta = Adega::with('pedidosAprovados.pedidoVenda')->find($pedidoEntrega->id_adega);
if (isset($adegaAberta)) {
$adegaAberta->data_fechamento = date('Y-m-d H:i:s');
$adegaAberta->status = 'F';
$adegaAberta->save();
Log::info('adegaAberta->pedidosPreVenda' . count($adegaAberta->pedidosPreVenda));
Log::info('adegaAberta->pedidosPendentes' . count($adegaAberta->pedidosPendentes));
Log::info('adegaAberta->pedidosAprovados' . count($adegaAberta->pedidosAprovados));
Log::info('adegaAberta->pedidosParciais' . count($adegaAberta->pedidosParciais));
if(count($adegaAberta->pedidosPendentes) || count($adegaAberta->pedidosPreVenda)){
$adega = new Adega();
$adega->id_cliente = $adegaAberta->id_cliente;
if($adegaAberta->confraria_id){
$adega->confraria_id = $adegaAberta->confraria_id;
}
if($adegaAberta->cadastro_endereco_id){
$adega->cadastro_endereco_id = $adegaAberta->cadastro_endereco_id;
}
$adega->status = 'A';
$adega->data_abertura = date('Y-m-d H:i:s');
$adega->quantidade_produtos = 0;
$adega->quantidade_pedidos = 0;
$adega->valor_total = 0;
$adega->save();
Log::info('start:pedidosPendentes,PreVenda');
$updateAdegaPedidosPendentes = "UPDATE adega_has_pedido_venda SET id_adega = {$adega->id} WHERE id_pedido_venda IN (SELECT id FROM pedido_venda WHERE id_status_pedido_venda IN (1,9) AND id_cliente = {$adega->id_cliente} AND pedido_venda.confraria_id IS NULL)";
if($adega->confraria_id){
$updateAdegaPedidosPendentes = "UPDATE adega_has_pedido_venda SET id_adega = {$adega->id} WHERE id_pedido_venda IN (SELECT id FROM pedido_venda WHERE id_status_pedido_venda IN (1,9) AND pedido_venda.confraria_id = {$adega->confraria_id})";
}
DB::update(DB::raw($updateAdegaPedidosPendentes));
Log::info('end:pedidosPendentes,PreVenda');
}
}
//Pedidos Aprovados
$valor_desconto = 0;
Log::info('start:pedidosAprovados');
foreach ($adegaAberta->pedidosAprovados as $adegaHasPedidoVenda) {
$valor_desconto += abs($adegaHasPedidoVenda->pedidoVenda->ajuste);
$adegaHasPedidoVenda->pedidoVenda->id_status_pedido_venda = 7;
$adegaHasPedidoVenda->pedidoVenda->save();
}
Log::info('end:pedidosAprovados');
//Pedidos ParciaisxPendentes
Log::info('start:pedidosParciais');
if(count($adegaAberta->pedidosParciais)){
$updatePedidosParciais = "UPDATE pedido_venda SET id_status_pedido_venda = 2, parcial = 0 WHERE id_cliente = {$adega->id_cliente} AND confraria_id IS NULL AND parcial = 1 AND id_status_pedido_venda = 1";
if($adega->confraria_id){
$updatePedidosParciais = "UPDATE pedido_venda SET id_status_pedido_venda = 2, parcial = 0 WHERE confraria_id = {$adega->confraria_id} AND parcial = 1 AND id_status_pedido_venda = 1";
}
DB::update(DB::raw($updatePedidosParciais));
//Atualiza Adega
Log::info('start:pedidosParciais:recalcularAdega');
if($adega){
GeralService::recalcularAdega($adega->id_cliente, FALSE, $adega->id, $adega->confraria_id);
}
Log::info('end:pedidosParciais:recalcularAdega');
}
Log::info('end:pedidosParciais');
$user = Sentinel::getUser();
$user_id = (!$user) ? 1 : $user->id;
$novoStatus = new PedidoEntregaHasStatus();
$novoStatus->id_pedido_entrega_status = 1;
$novoStatus->id_pedido_entrega = $pedidoEntrega->id;
$novoStatus->data = date('Y-m-d H:i:s');
$novoStatus->notificar_cliente = 1;
$novoStatus->user_id = $user_id;
$novoStatus->observacao = 'Pedido solicitado pelo sistema';
$novoStatus->save();
Log::info('end:novoStatusPedidoEntrega');
$Empresa = FALSE;
if($pedidoEntrega->adega->cliente->empresa_id){
$Empresa = Empresa::find($pedidoEntrega->adega->cliente->empresa_id);
}
if(!$Empresa)
$Empresa = Empresa::default();
$pedidoEntrega->empresa_id = $Empresa->id;
$pedidoEntrega->save();
if($pedidoEntrega->disponibilidade == 1){
$result = GeralService::gerarPedidoTiny($pedidoEntrega, $valor_desconto);
Log::info('PedidoEntregaEvent:created gerarPedidoTiny - ' . json_encode($result));
}
GeralService::recalcularAdega($pedidoEntrega->adega->id_cliente, FALSE, FALSE, $pedidoEntrega->confraria_id);
/* Notificacao * /
$admins = User::join('sis_role_users', 'sis_users.id', '=', 'sis_role_users.user_id')->where('sis_role_users.role_id', 1)->get();
$titulo = $pedidoEntrega->adega->cliente->nome.' '.$pedidoEntrega->adega->cliente->sobrenome.' solicitou uma entrega';
Notification::send($admins, new NotificacaoAdmin($titulo, '', url('admin/pedido-entrega/edit/'.$pedidoEntrega->slug)));*/
}
/**
* Listen to the User deleting event.
*
* @param User $user
* @return void
*/
public function deleting(PedidoEntrega $pedidoEntrega)
{
//
}
/**
* Listen to the User created event.
*
* @param User $user
* @return void
*/
public function updated(PedidoEntrega $pedidoEntrega)
{
}
/**
* Listen to the User deleting event.
*
* @param User $user
* @return void
*/
public function updating(PedidoEntrega $pedidoEntrega)
{
//
}
/**
* Listen to the User created event.
*
* @param User $user
* @return void
*/
public function saved(PedidoEntrega $pedidoEntrega)
{
}
/**
* Listen to the User deleting event.
*
* @param User $user
* @return void
*/
public function saving(PedidoEntrega $pedidoEntrega)
{
//
}
}