Parcourir la source

Merge branch 'master' of https://git.miralo.xyz/SAGARHPA/Sagarhpa_2024_api_php

Jose Cienfuegos il y a 10 mois
Parent
commit
11462f31d0

+ 81 - 0
commands/ImportacionController.php

@@ -0,0 +1,81 @@
+<?php
+
+namespace app\commands;
+
+use v1\models\Estado;
+use v1\models\Municipio;
+use yii\console\Controller;
+use yii\db\Expression;
+use yii\helpers\Json;
+
+class ImportacionController extends Controller {
+
+	public $truncate = null;
+	private $_db = null;
+	private $_actions = [
+		'actionProveedores',
+	];
+
+	public function options($actionID) {
+		return [
+			'truncate',
+		];
+	}
+
+	public function actionIndex() {
+		if ($this->truncate == 1) {
+			$this->actionTruncate();
+		}
+		foreach ($this->_actions as $action) {
+			$this->{$action}();
+		}
+	}
+
+	public function actionMunicipios() {
+		$this->stdout("Guardando Sincronizar estados\n");
+		$basePath = \Yii::getAlias('@app');
+		$file = fopen("{$basePath}/csv/municipios.csv", "r");
+		if ($file !== false) {
+			while (($data = fgetcsv($file, 1000)) !== false) {
+				@list($claveEstado, $nombreEstado, $abreviado, $claveMun, $nombreMun) = $data;
+				$municipio = null;
+				$estado = null;
+
+				$municipio = Municipio::find()
+					->andWhere([
+						'clave' => intval($claveMun),
+						'nombre' => trim($nombreMun)
+					])
+					->orderBy('clave')
+					->one();
+
+				if ($municipio === null) {
+					$this->stdout('No se encontró el Municipio');
+					continue;
+				}
+				$this->stdout($municipio->nombre);
+				$estado = Estado::find()
+					->andWhere([
+						'clave' => intval($claveEstado),
+						'nombre' => trim($nombreEstado)
+					])
+					->one();
+
+				if ($estado === null) {
+					$this->stdout('No se encontró el Municipio');
+					continue;
+				}
+
+				$municipio->idEstado = $estado->id;
+
+				$this->stdout("{$estado->nombre}\n");
+
+				if (!$municipio->save()) {
+					$errores = Json::encode($municipio->getFirstErrors());
+					$this->stderr("{$errores}\n");
+				}
+			}
+		}
+		fclose($file);
+	}
+}

+ 17 - 0
migrations/m240625_173930_campo_clave_estado.php

@@ -0,0 +1,17 @@
+<?php
+
+use yii\db\Migration;
+
+class m240625_173930_campo_clave_estado extends Migration {
+  public function safeUp() {
+    $this->addColumn('Estado', 'clave', $this->integer());
+    $this->addColumn('Estado', 'pais', $this->string(36));
+    $this->addColumn('Municipio', 'clave', $this->integer());
+  }
+
+  public function safeDown() {
+    $this->dropColumn('Municipio', 'clave');
+    $this->dropColumn('Estado', 'pais');
+    $this->dropColumn('Estado', 'clave');
+  }
+}

+ 44 - 46
models/Estado.php

@@ -17,53 +17,51 @@ use yii\base\Model;
  *
  * @property Municipio[] $municipios
  */
