KOKINIO - MANAGER
Edit File: Ordenes.php
<?php namespace Trash; use Illuminate\Database\Eloquent\Model; class Ordenes extends Model { // protected $table = 'orden'; public $timestamps = false; public function checkOrden($data){ $sql="SELECT * FROM orden WHERE orden_no = '".$data['orden']."' "; $data = \DB::select( \DB::raw( $sql )); if(count($data) < 1 ){ return true ; }else{ return false; } } public function checkOrdenInicio($id){ $sql="SELECT o.id, o.orden_no, o.cliente_id, c.nombre_comercial, o.user_id, o.camion_id, o.fecha_inicio, DATE(o.fecha_inicio) fecha, TIME(o.fecha_inicio) hora_inicio, un.unidad, g.giro FROM orden o JOIN cliente c ON o.cliente_id = c.id JOIN unidad un ON un.id = c.unidad_id LEFT JOIN giro_empresa g ON g.id = c.giro_id WHERE user_id = ".$id." AND DATE(fecha_inicio) = '".date('Y-m-d')."' AND DATE(fecha_termino) = '0000-00-00'"; $data = \DB::select( \DB::raw( $sql )); if(count($data) < 1 ){ return response()->json(array('status'=>true , 'data'=>$data)); }else{ return response()->json(array('status'=>false , 'data'=>$data)); } } public function orden_day($fecha){ $sql="SELECT o.id, o.orden_no, u.username, u.ape_pat, ca.placas, cl.nombre_comercial, un.unidad, o.cantidad, o.observaciones, o.latitude_inicio lat_ini,o.longitude_inicio lng_ini, o.latitude_termino lat_ter, o.longitude_termino lng_ter, cl.latitude cliente_lat, cl.longitude cliente_lng, o.fecha_inicio, o.fecha_termino, TIME(o.fecha_inicio) hora_inicio, TIME(o.fecha_termino) hora_termino, o.km, o.km_dif, h.kilometraje_inicio, o.cancel, o.tiempo_traslado, r.reciclable, o.peso_reciclable FROM orden o JOIN user u ON u.id = o.user_id JOIN camion ca ON ca.id = o.camion_id JOIN cliente cl ON cl.id = o.cliente_id JOIN unidad un ON un.id = cl.unidad_id LEFT JOIN reciclable r ON r.id = o.reciclable_id JOIN history_choferes h ON h.user_id = o.user_id AND DATE(h.fecha_inicio) = '$fecha' WHERE DATE(o.fecha_inicio) = '$fecha' ORDER BY id DESC"; $data = \DB::select( \DB::raw( $sql )); if(count($data) != null ){ return response()->json(array('status'=>true , 'data'=>$data)); }else{ return response()->json(array('status'=>false )); } } public function km_day($km,$id){ $sql="SELECT km, fecha_termino FROM orden WHERE DATE(fecha_inicio) = CURDATE() AND km >= $km AND user_id=$id Order BY km DESC limit 0, 1"; $data = \DB::select( \DB::raw( $sql )); if(count($data) != null ){ return response()->json(array('status'=>true , 'data'=>$data)); }else{ return response()->json(array('status'=>false )); } } public function ordenReport($fecha_ini,$fecha_ter,$conductores,$clientes){ $conductores_;$clientes_; if($conductores == 'null' | $conductores == 'undefined'){ $conductores_ = ' '; }else{ $conductores_ = ' AND u.id in ('.$conductores.') '; } if($clientes == 'null' | $clientes == 'undefined'){ $clientes_ = ' '; }else{ $clientes_ = ' AND cl.id in ('.$clientes.') '; } $sql=" SELECT o.orden_no, DATE(o.fecha_inicio) fecha, CASE DAYNAME(o.fecha_inicio) WHEN 'Sunday' THEN 'Domingo' WHEN 'Monday' THEN 'Lunes' WHEN 'Tuesday' THEN 'Martes' WHEN 'Wednesday' THEN 'Miercoles' WHEN 'Thursday' THEN 'Jueves' WHEN 'Friday' THEN 'Viernes' WHEN 'Saturday' THEN 'Sabado' END dia, MONTH(o.fecha_inicio) Mes, cl.semades, cl.cliente razon_social, cl.nombre_comercial cliente, cu.cuota, un.unidad, cl.pu, ca.placas, u.username, pc.peso, ((pc.peso*o.cantidad)*.001) toneladas, o.cantidad, r.reciclable, r.clasificacion, o.peso_reciclable, (o.peso_reciclable*.001) toneladas_r, o.observaciones, TIME(o.fecha_inicio) hora_inicio, TIME(o.fecha_termino) hora_termino, o.km, h.kilometraje_inicio, o.km_dif, (cl.inorganicos*((pc.peso*o.cantidad)*.001) )/100 inorganicos, (cl.organicos*((pc.peso*o.cantidad)*.001) )/100 organicos, (cl.sanitarios*((pc.peso*o.cantidad)*.001) )/100 sanitarios, o.costo_pago, o.costo_cliente FROM orden o JOIN user u ON u.id = o.user_id $conductores_ JOIN camion ca ON ca.id = o.camion_id JOIN cliente cl ON cl.id = o.cliente_id $clientes_ JOIN unidad un ON un.id = cl.unidad_id JOIN cuota cu ON cu.id = cl.cuota_id LEFT JOIN reciclable r ON r.id = o.reciclable_id JOIN history_choferes h ON h.user_id = o.user_id AND DATE(h.fecha_inicio) = DATE(o.fecha_inicio) LEFT JOIN peso_cliente pc ON pc.cliente_id = cl.id AND MONTH(o.fecha_inicio) = pc.mes_id AND YEAR(o.fecha_inicio) = pc.year WHERE DATE(o.fecha_inicio) BETWEEN '$fecha_ini' AND '$fecha_ter' AND o.cancel = 0 ORDER BY fecha"; $data = \DB::select( \DB::raw( $sql )); if(count($data) != null ){ return response()->json(array('status' => true , 'data'=>$data)); }else{ return response()->json(array('status' => false )); } } public function getOrden($id,$fecha){ $sql="SELECT o.id, o.orden_no, o.user_id, o.cliente_id, o.camion_id, u.username, u.ape_pat, ca.placas, cl.nombre_comercial, un.unidad, o.cantidad, o.observaciones, o.reciclable_id, o.peso_reciclable, o.latitude_inicio lat_ini,o.longitude_inicio lng_ini, o.latitude_termino lat_ter, o.longitude_termino lng_ter, cl.latitude cliente_lat, cl.longitude cliente_lng, TIME(o.fecha_inicio) hora_inicio, TIME(o.fecha_termino) hora_termino, o.km, o.km_dif, h.kilometraje_inicio, o.cancel, g.giro, vo.nota nota_vertedero, vo.peso_neto, vo.precio, vo.hora hora_vertedero, co.nota nota_combustible, co.litros, co.precio_litro, co.hora hora_combustible, co.tipo FROM orden o JOIN user u ON u.id = o.user_id JOIN camion ca ON ca.id = o.camion_id JOIN cliente cl ON cl.id = o.cliente_id JOIN giro_empresa g ON cl.giro_id = g.id JOIN unidad un ON un.id = cl.unidad_id JOIN history_choferes h ON h.user_id = o.user_id AND date(h.fecha_inicio) = '$fecha' LEFT JOIN vertedero_orden vo ON vo.orden_id = o.id AND vo.status_id = 1 LEFT JOIN combustible_orden co ON co.orden_id = o.id AND co.status_id = 1 WHERE o.id = $id"; $data = \DB::select( \DB::raw( $sql )); if(count($data) != null ){ return response()->json(array('status'=>true , 'data'=>$data)); }else{ return response()->json(array('status'=>false )); } } public function orden_inicio($id){ $sql= "SELECT orden_no FROM orden WHERE user_id = $id AND DATE(fecha_inicio) = CURDATE() ORDER BY id DESC Limit 0, 1"; $data = \DB::select( \DB::raw( $sql )); if(count($data) != null ){ return response()->json(array('status'=>true , 'data'=>$data)); }else{ return response()->json(array('status'=>false, 'data'=>'' )); } } public function tota_orden($id = "null", $year = "null", $month = "null", $op = 0){ $costumer = ""; $yearWhere = " YEAR(CURDATE()) "; $monthWhere = ""; if($id != "null"){ $costumer = " AND cl.id = ".$id; } if($year != "null"){ $yearWhere = " $year "; } if(!$op){ $monthWhere = " AND MONTH(o.fecha_inicio) = ".$month; }else{ $monthWhere = " AND MONTH(o.fecha_inicio) <= ".$month; } $sql= "SELECT YEAR(o.fecha_inicio) Year, MONTH(o.fecha_inicio) NoMes, CASE WHEN MONTH(o.fecha_inicio) = 1 THEN 'Enero' WHEN MONTH(o.fecha_inicio) = 2 THEN 'Febrero' WHEN MONTH(o.fecha_inicio) = 3 THEN 'Marzo' WHEN MONTH(o.fecha_inicio) = 4 THEN 'Abril' WHEN MONTH(o.fecha_inicio) = 6 THEN 'Junio' WHEN MONTH(o.fecha_inicio) = 5 THEN 'Mayo' WHEN MONTH(o.fecha_inicio) = 7 THEN 'Julio' WHEN MONTH(o.fecha_inicio) = 8 THEN 'Agosto' WHEN MONTH(o.fecha_inicio) = 9 THEN 'Septiembre' WHEN MONTH(o.fecha_inicio) = 10 THEN 'Octubre' WHEN MONTH(o.fecha_inicio) = 11 THEN 'Noviembre' WHEN MONTH(o.fecha_inicio) = 12 THEN 'Diciembre' ELSE 'Error' END AS Mes, cl.nombre_comercial cliente, cl.cliente cliente2, cl.direccion, cl.colonia, SUM((cl.inorganicos*((pc.peso*o.cantidad)*.001) )/100) inorganicos, SUM((cl.organicos*((pc.peso*o.cantidad)*.001) )/100) organicos, SUM((cl.sanitarios*((pc.peso*o.cantidad)*.001) )/100) sanitarios, SUM((cl.inorganicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((cl.organicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((cl.sanitarios*((pc.peso*o.cantidad)*.001) )/100) Total FROM orden o JOIN cliente cl ON cl.id = o.cliente_id $costumer -- AND cl.status_id = 1 LEFT JOIN peso_cliente pc ON pc.cliente_id = cl.id AND MONTH(o.fecha_inicio) = pc.mes_id AND YEAR(o.fecha_inicio) = pc.year WHERE YEAR(o.fecha_inicio) = $yearWhere AND o.cancel = 0 $monthWhere GROUP BY Year, Mes, cl.id ORDER BY Year DESC, NoMes, cliente ASC"; $data = \DB::select( \DB::raw( $sql )); if(count($data) != null ){ return response()->json(array('status'=>true , 'data'=>$data)); }else{ return response()->json(array('status'=>false, 'data'=>'' )); } } public function year_order(){ $sql = "SELECT YEAR(o.fecha_inicio) Year FROM orden o JOIN cliente cl ON cl.id = o.cliente_id AND cl.status_id = 1 LEFT JOIN peso_cliente pc ON pc.cliente_id = cl.id AND MONTH(o.fecha_inicio) = pc.mes_id AND YEAR(o.fecha_inicio) = pc.year WHERE o.cancel = 0 GROUP BY YEAR(o.fecha_inicio) ORDER BY YEAR(o.fecha_inicio) DESC"; $data = \DB::select( \DB::raw( $sql )); if(count($data) != null ){ return $data; } } public function tota_orden_mes($year, $month){ $sql= "SELECT YEAR(o.fecha_inicio) Year, CASE WHEN MONTH(o.fecha_inicio) = 1 THEN 'Enero' WHEN MONTH(o.fecha_inicio) = 2 THEN 'Febrero' WHEN MONTH(o.fecha_inicio) = 3 THEN 'Marzo' WHEN MONTH(o.fecha_inicio) = 4 THEN 'Abril' WHEN MONTH(o.fecha_inicio) = 6 THEN 'Junio' WHEN MONTH(o.fecha_inicio) = 5 THEN 'Mayo' WHEN MONTH(o.fecha_inicio) = 7 THEN 'Julio' WHEN MONTH(o.fecha_inicio) = 8 THEN 'Agosto' WHEN MONTH(o.fecha_inicio) = 9 THEN 'Septiembre' WHEN MONTH(o.fecha_inicio) = 10 THEN 'Octubre' WHEN MONTH(o.fecha_inicio) = 11 THEN 'Noviembre' WHEN MONTH(o.fecha_inicio) = 12 THEN 'Diciembre' ELSE 'Error' END AS Mes, cl.nombre_comercial cliente, SUM((cl.inorganicos*((pc.peso*o.cantidad)*.001) )/100) inorganicos, SUM((cl.organicos*((pc.peso*o.cantidad)*.001) )/100) organicos, SUM((cl.sanitarios*((pc.peso*o.cantidad)*.001) )/100) sanitarios, SUM((cl.inorganicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((cl.organicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((cl.sanitarios*((pc.peso*o.cantidad)*.001) )/100) Total FROM orden o JOIN cliente cl ON cl.id = o.cliente_id LEFT JOIN peso_cliente pc ON pc.cliente_id = cl.id AND MONTH(o.fecha_inicio) = pc.mes_id AND YEAR(o.fecha_inicio) = pc.year WHERE YEAR(o.fecha_inicio) = $year AND MONTH(o.fecha_inicio) = $month AND o.cancel = 0 GROUP BY Mes ORDER BY Mes"; $data = \DB::select( \DB::raw( $sql )); if(count($data) != null ){ return response()->json(array('status'=>true , 'data'=>$data)); }else{ return response()->json(array('status'=>false, 'data'=>'' )); } } public function orden_mes_year($year = null){ $where = ""; $groupBy = " GROUP BY YEAR(o.fecha_inicio) "; if($year!= null){ $where = " WHERE YEAR(o.fecha_inicio) = $year "; $groupBy = " GROUP BY Year,Mes "; } $sql= "SELECT YEAR(o.fecha_inicio) Year, MONTH(o.fecha_inicio) Month, CASE WHEN MONTH(o.fecha_inicio) = 1 THEN 'Enero' WHEN MONTH(o.fecha_inicio) = 2 THEN 'Febrero' WHEN MONTH(o.fecha_inicio) = 3 THEN 'Marzo' WHEN MONTH(o.fecha_inicio) = 4 THEN 'Abril' WHEN MONTH(o.fecha_inicio) = 6 THEN 'Junio' WHEN MONTH(o.fecha_inicio) = 5 THEN 'Mayo' WHEN MONTH(o.fecha_inicio) = 7 THEN 'Julio' WHEN MONTH(o.fecha_inicio) = 8 THEN 'Agosto' WHEN MONTH(o.fecha_inicio) = 9 THEN 'Septiembre' WHEN MONTH(o.fecha_inicio) = 10 THEN 'Octubre' WHEN MONTH(o.fecha_inicio) = 11 THEN 'Noviembre' WHEN MONTH(o.fecha_inicio) = 12 THEN 'Diciembre' ELSE 'Error' END AS Mes FROM orden o JOIN cliente cl ON cl.id = o.cliente_id LEFT JOIN peso_cliente pc ON pc.cliente_id = cl.id AND MONTH(o.fecha_inicio) = pc.mes_id AND YEAR(o.fecha_inicio) = pc.year AND pc.peso <> NULL AND o.cancel = 0 ".$where." ".$groupBy." ORDER BY YEAR(o.fecha_inicio),MONTH(o.fecha_inicio)"; $data = \DB::select( \DB::raw( $sql )); if(count($data) != null ){ return response()->json(array('status'=>true , 'data'=>$data)); }else{ return response()->json(array('status'=>false, 'data'=>'' )); } } public function total_month($costumer,$month,$year){ $sql = "SELECT o.id, o.orden_no, DATE(o.fecha_inicio) fecha, cl.nombre_comercial cliente, cl.cliente cliente_n, cl.direccion, cl.colonia, pc.peso, o.cantidad, u.unidad, SUM((cl.inorganicos*((pc.peso*o.cantidad)*.001) )/100) inorganicos, SUM((cl.organicos*((pc.peso*o.cantidad)*.001) )/100) organicos, SUM((cl.sanitarios*((pc.peso*o.cantidad)*.001) )/100) sanitarios, SUM((cl.inorganicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((cl.organicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((cl.sanitarios*((pc.peso*o.cantidad)*.001) )/100) total FROM orden o JOIN cliente cl ON cl.id = o.cliente_id AND cl.id = $costumer JOIN unidad u ON u.id = cl.unidad_id LEFT JOIN peso_cliente pc ON pc.cliente_id = cl.id AND MONTH(o.fecha_inicio) = pc.mes_id AND YEAR(o.fecha_inicio) = pc.year WHERE MONTH(o.fecha_inicio) = $month AND YEAR(o.fecha_inicio) = $year AND o.cancel = 0 GROUP BY id, orden_no ORDER BY fecha"; $data = \DB::select( \DB::raw( $sql )); if(count($data) != null ){ return response()->json(array('status'=>true , 'data'=>$data)); }else{ return response()->json(array('status'=>false, 'data'=>'' )); } } public function ordenReciclable($id,$year,$month){ $sql = "SELECT o.id, o.orden_no, r.reciclable, c.cliente, c.nombre_comercial, DATE(o.fecha_inicio) fecha, (o.peso_reciclable) peso_kg, (o.peso_reciclable / 1000) peso_tn, c.direccion, c.colonia, o.costo_pago, o.costo_cliente FROM orden o JOIN cliente c ON c.id = o.cliente_id AND c.id = $id JOIN reciclable r ON r.id = o.reciclable_id WHERE reciclable_id <> 0 AND o.cancel = 0 AND o.peso_reciclable <> 0 AND YEAR(o.fecha_inicio) = $year AND MONTH(o.fecha_inicio) = $month ORDER BY fecha_inicio"; $data = \DB::select( \DB::raw( $sql )); if(count($data) != null ){ return $data; }else{ return false; } } public function ordenReciclableAnual($id, $year, $month = null, $type = null){ $whereMonth = ""; $whereType = ""; if($month != null){ $whereMonth = " AND MONTH(o.fecha_inicio) <= $month "; } if($type != null){ $whereType = " AND r.reciclable = '$type' "; $whereMonth = " AND MONTH(o.fecha_inicio) = $month "; } $sql = "SELECT o.id, o.orden_no, c.nombre_comercial, c.cliente, c.direccion, c.colonia, r.reciclable, MONTH(o.fecha_inicio) NoMes, SUM((o.peso_reciclable) * 100) peso_kg, SUM(o.peso_reciclable) peso_tn FROM orden o JOIN cliente c ON c.id = o.cliente_id AND c.id = $id JOIN reciclable r ON r.id = o.reciclable_id $whereType WHERE reciclable_id <> 0 AND o.cancel = 0 $whereMonth AND YEAR(o.fecha_inicio) = $year AND o.peso_reciclable <> 0 AND o.peso_reciclable <> 0 GROUP BY MONTH(o.fecha_inicio), reciclable ORDER BY reciclable"; $data = \DB::select( \DB::raw( $sql )); if(count($data) != null ){ return $data; }else{ return false; } } public function totalReciclable($id, $year, $month){ $sql = "SELECT r.reciclable, SUM((o.peso_reciclable) * (.001)) peso_tn FROM orden o JOIN cliente c ON c.id = o.cliente_id AND c.id = $id JOIN reciclable r ON r.id = o.reciclable_id WHERE reciclable_id <> 0 AND o.cancel = 0 AND YEAR(o.fecha_inicio) = $year AND MONTH(o.fecha_inicio) <= $month AND o.peso_reciclable <> 0 GROUP BY reciclable ORDER BY reciclable "; $data = \DB::select( \DB::raw( $sql )); if(count($data) != null ){ return $data; }else{ return false; } } public function reciclableUser($user_id){ $sql = "SELECT o.id, o.user_id, o.fecha_inicio, r.id reciclable_id, r.reciclable, o.costo_cliente, o.costo_pago FROM orden o JOIN reciclable r ON r.id = o.reciclable_id AND r.status_id = 1 WHERE date(fecha_inicio) = CURDATE() AND o.user_id = $user_id AND o.peso_reciclable != 0 AND o.cancel = 0 GROUP BY r.id"; $data = \DB::select( \DB::raw( $sql )); if(count($data) != null ){ return $data; } } public function getOrdenesCosto($user_id, $id, $fecha = null, $cliente = null){ $where_fecha = " CURDATE() "; $where_cliente = " "; if($fecha != null){ $where_fecha = " '$fecha' "; } if($cliente != null){ $where_cliente = " AND cliente_id = $cliente "; } $sql = "SELECT id FROM orden WHERE user_id = $user_id AND reciclable_id = $id $where_cliente AND DATE(fecha_inicio) = $where_fecha "; $data = \DB::select( \DB::raw( $sql )); if(count($data) != null ){ return $data; } } public function updateCostoOrden($ids,$costo, $type = 1){ try{ \DB::beginTransaction(); if($type == 1){ $sql="UPDATE orden SET costo_pago = $costo WHERE id IN ($ids) "; }else{ $sql="UPDATE orden SET costo_cliente = $costo WHERE id IN ($ids) "; } $data = \DB::update( \DB::raw( $sql )); }catch ( \Exception $e ){ \DB::rollback(); return \Response::json(array('status'=>false)); } \DB::commit(); return \Response::json(array('status'=>true)); } public function getReciclableCosto($date1 = null, $date2 = null){ $where_dates = " = CURDATE() "; if($date1 != null){ $where_dates = " BETWEEN '$date1' AND '$date2' "; } $sql = "SELECT o.orden_no, o.user_id, o.cliente_id, u.username, c.nombre_comercial, o.reciclable_id, r.reciclable, o.peso_reciclable, DATE(o.fecha_inicio) fecha, o.costo_pago, o.costo_cliente FROM orden o JOIN cliente c ON c.id = o.cliente_id JOIN user u ON u.id = o.user_id JOIN reciclable r ON r.id = o.reciclable_id AND r.id <> 13 WHERE o.reciclable_id <> 0 AND DATE(o.fecha_inicio) $where_dates AND o.cancel = 0 GROUP BY o.user_id, o.reciclable_id, fecha_inicio ORDER BY fecha_inicio DESC"; $data = \DB::select( \DB::raw( $sql )); if(count($data) != null ){ return $data; } } public function metricasReciclables($year = null){ if($year == null){ $year = date('Y'); } $sql = "SELECT cliente, id, clasificacion, SUM(mes_1) mes_1, SUM(mes_2) mes_2, SUM(mes_3) mes_3, SUM(mes_4) mes_4, SUM(mes_5) mes_5, SUM(mes_6) mes_6, SUM(mes_7) mes_7, SUM(mes_8) mes_8, SUM(mes_9) mes_9, SUM(mes_10) mes_10, SUM(mes_11) mes_11, SUM(mes_12) mes_12, SUM(peso_kg) peso_kg, SUM(peso_tn) peso_tn FROM( SELECT c.nombre_comercial cliente, c.id , r.clasificacion, IF(MONTH(o.fecha_inicio) = 1,SUM(o.peso_reciclable),0) mes_1, IF(MONTH(o.fecha_inicio) = 2,SUM(o.peso_reciclable),0) mes_2, IF(MONTH(o.fecha_inicio) = 3,SUM(o.peso_reciclable),0) mes_3, IF(MONTH(o.fecha_inicio) = 4,SUM(o.peso_reciclable),0) mes_4, IF(MONTH(o.fecha_inicio) = 5,SUM(o.peso_reciclable),0) mes_5, IF(MONTH(o.fecha_inicio) = 6,SUM(o.peso_reciclable),0) mes_6, IF(MONTH(o.fecha_inicio) = 7,SUM(o.peso_reciclable),0) mes_7, IF(MONTH(o.fecha_inicio) = 8,SUM(o.peso_reciclable),0) mes_8, IF(MONTH(o.fecha_inicio) = 9,SUM(o.peso_reciclable),0) mes_9, IF(MONTH(o.fecha_inicio) = 10,SUM(o.peso_reciclable),0) mes_10, IF(MONTH(o.fecha_inicio) = 11,SUM(o.peso_reciclable),0) mes_11, IF(MONTH(o.fecha_inicio) = 12,SUM(o.peso_reciclable),0) mes_12, SUM(o.peso_reciclable) peso_kg, SUM(o.peso_reciclable) peso_tn FROM orden o JOIN cliente c ON c.id = o.cliente_id JOIN reciclable r ON r.id = o.reciclable_id WHERE reciclable_id <> 0 AND o.cancel = 0 AND YEAR(o.fecha_inicio) = $year AND o.peso_reciclable <> 0 GROUP BY c.id, r.clasificacion, MONTH(o.fecha_inicio) ORDER BY c.nombre_comercial ) as derived GROUP BY id, clasificacion ORDER BY cliente, clasificacion"; $data = \DB::select( \DB::raw( $sql )); if(count($data) != null ){ return $data; } } public function metricasReciclablesCantidad($year = null){ if($year == null){ $year = date('Y'); } $sql = "SELECT nombre_comercial, id, unidad, fiscal, cuota, pu, SUM(mes_1) mes_1, SUM(mes_2) mes_2, SUM(mes_3) mes_3, SUM(mes_4) mes_4, SUM(mes_5) mes_5, SUM(mes_6) mes_6, SUM(mes_7) mes_7, SUM(mes_8) mes_8, SUM(mes_9) mes_9, SUM(mes_10) mes_10, SUM(mes_11) mes_11, SUM(mes_12) mes_12, SUM(total) total FROM( SELECT c.nombre_comercial, c.id, u.unidad, c.fiscal, cu.cuota, c.pu, IF(MONTH(o.fecha_inicio) = 1,SUM(o.cantidad),0) mes_1, IF(MONTH(o.fecha_inicio) = 2,SUM(o.cantidad),0) mes_2, IF(MONTH(o.fecha_inicio) = 3,SUM(o.cantidad),0) mes_3, IF(MONTH(o.fecha_inicio) = 4,SUM(o.cantidad),0) mes_4, IF(MONTH(o.fecha_inicio) = 5,SUM(o.cantidad),0) mes_5, IF(MONTH(o.fecha_inicio) = 6,SUM(o.cantidad),0) mes_6, IF(MONTH(o.fecha_inicio) = 7,SUM(o.cantidad),0) mes_7, IF(MONTH(o.fecha_inicio) = 8,SUM(o.cantidad),0) mes_8, IF(MONTH(o.fecha_inicio) = 9,SUM(o.cantidad),0) mes_9, IF(MONTH(o.fecha_inicio) = 10,SUM(o.cantidad),0) mes_10, IF(MONTH(o.fecha_inicio) = 11,SUM(o.cantidad),0) mes_11, IF(MONTH(o.fecha_inicio) = 12,SUM(o.cantidad),0) mes_12, SUM(o.cantidad) total FROM orden o JOIN cliente c ON c.id = o.cliente_id LEFT JOIN unidad u ON u.id = c.unidad_id LEFT JOIN cuota cu ON cu.id = c.cuota_id WHERE o.cancel = 0 AND YEAR(o.fecha_inicio) = $year GROUP BY c.id, MONTH(o.fecha_inicio) ORDER BY c.nombre_comercial ) as derived GROUP BY id, unidad having total > 0 ORDER BY nombre_comercial, unidad"; $data = \DB::select( \DB::raw( $sql )); if(count($data) != null ){ return $data; } } public function metricasResiduos($year = null){ if($year == null){ $year = date('Y'); } $sql = "SELECT id, nombre_comercial, semades, SUM(mes_1) mes_1, SUM(mes_2) mes_2, SUM(mes_3) mes_3, SUM(mes_4) mes_4, SUM(mes_5) mes_5, SUM(mes_6) mes_6, SUM(mes_7) mes_7, SUM(mes_8) mes_8, SUM(mes_9) mes_9, SUM(mes_10) mes_10, SUM(mes_11) mes_11, SUM(mes_12) mes_12, SUM(total) total FROM( SELECT c.id, c.nombre_comercial, c.semades, IF(MONTH(o.fecha_inicio) = 1, SUM((c.inorganicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((c.organicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((c.sanitarios*((pc.peso*o.cantidad)*.001) )/100),0) mes_1, IF(MONTH(o.fecha_inicio) = 2,SUM((c.inorganicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((c.organicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((c.sanitarios*((pc.peso*o.cantidad)*.001) )/100),0) mes_2, IF(MONTH(o.fecha_inicio) = 3,SUM((c.inorganicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((c.organicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((c.sanitarios*((pc.peso*o.cantidad)*.001) )/100),0) mes_3, IF(MONTH(o.fecha_inicio) = 4,SUM((c.inorganicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((c.organicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((c.sanitarios*((pc.peso*o.cantidad)*.001) )/100),0) mes_4, IF(MONTH(o.fecha_inicio) = 5,SUM((c.inorganicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((c.organicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((c.sanitarios*((pc.peso*o.cantidad)*.001) )/100),0) mes_5, IF(MONTH(o.fecha_inicio) = 6,SUM((c.inorganicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((c.organicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((c.sanitarios*((pc.peso*o.cantidad)*.001) )/100),0) mes_6, IF(MONTH(o.fecha_inicio) = 7,SUM((c.inorganicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((c.organicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((c.sanitarios*((pc.peso*o.cantidad)*.001) )/100),0) mes_7, IF(MONTH(o.fecha_inicio) = 8,SUM((c.inorganicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((c.organicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((c.sanitarios*((pc.peso*o.cantidad)*.001) )/100),0) mes_8, IF(MONTH(o.fecha_inicio) = 9,SUM((c.inorganicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((c.organicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((c.sanitarios*((pc.peso*o.cantidad)*.001) )/100),0) mes_9, IF(MONTH(o.fecha_inicio) = 10,SUM((c.inorganicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((c.organicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((c.sanitarios*((pc.peso*o.cantidad)*.001) )/100),0) mes_10, IF(MONTH(o.fecha_inicio) = 11,SUM((c.inorganicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((c.organicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((c.sanitarios*((pc.peso*o.cantidad)*.001) )/100),0) mes_11, IF(MONTH(o.fecha_inicio) = 12,SUM((c.inorganicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((c.organicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((c.sanitarios*((pc.peso*o.cantidad)*.001) )/100),0) mes_12, SUM((c.inorganicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((c.organicos*((pc.peso*o.cantidad)*.001) )/100) + SUM((c.sanitarios*((pc.peso*o.cantidad)*.001) )/100) total FROM orden o JOIN cliente c ON c.id = o.cliente_id LEFT JOIN peso_cliente pc ON pc.cliente_id = c.id AND MONTH(o.fecha_inicio) = pc.mes_id AND YEAR(o.fecha_inicio) = pc.year WHERE o.cancel = 0 AND YEAR(o.fecha_inicio) = $year GROUP BY c.nombre_comercial, MONTH(o.fecha_inicio) ORDER BY c.nombre_comercial ) as derived WHERE total <> 0 GROUP BY nombre_comercial ORDER BY nombre_comercial"; $data = \DB::select( \DB::raw( $sql )); if(count($data) != null ){ return $data; } } public function metricasUnidad($year = null){ if($year == null){ $year = date('Y'); } $sql = "SELECT id, cliente, SUM(mes_1) mes_1, SUM(mes_2) mes_2, SUM(mes_3) mes_3, SUM(mes_4) mes_4, SUM(mes_5) mes_5, SUM(mes_6) mes_6, SUM(mes_7) mes_7, SUM(mes_8) mes_8, SUM(mes_9) mes_9, SUM(mes_10) mes_10, SUM(mes_11) mes_11, SUM(mes_12) mes_12 FROM ( SELECT c.id, c.cliente, IF((pc.mes_id) = 1,SUM(pc.peso),0) mes_1, IF((pc.mes_id) = 2,SUM(pc.peso),0) mes_2, IF((pc.mes_id) = 3,SUM(pc.peso),0) mes_3, IF((pc.mes_id) = 4,SUM(pc.peso),0) mes_4, IF((pc.mes_id) = 5,SUM(pc.peso),0) mes_5, IF((pc.mes_id) = 6,SUM(pc.peso),0) mes_6, IF((pc.mes_id) = 7,SUM(pc.peso),0) mes_7, IF((pc.mes_id) = 8,SUM(pc.peso),0) mes_8, IF((pc.mes_id) = 9,SUM(pc.peso),0) mes_9, IF((pc.mes_id) = 10,SUM(pc.peso),0) mes_10, IF((pc.mes_id) = 11,SUM(pc.peso),0) mes_11, IF((pc.mes_id) = 12,SUM(pc.peso),0) mes_12 FROM cliente c LEFT JOIN peso_cliente pc ON pc.cliente_id = c.id AND pc.year = $year WHERE c.giro_id not in (24) GROUP BY c.cliente, pc.mes_id ) as derived GROUP BY id ORDER BY cliente"; $data = \DB::select( \DB::raw( $sql )); if(count($data) != null ){ return $data; } } public function metricasCobroChofer($year = null, $month = null){ if($year == null){ $year = date('Y'); } if($month == null){ $month = date('m'); } $sql = "SELECT o.id, u.username, c.cliente, c.nombre_comercial, r.reciclable, o.orden_no, MONTH(o.fecha_inicio) mes, DATE(o.fecha_inicio) fecha, o.peso_reciclable, o.costo_cliente, o.costo_pago FROM orden o JOIN cliente c ON c.id = o.cliente_id JOIN user u ON u.id = o.user_id JOIN reciclable r ON r.id = o.reciclable_id WHERE o.reciclable_id <> 0 AND o.cancel = 0 AND YEAR(o.fecha_inicio) = $year AND MONTH(o.fecha_inicio) = $month AND o.peso_reciclable <> 0 GROUP BY id ORDER BY username,cliente, orden_no, mes"; $data = \DB::select( \DB::raw( $sql )); if(count($data) != null ){ return $data; } } public function metricasCobroTotal($year = null, $month = null){ if($year == null){ $year = date('Y'); } if($month == null){ $month = date('m'); } $sql = "SELECT o.id, u.username, c.cliente, c.id, c.nombre_comercial, MONTH(o.fecha_inicio) mes, SUM(o.peso_reciclable) peso_reciclable, (o.costo_cliente) costo_cliente, (o.costo_pago) costo_pago, SUM((o.peso_reciclable) * (o.costo_pago)) total_chofer FROM orden o JOIN cliente c ON c.id = o.cliente_id JOIN user u ON u.id = o.user_id WHERE o.reciclable_id <> 0 AND o.cancel = 0 AND YEAR(o.fecha_inicio) = $year AND MONTH(o.fecha_inicio) = $month AND o.peso_reciclable <> 0 GROUP BY username, c.id ORDER BY username,cliente"; $data = \DB::select( \DB::raw( $sql )); if(count($data) != null ){ return $data; } } public function metricasUtilidad($year = null, $month = null){ if($year == null){ $year = date('Y'); } if($month == null){ $month = date('m'); } $sql = "SELECT r.reciclable, c.nombre_comercial, o.orden_no, c.id, MONTH(o.fecha_inicio) mes, SUM(o.peso_reciclable) peso_reciclable, o.costo_cliente, o.costo_pago FROM orden o JOIN cliente c ON c.id = o.cliente_id JOIN user u ON u.id = o.user_id JOIN reciclable r ON r.id = o.reciclable_id WHERE o.reciclable_id <> 0 AND o.cancel = 0 AND YEAR(o.fecha_inicio) = $year AND MONTH(o.fecha_inicio) = $month AND o.peso_reciclable <> 0 GROUP BY reciclable, c.id, mes, costo_cliente, costo_pago ORDER BY reciclable, cliente, mes, costo_cliente, costo_pago"; $data = \DB::select( \DB::raw( $sql )); if(count($data) != null ){ return $data; } } }