/
migrate_services_external_billing_id.php
124 lines (116 loc) · 7.15 KB
/
migrate_services_external_billing_id.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
require_once __DIR__ . '/../../../init.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'api.php';
use Illuminate\Database\Capsule\Manager as Capsule;
$whmcsServicesCount = Capsule::table('tblhosting')
->join('tblproducts', 'tblhosting.packageid', '=', 'tblproducts.id')
->join('tblclients as tc', 'tc.id', '=', 'tblhosting.userid')
->where('tblproducts.servertype', '=', 'fleio')
->count();
$whmcsClients = Capsule::table('tblclients')
->select('tblclients.id', 'tblclients.uuid')
->get();
// SCRIPT WORKS IF THERE IS AT MOST ONE SERVICE PER CLIENT IN FLEIO
echo 'There are '. $whmcsServicesCount . ' fleio services';
echo "\r\n";
$processedServices = 0;
foreach($whmcsClients AS $whmcsClient) {
$whmcsClientServices = Capsule::table('tblhosting')
->join('tblproducts', 'tblhosting.packageid', '=', 'tblproducts.id')
->where('tblproducts.servertype', '=', 'fleio')
->where('tblhosting.userid', '=', $whmcsClient->id)
->select('tblhosting.id', 'tblhosting.domainstatus')
->get();
$servicesStatusesCountMap = array(
"Active" => array(
"count" => 0,
"id_list" => array()
),
"Suspended" => array(
"count" => 0,
"id_list" => array()
),
"Terminated" => array(
"count" => 0,
"id_list" => array()
),
"Cancelled" => array(
"count" => 0,
"id_list" => array()
),
"Fraud" => array(
"count" => 0,
"id_list" => array()
),
"Pending" => array(
"count" => 0,
"id_list" => array()
)
);
foreach($whmcsClientServices AS $whmcsClientService) {
$servicesStatusesCountMap[$whmcsClientService->domainstatus]["count"] = $servicesStatusesCountMap[$whmcsClientService->domainstatus]["count"] + 1;
array_push($servicesStatusesCountMap[$whmcsClientService->domainstatus]["id_list"], $whmcsClientService->id);
$processedServices = $processedServices + 1;
}
if ($servicesStatusesCountMap["Active"]["count"] > 1) {
// if more than one active found, we cannot automatically determine
echo "Cannot process services for client " . $whmcsClient->id . " : More than one active service found\n";
} else if ($servicesStatusesCountMap["Active"]["count"] == 1) {
// if only one active, update it in fleio
try {
$flApi = Fleio::fromServiceId($servicesStatusesCountMap["Active"]["id_list"][0]);
$flApi->updateServiceExternalBillingId($servicesStatusesCountMap["Active"]["id_list"][0], $whmcsClient->uuid);
} catch (Exception $e) {
echo ''.$e->getMessage();
}
} else if ($servicesStatusesCountMap["Suspended"]["count"] > 1) {
// if none active found and multiple suspended found, we cannot determine
echo "Cannot process services for client " . $whmcsClient->id . " : More than one suspended service found and none active\n";
} else if ($servicesStatusesCountMap["Suspended"]["count"] == 1) {
// if none active but one suspended set it
try {
$flApi = Fleio::fromServiceId($servicesStatusesCountMap["Suspended"]["id_list"][0]);
$flApi->updateServiceExternalBillingId($servicesStatusesCountMap["Suspended"]["id_list"][0], $whmcsClient->uuid);
} catch (Exception $e) {
echo ''.$e->getMessage();
}
} else if ($servicesStatusesCountMap["Terminated"]["count"] > 0 && $servicesStatusesCountMap["Cancelled"]["count"] > 0) {
// if none active or suspended but at least one terminated and cancelled, we cannot determine
echo "Cannot process services for client " . $whmcsClient->id . " : Has no active or suspended service and has at least one terminated and at least one cancelled service.\n";
} else if ($servicesStatusesCountMap["Terminated"]["count"] > 1) {
// if none active, suspended or cancelled but multiple terminated we cannot determine
echo "Cannot process services for client " . $whmcsClient->id . " : Has no active, suspended or cancelled service and has multiple terminated services.\n";
} else if ($servicesStatusesCountMap["Cancelled"]["count"] > 1) {
// if none active, suspended or terminated but multiple cancelled we cannot determine
echo "Cannot process services for client " . $whmcsClient->id . " : Has no active, suspended or terminated service and has multiple cancelled services.\n";
} else if ($servicesStatusesCountMap["Cancelled"]["count"] == 1) {
// if no other status than one of cancelled (excluding pending), update it in fleio
try {
$flApi = Fleio::fromServiceId($servicesStatusesCountMap["Cancelled"]["id_list"][0]);
$flApi->updateServiceExternalBillingId($servicesStatusesCountMap["Cancelled"]["id_list"][0], $whmcsClient->uuid);
} catch (Exception $e) {
echo ''.$e->getMessage();
}
} else if ($servicesStatusesCountMap["Terminated"]["count"] == 1) {
// if no other status than one of terminated (excluding pending), update it in fleio
try {
$flApi = Fleio::fromServiceId($servicesStatusesCountMap["Terminated"]["id_list"][0]);
$flApi->updateServiceExternalBillingId($servicesStatusesCountMap["Terminated"]["id_list"][0], $whmcsClient->uuid);
} catch (Exception $e) {
echo ''.$e->getMessage();
}
} else if ($servicesStatusesCountMap["Fraud"]["count"] > 1) {
// if none active, suspended, terminated or cancelled but multiple fraud we cannot determine
echo "Cannot process services for client " . $whmcsClient->id . " : Has no active, suspended, terminated or cancelled services and has multiple fraud services.\n";
} else if ($servicesStatusesCountMap["Fraud"]["count"] == 1) {
// if no other status than one of fraud (excluding pending), update it in fleio
try {
$flApi = Fleio::fromServiceId($servicesStatusesCountMap["Fraud"]["id_list"][0]);
$flApi->updateServiceExternalBillingId($servicesStatusesCountMap["Fraud"]["id_list"][0], $whmcsClient->uuid);
} catch (Exception $e) {
echo ''.$e->getMessage();
}
}
}
echo 'Processed services count: '.$processedServices;
?>