ソースを参照

Se agregaron tablas Servicio y ServicioElemento

Jose Cienfuegos 9 ヶ月 前
コミット
f052a6dea5

+ 57 - 0
migrations/m240718_220523_tabla_servicio_servicio_elemento.php

@@ -0,0 +1,57 @@
+<?php
+
+use yii\db\Migration;
+
+/**
+ * Class m240718_220523_tabla_servicio_servicio_elemento
+ */
+class m240718_220523_tabla_servicio_servicio_elemento extends Migration
+{
+    public function safeUp() {
+
+        $this->createTable('Servicio', [
+          "id" => $this->string(36),
+          "factura" => $this->string(100)->notNull(),
+          "tipo" => $this->string(100)->notNull(),
+          "descripcion" => $this->text(),
+          "fechaServicio" => $this->timestamp()->append(" with time zone"),
+          "idTaller" => $this->string(36),
+          "idVehiculo" => $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("ServicioPK", "Servicio", "id");
+    
+        $this->addForeignKey("ServicioTallerFK", "Servicio", "idTaller", "Taller", "id");
+        $this->addForeignKey("ServicioVehiculoFK", "Servicio", "idVehiculo", "Vehiculo", "id");
+    
+        $this->createTable('ServicioElemento', [
+          "id" => $this->string(36),
+          "idServicio" => $this->string(36),
+          "elemento" => $this->string(),
+          "costo" => $this->string(100),
+          "descripcion" => $this->string(100),
+          "creado" => $this->timestamp()->append(" with time zone"),
+          "modificado" => $this->timestamp()->append(" with time zone"),
+          "eliminado" => $this->timestamp()->append(" with time zone"),
+        ]);
+
+        $this->addPrimaryKey("ServicioElementoPK", "ServicioElemento", "id");
+    
+        $this->addForeignKey("ServicioElementoServicioFK", "ServicioElemento", "idServicio", "Servicio", "id");
+    
+      }
+    
+      /**
+       * {@inheritdoc}
+       */
+      public function safeDown() {
+        $this->dropForeignKey("ServicioElementoServicioFK", "ServicioElemento");
+        $this->dropTable('ServicioElemento');
+        $this->dropForeignKey("ServicioVehiculoFK", "Servicio");
+        $this->dropForeignKey("ServicioTallerFK", "Servicio");
+        $this->dropTable('Servicio');
+      }
+}

+ 100 - 0
models/Servicio.php

@@ -0,0 +1,100 @@
+<?php
+
+namespace app\models;
+
+use Yii;
+
+/**
+ * This is the model class for table "Servicio".
+ *
+ * @property string $id
+ * @property string $factura
+ * @property string $tipo
+ * @property string|null $descripcion
+ * @property string|null $fechaServicio
+ * @property string|null $idTaller
+ * @property string|null $idVehiculo
+ * @property string|null $creado
+ * @property string|null $modificado
+ * @property string|null $eliminado
+ *
+ * @property Taller $idTaller
+ * @property Vehiculo $idVehiculo
+ * @property ServicioElemento[] $servicioElementos
+ */
+class Servicio extends ModeloBase
+{
+    /**
+     * {@inheritdoc}
+     */
+    public static function tableName()
+    {
+        return 'Servicio';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function rules()
+    {
+        return [
+            [['id', 'factura', 'tipo'], 'required'],
+            [['descripcion'], 'string'],
+            [['fechaServicio', 'creado', 'modificado', 'eliminado'], 'safe'],
+            [['id', 'idTaller', 'idVehiculo'], 'string', 'max' => 36],
+            [['factura', 'tipo'], 'string', 'max' => 100],
+            [['id'], 'unique'],
+            [['idTaller'], 'exist', 'skipOnError' => true, 'targetClass' => Taller::class, 'targetAttribute' => ['idTaller' => 'id']],
+            [['idVehiculo'], 'exist', 'skipOnError' => true, 'targetClass' => Vehiculo::class, 'targetAttribute' => ['idVehiculo' => 'id']],
+        ];
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function attributeLabels()
+    {
+        return [
+            'id' => 'ID',
+            'factura' => 'Factura',
+            'tipo' => 'Tipo',
+            'descripcion' => 'Descripcion',
+            'fechaServicio' => 'Fecha Servicio',
+            'idTaller' => 'Id Taller',
+            'idVehiculo' => 'Id Vehiculo',
+            'creado' => 'Creado',
+            'modificado' => 'Modificado',
+            'eliminado' => 'Eliminado',
+        ];
+    }
+
+    /**
+     * Gets query for [[IdTaller0]].
+     *
+     * @return \yii\db\ActiveQuery
+     */
+    public function getTaller()
+    {
+        return $this->hasOne(Taller::class, ['id' => 'idTaller']);
+    }
+
+    /**
+     * Gets query for [[IdVehiculo0]].
+     *
+     * @return \yii\db\ActiveQuery
+     */
+    public function getVehiculo()
+    {
+        return $this->hasOne(Vehiculo::class, ['id' => 'idVehiculo']);
+    }
+
+    /**
+     * Gets query for [[ServicioElementos]].
+     *
+     * @return \yii\db\ActiveQuery
+     */
+    public function getServicioElemento()
+    {
+        return $this->hasMany(ServicioElemento::class, ['idServicio' => 'id']);
+    }
+}

+ 73 - 0
models/ServicioElemento.php

@@ -0,0 +1,73 @@
+<?php
+
+namespace app\models;
+
+use Yii;
+
+/**
+ * This is the model class for table "ServicioElemento".
+ *
+ * @property string $id
+ * @property string|null $idServicio
+ * @property string|null $elemento
+ * @property string|null $costo
+ * @property string|null $descripcion
+ * @property string|null $creado
+ * @property string|null $modificado
+ * @property string|null $eliminado
+ *
+ * @property Servicio $idServicio
+ */
+class ServicioElemento extends ModeloBase
+{
+    /**
+     * {@inheritdoc}
+     */
+    public static function tableName()
+    {
+        return 'ServicioElemento';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function rules()
+    {
+        return [
+            [['id'], 'required'],
+            [['creado', 'modificado', 'eliminado'], 'safe'],
+            [['id', 'idServicio'], 'string', 'max' => 36],
+            [['elemento'], 'string', 'max' => 255],
+            [['costo', 'descripcion'], 'string', 'max' => 100],
+            [['id'], 'unique'],
+            [['idServicio'], 'exist', 'skipOnError' => true, 'targetClass' => Servicio::class, 'targetAttribute' => ['idServicio' => 'id']],
+        ];
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function attributeLabels()
+    {
+        return [
+            'id' => 'ID',
+            'idServicio' => 'Id Servicio',
+            'elemento' => 'Elemento',
+            'costo' => 'Costo',
+            'descripcion' => 'Descripcion',
+            'creado' => 'Creado',
+            'modificado' => 'Modificado',
+            'eliminado' => 'Eliminado',
+        ];
+    }
+
+    /**
+     * Gets query for [[IdServicio]].
+     *
+     * @return \yii\db\ActiveQuery
+     */
+    public function getServicio()
+    {
+        return $this->hasOne(Servicio::class, ['id' => 'idServicio']);
+    }
+}

+ 1 - 1
models/Vehiculo.php

@@ -17,7 +17,7 @@ use Yii;
  * @property string|null $modificado
  * @property string|null $eliminado
  */
-class Vehiculo extends \yii\db\ActiveRecord
+class Vehiculo extends ModeloBase
 {
     /**
      * {@inheritdoc}

+ 93 - 0
modules/v1/controllers/ServicioController.php

@@ -0,0 +1,93 @@
+<?php
+
+namespace v1\controllers;
+
+use common\data\Respuesta;
+use common\rest\AuthController;
+use yii\db\Expression;
+
+class ServicioController extends AuthController {
+
+  public $modelClass = "v1\models\Servicio";
+
+  public function actionIndex() {
+    $id = trim($this->req->get("id", ""));
+    $buscar = trim($this->req->get("q", ""));
+    $taller = trim($this->req->get("taller", ""));
+    $vehiculo = trim($this->req->get("vehiculo", ""));
+    
+    $query = $this->queryInicial;
+    
+
+    if($id !== "") {
+      $query->andWhere(["id" => $id]);
+    }
+
+    if($taller !== "") {
+        $query->andWhere(["taller.id" => $taller]);
+    }
+
+    if($vehiculo !== "") {
+        $query->andWhere(["vehiculo.id" => $vehiculo]);
+    }
+
+    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", ""));
+    $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(), '');
+    if (!$modelo->save()) {
+      return (new Respuesta($modelo))
+        ->mensaje("Hubo un problema al guardar el Servicio");
+    }
+
+    $modelo->refresh();
+    return (new Respuesta($modelo))
+      ->mensaje("Serivicio guardado");
+  }
+
+  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("Servicio no encontrado");
+    }
+    $modelo->eliminado = new Expression('now()');
+    if(!$modelo->save()) {
+      return (new Respuesta($modelo))
+        ->mensaje("No se pudo eliminar Servicio");
+    }
+
+    return (new Respuesta())
+      ->mensaje("Servicio eliminada");
+  }
+}

+ 87 - 0
modules/v1/controllers/ServicioElementoController.php

@@ -0,0 +1,87 @@
+<?php
+
+namespace v1\controllers;
+
+use common\data\Respuesta;
+use common\rest\AuthController;
+use yii\db\Expression;
+
+class ServicioElementoController extends AuthController {
+
+  public $modelClass = "v1\models\ServicioElemento";
+
+  public function actionIndex() {
+    $id = trim($this->req->get("id", ""));
+    $buscar = trim($this->req->get("q", ""));
+    $servicio = trim($this->req->get("servicio", ""));
+
+    $query = $this->queryInicial;
+
+    if($id > 0) {
+      $query->andWhere(["id" => $id]);
+    }
+
+    if($servicio) {
+        $query->andWhere(["[[Servicio]].{{id}}", $servicio]);
+    }
+
+    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", ""));
+    $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(), '');
+    if (!$modelo->save()) {
+      return (new Respuesta($modelo))
+        ->mensaje("Hubo un problema al guardar el Servicio Elemento");
+    }
+
+    $modelo->refresh();
+    return (new Respuesta($modelo))
+      ->mensaje("Serivicio Elemento guardado");
+  }
+
+  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("Servicio Elemento no encontrado");
+    }
+    $modelo->eliminado = new Expression('now()');
+    if(!$modelo->save()) {
+      return (new Respuesta($modelo))
+        ->mensaje("No se pudo eliminar Servicio Elemento");
+    }
+
+    return (new Respuesta())
+      ->mensaje("Servicio Elemento eliminada");
+  }
+}

+ 46 - 0
modules/v1/models/Servicio.php

@@ -0,0 +1,46 @@
+<?php
+
+namespace v1\models;
+
+use app\models\Servicio as ModeloServicio;
+use app\models\ServicioElemento;
+use app\models\Taller;
+use app\models\Vehiculo;
+
+class Servicio extends ModeloServicio {
+
+    public function fields() {
+        return [
+            'id',
+            'factura',
+            'tipo',
+            'descripcion',
+            'fechaServicio',
+            'idTaller',
+            'idVehiculo',
+            'creado',
+            'modificado',
+        ];
+    }
+
+    public function extraFields() {
+        return [
+            'taller',
+            'vehiculo',
+            'servicioElemento'
+        ];
+    }
+
+    public function getTaller() {
+        return $this->hasOne(Taller::class, ['id' => 'idTaller']);
+    }
+
+    public function getVehiculo() {
+        return $this->hasOne(Vehiculo::class, ['id' => 'idVehiculo']);
+    }
+
+    public function getServicioElemento()
+    {
+        return $this->hasMany(ServicioElemento::class, ['idServicio' => 'id']);
+    }
+}

+ 31 - 0
modules/v1/models/ServicioElemento.php

@@ -0,0 +1,31 @@
+<?php
+
+namespace v1\models;
+
+use app\models\ServicioElemento as ModeloServicioElemento;
+
+class ServicioElemento extends ModeloServicioElemento {
+
+    public function fields() {
+        return [
+            'id',
+            'idServicio',
+            'elemento',
+            'costo',
+            'descripcion',
+            'creado',
+            'modificado',
+        ];
+    }
+
+    public function extraFields() {
+        return [
+            'servicio',
+        ];
+    }
+
+    public function getServicio()
+    {
+        return $this->hasOne(Servicio::class, ['id' => 'idServicio']);
+    }
+}