-class Estado extends ModeloBase
-{
-    /**
-     * {@inheritdoc}
-     */
-    public static function tableName()
-    {
-        return 'Estado';
-    }
+class Estado extends ModeloBase {
+  /**
+   * {@inheritdoc}
+   */
+  public static function tableName() {
+    return 'Estado';
+  }
 
-    /**
-     * {@inheritdoc}
-     */
-    public function rules()
-    {
-        return [
-            [['id'], 'required'],
-            [['creado', 'modificado', 'eliminado'], 'safe'],
-            [['id'], 'string', 'max' => 36],
-            [['nombre'], 'string', 'max' => 100],
-            [['abreviacion'], 'string', 'max' => 16],
-            [['id'], 'unique'],
-        ];
-    }
+  /**
+   * {@inheritdoc}
+   */
+  public function rules() {
+    return [
+      [['id'], 'required'],
+      [['clave'], 'integer'],
+      [['creado', 'modificado', 'eliminado'], 'safe'],
+      [['id', 'pais'], 'string', 'max' => 36],
+      [['nombre'], 'string', 'max' => 100],
+      [['abreviacion'], 'string', 'max' => 16],
+      [['id'], 'unique'],
+    ];
+  }
 
-    /**
-     * {@inheritdoc}
-     */
-    public function attributeLabels()
-    {
-        return [
-            'id' => 'ID',
-            'nombre' => 'Nombre',
-            'abreviacion' => 'Abreviacion',
-            'creado' => 'Creado',
-            'modificado' => 'Modificado',
-            'eliminado' => 'Eliminado',
-        ];
-    }
+  /**
+   * {@inheritdoc}
+   */
+  public function attributeLabels() {
+    return [
+      'id' => 'ID',
+      'nombre' => 'Nombre',
+      'abreviacion' => 'Abreviacion',
+      'creado' => 'Creado',
+      'modificado' => 'Modificado',
+      'eliminado' => 'Eliminado',
+      'clave' => 'Clave',
+      'pais' => 'Pais',
+    ];
+  }
 
-    /**
-     * Gets query for [[Municipios]].
-     *
-     * @return \yii\db\ActiveQuery
-     */
-    public function getMunicipios()
-    {
-        return $this->hasMany(Municipio::class, ['idEstado' => 'id']);
-    }
+  /**
+   * Gets query for [[Municipios]].
+   *
+   * @return \yii\db\ActiveQuery
+   */
+  public function getMunicipios() {
+    return $this->hasMany(Municipio::class, ['idEstado' => 'id']);
+  }
 }

+ 43 - 46
models/Municipio.php

@@ -16,53 +16,50 @@ use Yii;
  *
  * @property Estado $idEstado
  */
-class Municipio extends ModeloBase
-{
-    /**
-     * {@inheritdoc}
-     */
-    public static function tableName()
-    {
-        return 'Municipio';
-    }
+class Municipio extends ModeloBase {
+  /**
+   * {@inheritdoc}
+   */
+  public static function tableName() {
+    return 'Municipio';
+  }
 
-    /**
-     * {@inheritdoc}
-     */
-    public function rules()
-    {
-        return [
-            [['id'], 'required'],
-            [['creado', 'modificado', 'eliminado'], 'safe'],
-            [['id', 'idEstado'], 'string', 'max' => 36],
-            [['nombre'], 'string', 'max' => 100],
-            [['id'], 'unique'],
-            [['idEstado'], 'exist', 'skipOnError' => true, 'targetClass' => Estado::class, 'targetAttribute' => ['idEstado' => 'id']],
-        ];
-    }
+  /**
+   * {@inheritdoc}
+   */
+  public function rules() {
+    return [
+      [['id'], 'required'],
+      [['clave'], 'integer'],
+      [['creado', 'modificado', 'eliminado'], 'safe'],
+      [['id', 'idEstado'], 'string', 'max' => 36],
+      [['nombre'], 'string', 'max' => 100],
+      [['id'], 'unique'],
+      [['idEstado'], 'exist', 'skipOnError' => true, 'targetClass' => Estado::class, 'targetAttribute' => ['idEstado' => 'id']],
+    ];
+  }
 
-    /**
-     * {@inheritdoc}
-     */
-    public function attributeLabels()
-    {
-        return [
-            'id' => 'ID',
-            'nombre' => 'Nombre',
-            'idEstado' => 'Id Estado',
-            'creado' => 'Creado',
-            'modificado' => 'Modificado',
-            'eliminado' => 'Eliminado',
-        ];
-    }
+  /**
+   * {@inheritdoc}
+   */
+  public function attributeLabels() {
+    return [
+      'id' => 'ID',
+      'nombre' => 'Nombre',
+      'idEstado' => 'Id Estado',
+      'creado' => 'Creado',
+      'modificado' => 'Modificado',
+      'eliminado' => 'Eliminado',
+      'clave' => 'Clave'
+    ];
+  }
 
-    /**
-     * Gets query for [[IdEstado]].
-     *
-     * @return \yii\db\ActiveQuery
-     */
-    public function getIdEstado()
-    {
-        return $this->hasOne(Estado::class, ['id' => 'idEstado']);
-    }
+  /**
+   * Gets query for [[IdEstado]].
+   *
+   * @return \yii\db\ActiveQuery
+   */
+  public function getIdEstado() {
+    return $this->hasOne(Estado::class, ['id' => 'idEstado']);
+  }
 }

+ 6 - 7
modules/v1/models/Estado.php

@@ -2,22 +2,21 @@
 
 namespace v1\models;
 
-class Estado extends \app\models\Estado
-{
+class Estado extends \app\models\Estado {
 
-  public function fields()
-  {
+  public function fields() {
     return [
       "id",
       "nombre",
       "abreviacion",
       "creado",
-      "modificado"
+      "modificado",
+      "clave",
+      "pais"
     ];
   }
 
-  public function getMunicipios()
-  {
+  public function getMunicipios() {
     return $this->hasMany(Municipio::className(), ['idEstado' => 'idEstado'])->andWhere(["eliminado" => null]);
   }
 }