<?php

use yii\db\Migration;

class m160516_222821_orders extends Migration
{
    public function up()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
        }


        $this->createTable('{{%orders}}', [
            'id' => $this->primaryKey(),
            'user_id' => $this->integer(),
            'name' => $this->string()->notNull(),
            'surname' => $this->string()->notNull(),
            'patronymic' => $this->string()->notNull(),
            'phone' => $this->string(),
            'phone2' => $this->string(),
            'email' => $this->string(),
            'adress' => $this->string(),
            'body' => $this->text(),
            'total' => $this->float(),
            'date_time' => $this->dateTime(),
            'date_dedline' => $this->date(),
            'reserve' => $this->string(),
            'status' => $this->string(),
            'comment' => $this->text(),
            'label' => $this->integer(),
            'pay' => $this->integer(),
            'numbercard' => $this->integer(),
            'delivery' => $this->string(),
            'declaration' => $this->string(),
            'stock' => $this->string(),
            'consignment' => $this->string(),
            'payment' => $this->string(),
            'insurance' => $this->string(),
            'amount_imposed' => $this->float(),
            'shipping_by' => $this->string(),
            'city' => $this->string(),
        ], $tableOptions);


        $this->createTable('{{%orders_products}}', [
            'id' => $this->primaryKey(),
            'order_id' => $this->integer(),
            'mod_id' => $this->integer(),
            'product_name' => $this->string(),
            'name' => $this->string(),
            'sku' => $this->string(),
            'price' => $this->float(),
            'count' => $this->integer(),
            'sum_cost' => $this->float(),
        ], $tableOptions);

        $this->addForeignKey('orders_products_fk', '{{%orders_products}}', 'order_id', '{{%orders}}', 'id', 'CASCADE', 'CASCADE');
        $this->addForeignKey('orders_products_items_fk', '{{%orders_products}}', 'id', '{{%product}}', 'product_id', 'RESTRICT', 'RESTRICT');
    }

    public function down()
    {
        $this->dropForeignKey('orders_products_fk', '{{%orders_products}}');
        $this->dropForeignKey('orders_products_items_fk', '{{%orders_products}}');
        $this->dropTable('{{%orders}}');
        $this->dropTable('{{%orders_products}}');
    }

}