Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
buyerstatistic.php
1<?php
9
10use Bitrix\Main;
11
28class BuyerStatisticTable extends Main\Entity\DataManager
29{
35 public static function getTableName()
36 {
37 return 'b_sale_buyer_stat';
38 }
39
45 public static function getMap()
46 {
47 return array(
48 new Main\Entity\IntegerField(
49 'ID',
50 array(
51 'autocomplete' => true,
52 'primary' => true,
53 )
54 ),
55
56 new Main\Entity\StringField(
57 'LID',
58 array(
59 'required' => true,
60 )
61 ),
62
63 new Main\Entity\StringField(
64 'CURRENCY',
65 array(
66 'required' => true,
67 'size' => 3
68 )
69 ),
70
71 new Main\Entity\StringField(
72 'USER_ID',
73 array(
74 'required' => true,
75 )
76 ),
77
78 new Main\Entity\ReferenceField(
79 'USER',
80 '\Bitrix\Main\User',
81 array('=this.USER_ID' => 'ref.ID'),
82 array('join_type' => 'INNER')
83 ),
84
85 new Main\Entity\DatetimeField('LAST_ORDER_DATE'),
86
87 new Main\Entity\FloatField(
88 'SUM_PAID',
89 array(
90 'default_value' => '0.0000'
91 )
92 ),
93
94 new Main\Entity\IntegerField('COUNT_FULL_PAID_ORDER'),
95
96 new Main\Entity\IntegerField('COUNT_PART_PAID_ORDER'),
97
98 new Main\Entity\ReferenceField(
99 'GROUP',
100 '\Bitrix\Main\UserGroup',
101 array('=this.USER_ID' => 'ref.USER_ID'),
102 array('join_type' => 'INNER')
103 ),
104 );
105 }
106
110 public static function recalculate()
111 {
112 $connection = Main\Application::getConnection();
113 $type = $connection->getType();
114 if ($type == "mysql")
115 {
116 $sqlDelete = "TRUNCATE TABLE ".self::getTableName();
117 $connection->query($sqlDelete);
118
119 $sqlInsertInSelect = "
120 INSERT INTO ".self::getTableName()." (USER_ID, LID, CURRENCY, LAST_ORDER_DATE, COUNT_FULL_PAID_ORDER, COUNT_PART_PAID_ORDER, SUM_PAID)
121 SELECT UN.USER_ID, UN.LID, UN.CURRENCY, MAX(UN.DATE_INSERT), SUM(CASE WHEN UN.PAYED = 'Y' THEN 1 ELSE 0 END), SUM(CASE WHEN NOT UN.SUM_PAID = 0 THEN 1 ELSE 0 END), SUM(UN.SUM_PAID)
122 FROM (
123 (SELECT USER_ID, LID, CURRENCY, DATE_INSERT, PAYED, SUM_PAID FROM b_sale_order)
124 UNION
125 (SELECT USER_ID, LID, CURRENCY, DATE_INSERT, PAYED, SUM_PAID FROM b_sale_order_archive)
126 ) UN
127 GROUP BY UN.USER_ID, UN.CURRENCY, UN.LID
128 ORDER BY UN.USER_ID";
129 $connection->query($sqlInsertInSelect);
130 }
131 }
132}