Browse Source

Incidente e IncidenteEmpleado Modulos y Controladores, como tambien sus migrates

Jogibeda 7 months ago
parent
commit
6cea99ccf5

+ 72 - 0
migrations/m240927_010701_Tabla_Incidente_e_IncidenteEmpleado.php

@@ -0,0 +1,72 @@
+<?php
+
+use yii\db\Migration;
+
+/**
+ * Class m240927_010701_Tabla_Incidente_e_IncidenteEmpleado
+ */
+class m240927_010701_Tabla_Incidente_e_IncidenteEmpleado extends Migration
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function safeUp()
+    {
+        $this->createTable('Incidente', [
+            "id" => $this->string(36),
+            "nombre" => $this->string(100)->notNull(),
+            "fechaIncidente" => $this->timestamp()->append(" with time zone"),
+            "idObra" => $this->string(36),
+            "descripcion" => $this->string(100)->notNull(),
+            "creado" => $this->timestamp()->append(" with time zone"),
+            "modificado" => $this->timestamp()->append(" with time zone"),
+            "eliminado" => $this->timestamp()->append(" with time zone"),
+        ]);
+        $this->addForeignKey("IncidenteObraFK", "Incidente", "idObra", "Obra", "id");
+        $this->addPrimaryKey('IncidentePK', 'Incidente', 'id');
+
+        $this->createTable("IncidenteEmpleado", [
+            "id" => $this->string(36),
+            "idIncidente" => $this->string(36),
+            "idEmpleado" => $this->string(36),
+            "creado" => $this->timestamp()->append("with time zone"),
+            "modificado" => $this->timestamp()->append("with time zone"),
+            "eliminado" => $this->timestamp()->append("with time zone"),
+        ]);
+
+        $this->addPrimaryKey('IncidenteEmpleadoPK', 'IncidenteEmpleado', 'id');
+
+        $this->addForeignKey('IncidenteEmpleadoIncidenteFK', 'IncidenteEmpleado', 'idIncidente', 'Incidente', 'id');
+        $this->addForeignKey('IncidenteEmpleadoEmpleadoFK', 'IncidenteEmpleado', 'idEmpleado', 'Empleado', 'id');
+
+
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function safeDown()
+    {
+        $this->dropForeignKey("IncidenteObraFK", "Incidente");
+        $this->dropTable('Incidente');
+
+        $this->dropForeignKey('IncidenteEmpleadoIncidenteFK', "IncidenteEmpleado");
+        $this->dropForeignKey('IncidenteEmpleadoEmpleadoFK', "IncidenteEmpleado");
+        $this->dropTable('IncidenteEmpleado');
+    }
+
+    /*
+    // Use up()/down() to run migration code without a transaction.
+    public function up()
+    {
+
+    }
+
+    public function down()
+    {
+        echo "m240927_010701_Tabla_Incidente_e_IncidenteEmpleado cannot be reverted.\n";
+
+        return false;
+    }
+    */
+}

+ 75 - 0
models/Incidente.php

