Your IP : 216.73.216.86


Current Path : /var/www/homesaver/www/bitrix/modules/fermaofd.ferma/classes/general/
Upload File :
Current File : /var/www/homesaver/www/bitrix/modules/fermaofd.ferma/classes/general/COfdFermaAgent.php

<?php

require_once __DIR__ . '/../../const.php';

use Bitrix\Main\Web\Json;
use Bitrix\Sale\Order;

CModule::IncludeModule(FERMAOFD_FERMA_MODULE_ID);

class COfdFermaAgent
{
    public static function sendReceiptAgent($orderId, $receiptData, $startedAt = null)
    {
        CModule::IncludeModule('sale');

        $receiptData = Json::decode($receiptData);

        try {
            $fermaApiClient = CMainOfdFerma::getApiService();
        } catch (\RuntimeException $e) {
            CMainOfdFerma::log(
                GetMessage('FERMAOFD_SENDING_RECEIPT'), GetMessage('FERMAOFD_ORDER_N') . $orderId,
                GetMessage('FERMAOFD_FAILED_SENDING_FERMA_SETTINGS'), 'WARNING'
            );

            return null;
        }

        $hasReceipt = COfdFermaReceipt::hasReceipt($orderId, $receiptData['Type']);

        if ($hasReceipt) {
            CMainOfdFerma::log(
                GetMessage('FERMAOFD_SENDING_RECEIPT'), GetMessage('FERMAOFD_ORDER_N') . $orderId,
                GetMessage('FERMAOFD_ALREADY_SENDING_RECEIPT'), 'WARNING'
            );

            return false;
        }

        try {
            COfdFermaAgent::sendReceiptData($orderId, $receiptData);
        } catch (\RuntimeException $e) {
            CMainOfdFerma::log(
                GetMessage('FERMAOFD_SENDING_RECEIPT'), GetMessage('FERMAOFD_ORDER_N') . $orderId,
                $e->getMessage(), 'WARNING'
            );

            // �������� ��� ��� � ����
            if ($e->getCode() === 1019) {
                return false;
            }

            if (empty($receiptData)) {
                CMainOfdFerma::log(
                    GetMessage('FERMAOFD_RECEIPT_MAKING_FAILED'), GetMessage('FERMAOFD_ORDER_N') . $orderId,
                    GetMessage('FERMAOFD_FAILED_RECEIPT_MAKING_UNEXPECTED'), 'ERROR'
                );

                return false;
            }

            $order = Order::load($orderId);

            if (!$order) {
                CMainOfdFerma::log(
                    GetMessage('FERMAOFD_SENDING_RECEIPT'), GetMessage('FERMAOFD_ORDER_N') . $orderId,
                    GetMessage('FERMAOFD_FAILED_SENDING_ORDER_NOT_FOUND'), 'WARNING'
                );

                return false;
            }

            if ($startedAt !== null) {
                $passedTime = time() - $startedAt;

                // ������������� ���������� ����� 24 ����
                if ($passedTime > 86400) {
                    CMainOfdFerma::log(
                        GetMessage('FERMAOFD_ATTEMPTS_SENDING_ARE_TERMINATED'), GetMessage('FERMAOFD_ORDER_N') . $orderId,
                        GetMessage('FERMAOFD_FAILED_SENDING_PERIODIC'), 'WARNING'
                    );

                    return false;
                }
            }

            $newReceiptData = $fermaApiClient->prepareElectronReceipt($receiptData['Type'], $order);

            return sprintf('COfdFermaAgent::sendReceiptAgent(%s, "%s", %d);', Json::encode($orderId), addslashes(Json::encode($newReceiptData)), $startedAt === null ? time() : $startedAt);
        }

        CMainOfdFerma::log(
            GetMessage('FERMAOFD_SENDING_RECEIPT'), GetMessage('FERMAOFD_ORDER_N') . $orderId,
            GetMessage('FERMAOFD_SUCCESS_SENDING'), 'INFO'
        );

        return false;
    }

