Repository navigation
Expand file tree
/
Copy pathhook.php
More file actions
273 lines (225 loc) · 8.83 KB
/
Copy pathhook.php
File metadata and controls
273 lines (225 loc) · 8.83 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
if (!defined("WHMCS")) {
die("This file cannot be accessed directly");
}
use WHMCS\Database\Capsule;
/**
* Register all module hooks
*/
function tiered_reseller_register_hooks() {
return [
// System hooks
'DailyCronJob' => 'tiered_reseller_daily_sync',
// Order processing
'AfterModuleCreate' => 'tiered_reseller_after_service_creation',
'AfterModuleTerminate' => 'tiered_reseller_after_service_termination',
// Invoice generation
'InvoiceCreation' => 'tiered_reseller_invoice_created',
'InvoicePaid' => 'tiered_reseller_invoice_paid',
// Client area
'ClientAreaHomepage' => 'tiered_reseller_client_area_widget',
// Admin area
'AdminAreaHeader' => 'tiered_reseller_admin_notices'
];
}
/**
* Daily cron job for account sync and invoicing
*/
function tiered_reseller_daily_sync() {
try {
require_once __DIR__ . '/includes/WHMAPI.php';
require_once __DIR__ . '/includes/Utilities.php';
$whmAPI = new TieredReseller_WHMAPI();
$utils = new TieredReseller_Utilities();
// Get all active resellers
$resellers = Capsule::table('mod_tiered_reseller')
->whereNotNull('whm_username')
->get();
foreach ($resellers as $reseller) {
// 1. Sync account count from WHM
$accountCount = $whmAPI->getAccountCount($reseller->whm_username);
// 2. Update database
Capsule::table('mod_tiered_reseller')
->where('id', $reseller->id)
->update([
'account_count' => $accountCount,
'last_sync' => Capsule::raw('NOW()')
]);
// 3. Check if today is billing anniversary
if (date('Y-m-d') == $reseller->next_due_date) {
$utils->generateAnnualInvoice($reseller->userid);
}
// 4. Check for tier changes
$currentTier = $reseller->current_tier;
$newTier = $utils->calculateTier($accountCount);
if ($currentTier != $newTier) {
$utils->handleTierChange($reseller->userid, $currentTier, $newTier);
}
}
logActivity("[Tiered Reseller] Daily sync completed for " . count($resellers) . " resellers");
} catch (Exception $e) {
logActivity("[Tiered Reseller] Sync Error: " . $e->getMessage());
}
}
/**
* After service creation hook
*/
function tiered_reseller_after_service_creation($vars) {
$serviceId = $vars['params']['serviceid'];
$productName = $vars['params']['productname'];
if (stripos($productName, 'reseller') !== false) {
try {
$userId = $vars['params']['clientsdetails']['userid'];
// Check if already exists
$exists = Capsule::table('mod_tiered_reseller')
->where('userid', $userId)
->exists();
if (!$exists) {
// Add new reseller record
Capsule::table('mod_tiered_reseller')->insert([
'userid' => $userId,
'serviceid' => $serviceId,
'whm_username' => $vars['params']['username'],
'setup_paid' => 0,
'next_due_date' => date('Y-m-d', strtotime('+1 year'))
]);
// Charge setup fee
$setupFee = Capsule::table('tbladdonmodules')
->where('module', 'tiered_reseller')
->where('setting', 'setup_fee')
->value('value');
if ($setupFee > 0) {
$invoiceId = localAPI('CreateInvoice', [
'userid' => $userId,
'date' => date('Y-m-d'),
'duedate' => date('Y-m-d'),
'itemdescription1' => 'Reseller Program Setup Fee',
'itemamount1' => $setupFee,
'itemtaxed1' => false
]);
if ($invoiceId['result'] == 'success') {
Capsule::table('mod_tiered_reseller')
->where('userid', $userId)
->update(['setup_paid' => 1]);
}
}
}
} catch (Exception $e) {
logActivity("[Tiered Reseller] Service Creation Error: " . $e->getMessage());
}
}
}
/**
* After service termination hook
*/
function tiered_reseller_after_service_termination($vars) {
$serviceId = $vars['params']['serviceid'];
try {
Capsule::table('mod_tiered_reseller')
->where('serviceid', $serviceId)
->delete();
} catch (Exception $e) {
logActivity("[Tiered Reseller] Service Termination Error: " . $e->getMessage());
}
}
/**
* Invoice creation hook
*/
function tiered_reseller_invoice_created($vars) {
$invoiceId = $vars['invoiceid'];
try {
$invoice = localAPI('GetInvoice', ['invoiceid' => $invoiceId]);
foreach ($invoice['items']['item'] as $item) {
if (strpos($item['description'], 'hosted accounts') !== false) {
$userId = $invoice['userid'];
// Update last invoice date
Capsule::table('mod_tiered_reseller')
->where('userid', $userId)
->update([
'last_invoice_date' => date('Y-m-d'),
'last_invoice_amount' => $item['amount']
]);
break;
}
}
} catch (Exception $e) {
logActivity("[Tiered Reseller] Invoice Creation Error: " . $e->getMessage());
}
}
/**
* Invoice paid hook
*/
function tiered_reseller_invoice_paid($vars) {
$invoiceId = $vars['invoiceid'];
try {
$invoice = localAPI('GetInvoice', ['invoiceid' => $invoiceId]);
foreach ($invoice['items']['item'] as $item) {
if (strpos($item['description'], 'hosted accounts') !== false) {
$userId = $invoice['userid'];
// Send confirmation email
$config = Capsule::table('tbladdonmodules')
->where('module', 'tiered_reseller')
->pluck('value', 'setting');
if ($config['enable_emails'] == 'on') {
sendMessage('Reseller Invoice Paid', $userId, [
'invoice_id' => $invoiceId,
'invoice_amount' => $item['amount'],
'account_count' => Capsule::table('mod_tiered_reseller')
->where('userid', $userId)
->value('account_count')
]);
}
break;
}
}
} catch (Exception $e) {
logActivity("[Tiered Reseller] Invoice Paid Error: " . $e->getMessage());
}
}
/**
* Client area widget
*/
function tiered_reseller_client_area_widget() {
$userId = $_SESSION['uid'] ?? 0;
try {
$resellerData = Capsule::table('mod_tiered_reseller')
->where('userid', $userId)
->first();
if ($resellerData) {
return [
'title' => 'Reseller Program',
'data' => [
'Hosted Accounts' => $resellerData->account_count,
'Current Tier' => 'Tier ' . $resellerData->current_tier,
'Next Billing Date' => $resellerData->next_due_date
],
'footer' => '<a href="index.php?m=tiered_reseller">View Details</a>'
];
}
} catch (Exception $e) {
logActivity("[Tiered Reseller] Client Widget Error: " . $e->getMessage());
}
return [];
}
/**
* Admin area notices
*/
function tiered_reseller_admin_notices() {
$needsSync = Capsule::table('mod_tiered_reseller')
->whereNull('last_sync')
->orWhere('last_sync', '<', date('Y-m-d', strtotime('-2 days')))
->exists();
if ($needsSync) {
echo '<div class="alert alert-warning text-center">
<strong>Tiered Reseller:</strong> Some accounts need synchronization
<a href="addonmodules.php?module=tiered_reseller" class="btn btn-xs btn-default">Sync Now</a>
</div>';
}
}
// Register all hooks
add_hook('AdminAreaHeadOutput', 1, function() {
$hooks = tiered_reseller_register_hooks();
foreach ($hooks as $hookPoint => $function) {
add_hook($hookPoint, 1, $function);
}
});