Your IP : 216.73.216.86


Current Path : /var/www/homesaver/www/bitrix/modules/fermaofd.ferma/install/
Upload File :
Current File : /var/www/homesaver/www/bitrix/modules/fermaofd.ferma/install/index.php

<?php

IncludeModuleLangFile(__FILE__);

require __DIR__ . '/../const.php';
require __DIR__ . '/functions.php';

CModule::IncludeModule('catalog');

class fermaofd_ferma extends CModule
{
    public $MODULE_ID = 'fermaofd.ferma';
    public $MODULE_VERSION;
    public $MODULE_VERSION_DATE;
    public $MODULE_NAME;
    public $MODULE_DESCRIPTION;
    public $MODULE_CSS;

    function __construct()
    {
        $arModuleVersion = [];

        $path = str_replace('\\', '/', __FILE__);
        $path = substr($path, 0, strlen($path) - strlen('/index.php'));
        include($path . '/version.php');
        if (is_array($arModuleVersion) && array_key_exists('VERSION', $arModuleVersion)) {
            $this->MODULE_VERSION = $arModuleVersion['VERSION'];
            $this->MODULE_VERSION_DATE = $arModuleVersion['VERSION_DATE'];
        }
        $this->MODULE_NAME = GetMessage('FERMAOFD_MODULE_NAME');
        $this->MODULE_DESCRIPTION = GetMessage('FERMAOFD_MODULE_DESCRIPTION');
        $this->PARTNER_NAME = GetMessage('FERMAOFD_PARTNER_NAME');
        $this->PARTNER_URI = 'https://ofd.ru/uslugi/ferma';
    }

    function GetModuleRightList()
    {
        // @todo
        return [
            'reference_id' => ['D', 'R'],
            'reference' => [
                '[D] ' . GetMessage('FERMAOFD_PERM_DENIED'),
                '[R] ' . GetMessage('FERMAOFD_PERM_VIEW'),
            ],
        ];
    }

    function DoInstall()
    {
        global $DOCUMENT_ROOT, $APPLICATION;

        $requirements = $this->checkRequirements();

        if (!empty($requirements)) {
            $GLOBALS["fermaofdRequirements"] = $requirements;

            $APPLICATION->IncludeAdminFile(GetMessage('FERMAOFD_INSTALL_TITLE'), $DOCUMENT_ROOT . '/bitrix/modules/' . FERMAOFD_FERMA_MODULE_ID . '/install/failed.php');

            return false;
        }

        $this->InstallDB();
        $this->insertMissedVatValues();
        $this->createPaymentTypeProperty();
        $this->InstallFiles();
        $this->registerModule();

        $APPLICATION->IncludeAdminFile(GetMessage('FERMAOFD_INSTALL_TITLE'), $DOCUMENT_ROOT . '/bitrix/modules/' . FERMAOFD_FERMA_MODULE_ID . '/install/step.php');

        return true;
    }

    function checkRequirements()
    {
        $result = [];

        if (!function_exists('curl_init')) {
            $result[] = 'curl';
        }

        return $result;
    }

    function DoUninstall()
    {
        global $DOCUMENT_ROOT, $APPLICATION, $step;

        $step = (int)$step;
        $step = $step < 1 ? 1 : $step;
        $step = $step > 2 ? 2 : $step;

        if ($step === 1) {
            $APPLICATION->includeAdminFile(
                GetMessage('FERMAOFD_UNINSTALL_TITLE'),
                $DOCUMENT_ROOT . '/bitrix/modules/' . FERMAOFD_FERMA_MODULE_ID . '/install/unstep1.php'
            );
        } elseif ($step === 2) {
            $this->UninstallDB(array('savedata' => $_REQUEST['savedata']));
            $this->UninstallFiles();
            $this->unregisterModule();
            $APPLICATION->includeAdminFile(
                GetMessage('FERMAOFD_UNINSTALL_TITLE'),
                $DOCUMENT_ROOT . '/bitrix/modules/' . FERMAOFD_FERMA_MODULE_ID . '/install/unstep2.php'
            );
        }

        return true;
    }


