Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
cumulativecalculator.php
1<?php
2
4
7
9{
12
13 const TYPE_COUNT_PERIOD_ALL_TIME = 'all_time';
14 const TYPE_COUNT_PERIOD_INTERVAL = 'interval';
15 const TYPE_COUNT_PERIOD_RELATIVE = 'relative';
16
17 private $userId;
18 private $siteId;
19 private $sumConfiguration = array();
20
21 public function __construct($userId, $siteId)
22 {
23 $this->userId = $userId;
24 $this->siteId = $siteId;
25 }
26
27 public function setSumConfiguration(array $sumConfiguration)
28 {
29 $this->sumConfiguration = $sumConfiguration;
30
31 return $this;
32 }
33
34 public function calculate()
35 {
36 if (!Loader::includeModule('currency'))
37 {
38 return 0;
39 }
40
41 if(empty($this->userId))
42 {
43 return 0;
44 }
45
46 $filter = $this->createFilterBySumConfiguration($this->sumConfiguration);
47 $orderUserId = $this->userId;
48 $filter = array_merge(array(
49 'USER_ID' => $orderUserId,
50 '=LID' => $this->siteId,
51 '=PAYED' => 'Y',
52 '=CANCELED' => 'N',
53 ), $filter);
54
55 $sum = 0;
56 foreach (array(self::TYPE_ORDER_NON_ARCHIVED, self::TYPE_ORDER_ARCHIVED) as $orderType)
57 {
58 $sum += $this->sumOrders($filter, $orderType);
59 }
60
61 return $sum;
62 }
63
64 private function createFilterBySumConfiguration($sumConfiguration)
65 {
66 $filter = array();
67 if (empty($sumConfiguration))
68 {
69 return $filter;
70 }
71
72 $type = $sumConfiguration['type_sum_period'];
73 $periodData = $sumConfiguration['sum_period_data'];
74
75 if ($type === self::TYPE_COUNT_PERIOD_INTERVAL)
76 {
77 if (!empty($periodData['order_start']))
78 {
79 $filter['>=DATE_INSERT'] = Main\Type\DateTime::createFromTimestamp($periodData['order_start']);
80 }
81 if (!empty($periodData['order_end']))
82 {
83 $filter['<DATE_INSERT'] = Main\Type\DateTime::createFromTimestamp($periodData['order_end']);
84 }
85 }
86 elseif ($type === self::TYPE_COUNT_PERIOD_RELATIVE)
87 {
88 $value = (int)$periodData['period_value'];
89 $typeRelativePeriod = $periodData['period_type'];
90 if (!in_array($typeRelativePeriod, array('D', 'M', 'Y')))
91 {
92 return array();
93 }
94
95 $start = new Main\Type\DateTime();
96 $end = $start->add("-P{$value}{$typeRelativePeriod}");
97
98 $filter['>=DATE_INSERT'] = $end;
99 }
100 elseif ($type === self::TYPE_COUNT_PERIOD_ALL_TIME)
101 {
102 return array();
103 }
104
105 return $filter;
106 }
107
108 private function sumOrders($filter, $orderType)
109 {
110 $provider = null;
111 if ($orderType === self::TYPE_ORDER_ARCHIVED)
112 {
114 $provider = '\Bitrix\Sale\Archive\Manager';
115 }
116 elseif ($orderType === self::TYPE_ORDER_NON_ARCHIVED)
117 {
119 $provider = '\Bitrix\Sale\Order';
120 }
121
122 if ($provider === null)
123 {
124 return false;
125 }
126
127 $orders = $provider::getList(
128 array(
129 'filter' => $filter,
130 'select' => array('DATE_INSERT', 'PRICE', 'CURRENCY')
131 )
132 );
133
134 $sum = 0;
135 $currency = null;
136 foreach ($orders as $orderData)
137 {
138 if (!$currency)
139 {
140 $currency = $orderData['CURRENCY'];
141 }
142
143 if ($currency !== $orderData['CURRENCY'])
144 {
145 $sum += \CCurrencyRates::ConvertCurrency($orderData['PRICE'], $orderData['CURRENCY'], $currency);
146 }
147 else
148 {
149 $sum += $orderData['PRICE'];
150 }
151 }
152
153 return $sum;
154 }
155}