Carga inicial
This commit is contained in:
294
app/Http/Controllers/Operador/ServiciosProgresoNewController.php
Normal file
294
app/Http/Controllers/Operador/ServiciosProgresoNewController.php
Normal file
@@ -0,0 +1,294 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Operador;
|
||||
|
||||
use App\Mail\SolicitudServicioTerminadoNew;
|
||||
use App\Models\Cliente;
|
||||
use App\Models\ClienteDomicilio;
|
||||
use App\Models\RespuestaDomestico;
|
||||
use App\Models\RespuestaEmpresarial;
|
||||
use App\Models\ServicioDet;
|
||||
use App\Models\ServicioEnc;
|
||||
use App\Models\ServicioEncuestaDomestico;
|
||||
use App\Models\ServicioEncuestaEmpresarial;
|
||||
use App\Models\ServicioEvidencia;
|
||||
use App\Models\ServicioProgreso;
|
||||
use App\Models\Sucursal;
|
||||
use Carbon\Carbon;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use const App\Http\Controllers\COSTO_SERVICIO_NEGATIVO;
|
||||
use const App\Http\Controllers\DEFAULT_PATH_ZIP_EVIDENCES;
|
||||
use const App\Http\Controllers\DOMESTICO;
|
||||
use const App\Http\Controllers\NEGATIVO;
|
||||
use const App\Http\Controllers\REALIZADO;
|
||||
use Log;
|
||||
|
||||
class ServiciosProgresoNewController extends Controller
|
||||
{
|
||||
/**
|
||||
* Finalizar Servicio.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function finalizarServicio(Request $request)
|
||||
{
|
||||
$user = Auth::user();
|
||||
$finalizar_servicio_data = $request->only(['fecha_fin_celular', 'duracion', 'lat_fin', 'lng_fin', 'comentarios', 'aplica_garantia','litraje']);
|
||||
$cat_motivos_estatus_id = $request->input('cat_motivos_estatus_id',0);
|
||||
$now = Carbon::now();
|
||||
|
||||
$servicio_enc_id = $request->input('servicio_enc_id');
|
||||
$servicio_det_id = $request->input('servicio_det_id');
|
||||
|
||||
$litraje = $request->input('litraje', null);
|
||||
|
||||
$servicio_progreso = ServicioProgreso::where('servicio_enc_id', $servicio_enc_id)
|
||||
->where('servicio_det_id', $servicio_det_id)
|
||||
->first();
|
||||
|
||||
$servicio_det = ServicioDet::where('servicio_enc_id', $servicio_enc_id)
|
||||
->where('id', $servicio_det_id)
|
||||
->first();
|
||||
$servicio = ServicioEnc::where('id', $servicio_enc_id)->first();
|
||||
|
||||
$correos = [];
|
||||
$correo_cliente = Cliente::select('email')->where('id', $servicio->cliente_id)->first();
|
||||
$correo_cliente_domicilio = ClienteDomicilio::select('email')->where('id', $servicio->cliente_domicilio_id)->first();
|
||||
|
||||
if($correo_cliente && $correo_cliente->email){
|
||||
$correos[] = $correo_cliente->email;
|
||||
}
|
||||
if($correo_cliente_domicilio && $correo_cliente_domicilio->email){
|
||||
$correos[] = $correo_cliente_domicilio->email;
|
||||
}
|
||||
|
||||
//Validar que el servicio no este terminado
|
||||
if($servicio_det->estatus_servicio_id == REALIZADO){
|
||||
return response()->success(['result' => 'ok']);
|
||||
}
|
||||
|
||||
//Validar que el servicio esté iniciado
|
||||
if(!$servicio_progreso){
|
||||
return response()->unprocessable('Parametros inválidos',['El servicio no está iniciado.']);
|
||||
}
|
||||
|
||||
if (!$recibo = $request->input('recibo')){
|
||||
return response()->unprocessable('Parametros inválidos',['No se ha podido generar el recibo.']);
|
||||
}
|
||||
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
|
||||
//ENCUESTA
|
||||
if($encuesta = $request->input('encuesta')){
|
||||
$this->encuesta($encuesta, $servicio_det_id);
|
||||
$finalizar_servicio_data['encuesta_contestada'] = 1;
|
||||
}
|
||||
//
|
||||
|
||||
$finalizar_servicio_data['fecha_fin_servidor'] = $now->toDateTimeString();
|
||||
|
||||
//Se agrego por detalle de Geolocalizacion
|
||||
$servicio_enc = ServicioEnc::select('clientes_domicilios.lat','clientes_domicilios.lng')
|
||||
->join('clientes_domicilios','servicios_enc.cliente_domicilio_id', '=','clientes_domicilios.id')
|
||||
->where('servicios_enc.id', $servicio_det->servicio_enc_id)
|
||||
->first();
|
||||
$finalizar_servicio_data['lat_fin'] = $servicio_enc->lat;
|
||||
$finalizar_servicio_data['lng_fin'] = $servicio_enc->lng;
|
||||
$finalizar_servicio_data['litraje'] = ($litraje == 0)? null: $litraje;
|
||||
|
||||
$costo_servicio_negativo = Sucursal::select('costo_negativo')->where('sucursales.id', $user->sucursal_id)->get();
|
||||
|
||||
$servicio_progreso->update($finalizar_servicio_data);
|
||||
if ($cat_motivos_estatus_id != 0){
|
||||
$servicio_det->update(
|
||||
[
|
||||
'estatus_servicio_id' => NEGATIVO,
|
||||
'cat_motivos_estatus_id' => $cat_motivos_estatus_id,
|
||||
'costo_servicio' => $costo_servicio_negativo,
|
||||
]
|
||||
);
|
||||
} else{
|
||||
$servicio_det->update(['estatus_servicio_id' => REALIZADO]);
|
||||
}
|
||||
|
||||
$recibo = base64_decode($recibo);
|
||||
$nombre = 'solicitud_' . $servicio_enc_id . '_servicio_' . $servicio_det_id . '.pdf';
|
||||
Storage::disk('public')->put('servicios/recibos_pdf/' . $nombre, $recibo);
|
||||
|
||||
DB::commit();
|
||||
|
||||
try {
|
||||
$ambiente = strtolower(config('ambiente')['ambiente']);
|
||||
if(count($correos) > 0 && $ambiente == 'dev'){
|
||||
try {
|
||||
Mail::to($correos)->send(new SolicitudServicioTerminadoNew($servicio_enc_id,$servicio_det_id, $nombre));
|
||||
} catch (\Exception $e) {
|
||||
\Log::info("Error al enviar correo: " . json_encode($e->getMessage()));
|
||||
}
|
||||
}
|
||||
}catch (\Exception $e){
|
||||
return response()->success(['result' => 'ok']);
|
||||
}
|
||||
return response()->success(['result' => 'ok']);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
Log::info($e);
|
||||
return response()->unprocessable('Error', ['Error al finalizar servicio.']);
|
||||
}
|
||||
}
|
||||
|
||||
public function encuesta($data, $servicio_id)
|
||||
{
|
||||
$servicio_det = ServicioDet::where('id', $servicio_id)->firstOrFail();
|
||||
|
||||
if($servicio_det->tipo_servicio_id == DOMESTICO) {
|
||||
$validator = Validator::make($data, [
|
||||
'*.pregunta_id' => 'numeric|exists:preguntas_domestico,id',
|
||||
'*.respuesta_id' => 'numeric|exists:respuestas_domestico,id',
|
||||
'*.respuesta' => 'string|max:255'
|
||||
]);
|
||||
|
||||
if(ServicioEncuestaDomestico::where('servicio_det_id', $servicio_id)->first()){
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($data as $d){
|
||||
if(isset($d['respuesta_id'])){
|
||||
$respuesta = RespuestaDomestico::where('pregunta_id', $d['pregunta_id'])
|
||||
->where('id', $d['respuesta_id'])
|
||||
->first();
|
||||
|
||||
if(!$respuesta){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}else{
|
||||
$validator = Validator::make($data, [
|
||||
'*.pregunta_id' => 'numeric|exists:preguntas_empresarial,id',
|
||||
'*.respuesta_id' => 'numeric|exists:respuestas_empresarial,id',
|
||||
'*.respuesta' => 'string|max:255'
|
||||
]);
|
||||
|
||||
if(ServicioEncuestaEmpresarial::where('servicio_det_id', $servicio_id)->first()){
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($data as $d){
|
||||
if(isset($d['respuesta_id'])){
|
||||
$respuesta = RespuestaEmpresarial::where('pregunta_id', $d['pregunta_id'])
|
||||
->where('id', $d['respuesta_id'])
|
||||
->first();
|
||||
|
||||
if(!$respuesta){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if($validator->fails()){
|
||||
return false;
|
||||
}
|
||||
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
if($servicio_det->tipo_servicio_id == DOMESTICO) {
|
||||
foreach ($data as $d){
|
||||
|
||||
$data_domestico = [
|
||||
'servicio_det_id' => $servicio_id,
|
||||
'pregunta_id' => $d['pregunta_id'],
|
||||
'respuesta_id' => (isset($d['respuesta_id']))? $d['respuesta_id'] : null,
|
||||
'respuesta' => (isset($d['respuesta']))? $d['respuesta'] : null
|
||||
];
|
||||
ServicioEncuestaDomestico::create($data_domestico);
|
||||
}
|
||||
|
||||
}else{
|
||||
foreach ($data as $d){
|
||||
$data_empresarial = [
|
||||
'servicio_det_id' => $servicio_id,
|
||||
'pregunta_id' => $d['pregunta_id'],
|
||||
'respuesta_id' => (isset($d['respuesta_id']))? $d['respuesta_id'] : null,
|
||||
'respuesta' => (isset($d['respuesta']))? $d['respuesta'] : null
|
||||
];
|
||||
ServicioEncuestaEmpresarial::create($data_empresarial);
|
||||
}
|
||||
}
|
||||
DB::commit();
|
||||
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function saveEvidence($evidencia, $servicio_enc_id, $servicio_det_id, $servicio_progreso_id){
|
||||
$image = storage_path(DEFAULT_PATH_ZIP_EVIDENCES.$evidencia['nombre']);
|
||||
|
||||
if(file_exists($image)){
|
||||
$data['uuid'] = hash_file('sha256', $image);
|
||||
|
||||
if(ServicioEvidencia::where('uuid', $data['uuid'])->first()){
|
||||
unlink($image);
|
||||
return false;
|
||||
}
|
||||
|
||||
//Se agrego por detalle de Geolocalizacion
|
||||
$servicio_enc = ServicioEnc::select('clientes_domicilios.lat','clientes_domicilios.lng')
|
||||
->join('clientes_domicilios','servicios_enc.cliente_domicilio_id', '=','clientes_domicilios.id')
|
||||
->where('servicios_enc.id', $servicio_enc_id)
|
||||
->first();
|
||||
|
||||
$data['lat'] = $servicio_enc->lat;
|
||||
$data['lng'] = $servicio_enc->lng;
|
||||
$data['servicio_progreso_id'] = $servicio_progreso_id;
|
||||
$data['etapa'] = $evidencia['etapa'];
|
||||
|
||||
$evidence = ServicioEvidencia::create($data);
|
||||
$nombre_img = 'solicitud_'.$servicio_enc_id.'_servicio_'.$servicio_det_id.'_evidencia_'.$evidence->id.'.jpg';
|
||||
rename($image, storage_path("app/public/servicios/evidencias/$nombre_img"));
|
||||
$evidence->update(['imagen' => $nombre_img]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function saveEvidenceImage($pathFile, Collection $collect_evidencias, $servicio_enc_id, $servicio_det_id, $servicio_progreso_id){
|
||||
$zip = new \ZipArchive;
|
||||
$pathInfo = pathinfo(realpath($pathFile), PATHINFO_DIRNAME);
|
||||
if($zip->open($pathFile)){
|
||||
if($zip->extractTo($pathInfo)){
|
||||
for($i=0;$i<$zip->numFiles;++$i){
|
||||
$stat = $zip->statIndex($i);
|
||||
if($evidencia = $collect_evidencias->where('nombre', '=', basename($stat['name']))->first())
|
||||
$this->saveEvidence($evidencia, $servicio_enc_id, $servicio_det_id, $servicio_progreso_id);
|
||||
}
|
||||
}
|
||||
$zip->close();
|
||||
unlink($pathFile);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function saveSignature($base64_firma, $servicio_enc_id, $servicio_det_id, $servicio_progreso){
|
||||
$nombre_img = 'firma_'.'solicitud_'.$servicio_enc_id.'_servicio_'.$servicio_det_id.'.jpg';
|
||||
Storage::disk('public')->put('servicios/firmas/' . $nombre_img, $this->base64ToImage($base64_firma), 'public');
|
||||
$servicio_progreso->update(['firma' => $nombre_img]);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user