/application/controllers/adminpanel/cashier.php
PHP | 699 lines | 555 code | 120 blank | 24 comment | 75 complexity | 2ca7154afb3411cec425884c5c6d7481 MD5 | raw file
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class Cashier extends MY_Controller
- {
- public function __construct()
- {
- parent::__construct(true);
- if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
- {
- show_404();
- }
- $this->load->model('support_ticket_model', 'Support');
- $this->load->model('payment_method_model', 'PaymentMethod');
- $this->load->model('transaction_model', 'Transaction');
- $this->load->model('user_model', 'User');
- $this->load->model('history_model', 'History');
- $this->load->model('cashier_model', 'Cashier');
- $this->load->model('shares_model', 'Shares');
- $this->load->library('table');
- $this->load->library('AccountObject');
- $this->load->helper('html');
- $this->setLayout('layout/admin');
- $this->layoutData['menu'] = $this->loadPartialView('admin/menu');
- $this->layoutData['title'] = 'xxxxxxxxxxxxx';
- }
- private function __methodsMenu($code = null, $url = null)
- {
- $methods = $this->PaymentMethod->getAll();
- return $this->loadPartialView('admin/cashier/partial/methods', compact('methods', 'code', 'url'));
- }
- public function index($code = null)
- {
- $methodsMenu = $this->__methodsMenu($code, site_url('adminpanel/cashier/index'));
- $dataShow = array('lr', 'ap', 'wu', 'bw');
- $data = $this->Transaction->history($dataShow);
- $this->layoutData['title'] = 'Cashier';
- $this->loadView('admin/cashier/index', '', compact('methodsMenu', 'dataShow', 'code', 'data'));
- }
- public function accounts($code, $id = null)
- {
- $methodsMenu = $this->__methodsMenu($code, site_url('adminpanel/cashier/accounts'));
- $accounts = $this->PaymentMethod->getAccountDetails($code);
- if ($this->input->is_ajax_request())
- {
- $data = $this->PaymentMethod->getAccountDetailsById($id);
- $account = null;
- switch($code)
- {
- case 'lr':
- case 'ap': $account = $data->details; break;
- case 'wu': $account = new WesternUnion($data->details); break;
- case 'bw': $account = new BankWire($data->details); break;
- }
- echo $this->loadPartialView('admin/cashier/partial/account_details', compact('code', 'account'));
- }
- else
- {
- $this->addJavascript(asset('scripts/forms.js'));
- $this->layoutData['title'] = 'Cashier Accounts';
- $this->loadView('admin/cashier/accounts', '', compact('methodsMenu', 'accounts'));
- }
- }
- // This will be used to either edit an account or adding a new one
- public function account($code, $accountId = null)
- {
- $account = null;
- if ($accountId)
- $account = $this->PaymentMethod->getAccountDetailsById($accountId);
- if ($this->input->is_ajax_request())
- {
- $post = $this->input->post();
- if ($this->form_validation->run($code . '_account') === TRUE)
- {
- switch ($code)
- {
- case 'bw':
- $data = new BankWire($post);
- $details = $data->__toString();
- break;
- case 'wu':
- $data = new WesternUnion($post);
- $details = $data->__toString();
- break;
- default:
- $details = $post['account'];
- }
- $data = array(
- 'payment_code' => $code,
- 'name' => $post['name'],
- 'details' => $details,
- 'extra_field_1' => $post['extra_field_1'],
- 'extra_field_2' => $post['extra_field_2'],
- 'restrict_to' => $post['restrict_to'],
- 'minimum' => $post['minimum'],
- 'maximum' => $post['maximum'],
- 'maximum_duration' => $post['maximum_duration']
- );
- if ($account)
- $res = $this->PaymentMethod->updateAccount($accountId, $data);
- else $res = $this->PaymentMethod->addAccount($data);
- if ($res)
- {
- $this->session->set_flashdata('success', 'Account updated');
- $data = array(
- 'success' => 'hurray!',
- 'redirect' => array (
- 'url' => site_url('adminpanel/cashier/accounts/' . $code)
- )
- );
- }
- else
- {
- $data = array(
- 'error' => 'crying face :('
- );
- }
- }
- else
- {
- $data = array(
- 'error' => renderErrors($this->form_validation->error_array())
- );
- }
- echo json_encode($data);
- return;
- }
- else
- {
- $listCountries = $this->User->getCountries();
- $countries = dropdown($listCountries, 'name');
- $methodsMenu = $this->__methodsMenu($code, site_url('adminpanel/cashier/accounts'));
- $this->addJavascript(asset('scripts/forms.js'));
- $this->layoutData['title'] = 'Cashier Accounts';
- $this->loadView('admin/cashier/account', '', compact('methodsMenu', 'code', 'account', 'countries'));
- }
- }
- public function billing($code)
- {
- $methodsMenu = $this->__methodsMenu($code, site_url('adminpanel/cashier/billing'));
- $this->layoutData['title'] = 'Cashier Billing';
- $this->loadView('admin/cashier/index', '', compact('methodsMenu'));
- }
- public function deposits($code = 'any', $status = 'pending', $page = 1, $perpage = 30)
- {
- // pending/completed/add new
- $methodsMenu = $this->__methodsMenu($code, site_url('adminpanel/cashier/deposits'));
- $count = $this->Transaction->countTransactions($code, 'deposit', $status);
- if ($count)
- {
- $data = $this->Transaction->getDepositsSubset($code, $status, $page, $perpage);
- $paging = generatePagination(site_url('adminpanel/cashier/deposits/' . $code . '/' . $status), $count, $page, $perpage, true);
- $hasPages = $count > $perpage;
- $deposits = $this->load->view('admin/cashier/partial/deposits', compact ('data', 'paging', 'hasPages', 'status'), true);
- }
- else $deposits = "No $status deposits found";
- if ($this->input->is_ajax_request())
- {
- echo $deposits;
- }
- else
- {
- $this->addJavascript(asset('scripts/paging.js'));
- $codeUrl = site_url('adminpanel/cashier/deposits/' . $code);
- $this->layoutData['title'] = 'Cashier Deposits';
- $this->loadView('admin/cashier/deposits', '', compact('methodsMenu', 'deposits', 'codeUrl'));
- }
- }
- public function deposit_details($id)
- {
- if ($this->input->is_ajax_request())
- {
- $deposit = $this->Transaction->getDetails($id);
- $userAccount = $this->PaymentMethod->getAccountForUser($deposit->user_id, $deposit->method);
- echo $this->loadPartialView('admin/cashier/partial/deposit_details', compact('deposit', 'userAccount'));
- }
- else
- {
- show_404();
- }
- }
- public function cashout_details($id)
- {
- if ($this->input->is_ajax_request())
- {
- $cashout = $this->Transaction->getDetails($id);
- $userAccount = $this->PaymentMethod->getAccountForUser($cashout->user_id, $cashout->method);
- echo $this->loadPartialView('admin/cashier/partial/cashout_details', compact('cashout', 'userAccount'));
- }
- else
- {
- show_404();
- }
- }
- public function cashouts($code, $status = 'pending',$page = 1, $perpage = 30)
- {
- // pending/completed/add new
- $methodsMenu = $this->__methodsMenu($code, site_url('adminpanel/cashier/cashouts'));
- $count = $this->Transaction->countTransactions($code, 'cashout', $status);
- if ($count)
- {
- $data = $this->Transaction->getCashoutsSubset($code, $status, $page);
- $paging = generatePagination(site_url('adminpanel/cashier/cashouts/' . $code . '/' . $status), $count, $page, $perpage, true);
- $hasPages = $count > $perpage;
- $cashouts = $this->load->view('admin/cashier/partial/cashouts', compact ('data', 'paging', 'hasPages', 'status'), true);
- }
- else $cashouts = "no $status cashouts found";
- if ($this->input->is_ajax_request())
- {
- echo $cashouts;
- }
- else
- {
- $this->addJavascript(asset('scripts/paging.js'));
- $codeUrl = site_url('adminpanel/cashier/cashouts/' . $code);
- $this->layoutData['title'] = 'Cashier Cashouts';
- $this->loadView('admin/cashier/cashouts', '', compact('code', 'methodsMenu', 'cashouts', 'codeUrl', 'type'));
- }
- }
- public function process_cashouts($code)
- {
- // Find the account(s) to use for the cashout
- $sendFrom = $this->PaymentMethod->getAccountDetails($code, 'out');
- $cashoutIds = $this->input->post('cashout');
- $references = $this->input->post('reference');
- $commit = $this->input->post('commit');
- // Specific fields
- $costs = $this->input->post('cost');
- $infos = $this->input->post('info');
- $mtcns = $this->input->post('mtcn');
- $accounts = $this->input->post('account');
- $amounts = $this->input->post('pickup_amount');
- $currencies = $this->input->post('pickup_currency');
- $cashouts = array();
- // let's do the loop - oh yeah!
- foreach ($cashoutIds as $cashoutId)
- {
- $cashout = $this->Transaction->getCashoutDetails($cashoutId);
- if ($cashout->status != 'pending')
- continue; // if the page times out or is reloaded then cashouts may be sent again - instead we should skip
- $reference = isset($references[$cashoutId]) ? $references[$cashoutId] : null;
- $cost = isset($costs[$cashoutId]) ? $costs[$cashoutId] : null;
- $amount = isset($amounts[$cashoutId]) ? $amounts[$cashoutId] : null;
- $currency = isset($currencies[$cashoutId]) ? $currencies[$cashoutId] : null;
- $accountId = $accounts[$cashoutId];
- $data = null;
- // if we actually pressed the button to send the funds
- if ($commit)
- {
- switch ($code)
- {
- case 'bw':
- $info = isset($infos[$cashoutId]) ? $infos[$cashoutId] : null;
- if ($cost && $reference && $info)
- {
- $details = new BankWireDetails();
- $details->info = $info;
- $details->amount = $amount;
- $details->currency = $currency;
- $data = array(
- 'account_id' => $accountId,
- 'details' => $details->__toString(),
- 'cost' => $cost,
- 'reference' => $reference
- );
- }
- break;
- case 'wu':
- $mtcn = isset($mtcns[$cashoutId]) ? $mtcns[$cashoutId] : null;
- if ($mtcn && $reference && $amount)
- {
- $details = new WesternUnionDetails($cashout->details);
- $details->mtcn = $mtcn;
- $details->amount = $amount;
- $details->currency = $currency;
- $data = array(
- 'account_id' => $accountId,
- 'details' => $details->__toString(),
- 'cost' => 0,
- 'reference' => $reference
- );
- }
- break;
- // all other methods are (should be) automatic
- default:
- // manual reference entered so just use it instead of processing the payment
- if ($reference)
- {
- $data = array(
- 'account_id' => $accountId,
- 'reference' => $reference
- );
- }
- else
- {
- //TODO: Process automatically (add to an array?)
- }
- }
- }
- // If we have data we can try to update the cashout
- if ($data && $this->Transaction->update($cashoutId, 'ok', $data))
- {
- // Refresh the data to grab all the necessary new information
- $cashout = $this->Transaction->getCashoutDetails($cashoutId);
- $userId = $cashout->user_id;
- $email = $this->ion_auth->select('email')->user($userId)->row()->email;
- $fromAccount = $this->PaymentMethod->getAccountDetailsById($accountId);
- $userAccount = $this->PaymentMethod->getByUserId($userId, $code);
- // The cashout has been stored properly so send an email
- switch ($code)
- {
- case 'bw':
- $this->EmailQueue->store($email, 'My Traffic Value - Bank Wire Cashout Sent', 'emails/cashier/cashout_sent_bw', compact('cashout', 'userAccount'));
- break;
- case 'wu':
- $this->EmailQueue->store($email, 'My Traffic Value - Western Union Cashout Sent', 'emails/cashier/cashout_sent_wu', compact('cashout', 'fromAccount', 'userAccount'));
- break;
- }
- }
- else $cashouts[$cashoutId] = $cashout;
- }
- if (count ($cashouts))
- {
- $fromAccounts = dropdown($sendFrom);
- $this->layoutData['title'] = 'Process Cashouts';
- $this->loadView('admin/cashier/preview_cashouts', '', compact('code', 'cashouts', 'references', 'costs', 'infos', 'mtcns', 'accounts', 'amounts', 'currencies', 'sendFrom', 'fromAccounts'));
- }
- else redirect('adminpanel/cashier/cashouts/' .$code);
- }
- public function reject($id)
- {
- $transactionData = $this->Transaction->getDetails($id);
- $type = $transactionData->type;
- $userId = $transactionData->user_id;
- $email = $this->ion_auth->select('email')->user($userId)->row()->email;
- switch ($type)
- {
- case 'deposit':
- $this->EmailQueue->store($email, 'My Traffic Value - Deposit Request Cancelled', 'emails/cashier/deposit_rejected', compact('transactionData'));
- break;
- case 'cashout':
- $this->EmailQueue->store($email, 'My Traffic Value - Cashout Request Cancelled', 'emails/cashier/cashout_rejected', compact('transactionData'));
- break;
- }
- $this->Transaction->update($id, 'reject');
- return;
- }
- public function reset($id)
- {
- $this->Transaction->update($id, 'pending');
- return;
- }
- public function deposit($depositId, $userId = null, $code = null)
- {
- $details = null;
- if ($depositId > 0)
- {
- $details = $this->Transaction->getDetails($depositId);
- if (!$details || $details->status != 'pending')
- redirect('adminpanel/cashier');
- $code = $details->method;
- $userId = $details->user_id;
- }
- $username = $this->ion_auth->select('username')->user($userId)->row()->username;
- $accounts = dropdown($this->PaymentMethod->getAccountDetails($code, 'in'));
- $userAccount = $this->PaymentMethod->getAccountForUser($userId, $code);
- if (!$userAccount)
- {
- // User does not have an account for this payment method
- $this->session->set_flashdata('error', "$username does not have a $code account");
- redirect('adminpanel/cashier');
- }
- if ($this->input->is_ajax_request())
- {
- if ($this->form_validation->run('admin/cashier/deposit') !== false)
- {
- $post = $this->input->post();
- // Ok so we know we need to set the reference number and that it exists
- $newData = array(
- 'reference' => $post['reference']
- );
- $depositDetails = null;
- switch ($code)
- {
- case 'bw':
- $depositDetails = new BankWireDetails($details ? $details->details : null);
- if ($depositDetails->info != $post['info'])
- $depositDetails->info = $post['info'];
- if ($depositDetails->memo != $post['memo'])
- $depositDetails->memo = $post['memo'];
- break;
- case 'wu':
- $depositDetails = new WesternUnionDetails($details ? $details->details : null);
- if ($depositDetails->city != $post['city'])
- $depositDetails->city = $post['city'];
- if ($depositDetails->country != $post['country'])
- $depositDetails->country = $post['country'];
- if ($depositDetails->mtcn != $post['mtcn'])
- $depositDetails->mtcn = $post['mtcn'];
- break;
- }
- if ($details)
- {
- // Editing a deposit
- if ($depositDetails && $depositDetails->__toString() != $details->details)
- {
- // Some of the details given by the user had to be changed for whatever reason
- $newData = array_merge ($newData, array(
- 'details' => $depositDetails->__toString()
- ));
- }
- if ($post['gross_amount'] != $details->gross_amount)
- {
- // If the amount received has changed one then we need to adjust the fee/cost
- $grossAmount = $post['gross_amount'];
- $fee = roundUp($this->PaymentMethod->calculateGross($grossAmount, $code, 'fee', 'deposit'));
- $netAmount = $grossAmount - $fee;
- $newData = array_merge ($newData, array(
- 'gross_amount' => $grossAmount,
- 'amount' => $netAmount,
- 'fee' => $fee
- ));
- }
- if ($post['cost'] != $details->cost)
- {
- $newData = array_merge ($newData, array(
- 'cost' => $post['cost']
- ));
- }
- // if the account has changed (mainly for Bank Wires)
- if ($post['account_id'] != $details->account_id)
- {
- $newData = array_merge ($newData, array(
- 'account_id' => $post['account_id']
- ));
- }
- }
- else
- {
- // Adding a deposit
- $depositData = array(
- 'user_id' => $userId,
- 'method' => $code,
- 'account_id' => $post['account_id'],
- 'gross_amount' => $post['gross_amount'],
- 'cost' => isset($post['cost']) ? $post['cost'] : null,
- 'identifier' => $this->Transaction->identifier()
- );
- if ($depositDetails)
- $depositData['details'] = $depositDetails->__toString();
- $depositId = $this->Transaction->addDeposit($depositData);
- }
- if ($this->Transaction->update($depositId, 'ok', $newData))
- {
- // Email the user when we got the deposit from Western Union or Bank Wire
- if ($code == 'bw' || $code == 'wu')
- {
- $email = $this->ion_auth->select('email')->user($userId)->row()->email;
- $newDepositData = $this->Transaction->getById($depositId);
- $this->EmailQueue->store($email, 'My Traffic Value - Deposit Received', 'emails/cashier/deposit_received_' . $code, compact('newDepositData', 'depositDetails', 'userAccount'));
- }
- $this->session->set_flashdata('success', 'Successfully added deposit');
- $data = array(
- 'success' => 'success',
- 'redirect' => array(
- 'url' => site_url('adminpanel/cashier'),
- )
- );
- }
- else
- {
- $data = array(
- 'error' => 'Problem with the deposit data'
- );
- }
- }
- else
- {
- $data = array(
- 'error' => renderErrors($this->form_validation->error_array())
- );
- }
- echo json_encode($data);
- return;
- }
- $this->addJavascript(asset('scripts/forms.js'));
- $listCountries = $this->User->getCountries();
- $countries = dropdown($listCountries, 'name');
- $this->layoutData['title'] = 'Manage Deposit';
- $this->loadView('admin/cashier/deposit', '', compact('userId', 'username', 'code', 'details', 'userAccount', 'accounts', 'countries'));
- }
- public function account_status()
- {
- if ($this->input->is_ajax_request())
- {
- $accountId = $this->input->post('account_id');
- $enabled = $this->input->post('enabled');
- $this->PaymentMethod->accountStatus($accountId, $enabled ? 1 : 0);
- }
- }
- public function settings($code = null)
- {
- if ($this->input->is_ajax_request())
- {
- $post = $this->input->post();
- $data = array(
- 'percent' => isset($post['percent']) ? $post['percent'] : NULL,
- 'fixed' => isset($post['fixed']) ? $post['fixed'] : NULL,
- 'max' => $post['max'] ? $post['max'] : NULL
- );
- if ($this->PaymentMethod->updateMethodBill($code, $post['type'], $post['operation'], $data))
- {
- $data = array(
- 'success' => 'success'
- );
- }
- else
- {
- $data = array(
- 'error' => 'Error Updating Data!'
- );
- }
- echo json_encode($data);
- }
- else
- {
- // We need to get the deposit and cashout fees + costs all the time
- $billing = array(
- 'deposit' => array(
- 'fee' => $this->PaymentMethod->getFeeData($code, 'deposit', 'fee'),
- 'cost' => $this->PaymentMethod->getFeeData($code, 'deposit', 'cost')
- ),
- 'cashout' => array(
- 'fee' => $this->PaymentMethod->getFeeData($code, 'cashout', 'fee'),
- 'cost' => $this->PaymentMethod->getFeeData($code, 'cashout', 'cost')
- )
- );
- $this->addJavascript(asset('scripts/forms.js'));
- $this->layoutData['title'] = 'Cashier - ' . strtoupper($code) . ' Settings';
- $this->loadView('admin/cashier/settings', '', compact('billing', 'code'));
- }
- }
- public function user_account_details($userId, $code)
- {
- $data = $this->PaymentMethod->getAccountForUser($userId, $code);
- $account = null;
- switch($code)
- {
- case 'lr':
- case 'ap': $account = $data->account; break;
- case 'wu': $account = new WesternUnion($data->account); break;
- case 'bw': $account = new BankWire($data->account); break;
- }
- echo $this->loadPartialView('admin/cashier/partial/account_details', compact('code', 'account'));
- }
- public function get_users_balances($code, $page = 1, $perpage = 50)
- {
- $this->load->model('cashier_model','Cashier');
- $balances = $this->Cashier->getUsersBalances($code, $page, $perpage);
- $count = $this->Cashier->countGetUsersBalances($code);
- $balance_paging = generatePagination(site_url("adminpanel/cashier/get_users_balances/$code"), $count, $page, $perpage, true);
- $balance_hasPages = $count > $perpage;
- $balanceUserTable = $this->loadPartialView('admin/cashier/partial/user_balances_table.php',compact('balances','balance_paging','balances_hasPages','count','code'));
- $methodsMenu = $this->__methodsMenu($code, site_url('adminpanel/cashier/accounts'));
- if ($this->input->is_ajax_request())
- {
- $data = array('html' => $balanceUserTable);
- echo json_encode($data);
- }
- else
- $this->layoutData['title'] = 'Balances';
- ' - User Balances';
- $this->loadView('admin/cashier/user_balances', 'My Traffic Value: Admin', compact('balanceUserTable','methodsMenu','balance_paging','balances_hasPages'));
- //echo show_404();
- }
- // Temporary function
- public function fixTransaction($ref, $amount)
- {
- $this->Cashier->fixTransaction($ref, $amount);
- }
- }