ModalidadEntregaController.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace v1\controllers;
  3. use common\data\Respuesta;
  4. use common\rest\AuthController;
  5. use yii\db\Expression;
  6. class ModalidadEntregaController extends AuthController {
  7. public $modelClass = "v1\models\ModalidadEntrega";
  8. public function actionIndex() {
  9. $id = trim($this->req->get("id", ""));
  10. $buscar = trim($this->req->get("q", ""));
  11. $query = $this->queryInicial;
  12. if ($id !== "") {
  13. $query->andWhere(["id" => $id]);
  14. }
  15. if ($buscar) {
  16. $query->andWhere([
  17. "OR",
  18. "f_unaccent([[nombre]]) ilike f_unaccent(:q)",
  19. ])->addParams([':q' => "%{$buscar}%"]);
  20. }
  21. return new Respuesta($query, $this->limite, $this->pagina, $this->ordenar);
  22. }
  23. public function actionGuardar() {
  24. $id = trim($this->req->getBodyParam("id", ""));
  25. $modelo = null;
  26. if ($id !== "") {
  27. $modelo = $this->modelClass::findOne($id);
  28. }
  29. $tran = \Yii::$app->getDb()->beginTransaction();
  30. try {
  31. if ($modelo === null) {
  32. $modelo = new $this->modelClass();
  33. $modelo->creado = new Expression('now()');
  34. } else {
  35. $modelo->modificado = new Expression('now()');
  36. }
  37. if (!$modelo->save()) {
  38. return (new Respuesta($modelo))
  39. ->mensaje("Hubo un problema al guardar el registro de Entrega");
  40. }
  41. $tran->commit();
  42. $modelo->refresh();
  43. return (new Respuesta($modelo))
  44. ->mensaje("Registro de Entrega guardado con éxito.");
  45. } catch (\Exception $e) {
  46. $tran->rollBack();
  47. return (new Respuesta())
  48. ->esError($e)
  49. ->mensaje("Hubo un error en el servidor");
  50. }
  51. }
  52. public function actionEliminar() {
  53. $id = intval($this->req->getBodyParam("id", null));
  54. $modelo = null;
  55. if ($id !== "") {
  56. $modelo = $this->modelClass::findOne(["id" => $id]);
  57. }
  58. if ($modelo === null) {
  59. return (new Respuesta())
  60. ->esError()
  61. ->mensaje("Registro de Entrega no encontrado");
  62. }
  63. $modelo->eliminado = new Expression('now()');
  64. if (!$modelo->save()) {
  65. return (new Respuesta($modelo))
  66. ->mensaje("No se pudo eliminar el registro de Entrega");
  67. }
  68. return (new Respuesta())
  69. ->mensaje("Registro de Entrega eliminado");
  70. }
  71. }