-
Notifications
You must be signed in to change notification settings - Fork 2
/
addNewStatusesToDealCategories.php
105 lines (97 loc) · 2.96 KB
/
addNewStatusesToDealCategories.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
class AbstractMigration
{
protected static $arStatuses = array(
'NEW'=>[
'NAME'=>'Новый заказ',
'SYSTEM'=>'Y'
],
'RETURNED_WITH_NUANCES'=>[
'NAME'=>'Должник',
'SYSTEM'=>'N'
],
'WON'=>[
'NAME'=>'Успешна',
'SYSTEM'=>'Y'
],
'LOSE'=>[
'NAME'=>'Неуспешна',
'SYSTEM'=>'Y'
],
);
private function getDealCategories():array
{
return [
[
'CREATED_DATE'=>new \Bitrix\Main\Type\DateTime(),
'NAME'=>'Test',
'SORT'=>50,
],
[
'CREATED_DATE'=>new \Bitrix\Main\Type\DateTime(),
'NAME'=>'Test2',
'SORT'=>60,
],
];
}
protected function deleteDealStatuses()
{
$connection = \Bitrix\Main\Application::getInstance()->getConnection();
foreach (self::getDealCategories() as $arCategory)
{
$res = \Bitrix\Crm\Category\Entity\DealCategoryTable::getList([
'filter'=>['NAME'=>$arCategory['NAME']],
'select'=>['ID'],
])->fetch();
if($res)
{
$sql = 'DELETE FROM b_crm_status WHERE ENTITY_ID = "' . "DEAL_STAGE_".$res['ID']. '"';
$connection->queryExecute($sql);
}
}
}
protected function setDealStatuses()
{
$connection = \Bitrix\Main\Application::getInstance()->getConnection();
$sort = 0;
foreach (self::getDealCategories() as $arCategory)
{
$res = \Bitrix\Crm\Category\Entity\DealCategoryTable::getList([
'filter'=>['NAME'=>$arCategory['NAME']],
'select'=>['ID'],
])->fetch();
if($res)
{
$categoryId = $res['ID'];
foreach (self::$arStatuses as $statusId=>$arStatus)
{
$sort += 10;
$DEAL_STAGE = "DEAL_STAGE_".$categoryId;
$sql = 'INSERT INTO b_crm_status (ENTITY_ID, NAME, STATUS_ID, SORT, SYSTEM)'.
' VALUES ('
.'"'.$DEAL_STAGE.'"'.','
.'"'.$arStatus["NAME"].'"'.','
.'"'.'C'.$categoryId.':'.$statusId.'"'.','
.'"'.$sort.'"'.','
.'"'.$arStatus["SYSTEM"].'"'.')';
$connection->queryExecute($sql);
}
}
}
}
public function up()
{
if(\Bitrix\Main\Loader::includeModule('crm'))
{
$this->deleteDealStatuses();
$this->setDealStatuses();
}
}
public function down()
{
if(\Bitrix\Main\Loader::includeModule('crm'))
{
$this->deleteDealStatuses();
}
}
}