<?php

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

class CreateImbangdugabackupToDbcawangan extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
        /**
     *
     * @var object $db_connection
     */

     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('imbang_duga_backup')) {

                Schema::connection($dbase)->create('imbang_duga_backup', function (Blueprint $table) {
                    $table->id('recno');
                    $table->string('kodcaw',10)->nullable();
                    $table->date('tarikh')->nullable();
                    $table->decimal('tunaiditangan', $precision = 11, $scale = 2)->nullable();
                    $table->decimal('pembiayaantawarruq', $precision = 11, $scale = 2)->nullable();
                    $table->decimal('bayaranpendahuluan', $precision = 11, $scale = 2)->nullable();
                    $table->decimal('pinjamankoop', $precision = 11, $scale = 2)->nullable();
                    $table->decimal('emaspalsu', $precision = 11, $scale = 2)->nullable();
                    $table->decimal('serahan', $precision = 11, $scale = 2)->nullable();
                    $table->decimal('bds', $precision = 11, $scale = 2)->nullable();
                    $table->decimal('perubatan', $precision = 11, $scale = 2)->nullable();
                    $table->decimal('keraian', $precision = 11, $scale = 2)->nullable();
                    $table->decimal('rugilelong', $precision = 11, $scale = 2)->nullable();
                    $table->decimal('bakilelong', $precision = 11, $scale = 2)->nullable();
                    $table->decimal('wtd', $precision = 11, $scale = 2)->nullable();
                    $table->decimal('untungrugiterkumpul', $precision = 11, $scale = 2)->nullable();
                    $table->decimal('keun_sebenar', $precision = 11, $scale = 2)->nullable();
                    $table->decimal('pendahuluan', $precision = 11, $scale = 2)->nullable();
                    $table->decimal('cajlelong', $precision = 11, $scale = 2)->nullable();
                    $table->decimal('pindahkekpkb', $precision = 11, $scale = 2)->nullable();
                    $table->decimal('penghutangkaki', $precision = 11, $scale = 2)->nullable();
                    $table->decimal('phapuskira', $precision = 11, $scale = 2)->nullable();
                    $table->decimal('belian1stop', $precision = 11, $scale = 2)->nullable();
                    $table->decimal('keun_penggadai', $precision = 11, $scale = 2)->nullable();
                    $table->decimal('pend_terakru', $precision = 11, $scale = 2)->nullable();
                    $table->decimal('cajperkhidmatan', $precision = 11, $scale = 2)->nullable();
                    $table->decimal('cajlelongxtra', $precision = 11, $scale = 2)->nullable();
                    $table->decimal('upahsimpanxtra', $precision = 11, $scale = 2)->nullable();
                    $table->decimal('bakilelongxtra', $precision = 11, $scale = 2)->nullable();
                    $table->decimal('rugilelongxtra', $precision = 11, $scale = 2)->nullable();
                    $table->decimal('utgterkumpulxtra', $precision = 11, $scale = 2)->nullable();
                    $table->decimal('jualan1stop', $precision = 11, $scale = 2)->nullable();
                    $table->decimal('hapuskira', $precision = 11, $scale = 2)->nullable();
                    $table->decimal('totalharta', $precision = 11, $scale = 2)->nullable();
                    $table->decimal('totaltanggungan', $precision = 11, $scale = 2)->nullable();
                    $table->integer('peringatan')->nullable();
                    $table->timestamps();
                });

            }


        }


    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {

        $db_connection = $this->getActiveDB();

        foreach($db_connection as $dbase){

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


    }
}
