PageRenderTime 35ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Http/Controllers/BankController.php

https://bitbucket.org/kiruthiga208/expertplus_enc
PHP | 369 lines | 261 code | 68 blank | 40 comment | 41 complexity | cae324c377dfce8de627020eee8fa3d8 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause, MIT
  1. <?php namespace App\Http\Controllers;
  2. use App\Http\Controllers\Controller;
  3. use App\Models\Bank;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Pagination\LengthAwarePaginator as Paginator;
  6. use Validator, Input, Redirect ;
  7. use App\Models\Course;
  8. use App\Models\bsetec;
  9. use App\Models\Transaction;
  10. use App\Models\UserCredits;
  11. use App\Models\CourseTaken;
  12. use App\Models\Coupon;
  13. use App\Models\Admincoupon;
  14. use danielme85\CConverter\Currency;
  15. use Carbon\Carbon;
  16. class BankController extends Controller {
  17. protected $layout = "layouts.main";
  18. protected $data = array();
  19. public $module = 'bank';
  20. static $per_page = '10';
  21. public function __construct()
  22. {
  23. $this->bsetec = new bsetec();
  24. $this->transaction = new Transaction();
  25. $this->course = new Course();
  26. $this->user_credits = new UserCredits();
  27. $this->model = new Bank();
  28. $this->info = $this->model->makeInfo( $this->module);
  29. $this->access = $this->model->validAccess($this->info['id']);
  30. $this->data = array(
  31. 'pageTitle' => $this->info['title'],
  32. 'pageNote' => $this->info['note'],
  33. 'pageModule'=> 'bank',
  34. 'return' => self::returnUrl()
  35. );
  36. }
  37. public function Index( Request $request )
  38. {
  39. if($this->access['is_view'] ==0)
  40. return Redirect::to('dashboard')
  41. ->with('messagetext', \Lang::get('core.note_restric'))->with('msgstatus','error');
  42. $sort = (!is_null($request->input('sort')) ? $request->input('sort') : 'id');
  43. $order = (!is_null($request->input('order')) ? $request->input('order') : 'asc');
  44. // End Filter sort and order for query
  45. // Filter Search for query
  46. $filter = (!is_null($request->input('search')) ? $this->buildSearch() : '');
  47. $page = $request->input('page', 1);
  48. $params = array(
  49. 'page' => $page ,
  50. 'limit' => (!is_null($request->input('rows')) ? filter_var($request->input('rows'),FILTER_VALIDATE_INT) : static::$per_page ) ,
  51. 'sort' => $sort ,
  52. 'order' => $order,
  53. 'params' => $filter,
  54. 'global' => (isset($this->access['is_global']) ? $this->access['is_global'] : 0 )
  55. );
  56. // Get Query
  57. $results = $this->model->getRows( $params );
  58. // Build pagination setting
  59. $page = $page >= 1 && filter_var($page, FILTER_VALIDATE_INT) !== false ? $page : 1;
  60. $pagination = new Paginator($results['rows'], $results['total'], $params['limit']);
  61. $pagination->setPath('bank');
  62. $this->data['rowData'] = $results['rows'];
  63. // Build Pagination
  64. $this->data['pagination'] = $pagination;
  65. // Build pager number and append current param GET
  66. $this->data['pager'] = $this->injectPaginate();
  67. // Row grid Number
  68. $this->data['i'] = ($page * $params['limit'])- $params['limit'];
  69. // Grid Configuration
  70. $this->data['tableGrid'] = $this->info['config']['grid'];
  71. $this->data['tableForm'] = $this->info['config']['forms'];
  72. $this->data['colspan'] = \SiteHelpers::viewColSpan($this->info['config']['grid']);
  73. // Group users permission
  74. $this->data['access'] = $this->access;
  75. // Detail from master if any
  76. // Master detail link if any
  77. $this->data['subgrid'] = (isset($this->info['config']['subgrid']) ? $this->info['config']['subgrid'] : array());
  78. // Render into template
  79. return view('bank.index',$this->data);
  80. }
  81. function getUpdate(Request $request, $id = null)
  82. {
  83. if($id =='')
  84. {
  85. if($this->access['is_add'] ==0 )
  86. return Redirect::to('dashboard')->with('messagetext',\Lang::get('core.note_restric'))->with('msgstatus','error');
  87. }
  88. if($id !='')
  89. {
  90. if($this->access['is_edit'] ==0 )
  91. return Redirect::to('dashboard')->with('messagetext',\Lang::get('core.note_restric'))->with('msgstatus','error');
  92. }
  93. $row = $this->model->find($id);
  94. if($row)
  95. {
  96. $this->data['row'] = $row;
  97. } else {
  98. $this->data['row'] = $this->model->getColumnTable('transactions');
  99. }
  100. $this->data['id'] = $id;
  101. return view('bank.form',$this->data);
  102. }
  103. public function getShow( $id = null)
  104. {
  105. if($this->access['is_detail'] ==0)
  106. return Redirect::to('dashboard')
  107. ->with('messagetext', Lang::get('core.note_restric'))->with('msgstatus','error');
  108. $row = $this->model->getRow($id);
  109. if($row)
  110. {
  111. $this->data['row'] = $row;
  112. } else {
  113. $this->data['row'] = $this->model->getColumnTable('transactions');
  114. }
  115. $this->data['id'] = $id;
  116. $this->data['access'] = $this->access;
  117. return view('bank.view',$this->data);
  118. }
  119. function postSave( Request $request, $id =0)
  120. {
  121. $rules = $this->validateForm();
  122. $validator = Validator::make($request->all(), $rules);
  123. if ($validator->passes()) {
  124. $data = $this->validatePost('tb_bank');
  125. $newID = $this->model->insertRow($data , $request->input('id'));
  126. if(!is_null($request->input('apply')))
  127. {
  128. $return = 'bank/update/'.$id.'?return='.self::returnUrl();
  129. } else {
  130. $return = 'bank?return='.self::returnUrl();
  131. }
  132. // Insert logs into database
  133. if($id ==0)
  134. {
  135. \SiteHelpers::auditTrail( $request , 'New Data with ID '.$newID.' Has been Inserted !');
  136. } else {
  137. \SiteHelpers::auditTrail($request ,'Data with ID '.$newID.' Has been Updated !');
  138. }
  139. return Redirect::to($return)->with('messagetext',\Lang::get('core.note_success'))->with('msgstatus','success');
  140. } else {
  141. return Redirect::to('bank/update/'.$id)->with('messagetext',\Lang::get('core.note_error'))->with('msgstatus','error')
  142. ->withErrors($validator)->withInput();
  143. }
  144. }
  145. public function postDelete( Request $request)
  146. {
  147. if($this->access['is_remove'] ==0)
  148. return Redirect::to('dashboard')
  149. ->with('messagetext', \Lang::get('core.note_restric'))->with('msgstatus','error');
  150. // delete multipe rows
  151. if(count($request->input('id')) >=1)
  152. {
  153. $this->model->destroy($request->input('id'));
  154. \SiteHelpers::auditTrail( $request , "ID : ".implode(",",$request->input('id'))." , Has Been Removed Successfull");
  155. // redirect
  156. return Redirect::to('bank')
  157. ->with('messagetext', \Lang::get('core.note_success_delete'))->with('msgstatus','success');
  158. } else {
  159. return Redirect::to('bank')
  160. ->with('messagetext','No Item Deleted')->with('msgstatus','error');
  161. }
  162. }
  163. // Bank Transfer and Cash on Delivery
  164. public function postCashondelivery(Request $request){
  165. $rules = array(
  166. 'price' => 'required',
  167. 'name' => 'required',
  168. 'phone' => 'required'
  169. );
  170. if($request->paymenttype=='bank') {
  171. $rules['bankdetails']='required';
  172. } else {
  173. $rules['address']='required';
  174. }
  175. if(array_key_exists('course_id', $request->all())) {
  176. $course_id = $request->input('course_id');
  177. } else {
  178. $course_id = '';
  179. }
  180. if(array_key_exists('purchasetype', $request->all())){
  181. $purchasetype = $request->get('purchasetype');
  182. } else {
  183. $purchasetype = '';
  184. }
  185. $validator = Validator::make($request->all(), $rules);
  186. if($validator->fails()){
  187. if($request->devicetypes=='moblie' && $course_id){
  188. return Redirect::to('payment-form/'.$course_id.'/'.$request->input('uid'))->with('messagetext',\Lang::get('core.note_error'))->with('msgstatus','error')->withErrors($validator)->withInput();
  189. }
  190. return Redirect::to('payment/form')->with('messagetext',\Lang::get('core.note_error'))->with('msgstatus','error')->withErrors($validator)->withInput();
  191. }
  192. $details = array('Name' => $request->input('name'), 'phone' => $request->input('phone'));
  193. ($request->paymenttype == 'bank') ? $details['Bankdetails'] = $request->input('bankdetails') : $details['Address'] = $request->input('address');
  194. $detail = json_encode($details);
  195. if($request->devicetypes=='moblie'){
  196. $this->transaction->user_id = $request->input('uid');
  197. }else{
  198. $this->transaction->user_id = \Session::get('uid');
  199. }
  200. $this->transaction->course_id = $course_id;
  201. $coupon_price = $request->input('coupon_price');
  202. if($coupon_price) {
  203. $amount = $coupon_price;
  204. }
  205. else {
  206. $amount = $request->input('price');
  207. }
  208. $this->transaction->amount = $amount;
  209. $this->transaction->status = 'pending';
  210. $this->transaction->order_details = $detail;
  211. $this->transaction->payment_method = $request->input('paymenttype');
  212. $this->transaction->purchase_type = $purchasetype;
  213. $this->transaction->save();
  214. if($purchasetype==1){
  215. $updateWallet = \DB::table('users')->where('id', $this->transaction->user_id)->update(['wallet'=> 0]);
  216. }
  217. if($course_id)
  218. $course = Course::find($course_id);
  219. $approve = $this->bsetec->get_options('autoApprove');
  220. if(trim($request->devicetypes)=='mobile' && $course_id) {
  221. return view('payment-mobile/success')->with('course', $course)->with('status', 'success')->with('transId', $this->transaction->id)->with('title', 'Course');
  222. }
  223. if($purchasetype=='3'){
  224. return Redirect::to('user/wallet')->with('messagetext','request sent to admin')->with('msgstatus','success');
  225. } else {
  226. if($approve['autoApprove']=='1'){
  227. $courseTaken = new CourseTaken;
  228. $courseTaken->user_id = \Session::get('uid');
  229. $courseTaken->course_id = $course_id;
  230. $courseTaken->type = 'course';
  231. $courseTaken->save();
  232. return view('course/success')->with('course', $course)->with('status', 'success')->with('transId', $this->transaction->id)->with('title','Course');
  233. } else {
  234. return Redirect::to('course')->with('messagetext','request sent to admin')->with('msgstatus','success');
  235. }
  236. }
  237. }
  238. function generate_invoice($transaction_id)
  239. {
  240. $transaction_detail = $this->transaction->get_transaction_detail($transaction_id);
  241. // echo '<pre>';print_r($transaction_detail);exit;
  242. //update the order details on transaction table
  243. $invoice['transaction_id'] = $transaction_detail->id;
  244. $invoice['customer_name'] = $transaction_detail->customer_name;
  245. $invoice['course_title'] = $transaction_detail->course_title;
  246. $invoice['amount'] = $transaction_detail->amount;
  247. $invoice['email'] = $transaction_detail->email;
  248. $invoice['status'] = $transaction_detail->status;
  249. $invoice['payment_method'] = $transaction_detail->payment_method;
  250. $invoice['invoice_number'] = time().$transaction_id;
  251. $invoice['ordered_on'] = date('Y-m-d H:i:s');
  252. $this->transaction->save_invoice($invoice);
  253. $email = $invoice['email'];
  254. $this->data['invoice'] = $invoice;
  255. // echo view('course.invoice',$this->data);exit;
  256. //send mail to the customer
  257. /*\Mail::send('course.invoice', $this->data, function($message) use($email)
  258. {
  259. $message->subject('Invoice from expert plus');
  260. $message->from('admin@expertplus.com', 'Expert Plus');
  261. $message->to($email);
  262. //pass the same data to pdf view file and generate a pdf
  263. $pdf = \PDF::loadView('course.invoice-pdf', $this->data);
  264. //return $pdf->download('invoice.pdf');
  265. //get the pdf file as data
  266. $attach = $pdf->stream();
  267. //attach the pdf in mail
  268. $message->attachData($attach, 'invoice.pdf');
  269. });*/
  270. }
  271. function save_credits($transaction_id)
  272. {
  273. //get transaction details
  274. $transaction = $this->transaction->find($transaction_id);
  275. //get commision percentage from db
  276. $commision_percentage = $this->bsetec->get_option('commision_percentage');
  277. //calculate the credits
  278. $amount = $transaction->amount;
  279. $admin_credit = ($amount * $commision_percentage)/100;
  280. $instructor_credit = $amount - $admin_credit;
  281. //get instructor id for the course id
  282. $instructor_id = $this->course->getCourseInstructor($transaction->course_id);
  283. //save credit for instructor
  284. $credit['transaction_id'] = $transaction_id;
  285. $credit['instructor_user_id'] = $instructor_id;
  286. $credit['course_id'] = $transaction->course_id;
  287. $credit['by_user_id'] = $transaction->user_id;
  288. $credit['is_admin'] = 0;
  289. $credit['credits_for'] = 'course_cost';
  290. $credit['credit'] = $instructor_credit;
  291. $credit['created_at'] = time();
  292. $this->user_credits->save_credits($credit);
  293. //save credit for instructor
  294. $credit['instructor_user_id'] = 0;
  295. $credit['is_admin'] = 1;
  296. $credit['credits_for'] = 'course_commision';
  297. $credit['credit'] = $admin_credit;
  298. $this->user_credits->save_credits($credit);
  299. }
  300. }