mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
105 lines
3.7 KiB
PHP
105 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Config\Services;
|
|
|
|
class LogisticaService
|
|
{
|
|
public static function findPedidoOrISBN($search)
|
|
{
|
|
$modelPedido = model('App\Models\Pedidos\PedidoModel');
|
|
|
|
$search = trim($search);
|
|
$searchClean = str_replace('-', '', $search);
|
|
$modelPedido = model('App\Models\Pedidos\PedidoModel');
|
|
|
|
$builder = $modelPedido->builder();
|
|
$builder->select('pedidos.id as pedido_id, pedidos_linea.id as linea_id, presupuestos.id as presupuesto_id');
|
|
$builder->join('pedidos_linea', 'pedidos_linea.pedido_id = pedidos.id', 'left');
|
|
$builder->join('presupuestos', 'presupuestos.id = pedidos_linea.presupuesto_id', 'left');
|
|
|
|
$builder->groupStart()
|
|
->where('pedidos.id', $search)
|
|
->whereIn('pedidos.estado', ['finalizado'])
|
|
->orWhere("REPLACE(presupuestos.isbn, '-', '')", $searchClean)
|
|
->groupEnd();
|
|
|
|
$result = $builder->get()->getResult();
|
|
|
|
if (empty($result)) {
|
|
$response = [
|
|
'status' => false,
|
|
'message' => lang('Logistica.errors.notFound'),
|
|
];
|
|
return $response;
|
|
}
|
|
|
|
$PresupuestoDireccionesModel = model('App\Models\Presupuestos\PresupuestoDireccionesModel');
|
|
$numDirecciones = $PresupuestoDireccionesModel->where('presupuesto_id', $result[0]->presupuesto_id)
|
|
->countAllResults();
|
|
if ($numDirecciones == 0) {
|
|
$response = [
|
|
'status' => false,
|
|
'message' => lang('Logistica.errors.noAddresses'),
|
|
];
|
|
return $response;
|
|
}
|
|
|
|
// detectar si el pedido tiene los albaranes generados
|
|
$AlbaranModel = model('App\Models\Pedidos\AlbaranModel');
|
|
$numAlbaranes = $AlbaranModel->where('pedido_id', $result[0]->pedido_id)
|
|
->countAllResults();
|
|
|
|
$response = [
|
|
'status' => true,
|
|
'data' => $result[0],
|
|
];
|
|
|
|
if($numAlbaranes == 0){
|
|
$user = auth()->user()->id;
|
|
$AlbaranModel->generarAlbaranes($result[0]->pedido_id, [$result[0]->presupuesto_id], $user);
|
|
$response['data']->createAlbaran = true;
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
|
|
|
|
|
|
private function generateEnvio($pedido){
|
|
|
|
// Se obtiene los datos de att, direccion, ciudad, cp, telefono, pais_id
|
|
$modelPedidoLinea = model('App\Models\Pedidos\PedidoLineaModel');
|
|
$lineasPedido = $modelPedidoLinea->where('pedido_id', $pedido->id)
|
|
$multienvio = false;
|
|
->findAll();
|
|
if(empty($lineasPedido)){
|
|
return [
|
|
'status' => false,
|
|
'message' => lang('Logistica.errors.noLines'),
|
|
];
|
|
}
|
|
if(count($lineasPedido) > 1){
|
|
$multienvio = true;
|
|
}
|
|
if(!$multienvio){
|
|
// solo hay una dirección, se obtiene de los albaranes
|
|
$AlbaranModel = model('App\Models\Pedidos\AlbaranModel');
|
|
$datosEnvio = $AlbaranModel
|
|
->select('albaranes.att_albaran as att, albaranes.direccion_albaran as direccion,
|
|
presupuestos_direcciones.ciudad as ciudad, presupuestos_direcciones.cp as cp, presupuestos_direcciones.telefono as telefono,
|
|
presupuestos_direcciones.pais_id as pais_id, albaranes_linea.cantidad as cantidad, albaranes_linea.cajas as cajas')
|
|
->where('albaranes.pedido_id', $pedido->id)
|
|
->where('albaranes_linea.cajas !=', null)
|
|
->join('albaranes_linea', 'albaranes_linea.albaran_id = albaranes.id')
|
|
->join('presupuestos_direcciones', 'presupuestos_direcciones.id = albaran.presupuesto_id')
|
|
->first();
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|