Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
statistic.php
1
<?php
2
3
4
namespace
Bitrix\Sale\Exchange\Integration\Manager
;
5
6
use
Bitrix\Main\Type\DateTime
;
7
use
Bitrix\Sale\Exchange\Integration\App\IntegrationB24
;
8
use
Bitrix\Sale\Exchange\Integration\Connector\Manager
;
9
use
Bitrix\Sale\Exchange\Integration\Entity\StatusType
;
10
use
Bitrix\Sale\Exchange\Integration\EntityType
;
11
use
Bitrix\Sale\Exchange\Integration\Service\Command\Line\StatisticsProvider
;
12
use
Bitrix\Sale\Exchange\Integration\Service\Container\Collection
;
13
use
Bitrix\Sale\Exchange\Integration\Service\Container\Item
;
14
use
Bitrix\Sale\Exchange\Integration\Service\Scenarios\Statistics
;
15
use
Bitrix\Sale\Exchange\Integration\Service\Statistic\Entity\Order
;
16
use
Bitrix\Sale\Exchange\Logger\Exchange
;
17
use
Bitrix\Sale\Exchange\Logger\ProviderType
;
18
use
Bitrix\Sale\Exchange\ManagerExport
;
19
use
Bitrix\Sale\OrderTable
;
20
21
class
Statistic
22
{
23
const
STATISTIC_IMPORT_PACKAGE_LIMIT
= 1000;
24
const
LOGGER_DAYS_INTERVAL
= 1;
25
26
protected
$app
;
27
protected
$collection
;
28
protected
$exchange
;
29
30
public
function
__construct
()
31
{
32
$this->app =
new
IntegrationB24
();
33
$this->collection =
new
Collection
();
34
$this->exchange =
new
Exchange
(
ProviderType::B24_INTEGRATION_NAME
);
35
}
36
40
public
function
getCollection
():
Collection
41
{
42
return
$this->collection
;
43
}
44
45
public
function
isOn
()
46
{
47
return
(
new
Manager
())->isOn();
48
}
49
50
public
function
modify
()
51
{
52
if
($this->
isOn
())
53
{
54
$provider = $this->
getProvider
();
55
if
(is_null($provider))
56
{
57
return
false
;
58
}
59
60
$list = $this->
getListByParams
($provider)
61
->getCollection()
62
->toArray();
63
64
if
(count($list)>0)
65
{
66
return
(
new
Statistics
())
67
->modify($list)
68
->isSuccess();
69
}
70
}
71
72
return
true
;
73
}
74
75
protected
function
getStatisticsStartDate
()
76
{
77
$startDate = new \Bitrix\Main\Type\DateTime();
78
$startDate->add(
'-1 month'
);
79
80
return
$startDate;
81
}
82
83
protected
function
getProvider
()
84
{
85
$res =
StatisticsProvider::getList
([
86
'xmlId'
=>$this->app
87
->getCode()
88
]);
89
90
return
(isset($res[
'error'
]) ==
false
) ? $res:
null
;
91
}
92
93
protected
function
prepareParamsByProviderFields
($provider)
94
{
95
$lastDateUpdate =
null
;
96
if
(isset($provider[
'statisticProviders'
][0][
'settings'
]))
97
{
98
$lastDateUpdate = $provider[
'statisticProviders'
][0][
'settings'
][
'lastDateUpdate'
];
99
}
100
101
return
[
102
'ID'
=> (int)$provider[
'statisticProviders'
][0][
'id'
],
103
'LAST_DATE_UPDATE'
=> $lastDateUpdate
104
];
105
}
106
107
protected
function
getListByParams
($provider)
108
{
109
$this->
deleteOldEffectedRows
();
110
111
$startDate = $this->
getStatisticsStartDate
()
112
->toString();
113
114
\CTimeZone::Disable();
115
116
$providerFields = $this->
prepareParamsByProviderFields
($provider);
117
118
if
(is_null($providerFields[
'LAST_DATE_UPDATE'
]))
119
{
120
$time = $startDate;
121
}
122
else
123
{
124
$lastDateUpdate = strtotime($providerFields[
'LAST_DATE_UPDATE'
]);
125
$time =
DateTime::createFromTimestamp
($lastDateUpdate)
126
->toString();
127
}
128
129
$filter[
'>=DATE_UPDATE'
] = $time;
130
$filter[
'=RUNNING'
] =
'N'
;
131
132
$logs = $this->exchange->getEffectedRows(
133
$time,
134
EntityType::ORDER
,
135
ManagerExport::EXCHANGE_DIRECTION_EXPORT
);
136
137
$r = OrderTable::getList([
138
'order'
=>[
'DATE_UPDATE'
=>
'ASC'
],
139
'filter'
=>$filter,
140
'limit'
=> static::STATISTIC_IMPORT_PACKAGE_LIMIT
141
]);
142
143
while
($res = $r->fetch())
144
{
145
if
($this->exchange->isEffected($res, $logs))
146
{
147
continue
;
148
}
149
150
$res[
'AMOUNT'
] = $res[
'PRICE'
];
151
$res[
'STATUS'
] = $this->
resolveStatusId
($res);
152
$res[
'ENTITY_ID'
] = $res[
'ID'
];
153
$res[
'PROVIDER_ID'
] = $providerFields[
'ID'
];
154
155
$this->collection->addItem(
156
Item::create(
157
Order::createFromArray
($res))
158
->setInternalIndex($res[
'ID'
])
159
);
160
161
$this->
addEffectedRows
($res);
162
}
163
164
\CTimeZone::Enable();
165
166
return
$this;
167
}
168
169
protected
function
resolveStatusId
($fields)
170
{
171
if
($fields[
'PAYED'
] ==
'Y'
|| $fields[
'DEDUCTED'
] ==
'Y'
)
172
{
173
return
StatusType::SUCCESS_NAME;
174
}
175
elseif ($fields[
'CANCELED'
] ==
'Y'
)
176
{
177
return
StatusType::FAULTY_NAME;
178
}
179
else
180
{
181
return
StatusType::PROCESS_NAME;
182
}
183
}
184
185
protected
function
deleteOldEffectedRows
()
186
{
187
$this->exchange->deleteOldRecords(
ManagerExport::EXCHANGE_DIRECTION_EXPORT
, static::LOGGER_DAYS_INTERVAL);
188
}
189
190
protected
function
addEffectedRows
(array $fields)
191
{
192
$this->exchange->add([
193
'XML_ID'
=> $fields[
'XML_ID'
],
194
'ENTITY_ID'
=> $fields[
'ID'
],
195
'DIRECTION'
=>
ManagerExport::EXCHANGE_DIRECTION_EXPORT
,
196
'ENTITY_TYPE_ID'
=>
EntityType::ORDER
,
197
'ENTITY_DATE_UPDATE'
=> $fields[
'DATE_UPDATE'
]
198
]);
199
}
200
201
}
Bitrix\Main\Type\DateTime
Definition
datetime.php:9
Bitrix\Main\Type\DateTime\createFromTimestamp
static createFromTimestamp($timestamp)
Definition
datetime.php:246
Bitrix\Sale\Exchange\Integration\App\IntegrationB24
Definition
integrationb24.php:6
Bitrix\Sale\Exchange\Integration\CRM\EntityType
Definition
entitytype.php:5
Bitrix\Sale\Exchange\Integration\Connector\Manager
Definition
manager.php:14
Bitrix\Sale\Exchange\Integration\Entity\StatusType
Definition
statustype.php:10
Bitrix\Sale\Exchange\Integration\EntityType\ORDER
const ORDER
Definition
entitytype.php:12
Bitrix\Sale\Exchange\Integration\Manager\Statistic
Definition
statistic.php:22
Bitrix\Sale\Exchange\Integration\Manager\Statistic\modify
modify()
Definition
statistic.php:50
Bitrix\Sale\Exchange\Integration\Manager\Statistic\__construct
__construct()
Definition
statistic.php:30
Bitrix\Sale\Exchange\Integration\Manager\Statistic\addEffectedRows
addEffectedRows(array $fields)
Definition
statistic.php:190
Bitrix\Sale\Exchange\Integration\Manager\Statistic\$exchange
$exchange
Definition
statistic.php:28
Bitrix\Sale\Exchange\Integration\Manager\Statistic\isOn
isOn()
Definition
statistic.php:45
Bitrix\Sale\Exchange\Integration\Manager\Statistic\prepareParamsByProviderFields
prepareParamsByProviderFields($provider)
Definition
statistic.php:93
Bitrix\Sale\Exchange\Integration\Manager\Statistic\deleteOldEffectedRows
deleteOldEffectedRows()
Definition
statistic.php:185
Bitrix\Sale\Exchange\Integration\Manager\Statistic\getListByParams
getListByParams($provider)
Definition
statistic.php:107
Bitrix\Sale\Exchange\Integration\Manager\Statistic\STATISTIC_IMPORT_PACKAGE_LIMIT
const STATISTIC_IMPORT_PACKAGE_LIMIT
Definition
statistic.php:23
Bitrix\Sale\Exchange\Integration\Manager\Statistic\getStatisticsStartDate
getStatisticsStartDate()
Definition
statistic.php:75
Bitrix\Sale\Exchange\Integration\Manager\Statistic\$collection
$collection
Definition
statistic.php:27
Bitrix\Sale\Exchange\Integration\Manager\Statistic\getCollection
getCollection()
Definition
statistic.php:40
Bitrix\Sale\Exchange\Integration\Manager\Statistic\getProvider
getProvider()
Definition
statistic.php:83
Bitrix\Sale\Exchange\Integration\Manager\Statistic\$app
$app
Definition
statistic.php:26
Bitrix\Sale\Exchange\Integration\Manager\Statistic\LOGGER_DAYS_INTERVAL
const LOGGER_DAYS_INTERVAL
Definition
statistic.php:24
Bitrix\Sale\Exchange\Integration\Manager\Statistic\resolveStatusId
resolveStatusId($fields)
Definition
statistic.php:169
Bitrix\Sale\Exchange\Integration\Service\Command\Batch\Statistics
Definition
statistics.php:17
Bitrix\Sale\Exchange\Integration\Service\Command\Line\StatisticsProvider
Definition
statisticsprovider.php:11
Bitrix\Sale\Exchange\Integration\Service\Command\Line\StatisticsProvider\getList
static getList($filter)
Definition
statisticsprovider.php:39
Bitrix\Sale\Exchange\Integration\Service\Container\Collection
Definition
collection.php:7
Bitrix\Sale\Exchange\Integration\Service\Container\Item
Definition
item.php:9
Bitrix\Sale\Exchange\Integration\Service\Scenarios\Statistics
Definition
statistics.php:9
Bitrix\Sale\Exchange\Integration\Service\Statistic\Entity\Order
Definition
order.php:8
Bitrix\Sale\Exchange\Integration\Service\Statistic\Entity\Order\createFromArray
static createFromArray(array $fields)
Definition
order.php:97
Bitrix\Sale\Exchange\Logger\Exchange
Definition
exchange.php:11
Bitrix\Sale\Exchange\Logger\ProviderType
Definition
providertype.php:5
Bitrix\Sale\Exchange\Logger\ProviderType\B24_INTEGRATION_NAME
const B24_INTEGRATION_NAME
Definition
providertype.php:7
Bitrix\Sale\Exchange\ManagerBase\EXCHANGE_DIRECTION_EXPORT
const EXCHANGE_DIRECTION_EXPORT
Definition
managerbase.php:16
Bitrix\Sale\Exchange\ManagerExport
Definition
managerexport.php:10
Bitrix\Sale\Internals\OrderTable
Definition
order.php:32
Bitrix\Sale\Exchange\Integration\Manager
Definition
statistic.php:4
modules
sale
lib
exchange
integration
manager
statistic.php
Создано системой
1.10.0