mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
601 lines
20 KiB
PHP
Executable File
601 lines
20 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Controllers\Logistica;
|
|
|
|
use App\Controllers\BaseController;
|
|
use App\Services\ImpresoraEtiquetaService;
|
|
use App\Services\LogisticaService;
|
|
use CodeIgniter\HTTP\RequestInterface;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
use Hermawan\DataTables\DataTable;
|
|
|
|
class LogisticaController extends BaseController
|
|
{
|
|
|
|
protected ImpresoraEtiquetaService $impresoraEtiquetaService;
|
|
protected string $locale;
|
|
protected array $viewData;
|
|
|
|
protected static $controllerSlug = 'logistica';
|
|
protected static $viewPath = 'themes/vuexy/form/logistica/';
|
|
|
|
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
|
|
{
|
|
$this->impresoraEtiquetaService = service('impresora_etiqueta');
|
|
$this->locale = session()->get('lang');
|
|
|
|
$this->viewData['pageTitle'] = lang('Logistica.logistica');
|
|
|
|
// Breadcrumbs
|
|
$this->viewData['breadcrumb'] = [
|
|
['title' => lang("App.menu_logistica"), 'route' => route_to("LogisticaPanel"), 'active' => false],
|
|
];
|
|
|
|
|
|
parent::initController($request, $response, $logger);
|
|
}
|
|
public function print_test_label()
|
|
{
|
|
$etiquetaData = $this->impresoraEtiquetaService->test();
|
|
$responseMessage = $etiquetaData["status"] ? "OK" : "ERROR";
|
|
return $this->response->setJSON(["message" => $responseMessage, "data" => $etiquetaData, "status" => $etiquetaData["status"]]);
|
|
}
|
|
|
|
public function panel()
|
|
{
|
|
$viewData = [
|
|
'currentModule' => static::$controllerSlug,
|
|
'boxTitle' => lang('Logistica.panel'),
|
|
'pageSubTitle' => 'Panel',
|
|
'usingServerSideDataTable' => true,
|
|
];
|
|
|
|
$viewData = array_merge($this->viewData, $viewData); // merge any possible values from the parent controller class
|
|
|
|
return view(static::$viewPath . 'viewPanelLogistica', $viewData);
|
|
}
|
|
|
|
public function gestionEnvios()
|
|
{
|
|
$viewData = [
|
|
'currentModule' => static::$controllerSlug,
|
|
'boxTitle' => lang('Logistica.gestionEnvios'),
|
|
'usingServerSideDataTable' => true,
|
|
'tipo_envio' => 'estandar',
|
|
];
|
|
|
|
$viewData = array_merge($this->viewData, $viewData); // merge any possible values from the parent controller class
|
|
|
|
return view(static::$viewPath . 'viewLogisticaSelectEnvios', $viewData);
|
|
}
|
|
|
|
|
|
public function gestionEnviosFerros()
|
|
{
|
|
$viewData = [
|
|
'currentModule' => static::$controllerSlug,
|
|
'boxTitle' => lang('Logistica.envioFerros'),
|
|
'usingServerSideDataTable' => true,
|
|
'tipo_envio' => 'ferro_prototipo',
|
|
];
|
|
|
|
$viewData = array_merge($this->viewData, $viewData); // merge any possible values from the parent controller class
|
|
|
|
return view(static::$viewPath . 'viewLogisticaSelectEnvios', $viewData);
|
|
}
|
|
|
|
public function etiquetasLogistica()
|
|
{
|
|
$viewData = [
|
|
'currentModule' => static::$controllerSlug,
|
|
'boxTitle' => lang('Logistica.etiquetasTitulos'),
|
|
'usingServerSideDataTable' => true,
|
|
];
|
|
|
|
$viewData = array_merge($this->viewData, $viewData); // merge any possible values from the parent controller class
|
|
|
|
return view(static::$viewPath . 'viewImpresionEtiquetas', $viewData);
|
|
}
|
|
|
|
public function listAlbaranes()
|
|
{
|
|
$viewData = [
|
|
'currentModule' => static::$controllerSlug,
|
|
'boxTitle' => lang('Albaran.albaranes'),
|
|
'usingServerSideDataTable' => true,
|
|
];
|
|
|
|
$viewData = array_merge($this->viewData, $viewData); // merge any possible values from the parent controller class
|
|
|
|
return view(static::$viewPath . '/albaranes/viewAlbaranesList', $viewData);
|
|
}
|
|
|
|
public function findForNewEnvio()
|
|
{
|
|
|
|
if ($this->request->isAJAX()) {
|
|
|
|
$tipo_envio = $this->request->getGet('tipo_envio') ?? 'estandar';
|
|
|
|
if ($tipo_envio == 'ferro_prototipo') {
|
|
$query = LogisticaService::findForNewEnvioFerro();
|
|
} else {
|
|
$query = LogisticaService::findForNewEnvio();
|
|
}
|
|
if ($this->request->getGet("q")) {
|
|
$query->groupStart()
|
|
->orLike("id", $this->request->getGet("q"))
|
|
->orLike("name", $this->request->getGet("q"))
|
|
->groupEnd();
|
|
}
|
|
|
|
|
|
$result = $query->orderBy("name", "asc")->get()->getResultObject();
|
|
|
|
$query = model('App\Models\Logistica\EnvioModel')->db->getLastQuery();
|
|
|
|
return $this->response->setJSON($result);
|
|
} else {
|
|
return $this->failUnauthorized('Invalid request', 403);
|
|
}
|
|
}
|
|
|
|
public function selectDireccionForEnvio()
|
|
{
|
|
|
|
if ($this->request->isAJAX()) {
|
|
$ot = $this->request->getGet('ot_id');
|
|
if ($ot == null || $ot == 0) {
|
|
return [];
|
|
}
|
|
$searchVal = $this->request->getGet("q") ?? "";
|
|
$result = LogisticaService::findDireccionesNewEnvio($ot, $searchVal);
|
|
|
|
return $this->response->setJSON($result);
|
|
} else {
|
|
return $this->failUnauthorized('Invalid request', 403);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function generarEnvio()
|
|
{
|
|
if ($this->request->isAJAX()) {
|
|
|
|
$ot_id = $this->request->getPost('ot_id');
|
|
$direccion = $this->request->getPost('direccion') ?? "";
|
|
$result = LogisticaService::generateEnvio($ot_id, $direccion);
|
|
return $this->response->setJSON($result);
|
|
} else {
|
|
return $this->failUnauthorized('Invalid request', 403);
|
|
}
|
|
}
|
|
|
|
|
|
public function generarEnvioFerro()
|
|
{
|
|
if ($this->request->isAJAX()) {
|
|
|
|
$ot_id = $this->request->getPost('ot_id');
|
|
$result = LogisticaService::generateEnvioFerro($ot_id);
|
|
return $this->response->setJSON($result);
|
|
} else {
|
|
return $this->failUnauthorized('Invalid request', 403);
|
|
}
|
|
}
|
|
|
|
public function imprimirEtiquetas()
|
|
{
|
|
if ($this->request->isAJAX()) {
|
|
$envio_id = $this->request->getPost('envio_id');
|
|
$ids = $this->request->getPost('envio_lineas');
|
|
$cajas = $this->request->getPost('cajas');
|
|
$printer_id = $this->request->getPost('printer_id');
|
|
|
|
if ($cajas == null || $cajas == 0) {
|
|
return $this->response->setJSON([
|
|
'status' => false,
|
|
'message' => 'Cajas no válidas'
|
|
]);
|
|
}
|
|
|
|
$model = model('App\Models\Logistica\EnvioModel');
|
|
$envio = $model->select('envios.*, clientes.nombre as cliente')
|
|
->join('clientes', 'clientes.id = envios.cliente_id', 'left')
|
|
->where('envios.id', $envio_id)
|
|
->first();
|
|
if ($envio == null) {
|
|
return $this->response->setJSON([
|
|
'status' => false,
|
|
'message' => 'Envio no válido'
|
|
]);
|
|
}
|
|
|
|
$model = model('App\Models\Logistica\EnvioLineaModel');
|
|
$lineas = $model->select('envios_lineas.*, presupuestos.titulo as titulo, presupuestos.referencia_cliente as referencia_cliente')
|
|
->join('presupuestos', 'presupuestos.id = envios_lineas.presupuesto_id', 'left')
|
|
->whereIn('envios_lineas.id', $ids)->findAll();
|
|
if ($lineas == null) {
|
|
return $this->response->setJSON([
|
|
'status' => false,
|
|
'message' => 'Lineas no válidas'
|
|
]);
|
|
}
|
|
|
|
$modelImpresora = model('App\Models\Configuracion\ImpresoraEtiquetaModel');
|
|
$impresora = $modelImpresora->select('id, name, description, ip, port, user, pass')
|
|
->where('deleted_at', null)
|
|
->where('id', $printer_id)
|
|
->orderBy('name', 'asc')
|
|
->first();
|
|
if ($impresora == null) {
|
|
return $this->response->setJSON([
|
|
'status' => false,
|
|
'message' => 'Impresora no válida'
|
|
]);
|
|
}
|
|
|
|
$response = LogisticaService::generateEtiquetasTitulos($envio, $lineas, $impresora, $cajas);
|
|
return $this->response->setJSON($response);
|
|
|
|
} else {
|
|
return $this->failUnauthorized('Invalid request', 403);
|
|
}
|
|
}
|
|
|
|
public function selectAddEnvioLinea()
|
|
{
|
|
|
|
if ($this->request->isAJAX()) {
|
|
$query = LogisticaService::findLineaEnvioPorEnvio($this->request->getGet('envio'));
|
|
if ($this->request->getGet("q")) {
|
|
$query->groupStart()
|
|
->orLike("id", $this->request->getGet("q"))
|
|
->orLike("name", $this->request->getGet("q"))
|
|
->groupEnd();
|
|
}
|
|
|
|
|
|
$result = $query->orderBy("name", "asc")->get()->getResultObject();
|
|
|
|
return $this->response->setJSON($result);
|
|
} else {
|
|
return $this->failUnauthorized('Invalid request', 403);
|
|
}
|
|
}
|
|
|
|
public function addEnvioLinea()
|
|
{
|
|
|
|
if ($this->request->isAJAX()) {
|
|
$pedido_id = $this->request->getGet('pedido_id');
|
|
$envio_id = $this->request->getGet('envio_id');
|
|
$envioModel = model('App\Models\Logistica\EnvioModel');
|
|
$direccion = $envioModel->find($envio_id)->direccion;
|
|
|
|
$result = LogisticaService::addLineaEnvio($envio_id, $pedido_id, $direccion);
|
|
return $this->response->setJSON($result);
|
|
} else {
|
|
return $this->failUnauthorized('Invalid request', 403);
|
|
}
|
|
}
|
|
|
|
public function datatable_envios()
|
|
{
|
|
|
|
$otsFilter = $this->request->getGetPost('otsFilter');
|
|
$tipo_envio = $this->request->getGetPost('tipo_envio') ?? 'estandar';
|
|
|
|
$model = model('App\Models\Logistica\EnvioModel');
|
|
$q = $model->getDatatableQuery($tipo_envio);
|
|
|
|
if (!empty($otsFilter)) {
|
|
$q->groupStart();
|
|
$q->like('t5.id', $otsFilter);
|
|
$q->groupEnd();
|
|
}
|
|
|
|
|
|
$result = DataTable::of($q)
|
|
->edit(
|
|
"finalizado",
|
|
function ($row, $meta) {
|
|
if ($row->finalizado == 1)
|
|
return '<i class="ti ti-check"></i>';
|
|
else
|
|
return '<i class="ti ti-x"></i>';
|
|
}
|
|
)
|
|
->add("action", callback: function ($q) {
|
|
|
|
return '
|
|
<div class="btn-group btn-group-sm">
|
|
<a href="javascript:void(0);"><i class="ti ti-eye ti-sm btn-edit mx-2" data-id="' . $q->id . '"></i></a>
|
|
</div>
|
|
';
|
|
});
|
|
|
|
return $result->toJson(returnAsObject: true);
|
|
}
|
|
|
|
public function editEnvio($id = null)
|
|
{
|
|
|
|
if (empty($id)) {
|
|
return redirect()->to(base_url('logistica/selectEnvios/simple'))->with('error', lang('Logistica.errors.noEnvio'));
|
|
}
|
|
$model = model('App\Models\Logistica\EnvioModel');
|
|
$envioEntity = $model->select('envios.*, lg_paises.nombre as pais')
|
|
->join('lg_paises', 'lg_paises.id = envios.pais_id', 'left')
|
|
->where('envios.id', $id)
|
|
->first();
|
|
if (empty($envioEntity)) {
|
|
return redirect()->to(base_url('logistica/selectEnvios/simple'))->with('error', lang('Logistica.errors.noEnvio'));
|
|
}
|
|
|
|
$modelProveedor = model('App\Models\Compras\ProveedorModel');
|
|
$proveedor = $modelProveedor->select('id, nombre')
|
|
->where('deleted_at', null)
|
|
->where('id', $envioEntity->proveedor_id)
|
|
->orderBy('nombre', 'asc')
|
|
->first();
|
|
if (!empty($proveedor)) {
|
|
$envioEntity->proveedor_nombre = $proveedor->nombre;
|
|
}
|
|
|
|
$modelImpresora = model('App\Models\Configuracion\ImpresoraEtiquetaModel');
|
|
$impresoras = $modelImpresora->select('id, name, description')
|
|
->where('deleted_at', null)
|
|
->where('tipo', 1)
|
|
->orderBy('name', 'asc')
|
|
->findAll();
|
|
$envioEntity->impresoras = $impresoras;
|
|
|
|
$viewData = [
|
|
'currentModule' => static::$controllerSlug,
|
|
'boxTitle' => '<i class="ti ti-truck ti-xl"></i>' . ' ' . lang('Logistica.envio') . ' [' . $envioEntity->id . ']: ' . $envioEntity->direccion,
|
|
'usingServerSideDataTable' => true,
|
|
'envioEntity' => $envioEntity,
|
|
];
|
|
|
|
$viewData = array_merge($this->viewData, $viewData); // merge any possible values from the parent controller class
|
|
|
|
return view(static::$viewPath . 'viewEnvioEditForm', $viewData);
|
|
}
|
|
|
|
public function updateCajasEnvio()
|
|
{
|
|
if ($this->request->isAJAX()) {
|
|
$id = $this->request->getPost('id');
|
|
$cajas = $this->request->getPost('cajas');
|
|
$model = model('App\Models\Logistica\EnvioModel');
|
|
$result = $model->update($id, [
|
|
'cajas' => $cajas,
|
|
]);
|
|
return $this->response->setJSON([
|
|
"status" => $result,
|
|
]);
|
|
} else {
|
|
return $this->failUnauthorized('Invalid request', 403);
|
|
}
|
|
}
|
|
|
|
public function finalizarEnvio()
|
|
{
|
|
if ($this->request->isAJAX()) {
|
|
|
|
$id = $this->request->getPost('id') ?? null;
|
|
$finalizar_ots = $this->request->getPost('finalizar_ots') ?? false;
|
|
|
|
$result = LogisticaService::finalizarEnvio($id, $finalizar_ots);
|
|
return $this->response->setJSON($result);
|
|
} else {
|
|
return $this->failUnauthorized('Invalid request', 403);
|
|
}
|
|
}
|
|
|
|
public function ficharEmbalaje()
|
|
{
|
|
if ($this->request->isAJAX()) {
|
|
|
|
$ids = $this->request->getPost('ids') ?? [];
|
|
$result = LogisticaService::ficharEmbalaje($ids);
|
|
return $this->response->setJSON($result);
|
|
} else {
|
|
return $this->failUnauthorized('Invalid request', 403);
|
|
}
|
|
}
|
|
|
|
public function datatable_enviosEdit($idEnvio)
|
|
{
|
|
$model = model('App\Models\Logistica\EnvioLineaModel');
|
|
$q = $model->getDatatableQuery($idEnvio);
|
|
|
|
|
|
$result = DataTable::of($q)
|
|
->add(
|
|
"rowSelected",
|
|
callback: function ($q) {
|
|
return '<input type="checkbox" class="form-check-input checkbox-linea-envio" name="row_selected[]" value="' . $q->id . '">';
|
|
}
|
|
)
|
|
->edit(
|
|
"ordenTrabajo",
|
|
function ($row, $meta) {
|
|
return '<a href="' . base_url('produccion/ordentrabajo/edit/' . $row->ordenTrabajo) . '" target="_blank">' . $row->ordenTrabajo . '</a>';
|
|
}
|
|
)
|
|
->edit(
|
|
"pedido",
|
|
function ($row, $meta) {
|
|
return '<a href="' . base_url('pedidos/edit/' . $row->pedido) . '" target="_blank">' . $row->pedido . '</a>';
|
|
}
|
|
)
|
|
->edit(
|
|
"presupuesto",
|
|
function ($row, $meta) {
|
|
return '<a href="' . base_url('presupuestoadmin/edit/' . $row->presupuesto) . '" target="_blank">' . $row->presupuesto . '</a>';
|
|
}
|
|
)->edit(
|
|
"unidadesEnvio",
|
|
function ($row, $meta) {
|
|
if ($row->finalizado == 1 || $row->tipo_envio == 'ferro_prototipo') {
|
|
return $row->unidadesEnvio;
|
|
}
|
|
return '<input type="number" class="form-control input-lineas input-unidades text-center"
|
|
data-id="' . $row->id . '" data-name="unidades_envio" value="' . $row->unidadesEnvio . '">';
|
|
}
|
|
);
|
|
|
|
return $result->toJson(returnAsObject: true);
|
|
}
|
|
|
|
public function datatable_proximosEnvios($envio_id = null)
|
|
{
|
|
$q = LogisticaService::findNextEnvios($envio_id);
|
|
|
|
$result = DataTable::of($q)
|
|
->edit(
|
|
"ot",
|
|
function ($row, $meta) {
|
|
return '<a href="' . base_url('produccion/ordentrabajo/edit/' . $row->ot) . '" target="_blank">' . $row->ot . '</a>';
|
|
}
|
|
);
|
|
|
|
$result = $result->toJson(returnAsObject: true);
|
|
$query = model('App\Models\Logistica\EnvioModel')->db->getLastQuery();
|
|
return $result;
|
|
|
|
}
|
|
|
|
public function setCajaLinea()
|
|
{
|
|
|
|
if ($this->request->isAJAX()) {
|
|
$id = $this->request->getPost('id');
|
|
$caja = $this->request->getPost('caja');
|
|
|
|
|
|
$model = model('App\Models\Logistica\EnvioLineaModel');
|
|
$result = $model->update($id, [
|
|
'cajas' => $caja,
|
|
]);
|
|
return $this->response->setJSON([
|
|
"status" => $result,
|
|
]);
|
|
} else {
|
|
return $this->failUnauthorized('Invalid request', 403);
|
|
}
|
|
}
|
|
|
|
public function deleteLineas()
|
|
{
|
|
if ($this->request->isAJAX()) {
|
|
$ids = $this->request->getPost('ids');
|
|
$model = model('App\Models\Logistica\EnvioLineaModel');
|
|
$result = $model->delete($ids);
|
|
return $this->response->setJSON([
|
|
"status" => $result,
|
|
]);
|
|
} else {
|
|
return $this->failUnauthorized('Invalid request', 403);
|
|
}
|
|
}
|
|
|
|
public function updateLineaEnvio()
|
|
{
|
|
$id = $this->request->getPost('id');
|
|
$fieldName = $this->request->getPost('name');
|
|
$fieldValue = $this->request->getPost('value');
|
|
|
|
if (!$id || !$fieldName || ($fieldName == 'unidades_envio' && !$fieldValue)) {
|
|
return $this->response->setJSON([
|
|
'status' => false,
|
|
'message' => 'Datos inválidos'
|
|
]);
|
|
}
|
|
|
|
$model = model('App\Models\Logistica\EnvioLineaModel');
|
|
$updated = $model->update($id, [
|
|
"" . $fieldName => $fieldValue == "" ? null : $fieldValue,
|
|
]);
|
|
|
|
return $this->response->setJSON([
|
|
'status' => $updated,
|
|
'message' => $updated ? 'Actualizado' : 'Error al actualizar'
|
|
]);
|
|
}
|
|
|
|
public function updateCodigoSeguimiento()
|
|
{
|
|
$id = $this->request->getPost('id');
|
|
$fieldValue = $this->request->getPost('codigo_seguimiento');
|
|
|
|
if (!$id) {
|
|
return $this->response->setJSON([
|
|
'status' => false,
|
|
'message' => 'Datos inválidos'
|
|
]);
|
|
}
|
|
|
|
$model = model('App\Models\Logistica\EnvioModel');
|
|
$updated = $model->update($id, [
|
|
"codigo_seguimiento" => $fieldValue == "" ? null : $fieldValue,
|
|
]);
|
|
|
|
return $this->response->setJSON([
|
|
'status' => $updated,
|
|
'message' => $updated ? 'Actualizado' : 'Error al actualizar'
|
|
]);
|
|
}
|
|
|
|
public function updateProveedorEnvio()
|
|
{
|
|
$id = $this->request->getPost('id');
|
|
$fieldValue = $this->request->getPost('proveedor_id');
|
|
|
|
if (!$id) {
|
|
return $this->response->setJSON([
|
|
'status' => false,
|
|
'message' => 'Datos inválidos'
|
|
]);
|
|
}
|
|
|
|
$model = model('App\Models\Logistica\EnvioModel');
|
|
$updated = $model->update($id, [
|
|
"proveedor_id" => $fieldValue == "" ? null : $fieldValue,
|
|
]);
|
|
|
|
return $this->response->setJSON([
|
|
'status' => $updated,
|
|
'message' => $updated ? 'Actualizado' : 'Error al actualizar'
|
|
]);
|
|
}
|
|
|
|
public function saveComments()
|
|
{
|
|
$id = $this->request->getPost('id');
|
|
$comments = $this->request->getPost('comentarios');
|
|
|
|
if (!$id || !$comments) {
|
|
return $this->response->setJSON([
|
|
'status' => false,
|
|
'message' => 'Datos inválidos'
|
|
]);
|
|
}
|
|
|
|
$model = model('App\Models\Logistica\EnvioModel');
|
|
$updated = $model->update($id, [
|
|
'comentarios' => $comments,
|
|
]);
|
|
|
|
return $this->response->setJSON([
|
|
'status' => $updated,
|
|
'message' => $updated ? 'Actualizado' : 'Error al actualizar'
|
|
]);
|
|
}
|
|
|
|
}
|