<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use App\Models\Currency;

class CreateTableCurrencies extends Migration
{
	private $convert = 'App\Models\Old\Currency';

	/**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{
		Schema::dropIfExists('currencies');
		Schema::create('currencies', function (Blueprint $table) {
			$table->increments('id');
			$table->string('name');
			$table->string('symbol',2)->nullable();
			$table->string('threecode',3);
			$table->boolean('active');
		});

		if ($this->convert)
			foreach (($this->convert)::all() as $o)
			{
				$oo = new Currency;
				$oo->name = $o->name;
				$oo->symbol = $o->symbol;
				$oo->threecode = $o->three_digit;
				$oo->active = $o->three_code == 'AUS' ? TRUE : FALSE;
				$oo->save();
			}
	}

	/**
	 * Reverse the migrations.
	 *
	 * @return void
	 */
	public function down()
	{
		Schema::dropIfExists('currencies');
	}
}