-
Notifications
You must be signed in to change notification settings - Fork 15
/
class_addon.php
executable file
·353 lines (323 loc) · 11.9 KB
/
class_addon.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
/* For licensing terms, see /license.txt */
class addon extends model {
public $columns = array('id', 'name','setup_fee', 'description','status', 'install_package', 'mandatory');
public $table_name = 'addons';
public function create($params) {
$addon_id = $this->save($params);
return $addon_id;
}
public function edit($id, $params) {
$this->setId($id);
$this->update($params);
}
public function delete($id) {
$this->setId($id);
parent::delete();
//Deleting relation between addons and packages
$id = intval($id);
global $package;
$package->package_addons->setPrimaryKey('addon_id');
$package->package_addons->setId($id);
$package->package_addons->delete();
}
public function createPackageAddons($billing_id, $product_id, $amount, $type) {
global $main,$billing;
$billing_id = intval($billing_id);
$product_id = intval($product_id);
if (!in_array($type, array(BILLING_TYPE_ADDON, BILLING_TYPE_PACKAGE))) {
$type = 0;
}
$params['billing_id'] = $billing_id;
$params['product_id'] = $product_id;
$params['amount'] = $amount;
$params['type'] = $type;
$billing->billing_products->save($params);
}
/**
* Gets all addons by billing cycle id and package id
* @param int billing id
* @return array a list with all addons
* @author Julio Montoya <[email protected]> BeezNest 2010
*/
public function getAllAddonsByBillingCycleAndPackage($billing_id, $package_id) {
global $db;
$addon_list = array(); //&& !empty($package_id)
if (!empty($billing_id) && !empty($package_id) ) {
$billing_id = intval($billing_id);
$package_id = intval($package_id);
$sql = "SELECT a.id, a.name, amount, bc.name as billing_name FROM `<PRE>addons` a INNER JOIN `<PRE>billing_products` b ON (a.id = b.product_id) INNER JOIN `<PRE>billing_cycles` bc
ON (bc.id = b.billing_id) INNER JOIN `<PRE>package_addons` pa ON (pa.addon_id= a.id) WHERE bc.id = {$billing_id} AND pa.package_id = {$package_id} AND b.type = '".BILLING_TYPE_ADDON."' ";
$result = $db->query($sql);
while($data = $db->fetch_array($result)) {
$addon_list[$data['id']] = array('id'=>$data['id'], 'name' => $data['name'], 'amount'=>$data['amount']);
}
}
return $addon_list;
}
/**
* Show addon information of the selected addons
* @param int billing cycle id
* @param int package id
* @param array list of addon ids
* @param bool will generate or not checkboxes
* @return array The return value will have this structure array('html'=>$content, 'total'=>$total) where the $content is a html string, and the total is the addition of all selected addons
*
*/
public function showAllAddonsByBillingCycleAndPackage($billing_id, $package_id, $selected_addon_list = array(), $generate_checkboxes = true) {
global $main,$currency;
$values = $this->getAllAddonsByBillingCycleAndPackage($billing_id, $package_id);
$return_value = array();
$html = '';
$total = 0;
if (is_array($values) && count($values) > 0 ){
foreach($values as $value ) {
$checked = false;
if (isset($selected_addon_list[$value['id']])) {
$checked = true;
$total = $total + $value['amount'];
if ($generate_checkboxes == false) {
$html .= $value['name'].' - '.$currency->toCurrency($value['amount']).'<br />';
}
}
if($generate_checkboxes == true) {
$html .= $main->createCheckbox($value['name'].' - '.$currency->toCurrency($value['amount']), 'addon_'.$value['id'], $checked);
}
}
} else {
$html = ' - ';
}
if(empty($html)) {
$html = ' - ';
}
$return_value= array('html'=> $html, 'total' => $total);
return $return_value;
}
/**
* Gets all addons by billing id
* @param int billing cycle id
* @return array
*/
public function getAllAddonsByBillingId($billing_id) {
global $db;
$addon_list = array();
if (!empty($billing_id)) {
$billing_id = intval($billing_id);
$sql = "SELECT a.id, a.name, amount, bc.name as billing_name FROM `<PRE>addons` a INNER JOIN `<PRE>billing_products` b ON (a.id = b.product_id) INNER JOIN `<PRE>billing_cycles` bc
ON (bc.id = b.billing_id) WHERE bc.id = {$billing_id} AND b.type = '".BILLING_TYPE_ADDON."' ";
$addons_billing = $db->query($sql);
while($data = $db->fetch_array($addons_billing)) {
$addon_list[$data['id']] = array('id'=>$data['id'], 'name' => $data['name'], 'amount'=>$data['amount']);
}
}
return $addon_list;
}
/**
* Generates a serialized value of the addons payments
*
* Addon_fee structure
* array
0 =>
array
'addon_id' => int 9
'billing_id' => string '3'
'amount' => string '10.600000'
*
* @param array list of addon ids
* @param int billing id
* @param bool the array will be serialize true or false?
* @return mixed an array or a serialized string
* @author Julio Montoya <[email protected]> BeezNest 2010
*/
public function generateAddonFee($list_of_addons_ids, $billing_id, $serialize = false) {
global $db;
$addon_fee = array();
$billing_id = intval($billing_id);
if (is_array($list_of_addons_ids) && count($list_of_addons_ids) > 0 ) {
foreach ($list_of_addons_ids as $addon_id) {
if (is_numeric($addon_id)) {
$addon_id = intval($addon_id);
$sql_select = "SELECT amount FROM `<PRE>billing_products` WHERE product_id = $addon_id AND type = '".BILLING_TYPE_ADDON."' AND billing_id = $billing_id ";
$result = $db->query($sql_select);
$data_amount_addon = $db->fetch_array($result);
$addon_fee[] = array('addon_id'=>$addon_id,'billing_id'=>$billing_id, 'amount'=> $data_amount_addon['amount']);
}
}
}
if ($serialize == true) {
$addon_fee = serialize($addon_fee);
}
return $addon_fee;
}
/**
* Genererates the addon fee serialized from a given list of addons
* @param array list of addons id
* @param int billing cycle id
* @param bool the array will be serialized or not?
* @return mixed an array or a serialized string
*/
public function generateAddonFeeFromList($list_of_addons, $billing_id, $serialize = false) {
global $currency;
$addon_fee = array();
$billing_id = intval($billing_id);
if (is_array($list_of_addons) && count($list_of_addons) > 0 ) {
foreach ($list_of_addons as $addon_id=>$amount) {
if (is_numeric($addon_id)) {
$addon_id = intval($addon_id);
$addon_fee[] = array('addon_id'=>$addon_id,'billing_id'=>$billing_id, 'amount'=> $currency->toFloat($amount));
}
}
if ($serialize == true) {
$addon_fee = serialize($addon_fee);
}
}
return $addon_fee;
}
/**
* Updates the order_addons table
*/
public function updateAddonOrders($list_of_addons_ids, $order_id) {
global $order;
$addon_fee = array();
$order_id = intval($order_id);
$order->order_addons->setPrimaryKey('order_id');
$order->order_addons->setId($order_id);
$order->order_addons->delete();
//$result = $db->query("DELETE FROM `<PRE>order_addons` WHERE order_id = ".$order_id);
if (is_array($list_of_addons_ids) && count($list_of_addons_ids) > 0 ) {
foreach ($list_of_addons_ids as $addon_id) {
if (is_numeric($addon_id)) {
$addon_id = intval($addon_id);
$params['order_id'] = $order_id;
$params['addon_id'] = $addon_id;
$order->order_addons->save($params);
}
}
}
}
/**
* Generates addon checkboxes
*/
public function generateAddonCheckboxes($selected_values = array()) {
global $db, $main;
$result = $db->query("SELECT * FROM `<PRE>addons` WHERE status = ".ADDON_STATUS_ACTIVE);
$html = '';
if ($db->num_rows($result) > 0 ) {
while($data = $db->fetch_array($result)) {
$checked = false;
if (isset($selected_values[$data['id']])) {
$checked = true;
}
$mandatory_message = '';
if (isset($data['mandatory']) && $data['mandatory'] == 1) {
$mandatory_message = '(Mandatory)';
}
$html .= $main->createCheckbox($data['name'].' '.$mandatory_message, 'addon_'.$data['id'], $checked).'<br />';
}
}
if (empty($html)) {
$html = '-';
}
return $html;
}
/**
* Generate addon checkboxes within a given list
*/
public function generateAddonCheckboxesWithList($values, $selected_values = array(), $billing_id = null) {
global $main;
$html = '';
foreach($values as $value ) {
$checked = false;
if (isset($selected_values[$value['id']])) {
$checked = true;
}
$html .= $main->createCheckbox($value['name'], 'addon_'.$value['id'], $checked);
}
return $html;
}
/**
* Gets all addons
*/
public function getAllAddons($status = ADDON_STATUS_ACTIVE) {
global $db, $main;
if (!in_array($status, array(ADDON_STATUS_ACTIVE, ADDON_STATUS_INACTIVE))) {
$status = ADDON_STATUS_ACTIVE;
}
$result = $db->query("SELECT * FROM <PRE>addons WHERE status = ".$status);
$addon_list = array();
if($db->num_rows($result) > 0) {
while($data = $db->fetch_array($result, 'ASSOC')) {
$addon_list[$data['id']] = $data;
}
}
return $addon_list;
}
/**
* Gets all addons by billing id
*/
public function getAddonByBillingCycle($addon_id, $billing_id) {
global $db;
$addon_list = array();
if (!empty($billing_id) && !empty($addon_id)) {
$billing_id = intval($billing_id);
$addon_id = intval($addon_id);
$sql = "SELECT a.id, a.name, amount, bc.name as billing_name FROM `<PRE>addons` a INNER JOIN `<PRE>billing_products` b ON (a.id = b.product_id) INNER JOIN `<PRE>billing_cycles` bc
ON (bc.id = b.billing_id) WHERE bc.id = {$billing_id} AND a.id = {$addon_id} AND b.type= '".BILLING_TYPE_ADDON."'";
$addons_billing = $db->query($sql);
$addon_list = array();
while($data = $db->fetch_array($addons_billing)) {
$addon_list[$data['id']] = array('id'=>$data['id'], 'name' => $data['name'], 'amount'=>$data['amount']);
}
}
return $addon_list;
}
/**
* Gets all addons by package id
*/
public function getAddonsByPackage($package_id) {
global $db, $main;
$package_id = intval($package_id);
$addon_list = array();
if (!empty($package_id)) {
$result = $db->query("SELECT * FROM <PRE>addons a INNER JOIN <PRE>package_addons pa ON (pa.addon_id = a.id) WHERE package_id = ".$package_id);
if ($db->num_rows($result) > 0) {
while($data = $db->fetch_array($result)) {
$addon_list[$data['id']] = $data;
}
}
}
return $addon_list;
}
/**
* Gets all addons by package id
*/
public function getMandatoryAddonsByPackage($package_id) {
global $db, $main;
$package_id = intval($package_id);
$addon_list = array();
if (!empty($package_id)) {
$result = $db->query("SELECT id FROM <PRE>addons a INNER JOIN <PRE>package_addons pa ON (pa.addon_id = a.id) WHERE package_id = ".$package_id." AND mandatory = 1");
if($db->num_rows($result) > 0) {
while($data = $db->fetch_array($result, 'ASSOC')) {
$addon_list[$data['id']] = $data;
}
}
}
return $addon_list;
}
/*
public function getAddonByBillingCycleByOrder($addon_id, $billing_id) {
global $db;
$addong_list = array();
if (!empty($billing_id)) {
$sql = "SELECT a.id, a.name, amount, bc.name as billing_name FROM `<PRE>addons` a INNER JOIN `<PRE>billing_products` b ON (a.id = b.product_id) INNER JOIN `<PRE>billing_cycles` bc
ON (bc.id = b.billing_id) WHERE bc.id = {$billing_id} AND a.id = {$addon_id} ";
$addons_billing = $db->query($sql);
$addong_list = array();
while($data = $db->fetch_array($addons_billing)) {
$addong_list[$data['id']] = array('id'=>$data['id'], 'name' => $data['name'], 'amount'=>$data['amount']);
}
}
return $addong_list;
}*/
}