    private function registerModule()
    {
        // @deprecated ��������� �����
        UnRegisterModuleDependences('sale', 'OnSalePaymentEntitySaved', FERMAOFD_FERMA_MODULE_ID, 'CMainOfdFerma', 'onSalePayOrder');
        UnRegisterModuleDependences('sale', 'OnSalePaymentEntitySaved', FERMAOFD_FERMA_MODULE_ID, 'CMainOfdFerma', 'onSalePayCancelOrder');

        RegisterModuleDependences('sale', 'OnSaleOrderSaved', FERMAOFD_FERMA_MODULE_ID, 'CMainOfdFerma', 'createReceipt');
        RegisterModuleDependences('sale', 'OnOrderUpdate', FERMAOFD_FERMA_MODULE_ID, 'CMainOfdFerma', 'OnOrderUpdate');
        RegisterModuleDependences('sale', 'OnSaleDeductOrder', FERMAOFD_FERMA_MODULE_ID, 'CMainOfdFerma', 'onSaleDeductOrder');
        RegisterModuleDependences('main', 'OnAdminListDisplay', FERMAOFD_FERMA_MODULE_ID, 'CMainOfdFerma', 'onAdminListDisplay');
        RegisterModuleDependences('main', 'OnAdminContextMenuShow', FERMAOFD_FERMA_MODULE_ID, 'CMainOfdFerma', 'onAdminContextMenuShow');
        RegisterModule($this->MODULE_ID);
    }

    private function unregisterModule()
    {
        // @deprecated ��������� �����
        UnRegisterModuleDependences('sale', 'OnSalePaymentEntitySaved', FERMAOFD_FERMA_MODULE_ID, 'CMainOfdFerma', 'onSalePayOrder');
        UnRegisterModuleDependences('sale', 'OnSalePaymentEntitySaved', FERMAOFD_FERMA_MODULE_ID, 'CMainOfdFerma', 'onSalePayCancelOrder');

        UnRegisterModuleDependences('sale', 'OnSaleOrderSaved', FERMAOFD_FERMA_MODULE_ID, 'CMainOfdFerma', 'createReceipt');
        UnRegisterModuleDependences('sale', 'OnSaleDeductOrder', FERMAOFD_FERMA_MODULE_ID, 'CMainOfdFerma', 'onSaleDeductOrder');
        UnRegisterModuleDependences('main', 'OnAdminListDisplay', FERMAOFD_FERMA_MODULE_ID, 'CMainOfdFerma', 'onAdminListDisplay');
        UnRegisterModuleDependences('main', 'OnAdminContextMenuShow', FERMAOFD_FERMA_MODULE_ID, 'CMainOfdFerma', 'onAdminContextMenuShow');
        UnRegisterModule($this->MODULE_ID);
    }

    public function InstallDB()
    {
        global $DB, $DBType, $DOCUMENT_ROOT, $APPLICATION;

        if (!$DB->Query('SELECT \'x\' FROM b_fermaofd_ferma_receipt', true)) {
            $errors = $DB->RunSQLBatch($DOCUMENT_ROOT . '/bitrix/modules/' . FERMAOFD_FERMA_MODULE_ID . '/install/' . $DBType . '/install.sql');

            $this->migrateData();
        }

        if (!empty($errors)) {
            $APPLICATION->ThrowException(implode('', $errors));

            return false;
        }

        return true;
    }

    public function UninstallDB($arParams = [])
    {
        global $DB, $DBType, $DOCUMENT_ROOT, $APPLICATION;

        if (array_key_exists('savedata', $arParams) && $arParams['savedata'] !== 'Y') {

            $errors = $DB->RunSQLBatch($DOCUMENT_ROOT . '/bitrix/modules/' . FERMAOFD_FERMA_MODULE_ID . '/install/' . $DBType . '/uninstall.sql');

            if (!empty($errors)) {
                $APPLICATION->ThrowException(implode('', $errors));

                return false;
            }
        }

        return true;
    }

