<?php

class ModelUserInstall extends Model
{


    public function update_tables()
    {
        $this->runExecute("CREATE TABLE IF NOT EXISTS `product_url` (
  `stock_code` varchar(128) NOT NULL,
  `barcode` varchar(128) NOT NULL,
  `url` text NOT NULL,
  `sale_price` decimal(10,2) NOT NULL,
  `stock` varchar(128) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;");

    }




    public function install_tables()
    {


        $this->runExecute("CREATE TABLE IF NOT EXISTS `setting` (
  `setting_id`  INT NOT NULL AUTO_INCREMENT,
  `code` varchar(128) NOT NULL,
  `key` varchar(128) NOT NULL,
  `value` text NOT NULL,
  `serialized` tinyint(1) NOT NULL, PRIMARY KEY (`setting_id`)) ENGINE = InnoDB;");

        $this->runExecute("ALTER TABLE `setting` ADD INDEX(`setting_id`);");


        $this->runExecute("CREATE TABLE IF NOT EXISTS `product` (
 `product_id` INT NOT NULL AUTO_INCREMENT,
  `model` varchar(256) CHARACTER SET utf8 NOT NULL,
  `barcode` varchar(256) CHARACTER SET utf8 NOT NULL,
  `stock_code` varchar(256) CHARACTER SET utf8 NOT NULL,
  `quantity` tinyint(4) NOT NULL,  
  `image` varchar(256) CHARACTER SET utf8 NOT NULL,
  `category_id` int(11) NOT NULL,
  `category_name` varchar(256) CHARACTER SET utf8 NOT NULL,  
  `manufacturer_id` int(11) NOT NULL,
  `manufacturer_name` varchar(256) CHARACTER SET utf8 NOT NULL,
  `shipping` tinyint(1) NOT NULL,
  `sale_price` float(10,2) NOT NULL,
  `list_price` float(10,2) NOT NULL,
  `tax` tinyint(2) NOT NULL,
  `desi` tinyint(2) NOT NULL,
  `varianter` tinyint(1) NOT NULL,
  `sort_order` tinyint(3) NOT NULL,
  `status` tinyint(1) NOT NULL,
  `date_added` datetime NOT NULL,
  `date_modified` datetime NOT NULL, PRIMARY KEY (`product_id`)) ENGINE = InnoDB;");



        $this->runExecute("CREATE TABLE IF NOT EXISTS `product_description` (
  `product_id` int(11) NOT NULL,
  `language_id` int(11) NOT NULL,
  `name` text CHARACTER SET utf8 NOT NULL,
  `description` text CHARACTER SET utf8 NOT NULL,
  `tag` text CHARACTER SET utf8 NOT NULL
)");

        $this->runExecute("ALTER TABLE `product_description` ADD INDEX(`product_id`);");

        $this->runExecute("CREATE TABLE IF NOT EXISTS `product_image` (
  `product_image_id` int(11) NOT NULL  AUTO_INCREMENT,
  `product_id` int(11) NOT NULL,
  `image` varchar(256) CHARACTER SET utf8 NOT NULL,
  `sort_order` int(11) NOT NULL , PRIMARY KEY (`product_image_id`)) ENGINE = InnoDB;");

        $this->runExecute("ALTER TABLE `product_image` ADD INDEX(`product_image_id`);");


        $this->runExecute(
            "CREATE TABLE IF NOT EXISTS `temp_image` ( `image_id` INT NOT NULL AUTO_INCREMENT , `url` VARCHAR(256) NOT NULL , `product_id` INT(11) NOT NULL , `sort_order` INT(1) NOT NULL , `status` INT(1) NOT NULL , `message` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL , PRIMARY KEY (`image_id`)) ENGINE = InnoDB;
");

        

        $this->runExecute("CREATE TABLE IF NOT EXISTS `product_variant` (
  `product_variant_id` INT NOT NULL AUTO_INCREMENT,
  `product_id` int(11) NOT NULL,
  `name` varchar(256) CHARACTER SET utf8 NOT NULL,
  `image` varchar(256) CHARACTER SET utf8 NOT NULL,
  `barcode` varchar(256) CHARACTER SET utf8 NOT NULL,
  `stock_code` varchar(256) CHARACTER SET utf8 NOT NULL,
  `quantity` tinyint(5) NOT NULL,
  `sale_price` float(10,2) NOT NULL,
  `sort_order` int(2) NOT NULL,
    PRIMARY KEY (`product_variant_id`)) ENGINE = InnoDB");

        $this->runExecute("ALTER TABLE `product_variant` ADD INDEX(`product_variant_id`);");


        $this->runExecute("CREATE TABLE IF NOT EXISTS `product_variant_value` (
  `product_variant_id` int(11) NOT NULL,
  `attribute_id` int(11) NOT NULL,
  `attribute_value_id` int(11) NOT NULL,
  `sort_order` tinyint(3) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;");

        $this->runExecute("ALTER TABLE `product_variant_value` ADD INDEX (`product_variant_id`)");

/*
        $this->runExecute("CREATE TABLE IF NOT EXISTS `request` (
  `req_id` INT NOT NULL AUTO_INCREMENT,
  `product_id` int(11) NOT NULL,
  `variant_id` int(11) NOT NULL,
  `sale_price` decimal(15,4) NOT NULL,
  `list_price` decimal(15,4) NOT NULL,
  `quantity` int(11) NOT NULL,
  `product_code` varchar(64) NOT NULL,
  `code` varchar(10) NOT NULL,
  `service_type` int(11) NOT NULL,
  `date_added` datetime NOT NULL, PRIMARY KEY (`req_id`)) ENGINE = InnoDB");

        $this->runExecute("ALTER TABLE `request` ADD INDEX(`req_id`);");

*/

        $this->runExecute(" CREATE TABLE IF NOT EXISTS `category_setting` (
`id` INT NOT NULL AUTO_INCREMENT,
`category_id` int(11) NOT NULL,
`n11` text CHARACTER SET utf8 NOT NULL,
`gg` text CHARACTER SET utf8 NOT NULL,
`hb` text CHARACTER SET utf8 NOT NULL,
`ty` text CHARACTER SET utf8 NOT NULL,
`eptt` text CHARACTER SET utf8 NOT NULL,
`amz` text CHARACTER SET utf8 NOT NULL,
`amzus` text CHARACTER SET utf8 NOT NULL,
`amzeu` text CHARACTER SET utf8 NOT NULL,
`cs` text CHARACTER SET utf8 NOT NULL,
`etsy` text CHARACTER SET utf8 NOT NULL,
`date_added` datetime NOT NULL,
`date_modified` datetime NOT NULL, PRIMARY KEY (`id`)) ENGINE = InnoDB");

        $this->runExecute("ALTER TABLE `category` ADD INDEX(`category_id`);");

        $this->runExecute("CREATE TABLE IF NOT EXISTS `market_product` (
  `market_product_id` INT NOT NULL AUTO_INCREMENT,
  `code` varchar(10) CHARACTER SET utf8 NOT NULL,
  `product_id` int(11) NOT NULL,
  `name` varchar(255) CHARACTER SET utf8 NOT NULL,
  `model` varchar(255) CHARACTER SET utf8 NOT NULL,
  `barcode` varchar(255) CHARACTER SET utf8 NOT NULL,
  `stock_code` varchar(255) CHARACTER SET utf8 NOT NULL,
  `sale_price` decimal(15,4) NOT NULL,
  `list_price` decimal(15,4) NOT NULL,
  `quantity` int(11) NOT NULL,
  `sale_status` int(1) NOT NULL,
  `approval_status` int(1) NOT NULL,
  `custom_data` longtext CHARACTER SET utf8 NOT NULL,
  `marketplace_product_id` varchar(256) NOT NULL,
  `brand` varchar(128) NOT NULL,
  `date_added` datetime NOT NULL,
  `date_modified` datetime NOT NULL, PRIMARY KEY (`market_product_id`)) ENGINE = InnoDB");
        $this->runExecute("ALTER TABLE `market_product` ADD INDEX(`market_product_id`);");
        $this->runExecute("ALTER TABLE `market_product` ADD INDEX(`name`);");
        $this->runExecute("ALTER TABLE `market_product` ADD INDEX(`model`);");
        $this->runExecute("ALTER TABLE `market_product` ADD INDEX(`barcode`);");
       // $this->runExecute("ALTER TABLE `market_product` ADD FULLTEXT(`name`);");


        $this->runExecute("CREATE TABLE IF NOT EXISTS `product_url` (
  `stock_code` varchar(128) NOT NULL,
  `barcode` varchar(128) NOT NULL,
  `url` text NOT NULL,
  `sale_price` decimal(10,2) NOT NULL,
  `stock` varchar(128) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;");


//MANUFACTURER
        $this->runExecute(" CREATE TABLE IF NOT EXISTS `manufacturer_setting` (
`manufacturer_id` int(11) NOT NULL,
`n11` text CHARACTER SET utf8 NOT NULL,
`gg` text CHARACTER SET utf8 NOT NULL,
`hb` text CHARACTER SET utf8 NOT NULL,
`ty` text CHARACTER SET utf8 NOT NULL,
`eptt` text CHARACTER SET utf8 NOT NULL,
`amz` text CHARACTER SET utf8 NOT NULL,
`amzus` text CHARACTER SET utf8 NOT NULL,
`amzeu` text CHARACTER SET utf8 NOT NULL,
`cs` text CHARACTER SET utf8 NOT NULL,
`etsy` text CHARACTER SET utf8 NOT NULL,
`date_added` datetime NOT NULL,
`date_modified` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;");

        $this->runExecute("ALTER TABLE `manufacturer_setting` ADD INDEX(`manufacturer_id`);");



            $this->runExecute("CREATE TABLE IF NOT EXISTS `order` (
`order_id`INT NOT NULL AUTO_INCREMENT,
`code` varchar(11) NOT NULL,
`market_order_id` varchar(256) NOT NULL,
`order_status` varchar(64) CHARACTER SET utf8 NOT NULL,
`first_name` varchar(32) CHARACTER SET utf8 NOT NULL,
`last_name` varchar(32) CHARACTER SET utf8 NOT NULL,
`total` float(10,2) NOT NULL,
`shipping_address` text CHARACTER SET utf8 NOT NULL,
`billing_address` text CHARACTER SET utf8 NOT NULL,
`phone` varchar(15) NOT NULL,
`email` varchar(32) CHARACTER SET utf8 NOT NULL,
`city` varchar(32) CHARACTER SET utf8 NOT NULL,
`town` varchar(256) CHARACTER SET utf8 NOT NULL,
`shipping_info` text CHARACTER SET utf8 NOT NULL,
`payment_info` text CHARACTER SET utf8 NOT NULL,
`date_added` datetime NOT NULL,
`date_modified` datetime NOT NULL, PRIMARY KEY (`order_id`)) ENGINE = InnoDB");

        $this->runExecute("ALTER TABLE `order` ADD INDEX(`order_id`);");




        $this->runExecute("CREATE TABLE IF NOT EXISTS `ordered_product` (
`order_id` int(11) NOT NULL,
`product_id` int(11) NOT NULL,
`model` varchar(64) CHARACTER SET utf8 NOT NULL,
`item_id` int(11) NOT NULL,
`quantity` int(3) NOT NULL,
`list_price` float(10,2) NOT NULL,
`price` float(10,2) NOT NULL,
`kdv`  float(10,2) NOT NULL,
`discount` float(10,2) NOT NULL,
`name` varchar(256) CHARACTER SET utf8mb4 NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;");

        $this->runExecute("ALTER TABLE `ordered_product` ADD INDEX(`order_id`);");


        $this->runExecute("CREATE TABLE IF NOT EXISTS `order_status` (
`order_status_id` int(11) NOT NULL,
`name` varchar(64) CHARACTER SET utf8 NOT NULL,
`oc` varchar(64) CHARACTER SET utf8 NOT NULL,
`n11` text CHARACTER SET utf8 NOT NULL,
`gg` text CHARACTER SET utf8 NOT NULL,
`hb` text CHARACTER SET utf8 NOT NULL,
`ty` text CHARACTER SET utf8 NOT NULL,
`eptt` text CHARACTER SET utf8 NOT NULL,
`amz` text CHARACTER SET utf8 NOT NULL,
`amzus` text CHARACTER SET utf8 NOT NULL,
`amzeu` text CHARACTER SET utf8 NOT NULL,
`cs` text CHARACTER SET utf8 NOT NULL,
`etsy` text CHARACTER SET utf8 NOT NULL
 NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;");






     /*   $isExists=$this->runExecute("select * from `order_status` ");


        if(!$isExists->num_rows){
            $this->runExecute("INSERT INTO `order_status` (`order_status_id`,`name`, `oc`, `n11`, `gg`, `ty`, `eptt`, `hb`) VALUES
(1,'Onay Bekliyor', '1', '1', '0', '0', '0', '0'),
(2, 'Kargolanma Aşamasında','2', '5', 'STATUS_WAITING_CARGO_INFO', 'ReadyToShip', 'kargo_yapilmasi_bekleniyor', 'Open'),
(3, 'Kargolandı','3', '6', 'STATUS_WAITING_APPROVAL', 'Shipped', 'gonderilmis', 'Unpacked');");
        }
     */

        $this->runExecute("CREATE TABLE IF NOT EXISTS `product_setting` (
`product_id` int(11) NOT NULL,

`n11` text CHARACTER SET utf8 NOT NULL,
`gg` text CHARACTER SET utf8 NOT NULL,
`hb` text CHARACTER SET utf8 NOT NULL,
`ty` text CHARACTER SET utf8 NOT NULL,
`eptt` text CHARACTER SET utf8 NOT NULL,
`amz` text CHARACTER SET utf8 NOT NULL,
`amzus` text CHARACTER SET utf8 NOT NULL,
`amzeu` text CHARACTER SET utf8 NOT NULL,
`cs` text CHARACTER SET utf8 NOT NULL,
`etsy` text CHARACTER SET utf8 NOT NULL,
`date_added` datetime NOT NULL,
`date_modified` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
        $this->runExecute("ALTER TABLE `product_setting` ADD INDEX(`product_id`);");


        $this->runExecute("CREATE TABLE IF NOT EXISTS `attribute` (
  `attribute_id` int(11) NOT NULL,
  `category_id` varchar(64) CHARACTER SET utf8 NOT NULL,
  `attribute` longtext CHARACTER SET utf8 NOT NULL,
   `required` varchar(256) NOT NULL,
  `code` varchar(5) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;");

        $this->runExecute("ALTER TABLE `attribute` ADD INDEX(`attribute_id`);");



        $this->runExecute("CREATE TABLE IF NOT EXISTS `product_error` (
  `product_id` int(11) NOT NULL,
   `code` varchar(10) NOT NULL,
     `type` int(1) NOT NULL,
  `error` text CHARACTER SET utf8 NOT NULL,
  `solution_id` int(11) NOT NULL,
  `date_added` datetime NOT NULL,
`date_modified` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;");

        $this->runExecute("ALTER TABLE `product_error` ADD INDEX(`product_id`);");



        $this->runExecute("CREATE TABLE IF NOT EXISTS `product_to_marketplace` (
`product_id` int(11) NOT NULL,

    `n11` text CHARACTER SET utf8 NOT NULL,
`gg` text CHARACTER SET utf8 NOT NULL,
`hb` text CHARACTER SET utf8 NOT NULL,
`ty` text CHARACTER SET utf8 NOT NULL,
`eptt` text CHARACTER SET utf8 NOT NULL,
`amz` text CHARACTER SET utf8 NOT NULL,
`amzus` text CHARACTER SET utf8 NOT NULL,
`amzeu` text CHARACTER SET utf8 NOT NULL,
`cs` text CHARACTER SET utf8 NOT NULL,
`etsy` text CHARACTER SET utf8 NOT NULL,

`date_added` datetime NOT NULL,
`date_modified` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;");


        $this->runExecute("ALTER TABLE `product_to_marketplace` ADD INDEX(`product_id`);");


        $this->runExecute("CREATE TABLE IF NOT EXISTS `variant_to_marketplace` (
`product_variant_id` int(11) NOT NULL,
`n11` text CHARACTER SET utf8 NOT NULL,
`gg` text CHARACTER SET utf8 NOT NULL,
`hb` text CHARACTER SET utf8 NOT NULL,
`ty` text CHARACTER SET utf8 NOT NULL,
`eptt` text CHARACTER SET utf8 NOT NULL,
`amz` text CHARACTER SET utf8 NOT NULL,
`amzus` text CHARACTER SET utf8 NOT NULL,
`amzeu` text CHARACTER SET utf8 NOT NULL,
`cs` text CHARACTER SET utf8 NOT NULL,
`etsy` text CHARACTER SET utf8 NOT NULL,
`date_added` datetime NOT NULL,
`date_modified` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;");


        $this->runExecute("CREATE TABLE IF NOT EXISTS `product_question` (
 
  `product_question_id` INT NOT NULL AUTO_INCREMENT,
  `question_id` varchar(255) NOT NULL,
  `user` varchar(50) CHARACTER SET utf8 NOT NULL,
  `product` varchar(255) CHARACTER SET utf8 NOT NULL,
  `code` varchar(10) CHARACTER SET utf8 NOT NULL,
  `is_rejected` int(1) NOT NULL,
  `answered` int(1) NOT NULL,
  `message` text CHARACTER SET utf8 NOT NULL,
  `date_added` datetime NOT NULL , PRIMARY KEY (`product_question_id`)) ENGINE = InnoDB");
        $this->runExecute("ALTER TABLE `product_question` ADD INDEX(`product_question_id`);");

        

    }

    private function runExecute($sql)
    {
        try {
            $this->udb->query($sql);
        } catch (Exception $exception) {

            echo $exception->getMessage();
        }

    }


}