<?php

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

class CreatePrPurchasePaymentsTable extends Migration
{
    public function up()
    {
        Schema::dropIfExists('pr_purchase_payments');
        Schema::create('pr_purchase_payments', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('pr_purchase_share_id');
            $table->decimal('paid_amount', 15, 4);
            $table->unsignedInteger('paid_currency_id')->nullable();
            $table->decimal('rate_to_egp', 15, 6)->default(1);
            $table->decimal('egp_amount', 15, 4);
            $table->unsignedInteger('order_currency_id')->nullable();
            $table->decimal('rate_to_order_currency', 15, 6)->default(1);
            $table->decimal('order_currency_amount', 15, 4);
            $table->text('notes')->nullable();
            $table->boolean('received')->default(false);
            $table->text('received_note')->nullable();
            $table->timestamp('received_at')->nullable();
            $table->unsignedInteger('created_by');
            $table->timestamps();

            $table->foreign('pr_purchase_share_id')
                  ->references('id')->on('pr_purchase_shares')
                  ->onDelete('cascade');

            $table->foreign('paid_currency_id')
                  ->references('id')->on('pr_currencies')
                  ->onDelete('set null');

            $table->foreign('order_currency_id')
                  ->references('id')->on('pr_currencies')
                  ->onDelete('set null');
        });
    }

    public function down()
    {
        Schema::dropIfExists('pr_purchase_payments');
    }
}
