Passed
Pull Request — v3.0 (#166)
by Samir
12:13
created

Accounts::reseller_save()   C

Complexity

Conditions 8
Paths 6

Size

Total Lines 90
Code Lines 62

Duplication

Lines 28
Ratio 31.11 %

Importance

Changes 0
Metric Value
cc 8
eloc 62
nc 6
nop 0
dl 28
loc 90
rs 5.6109
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
###############################################################################
4
# ASTPP - Open Source VoIP Billing Solution
5
#
6
# Copyright (C) 2016 iNextrix Technologies Pvt. Ltd.
7
# Samir Doshi <[email protected]>
8
# ASTPP Version 3.0 and above
9
# License https://www.gnu.org/licenses/agpl-3.0.html
10
#
11
# This program is free software: you can redistribute it and/or modify
12
# it under the terms of the GNU Affero General Public License as
13
# published by the Free Software Foundation, either version 3 of the
14
# License, or (at your option) any later version.
15
# 
16
# This program is distributed in the hope that it will be useful,
17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
# GNU Affero General Public License for more details.
20
# 
21
# You should have received a copy of the GNU Affero General Public License
22
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
###############################################################################
24
25
class Accounts extends MX_Controller {
26
27
	function Accounts() {
28
		parent::__construct();
29
		$this->load->helper('template_inheritance');
30
31
		$this->load->library('accounts_form');
32
		$this->load->library('astpp/form');
33
34
		$this->load->model('common_model');
35
		$this->load->library('session');
36
		$this->load->helper('form');
37
		$this->load->model('accounts_model');
38
		$this->load->model('Astpp_common');
39
		$this->protected_pages = array('account_list');
40
		if ($this->session->userdata('user_login') == FALSE)
41
			redirect(base_url() . '/login/login');
42
	}
43
44
	function customer_list() {
45
		$data['username'] = $this->session->userdata('user_name');
46
		$data['page_title'] = 'Customers';
47
		$data['search_flag'] = true;
48
		/**
49
          ASTPP  3.0 
50
          Customer Batch Update.
51
		 * */
52
		$data['batch_update_flag'] = true;
53
		/*         * *********************************** */
54
		$this->session->set_userdata('advance_search', 0);
55
		$data['grid_fields'] = $this->accounts_form->build_account_list_for_customer();
56
		$data["grid_buttons"] = $this->accounts_form->build_grid_buttons_customer();
57
		$data['form_search'] = $this->form->build_serach_form($this->accounts_form->get_search_customer_form());
58
		/**
59
          ASTPP  3.0 
60
          Customer Batch Update.
61
		 * */
62
         
63
		$data['form_batch_update'] = $this->form->build_batchupdate_form($this->accounts_form->customer_batch_update_form());
64
		/*         * *****************************************************************************************8 */
65
		$this->load->view('view_accounts_list', $data);
66
	}
67
68
	/**
69
	 * -------Here we write code for controller accounts functions account_list------
70
	 * Listing of Accounts table data through php function json_encode
71
	 */
72
	function customer_list_json() {
73
		$json_data = array();
74
		$count_all = $this->accounts_model->get_customer_Account_list(false);
75
		$paging_data = $this->form->load_grid_config($count_all, $_GET['rp'], $_GET['page']);
76
		$json_data = $paging_data["json_paging"];
77
		$query = $this->accounts_model->get_customer_Account_list(true, $paging_data["paging"]["start"], $paging_data["paging"]["page_no"]);
78
		$grid_fields = json_decode($this->accounts_form->build_account_list_for_customer());
79
		$json_data['rows'] = $this->form->build_grid($query, $grid_fields);
80
		echo json_encode($json_data);
81
	}
82
83
	function customer_list_search() {
84
		$ajax_search = $this->input->post('ajax_search', 0);
85
86
		if ($this->input->post('advance_search', TRUE) == 1) {
87
			$this->session->set_userdata('advance_search', $this->input->post('advance_search'));
88
			$action = $this->input->post();
89
			unset($action['action']);
90
			unset($action['advance_search']);
91
			if (isset($action['balance']['balance']) && $action['balance']['balance'] != '') {
92
				$action['balance']['balance'] = $this->common_model->add_calculate_currency($action['balance']['balance'], "", '', false, false);
93
			}
94
			if (isset($action['credit_limit']['credit_limit']) && $action['credit_limit']['credit_limit'] != '') {
95
				$action['credit_limit']['credit_limit'] = $this->common_model->add_calculate_currency($action['credit_limit']['credit_limit'], "", '', false, false);
96
			}
97
			$this->session->set_userdata('customer_list_search', $action);
98
		}
99
		if (@$ajax_search != 1) {
100
			redirect(base_url() . 'accounts/customer_list/');
101
		}
102
	}
103
104
	function customer_list_clearsearchfilter() {
105
		$this->session->set_userdata('advance_search', 0);
106
		$this->session->set_userdata('customer_list_search', "");
107
	}
108
109
	function customer_export_cdr_xls() {
110
	$account_info = $accountinfo = $this->session->userdata('accountinfo');
111
	$currency_id=$account_info['currency_id'];
112
	 $currency=$this->common->get_field_name('currency', 'currency', $currency_id);
113
		$query = $this->accounts_model->get_customer_Account_list(true, '', '', true);
114
		ob_clean();
115
		$customer_array[] = array("Account", "First Name", "Last Name", "Company", "Rate Group ", "Balance ($currency)", "Credit Limit ($currency)", "First Used", "Expiry Date","CC","Status","Created Date");
116
		if ($query->num_rows() > 0) {
117
118
			foreach ($query->result_array() as $row) {
119
				$customer_array[] = array(
120
					$row['number'],
121
					$row['first_name'],
122
					$row['last_name'],
123
					$row['company_name'],
124
					$this->common->get_field_name('name', 'pricelists', $row['pricelist_id']),
125
					$this->common_model->calculate_currency_customer($row['balance']),
126
					$this->common_model->calculate_currency_customer($row['credit_limit']),
127
					$row['first_used'],
128
					$row['expiry'],
129
					$row['maxchannels'],
130
					$this->common->get_status('export', '', $row['status']),
131
					$row['creation'],
132
				   );
133
			}
134
		}
135
		$this->load->helper('csv');
136
		array_to_csv($customer_array, 'Customers_' . date("Y-m-d") . '.csv');
137
	}
138
139
	function provider_add() {
140
		$this->customer_add(3);
141
	}
142
143
	function provider_edit($edit_id = '') {
144
		$this->customer_edit($edit_id);
145
	}
146
147
	function provider_save() {
148
		$add_array = $this->input->post();
149
		$this->customer_save($add_array);
150
	}
151
152
	function customer_add($type = 0) {
153
		$accountinfo = $this->session->userdata('accountinfo');
154
		$entity_type = strtolower($this->common->get_entity_type('', '', $type));
155
		$data['username'] = $this->session->userdata('user_name');
156
		$data['flag'] = 'create';
157
		$data['page_title'] = 'Create ' . $entity_type;
158
		$data['back_flag'] = true;
159
		$data['country_id'] = $accountinfo['country_id'];
160
		$data['callingcard'] = Common_model::$global_config['system_config']['pinlength'];
161
		$data['currency_id'] = $accountinfo['currency_id'];
162
		$data['timezone_id'] = $accountinfo['timezone_id'];
163
		$data['form'] = $this->form->build_form($this->accounts_form->get_customer_form_fields($entity_type), '');
164
		if (!$data['timezone_id']) {
165
			$data['timezone_id'] = 1;
166
		}
167
		if (!$data['currency_id']) {
168
			$data['currency_id'] = 1;
169
		}
170
		if (!$data['country_id']) {
171
			$data['country_id'] = 1;
172
		}
173
		$data['entity_name'] = $entity_type;
174
		$this->load->view('view_accounts_create', $data);
175
	}
176
177
	function customer_edit($edit_id = '') {
178
		//Get Account information from session.
179
		$accountinfo = $this->session->userdata('accountinfo');
180
		//Get Parent informartion
181
		$reseller_id = ($accountinfo['type'] == 1 || $accountinfo['type'] == 5) ? $accountinfo['id'] : 0;
182
		$where = array('id' => $edit_id, "reseller_id" => $reseller_id);
183
		$account = $this->db_model->getSelect("*", "accounts", $where);
184
		if ($account->num_rows > 0) {
185
			$account_data = (array) $account->first_row();
186
			$entity_name = strtolower($this->common->get_entity_type('', '', $account_data['type']));
187
			$data['page_title'] =ucfirst($entity_name) . " Profile";
188
			$data['invoice_date'] = $account_data['invoice_day'];
189
			$data["account_data"] = $account_data;
190
			 $data['callingcard'] = Common_model::$global_config['system_config']['pinlength'];
191
			$data['entity_name'] = $entity_name;
192
			//Purpose : Get assigned taxes value for customer
193
			$taxes_data = $this->db_model->getSelect("group_concat(taxes_id) as taxes_id", "taxes_to_accounts", array("accountid" => $edit_id));
194
			if (isset($taxes_data) && $taxes_data->num_rows() > 0) {
195
				$taxes_data = $taxes_data->result_array();
196
				$account_data["tax_id"] = explode(",", $taxes_data[0]['taxes_id']);
197
			}
198
			/*             * ***
199
              ASTPP  3.0 
200
              Password decode
201
             * **** */
202
			$account_data['password'] = $this->common->decode($account_data['password']);
203
			/*             * ********************** */
204
			$data['form'] = $this->form->build_form($this->accounts_form->get_customer_form_fields($entity_name, $edit_id), $account_data);
205
			$data['edit_id']=$edit_id;
206
			$this->load->view('view_customer_details', $data);
207
		} else {
208
			redirect(base_url() . 'accounts/customer_list/');
209
		}
210
	}
211
212
    function customer_save($add_array = false) {
213
        $add_array = $this->input->post();
214
        $entity_name = strtolower($this->common->get_entity_type('', '', $add_array['type']));
215
        $data['country_id'] = isset($add_array['country_id'])? $add_array['country_id']:'';
216
        $data['timezone_id'] = isset($add_array['timezone_id']) ? $add_array['timezone_id']:'';
217
        $data['currency_id'] = isset($add_array['currency_id']) ? $add_array['currency_id']:'';
218
        $data['entity_name'] = $entity_name;
219
        $data['callingcard'] = Common_model::$global_config['system_config']['pinlength'];
220
        $data['edit_id']=$add_array['id'];
221
        $data['form'] = $this->form->build_form($this->accounts_form->get_customer_form_fields($entity_name, $add_array['id']), $add_array);
222
        if ($add_array['id'] != '') {
223
            $data['page_title'] = 'Edit ' . $this->common->get_entity_type('', '', $add_array['type']);
224
            if ($this->form_validation->run() == FALSE) {
225
                $data['validation_errors'] = validation_errors();
226
            } else {
227
                  ASTPP  3.0 
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_DNUMBER
Loading history...
228
                  Password encode
229
                 * **** */
230
				$add_array['password'] = $this->common->encode($add_array['password']);
231
				/*                 * ****************** */
232
				$add_array['credit_limit'] = $this->common_model->add_calculate_currency($add_array['credit_limit'], '', '', false, false);
233
				//Purpose : Get assigned taxes value for customer
234
235
				$query = $this->accounts_model->remove_all_account_tax($add_array['id']);
236
				if (isset($add_array['tax_id'])) {
237
					foreach ($add_array['tax_id'] as $key => $val) {
238
						$data1 = array(
239
							'accountid' => $add_array['id'],
240
							'taxes_id' => $val,
241
						);
242
						$this->accounts_model->add_account_tax($data1);
243
					}
244
					unset($add_array['tax_id']);
245
				}
246
				//Completed
247
				unset($add_array['posttoexternal'],$add_array['number'],$add_array['balance']);
248
				$this->accounts_model->edit_account($add_array, $add_array['id']);
249
				$this->session->set_flashdata('astpp_errormsg', ucfirst($entity_name) . ' updated successfully!');
250
				redirect(base_url() . 'accounts/customer_list/');
251
				exit;
252
			}
253
			$data["account_data"]["0"] = $add_array;
254
			$this->load->view('view_customer_details', $data);
255
		} else {
256
			$data['page_title'] = 'Create ' . $entity_name;
257
			if ($this->form_validation->run() == FALSE) {
258
				$data['validation_errors'] = validation_errors();
259
			} else {
260
				/*                 * ****
261
                  ASTPP  3.0 
262
                  Password encode
263
                 * **** */
264
				$add_array['password'] = $this->common->encode($add_array['password']);
265
				/*                 * ****************** */
266
				$add_array['credit_limit'] = $this->common_model->add_calculate_currency($add_array['credit_limit'], '', '', false, false);
267
				$last_id = $this->accounts_model->add_account($add_array);
268
269
				if (isset($add_array['tax_id'])) {
270
					foreach ($add_array['tax_id'] as $key => $val) {
271
						$data1 = array(
272
							'accountid' => $last_id,
273
							'taxes_id' => $val,
274
						);
275
						$this->accounts_model->add_account_tax($data1);
276
					}
277
					unset($add_array['tax_id']);
278
				}
279
				$this->session->set_flashdata('astpp_errormsg', ucfirst($entity_name) . ' added successfully!');
280
				redirect(base_url() . 'accounts/customer_list/');
281
				exit;
282
			}
283
			$this->load->view('view_accounts_create', $data);
284
		}
285
	}
286
287
	function provider_speeddial($edit_id) {
288
		$this->customer_speeddial($edit_id);
289
	}
290
291
	function customer_speeddial($edit_id) {
292
		//Get Account information from session.
293
		$data['page_title'] = "Speed Dial";
294
		$accountinfo = $this->session->userdata('accountinfo');
295
		//Get Parent informartion
296
		$reseller_id = ($accountinfo['type'] == 1 || $accountinfo['type'] == 5) ? $accountinfo['id'] : 0;
297
		$where = array('id' => $edit_id, "reseller_id" => $reseller_id);
298
		$account = $this->db_model->getSelect("*", "accounts", $where);
299
300
		if ($account->num_rows > 0) {
301
			$account_data = (array) $account->first_row();
302
			$accounttype = strtolower($this->common->get_entity_type('', '', $account_data['type']));
303
			$data['page_title'] = 'Speed Dial';
304
			$data['accounttype'] = $accounttype;
305
			$data['invoice_date'] = $account_data['invoice_day'];
306
			$data["account_data"] = $account_data;
307
			$data['edit_id'] = $edit_id;
308
			$speeddial_res = $this->db->get_where("speed_dial", array("accountid" => $edit_id));
309
			$speeddial_info = array();
310
			if ($speeddial_res->num_rows() > 0) {
311
				$speeddial_res = $speeddial_res->result_array();
312
				foreach ($speeddial_res as $key => $value) {
313
					$speeddial_info[$value['speed_num']] = $value['number'];
314
				}
315
			}
316
			$data['speeddial'] = $speeddial_info;
317
			$data['form'] = $this->form->build_form($this->accounts_form->get_customer_form_fields($accounttype, $edit_id), $account_data);
318
			$this->load->view('view_customer_speed_dial', $data);
319
		} else {
320
			redirect(base_url() . 'accounts/customer_list/');
321
		}
322
	}
323
324
	function customer_speeddial_save($number, $accountid, $speed_num) {
325
		$where = array("accountid" => $accountid);
326
		$account = $this->db_model->getSelect("*", "speed_dial", $where);
327
		if ($account->num_rows() == 0) {
328
			for ($i = 0; $i <= 9; $i++) {
329
				$dest_number = $speed_num == $i ? $number : '';
330
				$data[$i] = array("number" => $dest_number, "speed_num" => $i, 'accountid' => $accountid);
331
			}
332
			$this->db->insert_batch('speed_dial', $data);
333
		} else {
334
			$updateinfo = array('number' => $number);
335
			$this->db->where('speed_num', $speed_num);
336
			$this->db->where('accountid', $accountid);
337
			$result = $this->db->update('speed_dial', $updateinfo);
338
		}
339
	}
340
341
	function customer_speeddial_remove($accountid, $speed_num) {
342
		$updateinfo = array('number' => '');
343
		$this->db->where('speed_num', $speed_num);
344
		$this->db->where('accountid', $accountid);
345
		$result = $this->db->update('speed_dial', $updateinfo);
346
	}
347
348
	function provider_ipmap($edit_id) {
349
		$this->customer_ipmap($edit_id);
350
	}
351
352
	function customer_ipmap($edit_id) {
353
		$data['page_title'] = "IP Settings";
354
		$data['add_form']=true;
355
		//Get Account information from session.
356
		$accountinfo = $this->session->userdata('accountinfo');
357
		//Get Parent informartion
358
		$reseller_id = ($accountinfo['type'] == 1 || $accountinfo['type'] == 5) ? $accountinfo['id'] : 0;
359
		$where = array('id' => $edit_id, "reseller_id" => $reseller_id);
360
		$account_res = $this->db_model->getSelect("type", "accounts", $where);
361
		if ($account_res->num_rows > 0) {
362
			$account_data = (array) $account_res->first_row();
363
			$accounttype = strtolower($this->common->get_entity_type('', '', $account_data['type']));
364
			$data["grid_fields"] = $this->accounts_form->build_ip_list_for_customer($edit_id, $accounttype);
365
			$data['edit_id'] = $edit_id;
366
			$data['accounttype'] = $accounttype;
367
			$this->load->view('view_customer_ipmap', $data);
368
		} else {
369
			redirect(base_url() . 'accounts/customer_list/');
370
			exit;
371
		}
372
	}
373
374
	function customer_ipmap_json($accountid, $accounttype) {
375
		$json_data = array();
376
		$where = array("accountid" => $accountid);
377
		$instant_search=$this->session->userdata('left_panel_search_'.$accounttype.'_ipmap');
378
		$like_str=!empty($instant_search) ? "(name like '%$instant_search%'  OR ip like '%$instant_search%' OR prefix like '%$instant_search%' OR created_date like '%$instant_search%' )" :null;
379
		if(!empty($like_str))
380
		$this->db->where($like_str);
381
		$count_all = $this->db_model->countQuery("*", "ip_map", $where);
382
		$paging_data = $this->form->load_grid_config($count_all, $_GET['rp'], $_GET['page']);
383
		$json_data = $paging_data["json_paging"];
384
		if(!empty($like_str))
385
		$this->db->where($like_str);
386
		$query = $this->db_model->select("*", "ip_map", $where, "id", "ASC", $paging_data["paging"]["page_no"], $paging_data["paging"]["start"]);
387
		$grid_fields = json_decode($this->accounts_form->build_ip_list_for_customer($accountid, $accounttype));
388
		$json_data['rows'] = $this->form->build_grid($query, $grid_fields);
389
        
390
		echo json_encode($json_data);
391
	}
392
393
	function provider_ipmap_action($action, $accountid, $accounttype, $ipmapid = "") {
394
		$this->customer_ipmap_action($action, $accountid, $accounttype, $ipmapid);
395
	}
396
397
	function customer_ipmap_action($action, $accountid, $accounttype, $ipmapid = "") {
398
		$add_array = $this->input->post();
399
		if ($action == "add") {
400
			if ($add_array['ip'] != "") {
401
				$ip = $add_array['ip'];
402
				if (strpos($ip, '/') !== false) {
403
					$add_array['ip'] = $add_array['ip'];
404
				} else {
405
					$add_array['ip'] = $add_array['ip'] . '/32';
406
				}
407
				$where = array("ip" => trim($add_array['ip']), "prefix" => trim($add_array['prefix']));
408
				$getdata = $this->db_model->countQuery("*", "ip_map", $where);
409
				if ($getdata > 0) {
410
					$this->session->set_flashdata('astpp_notification', 'IP already exist in system.');
411
				} else {
412
					if ($accounttype == "provider") {
413
						$add_array['pricelist_id'] = 0;
414
					}
415
					unset($add_array['action']);
416
					$add_array['context'] = 'default';
417
					$add_array['accountid'] = $accountid;
418
					$ip_flag = $this->db->insert("ip_map", $add_array);
419
					if ($ip_flag) {
420
						$this->load->library('freeswitch_lib');
421
						$this->load->module('freeswitch/freeswitch');
422
						$command = "api reloadacl";
423
						$response = $this->freeswitch_model->reload_freeswitch($command);
424
						$this->session->set_userdata('astpp_notification', $response);
425
					}
426
					$this->session->set_flashdata('astpp_errormsg', 'IP added sucessfully.');
427
				}
428
			}
429
		}
430
		if ($action == "delete") {
431
			$ip_flag = $this->db_model->delete("ip_map", array("id" => $ipmapid));
432
			if ($ip_flag) {
433
				$this->load->library('freeswitch_lib');
434
				$this->load->model("freeswitch_model");
435
				$command = "api reloadacl";
436
				$this->freeswitch_model->reload_freeswitch($command);
437
			}
438
			$this->session->set_flashdata('astpp_notification', 'IP removed sucessfully.');
439
		}
440
		redirect(base_url() . "accounts/" . $accounttype . "_ipmap/" . $accountid . "/");
441
	}
442
443
	//This function using for provider t
444
	function provider_animap($edit_id) {
445
		$this->customer_animap($edit_id, 'provider');
446
	}
447
448
	// This function using for showing data of customer/provider CallerID Tab
449
	function customer_animap($edit_id, $entity_type = 'customer') {
450
		//Get Account information from session.
451
		$data['page_title'] = "Caller ID";
452
		$accountinfo = $this->session->userdata('accountinfo');
453
		//Get Parent informartion
454
		$reseller_id = ($accountinfo['type'] == 1 || $accountinfo['type'] == 5) ? $accountinfo['id'] : 0;
455
		$where = array('id' => $edit_id, "reseller_id" => $reseller_id);
456
		$account_res = $this->db_model->getSelect("type", "accounts", $where);
457
		if ($account_res->num_rows > 0) {
458
			$account_data = (array) $account_res->first_row();
459
			$accounttype = strtolower($this->common->get_entity_type('', '', $account_data['type']));
460
			$data["grid_fields"] = $this->accounts_form->build_animap_list_for_customer($edit_id, $accounttype);
461
			$data['edit_id'] = $edit_id;
462
			$data['accounttype'] = $accounttype;
463
			$this->load->view('view_customer_animap', $data);
464
		} else {
465
			redirect(base_url() . 'accounts/customer_list/');
466
			exit;
467
		}
468
	}
469
470
	function customer_animap_json($accountid, $accounttype) {
471
		$json_data = array();
472
		$instant_search=$this->session->userdata('left_panel_search_'.$accounttype.'_animap'); 
473
		$like_str=!empty($instant_search) ? "(number like '%$instant_search%'  OR  creation_date like '%$instant_search%' )" :null;
474
		if(!empty($like_str))
475
		$this->db->where($like_str);
476
		$where = array("accountid" => $accountid);
477
		$count_all = $this->db_model->countQuery("*", "ani_map", $where);
478
479
		$paging_data = $this->form->load_grid_config($count_all, $_GET['rp'], $_GET['page']);
480
		$json_data = $paging_data["json_paging"];
481
		if(!empty($like_str))
482
		$this->db->where($like_str);
483
		$query = $this->db_model->select("*", "ani_map", $where, "id", "ASC", $paging_data["paging"]["page_no"], $paging_data["paging"]["start"]);
484
485
		$grid_fields = json_decode($this->accounts_form->build_animap_list_for_customer($accountid, $accounttype));
486
		$json_data['rows'] = $this->form->build_grid($query, $grid_fields);
487
488
		echo json_encode($json_data);
489
	}
490
491
	function provider_animap_action($action, $accountid, $aniid = "") {
492
		$this->customer_animap_action($action, $accountid, $aniid);
493
	}
494
495
	//This function using for customer/provider callerID add/delete
496
	function customer_animap_action($action, $accountid, $aniid = "") {
497
		$entity_type = $this->common->get_field_name('type', 'accounts', array('id' => $accountid));
498
		$entity_type = strtolower($this->common->get_entity_type('', '', $entity_type));
499
		$url = "accounts/" . $entity_type . "_animap/$accountid/";
500
		if ($action == "add") {
501
			$ani = $this->input->post();
502
			$this->db->where('number', $ani['number']);
503
			$this->db->select('count(id) as count');
504
			$cnt_result = $this->db->get('ani_map');
505
			$cnt_result = $cnt_result->result_array();
506
			$count = $cnt_result[0]['count'];
507
			if ($count == 0) {
508
				if ($ani['number'] != "") {
509
					/*
510
                      ASTPP  3.0 
511
                      Add creation field
512
                     */
513
					$insert_arr = array('creation_date' => gmdate('Y-m-d H:i:s'), "number" => $this->input->post('number'), "accountid" => $accountid,
514
						"context" => "default");
515
					/*                     * *********************************** */
516
					$this->db->insert("ani_map", $insert_arr);
517
					$this->session->set_flashdata('astpp_errormsg', 'Caller ID added successfully!');
518
				} else {
519
					$this->session->set_flashdata('astpp_notification', 'Please Enter Caller ID value.');
520
				}
521
			} else {
522
				$this->session->set_flashdata('astpp_notification', ' Caller ID already Exists.');
523
			}
524
		}
525
		if ($action == "delete") {
526
			$this->session->set_flashdata('astpp_notification', 'Caller ID removed sucessfully!');
527
			$this->db_model->delete("ani_map", array("id" => $aniid));
528
		}
529
		redirect(base_url() . $url);
530
	}
531
532
	//This function using by Customer/Provider edit tab
533
	function customer_details_json($module, $accountid) {
534
		$entity_type = $this->common->get_field_name('type', 'accounts', array('id' => $accountid));
535
		$entity_type = strtolower($this->common->get_entity_type('', '', $entity_type));
536
		if ($module == "pattern") {
537
			$this->load->module('rates/rates');
538
			$this->rates->customer_block_pattern_list($accountid, $entity_type);
539
		}
540
		if ($module == "freeswitch") {
541
			$this->load->module('freeswitch/freeswitch');
542
			$this->freeswitch->customer_fssipdevices_json($accountid, $entity_type);
543
		}
544
		if ($module == "did") {
545
			$this->load->module('did/did');
546
			$this->did->customer_did($accountid, $entity_type);
547
		}
548
		if ($module == "invoices") {
549
			$this->load->module('invoices/invoices');
550
			$this->invoices->customer_invoices($accountid,$entity_type, false);
551
		}
552
		if ($module == "subscription") {
553
			$this->load->module('charges/charges');
554
			$this->charges->customer_charge_list($accountid, $entity_type);
555
		}
556
		if ($module == "cdrs") {
557
			$this->load->module('reports/reports');
558
			$this->reports->customer_cdrreport($accountid, $entity_type);
559
		}
560
		if ($module == "charges") {
561
			$this->load->module('reports/reports');
562
			$this->reports->customer_charge_history($accountid,$entity_type);
563
		}
564
		if ($module == "refill") {
565
			$this->load->module('reports/reports');
566
			$this->reports->customer_refillreport($accountid,$entity_type);
567
		}
568
		if ($module == "emailhistory") {
569
			$this->load->module('email/email');
570
			$this->email->customer_mail_record($accountid, $entity_type);
571
		}
572
		if ($module == "opensips") {
573
			$this->load->module('opensips/opensips');
574
			$this->opensips->customer_opensips_json($accountid,$entity_type);
575
		}
576
	}
577
	function customer_details_search($module_name){
578
		$action = $this->input->post();
579
		$this->session->set_userdata('left_panel_search_'.$module_name,"");
580
		if(!empty($action['left_panel_search'])){
581
			$this->session->set_userdata('left_panel_search_'.$module_name, $action['left_panel_search']);
582
		}
583
	}
584
	function provider_sipdevices($edit_id) {
585
		$this->customer_sipdevices($edit_id);
586
	}
587
588
	function customer_sipdevices($edit_id) {
589
		$data['page_title'] = "SIP Devices";
590
		//Get Account information from session.
591
		$accountinfo = $this->session->userdata('accountinfo');
592
		//Get Parent informartion
593
		$reseller_id = ($accountinfo['type'] == 1 || $accountinfo['type'] == 5) ? $accountinfo['id'] : 0;
594
		$where = array('id' => $edit_id, "reseller_id" => $reseller_id);
595
		$account_res = $this->db_model->getSelect("type", "accounts", $where);
596
		if ($account_res->num_rows > 0) {
597
			$account_data = (array) $account_res->first_row();
598
			$accounttype = strtolower($this->common->get_entity_type('', '', $account_data['type']));
599
			$this->load->module('freeswitch/freeswitch');
600
			$data["grid_buttons"] = $this->freeswitch->freeswitch_form->fsdevices_build_grid_buttons($edit_id, $accounttype);
601
			$data['grid_fields'] = $this->freeswitch->freeswitch_form->build_devices_list_for_customer();
602
			$data['edit_id'] = $edit_id;
603
			$data['accounttype'] = $accounttype;
604
			$this->load->view('view_customer_sipdevices', $data);
605
		} else {
606
			redirect(base_url() . 'accounts/customer_list/');
607
			exit;
608
		}
609
	}
610
611
	function provider_fssipdevices_action($action, $id, $accountid) {
612
		$this->customer_fssipdevices_action($action, $id, $accountid);
613
	}
614
615
	function customer_fssipdevices_action($action, $id, $accountid) {
616
		$entity_type = $this->common->get_field_name('type', 'accounts', array('id' => $accountid));
617
		$entity_type = strtolower($this->common->get_entity_type('', '', $entity_type));
618
		$this->load->module('freeswitch/freeswitch');
619
		if ($action == "delete") {
620
			$this->freeswitch->freeswitch_model->delete_freeswith_devices($id);
621
			$this->session->set_flashdata('astpp_notification', 'Sip Device removed successfully!');
622
			redirect(base_url() . "accounts/" . $entity_type . "_sipdevices/$accountid/");
623
		}
624
		if ($action == "edit") {
625
			$this->freeswitch->customer_fssipdevices_edit($id, $accountid);
626
			$this->session->set_flashdata('astpp_errormsg', 'Sip updated successfully!');
627
		}
628
	}
629
630
	function provider_opensips($edit_id) {
631
		$this->customer_opensips($edit_id);
632
	}
633
634
	function customer_opensips($edit_id) {
635
		$data['page_title'] = "Opensips Devices";
636
		//Get Account information from session.
637
		$accountinfo = $this->session->userdata('accountinfo');
638
		//Get Parent informartion
639
		$reseller_id = ($accountinfo['type'] == 1 || $accountinfo['type'] == 5) ? $accountinfo['id'] : 0;
640
		$where = array('id' => $edit_id, "reseller_id" => $reseller_id);
641
		$account_res = $this->db_model->getSelect("type", "accounts", $where);
642
		if ($account_res->num_rows > 0) {
643
			$account_data = (array) $account_res->first_row();
644
			$accounttype = strtolower($this->common->get_entity_type('', '', $account_data['type']));
645
			$this->load->module('freeswitch/freeswitch');
646
			$this->load->module('opensips/opensips');
647
			$data["grid_buttons"] = $this->opensips->opensips_form->opensips_customer_build_grid_buttons($edit_id);
648
			$data['grid_fields'] = $this->opensips->opensips_form->opensips_customer_build_opensips_list($edit_id);
649
			$data['edit_id'] = $edit_id;
650
			$data['accounttype'] = $accounttype;
651
			$this->load->view('view_customer_opensips_devices', $data);
652
		} else {
653
			redirect(base_url() . 'accounts/customer_list/');
654
			exit;
655
		}
656
	}
657
658
	function customer_opensips_action($action, $accountid, $id) {
659
		$entity_type = $this->common->get_field_name('type', 'accounts', array('id' => $accountid));
660
		$entity_type = strtolower($this->common->get_entity_type('', '', $entity_type));
661
		$url = "accounts/" . $entity_type . "_opensips/$accountid/";
662
		$this->load->module('opensips/opensips');
663
		if ($action == "delete") {
664
			$this->opensips->opensips_model->remove_opensips($id);
665
			$this->session->set_flashdata('astpp_notification', 'Opensips removed successfully!');
666
			redirect(base_url() . $url);
667
		}
668
		if ($action == "edit") {
669
			$this->opensips->customer_opensips_edit($accountid, $id);
670
			$this->session->set_flashdata('astpp_errormsg', 'Opensips updated successfully!');
671
		}
672
	}
673
674
	function provider_charges($edit_id) {
675
		$this->customer_charges($edit_id);
676
	}
677
678
	function reseller_charges($edit_id) {
679
		$this->customer_charges($edit_id);
680
	}
681
682
	function customer_charges($edit_id) {
683
		$data['page_title'] = "Charges History";
684
		//Get Account information from session.
685
		$accountinfo = $this->session->userdata('accountinfo');
686
		//Get Parent informartion
687
		$reseller_id = ($accountinfo['type'] == 1 || $accountinfo['type'] == 5) ? $accountinfo['id'] : 0;
688
		$where = array('id' => $edit_id, "reseller_id" => $reseller_id);
689
		$account_res = $this->db_model->getSelect("type", "accounts", $where);
690
		if ($account_res->num_rows > 0) {
691
			$account_data = (array) $account_res->first_row();
692
			$accounttype = strtolower($this->common->get_entity_type('', '', $account_data['type']));
693
			$data['chargelist'] = form_dropdown('applayable_charge', $this->Astpp_common->list_applyable_charges($edit_id), '');
694
			$this->load->module('reports/reports');
695
			$data['grid_fields'] = $this->reports->reports_form->build_charge_list_for_customer($edit_id, $accounttype);
696
			$data['edit_id'] = $edit_id;
697
			$data['accounttype'] = $accounttype;
698
			$this->load->view('view_customer_charges', $data);
699
		} else {
700
			redirect(base_url() . 'accounts/customer_list/');
701
			exit;
702
		}
703
	}
704
705
	function provider_subscription($edit_id) {
706
		$this->customer_subscription($edit_id);
707
	}
708
709
	function reseller_subscription($edit_id) {
710
		$this->customer_subscription($edit_id);
711
	}
712
713
	function customer_subscription($edit_id) {
714
		$data['page_title'] = "Subscription";
715
		//Get Account information from session.
716
		$accountinfo = $this->session->userdata('accountinfo');
717
		//Get Parent informartion
718
		$reseller_id = ($accountinfo['type'] == 1 || $accountinfo['type'] == 5) ? $accountinfo['id'] : 0;
719
		$where = array('id' => $edit_id, "reseller_id" => $reseller_id);
720
		$account_res = $this->db_model->getSelect("type", "accounts", $where);
721
		if ($account_res->num_rows > 0) {
722
			$account_data = (array) $account_res->first_row();
723
			$accounttype = strtolower($this->common->get_entity_type('', '', $account_data['type']));
724
			$data['chargelist'] = form_dropdown_all(array("name" => 'applayable_charge', 'id' => 'applayable_charge', 'class' => ""), $this->Astpp_common->list_applyable_charges($edit_id), '');
725
			$this->load->module('charges/charges');
726
			$data['grid_fields'] = $this->charges->charges_form->build_charges_list_for_customer($edit_id, $accounttype);
727
			$data['edit_id'] = $edit_id;
728
			$data['accounttype'] = $accounttype;
729
			$this->load->view('view_customer_subscriptions', $data);
730
		} else {
731
			redirect(base_url() . 'accounts/customer_list/');
732
			exit;
733
		}
734
	}
735
736
	function reseller_subscription_action($action, $accountid, $accounttype, $chargeid = "") {
737
		$this->customer_subscription_action($action, $accountid, $accounttype, $chargeid);
738
	}
739
740
	function provider_subscription_action($action, $accountid, $accounttype, $chargeid = "") {
741
		$this->customer_subscription_action($action, $accountid, $accounttype, $chargeid);
742
	}
743
744
	function customer_subscription_action($action, $accountid, $accounttype, $chargeid = "") {
745
746
		/**
747
          ASTPP  3.0 
748
          For Email Broadcast when Subscription Add - Delete
749
		 * */
750
		$accountinfo = $this->db_model->getSelect("*", "accounts", array("id" => $accountid));
751
		$accountinfo = (array) $accountinfo->first_row();
752
		/*         * ****************************** */
753
		if ($action == "add") {
754
			$charge_id = $this->input->post("applayable_charge", true);
755
			if ($charge_id != "") {
756
				$insert_arr = array("charge_id" => $charge_id, "accountid" => $accountid, "assign_date" => gmdate("Y-m-d H:i:s"));
757
				$this->db->insert("charge_to_account", $insert_arr);
758
				$this->session->set_flashdata('astpp_errormsg', 'Subscripton Added Sucessfully.');
759
				$template_name = 'add_subscription';
760
			}
761
		}
762
		if ($action == "delete") {
763
			$this->db_model->delete("charge_to_account", array("id" => $chargeid));
764
			$this->session->set_flashdata('astpp_notification', 'Subscription Removed Sucessfully.');
765
			$template_name = 'remove_subscription';
766
		}
767
		/**
768
          ASTPP  3.0 
769
          For Email Broadcast when Subscription Delete
770
		 * */
771
		$this->common->mail_to_users($template_name, $accountinfo);
772
		/*         * ********************************* */
773
		redirect(base_url() . "accounts/" . $accounttype . "_subscription/$accountid/");
774
	}
775
776
	function provider_dids($edit_id) {
777
		$this->customer_dids($edit_id);
778
	}
779
780
	function customer_dids($edit_id) {
781
		$data['page_title'] = "DID";
782
		//Get Account information from session.
783
		$accountinfo = $this->session->userdata('accountinfo');
784
		//Get Parent informartion
785
		$reseller_id = ($accountinfo['type'] == 1 || $accountinfo['type'] == 5) ? $accountinfo['id'] : 0;
786
		$where = array('id' => $edit_id, "reseller_id" => $reseller_id);
787
		$account_res = $this->db_model->getSelect("type,country_id", "accounts", $where);
788
		if ($account_res->num_rows > 0) {
789
			$account_data = (array) $account_res->first_row();
790
			$accounttype = strtolower($this->common->get_entity_type('', '', $account_data['type']));
791
			$this->load->module('did/did');
792
			$data['grid_fields'] = $this->did->did_form->build_did_list_for_customer($edit_id, $accounttype);
793
			$data['edit_id'] = $edit_id;
794
			$data['accounttype'] = $accounttype;
795
			$result_did_final = array();
796
			$did_info = array("name" => "free_didlist", "id" => "free_didlist", "class" => "free_didlist");
797
			$data['didlist'] = form_dropdown_all($did_info, $result_did_final, '');
798
			$data['country_id'] = $account_data['country_id'];
799
			$this->load->view('view_customer_dids', $data);
800
		} else {
801
			redirect(base_url() . 'accounts/customer_list/');
802
			exit;
803
		}
804
	}
805
806
	function provider_dids_action($action, $accountid, $accounttype, $did_id = "") {
807
		$this->customer_dids_action($action, $accountid, $accounttype, $did_id);
808
	}
809
810
	function reseller_dids_action($action, $accountid, $accounttype, $did_id = "") {
811
		$this->customer_dids_action($action, $accountid, $accounttype, $did_id);
812
	}
813
814
	function customer_dids_action($action, $accountid, $accounttype, $did_id = "") {
815
		//Get account information from session.
816
		$accountinfo = $this->session->userdata('accountinfo');
817
		//Get Parent informartion
818
		$reseller_id = $accountinfo['type'] == 1 ? $accountinfo['id'] : 0;
819
		$did_id = empty($did_id) ? $this->input->post("free_didlist", true) : $did_id;
820
		if ($did_id != "") {
821
			$account_arr = (array)$this->db->get_where("accounts", array("id" => $accountid))->first_row();
822
			$did_arr = (array) $this->db->get_where("dids", array("id" => $did_id))->first_row();
823
			$field_name = $account_arr['type'] ==1 ? "parent_id" : 'accountid';
824
			if ($action == "add") {
825
				if ($did_arr['accountid'] == 0 && $did_arr['parent_id'] == $reseller_id) {
826
					if ($accountinfo['type'] == 1) {
827
						//for getting reseller setup price if reseller customer purchase
828
						$reseller_pricing_query = $this->db_model->getSelect("*", "reseller_pricing", array("note" => $did_arr['number'], 'reseller_id' => $reseller_id));
829
						if ($reseller_pricing_query->num_rows() > 0) {
830
							$reseller_pricing = (array) $reseller_pricing_query->first_row();
831
							$did_arr=array_merge($did_arr,$reseller_pricing);
832
						}
833
					}
834
					$available_bal = $this->db_model->get_available_bal($account_arr);
835
					if ($available_bal >= $did_arr['setup']) {
836
						$available_bal = $this->db_model->update_balance($did_arr['setup'], $accountid, "debit");
837
						$this->db_model->update("dids", array($field_name => $accountid, "assign_date" => gmdate("Y-m-d H:i:s")), array("id" => $did_id));
838
						if ($account_arr['type'] == 1) {
839
							$this->load->module('did/did');
840
							$this->did->did_model->insert_reseller_pricing($account_arr, $did_arr);
841
						}
842
						$this->common->add_invoice_details($account_arr, "DIDCHRG", $did_arr['setup'], $did_arr['number']);
843
						$account_arr['did_number'] = $did_arr['number'];
844
						$account_arr['did_country_id'] = $this->common->get_field_name('country', 'countrycode', $did_arr['country_id']);
845
			$account_arr['did_setup'] = $this->common_model->to_calculate_currency($did_arr['setup'], "", '', true, true);
846
			$account_arr['did_monthlycost'] = $this->common_model->to_calculate_currency($did_arr['monthlycost'], "", '', true, true);
847
			$account_arr['did_maxchannels'] = $did_arr['maxchannels'];
848
			
849
						$this->common->mail_to_users('email_add_did', $account_arr);
850
						$this->session->set_flashdata('astpp_errormsg', 'DID Purchased Successfully.');
851
					} else {
852
						$this->session->set_flashdata('astpp_notification', 'Insuffiecient fund to purchase this DID');
853
					}
854
				} else {
855
					$this->session->set_flashdata('astpp_notification', 'This DID already purchased by someone.');
856
				}
857
			}
858
			if ($action == "delete") {
859
				$data = array("accountid" => "0", "assign_date" => "0000-00-00 00:00:00", "charge_upto" => "0000-00-00 00:00:00");
860
				if ($accounttype == 'reseller') {
861
					$data = array("accountid" => "0", "parent_id" => $reseller_id, "assign_date" => "0000-00-00 00:00:00", "charge_upto" => "0000-00-00 00:00:00");
862
				}
863
				$this->db_model->update("dids", $data, array("id" => $did_id));
864
				if ($accounttype == 'reseller') {
865
					$this->db->where('reseller_id', $account_arr['id']);
866
					$this->db->where('note', $did_arr['number']);
867
					$this->db->delete('reseller_pricing');
868
				}
869
				$account_arr['did_number']=$did_arr['number'];
870
				$this->common->mail_to_users('email_remove_did', $account_arr);
871
				$this->session->set_flashdata('astpp_notification', 'Did Removed Successfully.');
872
			}
873
		}
874
		redirect(base_url() . "accounts/" . $accounttype . "_dids/$accountid/");
875
	}
876
877
	function provider_invoices($edit_id) {
878
		$this->customer_invoices($edit_id);
879
	}
880
881
	function reseller_invoices($edit_id) {
882
		$this->customer_invoices($edit_id);
883
	}
884
885
	function customer_invoices($edit_id) {
886
		$data['page_title'] = "Invoice";
887
		//Get Account information from session.
888
		$accountinfo = $this->session->userdata('accountinfo');
889
		//Get Parent informartion
890
		$reseller_id = ($accountinfo['type'] == 1 || $accountinfo['type'] == 5) ? $accountinfo['id'] : 0;
891
		$where = array('id' => $edit_id, "reseller_id" => $reseller_id);
892
		$account_res = $this->db_model->getSelect("type", "accounts", $where);
893
		if ($account_res->num_rows > 0) {
894
			$account_data = (array) $account_res->first_row();
895
			$accounttype = strtolower($this->common->get_entity_type('', '', $account_data['type']));
896
			$this->load->module('invoices/invoices');
897
			$data['grid_fields'] = $this->invoices->invoices_form->build_invoices_list_for_customer_admin(false);
898
			$data['edit_id'] = $edit_id;
899
			$data['accounttype'] = $accounttype;
900
			$this->load->view('view_customer_invoices', $data);
901
		} else {
902
			redirect(base_url() . 'accounts/customer_list/');
903
			exit;
904
		}
905
	}
906
907
	function provider_blocked_prefixes($edit_id) {
908
		$this->customer_blocked_prefixes($edit_id);
909
	}
910
911
	function customer_blocked_prefixes($edit_id) {
912
		$data['page_title'] = "Blocked Codes";
913
		//Get Account information from session.
914
		$accountinfo = $this->session->userdata('accountinfo');
915
		//Get Parent informartion
916
		$reseller_id = ($accountinfo['type'] == 1 || $accountinfo['type'] == 5) ? $accountinfo['id'] : 0;
917
		$where = array('id' => $edit_id, "reseller_id" => $reseller_id);
918
		$account_res = $this->db_model->getSelect("type", "accounts", $where);
919
		if ($account_res->num_rows > 0) {
920
			$account_data = (array) $account_res->first_row();
921
			$accounttype = strtolower($this->common->get_entity_type('', '', $account_data['type']));
922
			$this->load->module('rates/rates');
923
			$data['grid_fields'] = $this->rates->rates_form->build_pattern_list_for_customer($edit_id, $accounttype);
924
			$data['grid_buttons'] = $this->rates->rates_form->set_pattern_grid_buttons($edit_id);
925
			$data['edit_id'] = $edit_id;
926
			$data['accounttype'] = $accounttype;
927
			$this->load->view('view_customer_blocked_prefixes', $data);
928
		} else {
929
			redirect(base_url() . 'accounts/customer_list/');
930
			exit;
931
		}
932
	}
933
934
	function customer_add_blockpatterns($accountid) {
935
		$data['username'] = $this->session->userdata('user_name');
936
		$data['page_title'] = 'Unblocked Codes';
937
		$this->session->set_userdata('advance_search', 0);
938
		$this->load->module('rates/rates');
939
		$data['patters_grid_fields'] = $this->rates->rates_form->build_block_pattern_list_for_customer();
940
		$data["accountid"] = $accountid;
941
		$this->load->view('view_block_prefix_list', $data);
942
	}
943
944
	function customer_add_blockpatterns_json($accountid) {
945
		$this->load->module('rates/rates');
946
		$json_data = array();
947
		$count_all = $this->rates_model->getunblocked_pattern_list($accountid, false);
948
		$paging_data = $this->form->load_grid_config($count_all, $_GET['rp'], $_GET['page']);
949
		$json_data = $paging_data["json_paging"];
950
951
		$query = $this->rates->rates_model->getunblocked_pattern_list($accountid, true, $paging_data["paging"]["start"], $paging_data["paging"]["page_no"]);
952
		$grid_fields = json_decode($this->rates->rates_form->build_block_pattern_list_for_customer());
953
		$json_data['rows'] = $this->rates->form->build_grid($query, $grid_fields);
954
		echo json_encode($json_data);
955
	}
956
957
	function customer_block_prefix($accountid) {
958
		$result = $this->accounts_model->insert_block($this->input->post('prefixies', true), $accountid);
959
		echo $result;
960
		exit;
961
	}
962
963
	function provider_delete_block_pattern($accountid, $patternid) {
964
		$this->customer_delete_block_pattern($accountid, $patternid);
965
	}
966
967
	function customer_delete_block_pattern($accountid, $patternid) {
968
		$entity_type = $this->common->get_field_name('type', 'accounts', array('id' => $accountid));
969
		$entity_type = strtolower($this->common->get_entity_type('', '', $entity_type));
970
		$url = "accounts/" . $entity_type . "_blocked_prefixes/$accountid";
971
		$this->db_model->delete("block_patterns", array("id" => $patternid));
972
		$this->session->set_flashdata('astpp_notification', 'Block Code Removed Sucessfully!');
973
        
974
		redirect(base_url() . $url);
975
	}
976
	function customer_blockedprefixes_delete_multiple(){
977
		$ids = $this->input->post("selected_ids", true);
978
		$where = "id IN ($ids)";
979
		$this->db->delete("block_patterns",$where);
980
		echo TRUE;
981
	}
982
983
	/* ASTPP  3.0 
984
     * Using function using for provider cdrs tab
985
     */
986
987
	function provider_cdrs($edit_id) {
988
		$this->customer_cdrs($edit_id);
989
	}
990
991
	/*     * ************************************************ */
992
	/* ASTPP  3.0 
993
     * Using function using for reseller cdrs tab
994
     */
995
996
	function reseller_cdrs($edit_id) {
997
		$this->customer_cdrs($edit_id);
998
	}
999
1000
	/*     * ************************************************ */
1001
	/* ASTPP  3.0 
1002
     * Using function using for customer cdrs tab
1003
     */
1004
1005
	function customer_cdrs($edit_id) {
1006
		$data['page_title'] = "CDRs";
1007
		//Get Account information from session.
1008
		$accountinfo = $this->session->userdata('accountinfo');
1009
		//Get Parent informartion
1010
		$reseller_id = ($accountinfo['type'] == 1 || $accountinfo['type'] == 5) ? $accountinfo['id'] : 0;
1011
		$where = array('id' => $edit_id, "reseller_id" => $reseller_id);
1012
		$account_res = $this->db_model->getSelect("type", "accounts", $where);
1013
		if ($account_res->num_rows > 0) {
1014
			$account_data = (array) $account_res->first_row();
1015
			$accounttype = strtolower($this->common->get_entity_type('', '', $account_data['type']));
1016
			$this->load->module('reports/reports');
1017
			$data['grid_fields'] = $this->reports->reports_form->build_report_list_for_user($accounttype);
1018
			$data['edit_id'] = $edit_id;
1019
			$data['accounttype'] = $accounttype;
1020
			$this->load->view('view_customer_cdrs_list', $data);
1021
		} else {
1022
			redirect(base_url() . 'accounts/customer_list/');
1023
			exit;
1024
		}
1025
	}
1026
1027
	/*     * ***************************************** */
1028
	/* ASTPP  3.0 
1029
     * Using for provider refillreporttab.
1030
     */
1031
	function reseller_packages($edit_id){
1032
	   $data['page_title'] = "Packages";
1033
	   $accountinfo = $this->session->userdata('accountinfo');
1034
	   $reseller_id = ($accountinfo['type'] == 1 || $accountinfo['type'] == 5) ? $accountinfo['id'] : 0;
1035
	   $where = array('id' => $edit_id, "reseller_id" => $reseller_id);
1036
	   $account_res = $this->db_model->getSelect("type", "accounts", $where);
1037
	   if ($account_res->num_rows > 0) {
1038
			$account_data = (array) $account_res->first_row();
1039
			$accounttype = strtolower($this->common->get_entity_type('', '', $account_data['type']));
1040
			$this->load->module('package/package');
1041
			$data['grid_fields'] = $this->package->package_form->build_package_list_for_reseller();
1042
			$data['edit_id'] = $edit_id;
1043
			$data['accounttype'] = $accounttype;
1044
			$this->load->view('view_reseller_package_list', $data);
1045
	   }else {
1046
			redirect(base_url() . 'accounts/reseller_list/');
1047
			exit;
1048
	   }
1049
	} 
1050
     
1051
	function reseller_refillreport($edit_id){
1052
	  $this->customer_refillreport($edit_id);
1053
	}
1054
	function provider_refillreport($edit_id) {
1055
		$this->customer_refillreport($edit_id);
1056
	}
1057
1058
	/*     * ***************************************** */
1059
	/* ASTPP  3.0 
1060
     * Using for customer refill report tab.
1061
     */
1062
1063
	function customer_refillreport($edit_id) {
1064
		$data['page_title'] = "Refill Report";
1065
		//Get Account information from session.
1066
		$accountinfo = $this->session->userdata('accountinfo');
1067
		//Get Parent informartion
1068
		$reseller_id = ($accountinfo['type'] == 1 || $accountinfo['type'] == 5) ? $accountinfo['id'] : 0;
1069
		$where = array('id' => $edit_id, "reseller_id" => $reseller_id);
1070
		$account_res = $this->db_model->getSelect("type", "accounts", $where);
1071
		if ($account_res->num_rows > 0) {
1072
			$account_data = (array) $account_res->first_row();
1073
			$accounttype = strtolower($this->common->get_entity_type('', '', $account_data['type']));
1074
			$this->load->module('reports/reports');
1075
			$data['grid_fields'] = $this->reports->reports_form->build_refillreport_for_customer();
1076
			$data['edit_id'] = $edit_id;
1077
			$data['accounttype'] = $accounttype;
1078
			$this->load->view('view_customer_refill_report', $data);
1079
		} else {
1080
			redirect(base_url() . 'accounts/customer_list/');
1081
			exit;
1082
		}
1083
	}
1084
1085
	/*     * ***************************************** */
1086
	/* ASTPP  3.0 
1087
     * Using for provider email history tab.
1088
     */
1089
	function reseller_emailhistory($edit_id) {
1090
		$this->customer_emailhistory($edit_id);
1091
	}
1092
	function provider_emailhistory($edit_id) {
1093
		$this->customer_emailhistory($edit_id);
1094
	}
1095
1096
	/*     * ***************************************** */
1097
	/* ASTPP  3.0 
1098
     * Using for customer email history tab.
1099
     */
1100
1101
	function customer_emailhistory($edit_id) {
1102
		$data['page_title'] = "Emails";
1103
		//Get Account information from session.
1104
		$accountinfo = $this->session->userdata('accountinfo');
1105
		//Get Parent informartion
1106
		$reseller_id = ($accountinfo['type'] == 1 || $accountinfo['type'] == 5) ? $accountinfo['id'] : 0;
1107
		$where = array('id' => $edit_id, "reseller_id" => $reseller_id);
1108
		$account_res = $this->db_model->getSelect("type", "accounts", $where);
1109
		if ($account_res->num_rows > 0) {
1110
			$account_data = (array) $account_res->first_row();
1111
			$accounttype = strtolower($this->common->get_entity_type('', '', $account_data['type']));
1112
			$this->load->module('email/email');
1113
			$data['grid_fields'] = $this->email->email_form->build_list_for_email_customer($edit_id, $accounttype);
1114
			$data['edit_id'] = $edit_id;
1115
			$data['accounttype'] = $accounttype;
1116
			$this->load->view('view_customer_email_history', $data);
1117
		} else {
1118
			redirect(base_url() . 'accounts/customer_list/');
1119
			exit;
1120
		}
1121
	}
1122
1123
	/*     * *********************************************** */
1124
	/* ASTPP  3.0 
1125
     * Using for provider Alert threshold history tab.
1126
     */
1127
1128
	function reseller_alert_threshold($edit_id) {
1129
		$this->customer_alert_threshold($edit_id);
1130
	}
1131
	function provider_alert_threshold($edit_id) {
1132
		$this->customer_alert_threshold($edit_id);
1133
	}
1134
1135
	/*     * *********************************************** */
1136
	/* ASTPP  3.0 
1137
     * Using for customer Alert threshold history tab.
1138
     */
1139
1140
	function customer_alert_threshold($edit_id) {
1141
		$data['page_title'] = "Alert Threshold";
1142
		//Get Account information from session.
1143
		$accountinfo = $this->session->userdata('accountinfo');
1144
		//Get Parent informartion
1145
		$reseller_id = ($accountinfo['type'] == 1 || $accountinfo['type'] == 5) ? $accountinfo['id'] : 0;
1146
		$where = array('id' => $edit_id, "reseller_id" => $reseller_id);
1147
		$account_res = $this->db_model->getSelect("notify_credit_limit,notify_flag,notify_email,type,id", "accounts", $where);
1148
		if ($account_res->num_rows > 0) {
1149
			$account_data = (array) $account_res->first_row();
1150
			$accounttype = strtolower($this->common->get_entity_type('', '', $account_data['type']));
1151
			unset($account_data['type']);
1152
			$data['form'] = $this->form->build_form($this->accounts_form->customer_alert_threshold($accounttype), $account_data);
1153
			$data['edit_id'] = $edit_id;
1154
			$data['accounttype'] = $accounttype;
1155
			$this->load->view('view_customer_alert_threshold', $data);
1156
		} else {
1157
			redirect(base_url() . 'accounts/customer_list/');
1158
			exit;
1159
		}
1160
	}
1161
	function reseller_alert_threshold_save($entity_type) {
1162
		$this->customer_alert_threshold_save($entity_type);
1163
	}
1164
	function provider_alert_threshold_save($entity_type) {
1165
		$this->customer_alert_threshold_save($entity_type);
1166
	}
1167
1168
	function customer_alert_threshold_save($entity_type) {
1169
		$add_array = $this->input->post();
1170
		if (isset($add_array['id']) && !empty($add_array['id'])) {
1171
			$data['page_title'] = "Alert Threshold";
1172
			$data['form'] = $this->form->build_form($this->accounts_form->customer_alert_threshold($entity_type), $add_array);
1173
			$data['edit_id'] = $add_array['id'];
1174
			$data['accounttype'] = $entity_type;
1175
			//Get Account information from session.
1176
			$accountinfo = $this->session->userdata('accountinfo');
1177
			//Get Parent informartion
1178
			$reseller_id = ($accountinfo['type'] == 1 || $accountinfo['type'] == 5) ? $accountinfo['id'] : 0;
1179
			$where = array('id' => $add_array['id'], "reseller_id" => $reseller_id);
1180
			$account_res = $this->db_model->getSelect("type", "accounts", $where);
1181
			if ($account_res->num_rows > 0) {
1182
				if ($this->form_validation->run() == FALSE) {
1183
					$data['validation_errors'] = validation_errors();
1184
				} else {
1185
					$this->db->where('id', $add_array['id']);
1186
					$id=$add_array['id'];
1187
					unset($add_array['id'], $add_array['action']);
1188
					$this->db->update('accounts', $add_array);
1189
					$this->session->set_flashdata('astpp_errormsg','Alert threshold updated successfully!');
1190
					redirect(base_url() . 'accounts/'.$entity_type.'_alert_threshold/'.$id."/");
1191
					exit;
1192
				}
1193
			} else {
1194
				redirect(base_url() . 'accounts/'.$entity_type.'_list/');
1195
				exit;
1196
			}
1197
			$this->load->view('view_customer_alert_threshold', $data);
1198
		} else {
1199
			redirect(base_url() . 'accounts/customer_list/');
1200
			exit;
1201
		}
1202
	}
1203
1204
	function customer_bulk_creation() {
1205
		$type=0;
1206
		$data['entity_name'] = strtolower($this->common->get_entity_type('', '', $type));
1207
		$data['page_title'] = 'Mass Customer';
1208
		$data['country_id'] = Common_model::$global_config['system_config']['country'];
1209
		$data['currency_id'] = $this->common->get_field_name('id', 'currency', array('currency' => Common_model::$global_config['system_config']['base_currency']));
1210
		$data['timezone_id'] = Common_model::$global_config['system_config']['default_timezone'];
1211
		if (!$data['timezone_id']) {
1212
			$data['timezone_id'] = 1;
1213
		}
1214
		if (!$data['currency_id']) {
1215
			$data['currency_id'] = 1;
1216
		}
1217
		if (!$data['country_id']) {
1218
			$data['country_id'] = 1;
1219
		}
1220
		$data['form'] = $this->form->build_form($this->accounts_form->customer_bulk_generate_form(), '');
1221
		$this->load->view('view_bulk_account_creation', $data);
1222
	}
1223
1224
	function customer_bulk_save() {
1225
		$data['page_title'] = 'Create Bulk Customer';
1226
		$add_array = $this->input->post();
1227
		if (!empty($add_array) && isset($add_array)) {
1228
			$currentlength = $this->accounts_model->get_max_limit($add_array);
1229
			$account_data = $this->session->userdata("accountinfo");
1230
			$add_array['reseller_id'] = $account_data['type']==1 ? $account_data['id']:0;
1231
			$data['form'] = $this->form->build_form($this->accounts_form->customer_bulk_generate_form(), $add_array);
1232
			if ($this->form_validation->run() == FALSE) {
1233
				$data['validation_errors'] = validation_errors();
1234
				echo $data['validation_errors'];
1235
				exit;
1236
			}
1237
			if($currentlength <= 0){
1238
		echo json_encode(array("prefix_error" => "Your Account Limit has been reached.Please Change Your Prefix."));
1239
				exit;
1240
			}
1241
			if ($add_array['account_length'] <= strlen($add_array['prefix'])) {
1242
				echo json_encode(array("account_length_error" => "Please Enter Proper Account Length."));
1243
				exit;
1244
			}
1245
			if ($currentlength > 0 && $add_array['count'] > $currentlength) {
1246
				echo json_encode(array("count_error" => "You Can Create Maximum " . $currentlength . " accounts with " . $add_array['prefix'] . " prefix"));
1247
				exit;
1248
			} else {
1249
				$this->accounts_model->bulk_insert_accounts($add_array);
1250
				echo json_encode(array("SUCCESS" => "Bulk customer generate successfully!"));
1251
				exit;
1252
			}
1253
		} else {
1254
			redirect(base_url() . "accounts/customer_list/");
1255
		}
1256
	}
1257
1258
	function customer_invoice_option($value = false) {
1259
		$sweepid = $this->input->post("sweepid", true);
1260
		$invoice_dropdown = $this->common->set_invoice_option($sweepid, "", "", $value);
1261
		echo $invoice_dropdown;
1262
	}
1263
1264
	/*     * *****
1265
      ASTPP  3.0 
1266
      in customer Edit put one dropdown with the name of country and change with Purchase dropdown.
1267
     * ***** */
1268
1269
	function customer_did_country() {
1270
		$country_id = $_POST['country_id'];
1271
		$accountid= $this->input->post('accountid',true);
1272
		$accountinfo = $this->session->userdata("accountinfo");
1273
		$entity_info=(array)$this->db->get_where('accounts',array("id"=>$accountid))->first_row();
1274
		$reseller_id = $accountinfo['type'] == 1 ? $accountinfo['id'] : 0;
1275
		if ($entity_info['reseller_id'] > 0) {
1276
			$parent_id = $accountinfo['type'] == 1 ? $accountinfo['id'] : $accountinfo['reseller_id'];
1277
			$query = 'select dids.id as id,dids.number as number,reseller_pricing.setup as setup, reseller_pricing.monthlycost as monthlycost from dids,reseller_pricing where dids.number= reseller_pricing.note and reseller_pricing.reseller_id =' . $parent_id . " and dids.accountid=0 and dids.country_id =" . $country_id;
1278
			$did_list = $this->db->query($query);
1279
		} else {
1280
			$did_list = $this->db_model->getSelect("id,number,setup,monthlycost", "dids", array('country_id' => $country_id, 'accountid' => '0', 'parent_id' => $reseller_id));
1281
		}
1282
		$did_arr = array();
1283
		if ($did_list->num_rows > 0) {
1284
			$did_data = $did_list->result_array();
1285
			foreach ($did_data as $key => $value) {
1286
				$did_arr[$value['id']] = $value['number'] . " (Setup Cost : " . $this->common_model->calculate_currency($value['setup'], '', '', true) . ") (Monthly cost : " . $this->common_model->calculate_currency($value['monthlycost'], '', '', true) . ")";
1287
			}
1288
		}
1289
		$did_info = array("name" => "free_didlist", "id" => "free_didlist", "class" => "free_didlist");
1290
		echo form_dropdown_all($did_info, $did_arr, '');
1291
		exit;
1292
	}
1293
1294
	/*     * ************************************************************** */
1295
1296
	function customer_payment_process_add($id = '') {
1297
		$account = $this->accounts_model->get_account_by_number($id);
1298
		$currency = $this->accounts_model->get_currency_by_id($account['currency_id']);
1299
		$data['username'] = $this->session->userdata('user_name');
1300
		$data['page_title'] = '​Refill Process';
1301
		$data['form'] = $this->form->build_form($this->accounts_form->get_customer_payment_fields($currency['currency'], $account['number'], $currency['currency'], $id), '');
1302
		$this->load->view('view_accounts_process_payment', $data);
1303
	}
1304
	function customer_payment_save($id = '') {
1305
	$post_array = $this->input->post();
1306
	$data['username'] = $this->session->userdata('user_name');
1307
		$data['page_title'] = 'Process Payment';
1308
		$account = $this->accounts_model->get_account_by_number($post_array['id']);
1309
		$currency = $this->accounts_model->get_currency_by_id($account['currency_id']);
1310
		$data['form'] = $this->form->build_form($this->accounts_form->get_customer_payment_fields($currency['currency'], $account['number'], $currency['currency'], $id), '');
1311
	if ($this->form_validation->run() == FALSE) {
1312
		$data['validation_errors'] = validation_errors();
1313
		echo $data['validation_errors'];
1314
		exit;
1315
	} else {
1316
			$post_array['credit'] = $this->common_model->add_calculate_currency($post_array['credit'], "", '', false, false);
1317
			$logintype = $this->session->userdata('logintype');
1318
			$username = $this->session->userdata('username');
1319
		$login_user_data = $this->session->userdata("accountinfo");
1320
			$accountinfo = $this->accounts_model->get_account_by_number($post_array['id']);
1321
			if($accountinfo['reseller_id'] == 0){
1322
			   $where = array("accountid"=> 1);
1323
		}else{
1324
			$where = array("accountid"=> $accountinfo['id']);    
1325
		}
1326
		$query = $this->db_model->getSelect("*", "invoice_conf", $where);
1327
		if($query->num_rows >0){
1328
			$invoice_conf = $query->result_array();
1329
			$invoice_conf = $invoice_conf[0];
1330
		}else{
1331
			$query = $this->db_model->getSelect("*", "invoice_conf",array("accountid"=> 1));
1332
			$invoice_conf = $query->result_array();
1333
			$invoice_conf = $invoice_conf[0];            
1334
		}
1335
		 $last_invoice_ID = $this->get_invoice_date("invoiceid",$accountinfo["id"]);
1336
		 $invoice_prefix=$invoice_conf['invoice_prefix'];
1337
		  $due_date = gmdate("Y-m-d H:i:s",strtotime(gmdate("Y-m-d H:i:s")." +".$invoice_conf['interval']." days"));
1338
				$response = $this->accounts_model->account_process_payment($post_array);
1339
1340
				if($post_array['payment_type']== 1 ){
1341
			$this->load->module('invoices/invoices');
1342
			$invoice_id=$this->invoices->invoices->generate_receipt($post_array['id'],$post_array['credit'],$accountinfo,$last_invoice_ID+1,$invoice_prefix,$due_date);
1343
		$account_balance = $this->common->get_field_name('balance', 'accounts', $post_array['id']);
1344
			$insert_arr = array("accountid" => $post_array['id'],
1345
					  "description" => trim($post_array['notes']),
1346
					  "debit" => $post_array['credit'],
1347
					  "credit" => '0',
1348
					  "created_date" => gmdate("Y-m-d H:i:s"), 
1349
					  "invoiceid"=>$invoice_id,
1350
					  "reseller_id"=>'0',
1351
					  "item_type"=>'POSTCHARG',
1352
					  "item_id"=>'0',
1353
  					  'before_balance'=>$account_balance+$post_array['credit'],
1354
					  'after_balance'=>$account_balance,
1355
					);
1356
			$this->db->insert("invoice_details", $insert_arr);
1357
				}else{
1358
			$this->load->module('invoices/invoices');
1359
			$invoice_id=$this->invoices->invoices->generate_receipt($post_array['id'],$post_array['credit'],$accountinfo,$last_invoice_ID+1,$invoice_prefix,$due_date);
1360
			$account_balance = $this->common->get_field_name('balance', 'accounts', $post_array['id']);
1361
			$insert_arr = array("accountid" => $post_array['id'],
1362
					  "description" => trim($post_array['notes']),
1363
					  "debit" => 0,
1364
					  "credit" => $post_array['credit'],
1365
					  "created_date" => gmdate("Y-m-d H:i:s"), 
1366
					  "invoiceid"=>$invoice_id,
1367
					  "reseller_id"=>$accountinfo['reseller_id'],
1368
					  "item_type"=>'Refill',
1369
					  "item_id"=>'0',
1370
  					  'before_balance'=>$account_balance-$post_array['credit'],
1371
					  'after_balance'=>$account_balance,
1372
					);
1373
			$this->db->insert("invoice_details", $insert_arr);
1374
			if($login_user_data['type'] ==1){
1375
			  $reseller_ids=$this->common->get_parent_info($login_user_data['id'],0);
1376
			  $reseller_ids=rtrim($reseller_ids,",");
1377
			  $reseller_arr=explode(",",$reseller_ids);
1378
			  if(!empty($reseller_arr)){
1379
			 $custom_array=$post_array;
1380
			foreach($reseller_arr as $key=>$reseller_id){
1381
			  $custom_array['id']=$reseller_id;
1382
			  $response = $this->accounts_model->account_process_payment($custom_array);
1383
			  $reseller_info=(array)$this->db->get_where('accounts',array("id"=>$reseller_id))->first_row();
1384
			  $last_invoice_ID = $this->get_invoice_date("invoiceid",$reseller_info["id"]);
1385
			  $invoice_id=$this->invoices->invoices->generate_receipt($reseller_info['id'],$custom_array['credit'],$reseller_info,$last_invoice_ID+1,$invoice_prefix,$due_date);
1386
			  $account_balance = $this->common->get_field_name('balance', 'accounts', $reseller_info['id']);
1387
			  $insert_arr = array("accountid" => $reseller_info['id'],
1388
				      "description" => trim($custom_array['notes']),
1389
				      "debit" => 0,
1390
				      "credit" => $custom_array['credit'],
1391
				      "created_date" => gmdate("Y-m-d H:i:s"), 
1392
				      "invoiceid"=>$invoice_id,
1393
				      "reseller_id"=>$reseller_info['reseller_id'],
1394
				      "item_type"=>'Refill',
1395
				      "item_id"=>'0',
1396
  				      'before_balance'=>$account_balance - $custom_array['credit'],
1397
				      'after_balance'=>$account_balance,
1398
				    );
1399
			    $this->db->insert("invoice_details", $insert_arr);
1400
			}
1401
		      }
1402
		    }
1403
		}
1404
	        $message = $post_array['payment_type'] == 0 ? "Recharge successfully!" : "Post charge applied successfully.";
1405
/***********************************************************************************************/
1406
                echo json_encode(array("SUCCESS"=> $message));
1407
                exit;
1408
        //    }
1409
        }
1410
        $this->load->view('view_accounts_process_payment', $data);
1411
    }
1412
1413
    /**
1414
     * @param string $select
1415
     */
1416
    function get_invoice_date($select,$accountid){
1417
	$query = $this->db_model->select($select, "invoices", '',"id","DESC","1","0");
1418
	if($query->num_rows >0){
1419
		$invoiceid = $query->result_array();
1420
		$invoice_date=$invoiceid[0][$select];
1421
		return  $invoice_date;
1422
	}
1423
	return false;
1424
	} 
1425
/***************************************Completed********************************************************/
1426
1427
    
1428
	/**
1429
	 * -------Here we write code for controller accounts functions add_callerid------
1430
	 * Add caller ids against account no
1431
	 * @account_number: Account No
1432
	 */
1433
	function customer_add_callerid($id = "") {
1434
		$data['username'] = $this->session->userdata('user_name');
1435
		$data['page_title'] = '​Force Caller Id';
1436
		$account_num = $this->accounts_model->get_account_number($id);
1437
		$result = $this->accounts_model->get_callerid($id);
1438
		if ($result->num_rows() > 0) {
1439
			foreach ($result->result_array() as $values) {
1440
				$data['accountid'] = $values['accountid'];
1441
				$data['callerid_name'] = $values['callerid_name'];
1442
				$data['callerid_number'] = $values['callerid_number'];
1443
				$data['status'] = $values['status'];
1444
				$data['flag'] = '1';
1445
			}
1446
		} else {
1447
			$data['accountid'] = $id;
1448
			$data['callerid_name'] = '';
1449
			$data['callerid_number'] = '';
1450
			$data['status'] = '0';
1451
			$data['flag'] = '0';
1452
		}
1453
		$data['accountid'] = $account_num['number'];
1454
		$data['form'] = $this->form->build_form($this->accounts_form->get_customer_callerid_fields(), $data);
1455
		$post_array = $this->input->post();
1456
        
1457
		if (!empty($post_array)) {
1458
			if ($this->form_validation->run() == FALSE) {
1459
				$data['validation_errors'] = validation_errors();
1460
				echo $data['validation_errors'];
1461
				exit;
1462
			} else {
1463
				if ($post_array['flag'] == '1') {
1464
					$this->accounts_model->edit_callerid($post_array);
1465
					 echo json_encode(array("SUCCESS"=> "Account callerID updated successfully!"));
1466
					  exit;
1467
				} else {
1468
					$this->accounts_model->add_callerid($post_array);
1469
					echo json_encode(array("SUCCESS"=> "Account callerID added successfully!"));
1470
					exit;
1471
				}
1472
			}
1473
		}
1474
		$this->load->view('view_accounts_add_callerid', $data);
1475
	}
1476
	function reseller_add($type = "") {
1477
		$accountinfo = $this->session->userdata('accountinfo');
1478
		$data['username'] = $this->session->userdata('user_name');
1479
		$data['flag'] = 'create';
1480
		$data['page_title'] = 'Create Reseller';
1481
		$data['back_flag'] = true;
1482
		$data['form'] = $this->form->build_form($this->accounts_form->get_form_reseller_fields(), '');
1483
		$data['country_id'] = $accountinfo['country_id'];
1484
		$data['currency_id'] = $accountinfo['currency_id'];
1485
		$data['timezone_id'] = $accountinfo['timezone_id'];
1486
		if (!$data['timezone_id']) {
1487
			$data['timezone_id'] = 1;
1488
		}
1489
		if (!$data['currency_id']) {
1490
			$data['currency_id'] = 1;
1491
		}
1492
		if (!$data['country_id']) {
1493
			$data['country_id'] = 1;
1494
		}
1495
1496
		$this->load->view('view_accounts_create', $data);
1497
	}
1498
1499
	function reseller_edit($edit_id = '') {
1500
		$data['page_title'] = 'Edit Reseller';
1501
		$where = array('id' => $edit_id);
1502
		$account = $this->db_model->getSelect("*", "accounts", $where);
1503
		$data["account_data"] = $account->result_array();
1504
		$acc_data = $this->session->userdata("accountinfo");
1505
		foreach ($account->result_array() as $key => $value) {
1506
			$edit_data = $value;
1507
		}
1508
		$taxes_data = $this->db_model->getSelect("group_concat(taxes_id) as taxes_id", "taxes_to_accounts", array("accountid" => $edit_id));
1509
		if (isset($taxes_data) && $taxes_data->num_rows() > 0) {
1510
			$taxes_data = $taxes_data->result_array();
1511
			$edit_data["tax_id"] = explode(",", $taxes_data[0]['taxes_id']);
1512
		}
1513
1514
		/**
1515
          ASTPP  3.0 
1516
          Add For Signup module
1517
		 * */
1518
		$encrypted_string = $this->common->encode($edit_data['id']);
1519
		$encrypt = $this->common->encode_params($encrypted_string);
1520
		$edit_data['registration_url'] = base_url() . "signup/" . $encrypt;
1521
		/*         * *********************************************************** */
1522
		/*         * ***
1523
          ASTPP  3.0 
1524
          Password decode
1525
         * **** */
1526
		$edit_data['password'] = $this->common->decode($edit_data['password']);
1527
		$edit_data['credit_limit']=$this->common_model->calculate_currency(($edit_data['credit_limit']),'','',true,false);
1528
		$entity_name = strtolower($this->common->get_entity_type('', '', $edit_data['type']));
1529
		$data['edit_id']=$edit_id;
1530
		$data['page_title'] =ucfirst($entity_name)." Profile";
1531
		$data['entity_name'] = $entity_name;
1532
		/*         * ********************** */
1533
		$data['form'] = $this->form->build_form($this->accounts_form->get_form_reseller_fields($edit_id), $edit_data);
1534
		$this->load->view('view_reseller_details', $data);
1535
	}
1536
1537
	/*     * *********************************************************** */
1538
1539
	function reseller_save() {
1540
		$add_array = $this->input->post();
1541
		$entity_name = strtolower($this->common->get_entity_type('', '', $add_array['type']));
1542
		$data['country_id'] = $add_array['country_id'];
1543
		$data['timezone_id'] = $add_array['timezone_id'];
1544
		$data['currency_id'] = $add_array['currency_id'];
1545
		$data['entity_name'] = $entity_name;
1546
		$data['edit_id']=$add_array['id'];
1547
		$data['form'] = $this->form->build_form($this->accounts_form->get_form_reseller_fields($add_array['id']), $add_array);
1548
		if ($add_array['id'] != '') {
1549
			$data['page_title'] = 'Edit Reseller';
1550
			if ($this->form_validation->run() == FALSE) {
1551
				$data['validation_errors'] = validation_errors();
1552
			} else {
1553
				/*                 * ****
1554
                  ASTPP  3.0 
1555
                  Password encode
1556
                 * **** */
1557
				$add_array['password'] = $this->common->encode($add_array['password']);
1558
				/*                 * ****************** */
1559
				$add_array['credit_limit'] = $this->common_model->add_calculate_currency($add_array['credit_limit'], '', '', false, false);
1560
				$query = $this->accounts_model->remove_all_account_tax($add_array['id']);
1561
				if (isset($add_array['tax_id'])) {
1562
					foreach ($add_array['tax_id'] as $key => $val) {
1563
						$data1 = array(
1564
							'accountid' => $add_array['id'],
1565
							'taxes_id' => $val,
1566
						);
1567
						$this->accounts_model->add_account_tax($data1);
1568
					}
1569
					unset($add_array['tax_id']);
1570
				}
1571
				unset($add_array['number'],$add_array['registration_url']);
1572
				$this->accounts_model->edit_account($add_array, $add_array['id']);
1573
				$this->session->set_flashdata('astpp_errormsg', 'Reseller updated successfully!');
1574
1575
				redirect(base_url() . 'accounts/reseller_list/');
1576
				exit;
1577
			}
1578
			$data["account_data"]["0"] = $add_array;
1579
			$edit_id = $add_array["id"];
1580
			$data['chargelist'] = form_dropdown('applayable_charge', $this->Astpp_common->list_applyable_charges($edit_id), '');
1581
1582
			$this->load->module('charges/charges');
1583
			$data['charges_grid_field'] = $this->charges->charges_form->build_charges_list_for_customer($edit_id, "reseller");
1584
1585
			$data["sipiax_grid_field"] = json_decode($this->accounts_form->build_sipiax_list_for_customer());
1586
1587
			$this->load->module('did/did');
1588
			$data['did_grid_fields'] = $this->did->did_form->build_did_list_for_customer($edit_id, "reseller");
1589
			$data['didlist'] = form_dropdown('free_did_list', $this->db_model->build_dropdown("id,number", "dids", "accountid", "0"), '');
1590
1591
1592
			$this->load->module('invoices/invoices');
1593
			$data['invoice_grid_fields'] = $this->invoices->invoices_form->build_invoices_list_for_admin();
1594
1595
			$this->load->module('reports/reports');
1596
			$data['cdrs_grid_fields'] = $this->reports->reports_form->build_report_list_for_user();
1597
			$this->load->view('view_reseller_details', $data);
1598
		} else {
1599
1600
			$data['page_title'] = 'Create Reseller';
1601
			if ($this->form_validation->run() == FALSE) {
1602
				$data['validation_errors'] = validation_errors();
1603
			} else {
1604
				/*                 * ****
1605
                  ASTPP  3.0 
1606
                  Password encode
1607
                 * **** */
1608
				$add_array['password'] = $this->common->encode($add_array['password']);
1609
				/*                 * ****************** */
1610
				$add_array['credit_limit'] = $this->common_model->add_calculate_currency($add_array['credit_limit'], '', '', false, false);
1611
				$last_id = $this->accounts_model->add_account($add_array);
1612
				if (isset($add_array['tax_id'])) {
1613
					foreach ($add_array['tax_id'] as $key => $val) {
1614
						$data1 = array(
1615
							'accountid' => $last_id,
1616
							'taxes_id' => $val,
1617
						);
1618
						$this->accounts_model->add_account_tax($data1);
1619
					}
1620
					unset($add_array['tax_id']);
1621
				}
1622
				$this->session->set_flashdata('astpp_errormsg', 'Reseller added successfully!');
1623
				redirect(base_url() . 'accounts/reseller_list/');
1624
				exit;
1625
			}
1626
			$this->load->view('view_accounts_create', $data);
1627
		}
1628
	}
1629
1630
	function reseller_dids($edit_id) {
1631
		$this->customer_dids($edit_id);
1632
	}
1633
1634
	function customer_generate_password() {
1635
		echo $this->common->generate_password();
1636
	}
1637
1638
	function customer_generate_number($digit='') {
1639
       
1640
		echo $this->common->find_uniq_rendno($digit, 'number', 'accounts');
1641
	}
1642
1643
	function admin_add($type = 2) {
1644
		$accountinfo = $this->session->userdata('accountinfo');
1645
		$entity_type = strtolower($this->common->get_entity_type('', '', $type));
1646
		$entitytype = str_replace(' ', '', $entity_type);
1647
		$data['username'] = $this->session->userdata('user_name');
1648
		$data['flag'] = 'create';
1649
		$data['page_title'] = 'Create ' . ucfirst($entity_type);
1650
		$data['back_flag'] = true;
1651
		$data['form'] = $this->form->build_form($this->accounts_form->get_form_admin_fields($entitytype), '');
1652
		$data['country_id'] = $accountinfo['country_id'];
1653
		$data['currency_id'] = $accountinfo['currency_id'];
1654
		$data['timezone_id'] = $accountinfo['timezone_id'];
1655
		if (!$data['timezone_id']) {
1656
			$data['timezone_id'] = 1;
1657
		}
1658
		if (!$data['currency_id']) {
1659
			$data['currency_id'] = 1;
1660
		}
1661
		if (!$data['country_id']) {
1662
			$data['country_id'] = 1;
1663
		}
1664
		$data['entity_name'] = $entity_type;
1665
		$this->load->view('view_accounts_create', $data);
1666
	}
1667
1668
	function admin_edit($edit_id = '') {
1669
	$data['back_flag'] = true;
1670
		$accountinfo=(array)$this->db->get_where('accounts',array("id"=>$edit_id))->first_row();
1671
		$type = $accountinfo['type'] == -1 ? 2 : $accountinfo['type'];
1672
		$entity_type = strtolower($this->common->get_entity_type('', '', $type));
1673
		$entitytype = str_replace(' ', '', $entity_type);
1674
		$accountinfo['password'] = $this->common->decode($accountinfo['password']);
1675
		$data['form'] = $this->form->build_form($this->accounts_form->get_form_admin_fields($entitytype, $edit_id), $accountinfo);
1676
		$data['page_title'] = 'Edit ' . ucfirst($entity_type);
1677
		$this->load->view('view_admin_details', $data);
1678
        
1679
		}
1680
1681
	 function admin_save($add_array = false) {
1682
		$add_array = $this->input->post();
1683
		$accountinfo = $this->session->userdata('accountinfo');
1684
		$type = $add_array['type'] == -1 ? 2 : $add_array['type'];
1685
		$entity_type = strtolower($this->common->get_entity_type('', '', $type));
1686
		$entitytype = str_replace(' ', '', $entity_type);
1687
		$data['username'] = $this->session->userdata('user_name');
1688
		$data['flag'] = 'create';
1689
		$data['page_title'] = 'Create ' . $entity_type;
1690
		$data['form'] = $this->form->build_form($this->accounts_form->get_form_admin_fields($entitytype, $add_array['id']), $add_array);
1691
		if ($add_array['id'] != '') {
1692
			$data['page_title'] = 'Edit ' . $entity_type;
1693
			if ($this->form_validation->run() == FALSE) {
1694
				$data['validation_errors'] = validation_errors();
1695
			} else {
1696
				/*                 * ****
1697
                  ASTPP  3.0 
1698
                  Password encode
1699
                 * **** */
1700
				$add_array['password'] = $this->common->encode($add_array['password']);
1701
				/*                 * ****************** */
1702
				$add_array['credit_limit'] = $this->common_model->add_calculate_currency($add_array['credit_limit'], '', '', false, false);
1703
				$query = $this->accounts_model->remove_all_account_tax($add_array['id']);
1704
				if (isset($add_array['tax_id'])) {
1705
					foreach ($add_array['tax_id'] as $key => $val) {
1706
						$data1 = array(
1707
							'accountid' => $add_array['id'],
1708
							'taxes_id' => $val,
1709
						);
1710
						$this->accounts_model->add_account_tax($data1);
1711
					}
1712
					unset($add_array['tax_id']);
1713
				}
1714
				unset($add_array['number']);
1715
				$this->accounts_model->edit_account($add_array, $add_array['id']);
1716
				if ($add_array['id'] == $accountinfo['id']) {
1717
					$result = $this->db->get_where('accounts', array('id' => $add_array['id']));
1718
					$result = $result->result_array();
1719
					$this->session->set_userdata('accountinfo', $result[0]);
1720
				}
1721
				$this->session->set_flashdata('astpp_errormsg', ucfirst($entity_type) . ' updated successfully!');
1722
				redirect(base_url() . 'accounts/admin_list/');
1723
				exit;
1724
			}
1725
			$this->load->view('view_admin_details', $data);
1726
		} else {
1727
			$data['page_title'] = 'Create ' . ucfirst($entity_type);
1728
			if ($this->form_validation->run() == FALSE) {
1729
				$data['validation_errors'] = validation_errors();
1730
			} else {
1731
				$add_array['password'] = $this->common->encode($add_array['password']);
1732
				$last_id = $this->accounts_model->add_account($add_array);
1733
				if (isset($add_array['tax_id'])) {
1734
					foreach ($add_array['tax_id'] as $key => $val) {
1735
						$data1 = array(
1736
							'accountid' => $last_id,
1737
							'taxes_id' => $val,
1738
						);
1739
						$this->accounts_model->add_account_tax($data1);
1740
					}
1741
					unset($add_array['tax_id']);
1742
				}
1743
				$add_array['credit_limit'] = $this->common_model->add_calculate_currency($add_array['credit_limit'], '', '', false, false);
1744
1745
				$this->session->set_flashdata('astpp_errormsg', ucfirst($entity_type) . ' added successfully!');
1746
				redirect(base_url() . 'accounts/admin_list/');
1747
				exit;
1748
			}$this->load->view('view_accounts_create', $data);
1749
		}
1750
	}
1751
1752
	function subadmin_add($type = "") {
1753
		$this->admin_add(4);
1754
	}
1755
1756
	function subadmin_edit($edit_id = '') {
1757
		$this->admin_edit($edit_id);
1758
	}
1759
1760
	function subadmin_save() {
1761
		$add_array = $this->input->post();
1762
		$this->admin_save($add_array);
1763
	}
1764
1765
	function chargelist_json($accountid) {
1766
		$json_data = array();
1767
		$sweeplist = $this->common_model->get_sweep_list();
1768
1769
		$select = "charges.description,charges.charge,charges.sweep";
1770
		$table = "charges";
1771
		$jionTable = array('charge_to_account', 'accounts');
1772
		$jionCondition = array('charges.id = charge_to_account.charge_id', 'accounts.number = charge_to_account.cardnum');
1773
		$type = array('left', 'inner');
1774
		$where = array('accounts.accountid' => $accountid);
1775
		$order_type = 'charges.id';
1776
		$order_by = "ASC";
1777
1778
		$account_charge_count = $this->db_model->getCountWithJion($table, $select, $where, $jionTable, $jionCondition, $type);
1779
1780
		$count_all = $account_charge_count;
1781
		$config['total_rows'] = $count_all;
1782
		$config['per_page'] = $_GET['rp'];
1783
1784
		$page_no = $_GET['page'];
1785
		$json_data['page'] = $page_no;
1786
1787
		$json_data['total'] = $config['total_rows'];
1788
		$perpage = $config['per_page'];
1789
		$start = ($page_no - 1) * $perpage;
1790
		if ($start < 0)
1791
			$start = 0;
1792
1793
		$account_charge_list = $this->db_model->getAllJionQuery($table, $select, $where, $jionTable, $jionCondition, $type, $perpage, $start, $order_by, $order_type, "");
1794
		if ($account_charge_list->num_rows > 0) {
1795
			foreach ($account_charge_list->result() as $key => $charges_value) {
1796
				$json_data['rows'][] = array('cell' => array(
1797
						$charges_value->description,
1798
						$charges_value->charge,
1799
						$sweeplist[$charges_value->sweep]
1800
						));
1801
			}
1802
		}
1803
		echo json_encode($json_data);
1804
	}
1805
1806
	function admin_list() {
1807
		$data['username'] = $this->session->userdata('user_name');
1808
		$data['page_title'] = 'Admins';
1809
		$data['search_flag'] = true;
1810
		$data['cur_menu_no'] = 1;
1811
		$this->session->set_userdata('advance_search', 0);
1812
		$data['grid_fields'] = $this->accounts_form->build_account_list_for_admin();
1813
		$data["grid_buttons"] = $this->accounts_form->build_grid_buttons_admin();
1814
		$data['form_search'] = $this->form->build_serach_form($this->accounts_form->get_admin_search_form());
1815
		$this->load->view('view_accounts_list', $data);
1816
	}
1817
1818
	/**
1819
	 * -------Here we write code for controller accounts functions account_list------
1820
	 * Listing of Accounts table data through php function json_encode
1821
	 */
1822
	function admin_list_json() {
1823
		$json_data = array();
1824
		$account_data = $this->session->userdata("accountinfo");
1825
		$reseller_id = $account_data['type'] == 1 ? $account_data['id'] : 0;
1826
		$count_all = $this->accounts_model->get_admin_Account_list(false, '', '', $reseller_id);
1827
		$paging_data = $this->form->load_grid_config($count_all, $_GET['rp'], $_GET['page']);
1828
		$json_data = $paging_data["json_paging"];
1829
1830
		$query = $this->accounts_model->get_admin_Account_list(true, $paging_data["paging"]["start"], $paging_data["paging"]["page_no"], $reseller_id);
1831
		$grid_fields = json_decode($this->accounts_form->build_account_list_for_admin());
1832
		$json_data['rows'] = $this->form->build_grid($query, $grid_fields);
1833
1834
		echo json_encode($json_data);
1835
	}
1836
1837
	/**
1838
      ASTPP  3.0 
1839
      Customer Batch Update.
1840
	 * */
1841
	function customer_batch_update() {
1842
		$batch_update_arr = $this->input->post();
1843
		$result = $this->accounts_model->customer_rates_batch_update($batch_update_arr);
1844
		echo json_encode(array("SUCCESS" => "Customer batch updated successfully!"));
1845
		exit;
1846
	}
1847
1848
	/*     * ********************************************************************* */
1849
1850
	function reseller_list() {
1851
		$data['username'] = $this->session->userdata('user_name');
1852
		$data['page_title'] = 'Resellers';
1853
		$data['search_flag'] = true;
1854
		/**
1855
          ASTPP  3.0 
1856
          Reseller Batch Update
1857
		 * */
1858
		$data['batch_update_flag'] = true;
1859
		/*         * *************************************** */
1860
		$this->session->set_userdata('advance_search', 0);
1861
		$data['grid_fields'] = $this->accounts_form->build_account_list_for_reseller();
1862
		$data["grid_buttons"] = $this->accounts_form->build_grid_buttons_reseller();
1863
		$data['form_search'] = $this->form->build_serach_form($this->accounts_form->get_reseller_search_form());
1864
		$data['form_batch_update'] = $this->form->build_batchupdate_form($this->accounts_form->reseller_batch_update_form());
1865
		/*         * ************************************************************************************************************  */
1866
		$this->load->view('view_accounts_list', $data);
1867
	}
1868
1869
	/**
1870
	 * -------Here we write code for controller accounts functions account_list------
1871
	 * Listing of Accounts table data through php function json_encode
1872
	 */
1873
	function reseller_list_json() {
1874
		$json_data = array();
1875
		$count_all = $this->accounts_model->get_reseller_Account_list(false);
1876
		$paging_data = $this->form->load_grid_config($count_all, $_GET['rp'], $_GET['page']);
1877
		$json_data = $paging_data["json_paging"];
1878
1879
		$query = $this->accounts_model->get_reseller_Account_list(true, $paging_data["paging"]["start"], $paging_data["paging"]["page_no"]);
1880
		$grid_fields = json_decode($this->accounts_form->build_account_list_for_reseller());
1881
		$json_data['rows'] = $this->form->build_grid($query, $grid_fields);
1882
1883
		echo json_encode($json_data);
1884
	}
1885
1886
	function reseller_list_search() {
1887
		$ajax_search = $this->input->post('ajax_search', 0);
1888
		if ($this->input->post('advance_search', TRUE) == 1) {
1889
			$this->session->set_userdata('advance_search', $this->input->post('advance_search'));
1890
			$action = $this->input->post();
1891
			unset($action['action']);
1892
			unset($action['advance_search']);
1893
			if (isset($action['balance']['balance']) && $action['balance']['balance'] != '') {
1894
				$action['balance']['balance'] = $this->common_model->add_calculate_currency($action['balance']['balance'], "", '', true, false);
1895
			}
1896
			if (isset($action['credit_limit']['credit_limit']) && $action['credit_limit']['credit_limit'] != '') {
1897
				$action['credit_limit']['credit_limit'] = $this->common_model->add_calculate_currency($action['credit_limit']['credit_limit'], "", '',true, false);
1898
			}
1899
			$this->session->set_userdata('reseller_list_search', $action);
1900
		}
1901
		if (@$ajax_search != 1) {
1902
			redirect(base_url() . 'accounts/reseller_list/');
1903
		}
1904
	}
1905
1906
	function reseller_batch_update() {
1907
		$batch_update_arr = $this->input->post();
1908
		$result = $this->accounts_model->reseller_rates_batch_update($batch_update_arr);
1909
		echo json_encode(array("SUCCESS" => "Reseller batch updated successfully!"));
1910
		exit;
1911
	}
1912
1913
	function admin_list_clearsearchfilter() {
1914
		$this->session->set_userdata('advance_search', 0);
1915
		$this->session->set_userdata('admin_list_search', "");
1916
	}
1917
1918
	function admin_list_search() {
1919
		$ajax_search = $this->input->post('ajax_search', 0);
1920
		if ($this->input->post('advance_search', TRUE) == 1) {
1921
			$this->session->set_userdata('advance_search', $this->input->post('advance_search'));
1922
			$action = $this->input->post();
1923
			unset($action['action']);
1924
			unset($action['advance_search']);
1925
			$this->session->set_userdata('admin_list_search', $action);
1926
		}
1927
		if (@$ajax_search != 1) {
1928
			redirect(base_url() . 'accounts/admin_list/');
1929
		}
1930
	}
1931
1932
	function reseller_list_clearsearchfilter() {
1933
		$this->session->set_userdata('advance_search', 0);
1934
		$this->session->set_userdata('reseller_list_search', "");
1935
	}
1936
1937
1938
	function customer_delete($id) {
1939
		$this->common->customer_delete_dependencies($id);
1940
		$this->session->set_flashdata('astpp_notification', 'Customer removed successfully!');
1941
		redirect(base_url() . 'accounts/customer_list/');
1942
	}
1943
1944
	function reseller_delete($id) {
1945
		$this->common->subreseller_list($id);
1946
		$this->session->set_flashdata('astpp_notification', 'Reseller removed successfully!');
1947
		redirect(base_url() . 'accounts/reseller_list/');
1948
	}
1949
1950
	function free_customer_did($accountid) {
1951
		$this->db->where(array("accountid" => $accountid));
1952
		$this->db->update("dids", array('accountid' => "0"));
1953
		return true;
1954
	}
1955
1956
	function free_ani_map($accountid) {
1957
		$this->db->where(array("accountid" => $accountid));
1958
		$this->db->delete('ani_map');
1959
		return true;
1960
	}
1961
1962
	function free_reseller_did($ids) {
1963
		$accountinfo = $this->session->userdata('accountinfo');
1964
		$reseller_id = $accountinfo['type'] != 1 ? 0 : $accountinfo['id'];
1965
		$data = array('parent_id' => $reseller_id, 'accountid' => 0);
1966
		$where = "parent_id IN ($ids)";
1967
		$this->db->where($where);
1968
		$this->db->update('dids', $data);
1969
		$where = "reseller_id IN ($ids)";
1970
		$this->db->where($where);
1971
		$this->db->delete('reseller_pricing');
1972
		return true;
1973
	}
1974
1975
	function provider_delete($id) {
1976
		$this->accounts_model->remove_customer($id);
1977
		$this->session->set_flashdata('astpp_notification', 'Provider removed successfully!');
1978
		redirect(base_url() . 'accounts/customer_list/');
1979
	}
1980
1981
	function admin_delete($id) {
1982
		$this->accounts_model->remove_customer($id);
1983
		$this->session->set_flashdata('astpp_notification', 'Admin removed successfully!');
1984
		redirect(base_url() . 'accounts/admin_list/');
1985
	}
1986
1987
	function subadmin_delete($id) {
1988
		$this->accounts_model->remove_customer($id);
1989
		$this->session->set_flashdata('astpp_notification', 'Sub admin removed successfully!');
1990
		redirect(base_url() . 'accounts/admin_list/');
1991
	}
1992
    
1993
	function reseller_details_json($module, $accountid) {
1994
1995
		if ($module == "did") {
1996
			$this->load->module('did/did');
1997
			$this->did->reseller_did($accountid, "reseller");
1998
		}
1999
		if ($module == "invoices") {
2000
			$this->load->module('invoices/invoices');
2001
			$this->invoices->customer_invoices($accountid,"reseller");
2002
		}
2003
		if ($module == "charges") {
2004
			$this->load->module('charges/charges');
2005
			$this->charges->customer_charge_list($accountid, "reseller");
2006
		}
2007
		if($module =='packages'){
2008
	  $this->load->module('package/package');
2009
	  $this->package->package_list_reseller($accountid, "reseller");
2010
		}
2011
	}
2012
2013
	function provider_details_json($module, $accountid) {
2014
		if ($module == "invoices") {
2015
			$this->load->module('invoices/invoices');
2016
			$this->invoices->customer_invoices($accountid);
2017
		}
2018
	}
2019
2020
	function customer_add_postcharges($accounttype, $accountid) {
2021
		$charge = $this->input->post("amount", true);
2022
		if ($charge != "") {
2023
			$charge = $this->common_model->add_calculate_currency($charge, "", '', false, false);
2024
			$date = date('Y-m-d H:i:s');
2025
			$insert_arr = array("accountid" => $accountid, "description" => $this->input->post("desc", true),
2026
				"created_date" => $date, "debit" => $charge, "charge_type" => "post_charge");
2027
			$this->db->insert("invoice_item", $insert_arr);
2028
2029
			$this->accounts_model->update_balance($charge, $accountid, "debit");
2030
			redirect(base_url() . "accounts/" . $accounttype . "_edit/$accountid#packages");
2031
		} else {
2032
			redirect(base_url() . "accounts/" . $accounttype . "_edit/$accountid#packages");
2033
		}
2034
	}
2035
2036
	function reseller_did_action($action, $accountid, $accounttype, $did_id = "") {
2037
		if ($action == "add") {
2038
			$did_id = $this->input->post("free_did_list", true);
2039
			if ($did_id != "") {
2040
				$account_query = $this->db_model->getSelect("*", "accounts", array("id" => $accountid));
2041
2042
				$account_arr = $account_query->result_array();
2043
				$idofaccount = $accountid;
2044
				$this->db_model->update("dids", array("parent_id" => $accountid, 'assign_date' => gmdate('Y-m-d H:i:s')), array("id" => $did_id));
2045
				$accountid = $idofaccount;
2046
				$this->load->module('did/did');
2047
				$this->did->did_model->add_reseller_pricing($accountid, $did_id);
2048
2049
				$this->session->set_flashdata('astpp_errormsg', 'DID added successfully.');
2050
				redirect(base_url() . "accounts/" . $accounttype . "_edit/$accountid#did");
2051
			} else {
2052
				redirect(base_url() . "accounts/" . $accounttype . "_edit/$accountid#did");
2053
			}
2054
		}
2055
		if ($action == "delete") {
2056
			$this->db->where('id', $did_id);
2057
			$this->db->select('note');
2058
			$pricing_res = $this->db->get('reseller_pricing');
2059
			if ($pricing_res->num_rows() > 0) {
2060
				$pricing_res = $pricing_res->result_array();
2061
				$did_number = $pricing_res[0]['note'];
2062
				$accountinfo = $this->session->userdata('accountinfo');
2063
				if ($this->session->userdata['userlevel_logintype'] == -1) {
2064
					$parent_id = 0;
2065
				} else {
2066
					$parent_id = $this->session->userdata['accountinfo']['id'];
2067
				}
2068
2069
				$reseller_ids = $this->common->subreseller_list($accountinfo['id']);
2070
				$pricing_where = "parent_id = $parent_id AND note = $did_number";
2071
				$this->db->where($pricing_where);
2072
				$this->db->delete('reseller_pricing');
2073
				$dids_where = "parent_id IN ($reseller_ids) AND number = $did_number";
2074
				$this->db->where($dids_where);
2075
				$data = array('accountid' => 0, 'parent_id' => $accountinfo['id']);
2076
				$this->db->update('dids', $data);  
2077
				$this->session->set_flashdata('astpp_notification', 'DID removed successfully.');
2078
			} else {
2079
				$this->session->set_flashdata('astpp_notification', 'DID already removed before.');
2080
			}
2081
			redirect(base_url() . "accounts/" . $accounttype . "_edit/$accountid#did");
2082
		}
2083
	}
2084
2085
	function customer_selected_delete() {
2086
		$ids = $this->input->post("selected_ids", true);
2087
		$customer_ids=explode(",",$ids);
2088
		foreach($customer_ids as $customer_id){
2089
			$customer_id = str_replace("'", "", $customer_id);
2090
			$this->common->customer_delete_dependencies($customer_id);
2091
		}
2092
		echo TRUE;
2093
	}
2094
2095
	function reseller_selected_delete() {
2096
		$ids = $this->input->post("selected_ids", true);
2097
		$id_arr = explode(',', $ids);
2098
		foreach ($id_arr as $data) {
2099
	  $data = str_replace("'", "", $data);
2100
	  $this->common->subreseller_list($data);
2101
		}
2102
		echo TRUE;
2103
	}
2104
2105
	function callshop_selected_delete() {
2106
		echo $this->delete_multiple();
2107
	}
2108
2109
	function provider_selected_delete() {
2110
		echo $this->delete_multiple();
2111
	}
2112
2113
	function subadmin_selected_delete() {
2114
		echo $this->delete_multiple();
2115
	}
2116
2117
	function admin_selected_delete() {
2118
		echo $this->delete_multiple();
2119
	}
2120
2121
	function delete_multiple() {
2122
		$ids = $this->input->post("selected_ids", true);
2123
		$where = "id IN ($ids)";
2124
		$data = array('deleted' => 1);
2125
		$this->db->where($where);
2126
		$this->db->update("accounts", $data);
2127
		echo TRUE;
2128
	}
2129
2130
	function customer_account_taxes($action = false, $id = false) {
2131
		$data['username'] = $this->session->userdata('user_name');
2132
		$data['page_title'] = 'Account Taxes';
2133
2134
		if ($action == false)
2135
			$action = "list";
2136
2137
		if ($action == 'list') {
2138
			$this->load->view('view_account_taxes_list', $data);
2139
		} elseif ($action == 'add') {
2140
2141
			if (($this->input->post())) {
2142
				$post_array = $this->input->post();
2143
				$query = $this->accounts_model->remove_all_account_tax($post_array['account_id']);
2144
2145
				foreach ($post_array as $key => $value) {
2146
					$id = explode("_", $key);
2147
					if ($id[0] == 'tax') {
2148
						$data = array(
2149
							'accountid' => $post_array['account_id'],
2150
							'taxes_id' => $post_array[$key],
2151
						);
2152
						$this->accounts_model->add_account_tax($data);
2153
					}
2154
				}
2155
				$this->session->set_flashdata('astpp_errormsg', 'Account tax added successfully!');
2156
				redirect(base_url() . 'accounts/customer_list/');
2157
			}
2158
			$data['id'] = array();
2159
			$data['taxesList'] = $this->common_model->get_list_taxes();
2160
			$this->load->view('view_accounting_taxes_add', $data);
2161
		} elseif ($action == 'edit') {
2162
			$taxes_id = $this->accounts_model->get_accounttax_by_id($id);
2163
			$account_num = $this->accounts_model->get_account_number($id);
2164
			$data['accountnum'] = $account_num['number'];
2165
			$data['account_id'] = $id;
2166
			for ($i = 0; $i < count($taxes_id); $i++) {
2167
				$tax_ids[] = $taxes_id[$i]['taxes_id'];
2168
			}
2169
			$data['tax_ids'] = $tax_ids;
2170
2171
			$data['tax_id'] = $taxes_id;
2172
2173
			if (($this->input->post())) {
2174
				$post_array = $this->input->post();
2175
				$accountinfo = $this->accounts_model->get_account_by_number($post_array['account_id']);
2176
				$query = $this->accounts_model->remove_all_account_tax($post_array['account_id']);
2177
				foreach ($post_array as $key => $value) {
2178
					$id = explode("_", $key);
2179
					if ($id[0] == 'tax') {
2180
						$data = array(
2181
							'accountid' => $post_array['account_id'],
2182
							'taxes_id' => $post_array[$key],
2183
						);
2184
						$this->accounts_model->add_account_tax($data);
2185
					}
2186
				}
2187
				if ($accountinfo['type'] == '0') {
2188
					$link = base_url() . '/accounts/customer_list/';
2189
				} else {
2190
					$link = base_url() . '/accounts/reseller_list/';
2191
				}
2192
				$this->session->set_flashdata('astpp_errormsg', 'Account tax added successfully!');
2193
				redirect($link);
2194
			}
2195
			$data['taxesList'] = $this->common_model->get_list_taxes();
2196
			$this->load->view('view_accounting_taxes_add', $data);
2197
		} elseif ($action == 'delete') {
2198
			$this->accounting_model->remove_account_tax($id);
2199
			$this->session->set_flashdata('astpp_notification', 'Account tax removed successfully!');
2200
			redirect(base_url() . 'accounting/account_taxes/');
2201
		}
2202
	}
2203
2204
	/**
2205
	 * -------Here we write code for controller accounting functions vallid_account_tax------
2206
	 * here this function called by ajax form and vallidate the account number
2207
	 * @$_POST['username']: Account Number
2208
	 */
2209
	function valid_account_tax() {
2210
		$tax_id = '';
2211
		if (!empty($_POST['username'])) {
2212
2213
			$account_num = mysql_real_escape_string($_POST['username']);
2214
			$row = $this->accounts_model->check_account_num($account_num);
2215
			if (isset($row['accountid']) && $row['accountid'] != '') {
2216
				$taxes_id = $this->accounts_model->get_accounttax_by_id($row['accountid']);
2217
				if ($taxes_id) {
2218
					foreach ($taxes_id as $id) {
2219
						$tax_id.=$id['taxes_id'] . ",";
2220
					}
2221
2222
					$tax_id = rtrim($tax_id, ",");
2223
					echo $row['accountid'] . ',' . $tax_id;
2224
				} else {
2225
					echo $row['accountid'];
2226
				}
2227
			}
2228
		}
2229
	}
2230
2231
	function reseller_edit_account() {
2232
		$account_data = $this->session->userdata("accountinfo");
2233
2234
		$add_array = $this->input->post();
2235
		$data['form'] = $this->form->build_form($this->accounts_form->get_reseller_own_form_fields(), $add_array);
2236
		if ($add_array['id'] != '') {
2237
			$data['page_title'] = 'Edit Reseller';
2238
			if ($this->form_validation->run() == FALSE) {
2239
				$data['validation_errors'] = validation_errors();
2240
			} else {
2241
				$this->accounts_model->edit_account($add_array, $add_array['id']);
2242
				$accountinfo = $this->session->userdata('accountinfo');
2243
				if ($add_array['id'] == $accountinfo['id']) {
2244
					$result = $this->db->get_where('accounts', array('id' => $add_array['id']));
2245
					$result = $result->result_array();
2246
					$this->session->set_userdata('accountinfo', $result[0]);
2247
				}
2248
				$this->session->set_flashdata('astpp_errormsg', 'Reseller updated successfully!');
2249
				redirect(base_url() . '/dashboard/');
2250
			}
2251
			$this->load->view('view_reseller_edit_details_own', $data);
2252
		} else {
2253
			$data['page_title'] = 'Edit Reseller';
2254
			$where = array('id' => $account_data["id"]);
2255
			$account = $this->db_model->getSelect("*", "accounts", $where);
2256
			$data["account_data"] = $account->result_array();
2257
			foreach ($account->result_array() as $key => $value) {
2258
				$editable_data = $value;
2259
			}
2260
			$data['form'] = $this->form->build_form($this->accounts_form->get_reseller_own_form_fields(), $editable_data);
2261
			$this->load->view('view_reseller_edit_details_own', $data);
2262
		}
2263
	}
2264
	/**
2265
	 * -------Here we write code for controller accounts functions add_callerid------
2266
	 * Add caller ids against account no
2267
	 * @account_number: Account No
2268
	 */
2269
	function customer_animap_list($id = '') {
2270
		$data['animap_id'] = $id;
2271
		$data['username'] = $this->session->userdata('user_name');
2272
		$data['page_title'] = "Caller Id List";
2273
		$this->session->set_userdata('animap_search', 0);
2274
		$data['grid_fields'] = $this->accounts_form->build_animap_list();
2275
		$data["grid_buttons"] = $this->accounts_form->build_grid_buttons_destination();
2276
		$this->load->view('view_ani_map', $data);
2277
	}
2278
2279
	function customer_animap_list_json($id = '') {
2280
		$json_data = array();
2281
		$count_all = $this->accounts_model->get_animap(false, '', '', $id);
2282
		$data['callingcard_id'] = $id;
2283
		$paging_data = $this->form->load_grid_config($count_all, $_GET['rp'], $_GET['page']);
2284
		$json_data = $paging_data["json_paging"];
2285
		$query = $this->accounts_model->get_animap(true, $paging_data["paging"]["start"], $paging_data["paging"]["page_no"], $id);
2286
		$grid_fields = json_decode($this->accounts_form->build_animap_list());
2287
		$json_data['rows'] = $this->form->build_grid($query, $grid_fields);
2288
		echo json_encode($json_data);
2289
		exit;
2290
	}
2291
2292
	function customer_animap_list_action($id = '') {
2293
		$add_array = $this->input->post();
2294
		$add_array['id'] = trim($add_array['id']);
2295
		$add_array['number'] = trim($add_array['number']);
2296
		if (isset($add_array['id']) && $add_array['id'] != '') {
2297
			$add_array['id'] = trim($add_array['id']);
2298
			$id = $add_array['id'];
2299
		}
2300
		$where = array("number" => $add_array['number']);
2301
		$pro = $this->accounts_model->animap_authentication($where, $id);
2302
		if ($pro > 0) {
2303
			echo "2";
2304
			exit;
2305
		}
2306
		if (isset($add_array['number']) && !empty($add_array['number'])) {
2307
			if (isset($add_array['id']) && $add_array['id'] != '') {
2308
				unset($add_array['animap_id']);
2309
				$response = $this->accounts_model->edit_animap($add_array, $add_array['id']);
2310
				echo "1";
2311
				exit;
2312
			} else {
2313
				$add_array['context'] = "default";
2314
				unset($add_array['animap_id']);
2315
				$add_array['accountid'] = $id;
2316
				$response = $this->accounts_model->add_animap($add_array);
2317
				echo "0";
2318
				exit;
2319
			}
2320
		} else {
2321
			echo "3";
2322
			exit;
2323
		}
2324
	}
2325
2326
	function customer_animap_list_remove($id) {
2327
		$this->accounts_model->remove_ani_map($id);
2328
		echo "1";
2329
		exit;
2330
	}
2331
2332
	function customer_animap_list_edit($id) {
2333
		$where = array('id' => $id);
2334
		$account = $this->db_model->getSelect("*", "ani_map", $where);
2335
		foreach ($account->result_array() as $key => $value) {
2336
			$edit_data = $value;
2337
		}
2338
		$value_edit = '';
2339
		foreach ($edit_data as $key => $value) {
2340
			$value_edit.=$value . ",";
2341
		}
2342
		echo rtrim($value_edit, ',');
2343
		exit;
2344
	}
2345
2346
	function provider_edit_account() {
2347
		$this->customer_edit_account();
2348
	}
2349
2350
	function customer_show_password($id) {
2351
		$account = $this->db_model->getSelect("password", "accounts", array('id' => $id));
2352
		$account_data = $account->result_array();
2353
		$password = $this->common->decode($account_data[0]['password']);
2354
		echo $password;
2355
	}
2356
2357
	function reseller_export_cdr_xls() {
2358
	$account_info = $accountinfo = $this->session->userdata('accountinfo');
2359
	$currency_id=$account_info['currency_id'];
2360
	$currency=$this->common->get_field_name('currency', 'currency', $currency_id);
2361
	ob_clean();
2362
		$query = $this->accounts_model->get_reseller_Account_list(true, '', '', true);
2363
		$customer_array[] = array("Account", "First Name", "Last Name", "Company", "Rate Group ", "Account type", "Balance($currency)", "Credit Limit($currency)", "Status","Created Date");
2364
		if ($query->num_rows() > 0) {
2365
2366
			foreach ($query->result_array() as $row) {
2367
				$customer_array[] = array(
2368
					$row['number'],
2369
					$row['first_name'],
2370
					$row['last_name'],
2371
					$row['company_name'],
2372
					$this->common->get_field_name('name', 'pricelists', $row['pricelist_id']),
2373
					$this->common->get_account_type('', '', $row['posttoexternal']),
2374
					$this->common_model->calculate_currency($row['balance'],false,false),
2375
					$this->common_model->calculate_currency($row['credit_limit'],false,false),
2376
					$this->common->get_status('export', '', $row['status']),
2377
					$row['creation'],
2378
				);
2379
			}
2380
		}
2381
		$this->load->helper('csv');
2382
		array_to_csv($customer_array, 'Resellers_' . date("Y-m-d") . '.csv');
2383
	}
2384
	function customer_validate_ip(){
2385
	 $add_array=$this->input->post();
2386
	 if(!empty($add_array)){
2387
	   $ip = $add_array['ip'];
2388
	   if (strpos($ip, '/') !== false) {
2389
		 $add_array['ip'] = $add_array['ip'];
2390
	   } else {
2391
		 $add_array['ip'] = $add_array['ip'] . '/32';
2392
	   }
2393
	   $this->db->where('ip',$add_array['ip']);
2394
	   $this->db->where('prefix',$add_array['prefix']);
2395
	   $this->db->select('count(ip) as count');
2396
	   $ip_map_result=(array)$this->db->get('ip_map')->first_row();
2397
	   if($ip_map_result['count'] > 0 ){
2398
		 echo 'FALSE';
2399
	   }else{
2400
		 echo 'TRUE';
2401
	   }
2402
	 }else{
2403
	  echo 'FALSE';
2404
	 }
2405
    
2406
	}
2407
function reseller_invoice_config($id=''){
2408
		$data['page_title'] = 'Company Profile';
2409
		$accountinfo = $this->session->userdata("accountinfo");
2410
		$add_array = $this->input->post();
2411
		$data["account_data"] = $add_array;
2412
		if (isset($add_array['submit'])) {
2413
			if ($_FILES['file']['name'] == '') {
2414
				$this->load->module('invoices/invoices');
2415
				$this->load->model("invoices_model");
2416
				$invoiceconf = $this->invoices_model->get_invoiceconf();
2417
				$file_name=($invoiceconf['logo'] != '') ? $invoiceconf['logo'] : '';
2418
			}
2419
			if (isset($_FILES['file']['name']) && $_FILES['file']['name'] != '') {
2420
				$files = $_FILES['file'];
2421
				if ($files['size'] < 0) {
2422
					$this->session->set_flashdata('astpp_notification', 'PLease upload maximum file');
2423
				redirect(base_url() . "accounts/reseller_invoice_config/". $add_array['accountid']."/");
2424
				}
2425
				$file = $_FILES['file'];
2426
				$uploadedFile = $file["tmp_name"];
2427
				$file_name = $file['name'];
2428
				$file_type = $file['type'];
2429
				if ($file_type == 'image/jpg' || $file_type == 'image/png' || $file_type == 'image/jpeg') {
2430
					$dir_path = FCPATH. "upload/";
2431
					$path = $dir_path . $add_array['accountid']."_".$file['name'];
2432
					if (move_uploaded_file($uploadedFile, $path)) {
2433
						$this->session->set_flashdata('astpp_errormsg', gettext('files added successfully!'));
2434
					} else {
2435
						$this->session->set_flashdata('astpp_notification', "File Uploading Fail Please Try Again");
2436
					redirect(base_url() . "accounts/reseller_invoice_config/". $add_array['accountid']."/");
2437
					}
2438
				} else {
2439
					$this->session->set_flashdata('astpp_notification', 'Please upload only image!');
2440
				redirect(base_url() . "accounts/reseller_invoice_config/". $add_array['accountid']."/");
2441
				}
2442
			}
2443
			$add_array['logo'] = $file_name;
2444
			unset($add_array['submit']);
2445
			if ($add_array['id'] == '') {
2446
				$this->accounts_model->add_invoice_config($add_array);
2447
			} else {
2448
				$this->accounts_model->edit_invoice_config($add_array, $add_array['id']);
2449
			}
2450
			$this->session->set_flashdata('astpp_errormsg', 'Invoice config updated successfully!');
2451
			redirect(base_url() . "accounts/reseller_invoice_config/". $add_array['accountid']."/");
2452
		} else {
2453
			$data["account_data"] =(array)$this->db->get_where('invoice_conf',array("accountid"=>$id))->first_row();
2454
		$data["accounttype"]='Reseller';
2455
		$data["edit_id"]=$id;
2456
			if(isset($data["account_data"]['logo'])){
2457
				$data["account_data"]['file']=$id."_".$data["account_data"]['logo'];
2458
		}
2459
			$this->load->view('view_reseller_invoices_config', $data);
2460
		}
2461
}
2462
     
2463
	 function reseller_invoice_logo_delete($accountid){
2464
		 $invoiceconf  = $this->db_model->getSelect("*", "invoice_conf", array("accountid"=> $accountid));
2465
		 $result=$invoiceconf->result_array();
2466
		 $logo=$result[0]['logo'];
2467
		 $post_arr=array('logo'=>'');
2468
		 $where_arr=array('logo'=>$logo);
2469
		 $this->db->where($where_arr);
2470
		 $this->db->update('invoice_conf',$post_arr);
2471
	 }
2472
     
2473
     
2474
	 function customer_global_grid_list(){
2475
	  echo gettext($_POST['display']); 
2476
 	}
2477
 	
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
2478
 	
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
2479
}
2480
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
2481
 
2482