    public function InstallFiles()
    {
        global $DOCUMENT_ROOT;

        CopyDirFiles($DOCUMENT_ROOT . '/bitrix/modules/' . FERMAOFD_FERMA_MODULE_ID . '/install/admin', $DOCUMENT_ROOT . '/bitrix/admin', true);
        CopyDirFiles($DOCUMENT_ROOT . '/bitrix/modules/' . FERMAOFD_FERMA_MODULE_ID . '/install/js', $DOCUMENT_ROOT . '/bitrix/js/' . FERMAOFD_FERMA_MODULE_ID, true, true);
        CopyDirFiles($DOCUMENT_ROOT . '/bitrix/modules/' . FERMAOFD_FERMA_MODULE_ID . '/install/css', $DOCUMENT_ROOT . '/bitrix/css/' . FERMAOFD_FERMA_MODULE_ID, true, true);
    }

    public function UninstallFiles()
    {
        global $DOCUMENT_ROOT;

        DeleteDirFilesEx('/bitrix/js/' . FERMAOFD_FERMA_MODULE_ID . '/');
        DeleteDirFilesEx('/bitrix/css/' . FERMAOFD_FERMA_MODULE_ID . '/');
        DeleteDirFiles($DOCUMENT_ROOT . '/bitrix/modules/' . FERMAOFD_FERMA_MODULE_ID . '/install/admin', $DOCUMENT_ROOT . '/bitrix/admin');
    }

    private function insertMissedVatValues()
    {
        $vatList = CCatalogVat::GetList();

        $need = array(
            array(
                'rate' => '0.00',
                'name' => GetMessage('FERMAOFD_VAT_NO'),
                'code' => 'VatNo',
            ), array(
                'rate' => '0.00',
                'name' => GetMessage('FERMAOFD_VAT_0'),
                'code' => 'Vat0',
            ), array(
                'rate' => '10.00',
                'name' => GetMessage('FERMAOFD_VAT_10'),
                'code' => 'Vat10',
            ), array(
                'rate' => '20.00',
                'name' => GetMessage('FERMAOFD_VAT_20'),
                'code' => 'Vat20',
            ), array(
                'rate' => '11.00',
                'name' => GetMessage('FERMAOFD_VAT_CALCULATED_10110'),
                'code' => 'CalculatedVat10110',
            ), array(
                'rate' => '24.00',
                'name' => GetMessage('FERMAOFD_VAT_CALCULATED_20120'),
                'code' => 'CalculatedVat20120',
            ),
        );

        $exists = [];

        while ($vat = $vatList->NavNext()) {
            $exists[] = $vat['NAME'];
        }

        foreach ($need as $item) {
            if (in_array($item['name'], $exists, true)) {
                continue;
            }

            CCatalogVat::Add([
                'ACTIVE' => 'Y',
                'C_SORT' => 500,
                'NAME' => $item['name'],
                'RATE' => $item['rate'],
            ]);
        }
    }

