Добавление обработчика в список заказов админки 1С-Битрикс
С помощью кнопки над списком можно выгрузить выделенные галочкой заказы
Добавление кнопки сюда при помощи JavaScript:
Кнопка в меню (с помощью данной кнопки выгружается один заказ):
Код в bitrix/php_include/init.php:
<?php
// ...
AddEventHandler('main', 'OnBeforeProlog', "dwOnBeforeProlog1C", 1);
function dwOnBeforeProlog1C()
{
if (preg_match('#/admin/sale_order.php#', $_SERVER['REQUEST_URI']))
{
Bitrix\Main\Page\Asset::getInstance()->addString(
"<script>
(function() {
let dw = {};
dw.init = function () {
dw.btn = document.createElement('a');
dw.btn.className = 'adm-btn';
dw.btn.innerHTML = 'Скачать JSON для 1С';
document.querySelector('#tbl_sale_order_result_div #btn_new').after(dw.btn);
dw.btn.addEventListener('click', function(event) {
dw.download();
});
}
dw.download = function () {
let checkbox = document.querySelectorAll('#tbl_sale_order_result_div .adm-list-table-row .adm-list-table-checkbox input'),
sel_id = [];
for (let i = 0; i < checkbox.length; i++) {
if (checkbox[i].checked) {
sel_id.push(checkbox[i].value);
}
}
if (sel_id.length > 0) {
let tmp = document.createElement('a');
tmp.href = '';
for (let i = 0; i < sel_id.length; i++) {
if (tmp.href == '') {
tmp.href += '/?dw-order-json-1c[]=';
} else {
tmp.href += '&dw-order-json-1c[]=';
}
tmp.href += sel_id[i];
}
document.body.appendChild(tmp);
tmp.click();
} else {
alert('Не выбрано ни одного заказа');
}
}
document.addEventListener('DOMContentLoaded', function(event) {
dw.init();
});
})();
</script>",
true
);
}
if (isset($_REQUEST['dw-order-json-1c']) && $GLOBALS['USER']->IsAdmin())
{
if (!is_array($_REQUEST['dw-order-json-1c']))
{
$ar_order = [$_REQUEST['dw-order-json-1c']];
}
else
{
$ar_order = $_REQUEST['dw-order-json-1c'];
}
if (count($ar_order) == 1)
{
$filename = $ar_order[0];
}
else
{
$filename = uniqid();
}
header('Content-Type: application/json; charset=UTF-8');
header('Content-Disposition: attachment; filename="order-for-1c-' . $filename . '.json"');
$save_data = [
'version' => '1.0',
'data' => [],
];
foreach ($ar_order as $order_id)
{
$order_id = intval($order_id);
if ($order_id > 0)
{
Bitrix\Main\Loader::includeModule("sale");
$order = Bitrix\Sale\Order::load($order_id);
if (is_object($order->getField("DATE_PAYED")))
{
$ex = explode(" ", $order->getField("DATE_PAYED")->toString());
$ex1 = explode(".", $ex[0]);
$ex2 = explode(":", $ex[1]);
$date_payed = $ex1[2] . $ex1[1] . $ex1[0] . $ex2[0] . $ex2[1] . $ex2[2];
}
else
{
$datePayed = null;
}
$ex = explode(" ", $order->getField("DATE_INSERT")->toString());
$ex1 = explode(".", $ex[0]);
$ex2 = explode(":", $ex[1]);
$date_insert = $ex1[2] . $ex1[1] . $ex1[0] . $ex2[0] . $ex2[1] . $ex2[2];
$ex = explode(" ", $order->getField("DATE_UPDATE")->toString());
$ex1 = explode(".", $ex[0]);
$ex2 = explode(":", $ex[1]);
$date_update = $ex1[2] . $ex1[1] . $ex1[0] . $ex2[0] . $ex2[1] . $ex2[2];
$doc = [
"id" => $order_id,
"date" => $date_insert,
"date_payed" => $date_payed,
"payed" => $order->getField("PAYED") == "Y" ? true : false,
"price" => $order->getField("PRICE"),
"currency" => $order->getField("CURRENCY"),
"comments" => $order->getField("COMMENTS"),
"date_insert" => $date_insert,
"date_update" => $date_update,
"total" => 0,
"items" => [],
];
$orderBasket = $order->getBasket();
$basketItems = $orderBasket->getBasketItems();
foreach ($basketItems as $k => $v)
{
$el_res = CIBlockElement::GetList([], [
'ID' => $v->getProductId(),
], false, false, ["ID", 'NAME', 'EXTERNAL_ID']);
$el = $el_res->Fetch();
$basket_item = [
"guid" => $el['EXTERNAL_ID'],
"name" => $v->getField("NAME"),
"price" => $v->getPrice(),
"quantity" => $v->getQuantity(),
"final_price" => $v->getFinalPrice(),
];
$basket_item['sum'] = $basket_item["price"] * $basket_item["quantity"];
$get_product = CCatalogProduct::GetByID($v->getProductId());
if (isset($get_product['VAT_ID']) && $get_product['VAT_ID'] == 2)
{
$basket_item['vat_type'] = 'НДС10';
$basket_item['vat_sum'] = round($basket_item['sum'] * 0.083333333333333333333333, 2);
}
else
{
$basket_item['vat_type'] = 'НДС20';
$basket_item['vat_sum'] = round($basket_item['sum'] * 0.166666666666666666666666, 2);
}
$doc['total'] = $doc['total'] + $basket_item['sum'];
$doc['items'][] = $basket_item;
}
$shipmentCollection = $order->getShipmentCollection();
foreach ($shipmentCollection as $shipment) {
if ($shipment->getPrice() > 0) {
$basket_item = [
"guid" => '59dfd9d8-5b35-11eb-5a84-fa163ec2df79',
"name" => $shipment->getField('DELIVERY_NAME'),
"price" => round($shipment->getPrice(), 2),
"quantity" => 1,
"final_price" => round($shipment->getPrice(), 2),
];
$basket_item['sum'] = $basket_item["price"] * $basket_item["quantity"];
$basket_item['vat_type'] = 'НДС20';
$basket_item['vat_sum'] = round($basket_item['sum'] * 0.166666666666666666666666, 2);
$doc['total'] = $doc['total'] + $basket_item['sum'];
$doc['items'][] = $basket_item;
break;
}
}
$save_data['data'][] = $doc;
}
}
if (count($save_data['data']))
{
print json_encode($save_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
die();
}
}
}
AddEventHandler('main', 'OnAdminListDisplay', 'dwOnAdminListDisplay1C');
function dwOnAdminListDisplay1C(&$list)
{
if ($list->table_id == 'tbl_sale_order') {
foreach ($list->aRows as &$row)
{
$row->aActions[] = ['SEPARATOR' => true];
$row->aActions[] = [
'ICON' => 'edit',
'TEXT' => 'Скачать JSON для 1С',
'LINK' => '/?dw-order-json-1c=' . $row->id,
];
}
}
}