瀏覽代碼

Controladores y modelos de Partida y Unidad de medida, incluido su migracion

Jogibeda 7 月之前
父節點
當前提交
4f517bcd48

+ 62 - 0
migrations/m240925_184940_Creacion_tablas_Partida_Unidad_Medida.php

@@ -0,0 +1,62 @@
+<?php
+
+use yii\db\Migration;
+
+/**
+ * Class m240925_184940_Creacion_tablas_Partida_Unidad_Medida
+ */
+class m240925_184940_Creacion_tablas_Partida_Unidad_Medida extends Migration
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function safeUp()
+    {
+        $this->createTable('Partida', [
+            "id" => $this->string(36),
+            "nombre" => $this->string(100)->notNull(),
+            "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->addPrimaryKey('PartidaPK', 'Partida', 'id');
+
+        $this->createTable('UnidadMedida', [
+            "id" => $this->string(36),
+            "nombre" => $this->string(100)->notNull(),
+            "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->addPrimaryKey('UnidadMedidaPK', 'UnidadMedida', 'id');
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function safeDown()
+    {
+        $this->dropTable("Partida");
+        $this->dropTable("UnidadMedida");
+
+    }
+
+    /*
+    // Use up()/down() to run migration code without a transaction.
+    public function up()
+    {
+
+    }
+
+    public function down()
+    {
+        echo "m240925_184940_Creacion_tablas_Partida_Unidad_Medida cannot be reverted.\n";
+
+        return false;
+    }
+    */
+}

+ 53 - 0
models/Partida.php

@@ -0,0 +1,53 @@
+<?php
+
+namespace app\models;
+
+use Yii;
+
+/**
+ * This is the model class for table "Partida".
+ *
+ * @property string $id
+ * @property string|null $nombre
+ * @property string|null $descripcion
+ * 
+ */
+class Partida extends ModeloBase
+{
+    /**
+     * {@inheritdoc}
+     */
+    public static function tableName()
+    {
+        return 'Partida';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function rules()
+    {
+        return [
+            [['id'], 'required'],
+            [['creado', 'modificado', 'eliminado'], 'safe'],
+            [['id'], 'string', 'max' => 36],
+            [['nombre', 'descripcion'], 'string', 'max' => 100],
+            [['id'], 'unique'],
+        ];
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function attributeLabels()
+    {
+        return [
+            'id' => 'ID',
+            'nombre' => 'Clave',
+            'descripcion' => 'Descripcion',
+            'creado' => 'Creado',
+            'modificado' => 'Modificado',
+            'eliminado' => 'Eliminado',
+        ];
+    }
+}

+ 53 - 0
models/UnidadMedida.php

@@ -0,0 +1,53 @@
+<?php
+
+namespace app\models;
+
+use Yii;
+
+/**
+ * This is the model class for table "UnidadMedida".
+ *
+ * @property string $id
+ * @property string|null $nombre
+ * @property string|null $descripcion
+ * 
+ */
+class UnidadMedida extends ModeloBase
+{
+    /**
+     * {@inheritdoc}
+     */
+    public static function tableName()
+    {
+        return 'UnidadMedida';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function rules()
+    {
+        return [
+            [['id'], 'required'],
+            [['creado', 'modificado', 'eliminado'], 'safe'],
+            [['id'], 'string', 'max' => 36],
+            [['nombre', 'descripcion'], 'string', 'max' => 100],
+            [['id'], 'unique'],
+        ];
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function attributeLabels()
+    {
+        return [
+            'id' => 'ID',
+            'nombre' => 'Clave',
+            'descripcion' => 'Descripcion',
+            'creado' => 'Creado',
+            'modificado' => 'Modificado',
+            'eliminado' => 'Eliminado',
+        ];
+    }
+}

+ 83 - 0
modules/v1/controllers/PartidaController.php

@@ -0,0 +1,83 @@
+<?php
+
+namespace v1\controllers;
+
+use common\data\Respuesta;
+use common\rest\AuthController;
+use yii\db\Expression;
+
+class PartidaController extends AuthController
+{
+
+  public $modelClass = "v1\models\Partida";
+
+  public function actionIndex()
+  {
+    $id = trim($this->req->get("id", ""));
+    $buscar = trim($this->req->get("q", ""));
+
+    $query = $this->queryInicial;
+
+
+    if ($id !== "") {
+      $query->andWhere(["id" => $id]);
+    }
+
+    if($buscar !==""){
+      $query->andWhere(["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 la Partida");
+    }
+
+    $modelo->refresh();
+    return (new Respuesta($modelo))
+      ->mensaje("Partida guardada");
+  }
+
+  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("Partida no encontrado");
+    }
+    $modelo->eliminado = new Expression('now()');
+    if (!$modelo->save()) {
+      return (new Respuesta($modelo))
+        ->mensaje("No se pudo eliminar la Partida");
+    }
+
+    return (new Respuesta())
+      ->mensaje("Partida eliminada");
+  }
+}

+ 88 - 0
modules/v1/controllers/UnidadMedidaController.php

@@ -0,0 +1,88 @@
+<?php
+
+namespace v1\controllers;
+
+use common\data\Respuesta;
+use common\rest\AuthController;
+use yii\db\Expression;
+
+class UnidadMedidaController extends AuthController
+{
+
+  public $modelClass = "v1\models\UnidadMedida";
+
+  public function actionIndex()
+  {
+    $id = trim($this->req->get("id", ""));
+    $buscar = trim($this->req->get("q", ""));
+
+    $query = $this->queryInicial;
+
+
+    if ($id !== "") {
+      $query->andWhere(["id" => $id]);
+    }
+
+    if ($buscar) {
+
+      $query->andWhere([
+        "OR",
+        ["like", "id", $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(), '');
+
+  
+    $modelo->load($this->req->getBodyParams(), '');
+    if (!$modelo->save()) {
+      return (new Respuesta($modelo))
+        ->mensaje("Hubo un problema al guardar la Unidad de Medida");
+    }
+
+    $modelo->refresh();
+    return (new Respuesta($modelo))
+      ->mensaje("Unidad de Medida guardada");
+  }
+
+  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("Unidad de Medida no encontrado");
+    }
+    $modelo->eliminado = new Expression('now()');
+    if (!$modelo->save()) {
+      return (new Respuesta($modelo))
+        ->mensaje("No se pudo eliminar la Unidad de Medida");
+    }
+
+    return (new Respuesta())
+      ->mensaje("Unidad de Medida eliminada");
+  }
+}

+ 18 - 0
modules/v1/models/Partida.php

@@ -0,0 +1,18 @@
+<?php
+
+namespace v1\models;
+
+use app\models\Partida as ModeloPartida;
+
+class Partida extends ModeloPartida {
+
+  public function fields() {
+    return [
+      'id',
+      'nombre',
+      'descripcion',
+      'creado',
+      'modificado',
+    ];
+  }
+}

+ 18 - 0
modules/v1/models/UnidadMedida.php

@@ -0,0 +1,18 @@
+<?php
+
+namespace v1\models;
+
+use app\models\UnidadMedida as ModeloUnidadMedida;
+
+class UnidadMedida extends ModeloUnidadMedida {
+
+  public function fields() {
+    return [
+      'id',
+      'nombre',
+      'descripcion',
+      'creado',
+      'modificado',
+    ];
+  }
+}