Przeglądaj źródła

Se agregaron modulos ObraHerramienta yu ObraEmpleado

Jose Cienfuegos 8 miesięcy temu
rodzic
commit
fbcb49d27f

+ 61 - 0
migrations/m240821_182715_tablas_ObraHerramienta_ObraEmpleado.php

@@ -0,0 +1,61 @@
+<?php
+
+use yii\db\Migration;
+
+/**
+ * Class m240821_182715_tablas_ObraHerramienta_ObraEmpleado
+ */
+class m240821_182715_tablas_ObraHerramienta_ObraEmpleado extends Migration
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function safeUp()
+    {
+        $this->createTable("ObraEmpleado", [
+            "id" => $this->string(36),
+            "idObra" => $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('ObraEmpleadoPK', 'ObraEmpleado', 'id');
+
+        $this->addForeignKey('ObraEmpleadoObraFK', 'ObraEmpleado', 'idObra', 'Obra', 'id');
+        $this->addForeignKey('ObraEmpleadoEmpleadoFK', 'ObraEmpleado', 'idEmpleado', 'Empleado', 'id');
+
+        $this->createTable("ObraHerramienta", [
+            "id" => $this->string(36),
+            "idObra" => $this->string(36),
+            "idHerramienta" => $this->string(36),
+            "cantidad" => $this->integer(),
+            "creado" => $this->timestamp()->append("with time zone"),
+            "modificado" => $this->timestamp()->append("with time zone"),
+            "eliminado" => $this->timestamp()->append("with time zone"),
+        ]);
+
+        $this->addPrimaryKey('ObraHerramientaPK', 'ObraHerramienta', 'id');
+
+        $this->addForeignKey('ObraHerramientaObraFK', 'ObraHerramienta', 'idObra', 'Obra', 'id');
+        $this->addForeignKey('ObraHerramientaHerramientaFK', 'ObraHerramienta', 'idHerramienta', 'Herramienta', 'id');
+    
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function safeDown()
+    {
+        $this->dropForeignKey('ObraHerramientaHerramientaFK', 'ObraHerramienta');
+        $this->dropForeignKey('ObraHerramientaObraFK', 'ObraHerramienta');
+
+        $this->dropTable('ObraHerramienta');
+
+        $this->dropForeignKey('ObraEmpleadoEmpleadoFK', 'ObraEmpleado');
+        $this->dropForeignKey('ObraEmpleadoObraFK', 'ObraEmpleado');
+
+        $this->dropTable('ObraEmpleado');
+    }
+}

+ 23 - 4
models/Obra.php

@@ -8,14 +8,18 @@ use Yii;
  * This is the model class for table "Obra".
  *
  * @property string $id
- * @property string $clave
  * @property string $nombre
- * @property string $fechaInicio
- * @property string $fechaFinal
+ * @property string|null $fechaInicio
+ * @property string|null $fechaFinal
  * @property string|null $descripcion
  * @property string|null $creado
  * @property string|null $modificado
  * @property string|null $eliminado
+ * @property string|null $clave
+ *
+ * @property HerramientaObra[] $herramientaObras
+ * @property ObraEmpleado[] $obraEmpleados
+ * @property ObraHerramienta[] $obraHerramientas
  */
 class Obra extends ModeloBase
 {
@@ -48,7 +52,7 @@ class Obra extends ModeloBase
     {
         return [
             'id' => 'ID',
-            'clave'=>'Clave',
+            'clave' => 'Clave',
             'nombre' => 'Nombre',
             'fechaInicio' => 'Fecha Inicio',
             'fechaFinal' => 'Fecha Final',
@@ -58,4 +62,19 @@ class Obra extends ModeloBase
             'eliminado' => 'Eliminado',
         ];
     }
+
+    public function getHerramientaObras()
+    {
+        return $this->hasMany(HerramientaObra::class, ['idObra' => 'id']);
+    }
+
+    public function getObraEmpleado()
+    {
+        return $this->hasMany(ObraEmpleado::class, ['idObra' => 'id']);
+    }
+
+    public function getObraHerramienta()
+    {
+        return $this->hasMany(ObraHerramienta::class, ['idObra' => 'id']);
+    }
 }

+ 79 - 0
models/ObraEmpleado.php

@@ -0,0 +1,79 @@
+<?php
+
+namespace app\models;
+
+use Yii;
+
+/**
+ * This is the model class for table "ObraEmpleado".
+ *
+ * @property string $id
+ * @property string|null $idObra
+ * @property string|null $idEmpleado
+ * @property string|null $creado
+ * @property string|null $modificado
+ * @property string|null $eliminado
+ *
+ * @property Empleado $idEmpleado0
+ * @property Obra $idObra0
+ */
+class ObraEmpleado extends ModeloBase
+{
+    /**
+     * {@inheritdoc}
+     */
+    public static function tableName()
+    {
+        return 'ObraEmpleado';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function rules()
+    {
+        return [
+            [['id'], 'required'],
+            [['creado', 'modificado', 'eliminado'], 'safe'],
+            [['id', 'idObra', 'idEmpleado'], 'string', 'max' => 36],
+            [['id'], 'unique'],
+            [['idEmpleado'], 'exist', 'skipOnError' => true, 'targetClass' => Empleado::class, 'targetAttribute' => ['idEmpleado' => 'id']],
+            [['idObra'], 'exist', 'skipOnError' => true, 'targetClass' => Obra::class, 'targetAttribute' => ['idObra' => 'id']],
+        ];
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function attributeLabels()
+    {
+        return [
+            'id' => 'ID',
+            'idObra' => 'Id Obra',
+            'idEmpleado' => 'Id Empleado',
+            '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 getObra()
+    {
+        return $this->hasOne(Obra::class, ['id' => 'idObra']);
+    }
+}

+ 83 - 0
models/ObraHerramienta.php

@@ -0,0 +1,83 @@
+<?php
+
+namespace app\models;
+
+use Yii;
+
+/**
+ * This is the model class for table "ObraHerramienta".
+ *
+ * @property string $id
+ * @property string|null $idObra
+ * @property string|null $idHerramienta
+ * @property int|null $cantidad
+ * @property string|null $creado
+ * @property string|null $modificado
+ * @property string|null $eliminado
+ *
+ * @property Herramienta $idHerramienta0
+ * @property Obra $idObra0
+ */
+class ObraHerramienta extends ModeloBase
+{
+    /**
+     * {@inheritdoc}
+     */
+    public static function tableName()
+    {
+        return 'ObraHerramienta';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function rules()
+    {
+        return [
+            [['id'], 'required'],
+            [['cantidad'], 'default', 'value' => null],
+            [['cantidad'], 'integer'],
+            [['creado', 'modificado', 'eliminado'], 'safe'],
+            [['id', 'idObra', 'idHerramienta'], 'string', 'max' => 36],
+            [['id'], 'unique'],
+            [['idHerramienta'], 'exist', 'skipOnError' => true, 'targetClass' => Herramienta::class, 'targetAttribute' => ['idHerramienta' => 'id']],
+            [['idObra'], 'exist', 'skipOnError' => true, 'targetClass' => Obra::class, 'targetAttribute' => ['idObra' => 'id']],
+        ];
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function attributeLabels()
+    {
+        return [
+            'id' => 'ID',
+            'idObra' => 'Id Obra',
+            'idHerramienta' => 'Id Herramienta',
+            'cantidad' => 'Cantidad',
+            'creado' => 'Creado',
+            'modificado' => 'Modificado',
+            'eliminado' => 'Eliminado',
+        ];
+    }
+
+    /**
+     * Gets query for [[IdHerramienta0]].
+     *
+     * @return \yii\db\ActiveQuery
+     */
+    public function getHerramienta()
+    {
+        return $this->hasOne(Herramienta::class, ['id' => 'idHerramienta']);
+    }
+
+    /**
+     * Gets query for [[IdObra0]].
+     *
+     * @return \yii\db\ActiveQuery
+     */
+    public function getObra()
+    {
+        return $this->hasOne(Obra::class, ['id' => 'idObra']);
+    }
+}

+ 92 - 43
modules/v1/controllers/ObraController.php

@@ -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");
   }
-}
+}

+ 22 - 0
modules/v1/models/Obra.php

@@ -18,4 +18,26 @@ class Obra extends ModeloObra {
       'modificado',
     ];
   }
+
+  public function extraFields() {
+    return [
+        'obraEmpleado',
+        'obraHerramienta',
+    ];
+}
+
+  public function getHerramientaObras()
+  {
+      return $this->hasMany(HerramientaObra::class, ['idObra' => 'id']);
+  }
+
+  public function getObraEmpleado()
+  {
+      return $this->hasMany(ObraEmpleado::class, ['idObra' => 'id']);
+  }
+
+  public function getObraHerramienta()
+  {
+      return $this->hasMany(ObraHerramienta::class, ['idObra' => 'id']);
+  }
 }

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

@@ -0,0 +1,36 @@
+<?php
+
+namespace v1\models;
+
+use app\models\ObraEmpleado as ModeloObraEmpleado;
+
+class ObraEmpleado extends ModeloObraEmpleado {
+
+  public function fields() {
+    return [
+      'id',
+      'idObra',
+      'idEmpleado',
+      'descripcion',
+      'creado',
+      'modificado',
+    ];
+  }
+
+  public function extraFields() {
+    return [
+        'empleado',
+        'obra',
+    ];
+}
+
+  public function getEmpleado()
+  {
+      return $this->hasOne(Empleado::class, ['id' => 'idEmpleado']);
+  }
+
+  public function getObra()
+  {
+      return $this->hasOne(Obra::class, ['id' => 'idObra']);
+  }
+}

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

@@ -0,0 +1,36 @@
+<?php
+
+namespace v1\models;
+
+use app\models\ObraHerramienta as ModeloObraHerramienta;
+
+class ObraHerramienta extends ModeloObraHerramienta {
+
+  public function fields() {
+    return [
+      'id',
+      'idObra',
+      'idEmpleado',
+      'descripcion',
+      'creado',
+      'modificado',
+    ];
+  }
+
+  public function extraFields() {
+    return [
+        'herramienta',
+        'obra',
+    ];
+}
+
+  public function getHerramienta()
+  {
+      return $this->hasOne(Herramienta::class, ['id' => 'idHerramienta']);
+  }
+
+  public function getObra()
+  {
+      return $this->hasOne(Obra::class, ['id' => 'idObra']);
+  }
+}