Files
safekat/ci4/app/Controllers/Logistica/LogisticaController.php

298 lines
9.9 KiB
PHP

<?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' => "javascript:void(0);", '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 selectorEnvios($tipoEnvio = null)
{
$viewData = [
'currentModule' => static::$controllerSlug,
'boxTitle' => lang('Logistica.envioSimpleMultiple'),
'usingServerSideDataTable' => true,
'tipoEnvio' => $tipoEnvio,
];
$viewData = array_merge($this->viewData, $viewData); // merge any possible values from the parent controller class
return view(static::$viewPath . 'viewLogisticaSelectEnvios', $viewData);
}
public function searchPedidoOrISBN($search = "", $envio_id = null)
{
if (empty($search)) {
$result = [
'status' => false,
'message' => lang('Logistica.errors.noDataToFind'),
];
return $this->response->setJSON($result);
}
$result = LogisticaService::findPedidoOrISBN($search);
return $this->response->setJSON($result);
}
public function selectAddEnvioLinea()
{
if ($this->request->isAJAX()) {
$query = LogisticaService::findLineaEnvioPorEnvio($this->request->getGet('envio'));
if ($this->request->getGet("q")) {
$query->groupStart()
->orLike("p.id", $this->request->getGet("q"))
->orLike("pr.titulo", $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()
{
$model = model('App\Models\Logistica\EnvioModel');
$q = $model->getDatatableQuery();
$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'));
}
$envioEntity->nextCaja = model('App\Models\Logistica\EnvioLineaModel')->getMaxCaja();
$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 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 . '">';
}
)
->add("action", callback: function ($q) {
return '
<div class="btn-group btn-group-sm">
<a href="javascript:void(0);"><i class="ti ti-pencil ti-sm btn-edit mx-2" data-id="' . $q->id . '"></i></a>
</div>
';
})
->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>';
}
);;
return $result->toJson(returnAsObject: true);
}
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 updateUnidadesEnvio()
{
$id = $this->request->getPost('id');
$unidades = $this->request->getPost('unidades_envio');
if (!$id || !$unidades || intval($unidades) <= 0) {
return $this->response->setJSON([
'status' => false,
'message' => 'Datos inválidos'
]);
}
$model = model('App\Models\Logistica\EnvioLineaModel');
$updated = $model->update($id, [
'unidades_envio' => $unidades,
]);
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'
]);
}
}