<?php

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

class CreateInvoicesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('invoices', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger("company_id");
            $table->enum("type", ["income", "expense"]);
            $table->string("from_fiscal_name");
            $table->string("from_nif_cif");
            $table->string("from_fiscal_address");
            $table->string("to_fiscal_name");
            $table->string("to_nif_cif");
            $table->string("to_fiscal_address");
            $table->string("invoice_prefix")->nullable();
            $table->string("invoice_serie");
            $table->unsignedBigInteger("invoice_number");
            $table->date("emitted_date")->index();
            $table->date("due_date");
            $table->string("concept");
            $table->float("subtotal");
            $table->float("tax_prcnt");
            $table->float("tax_amount");
            $table->float("total");
            $table->timestamps();

            $table->unique(["company_id", "invoice_prefix", "invoice_serie", "invoice_number"], "invoice_numeration_unique");

            $table->foreign("company_id")->on("companies")->references("id")->onDelete("restrict");
        });
    }

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