    public static function checkReceiptStatusAgent($receiptId, $startedAt = null)
    {
        $receipt = COfdFermaReceipt::GetByID($receiptId);

        if (empty($receipt)) {
            CMainOfdFerma::log(
                GetMessage('FERMAOFD_RECEIPT_CHECKING'), GetMessage('FERMAOFD_RECEIPT_N') . $receiptId,
                GetMessage('FERMAOFD_FAILED_CHECKING_RECEIPT_NOT_FOUND'), 'WARNING'
            );

            return false;
        }

        try {
            $fermaApiClient = CMainOfdFerma::getApiService();
        } catch (\RuntimeException $e) {
            CMainOfdFerma::log(
                GetMessage('FERMAOFD_RECEIPT_CHECKING'), GetMessage('FERMAOFD_RECEIPT_N') . $receiptId,
                GetMessage('FERMAOFD_FAILED_CHECKING_FERMA_SETTINGS'), 'WARNING'
            );

            return false;
        }

        try {
            $response = $fermaApiClient->getElectronReceiptStatus($receipt['RECEIPT_ID']);
        } catch (CFermaApiException $e) {
            if ($startedAt !== null) {
                $passedTime = time() - $startedAt;

                // ������������� ���������� ����� 7 ����
                if ($passedTime > (86400 * 7)) {
                    CMainOfdFerma::log(
                        GetMessage('FERMAOFD_ATTEMPTS_CHECKING_ARE_TERMINATED'), GetMessage('FERMAOFD_RECEIPT_N') . $receiptId,
                        GetMessage('FERMAOFD_FAILED_CHECKING'), 'WARNING'
                    );

                    return false;
                }
            }

            return sprintf('COfdFermaAgent::checkReceiptStatusAgent(%s, %d);', Json::encode($receipt['ID']), $startedAt === null ? time() : $startedAt);
        }

        if ($response['Status'] === 'Failed') {
            if (isset($response['Error']['Message'])) {
                CMainOfdFerma::log(
                    GetMessage('FERMAOFD_RECEIPT_CHECKING'), GetMessage('FERMAOFD_RECEIPT_N') . $receiptId,
                    sprintf(GetMessage('FERMAOFD_FAILED_CHECKING') . ': %s.', $response['Error']['Message']), 'WARNING'
                );
            } else {
                CMainOfdFerma::log(
                    GetMessage('FERMAOFD_RECEIPT_CHECKING'), GetMessage('FERMAOFD_RECEIPT_N') . $receiptId,
                    sprintf(GetMessage('FERMAOFD_FAILED_CHECKING_UNEXPECTED')), 'WARNING'
                );
            }

            return false;
        }

        $data = $response['Data'];

        COfdFermaReceipt::Update($receipt['ID'], [
            'STATUS_ID' => $data['StatusCode'],
            'STATUS_NAME' => $data['StatusName'],
            'STATUS_MESSAGE' => $data['StatusMessage'],
            'DESCRIPTION' => $data['Description'],
            'DEVICE_ID' => $data['Device']['DeviceId'],
            'RNM' => $data['Device']['RNM'],
            'ZN' => $data['Device']['ZN'],
            'FN' => $data['Device']['FN'],
            'FDN' => $data['Device']['FDN'],
            'FPD' => $data['Device']['FPD'],
        ]);

        CMainOfdFerma::log(
            GetMessage('FERMAOFD_RECEIPT_CHECKING'), GetMessage('FERMAOFD_RECEIPT_N') . $receiptId,
            GetMessage('FERMAOFD_SUCCESS_CHECKING'), 'INFO'
        );

        if ($data['StatusCode'] < 2) {
            if ($startedAt !== null) {
                $passedTime = time() - $startedAt;

                // ������������� ���������� ����� 7 ����
                if ($passedTime > (86400 * 7)) {
                    CMainOfdFerma::log(
                        GetMessage('FERMAOFD_ATTEMPTS_CHECKING_ARE_TERMINATED'), GetMessage('FERMAOFD_RECEIPT_N') . $receiptId,
                        GetMessage('FERMAOFD_FAILED_CHECKING'), 'WARNING'
                    );

                    return false;
                }
            }


            return 'COfdFermaAgent::checkReceiptStatusAgent(' . Json::encode($receipt['ID']) . ');';
        }

        // ��������� false ��� ������� ������� �� ����, ���� ����� �� �������������
        return false;
    }

    public static function sendReceiptData($orderId, $receiptData)
    {
        try {
            $fermaApiClient = CMainOfdFerma::getApiService();
        } catch (\RuntimeException $e) {
            throw new \RuntimeException(GetMessage('FERMAOFD_FAILED_SENDING_FERMA_SETTINGS'));
        }

        $response = $fermaApiClient->sendElectronReceipt($orderId, $receiptData);

        if ($response['Status'] === 'Failed') {
            if (!isset($response['Error']['Message'])) {
                throw new \RuntimeException(GetMessage('FERMAOFD_FAILED_SENDING_UNEXPECTED'));
            }

            throw new \RuntimeException(sprintf(GetMessage('FERMAOFD_FAILED_SENDING') . ': %s.', $response['Error']['Message']), $response['Error']['Code']);
        }

        return true;
    }
}