|
@@ -2,6 +2,8 @@
|
|
|
|
|
|
namespace v1\controllers;
|
|
|
|
|
|
+use app\models\ObraEmpleado;
|
|
|
+use app\models\ObraHerramienta;
|
|
|
use common\data\Respuesta;
|
|
|
use common\rest\AuthController;
|
|
|
use yii\db\Expression;
|
|
@@ -13,29 +15,28 @@ class ObraController extends AuthController {
|
|
|
public function actionIndex() {
|
|
|
$id = trim($this->req->get("id", ""));
|
|
|
$buscar = trim($this->req->get("q", ""));
|
|
|
- $rangoInicio= trim($this->req->get("inicio",""));
|
|
|
- $rangoFin= trim($this->req->get("fin",""));
|
|
|
- $rangoI=trim($this->req->get("rinicio",""));
|
|
|
- $rangoF=trim($this->req->get("rfin",""));
|
|
|
+ $rangoInicio = trim($this->req->get("inicio", ""));
|
|
|
+ $rangoFin = trim($this->req->get("fin", ""));
|
|
|
+ $rangoI = trim($this->req->get("rinicio", ""));
|
|
|
+ $rangoF = trim($this->req->get("rfin", ""));
|
|
|
$query = $this->queryInicial;
|
|
|
|
|
|
- if($id > 0) {
|
|
|
+ if ($id > 0) {
|
|
|
$query->andWhere(["id" => $id]);
|
|
|
}
|
|
|
|
|
|
- if($rangoInicio !=="" && $rangoFin !==""){
|
|
|
- $query->andWhere(["between", "[[fechaInicio]]", $rangoInicio,$rangoFin]);
|
|
|
+ if ($rangoInicio !== "" && $rangoFin !== "") {
|
|
|
+ $query->andWhere(["between", "[[fechaInicio]]", $rangoInicio, $rangoFin]);
|
|
|
}
|
|
|
- if($rangoI !=="" && $rangoF !==""){
|
|
|
- $query->andWhere(["between", "[[fechaFinal]]", $rangoI,$rangoF]);
|
|
|
+ if ($rangoI !== "" && $rangoF !== "") {
|
|
|
+ $query->andWhere(["between", "[[fechaFinal]]", $rangoI, $rangoF]);
|
|
|
}
|
|
|
- if($buscar) {
|
|
|
-
|
|
|
+ if ($buscar) {
|
|
|
+
|
|
|
$query->andWhere([
|
|
|
"OR",
|
|
|
["ilike", "nombre", $buscar]
|
|
|
]);
|
|
|
-
|
|
|
}
|
|
|
|
|
|
return new Respuesta($query, $this->limite, $this->pagina, $this->ordenar);
|
|
@@ -43,53 +44,101 @@ class ObraController extends AuthController {
|
|
|
|
|
|
public function actionGuardar() {
|
|
|
$id = trim($this->req->getBodyParam("id", ""));
|
|
|
+ $herramienta = $this->req->getBodyParam("herramienta", []);
|
|
|
+ $empleado = $this->req->getBodyParam("empleado", []);
|
|
|
$modelo = null;
|
|
|
|
|
|
- if($id !== "") {
|
|
|
- $modelo = $this->modelClass::findOne($id);
|
|
|
- }
|
|
|
- if($modelo === null) {
|
|
|
- $modelo = new $this->modelClass();
|
|
|
- $modelo->uuid();
|
|
|
- $modelo->creado = new Expression('now()');
|
|
|
- } else {
|
|
|
- $modelo->modificado = new Expression('now()');
|
|
|
- }
|
|
|
- $modelo->load($this->req->getBodyParams(), '');
|
|
|
-
|
|
|
- $clave = $modelo->clave;
|
|
|
- $existeClave = $this->modelClass::find()->where(['clave' => $clave])->andWhere(['<>', 'id', $id])->exists();
|
|
|
-
|
|
|
- if ($existeClave) {
|
|
|
- return (new Respuesta())
|
|
|
- ->esError()
|
|
|
- ->mensaje("La clave '$clave' ya existe. Por favor, ingresa una clave diferente.");
|
|
|
- }
|
|
|
- $modelo->load($this->req->getBodyParams(), '');
|
|
|
- if (!$modelo->save()) {
|
|
|
+ $transaccion = \Yii::$app->db->beginTransaction();
|
|
|
+ try {
|
|
|
+
|
|
|
+ if ($id !== "") {
|
|
|
+ $modelo = $this->modelClass::findOne($id);
|
|
|
+ }
|
|
|
+ if ($modelo === null) {
|
|
|
+ $modelo = new $this->modelClass();
|
|
|
+ $modelo->uuid();
|
|
|
+ $modelo->creado = new Expression('now()');
|
|
|
+ } else {
|
|
|
+ $modelo->modificado = new Expression('now()');
|
|
|
+ }
|
|
|
+ $modelo->load($this->req->getBodyParams(), '');
|
|
|
+
|
|
|
+ $clave = $modelo->clave;
|
|
|
+ $existeClave = $this->modelClass::find()->where(['clave' => $clave])->andWhere(['<>', 'id', $id])->exists();
|
|
|
+
|
|
|
+ if ($existeClave) {
|
|
|
+ return (new Respuesta())
|
|
|
+ ->esError()
|
|
|
+ ->mensaje("La clave '$clave' ya existe. Por favor, ingresa una clave diferente.");
|
|
|
+ }
|
|
|
+ $modelo->load($this->req->getBodyParams(), '');
|
|
|
+ if (!$modelo->save()) {
|
|
|
+ return (new Respuesta($modelo))
|
|
|
+ ->mensaje("Hubo un problema al guardar la obra");
|
|
|
+ }
|
|
|
+
|
|
|
+ ObraEmpleado::deleteAll(['id' => $modelo->id]);
|
|
|
+
|
|
|
+ foreach ($empleado as $item) {
|
|
|
+ $idEmpleado = $item['idEmpleado'];
|
|
|
+ $interMedia = new ObraEmpleado();
|
|
|
+
|
|
|
+ $interMedia->idEmpleado = $idEmpleado;
|
|
|
+ $interMedia->idObra = $modelo->id;
|
|
|
+
|
|
|
+ if (!$interMedia->save()) {
|
|
|
+ $transaccion->rollBack();
|
|
|
+ return (new Respuesta($interMedia))
|
|
|
+ ->mensaje("Hubo un problema al guardar la imagen {$item['nombre']}");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ ObraHerramienta::deleteAll(['id' => $modelo->id]);
|
|
|
+
|
|
|
+ foreach ($herramienta as $item) {
|
|
|
+ $idHerramienta = $item['idHerramienta'];
|
|
|
+ $cantidad = $item['cantidad'];
|
|
|
+ $interMedia = new ObraHerramienta();
|
|
|
+
|
|
|
+ $interMedia->idHerramienta = $idHerramienta;
|
|
|
+ $interMedia->idObra = $modelo->id;
|
|
|
+ $interMedia->cantidad = $cantidad;
|
|
|
+
|
|
|
+ $interMedia->cantidad = $cantidad;
|
|
|
+
|
|
|
+ if (!$interMedia->save()) {
|
|
|
+ $transaccion->rollBack();
|
|
|
+ return (new Respuesta($interMedia))
|
|
|
+ ->mensaje("Hubo un problema al guardar la herramienta {$item['nombre']}");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $transaccion->commit();
|
|
|
+ $modelo->refresh();
|
|
|
+ return (new Respuesta($modelo))
|
|
|
+ ->mensaje("Obra ha sido guardado de manera exitosa");
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ $transaccion->rollBack();
|
|
|
return (new Respuesta($modelo))
|
|
|
- ->mensaje("Hubo un problema al guardar la obra");
|
|
|
+ ->esError()
|
|
|
+ ->mensaje($e->getMessage());
|
|
|
}
|
|
|
-
|
|
|
- $modelo->refresh();
|
|
|
- return (new Respuesta($modelo))
|
|
|
- ->mensaje("Obra guardada");
|
|
|
}
|
|
|
|
|
|
public function actionEliminar() {
|
|
|
$id = trim($this->req->getBodyParam("id", ""));
|
|
|
$modelo = null;
|
|
|
|
|
|
- if($id !== "") {
|
|
|
+ if ($id !== "") {
|
|
|
$modelo = $this->modelClass::findOne(["id" => $id]);
|
|
|
}
|
|
|
- if($modelo === null) {
|
|
|
+ if ($modelo === null) {
|
|
|
return (new Respuesta())
|
|
|
->esError()
|
|
|
->mensaje("Obra no encontrada");
|
|
|
}
|
|
|
$modelo->eliminado = new Expression('now()');
|
|
|
- if(!$modelo->save()) {
|
|
|
+ if (!$modelo->save()) {
|
|
|
return (new Respuesta($modelo))
|
|
|
->mensaje("No se pudo eliminar la obra");
|
|
|
}
|
|
@@ -97,4 +146,4 @@ class ObraController extends AuthController {
|
|
|
return (new Respuesta())
|
|
|
->mensaje("Obra eliminada");
|
|
|
}
|
|
|
-}
|
|
|
+}
|