HerramientaInventarioController.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace v1\controllers;
  3. use common\data\Respuesta;
  4. use common\rest\AuthController;
  5. use yii\db\Expression;
  6. class HerramientaInventarioController extends AuthController {
  7. public $modelClass = "v1\models\HerramientaInventario";
  8. public function actionIndex() {
  9. $id = trim($this->req->get("id", ""));
  10. $buscar = trim($this->req->get("q", ""));
  11. $herramienta = trim($this ->req->get("herramienta",""));
  12. $cantidad = trim($this ->req->get("cantidad",""));
  13. $inicio = trim($this->req->get("inicio", ""));
  14. $fin = trim($this->req->get("fin", ""));
  15. $query = $this->queryInicial;
  16. if($id > 0) {
  17. $query->andWhere(["id" => $id]);
  18. }
  19. if($herramienta){
  20. $query->andWhere( ['idHerramienta'=>$herramienta]);
  21. }
  22. if($cantidad){
  23. $query->andWhere( ['like', 'CAST(cantidad AS TEXT)', (string)$cantidad]);
  24. }
  25. if($buscar) {
  26. $query->andWhere([
  27. "OR",
  28. ["ilike", "cantidad", $buscar]
  29. ]);
  30. }
  31. if ($inicio !== "" && $fin !== "") {
  32. $query->andWhere(['>=', '[[fechaIngreso]]', $inicio])
  33. ->andWhere(['<=', '[[fechaIngreso]]', $fin]);
  34. }
  35. return new Respuesta($query, $this->limite, $this->pagina, $this->ordenar);
  36. }
  37. public function actionGuardar() {
  38. $id = trim($this->req->getBodyParam("id", ""));
  39. $modelo = null;
  40. if($id !== "") {
  41. $modelo = $this->modelClass::findOne($id);
  42. }
  43. if($modelo === null) {
  44. $modelo = new $this->modelClass();
  45. $modelo->uuid();
  46. $modelo->creado = new Expression('now()');
  47. } else {
  48. $modelo->modificado = new Expression('now()');
  49. }
  50. $modelo->load($this->req->getBodyParams(), '');
  51. if (!$modelo->save()) {
  52. return (new Respuesta($modelo))
  53. ->mensaje("Hubo un problema al guardar la herramienta en el inventario");
  54. }
  55. $modelo->refresh();
  56. return (new Respuesta($modelo))
  57. ->mensaje("Herramienta en el inventario guardada");
  58. }
  59. public function actionEliminar() {
  60. $id = trim($this->req->getBodyParam("id", ""));
  61. $modelo = null;
  62. if($id !== "") {
  63. $modelo = $this->modelClass::findOne(["id" => $id]);
  64. }
  65. if($modelo === null) {
  66. return (new Respuesta())
  67. ->esError()
  68. ->mensaje("Herramienta en el inventario no encontrada");
  69. }
  70. $modelo->eliminado = new Expression('now()');
  71. if(!$modelo->save()) {
  72. return (new Respuesta($modelo))
  73. ->mensaje("No se pudo eliminar herramienta del inventario");
  74. }
  75. return (new Respuesta())
  76. ->mensaje("Herramienta del inventario eliminada");
  77. }
  78. }