<?php

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

class PropertyChannelBookingPaymentSetupAddChannelMappingId extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        DB::statement("
ALTER TABLE `property_channel_booking_payment_setup`
ADD COLUMN `property_channel_mapping_id` int(10) UNSIGNED NULL AFTER `channel_id`,
ADD COLUMN `is_affected_price` tinyint(1) NOT NULL AFTER `payment_type_id`,
ADD FOREIGN KEY (`property_channel_mapping_id`) REFERENCES `property_channel_mapping` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT;
        ");

        DB::statement("
UPDATE property_channel_booking_payment_setup
SET property_channel_mapping_id = (
	SELECT id FROM property_channel_mapping
	WHERE property_channel_booking_payment_setup.channel_id = property_channel_mapping.channel_id
		AND property_channel_booking_payment_setup.property_id = property_channel_mapping.property_id
	LIMIT 1
	)
        ") ;

        DB::statement("
        ALTER TABLE `property_channel_booking_payment_setup`
MODIFY COLUMN `property_channel_mapping_id` int(10) UNSIGNED NOT NULL AFTER `channel_id`;
        ") ;


    }


}
