PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/application/controllers/adminpanel/cashier.php

https://bitbucket.org/justin_anastos/coin_flip_game
PHP | 699 lines | 555 code | 120 blank | 24 comment | 75 complexity | 2ca7154afb3411cec425884c5c6d7481 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. class Cashier extends MY_Controller
  3. {
  4. public function __construct()
  5. {
  6. parent::__construct(true);
  7. if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
  8. {
  9. show_404();
  10. }
  11. $this->load->model('support_ticket_model', 'Support');
  12. $this->load->model('payment_method_model', 'PaymentMethod');
  13. $this->load->model('transaction_model', 'Transaction');
  14. $this->load->model('user_model', 'User');
  15. $this->load->model('history_model', 'History');
  16. $this->load->model('cashier_model', 'Cashier');
  17. $this->load->model('shares_model', 'Shares');
  18. $this->load->library('table');
  19. $this->load->library('AccountObject');
  20. $this->load->helper('html');
  21. $this->setLayout('layout/admin');
  22. $this->layoutData['menu'] = $this->loadPartialView('admin/menu');
  23. $this->layoutData['title'] = 'xxxxxxxxxxxxx';
  24. }
  25. private function __methodsMenu($code = null, $url = null)
  26. {
  27. $methods = $this->PaymentMethod->getAll();
  28. return $this->loadPartialView('admin/cashier/partial/methods', compact('methods', 'code', 'url'));
  29. }
  30. public function index($code = null)
  31. {
  32. $methodsMenu = $this->__methodsMenu($code, site_url('adminpanel/cashier/index'));
  33. $dataShow = array('lr', 'ap', 'wu', 'bw');
  34. $data = $this->Transaction->history($dataShow);
  35. $this->layoutData['title'] = 'Cashier';
  36. $this->loadView('admin/cashier/index', '', compact('methodsMenu', 'dataShow', 'code', 'data'));
  37. }
  38. public function accounts($code, $id = null)
  39. {
  40. $methodsMenu = $this->__methodsMenu($code, site_url('adminpanel/cashier/accounts'));
  41. $accounts = $this->PaymentMethod->getAccountDetails($code);
  42. if ($this->input->is_ajax_request())
  43. {
  44. $data = $this->PaymentMethod->getAccountDetailsById($id);
  45. $account = null;
  46. switch($code)
  47. {
  48. case 'lr':
  49. case 'ap': $account = $data->details; break;
  50. case 'wu': $account = new WesternUnion($data->details); break;
  51. case 'bw': $account = new BankWire($data->details); break;
  52. }
  53. echo $this->loadPartialView('admin/cashier/partial/account_details', compact('code', 'account'));
  54. }
  55. else
  56. {
  57. $this->addJavascript(asset('scripts/forms.js'));
  58. $this->layoutData['title'] = 'Cashier Accounts';
  59. $this->loadView('admin/cashier/accounts', '', compact('methodsMenu', 'accounts'));
  60. }
  61. }
  62. // This will be used to either edit an account or adding a new one
  63. public function account($code, $accountId = null)
  64. {
  65. $account = null;
  66. if ($accountId)
  67. $account = $this->PaymentMethod->getAccountDetailsById($accountId);
  68. if ($this->input->is_ajax_request())
  69. {
  70. $post = $this->input->post();
  71. if ($this->form_validation->run($code . '_account') === TRUE)
  72. {
  73. switch ($code)
  74. {
  75. case 'bw':
  76. $data = new BankWire($post);
  77. $details = $data->__toString();
  78. break;
  79. case 'wu':
  80. $data = new WesternUnion($post);
  81. $details = $data->__toString();
  82. break;
  83. default:
  84. $details = $post['account'];
  85. }
  86. $data = array(
  87. 'payment_code' => $code,
  88. 'name' => $post['name'],
  89. 'details' => $details,
  90. 'extra_field_1' => $post['extra_field_1'],
  91. 'extra_field_2' => $post['extra_field_2'],
  92. 'restrict_to' => $post['restrict_to'],
  93. 'minimum' => $post['minimum'],
  94. 'maximum' => $post['maximum'],
  95. 'maximum_duration' => $post['maximum_duration']
  96. );
  97. if ($account)
  98. $res = $this->PaymentMethod->updateAccount($accountId, $data);
  99. else $res = $this->PaymentMethod->addAccount($data);
  100. if ($res)
  101. {
  102. $this->session->set_flashdata('success', 'Account updated');
  103. $data = array(
  104. 'success' => 'hurray!',
  105. 'redirect' => array (
  106. 'url' => site_url('adminpanel/cashier/accounts/' . $code)
  107. )
  108. );
  109. }
  110. else
  111. {
  112. $data = array(
  113. 'error' => 'crying face :('
  114. );
  115. }
  116. }
  117. else
  118. {
  119. $data = array(
  120. 'error' => renderErrors($this->form_validation->error_array())
  121. );
  122. }
  123. echo json_encode($data);
  124. return;
  125. }
  126. else
  127. {
  128. $listCountries = $this->User->getCountries();
  129. $countries = dropdown($listCountries, 'name');
  130. $methodsMenu = $this->__methodsMenu($code, site_url('adminpanel/cashier/accounts'));
  131. $this->addJavascript(asset('scripts/forms.js'));
  132. $this->layoutData['title'] = 'Cashier Accounts';
  133. $this->loadView('admin/cashier/account', '', compact('methodsMenu', 'code', 'account', 'countries'));
  134. }
  135. }
  136. public function billing($code)
  137. {
  138. $methodsMenu = $this->__methodsMenu($code, site_url('adminpanel/cashier/billing'));
  139. $this->layoutData['title'] = 'Cashier Billing';
  140. $this->loadView('admin/cashier/index', '', compact('methodsMenu'));
  141. }
  142. public function deposits($code = 'any', $status = 'pending', $page = 1, $perpage = 30)
  143. {
  144. // pending/completed/add new
  145. $methodsMenu = $this->__methodsMenu($code, site_url('adminpanel/cashier/deposits'));
  146. $count = $this->Transaction->countTransactions($code, 'deposit', $status);
  147. if ($count)
  148. {
  149. $data = $this->Transaction->getDepositsSubset($code, $status, $page, $perpage);
  150. $paging = generatePagination(site_url('adminpanel/cashier/deposits/' . $code . '/' . $status), $count, $page, $perpage, true);
  151. $hasPages = $count > $perpage;
  152. $deposits = $this->load->view('admin/cashier/partial/deposits', compact ('data', 'paging', 'hasPages', 'status'), true);
  153. }
  154. else $deposits = "No $status deposits found";
  155. if ($this->input->is_ajax_request())
  156. {
  157. echo $deposits;
  158. }
  159. else
  160. {
  161. $this->addJavascript(asset('scripts/paging.js'));
  162. $codeUrl = site_url('adminpanel/cashier/deposits/' . $code);
  163. $this->layoutData['title'] = 'Cashier Deposits';
  164. $this->loadView('admin/cashier/deposits', '', compact('methodsMenu', 'deposits', 'codeUrl'));
  165. }
  166. }
  167. public function deposit_details($id)
  168. {
  169. if ($this->input->is_ajax_request())
  170. {
  171. $deposit = $this->Transaction->getDetails($id);
  172. $userAccount = $this->PaymentMethod->getAccountForUser($deposit->user_id, $deposit->method);
  173. echo $this->loadPartialView('admin/cashier/partial/deposit_details', compact('deposit', 'userAccount'));
  174. }
  175. else
  176. {
  177. show_404();
  178. }
  179. }
  180. public function cashout_details($id)
  181. {
  182. if ($this->input->is_ajax_request())
  183. {
  184. $cashout = $this->Transaction->getDetails($id);
  185. $userAccount = $this->PaymentMethod->getAccountForUser($cashout->user_id, $cashout->method);
  186. echo $this->loadPartialView('admin/cashier/partial/cashout_details', compact('cashout', 'userAccount'));
  187. }
  188. else
  189. {
  190. show_404();
  191. }
  192. }
  193. public function cashouts($code, $status = 'pending',$page = 1, $perpage = 30)
  194. {
  195. // pending/completed/add new
  196. $methodsMenu = $this->__methodsMenu($code, site_url('adminpanel/cashier/cashouts'));
  197. $count = $this->Transaction->countTransactions($code, 'cashout', $status);
  198. if ($count)
  199. {
  200. $data = $this->Transaction->getCashoutsSubset($code, $status, $page);
  201. $paging = generatePagination(site_url('adminpanel/cashier/cashouts/' . $code . '/' . $status), $count, $page, $perpage, true);
  202. $hasPages = $count > $perpage;
  203. $cashouts = $this->load->view('admin/cashier/partial/cashouts', compact ('data', 'paging', 'hasPages', 'status'), true);
  204. }
  205. else $cashouts = "no $status cashouts found";
  206. if ($this->input->is_ajax_request())
  207. {
  208. echo $cashouts;
  209. }
  210. else
  211. {
  212. $this->addJavascript(asset('scripts/paging.js'));
  213. $codeUrl = site_url('adminpanel/cashier/cashouts/' . $code);
  214. $this->layoutData['title'] = 'Cashier Cashouts';
  215. $this->loadView('admin/cashier/cashouts', '', compact('code', 'methodsMenu', 'cashouts', 'codeUrl', 'type'));
  216. }
  217. }
  218. public function process_cashouts($code)
  219. {
  220. // Find the account(s) to use for the cashout
  221. $sendFrom = $this->PaymentMethod->getAccountDetails($code, 'out');
  222. $cashoutIds = $this->input->post('cashout');
  223. $references = $this->input->post('reference');
  224. $commit = $this->input->post('commit');
  225. // Specific fields
  226. $costs = $this->input->post('cost');
  227. $infos = $this->input->post('info');
  228. $mtcns = $this->input->post('mtcn');
  229. $accounts = $this->input->post('account');
  230. $amounts = $this->input->post('pickup_amount');
  231. $currencies = $this->input->post('pickup_currency');
  232. $cashouts = array();
  233. // let's do the loop - oh yeah!
  234. foreach ($cashoutIds as $cashoutId)
  235. {
  236. $cashout = $this->Transaction->getCashoutDetails($cashoutId);
  237. if ($cashout->status != 'pending')
  238. continue; // if the page times out or is reloaded then cashouts may be sent again - instead we should skip
  239. $reference = isset($references[$cashoutId]) ? $references[$cashoutId] : null;
  240. $cost = isset($costs[$cashoutId]) ? $costs[$cashoutId] : null;
  241. $amount = isset($amounts[$cashoutId]) ? $amounts[$cashoutId] : null;
  242. $currency = isset($currencies[$cashoutId]) ? $currencies[$cashoutId] : null;
  243. $accountId = $accounts[$cashoutId];
  244. $data = null;
  245. // if we actually pressed the button to send the funds
  246. if ($commit)
  247. {
  248. switch ($code)
  249. {
  250. case 'bw':
  251. $info = isset($infos[$cashoutId]) ? $infos[$cashoutId] : null;
  252. if ($cost && $reference && $info)
  253. {
  254. $details = new BankWireDetails();
  255. $details->info = $info;
  256. $details->amount = $amount;
  257. $details->currency = $currency;
  258. $data = array(
  259. 'account_id' => $accountId,
  260. 'details' => $details->__toString(),
  261. 'cost' => $cost,
  262. 'reference' => $reference
  263. );
  264. }
  265. break;
  266. case 'wu':
  267. $mtcn = isset($mtcns[$cashoutId]) ? $mtcns[$cashoutId] : null;
  268. if ($mtcn && $reference && $amount)
  269. {
  270. $details = new WesternUnionDetails($cashout->details);
  271. $details->mtcn = $mtcn;
  272. $details->amount = $amount;
  273. $details->currency = $currency;
  274. $data = array(
  275. 'account_id' => $accountId,
  276. 'details' => $details->__toString(),
  277. 'cost' => 0,
  278. 'reference' => $reference
  279. );
  280. }
  281. break;
  282. // all other methods are (should be) automatic
  283. default:
  284. // manual reference entered so just use it instead of processing the payment
  285. if ($reference)
  286. {
  287. $data = array(
  288. 'account_id' => $accountId,
  289. 'reference' => $reference
  290. );
  291. }
  292. else
  293. {
  294. //TODO: Process automatically (add to an array?)
  295. }
  296. }
  297. }
  298. // If we have data we can try to update the cashout
  299. if ($data && $this->Transaction->update($cashoutId, 'ok', $data))
  300. {
  301. // Refresh the data to grab all the necessary new information
  302. $cashout = $this->Transaction->getCashoutDetails($cashoutId);
  303. $userId = $cashout->user_id;
  304. $email = $this->ion_auth->select('email')->user($userId)->row()->email;
  305. $fromAccount = $this->PaymentMethod->getAccountDetailsById($accountId);
  306. $userAccount = $this->PaymentMethod->getByUserId($userId, $code);
  307. // The cashout has been stored properly so send an email
  308. switch ($code)
  309. {
  310. case 'bw':
  311. $this->EmailQueue->store($email, 'My Traffic Value - Bank Wire Cashout Sent', 'emails/cashier/cashout_sent_bw', compact('cashout', 'userAccount'));
  312. break;
  313. case 'wu':
  314. $this->EmailQueue->store($email, 'My Traffic Value - Western Union Cashout Sent', 'emails/cashier/cashout_sent_wu', compact('cashout', 'fromAccount', 'userAccount'));
  315. break;
  316. }
  317. }
  318. else $cashouts[$cashoutId] = $cashout;
  319. }
  320. if (count ($cashouts))
  321. {
  322. $fromAccounts = dropdown($sendFrom);
  323. $this->layoutData['title'] = 'Process Cashouts';
  324. $this->loadView('admin/cashier/preview_cashouts', '', compact('code', 'cashouts', 'references', 'costs', 'infos', 'mtcns', 'accounts', 'amounts', 'currencies', 'sendFrom', 'fromAccounts'));
  325. }
  326. else redirect('adminpanel/cashier/cashouts/' .$code);
  327. }
  328. public function reject($id)
  329. {
  330. $transactionData = $this->Transaction->getDetails($id);
  331. $type = $transactionData->type;
  332. $userId = $transactionData->user_id;
  333. $email = $this->ion_auth->select('email')->user($userId)->row()->email;
  334. switch ($type)
  335. {
  336. case 'deposit':
  337. $this->EmailQueue->store($email, 'My Traffic Value - Deposit Request Cancelled', 'emails/cashier/deposit_rejected', compact('transactionData'));
  338. break;
  339. case 'cashout':
  340. $this->EmailQueue->store($email, 'My Traffic Value - Cashout Request Cancelled', 'emails/cashier/cashout_rejected', compact('transactionData'));
  341. break;
  342. }
  343. $this->Transaction->update($id, 'reject');
  344. return;
  345. }
  346. public function reset($id)
  347. {
  348. $this->Transaction->update($id, 'pending');
  349. return;
  350. }
  351. public function deposit($depositId, $userId = null, $code = null)
  352. {
  353. $details = null;
  354. if ($depositId > 0)
  355. {
  356. $details = $this->Transaction->getDetails($depositId);
  357. if (!$details || $details->status != 'pending')
  358. redirect('adminpanel/cashier');
  359. $code = $details->method;
  360. $userId = $details->user_id;
  361. }
  362. $username = $this->ion_auth->select('username')->user($userId)->row()->username;
  363. $accounts = dropdown($this->PaymentMethod->getAccountDetails($code, 'in'));
  364. $userAccount = $this->PaymentMethod->getAccountForUser($userId, $code);
  365. if (!$userAccount)
  366. {
  367. // User does not have an account for this payment method
  368. $this->session->set_flashdata('error', "$username does not have a $code account");
  369. redirect('adminpanel/cashier');
  370. }
  371. if ($this->input->is_ajax_request())
  372. {
  373. if ($this->form_validation->run('admin/cashier/deposit') !== false)
  374. {
  375. $post = $this->input->post();
  376. // Ok so we know we need to set the reference number and that it exists
  377. $newData = array(
  378. 'reference' => $post['reference']
  379. );
  380. $depositDetails = null;
  381. switch ($code)
  382. {
  383. case 'bw':
  384. $depositDetails = new BankWireDetails($details ? $details->details : null);
  385. if ($depositDetails->info != $post['info'])
  386. $depositDetails->info = $post['info'];
  387. if ($depositDetails->memo != $post['memo'])
  388. $depositDetails->memo = $post['memo'];
  389. break;
  390. case 'wu':
  391. $depositDetails = new WesternUnionDetails($details ? $details->details : null);
  392. if ($depositDetails->city != $post['city'])
  393. $depositDetails->city = $post['city'];
  394. if ($depositDetails->country != $post['country'])
  395. $depositDetails->country = $post['country'];
  396. if ($depositDetails->mtcn != $post['mtcn'])
  397. $depositDetails->mtcn = $post['mtcn'];
  398. break;
  399. }
  400. if ($details)
  401. {
  402. // Editing a deposit
  403. if ($depositDetails && $depositDetails->__toString() != $details->details)
  404. {
  405. // Some of the details given by the user had to be changed for whatever reason
  406. $newData = array_merge ($newData, array(
  407. 'details' => $depositDetails->__toString()
  408. ));
  409. }
  410. if ($post['gross_amount'] != $details->gross_amount)
  411. {
  412. // If the amount received has changed one then we need to adjust the fee/cost
  413. $grossAmount = $post['gross_amount'];
  414. $fee = roundUp($this->PaymentMethod->calculateGross($grossAmount, $code, 'fee', 'deposit'));
  415. $netAmount = $grossAmount - $fee;
  416. $newData = array_merge ($newData, array(
  417. 'gross_amount' => $grossAmount,
  418. 'amount' => $netAmount,
  419. 'fee' => $fee
  420. ));
  421. }
  422. if ($post['cost'] != $details->cost)
  423. {
  424. $newData = array_merge ($newData, array(
  425. 'cost' => $post['cost']
  426. ));
  427. }
  428. // if the account has changed (mainly for Bank Wires)
  429. if ($post['account_id'] != $details->account_id)
  430. {
  431. $newData = array_merge ($newData, array(
  432. 'account_id' => $post['account_id']
  433. ));
  434. }
  435. }
  436. else
  437. {
  438. // Adding a deposit
  439. $depositData = array(
  440. 'user_id' => $userId,
  441. 'method' => $code,
  442. 'account_id' => $post['account_id'],
  443. 'gross_amount' => $post['gross_amount'],
  444. 'cost' => isset($post['cost']) ? $post['cost'] : null,
  445. 'identifier' => $this->Transaction->identifier()
  446. );
  447. if ($depositDetails)
  448. $depositData['details'] = $depositDetails->__toString();
  449. $depositId = $this->Transaction->addDeposit($depositData);
  450. }
  451. if ($this->Transaction->update($depositId, 'ok', $newData))
  452. {
  453. // Email the user when we got the deposit from Western Union or Bank Wire
  454. if ($code == 'bw' || $code == 'wu')
  455. {
  456. $email = $this->ion_auth->select('email')->user($userId)->row()->email;
  457. $newDepositData = $this->Transaction->getById($depositId);
  458. $this->EmailQueue->store($email, 'My Traffic Value - Deposit Received', 'emails/cashier/deposit_received_' . $code, compact('newDepositData', 'depositDetails', 'userAccount'));
  459. }
  460. $this->session->set_flashdata('success', 'Successfully added deposit');
  461. $data = array(
  462. 'success' => 'success',
  463. 'redirect' => array(
  464. 'url' => site_url('adminpanel/cashier'),
  465. )
  466. );
  467. }
  468. else
  469. {
  470. $data = array(
  471. 'error' => 'Problem with the deposit data'
  472. );
  473. }
  474. }
  475. else
  476. {
  477. $data = array(
  478. 'error' => renderErrors($this->form_validation->error_array())
  479. );
  480. }
  481. echo json_encode($data);
  482. return;
  483. }
  484. $this->addJavascript(asset('scripts/forms.js'));
  485. $listCountries = $this->User->getCountries();
  486. $countries = dropdown($listCountries, 'name');
  487. $this->layoutData['title'] = 'Manage Deposit';
  488. $this->loadView('admin/cashier/deposit', '', compact('userId', 'username', 'code', 'details', 'userAccount', 'accounts', 'countries'));
  489. }
  490. public function account_status()
  491. {
  492. if ($this->input->is_ajax_request())
  493. {
  494. $accountId = $this->input->post('account_id');
  495. $enabled = $this->input->post('enabled');
  496. $this->PaymentMethod->accountStatus($accountId, $enabled ? 1 : 0);
  497. }
  498. }
  499. public function settings($code = null)
  500. {
  501. if ($this->input->is_ajax_request())
  502. {
  503. $post = $this->input->post();
  504. $data = array(
  505. 'percent' => isset($post['percent']) ? $post['percent'] : NULL,
  506. 'fixed' => isset($post['fixed']) ? $post['fixed'] : NULL,
  507. 'max' => $post['max'] ? $post['max'] : NULL
  508. );
  509. if ($this->PaymentMethod->updateMethodBill($code, $post['type'], $post['operation'], $data))
  510. {
  511. $data = array(
  512. 'success' => 'success'
  513. );
  514. }
  515. else
  516. {
  517. $data = array(
  518. 'error' => 'Error Updating Data!'
  519. );
  520. }
  521. echo json_encode($data);
  522. }
  523. else
  524. {
  525. // We need to get the deposit and cashout fees + costs all the time
  526. $billing = array(
  527. 'deposit' => array(
  528. 'fee' => $this->PaymentMethod->getFeeData($code, 'deposit', 'fee'),
  529. 'cost' => $this->PaymentMethod->getFeeData($code, 'deposit', 'cost')
  530. ),
  531. 'cashout' => array(
  532. 'fee' => $this->PaymentMethod->getFeeData($code, 'cashout', 'fee'),
  533. 'cost' => $this->PaymentMethod->getFeeData($code, 'cashout', 'cost')
  534. )
  535. );
  536. $this->addJavascript(asset('scripts/forms.js'));
  537. $this->layoutData['title'] = 'Cashier - ' . strtoupper($code) . ' Settings';
  538. $this->loadView('admin/cashier/settings', '', compact('billing', 'code'));
  539. }
  540. }
  541. public function user_account_details($userId, $code)
  542. {
  543. $data = $this->PaymentMethod->getAccountForUser($userId, $code);
  544. $account = null;
  545. switch($code)
  546. {
  547. case 'lr':
  548. case 'ap': $account = $data->account; break;
  549. case 'wu': $account = new WesternUnion($data->account); break;
  550. case 'bw': $account = new BankWire($data->account); break;
  551. }
  552. echo $this->loadPartialView('admin/cashier/partial/account_details', compact('code', 'account'));
  553. }
  554. public function get_users_balances($code, $page = 1, $perpage = 50)
  555. {
  556. $this->load->model('cashier_model','Cashier');
  557. $balances = $this->Cashier->getUsersBalances($code, $page, $perpage);
  558. $count = $this->Cashier->countGetUsersBalances($code);
  559. $balance_paging = generatePagination(site_url("adminpanel/cashier/get_users_balances/$code"), $count, $page, $perpage, true);
  560. $balance_hasPages = $count > $perpage;
  561. $balanceUserTable = $this->loadPartialView('admin/cashier/partial/user_balances_table.php',compact('balances','balance_paging','balances_hasPages','count','code'));
  562. $methodsMenu = $this->__methodsMenu($code, site_url('adminpanel/cashier/accounts'));
  563. if ($this->input->is_ajax_request())
  564. {
  565. $data = array('html' => $balanceUserTable);
  566. echo json_encode($data);
  567. }
  568. else
  569. $this->layoutData['title'] = 'Balances';
  570. ' - User Balances';
  571. $this->loadView('admin/cashier/user_balances', 'My Traffic Value: Admin', compact('balanceUserTable','methodsMenu','balance_paging','balances_hasPages'));
  572. //echo show_404();
  573. }
  574. // Temporary function
  575. public function fixTransaction($ref, $amount)
  576. {
  577. $this->Cashier->fixTransaction($ref, $amount);
  578. }
  579. }