| Current Path : /var/www/homesaver/www/bitrix/modules/fermaofd.ferma/classes/general/ |
| Current File : /var/www/homesaver/www/bitrix/modules/fermaofd.ferma/classes/general/CMainOfdFerma.php |
<?php
require_once __DIR__ . '/../../const.php';
use Bitrix\Main\Page\Asset;
use Bitrix\Main\Web\Json;
use Bitrix\Sale\Order;
IncludeModuleLangFile(__DIR__ . '/other.php');
CModule::IncludeModule(FERMAOFD_FERMA_MODULE_ID);
class CMainOfdFerma
{
/**
* @param \CAdminSaleList|\CAdminList $list
*/
public static function onAdminListDisplay($list)
{
/** @var \CMain $APPLICATION */
global $APPLICATION;
if ($APPLICATION->GetCurPage() === '/bitrix/admin/sale_order.php') {
Asset::getInstance()->addJs('/bitrix/js/' . FERMAOFD_FERMA_MODULE_ID . '/ofdferma_receipts.js');
foreach ($list->aRows as $row) {
$actions = $row->aActions;
$newActions = [
[
'ICON' => 'edit',
'TEXT' => GetMessage('FERMAOFD_NEW_INCOME_RECEIPT'),
'ACTION' => 'window.ofdferma.openOrderReceiptPanel("' . $row->id . '");',
], [
'ICON' => 'edit',
'TEXT' => GetMessage('FERMAOFD_NEW_OUTCOME_RECEIPT'),
'ACTION' => 'window.ofdferma.openOrderRefundPanel("' . $row->id . '");',
],
];
array_splice($actions, -2, 0, $newActions);
$row->AddActions($actions);
}
}
}
public static function onAdminContextMenuShow(&$items)
{
/** @var \CMain $APPLICATION */
global $APPLICATION;
if ($APPLICATION->GetCurPage() === '/bitrix/admin/sale_order_view.php') {
Asset::getInstance()->addJs('/bitrix/js/' . FERMAOFD_FERMA_MODULE_ID . '/ofdferma_receipts.js');
$orderId = (int)$_GET['ID'];
$newItems = [
[
'TEXT' => GetMessage('FERMAOFD_NEW_RECEIPT'),
'TITLE' => GetMessage('FERMAOFD_NEW_INCOME_RECEIPT'),
'MENU' => [
[
'TEXT' => GetMessage('FERMAOFD_INCOME_RECEIPT'),
'TITLE' => GetMessage('FERMAOFD_NEW_INCOME_RECEIPT'),
'ACTION' => 'window.ofdferma.openOrderReceiptPanel("' . $orderId . '");',
], [
'TEXT' => GetMessage('FERMAOFD_OUTCOME_RECEIPT'),
'TITLE' => GetMessage('FERMAOFD_NEW_OUTCOME_RECEIPT'),
'ACTION' => 'window.ofdferma.openOrderRefundPanel("' . $orderId . '");',
],
],
],
];
array_splice($items, -2, 0, $newItems);
}
}
public static function createReceipt(Order $order, $oldValues)
{
CModule::IncludeModule('sale');
$enabled = COption::GetOptionString(FERMAOFD_FERMA_MODULE_ID, 'agent_auto_create', 'Y');
if ($enabled !== 'Y') {
return null;
}
$siteId = $order->getSiteId();
$siteEnabled = COption::GetOptionString(FERMAOFD_FERMA_MODULE_ID, sprintf('site_enabled_%s', $siteId), 'Y');
if ($siteEnabled !== 'Y') {
return null;
}
$prepaymentStatus = COption::GetOptionString(FERMAOFD_FERMA_MODULE_ID, 'receipt_prepayment', 'null');
$paymentStatus = COption::GetOptionString(FERMAOFD_FERMA_MODULE_ID, 'receipt_payment', 'P');
$refundStatus = COption::GetOptionString(FERMAOFD_FERMA_MODULE_ID, 'receipt_refund', 'null');
$orderId = $order->getId();
$orderStatusId = $order->getField('STATUS_ID');
$isNeedPrepaymentReceipt = $prepaymentStatus !== 'null' && $orderStatusId === $prepaymentStatus;
$isNeedPaymentReceipt = $paymentStatus !== 'null' && $orderStatusId === $paymentStatus;
$isNeedRefundReceipt = $refundStatus !== 'null' && $orderStatusId === $refundStatus;
try {
$fermaApi = 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;
}
if ($isNeedPrepaymentReceipt) {
$hasPrepaymentReceipt = COfdFermaReceipt::hasReceipt($orderId, 'IncomePrepayment');
if (!$hasPrepaymentReceipt) {
static::sendReceipt('IncomePrepayment', $fermaApi, $order);
}
}
if ($isNeedPaymentReceipt) {
$hasPaymentReceipt = COfdFermaReceipt::hasReceipt($orderId, 'Income');
if (!$hasPaymentReceipt) {
static::sendReceipt('Income', $fermaApi, $order);
}
}
if ($isNeedRefundReceipt) {
$hasPaymentReceipt = COfdFermaReceipt::getReceiptCount($orderId, 'Income') > 0;
$hasPrepaymentReceipt = COfdFermaReceipt::hasReceipt($orderId, 'IncomePrepayment');
$hasRefundReceipt = COfdFermaReceipt::hasReceipt($orderId, 'IncomeReturn');
// ���� �� ��� � ���� ��� �����, �� ������ ����� ������� �������
if ($hasPaymentReceipt) {
if (!$hasRefundReceipt) {
static::sendReceipt('IncomeReturn', $fermaApi, $order);
}
} elseif ($hasPrepaymentReceipt) {
// ���� �� ��� ���� �����, �� �� ��� ���������, �� ������ ������� ���������
static::sendReceipt('IncomeReturnPrepayment', $fermaApi, $order);
}
}
return true;
}
private static function sendReceipt(string $type, CFermaApiService $fermaApi, Order $order)
{
$receiptData = $fermaApi->prepareElectronReceipt($type, $order);
if (empty($receiptData)) {
CMainOfdFerma::log(
GetMessage('FERMAOFD_FAILED_RECEIPT_MAKING'), GetMessage('FERMAOFD_ORDER_N') . $order->getId(),
GetMessage('FERMAOFD_FAILED_RECEIPT_MAKING_UNEXPECTED'), 'ERROR'
);
return null;
}
CAgent::AddAgent(
sprintf('COfdFermaAgent::sendReceiptAgent(%s, "%s", %d);', Json::encode($order->getId()), addslashes(Json::encode($receiptData)), time()),
FERMAOFD_FERMA_MODULE_ID,
'Y', 30
);
}
/**
* @var \CFermaApiService
*/
private static $service;
public static function getApiService()
{
if (null === CMainOfdFerma::$service) {
$fermaApiHost = COption::GetOptionString(FERMAOFD_FERMA_MODULE_ID, 'api_host', 'https://ferma.ofd.ru');
$fermaApiLogin = COption::GetOptionString(FERMAOFD_FERMA_MODULE_ID, 'api_login');
$fermaApiPassword = COption::GetOptionString(FERMAOFD_FERMA_MODULE_ID, 'api_password');
$agentInn = COption::GetOptionString(FERMAOFD_FERMA_MODULE_ID, 'agent_inn');
$agentTaxation = COption::GetOptionString(FERMAOFD_FERMA_MODULE_ID, 'agent_taxation_system');
$errorsEmail = COption::GetOptionString(FERMAOFD_FERMA_MODULE_ID, 'errors_email');
if (empty($fermaApiHost)
|| empty($fermaApiLogin)
|| empty($fermaApiPassword)
|| empty($agentInn)
|| empty($agentTaxation)
) {
throw new \RuntimeException(GetMessage('FERMAOFD_EMPTY_SETTINGS'));
}
CMainOfdFerma::$service = CFermaApiService::instance(
$fermaApiHost, $fermaApiLogin, $fermaApiPassword,
$agentInn, $agentTaxation, $errorsEmail
);
}
return CMainOfdFerma::$service;
}
public static function log($operation, $entity, $message, $level)
{
if (class_exists('CEventLog')) {
CEventLog::Add([
'SEVERITY' => $level,
'AUDIT_TYPE_ID' => $operation,
'MODULE_ID' => FERMAOFD_FERMA_MODULE_ID,
'ITEM_ID' => $entity,
'DESCRIPTION' => $message,
]);
}
}
}