<?php

class Upgrade37 extends UpgradeNew
{
    public function getDataPath($folder)
    {
        return $this->getShopDir().'data/'.$folder;
    }

    private $folders = [
        '',
        'backup',
        'files',
        'images',
        'photos',
        'photos/sliders',
        'section',
        'producer',
        'delivery',
        'payment',
        'tmp',
    ];

    public function check_DataFolders()
    {
        foreach ($this->folders as $folder) {
            if (!file_exists($this->getDataPath($folder))) {
                return true;
            }
        }

        return false;
    }

    /** Create ./data folders */
    public function upgrade_DataFolders()
    {
        foreach ($this->folders as $folder) {
            $path = $this->getDataPath($folder);

            if (!file_exists($path)) {
                mkdir($path);
                chmod($path, 0777);
            }
        }

        $this->upgradeOK();
    }

    public function check_SectionsPhotos()
    {
        return $this->checkTableExists('photos_sections_relation');
    }

    /** Create sections photos*/
    public function upgrade_SectionsPhotos()
    {
        sqlQuery("
        CREATE TABLE IF NOT EXISTS `photos_sections_relation` (
           `id_photo` INT(11) UNSIGNED NOT NULL DEFAULT '0',
           `id_section` INT(11) NOT NULL DEFAULT '0',
           `show_in_lead` ENUM('Y','N') NOT NULL DEFAULT 'N',
           `active` ENUM('Y','N') NOT NULL DEFAULT 'Y',
           `date_added` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
           `position` INT(11) DEFAULT NULL
         ) ENGINE=InnoDB  COMMENT='fotky k sekci';

         ALTER TABLE `photos_sections_relation`
          ADD PRIMARY KEY (`id_photo`,`id_section`),
          ADD KEY `photos_sections_relation_ibfk_1` (`id_section`),
          ADD KEY `show_in_lead` (`show_in_lead`);

          ALTER TABLE `photos_sections_relation`
         ADD CONSTRAINT `photos_sections_relation_ibfk_1` FOREIGN KEY (`id_photo`) REFERENCES `photos` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
         ADD CONSTRAINT `photos_sections_relation_ibfk_2` FOREIGN KEY (`id_section`) REFERENCES `sections` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
         ");

        $this->upgradeOK();
    }

    public function check_ParametersConfigurations()
    {
        return findModule(\Modules::PRODUCTS_PARAMETERS, \Modules::SUB_CONFIGURATIONS) && $this->checkTableExists('parameters_configurations');
    }

    /** Create Parameters Configurations table */
    public function upgrade_ParametersConfigurations()
    {
        sqlQuery('
            CREATE TABLE `parameters_configurations` (
              `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
              `id_parameter` INT(10) UNSIGNED NOT NULL,
              `id_product` INT(11)  NOT NULL,
              FOREIGN KEY `id_parameter_fk` (id_parameter) REFERENCES parameters (id) ON DELETE CASCADE ON UPDATE CASCADE,
              FOREIGN KEY `id_product_fk` (id_product) REFERENCES products (id) ON DELETE CASCADE ON UPDATE CASCADE
            ) ENGINE=InnoDB;
        ');

        $this->upgradeOK();
    }

    public function check_ParametersConfigurationsPrice()
    {
        return findModule('products_parameters', 'configurations') && $this->checkColumnExists('parameters_products', 'configuration_price');
    }

    /** Add configuration price to products parameters */
    public function upgrade_ParametersConfigurationsPrice()
    {
        sqlQuery('
        ALTER TABLE `parameters_products` ADD COLUMN `configuration_price` DECIMAL NULL DEFAULT NULL
         ');

        $this->upgradeOK();
    }

    public function check_SectionAnnotation()
    {
        return findModule(\Modules::PRODUCTS_SECTIONS, \Modules::SUB_ANNOTATION) && $this->checkColumnExists('sections', 'annotation');
    }

    /** Add annotation to sections */
    public function upgrade_SectionAnnotation()
    {
        sqlQuery('
        ALTER TABLE `sections` ADD COLUMN `annotation` TEXT NULL DEFAULT NULL
         ');

        $this->upgradeOK();
    }

    public function check_productPriceBuy()
    {
        return findModule(\Modules::PRODUCTS, \Modules::SUB_PRICE_BUY) && $this->checkColumnExists('products', 'price_buy');
    }

    /** Add price_buy to products */
    public function upgrade_productPriceBuy()
    {
        sqlQuery('ALTER TABLE `products` ADD COLUMN `price_buy` DECIMAL(15, 4) NULL DEFAULT NULL');
        sqlQuery('ALTER TABLE `products_variations` ADD COLUMN `price_buy` DECIMAL(15, 4) NULL DEFAULT NULL');

        $this->upgradeOK();
    }

    public function check_PriceRoundOrder()
    {
        return is_null(Settings::getDefault()->price_round_order);
    }

    /** Add order price rounding to settings */
    public function upgrade_PriceRoundOrder()
    {
        $cfg = Settings::getDefault();

        $cfg->saveValue('price_round_order', $cfg->price_round);

        $this->upgradeOK();
    }

    public function check_OrderDueDate()
    {
        return findModule(\Modules::ORDERS) && $this->checkColumnExists('orders', 'date_due');
    }

    /** Add due date to order*/
    public function upgrade_OrderDueDate()
    {
        sqlQuery('
        ALTER TABLE `orders` ADD COLUMN `date_due` DATETIME NULL DEFAULT NULL AFTER date_updated
         ');

        $this->upgradeOK();
    }

    public function check_POSFakeProduct()
    {
        $id_product = returnSQLResult('SELECT id FROM products WHERE id=0');

        return findModule(\Modules::POS) && $id_product === false;
    }

    /** Create POS fake product - neprodukt */
    public function upgrade_POSFakeProduct()
    {
        sqlQuery("INSERT INTO products (id, title, code, figure, show_in_feed, short_descr, long_descr, parameters, vat, campaign) VALUES (0, 'NEPRODUKT - POKLADNA - NEMAZAT', '0', 'Y', 'N', 'NEPRODUKT - POKLADNA - NEMAZAT', 'NEPRODUKT - POKLADNA - NEMAZAT', 'NEPRODUKT - POKLADNA - NEMAZAT', 1, '');");
        $id = sqlInsertId();

        $this->updateSQL('products', ['id' => 0], ['id' => $id]);

        $this->upgradeOK();
    }
}
