<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateAkruNetPecahanTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */


     public function getActiveDB(){
        $config_db = array_keys(config("database.connections"));

        $db_connection = array_filter($config_db,function($connection){
            try{
                if($connection == 'mysql7')
                    return false;

                $db = DB::connection($connection)->getPdo();
                return true;
            } catch(\Exception $e){
                return false;
            }
        });
        return $db_connection;
    }

    public function up()
    {

        $db_connection = $this->getActiveDB();

        foreach($db_connection as $dbase){

            if (!Schema::connection($dbase)->hasTable('akru_net_pecahan')) {
                Schema::connection($dbase)->create('akru_net_pecahan', function (Blueprint $table) {
                    $table->id();
                    $table->integer('tahun');
                    $table->integer('bulan');
                    $table->decimal('persen_semasa', 9, 2);
                    $table->decimal('ujrah_semasa', 9, 2);
                    $table->decimal('persen_sebelum', 9, 2);
                    $table->decimal('ujrah_sebelum', 9, 2);
                    $table->decimal('keuntungan_semasa_1persen', 9, 2);
                    $table->decimal('keuntungan_semasa_ujrah', 9, 2);
                    $table->decimal('baki_keun_blm_byr_1persen', 9, 2);
                    $table->decimal('baki_keun_blm_byr_ujrah', 9, 2);
                    $table->decimal('pend_akru_1persen', 9, 2);
                    $table->decimal('pend_akru_ujrah', 9, 2);
                });
            }
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        $db_connection = $this->getActiveDB();

        foreach($db_connection as $dbase){

            Schema::connection($dbase)->dropIfExists('akru_net_pecahan');
        };
    }
}
