Files
safekat/ci4/app/Controllers/Pedidos/Albaran.php
2024-06-15 19:42:36 +02:00

132 lines
4.2 KiB
PHP

<?php
namespace App\Controllers\Pedidos;
use App\Models\Pedidos\AlbaranModel;
class Albaran extends \App\Controllers\BaseResourceController
{
protected $modelName = AlbaranModel::class;
protected $format = 'json';
protected static $singularObjectNameCc = 'albaran';
protected static $singularObjectName = 'Albaran';
protected static $pluralObjectName = 'Albaranes';
protected static $controllerSlug = 'albaran';
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
}
public function index()
{
}
public function delete($id = null)
{
return [];
}
public function add()
{
if ($this->request->isAJAX()) {
$user = auth()->user()->id;
$newTokenHash = csrf_hash();
$csrfTokenName = csrf_token();
$reqData = $this->request->getPost();
$pedido_id = $reqData['pedido_id'] ?? 0;
$presupuestos_id = $reqData['presupuestos_id'] ?? 0;
$return_data = $this->model->generarAlbaranes($pedido_id, $presupuestos_id, $user);
$data = [
'data' => $return_data,
$csrfTokenName => $newTokenHash
];
return $this->respond($data);
}
else {
return $this->failUnauthorized('Invalid request', 403);
}
}
public function update($id = null){
if ($this->request->isAJAX()) {
$newTokenHash = csrf_hash();
$csrfTokenName = csrf_token();
if ($id == null) :
return $this->redirect2listView();
endif;
$id = filter_var($id, FILTER_SANITIZE_URL);
$albaranEntity = $this->model->find($id);
if ($albaranEntity == false) :
$message = lang('Basic.global.notFoundWithIdErr', [mb_strtolower(lang('Pedidos.albaran')), $id]);
return $this->redirect2listView('sweet-error', $message);
endif;
if ($this->request->getPost()) :
$nullIfEmpty = true; // !(phpversion() >= '8.1');
$postData = $this->request->getPost();
$sanitizedData = $this->sanitized($postData, $nullIfEmpty);
// JJO
$sanitizedData['user_updated_id'] = auth()->user()->id;
$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('Pedidos.albaran'))]);
$this->session->setFlashdata('formErrors', $this->model->errors());
endif;
$albaranEntity->fill($sanitizedData);
endif;
if ($noException && $successfulResult) :
$id = $albaranEntity->id ?? $id;
$message = lang('Basic.global.updateSuccess', [lang('Basic.global.record')]) . '.';
$data = [
'error' => 0,
$csrfTokenName => $newTokenHash
];
return $this->respond($data);
endif; // $noException && $successfulResult
endif; // ($requestMethod === 'post')
$data = [
'error' => 1,
$csrfTokenName => $newTokenHash
];
return $this->respond($data);
}
else {
return $this->failUnauthorized('Invalid request', 403);
}
}
}