<?php

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

class AddTypeToTransactionMappingLogsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('transaction_mapping_logs', function (Blueprint $table) {
            $table->string('type',50)->default('invoices')->after('imported_date');
        });

        foreach (DB::table('transaction_mapping_logs')->get() as $key => $value) {
            $data = json_decode($value->data);
            DB::table('transaction_mapping_logs')->where('id',$value->id)->update([
                'type' => (isset($data->description) ? 'receipts' : 'invoices')
            ]);
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('transaction_mapping_logs', function (Blueprint $table) {
            $table->dropColumn('type');
        });
    }
}
