<?php

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

class CreatePromotionUserOrdersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('promotion_user_orders', function (Blueprint $table) {
            $table->increments('id');

            $table->unsignedInteger('promotion_admin_id')->index();
            $table->unsignedInteger('promotion_user_id')->index();
            $table->unsignedInteger('promotion_user_product_id')->index();
            $table->unsignedInteger('product_id')->index();
            $table->unsignedInteger('shopping_order_item_id')->index();
            $table->unsignedInteger('shopping_order_id')->index();
            $table->unsignedInteger('shopping_user_id')->index();

            $table->unsignedSmallInteger('qty')->nullable();
            $table->decimal('price', 8, 2)->nullable();
            $table->decimal('price_net', 8, 3)->nullable();
            $table->decimal('tax_rate', 5, 2)->nullable();
            $table->boolean('pick_up')->default(false);
            $table->unsignedTinyInteger('status')->index()->default(0);

            $table->foreign('promotion_admin_id')
                ->references('id')
                ->on('promotion_admins');     

            $table->foreign('promotion_user_id')
                ->references('id')
                ->on('promotion_users');     

            $table->foreign('promotion_user_product_id')
                ->references('id')
                ->on('promotion_user_products');

            $table->foreign('product_id')
                ->references('id')
                ->on('products');

            $table->foreign('shopping_order_item_id')
                ->references('id')
                ->on('shopping_order_items');

            $table->foreign('shopping_order_id')
                ->references('id')
                ->on('shopping_orders');

            $table->foreign('shopping_user_id')
                ->references('id')
                ->on('shopping_users');


            $table->timestamps();
        });
    }

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