File: /var/www/vhost/disk-apps/demo.sports-crowd.com/app/Http/Controllers/HomeController.php
<?php
namespace App\Http\Controllers;
use DB;
use App\AverageMonth;
use App\AverageTime;
use App\Order;
use App\Sucursal;
use App\Transfer;
use App\User;
use App\OrderType;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
class HomeController extends Controller
{
public function __construct()
{
ini_set('memory_limit', '512M');
}
public function index()
{
return redirect('/orders');
// TODO: check if it is necessary to keep this
$currentYear = date("Y");
$sucursals = Sucursal::where('active', 1)->get();
$numOrder = 0;
$numTransfer = 0;
$sucursal = Auth::user()->userInfo->sucursal->id;
$rol = Auth::user()->rol->id;
if ($rol == 1 || $rol == 6) {
// count de todo transfer y pedidos
$numOrder = count(Order::where('sucursal_id', $sucursal)->whereYear('creation_date', $currentYear)->get());
$countOrders = Order::where('sucursal_id', $sucursal)->whereYear('creation_date', $currentYear)->get();
foreach ($countOrders as $key => $order) {
$numTransfer = $numTransfer + count(Transfer::where('order_id', $order->id)->get());
}
$ecommerce = OrderType::where('id', 5)->first();
$ecommerceVerif = false;
if ($ecommerce->active == 1) {
$ecommerceVerif = true;
}
$average = AverageTime::groupBy('year')->select('year')->get();
return view('home')
->with('deliveryMan', count(User::where('rol_id', 3)->get()))
->with('transfer', $numTransfer)
->with('orders', $numOrder)
->with('clients', count(User::where('rol_id', 4)->get()))
->with('average', $average)
->with('currentYear', $currentYear)
->with('sucursals', $sucursals)
->with('ecommerceVerif', $ecommerceVerif);
} else {
$average = AverageTime::groupBy('year')->where('sucursal_id', $sucursal)->select('year')->get();
return view('home')
->with('deliveryMan', count(User::where('rol_id', 3)->leftJoin('user_informations', function ($join) {
$join->on('users.id', '=', 'user_informations.user_id');
})->get()))
->with('transfer', count(Transfer::where('sucursal_id', $sucursal)->get()))
->with('orders', count(Order::where('sucursal_id', $sucursal)->get()))
->with('clients', count(User::where('rol_id', 4)->leftJoin('user_informations', function ($join) {
$join->on('users.id', '=', 'user_informations.user_id');
})->get()))
->with('average', $average)
->with('currentYear', $currentYear)
->with('sucursals', $sucursals);
}
}
public function states()
{
$id = Auth::user()->userInfo->sucursal->id;
$pending = count(Order::where('order_state_id', 1)->where('sucursal_id', $id)->get());
$accepted = count(Order::where('order_state_id', 2)->where('sucursal_id', $id)->get());
$collected = count(Order::where('order_state_id', 3)->where('sucursal_id', $id)->get());
$route = count(Order::where('order_state_id', 4)->where('sucursal_id', $id)->get());
$delivered = count(Order::where('order_state_id', 5)->where('sucursal_id', $id)->get());
$finalized = count(Order::where('order_state_id', 6)->where('sucursal_id', $id)->get());
$canceled = count(Order::where('order_state_id', 7)->where('sucursal_id', $id)->get());
$res[] = array(
'pending' => $pending,
'accepted' => $accepted,
'collected' => $collected,
'route' => $route,
'delivered' => $delivered,
'finalized' => $finalized,
'canceled' => $canceled
);
$dres = json_encode($res);
return json_decode($dres, true);
}
public function average()
{
$currentYear = date("Y");
$id = Auth::user()->userInfo->sucursal->id;
$months = AverageTime::where('year', $currentYear)->where('sucursal_id', $id)->get();
$january = new AverageTime();
$february = new AverageTime();
$march = new AverageTime();
$april = new AverageTime();
$may = new AverageTime();
$june = new AverageTime();
$july = new AverageTime();
$august = new AverageTime();
$september = new AverageTime();
$october = new AverageTime();
$november = new AverageTime();
$december = new AverageTime();
foreach ($months as $key => $month) {
if ($month->month_number == 1) {
$january = $month;
}
if ($month->month_number == 2) {
$february = $month;
}
if ($month->month_number == 3) {
$march = $month;
}
if ($month->month_number == 4) {
$april = $month;
}
if ($month->month_number == 5) {
$may = $month;
}
if ($month->month_number == 6) {
$june = $month;
}
if ($month->month_number == 7) {
$july = $month;
}
if ($month->month_number == 8) {
$august = $month;
}
if ($month->month_number == 9) {
$september = $month;
}
if ($month->month_number == 10) {
$october = $month;
}
if ($month->month_number == 11) {
$november = $month;
}
if ($month->month_number == 12) {
$december = $month;
}
}
$res[] = array(
'january' => array('time' => $january->average_time, 'sale' => $january->order_quantity),
'february' => array('time' => $february->average_time, 'sale' => $february->order_quantity),
'march' => array('time' => $march->average_time, 'sale' => $march->order_quantity),
'april' => array('time' => $april->average_time, 'sale' => $april->order_quantity),
'may' => array('time' => $may->average_time, 'sale' => $may->order_quantity),
'june' => array('time' => $june->average_time, 'sale' => $june->order_quantity),
'july' => array('time' => $july->average_time, 'sale' => $july->order_quantity),
'august' => array('time' => $august->average_time, 'sale' => $august->order_quantity),
'september' => array('time' => $september->average_time, 'sale' => $september->order_quantity),
'october' => array('time' => $october->average_time, 'sale' => $october->order_quantity),
'november' => array('time' => $november->average_time, 'sale' => $november->order_quantity),
'december' => array('time' => $december->average_time, 'sale' => $december->order_quantity),
);
$dres = json_encode($res);
return json_decode($dres, true);
}
public function statesYear($year)
{
$id = Auth::user()->userInfo->sucursal->id;
$pending = count(Order::where('order_state_id', 1)->where('sucursal_id', $id)->whereYear('creation_date', $year)->get());
$accepted = count(Order::where('order_state_id', 2)->where('sucursal_id', $id)->whereYear('creation_date', $year)->get());
$collected = count(Order::where('order_state_id', 3)->where('sucursal_id', $id)->whereYear('creation_date', $year)->get());
$route = count(Order::where('order_state_id', 4)->where('sucursal_id', $id)->whereYear('creation_date', $year)->get());
$delivered = count(Order::where('order_state_id', 5)->where('sucursal_id', $id)->whereYear('creation_date', $year)->get());
$finalized = count(Order::where('order_state_id', 6)->where('sucursal_id', $id)->whereYear('creation_date', $year)->get());
$canceled = count(Order::where('order_state_id', 7)->where('sucursal_id', $id)->whereYear('creation_date', $year)->get());
$res[] = array(
'pending' => $pending,
'accepted' => $accepted,
'collected' => $collected,
'route' => $route,
'delivered' => $delivered,
'finalized' => $finalized,
'canceled' => $canceled
);
$dres = json_encode($res);
return json_decode($dres, true);
}
public function averageYear($year)
{
$id = Auth::user()->userInfo->sucursal->id;
$months = AverageTime::where('year', $year)->where('sucursal_id', $id)->get();
$january = new AverageTime();
$february = new AverageTime();
$march = new AverageTime();
$april = new AverageTime();
$may = new AverageTime();
$june = new AverageTime();
$july = new AverageTime();
$august = new AverageTime();
$september = new AverageTime();
$october = new AverageTime();
$november = new AverageTime();
$december = new AverageTime();
foreach ($months as $key => $month) {
if ($month->month_number == 1) {
$january = $month;
}
if ($month->month_number == 2) {
$february = $month;
}
if ($month->month_number == 3) {
$march = $month;
}
if ($month->month_number == 4) {
$april = $month;
}
if ($month->month_number == 5) {
$may = $month;
}
if ($month->month_number == 6) {
$june = $month;
}
if ($month->month_number == 7) {
$july = $month;
}
if ($month->month_number == 8) {
$august = $month;
}
if ($month->month_number == 9) {
$september = $month;
}
if ($month->month_number == 10) {
$october = $month;
}
if ($month->month_number == 11) {
$november = $month;
}
if ($month->month_number == 12) {
$december = $month;
}
}
$res[] = array(
'january' => array('time' => $january->average_time, 'sale' => $january->order_quantity),
'february' => array('time' => $february->average_time, 'sale' => $february->order_quantity),
'march' => array('time' => $march->average_time, 'sale' => $march->order_quantity),
'april' => array('time' => $april->average_time, 'sale' => $april->order_quantity),
'may' => array('time' => $may->average_time, 'sale' => $may->order_quantity),
'june' => array('time' => $june->average_time, 'sale' => $june->order_quantity),
'july' => array('time' => $july->average_time, 'sale' => $july->order_quantity),
'august' => array('time' => $august->average_time, 'sale' => $august->order_quantity),
'september' => array('time' => $september->average_time, 'sale' => $september->order_quantity),
'october' => array('time' => $october->average_time, 'sale' => $october->order_quantity),
'november' => array('time' => $november->average_time, 'sale' => $november->order_quantity),
'december' => array('time' => $december->average_time, 'sale' => $december->order_quantity),
);
$dres = json_encode($res);
return json_decode($dres, true);
}
public function stateMonth($year, $month, $sucursal)
{
$id = Auth::user()->userInfo->sucursal->id;
$rol = Auth::user()->rol->id;
if ($sucursal == "ec") {
$sucursal = null;
}
if ($rol == 1 || $rol == 6) {
if ($sucursal == 'todos' && $month != "ninguna") {
$pending = count(Order::where('order_state_id', 1)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$accepted = count(Order::where('order_state_id', 2)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$collected = count(Order::where('order_state_id', 3)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$route = count(Order::where('order_state_id', 4)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$delivered = count(Order::where('order_state_id', 5)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$finalized = count(Order::where('order_state_id', 6)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$canceled = count(Order::where('order_state_id', 7)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
} else {
if ($month == "ninguna" && $sucursal == "todos") {
$pending = count(Order::where('order_state_id', 1)->whereYear('creation_date', $year)->get());
$accepted = count(Order::where('order_state_id', 2)->whereYear('creation_date', $year)->get());
$collected = count(Order::where('order_state_id', 3)->whereYear('creation_date', $year)->get());
$route = count(Order::where('order_state_id', 4)->whereYear('creation_date', $year)->get());
$delivered = count(Order::where('order_state_id', 5)->whereYear('creation_date', $year)->get());
$finalized = count(Order::where('order_state_id', 6)->whereYear('creation_date', $year)->get());
$canceled = count(Order::where('order_state_id', 7)->whereYear('creation_date', $year)->get());
} else {
if ($month == "ninguna") {
$pending = count(Order::where('order_state_id', 1)->where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->get());
$accepted = count(Order::where('order_state_id', 2)->where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->get());
$collected = count(Order::where('order_state_id', 3)->where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->get());
$route = count(Order::where('order_state_id', 4)->where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->get());
$delivered = count(Order::where('order_state_id', 5)->where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->get());
$finalized = count(Order::where('order_state_id', 6)->where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->get());
$canceled = count(Order::where('order_state_id', 7)->where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->get());
} else {
$pending = count(Order::where('order_state_id', 1)->where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$accepted = count(Order::where('order_state_id', 2)->where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$collected = count(Order::where('order_state_id', 3)->where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$route = count(Order::where('order_state_id', 4)->where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$delivered = count(Order::where('order_state_id', 5)->where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$finalized = count(Order::where('order_state_id', 6)->where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$canceled = count(Order::where('order_state_id', 7)->where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
}
}
}
} else {
$pending = count(Order::where('order_state_id', 1)->where('sucursal_id', $id)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$accepted = count(Order::where('order_state_id', 2)->where('sucursal_id', $id)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$collected = count(Order::where('order_state_id', 3)->where('sucursal_id', $id)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$route = count(Order::where('order_state_id', 4)->where('sucursal_id', $id)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$delivered = count(Order::where('order_state_id', 5)->where('sucursal_id', $id)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$finalized = count(Order::where('order_state_id', 6)->where('sucursal_id', $id)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$canceled = count(Order::where('order_state_id', 7)->where('sucursal_id', $id)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
}
$res[] = array(
'pending' => $pending,
'accepted' => $accepted,
'collected' => $collected,
'route' => $route,
'delivered' => $delivered,
'finalized' => $finalized,
'canceled' => $canceled
);
$dres = json_encode($res);
return json_decode($dres, true);
}
// segun el rol
public function averageMonth($year, $month, $sucursal)
{
$id = Auth::user()->userInfo->sucursal->id;
$rol = Auth::user()->rol->id;
if ($rol == 1 || $rol == 6) {
if ($sucursal == "ec") {
$sucursal = null;
}
if ($sucursal == "todos" && $month != "ninguna") {
$average = AverageMonth::where('year', $year)->where('number_month', $month)->get();
} else {
if ($month == "ninguna" && $sucursal == "todos") {
$average = AverageMonth::where('year', $year)->get();
} else {
if ($month == "ninguna") {
$average = AverageMonth::where('year', $year)->where('sucursal_id', $sucursal)->get();
} else {
$average = AverageMonth::where('year', $year)->where('sucursal_id', $sucursal)->where('number_month', $month)->get();
}
}
}
} else {
$average = AverageMonth::where('year', $year)->where('sucursal_id', $id)->where('number_month', $month)->get();
}
$res[] = array('average' => $average);
$dres = json_encode($res);
return json_decode($dres, true);
}
///select sucursal
public function averageYearAdmin($year, $sucursal)
{
$january = new AverageTime();
$february = new AverageTime();
$march = new AverageTime();
$april = new AverageTime();
$may = new AverageTime();
$june = new AverageTime();
$july = new AverageTime();
$august = new AverageTime();
$september = new AverageTime();
$october = new AverageTime();
$november = new AverageTime();
$december = new AverageTime();
$numOrder = 0;
$numTransfer = 0;
$id = $sucursal;
// llevar los datos de todas las sucursals
if ($sucursal == "todos") {
$months = AverageTime::where('year', $year)->get();
$countJanuary = 0;
$countFebruary = 0;
$countMarch = 0;
$countApril = 0;
$countMay = 0;
$countJune = 0;
$countJuly = 0;
$countAugust = 0;
$countSeptember = 0;
$countOctober = 0;
$countNovember = 0;
$countDecember = 0;
//se valida el numero del month y de acuerdo a este se van sumando los valores que se mandan
foreach ($months as $key => $month) {
if ($month->month_number == 1) {
if ($countJanuary == 0) { // igualar el month solamente una ez y evitar que se remplace
$january = $month;
} else { // se evita que se sume el valor en caso de ser un solo valor
$january->coverage_time = $january->coverage_time + $month->coverage_time;
$january->cantidad_pedidos = $january->cantidad_pedidos + $month->cantidad_pedidos;
}
$countJanuary = $countJanuary + 1; //contiene el numero de valores, dividir el total del promedio por este valor
}
if ($month->month_number == 2) {
if ($countFebruary == 0) {
$february = $month;
} else {
$february->coverage_time = $february->coverage_time + $month->coverage_time;
$february->order_quantity = $february->order_quantity + $month->order_quantity;
}
$countFebruary = $countFebruary + 1;
}
if ($month->month_number == 3) {
if ($countMarch == 0) {
$march = $month;
} else {
$march->coverage_time = $march->coverage_time + $month->coverage_time;
$march->order_quantity = $march->order_quantity + $month->order_quantity;
}
$countMarch = $countMarch + 1;
}
if ($month->month_number == 4) {
if ($countApril == 0) {
$april = $month;
} else {
$april->coverage_time = $april->coverage_time + $month->coverage_time;
$april->order_quantity = $april->order_quantity + $month->order_quantity;
}
$countApril = $countApril + 1;
}
if ($month->month_number == 5) {
if ($countMay == 0) {
$may = $month;
} else {
$may->average_time = $may->average_time + $month->average_time;
$may->order_quantity = $may->order_quantity + $month->order_quantity;
}
$countMay = $countMay + 1;
}
if ($month->month_number == 6) {
if ($countJune == 0) {
$june = $month;
} else {
$june->average_time = $june->average_time + $month->average_time;
$june->order_quantity = $june->order_quantity + $month->order_quantity;
}
$countJune = $countJune + 1;
}
if ($month->month_number == 7) {
if ($countJuly == 0) {
$july = $month;
} else {
$july->average_time = $july->average_time + $month->average_time;
$july->order_quantity = $july->order_quantity + $month->order_quantity;
}
$countJuly = $countJuly + 1;
}
if ($month->month_number == 8) {
if ($countAugust == 0) {
$august = $month;
} else {
$august->average_time = $august->average_time + $month->average_time;
$august->order_quantity = $august->order_quantity + $month->order_quantity;
}
$countAugust = $countAugust + 1;
}
if ($month->month_number == 9) {
if ($countSeptember == 0) {
$september = $month;
} else {
$september->average_time = $september->average_time + $month->average_time;
$september->order_quantity = $september->order_quantity + $month->order_quantity;
}
$countSeptember = $countSeptember + 1;
}
if ($month->month_number == 10) {
if ($countOctober == 0) {
$october = $month;
} else {
$october->average_time = $october->average_time + $month->average_time;
$october->order_quantity = $october->order_quantity + $month->order_quantity;
}
$countOctober = $countOctober + 1;
}
if ($month->month_number == 11) {
if ($countNovember == 0) {
$november = $month;
} else {
$november->average_time = $november->average_time + $month->average_time;
$november->order_quantity = $november->order_quantity + $month->order_quantity;
}
$countNovember = $countNovember + 1;
}
if ($month->month_number == 12) {
if ($countDecember == 0) {
$december = $month;
} else {
$december->average_time = $december->average_time + $month->average_time;
$december->order_quantity = $december->order_quantity + $month->order_quantity;
}
$countDecember = $countDecember + 1;
}
}
// se valida que el dato exista para dividirlo por la cantidad de datos del month
if ($january->average_time != null) {
$january->average_time = $january->average_time / $countJanuary;
}
if ($february->average_time != null) {
$february->average_time = $february->average_time / $countFebruary;
}
if ($march->average_time != null) {
$march->average_time = $march->average_time / $countMarch;
}
if ($april->average_time != null) {
$april->average_time = $april->average_time / $countApril;
}
if ($may->average_time != null) {
$may->average_time = $may->average_time / $countMay;
}
if ($june->average_time != null) {
$june->average_time = $june->average_time / $countJune;
}
if ($july->average_time != null) {
$july->average_time = $july->average_time / $countJuly;
}
if ($august->average_time != null) {
$august->average_time = $august->average_time / $countAugust;
}
if ($september->average_time != null) {
$september->average_time = $september->average_time / $countSeptember;
}
if ($october->average_time != null) {
$october->average_time = $october->average_time / $countOctober;
}
if ($november->average_time != null) {
$november->average_time = $november->average_time / $countNovember;
}
if ($december->average_time != null) {
$december->average_time = $december->average_time / $countDecember;
}
// count de todo transfer y pedidos
$numOrder = count(Order::whereYear('creation_date', $year)->get());
$countOrders = Order::whereYear('creation_date', $year)->get();
foreach ($countOrders as $key => $order) {
$numTransfer = $numTransfer + count(Transfer::where('order_id', $order->id)->get());
}
} else {
if ($id == "ec") {
$months = AverageTime::where('year', $year)->whereNull('sucursal_id')->get();
// count de todo transfer y pedidos
$numOrder = count(Order::whereYear('creation_date', $year)->whereNull('sucursal_id')->get());
$countOrders = Order::whereYear('creation_date', $year)->whereNull('sucursal_id')->get();
} else {
// count de todo transfer y pedidos
$numOrder = count(Order::where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->get());
$countOrders = Order::where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->get();
$months = AverageTime::where('year', $year)->where('sucursal_id', $id)->get();
}
foreach ($months as $key => $month) {
if ($month->month_number == 1) {
$january = $month;
}
if ($month->month_number == 2) {
$february = $month;
}
if ($month->month_number == 3) {
$march = $month;
}
if ($month->month_number == 4) {
$april = $month;
}
if ($month->month_number == 5) {
$may = $month;
}
if ($month->month_number == 6) {
$june = $month;
}
if ($month->month_number == 7) {
$july = $month;
}
if ($month->month_number == 8) {
$august = $month;
}
if ($month->month_number == 9) {
$september = $month;
}
if ($month->month_number == 10) {
$october = $month;
}
if ($month->month_number == 11) {
$november = $month;
}
if ($month->month_number == 12) {
$december = $month;
}
}
foreach ($countOrders as $key => $order) {
$numTransfer = $numTransfer + count(Transfer::where('order_id', $order->id)->get());
}
}
$res[] = array(
'january' => array('time' => $january->average_time, 'sale' => $january->order_quantity),
'february' => array('time' => $february->average_time, 'sale' => $february->order_quantity),
'march' => array('time' => $march->average_time, 'sale' => $march->order_quantity),
'april' => array('time' => $april->average_time, 'sale' => $april->order_quantity),
'may' => array('time' => $may->average_time, 'sale' => $may->order_quantity),
'june' => array('time' => $june->average_time, 'sale' => $june->order_quantity),
'july' => array('time' => $july->average_time, 'sale' => $july->order_quantity),
'august' => array('time' => $august->average_time, 'sale' => $august->order_quantity),
'september' => array('time' => $september->average_time, 'sale' => $september->order_quantity),
'october' => array('time' => $october->average_time, 'sale' => $october->order_quantity),
'november' => array('time' => $november->average_time, 'sale' => $november->order_quantity),
'december' => array('time' => $december->average_time, 'sale' => $december->order_quantity),
'month' => false, 'transfer' => $numTransfer, 'orders' => $numOrder,
);
$dres = json_encode($res);
return json_decode($dres, true);
}
public function statesMonthAdmin($year, $month, $sucursal)
{
$id = Auth::user()->userInfo->sucursal->id;
$rol = Auth::user()->rol->id;
if ($sucursal == "ec") {
$sucursal = null;
}
if ($rol == 1 || $rol == 6) {
if ($sucursal == 'todos' && $month != "ninguna") {
$pending = count(Order::where('order_state_id', 1)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$accepted = count(Order::where('order_state_id', 2)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$collected = count(Order::where('order_state_id', 3)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$route = count(Order::where('order_state_id', 4)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$delivered = count(Order::where('order_state_id', 5)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$finalized = count(Order::where('order_state_id', 6)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$canceled = count(Order::where('order_state_id', 7)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
} else {
if ($month == "ninguna" && $sucursal == "todos") {
$pending = count(Order::where('order_state_id', 1)->whereYear('creation_date', $year)->get());
$accepted = count(Order::where('order_state_id', 2)->whereYear('creation_date', $year)->get());
$collected = count(Order::where('order_state_id', 3)->whereYear('creation_date', $year)->get());
$route = count(Order::where('order_state_id', 4)->whereYear('creation_date', $year)->get());
$delivered = count(Order::where('order_state_id', 5)->whereYear('creation_date', $year)->get());
$finalized = count(Order::where('order_state_id', 6)->whereYear('creation_date', $year)->get());
$canceled = count(Order::where('order_state_id', 7)->whereYear('creation_date', $year)->get());
} else {
if ($month == "ninguna") {
$pending = count(Order::where('order_state_id', 1)->where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->get());
$accepted = count(Order::where('order_state_id', 2)->where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->get());
$collected = count(Order::where('order_state_id', 3)->where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->get());
$route = count(Order::where('order_state_id', 4)->where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->get());
$delivered = count(Order::where('order_state_id', 5)->where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->get());
$finalized = count(Order::where('order_state_id', 6)->where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->get());
$canceled = count(Order::where('order_state_id', 7)->where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->get());
} else {
$pending = count(Order::where('order_state_id', 1)->where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$accepted = count(Order::where('order_state_id', 2)->where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$collected = count(Order::where('order_state_id', 3)->where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$route = count(Order::where('order_state_id', 4)->where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$delivered = count(Order::where('order_state_id', 5)->where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$finalized = count(Order::where('order_state_id', 6)->where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$canceled = count(Order::where('order_state_id', 7)->where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
}
}
}
} else {
if ($month == 'ninguna') {
$pending = count(Order::where('order_state_id', 1)->where('sucursal_id', $id)->whereYear('creation_date', $year)->get());
$accepted = count(Order::where('order_state_id', 2)->where('sucursal_id', $id)->whereYear('creation_date', $year)->get());
$collected = count(Order::where('order_state_id', 3)->where('sucursal_id', $id)->whereYear('creation_date', $year)->get());
$route = count(Order::where('order_state_id', 4)->where('sucursal_id', $id)->whereYear('creation_date', $year)->get());
$delivered = count(Order::where('order_state_id', 5)->where('sucursal_id', $id)->whereYear('creation_date', $year)->get());
$finalized = count(Order::where('order_state_id', 6)->where('sucursal_id', $id)->whereYear('creation_date', $year)->get());
$canceled = count(Order::where('order_state_id', 7)->where('sucursal_id', $id)->whereYear('creation_date', $year)->get());
} else {
$pending = count(Order::where('order_state_id', 1)->where('sucursal_id', $id)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$accepted = count(Order::where('order_state_id', 2)->where('sucursal_id', $id)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$collected = count(Order::where('order_state_id', 3)->where('sucursal_id', $id)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$route = count(Order::where('order_state_id', 4)->where('sucursal_id', $id)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$delivered = count(Order::where('order_state_id', 5)->where('sucursal_id', $id)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$finalized = count(Order::where('order_state_id', 6)->where('sucursal_id', $id)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$canceled = count(Order::where('order_state_id', 7)->where('sucursal_id', $id)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
}
}
$res[] = array(
'pending' => $pending,
'accepted' => $accepted,
'collected' => $collected,
'route' => $route,
'delivered' => $delivered,
'finalized' => $finalized,
'canceled' => $canceled
);
$dres = json_encode($res);
return json_decode($dres, true);
}
public function averageMonthAdmin($year, $month, $sucursal)
{
$id = Auth::user()->userInfo->sucursal->id;
$rol = Auth::user()->rol->id;
if ($rol != 1 && $rol != 6) {
$sucursal = $id;
}
if ($month != 'ninguna') {
if ($sucursal == "todos") {
$average = AverageMonth::where('year', $year)->where('number_month', $month)->get();
$numOrder = 0;
$numTransfer = 0;
$numOrder = count(Order::whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$orders = Order::whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get();
foreach ($orders as $key => $order) {
$numTransfer = $numTransfer + count(Transfer::where('order_id', $order->id)->get());
}
} else {
$numOrder = 0;
$numTransfer = 0;
if ($sucursal == "ec") {
$average = AverageMonth::where('year', $year)->whereNull('sucursal_id')->where('number_month', $month)->get();
$numOrder = count(Order::whereYear('creation_date', $year)->whereMonth('creation_date', $month)->whereNull('sucursal_id')->get());
$orders = Order::whereYear('creation_date', $year)->whereMonth('creation_date', $month)->whereNull('sucursal_id')->get();
} else {
$average = AverageMonth::where('year', $year)->where('sucursal_id', $sucursal)->where('number_month', $month)->get();
$numOrder = count(Order::where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get());
$orders = Order::where('sucursal_id', $sucursal)->whereYear('creation_date', $year)->whereMonth('creation_date', $month)->get();
}
foreach ($orders as $key => $order) {
$numTransfer = $numTransfer + count(Transfer::where('order_id', $order->id)->get());
}
}
$res[] = array('average' => $average, 'month' => true, 'transfer' => $numTransfer, 'orders' => $numOrder);
$dres = json_encode($res);
return json_decode($dres, true);
} else {
return $this->averageYearAdmin($year, $sucursal);
}
}
public function chartConnect()
{
try {
$users = DB::table('users')
->where('rol_id', 4)
->whereDate('last_session', '>=', Carbon::now()->subDays(7))
->select(DB::raw('DATE(last_session) as date'), DB::raw('count(DISTINCT id) as visitors'))
->groupBy('date')
->orderBy('date', 'asc')
->get();
return response()->json(array('r' => true, 'd' => $users), 200);
} catch (\Throwable $th) {
return response()->json(array('r' => false, 'd' => $th->getMessage()), 200);
}
}
public function chartRegister()
{
try {
$users = DB::table('users')
->where('rol_id', 4)
->whereDate('created_at', '>=', Carbon::now()->subMonth(3))
->select(DB::raw('DATE_FORMAT(created_at, "%m") as date'), DB::raw('count(DISTINCT id) as register'))
->groupBy('date')
->orderBy('date', 'asc')
->get();
return response()->json(array('r' => true, 'd' => $users), 200);
} catch (\Throwable $th) {
return response()->json(array('r' => false, 'd' => $th->getMessage()), 200);
}
}
}