    private function createPaymentTypeProperty()
    {
        $catalogs = ofdferma_getCatalogIBlocks();

        foreach ($catalogs as $catalog) {
            if ($catalog['PRODUCT_IBLOCK_ID'] !== '0' || ofdferma_getPaymentTypeProperty($catalog['IBLOCK_ID'])) {
                continue;
            }

            $propFactory = new CIBlockProperty();

            $propId = $propFactory->Add(array(
                'ACTIVE' => 'Y',
                'IS_REQUIRED' => 'N',
                'MULTIPLE' => 'N',
                'SEARCHABLE' => 'Y',
                'FILTRABLE' => 'Y',
                'IBLOCK_ID' => $catalog['IBLOCK_ID'],
                'NAME' => '������� �����',
                'CODE' => 'OFD_FERMA_PAYMENT_TYPE',
                'PROPERTY_TYPE' => 'L',
                'LIST_TYPE' => 'L',
            ));

            $enumItems = array_map(
                function ($item) {
                    return array(
                        'XML_ID' => $item['id'],
                        'SORT' => $item['id'] * 100,
                        'VALUE' => $item['name'],
                    );
                },
                array(
                    ['id' => '1', 'name' => GetMessage('FERMAOFD_PAYMENT_TYPE_PRODUCT')],
                    ['id' => '2', 'name' => GetMessage('FERMAOFD_PAYMENT_TYPE_EXCISE_PRODUCT')],
                    ['id' => '3', 'name' => GetMessage('FERMAOFD_PAYMENT_TYPE_WORK')],
                    ['id' => '4', 'name' => GetMessage('FERMAOFD_PAYMENT_TYPE_SERVICE')],
                    ['id' => '5', 'name' => GetMessage('FERMAOFD_PAYMENT_TYPE_GAME_BET')],
                    ['id' => '6', 'name' => GetMessage('FERMAOFD_PAYMENT_TYPE_WIN')],
                    ['id' => '7', 'name' => GetMessage('FERMAOFD_PAYMENT_TYPE_LOTTERY_BET')],
                    ['id' => '8', 'name' => GetMessage('FERMAOFD_PAYMENT_TYPE_LOTTERY_WIN')],
                    ['id' => '9', 'name' => GetMessage('FERMAOFD_PAYMENT_TYPE_RID')],
                    ['id' => '10', 'name' => GetMessage('FERMAOFD_PAYMENT_TYPE_PAYMENT')],
                    ['id' => '11', 'name' => GetMessage('FERMAOFD_PAYMENT_TYPE_AGENT_COMMISION')],
                    ['id' => '12', 'name' => GetMessage('FERMAOFD_PAYMENT_TYPE_COMPOSITE')],
                    ['id' => '13', 'name' => GetMessage('FERMAOFD_PAYMENT_TYPE_OTHER')],
                    ['id' => '14', 'name' => GetMessage('FERMAOFD_PAYMENT_TYPE_PROPERTY_LAW')],
                    ['id' => '15', 'name' => GetMessage('FERMAOFD_PAYMENT_TYPE_NON_OPERATING')],
                    ['id' => '16', 'name' => GetMessage('FERMAOFD_PAYMENT_TYPE_INSURANCE')],
                    ['id' => '17', 'name' => GetMessage('FERMAOFD_PAYMENT_TYPE_TRADE_FEE')],
                    ['id' => '18', 'name' => GetMessage('FERMAOFD_PAYMENT_TYPE_RESORT_FEE')],
                    ['id' => '19', 'name' => GetMessage('FERMAOFD_PAYMENT_TYPE_PLEDGE')],
                )
            );

            $propFactory->UpdateEnum($propId, $enumItems);
        }
    }

    private function migrateData()
    {
        global $DB;

        if (!$DB->Query('SELECT \'x\' FROM b_ofdferma_receipt', true)) {
            return;
        }

        $fields = [
            'ID',
            'RECEIPT_ID',
            'SITE_ID',
            'ORDER_ID',
            'TYPE',
            'CONTENT',
            'PAYMENT_METHOD_ID',
            'TOTAL_PRICE',
            'STATUS_ID',
            'STATUS_NAME',
            'STATUS_MESSAGE',
            'DEVICE_ID',
            'RNM',
            'ZN',
            'FN',
            'FDN',
            'FPD',
            'DATE_CREATE',
            'DATE_UPDATE',
//            'FINISHED',
        ];

        $fieldsStr = implode(',', $fields);

        $DB->Query(sprintf('INSERT INTO b_fermaofd_ferma_receipt (%s) SELECT %s FROM b_ofdferma_receipt', $fieldsStr, $fieldsStr), true);
    }
}