<?php

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

class CreateLoansTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('loans', function (Blueprint $table) {
            $table->id();
            $table->string('loan_code',100)->nullable();
            $table->foreignId('branch_id')->references('id')->on('branches');
            $table->foreignId('client_id')->references('id')->on('clients');
            $table->foreignId('co_id')->references('id')->on('users');
            $table->foreignId('currency_id')->references('id')->on('currencies');
            $table->foreignId('chart_of_acc_id')->references('id')->on('chart_of_accounts');
            $table->date('completed_date')->nullable();
            $table->date('installment_date')->nullable();
            $table->date('start_date')->nullable();
            $table->date('expired_date')->nullable();
            $table->date('write_off_date')->nullable();
            $table->double('reschedule_amount',20,2)->default(0.00);
            $table->double('loan_out',20,2)->default(0.00);
            $table->double('interest',4,2)->default(0.00);
            $table->double('total_interest',20,2)->default(0.00);
            $table->double('operating_service',4,2)->default(0.00)->comment('សេវាប្រតិបត្តិការ');
            $table->double('total_operating_service',20,2)->default(0.00);
            $table->double('maintenance',4,2)->default(0.00)->comment('សេវាសុវត្ថិភាព');
            $table->double('total_maintenance',20,2)->default(0.00);
            $table->double('service',4,2)->default(0.00)->comment('សេវារដ្ធបាល');
            $table->double('total_service',20,2)->default(0.00);
            $table->double('service_collection',4,2)->default(0.00)->comment('សេវាប្រមូល');
            $table->double('total_collection',20,2)->default(0.00);
            $table->double('insurance',20,2)->default(0.00)->comment('សេវាធានារ៉ាប់រង');
            $table->double('total_insurance',20,2)->default(0.00);
            $table->double('saving',20,2)->default(0.00);
            $table->double('total_saving',20,2)->default(0.00);
            $table->double('loan_receive',20,2)->default(0.00);
            $table->double('amount_client_receive',20,2)->default(0.00)->comment('Total amount client receive from loan disburst after sub(total service and insurance).');
            $table->double('balance_principal',20,2)->default(0.00);
            $table->double('balance_interest',20,2)->default(0.00);
            $table->double('balance_operating_service',20,2)->default(0.00);
            $table->double('balance_maintenance',20,2)->default(0.00);
            $table->double('loan_balance',20,2)->default(0.00);
            $table->double('paid_principal',20,2)->default(0.00);
            $table->double('paid_interest',20,2)->default(0.00);
            $table->double('paid_operating_service',20,2)->default(0.00);
            $table->double('paid_maintenance',20,2)->default(0.00);
            $table->double('loan_in',20,2)->default(0.00);
            $table->double('total_penalty',20,2)->default(0.00);
            $table->double('total_waiv',20,2)->default(0.00);
            $table->integer('period',)->default(0);
            $table->integer('level_loan',)->default(1);
            $table->integer('term_type',)->default(0)->comment('1=ថ្ងៃ, 2=សប្ដាហ៍, 3=កន្លះខែ, 4=ខែ');
            $table->integer('credit_type',)->default(0)->comment('1=អាជីវកម្ម, 2=ប្រើប្រាស់ផ្ទាល់ខ្លួន, 3=អតិថិជនថ្មី, 4=អតិថិជនចាស់');
            $table->integer('condition_pay',)->default(0)->comment('1=បង់ប្រាក់ថេរ, 2=បង់ប្រាក់ដើមថេរ, 3=បង់ប្រាក់ដើមនិងការប្រាក់ថេរ, 4=បង់តែការប្រាក់');
            $table->integer('loan_status_id')->default(0);
            $table->integer('type_loan')->default(1)->comment('1=Personal, 2=Team');
            $table->dateTime('closing_date')->nullable();
            $table->integer('approved_by')->nullable();
            $table->integer('closing_by')->nullable();
            $table->integer('term_condi_id')->nullable();
            $table->integer('client_type',)->default(1)->comment('1=Normal Client Pay, 2=Client Write Off, 3=Client Black List');
            $table->text('desc')->nullable();
            $table->softDeletes();
            $table->timestamps();
        });
    }

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