5use \Bitrix\Main\Config;
31 return "b_sale_product2product";
38 'data_type' =>
'integer',
40 'autocomplete' =>
true,
42 'PRODUCT_ID' => array(
43 'data_type' =>
'integer'
45 'PARENT_PRODUCT_ID' => array(
46 'data_type' =>
'integer'
49 'data_type' =>
'integer'
65 $liveTime = (int)$liveTime;
66 $connection = Main\Application::getConnection();
67 $type = $connection->getType();
68 $helper = $connection->getSqlHelper();
69 $liveTo = $helper->addSecondsToDateTime($liveTime * 24 * 3600,
"o.DATE_INSERT");
70 $now = $helper->getCurrentDateTimeFunction();
76 if ($connection->isTableExists(
'b_sale_order_product_stat'))
78 $liveTo = $helper->addSecondsToDateTime($liveTime * 24 * 3600,
"ORDER_DATE");
79 $sqlDelete =
"DELETE FROM b_sale_order_product_stat WHERE $now > $liveTo";
80 $connection->query($sqlDelete);
81 $connection->query(
"TRUNCATE TABLE b_sale_product2product");
82 $sqlUpdate =
"INSERT INTO b_sale_product2product(PRODUCT_ID, PARENT_PRODUCT_ID, CNT)
83 SELECT ops.PRODUCT_ID, ops.RELATED_PRODUCT_ID, SUM(ops.CNT)
84 FROM b_sale_order_product_stat ops
85 GROUP BY PRODUCT_ID, RELATED_PRODUCT_ID
87 $connection->query($sqlUpdate);
92 $sqlUpdate =
"UPDATE b_sale_product2product
94 FROM b_sale_product2product p2p, b_sale_basket b, b_sale_basket b1, b_sale_order o, b_sale_order_processing op
95 WHERE b.ORDER_ID = b1.ORDER_ID AND
99 o.ID = op.ORDER_ID AND
100 op.PRODUCTS_REMOVED = 'N' AND
101 p2p.PRODUCT_ID = b.PRODUCT_ID AND
102 p2p.PARENT_PRODUCT_ID = b1.PRODUCT_ID";
103 $connection->query($sqlUpdate);
107 $sqlUpdate =
"UPDATE b_sale_product2product
110 SELECT p2p.ID FROM b_sale_product2product p2p, b_sale_basket b, b_sale_basket b1, b_sale_order o, b_sale_order_processing op
111 WHERE b.ORDER_ID = b1.ORDER_ID AND
114 o.ID = b.ORDER_ID AND
115 o.ID = op.ORDER_ID AND
116 op.PRODUCTS_REMOVED = 'N' AND
117 p2p.PRODUCT_ID = b.PRODUCT_ID AND
118 p2p.PARENT_PRODUCT_ID = b1.PRODUCT_ID
120 $connection->query($sqlUpdate);
128 $updateRemStatusSql =
"UPDATE b_sale_order_processing SET PRODUCTS_REMOVED = 'Y'";
129 $connection->query($updateRemStatusSql);
131 if ($type !==
"mysql")
134 $deleteSql =
"DELETE FROM b_sale_product2product WHERE CNT <= 0";
135 $connection->query($deleteSql);
138 return "\\Bitrix\\Sale\\Product2ProductTable::deleteOldProducts(".$liveTime.
");";
150 $liveTime = (int)$liveTime;
151 $connection = Main\Application::getConnection();
153 if (!$connection->isTableExists(
'b_sale_order_product_stat'))
156 $sqlDelete =
"TRUNCATE TABLE b_sale_order_product_stat";
157 $connection->query($sqlDelete);
161 $helper = $connection->getSqlHelper();
162 $liveTo = $helper->addSecondsToDateTime($liveTime * 24 * 3600,
"b.DATE_INSERT");
163 $dateLimit =
" AND NOW() < $liveTo";
166 INSERT INTO b_sale_order_product_stat (PRODUCT_ID, RELATED_PRODUCT_ID, ORDER_DATE, CNT)
167 SELECT b.PRODUCT_ID as PRODUCT_ID, b1.PRODUCT_ID as RELATED_PRODUCT_ID, DATE(b.DATE_INSERT) as ORDER_DATE, COUNT(b.PRODUCT_ID)
168 FROM b_sale_basket b, b_sale_basket b1
169 WHERE b.ORDER_ID = b1.ORDER_ID
172 GROUP BY b.PRODUCT_ID, b1.PRODUCT_ID, ORDER_DATE
174 $connection->query($sqlUpdate);
176 $sqlDelete =
"TRUNCATE TABLE b_sale_product2product";
177 $connection->query($sqlDelete);
179 INSERT INTO b_sale_product2product (PRODUCT_ID, PARENT_PRODUCT_ID, CNT)
180 SELECT ops.PRODUCT_ID, ops.RELATED_PRODUCT_ID, SUM(ops.CNT)
181 FROM b_sale_order_product_stat ops
182 GROUP BY PRODUCT_ID, RELATED_PRODUCT_ID
186 return $connection->query($sqlUpdate);
198 $orderId = (int)$orderId;
203 $connection = Main\Application::getConnection();
204 $type = $connection->getType();
207 if ($type ==
"mysql" && $connection->isTableExists(
'b_sale_order_product_stat'))
210 INSERT INTO b_sale_order_product_stat (PRODUCT_ID, RELATED_PRODUCT_ID, ORDER_DATE)
211 SELECT b.PRODUCT_ID, b1.PRODUCT_ID, DATE(b.DATE_INSERT)
212 FROM b_sale_basket b, b_sale_basket b1
213 WHERE b.ORDER_ID = b1.ORDER_ID AND
214 b.ORDER_ID = $orderId AND
216 ON DUPLICATE KEY UPDATE CNT = CNT + 1;
218 $connection->query($sqlUpdate);
220 $sqlUpdate =
"UPDATE b_sale_product2product p2p, b_sale_basket b, b_sale_basket b1
221 SET p2p.CNT = p2p.CNT + 1
222 WHERE b.ORDER_ID = b1.ORDER_ID AND
224 b.ORDER_ID = $orderId AND
225 p2p.PRODUCT_ID = b.PRODUCT_ID AND
226 p2p.PARENT_PRODUCT_ID = b1.PRODUCT_ID";
228 elseif ($type ==
"mssql")
230 $sqlUpdate =
"UPDATE b_sale_product2product
232 FROM b_sale_product2product p2p, b_sale_basket b, b_sale_basket b1
233 WHERE b.ORDER_ID = b1.ORDER_ID AND
235 b.ORDER_ID = $orderId AND
236 p2p.PRODUCT_ID = b.PRODUCT_ID AND
237 p2p.PARENT_PRODUCT_ID = b1.PRODUCT_ID";
241 $sqlUpdate =
"UPDATE b_sale_product2product
244 SELECT p2p.ID FROM b_sale_product2product p2p, b_sale_basket b, b_sale_basket b1
245 WHERE b.ORDER_ID = b1.ORDER_ID AND
247 b.ORDER_ID = $orderId AND
248 p2p.PRODUCT_ID = b.PRODUCT_ID AND
249 p2p.PARENT_PRODUCT_ID = b1.PRODUCT_ID
253 $connection->query($sqlUpdate);
256 $sqlInsert =
"INSERT INTO b_sale_product2product (PRODUCT_ID, PARENT_PRODUCT_ID, CNT)
257 SELECT b.PRODUCT_ID, b1.PRODUCT_ID, 1
258 FROM b_sale_basket b, b_sale_basket b1
259 WHERE b.ORDER_ID = b1.ORDER_ID AND
260 b.ORDER_ID = $orderId AND
262 NOT EXISTS (SELECT 1 FROM b_sale_product2product d WHERE d.PRODUCT_ID = b.PRODUCT_ID AND d.PARENT_PRODUCT_ID = b1.PRODUCT_ID)";
264 $connection->query($sqlInsert);
266 Sale\OrderProcessingTable::markProductsAdded($orderId);
268 if (defined(
"BX_COMP_MANAGED_CACHE"))
270 $app = Main\Application::getInstance();
271 $app->getTaggedCache()->clearByTag(
'sale_product_buy');
283 $limit = (int)$limit;
284 $connection = Main\Application::getConnection();
285 $type = $connection->getType();
286 $isTableExists = $connection->isTableExists(
'b_sale_order_product_stat');
287 if ($type ==
"mysql" && $isTableExists)
290 "filter" => array(
"PRODUCTS_ADDED" =>
'N'),
291 "select" => array(
"ORDER_ID")
296 $params[
'limit'] = $limit;
300 $processingData = Sale\OrderProcessingTable::getList($params);
301 while ($processingOrder = $processingData->fetch())
303 $orderIds[] = (int)$processingOrder[
'ORDER_ID'];
306 if (!empty($orderIds))
308 $sqlOrderIds = implode(
',', $orderIds);
309 Sale\OrderProcessingTable::markProductsAddedByList($orderIds);
312 INSERT INTO b_sale_order_product_stat (CNT, PRODUCT_ID, RELATED_PRODUCT_ID, ORDER_DATE)
313 SELECT SUMM, PRODUCT_ID, PARENT_PRODUCT_ID, TODAY
315 SELECT COUNT(1) as SUMM,
316 b.PRODUCT_ID as PRODUCT_ID,
317 b1.PRODUCT_ID as PARENT_PRODUCT_ID,
319 FROM b_sale_basket b, b_sale_basket b1
321 b1.ORDER_ID = b.ORDER_ID
323 AND b.ORDER_ID IN ($sqlOrderIds)
324 GROUP BY b.PRODUCT_ID, b1.PRODUCT_ID
327 ON DUPLICATE KEY UPDATE CNT = CNT + cacl.SUMM;";
328 $connection->query($sqlInsert);
331 UPDATE b_sale_product2product p2p,
333 SELECT COUNT(1) as CNT,
334 b.PRODUCT_ID as PRODUCT_ID,
335 b1.PRODUCT_ID as PARENT_PRODUCT_ID
336 FROM b_sale_basket b, b_sale_basket b1
338 b1.ORDER_ID = b.ORDER_ID
340 AND b.ORDER_ID IN ($sqlOrderIds)
341 GROUP BY b.PRODUCT_ID, b1.PRODUCT_ID
344 SET p2p.CNT = p2p.CNT + calc.CNT
345 WHERE p2p.PRODUCT_ID = calc.PRODUCT_ID AND p2p.PARENT_PRODUCT_ID = calc.PARENT_PRODUCT_ID";
347 $connection->query($sqlUpdate);
350 INSERT INTO b_sale_product2product (PRODUCT_ID, PARENT_PRODUCT_ID, CNT)
351 SELECT b.PRODUCT_ID, b1.PRODUCT_ID, 1
352 FROM b_sale_basket b, b_sale_basket b1
353 WHERE b.ORDER_ID = b1.ORDER_ID AND
354 b.ORDER_ID IN ($sqlOrderIds) AND
356 NOT EXISTS (SELECT 1 FROM b_sale_product2product d WHERE d.PRODUCT_ID = b.PRODUCT_ID AND d.PARENT_PRODUCT_ID = b1.PRODUCT_ID)";
357 $connection->query($sqlInsert);
359 if (defined(
"BX_COMP_MANAGED_CACHE"))
361 $app = Main\Application::getInstance();
362 $app->getTaggedCache()->clearByTag(
'sale_product_buy');
367 $agentName =
"\\Bitrix\\Sale\\Product2ProductTable::addProductsByAgent($limit);";
368 $agentData = \CAgent::GetList(array(), array(
"NAME" => $agentName,
"MODULE_ID" =>
"sale"));
369 $agent = $agentData->Fetch();
371 $processingData = Sale\OrderProcessingTable::getList(
373 "filter" => array(
"PRODUCTS_ADDED" =>
'N')
377 if ($processingData->fetch())
379 if ($isTableExists && $agent[
'ID'] && $agent[
'ID'] > 60)
381 \CAgent::Delete($agent[
"ID"]);
382 \CAgent::AddAgent(
"Bitrix\\Sale\\Product2ProductTable::addProductsByAgent($limit);",
"sale",
"N", 60,
"",
"Y");
389 \CAgent::Update($agent[
"ID"], array(
"AGENT_INTERVAL" => 60*60*24));
403 $order = $event->getParameter(
'ENTITY');
404 $isNew = $event->getParameter(
'IS_NEW');
408 Main\EventResult::ERROR,
409 new Sale\
ResultError(Main\Localization\
Loc::getMessage(
'SALE_EVENT_PRODUCT2PRODUCT_WRONG_ORDER'),
'SALE_EVENT_PRODUCT2PRODUCT_ON_SALE_ORDER_ADD_WRONG_ORDER'),
414 $basket = $order->getBasket();
416 if ($isNew && ($basket && count($basket) > 0))
418 static::onSaleOrderAdd($order->getId());
431 $order = $event->getParameter(
'ENTITY');
432 $value = $event->getParameter(
'VALUE');
436 Main\EventResult::ERROR,
437 new Sale\
ResultError(Main\Localization\
Loc::getMessage(
'SALE_EVENT_PRODUCT2PRODUCT_WRONG_ORDER'),
'SALE_EVENT_PRODUCT2PRODUCT_ON_SALE_ORDER_STATUS_WRONG_ORDER'),
442 static::onSaleStatusOrderHandler($order->getId(), $value);
454 $shipment = $event->getParameter(
'ENTITY');
458 Main\EventResult::ERROR,
459 new Sale\
ResultError(Main\Localization\
Loc::getMessage(
'SALE_EVENT_PRODUCT2PRODUCT_WRONG_SHIPMENT'),
'SALE_EVENT_PRODUCT2PRODUCT_ON_SALE_DELIVERY_ORDER_WRONG_SHIPMENT'),
464 if (!$shipmentCollection = $shipment->getCollection())
467 Main\EventResult::ERROR,
468 new Sale\
ResultError(Main\Localization\
Loc::getMessage(
'SALE_EVENT_PRODUCT2PRODUCT_WRONG_SHIPMENTCOLLECTION'),
'SALE_EVENT_PRODUCT2PRODUCT_ON_SALE_DELIVERY_ORDER_WRONG_SHIPMENTCOLLECTION'),
474 if (!$order = $shipmentCollection->getOrder())
477 Main\EventResult::ERROR,
478 new Sale\
ResultError(Main\Localization\
Loc::getMessage(
'SALE_EVENT_PRODUCT2PRODUCT_WRONG_ORDER'),
'SALE_EVENT_PRODUCT2PRODUCT_ON_SALE_DELIVERY_ORDER_WRONG_ORDER'),
484 static::onSaleDeliveryOrderHandler($order->getId(), $order->isAllowDelivery() ?
'Y' :
'N');
496 $shipment = $event->getParameter(
'ENTITY');
500 Main\EventResult::ERROR,
501 new Sale\
ResultError(Main\Localization\
Loc::getMessage(
'SALE_EVENT_PRODUCT2PRODUCT_WRONG_SHIPMENT'),
'SALE_EVENT_PRODUCT2PRODUCT_ON_SALE_DEDUCT_ORDER_WRONG_SHIPMENT'),
506 if (!$shipmentCollection = $shipment->getCollection())
509 Main\EventResult::ERROR,
510 new Sale\
ResultError(Main\Localization\
Loc::getMessage(
'SALE_EVENT_PRODUCT2PRODUCT_WRONG_SHIPMENTCOLLECTION'),
'SALE_EVENT_PRODUCT2PRODUCT_ON_SALE_DEDUCT_ORDER_WRONG_SHIPMENTCOLLECTION'),
516 if (!$order = $shipmentCollection->getOrder())
519 Main\EventResult::ERROR,
520 new Sale\
ResultError(Main\Localization\
Loc::getMessage(
'SALE_EVENT_PRODUCT2PRODUCT_WRONG_ORDER'),
'SALE_EVENT_PRODUCT2PRODUCT_ON_SALE_DEDUCT_ORDER_WRONG_ORDER'),
527 static::onSaleDeductOrderHandler($order->getId(), $order->isShipped() ?
'Y' :
'N');
539 $order = $event->getParameter(
'ENTITY');
543 Main\EventResult::ERROR,
544 new Sale\
ResultError(Main\Localization\
Loc::getMessage(
'SALE_EVENT_PRODUCT2PRODUCT_WRONG_ORDER'),
'SALE_EVENT_PRODUCT2PRODUCT_ON_SALE_CANCELED_ORDER_WRONG_ORDER'),
549 static::onSaleCancelOrderHandler($order->getId(), $order->isCanceled() ?
'Y' :
'N');
561 $order = $event->getParameter(
'ENTITY');
565 Main\EventResult::ERROR,
566 new Sale\
ResultError(Main\Localization\
Loc::getMessage(
'SALE_EVENT_PRODUCT2PRODUCT_WRONG_ORDER'),
'SALE_EVENT_PRODUCT2PRODUCT_ON_SALE_PAID_ORDER_WRONG_ORDER'),
571 static::onSaleCancelOrderHandler($order->getId(), $order->isPaid() ?
'Y' :
'N');
585 static::addOrderProcessing($orderId, $statusName);
597 static::addOrderProcessing($orderId, $status);
611 $statusName =
"F_DELIVERY";
612 static::addOrderProcessing($orderId, $statusName);
627 $statusName =
"F_OUT";
628 static::addOrderProcessing($orderId, $statusName);
643 $statusName =
"F_CANCELED";
644 static::addOrderProcessing($orderId, $statusName);
659 $statusName =
"F_PAY";
660 static::addOrderProcessing($orderId, $statusName);
673 $allowStatuses = Config\Option::get(
"sale",
"p2p_status_list",
"");
674 $allowCollecting = Config\Option::get(
"sale",
"p2p_allow_collect_data");
675 if ($allowStatuses !=
'')
676 $allowStatuses = unserialize($allowStatuses, [
'allowed_classes' =>
false]);
678 $allowStatuses = array();
680 if ($allowCollecting ==
"Y" && !empty($allowStatuses) && is_array($allowStatuses) && in_array($statusName, $allowStatuses))
682 $orderInformation = Sale\OrderProcessingTable::getList(
684 "filter" => array(
"ORDER_ID" => (
int)$orderId),
688 $result = $orderInformation->fetch();
690 Sale\OrderProcessingTable::add(array(
"ORDER_ID" => (
int)$orderId));
static loadMessages($file)
static getMessage($code, $replace=null, $language=null)
static hasAddedProducts($orderId=0)
static onSaleStatusOrderHandler($orderId, $status)
static onSaleDeductOrderHandlerEvent(Main\Event $event)
static addProductsByAgent($limit=100)
static onSaleDeductOrderHandler($orderId, $status)
static onSaleStatusOrderHandlerEvent(Main\Event $event)
static onSaleOrderAdd($orderId)
static onSaleCancelOrderHandlerEvent(Main\Event $event)
static refreshProductStatistic($liveTime=10)
static addOrderProcessing($orderId, $statusName)
static deleteOldProducts($liveTime=10)
static addProductsFromOrder($orderId=0)
static onSaleOrderAddEvent(Main\Event $event)
static onSaleDeliveryOrderHandler($orderId, $status)
static onSaleDeliveryOrderHandlerEvent(Main\Event $event)
static onSaleCancelOrderHandler($orderId, $status)
static onSalePayOrderHandlerEvent(Main\Event $event)
static onSalePayOrderHandler($orderId, $status)