@@ -0,0 +1,75 @@
+<?php
+
+namespace app\models;
+
+use Yii;
+
+/**
+ * This is the model class for table "Obra".
+ *
+ * @property string $id
+ * @property string $nombre
+ * @property string|null $fechaIncidente
+ * @property string|null $idObra
+ * @property string|null $descripcion
+ * @property string|null $creado
+ * @property string|null $modificado
+ * @property string|null $eliminado
+ *
+ * @property IncidenteEmpleado[] $incidenteEmpleado
+ */
+class Incidente extends ModeloBase
+{
+    /**
+     * {@inheritdoc}
+     */
+    public static function tableName()
+    {
+        return 'Incidente';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function rules()
+    {
+        return [
+            [['id', 'nombre', 'idObra'], 'required'],
+            [['fechaIncidente','creado', 'modificado', 'eliminado'], 'safe'],
+            [['id', 'idObra'], 'string', 'max' => 36],
+            [['nombre', 'descripcion'], 'string', 'max' => 100],
+            [['id'], 'unique'],
+            [['idObra'], 'exist', 'skipOnError' => true, 'targetClass' => Obra::class, 'targetAttribute' => ['idObra' => 'id']],
+
+        ];
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function attributeLabels()
+    {
+        return [
+            'id' => 'ID',
+            'nombre' => 'Nombre',
+            'fechaIncidente' => 'Fecha Incidente',
+            'idObra' => 'ID Obra',
+            'descripcion' => 'Descripcion',
+            'creado' => 'Creado',
+            'modificado' => 'Modificado',
+            'eliminado' => 'Eliminado',
+        ];
+    }
+
+   
+
+    public function getIncidenteEmpleado()
+    {
+        return $this->hasMany(IncidenteEmpleado::class, ['idIncidente' => 'id']);
+    }
+
+    public function getObra()
+    {
+        return $this->hasOne(Obra::class, ['id' => 'idObra']);
+    }
+}

+ 79 - 0
models/IncidenteEmpleado.php

@@ -0,0 +1,79 @@
+<?php
+
+namespace app\models;
+
+use Yii;
+
+/**
+ * This is the model class for table "IncidenteEmpleado".
+ *
+ * @property string $id
+ * @property string|null $idIncidente
+ * @property string|null $idEmpleado
+ * @property string|null $creado
+ * @property string|null $modificado
+ * @property string|null $eliminado
+ *
+ * @property Empleado $idEmpleado0
+ * @property Incidente $idIncidente
+ */
+class IncidenteEmpleado extends ModeloBase
+{
+    /**
+     * {@inheritdoc}
+     */
+    public static function tableName()
+    {
+        return 'IncidenteEmpleado';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function rules()
+    {
+        return [
+            [['id'], 'required'],
+            [['creado', 'modificado', 'eliminado'], 'safe'],
+            [['id', 'idIncidente', 'idEmpleado'], 'string', 'max' => 36],
+            [['id'], 'unique'],
+            [['idEmpleado'], 'exist', 'skipOnError' => true, 'targetClass' => Empleado::class, 'targetAttribute' => ['idEmpleado' => 'id']],
+            [['idIncidente'], 'exist', 'skipOnError' => true, 'targetClass' => Incidente::class, 'targetAttribute' => ['idIncidente' => 'id']],
+        ];
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function attributeLabels()
+    {
+        return [
+            'id' => 'ID',
+            'idObra' => 'Id Obra',
+            'idIncidente' => 'Id Incidente',
+            'creado' => 'Creado',
+            'modificado' => 'Modificado',
+            'eliminado' => 'Eliminado',
+        ];
+    }
+
+    /**
+     * Gets query for [[IdEmpleado0]].
+     *
+     * @return \yii\db\ActiveQuery
+     */
+    public function getEmpleado()
+    {
+        return $this->hasOne(Empleado::class, ['id' => 'idEmpleado']);
+    }
+
+    /**
+     * Gets query for [[IdObra0]].
+     *
+     * @return \yii\db\ActiveQuery
+     */
+    public function getIncidente()
+    {
+        return $this->hasOne(Incidente::class, ['id' => 'idIncidente']);
+    }
+}

+ 124 - 0
modules/v1/controllers/IncidenteController.php

@@ -0,0 +1,124 @@
+<?php
+
+namespace v1\controllers;
+
+use app\models\Incidente;
+use app\models\IncidenteEmpleado;
+use common\data\Respuesta;
+use common\rest\AuthController;
+use Yii;
+use yii\db\Expression;
+ 
+class IncidenteController extends AuthController {
+
+  public $modelClass = "v1\models\Incidente";
+
+  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", ""));
+    
+    $query = $this->queryInicial;
+
+    if ($id > 0) {
+      $query->andWhere(["id" => $id]);
+    }
+
+    if ($rangoInicio !== "" && $rangoFin !== "") {
+      $query->andWhere(["between", "[[fechaInicio]]", $rangoInicio, $rangoFin]);
+    }
+   
+    if ($buscar) {
+
+      $query->andWhere([
+        "OR",
+        ["ilike", "nombre", $buscar]
+      ]);
+    }
+
+    return new Respuesta($query, $this->limite, $this->pagina, $this->ordenar);
+  }
+
+  public function actionGuardar() {
+    $id = trim($this->req->getBodyParam("id", ""));
+    $empleados = $this->req->getBodyParam("empleados", []);
+    $modelo = null;
+
+    $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(), '');
+
+      
+      $modelo->load($this->req->getBodyParams(), '');
+      if (!$modelo->save()) {
+        return (new Respuesta($modelo))
+          ->mensaje("Hubo un problema al guardar el incidente");
+      }
+
+      IncidenteEmpleado::deleteAll(['idIncidente' => $modelo->id]);
+
+      foreach ($empleados as $empleado) {
+        if (isset($empleado['id']) ) {
+          $incidenteEmpleado = new IncidenteEmpleado();
+          $incidenteEmpleado->uuid();
+          $incidenteEmpleado->idEmpleado = $empleado['id'];
+          $incidenteEmpleado->idIncidente = $modelo->id;
+
+          if (!$incidenteEmpleado->save()) {
+            $transaccion->rollBack();
+            return (new Respuesta($incidenteEmpleado))
+              ->mensaje("Hubo un problema al guardar el Empleado con ID {$empleado['id']}");
+          }
+        } else {
+          Yii::error('Falta el id del empleado o está vacío en el array', ['item' => $empleado]);
+        }
+      }
+
+      
+
+      $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))
+        ->esError()
+        ->mensaje($e->getMessage());
+    }
+  }
+
+  public function actionEliminar() {
+    $id = trim($this->req->getBodyParam("id", ""));
+    $modelo = null;
+
+    if ($id !== "") {
+      $modelo = $this->modelClass::findOne(["id" => $id]);
+    }
+    if ($modelo === null) {
+      return (new Respuesta())
+        ->esError()
+        ->mensaje("Incidente no encontrado");
+    }
+    $modelo->eliminado = new Expression('now()');
+    if (!$modelo->save()) {
+      return (new Respuesta($modelo))
+        ->mensaje("No se pudo eliminar el incidente");
+    }
+
+    return (new Respuesta())
+      ->mensaje("Incidente eliminado");
+  }
+}

