<?php

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

class CreateBoxOrdersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('box_orders', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger("box_id")->nullable();
            $table->unsignedBigInteger("payment_id")->nullable();
            $table->unsignedBigInteger("user_id")->nullable();
            $table->unsignedBigInteger("invoice_id")->nullable();
            $table->double("subtotal",2);
            $table->double("vat",2);
            $table->unsignedBigInteger("discount_id")->nullable();
            $table->double("total",2);
            $table->timestamps();

            $table->foreign("box_id")->references("id")->on("boxes")->onDelete("cascade"); //si borren un box, borra les entrades amb aquest box
            $table->foreign("payment_id")->references("id")->on("payments")->onDelete("cascade"); //si borren un box, borra les entrades amb aquest box
            $table->foreign("user_id")->references("id")->on("users")->onDelete("cascade"); //si borren un box, borra les entrades amb aquest box
            $table->foreign("invoice_id")->references("id")->on("invoices")->onDelete("cascade"); //si borren un box, borra les entrades amb aquest box
        });
    }

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