imposiciones

This commit is contained in:
amazuecos
2025-04-20 19:26:07 +02:00
parent 52a4e7d37b
commit c3d38e29a4
34 changed files with 1676 additions and 496 deletions

View File

@ -2,37 +2,41 @@
namespace App\Controllers\Configuracion;
use App\Controllers\BaseController;
use App\Controllers\BaseResourceController;
use App\Models\Collection;
use App\Entities\Configuracion\Imposicion;
use App\Models\Configuracion\ImposicionEsquemaModel;
use App\Models\Configuracion\ImposicionModel;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Validation\Validation;
use Hermawan\DataTables\DataTable;
use Psr\Log\LoggerInterface;
class Imposiciones extends \App\Controllers\BaseResourceController
class Imposiciones extends BaseController
{
protected $modelName = ImposicionModel::class;
protected $format = 'json';
protected static $singularObjectName = 'Imposicion';
protected static $singularObjectNameCc = 'imposicion';
protected static $pluralObjectName = 'Imposiciones';
protected static $pluralObjectNameCc = 'imposiciones';
protected ImposicionModel $model;
protected ImposicionEsquemaModel $imposicionEsquemaModel;
protected static $controllerSlug = 'imposiciones';
protected $format = 'json';
protected static $viewPath = 'themes/vuexy/form/configuracion/imposiciones/';
protected $indexRoute = 'imposicionList';
protected array $viewData = [];
protected Validation $validation;
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
$this->viewData['pageTitle'] = lang('Imposiciones.moduleTitle');
$this->viewData['usingSweetAlert'] = true;
$this->model = model($this->modelName);
$this->validation = service("validation");
$this->imposicionEsquemaModel = model(ImposicionEsquemaModel::class);
parent::initController($request, $response, $logger);
}
@ -47,7 +51,10 @@ class Imposiciones extends \App\Controllers\BaseResourceController
'usingServerSideDataTable' => true,
];
$this->viewData['breadcrumb'] = [
['title' => lang("App.menu_imposiciones"), 'route' => route_to("imposicionList"), 'active' => true],
];
$viewData = array_merge($this->viewData, $viewData); // merge any possible values from the parent controller class
return view(static::$viewPath . 'viewImposicionList', $viewData);
@ -56,234 +63,147 @@ class Imposiciones extends \App\Controllers\BaseResourceController
public function add()
{
$this->viewData['breadcrumb'] = [
['title' => lang("App.menu_imposiciones"), 'route' => route_to("imposicionList"), 'active' => true],
if ($this->request->getPost()) :
$nullIfEmpty = true; // !(phpversion() >= '8.1');
$postData = $this->request->getPost();
$sanitizedData = $this->sanitized($postData, $nullIfEmpty);
$noException = true;
if ($successfulResult = $this->canValidate()) : // if ($successfulResult = $this->validate($this->formValidationRules) ) :
if ($this->canValidate()) :
try {
$successfulResult = $this->model->skipValidation(true)->save($sanitizedData);
} catch (\Exception $e) {
$noException = false;
$this->dealWithException($e);
}
else:
$this->viewData['errorMessage'] = lang('Basic.global.formErr1', [lang('Basic.global.record')]);
$this->session->setFlashdata('formErrors', $this->model->errors());
endif;
$thenRedirect = true; // Change this to false if you want your user to stay on the form after submission
endif;
if ($noException && $successfulResult) :
$id = $this->model->db->insertID();
$message = lang('Basic.global.saveSuccess', [lang('Basic.global.record')]) . '.';
if ($thenRedirect) :
if (!empty($this->indexRoute)) :
return redirect()->to(route_to($this->indexRoute))->with('sweet-success', $message);
else:
return $this->redirect2listView('sweet-success', $message);
endif;
else:
$this->session->setFlashData('sweet-success', $message);
endif;
endif; // $noException && $successfulResult
endif; // ($requestMethod === 'post')
$this->viewData['imposicion'] = isset($sanitizedData) ? new Imposicion($sanitizedData) : new Imposicion();
$this->viewData['orientacionList'] = $this->getOrientacionOptions();
$this->viewData['formAction'] = route_to('createImposicion');
$this->viewData['boxTitle'] = lang('Basic.global.addNew') . ' ' . lang('Imposiciones.moduleTitle') . ' ' . lang('Basic.global.addNewSuffix');
return $this->displayForm(__METHOD__);
} // end function add()
public function edit($requestedId = null)
];
return view(static::$viewPath . 'viewImposicionNewForm', $this->viewData);
}
public function add_esquema()
{
$this->viewData['breadcrumb'] = [
['title' => lang("App.menu_imposiciones"), 'route' => route_to("imposicionList"), 'active' => true],
];
$this->viewData["imposicion_esquema"] = null;
if ($requestedId == null) :
return $this->redirect2listView();
endif;
$id = filter_var($requestedId, FILTER_SANITIZE_URL);
$imposicion = $this->model->find($id);
return view(static::$viewPath . 'viewImposicionEsquemaForm', $this->viewData);
}
public function edit($imposicion_id = null)
{
if ($imposicion_id) {
$this->viewData["imposicion"] = $this->model->find($imposicion_id);
$this->viewData['breadcrumb'] = [
['title' => lang("App.menu_imposiciones"), 'route' => route_to("imposicionList"), 'active' => false],
['title' => $this->viewData["imposicion"]->full_name, 'route' => route_to("updateImposicionForm", $imposicion_id), 'active' => true],
if ($imposicion == false) :
$message = lang('Basic.global.notFoundWithIdErr', [mb_strtolower(lang('Imposiciones.imposicion')), $id]);
return $this->redirect2listView('sweet-error', $message);
endif;
];
}
return view(static::$viewPath . 'viewImposicionForm', $this->viewData);
}
public function edit_imposicion_esquema($imposicion_esquema_id = null)
{
if ($imposicion_esquema_id) {
$this->viewData["imposicion_esquema"] = $this->imposicionEsquemaModel->find($imposicion_esquema_id);
$this->viewData['breadcrumb'] = [
['title' => lang("App.menu_imposiciones"), 'route' => route_to("imposicionList"), 'active' => false],
['title' => $this->viewData["imposicion_esquema"]->name, 'route' => route_to("updateImposicionEsquemaForm", $imposicion_esquema_id), 'active' => true],
if ($this->request->getPost()) :
$nullIfEmpty = true; // !(phpversion() >= '8.1');
$postData = $this->request->getPost();
$sanitizedData = $this->sanitized($postData, $nullIfEmpty);
$noException = true;
if ($successfulResult = $this->canValidate()) : // if ($successfulResult = $this->validate($this->formValidationRules) ) :
if ($this->canValidate()) :
try {
$successfulResult = $this->model->skipValidation(true)->update($id, $sanitizedData);
} catch (\Exception $e) {
$noException = false;
$this->dealWithException($e);
}
else:
$this->viewData['warningMessage'] = lang('Basic.global.formErr1', [mb_strtolower(lang('Imposiciones.imposicion'))]);
$this->session->setFlashdata('formErrors', $this->model->errors());
endif;
$imposicion->fill($sanitizedData);
$thenRedirect = false;
endif;
if ($noException && $successfulResult) :
$id = $imposicion->id ?? $id;
$message = lang('Basic.global.updateSuccess', [lang('Basic.global.record')]) . '.';
if ($thenRedirect) :
if (!empty($this->indexRoute)) :
return redirect()->to(route_to($this->indexRoute))->with('sweet-success', $message);
else:
return $this->redirect2listView('sweet-success', $message);
endif;
else:
$this->session->setFlashData('sweet-success', $message);
endif;
endif; // $noException && $successfulResult
endif; // ($requestMethod === 'post')
$this->viewData['imposicion'] = $imposicion;
$this->viewData['orientacionList'] = $this->getOrientacionOptions();
$this->viewData['formAction'] = route_to('updateImposicion', $id);
$this->viewData['boxTitle'] = lang('Basic.global.edit2') . ' ' . lang('Imposiciones.moduleTitle') . ' ' . lang('Basic.global.edit3');
return $this->displayForm(__METHOD__, $id);
} // end function edit(...)
];
}
return view(static::$viewPath . 'viewImposicionEsquemaForm', $this->viewData);
}
public function update($imposicion_id = null)
{
if ($imposicion_id) {
$bodyData = $this->request->getPost();
$status = $this->model->update($imposicion_id, $bodyData);
if ($status) {
$imposicionEntity = $this->model->find($imposicion_id);
} else {
return $this->response->setJSON(["message" => lang("App.global_alert_save_error"), "errors" => $this->model->errors(), "status" => true])->setStatusCode(400);
}
return $this->response->setJSON(["message" => lang("App.global_alert_save_success"), "status" => $status, "data" => $imposicionEntity]);
} else {
return $this->response->setJSON(["message" => lang("App.global_alert_save_error"), "message" => "", "status" => false])->setStatusCode(400);
}
}
public function update_imposicion_esquema($imposicion_esquema_id = null)
{
if ($imposicion_esquema_id) {
$bodyData = $this->request->getPost();
$status = $this->imposicionEsquemaModel->update($imposicion_esquema_id, $bodyData);
if ($status) {
$imposicionEsquemaEntity = $this->imposicionEsquemaModel->find($imposicion_esquema_id);
} else {
return $this->response->setJSON(["message" => lang("App.global_alert_save_error"), "errors" => $this->imposicionEsquemaModel->errors(), "status" => true])->setStatusCode(400);
}
return $this->response->setJSON(["message" => lang("App.global_alert_save_success"), "status" => $status, "data" => $imposicionEsquemaEntity]);
} else {
return $this->response->setJSON(["message" => lang("App.global_alert_save_error"), "message" => "", "status" => false])->setStatusCode(400);
}
}
public function delete($imposicion_id = null)
{
$status = $this->model->delete($imposicion_id);
return $this->response->setJSON(["message" => lang("App.user_alert_delete"), "status" => true]);
}
public function delete_imposicion_esquema($imposicion_esquema_id = null)
{
$status = $this->imposicionEsquemaModel->delete($imposicion_esquema_id);
return $this->response->setJSON(["message" => lang("App.user_alert_delete"), "status" => $status]);
}
public function datatable()
{
if ($this->request->isAJAX()) {
$reqData = $this->request->getPost();
if (!isset($reqData['draw']) || !isset($reqData['columns'])) {
$errstr = 'No data available in response to this specific request.';
$response = $this->respond(Collection::datatable([], 0, 0, $errstr), 400, $errstr);
return $response;
}
$start = $reqData['start'] ?? 0;
$length = $reqData['length'] ?? 5;
$search = $reqData['search']['value'];
$requestedOrder = $reqData['order']['0']['column'] ?? 1;
$order = ImposicionModel::SORTABLE[$requestedOrder > 0 ? $requestedOrder : 1];
$dir = $reqData['order']['0']['dir'] ?? 'asc';
$resourceData = $this->model->getResource($search)->orderBy($order, $dir)->limit($length, $start)->get()->getResultObject();
return $this->respond(Collection::datatable(
$resourceData,
$this->model->getResource()->countAllResults(),
$this->model->getResource($search)->countAllResults()
));
} else {
return $this->failUnauthorized('Invalid request', 403);
}
$q = $this->model->queryDatatable();
return DataTable::of($q)
->add(
'action',
fn($q) => "<div class='btn-group btn-group-md gap-2 w-100 d-flex justify-space-between'>". ImposicionModel::datatable_buttons($q->id)."</div>"
)
->toJson(true);
}
public function allItemsSelect()
public function datatable_imposicion_esquema()
{
if ($this->request->isAJAX()) {
$onlyActiveOnes = true;
$reqVal = $this->request->getPost('val') ?? 'id';
$menu = $this->model->getAllForMenu($reqVal . ', ancho', 'ancho', $onlyActiveOnes, false);
$nonItem = new \stdClass;
$nonItem->id = '';
$nonItem->ancho = '- ' . lang('Basic.global.None') . ' -';
array_unshift($menu, $nonItem);
$newTokenHash = csrf_hash();
$csrfTokenName = csrf_token();
$data = [
'menu' => $menu,
$csrfTokenName => $newTokenHash
];
return $this->respond($data);
} else {
return $this->failUnauthorized('Invalid request', 403);
}
}
public function menuItems()
{
if ($this->request->isAJAX()) {
$searchStr = goSanitize($this->request->getPost('searchTerm'))[0];
$reqId = goSanitize($this->request->getPost('id'))[0];
$reqText = goSanitize($this->request->getPost('text'))[0];
$onlyActiveOnes = false;
$columns2select = [$reqId ?? 'id', $reqText ?? 'ancho'];
$onlyActiveOnes = false;
$menu = $this->model->getSelect2MenuItems($columns2select, $columns2select[1], $onlyActiveOnes, $searchStr);
$nonItem = new \stdClass;
$nonItem->id = '';
$nonItem->text = '- ' . lang('Basic.global.None') . ' -';
array_unshift($menu, $nonItem);
$newTokenHash = csrf_hash();
$csrfTokenName = csrf_token();
$data = [
'menu' => $menu,
$csrfTokenName => $newTokenHash
];
return $this->respond($data);
} else {
return $this->failUnauthorized('Invalid request', 403);
}
$q = $this->imposicionEsquemaModel->queryDatatable();
return DataTable::of($q)
->add(
'action',
fn($q) => "<div class='btn-group btn-group-md gap-2'>". ImposicionEsquemaModel::datatable_buttons($q->id)."</div>"
)
->toJson(true);
}
protected function getOrientacionOptions()
{
$orientacionOptions = [
'' => lang('Basic.global.pleaseSelect'),
'H' => 'H',
'V' => 'V',
];
return $orientacionOptions;
}
public function selectImposicion()
{
$data = $this->model->querySelect($this->request->getGet('q'));
return $this->response->setJSON($data);
}
public function selectImposicionEsquema()
{
$data = $this->imposicionEsquemaModel->querySelect($this->request->getGet('q'));
return $this->response->setJSON($data);
}
public function find_imposicion(int $imposicion_id)
{
$imposicionEntity = $this->model->find($imposicion_id)->withImposicionEsquema();
return $this->response->setJSON($imposicionEntity);
}
public function find_imposicion_esquema(int $imposicion_esquema_id)
{
$imposicionEsquemaEntity = $this->imposicionEsquemaModel->find($imposicion_esquema_id);
return $this->response->setJSON($imposicionEsquemaEntity);
}
public function create()
{
$bodyData = $this->request->getPost();
$createdId = $this->model->insert($bodyData);
if ($createdId) {
return $this->response->setJSON(["message" => lang("App.global_alert_save_success"), "status" => true]);
} else {
return $this->response->setJSON(["message" => lang("App.global_alert_save_error"), "errors" => $this->model->errors(), "status" => true])->setStatusCode(400);
}
}
public function create_imposicion_esquema()
{
$bodyData = $this->request->getPost();
$createdId = $this->imposicionEsquemaModel->insert($bodyData);
if ($createdId) {
return $this->response->setJSON(["message" => lang("App.global_alert_save_success"), "status" => true]);
} else {
return $this->response->setJSON(["message" => lang("App.global_alert_save_error"), "errors" => $this->imposicionEsquemaModel->errors(), "status" => true])->setStatusCode(400);
}
}
}