+ 56 - 0
modules/v1/controllers/IncidenteEmpleadoController.php

@@ -0,0 +1,56 @@
+<?php
+
+namespace v1\controllers;
+
+use common\data\Respuesta;
+use common\rest\AuthController;
+use yii\db\Expression;
+
+class IncidenteEmpleadoController extends AuthController {
+
+  public $modelClass = "v1\models\IncidenteEmpleado";
+
+  public function actionIndex() {
+    $id = trim($this->req->get("id", ""));
+    $buscar = trim($this->req->get("q", ""));
+
+    $query = $this->queryInicial;
+
+    if($id > 0) {
+      $query->andWhere(["id" => $id]);
+    }
+
+    if($buscar) {
+      
+      $query->andWhere([
+        "OR",
+        ["ilike", "nombre", $buscar]
+      ]);
+
+    }
+
+    return new Respuesta($query, $this->limite, $this->pagina, $this->ordenar);
+  }
+
+  public function actionEliminar() {
+    $id = trim($this->req->getBodyParam("id", ""));
+    $modelo = null;
+
+    if ($id !== "") {
+      $modelo = $this->modelClass::findOne(["id" => $id]);
+    }
+    if ($modelo === null) {
+      return (new Respuesta())
+        ->esError()
+        ->mensaje("Empleado no encontrado");
+    }
+    $modelo->eliminado = new Expression('now()');
+    if (!$modelo->save()) {
+      return (new Respuesta($modelo))
+        ->mensaje("No se pudo eliminar el Empleado");
+    }
+
+    return (new Respuesta())
+      ->mensaje("Empleado eliminado");
+  }
+}

+ 38 - 0
modules/v1/models/Incidente.php

@@ -0,0 +1,38 @@
+<?php
+
+namespace v1\models;
+
+use app\models\Incidente as ModeloIncidente;
+
+class Incidente extends ModeloIncidente {
+ 
+  public function fields() {
+    return [
+      'id',
+      'nombre',
+      'fechaIncidente',
+      'idObra',
+      'descripcion',
+      'creado',
+      'modificado',
+    ];
+  }
+
+  public function extraFields() {
+    return [
+        'empleados',
+        'obra',
+    ];
+}
+
+  public function getObra()
+  {
+      return $this->hasOne(Obra::class, ['id' => 'idObra']);
+  }
+
+  public function getEmpleado()
+  {
+      return $this->hasMany(Empleado::class, ['id' => 'idEmpleado'])->viaTable("incidenteEmpleado",['idIncidente'=>'id']) ;
+  }
+
+}

+ 36 - 0
modules/v1/models/IncidenteEmpleado.php

@@ -0,0 +1,36 @@
+<?php
+
+namespace v1\models;
+
+use app\models\Incidente;
+use app\models\IncidenteEmpleado as ModeloIncidenteEmpleado;
+
+class IncidenteEmpleado extends ModeloIncidenteEmpleado {
+
+  public function fields() {
+    return [
+      'id',
+      'idIncidente',
+      'idEmpleado',
+      'creado',
+      'modificado',
+    ];
+  }
+
+  public function extraFields() {
+    return [
+        'empleado',
+        'incidente',
+    ];
+}
+
+  public function getEmpleado()
+  {
+      return $this->hasOne(Empleado::class, ['id' => 'idEmpleado']);
+  }
+
+  public function getIncidente()
+  {
+      return $this->hasOne(Incidente::class, ['id' => 'idIncidente']);
+  }
+}