Guest User

Untitled

a guest
Jan 11th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.81 KB | None | 0 0
  1. <?php
  2.  
  3. class AdminController extends \BaseController
  4. {
  5.  
  6. /**
  7. * Find purchase page
  8. */
  9. public function pending_approvals()
  10. {
  11. $purchases = Purchase::with('customer', 'salesrep', 'referral')
  12. ->where('approved', '=', false)
  13. ->where('reject', '=', false)
  14. ->get();
  15. $sales_reps = User::getSalesReps();
  16.  
  17. return View::make('kiosk.panels.pending_approvals', array(
  18. 'purchases' => $purchases,
  19. 'sales_reps' => $sales_reps
  20. ));
  21. }
  22.  
  23. /**
  24. * Add referral page (searches for customer
  25. * and displays purchase
  26. */
  27. public function approve_purchase($id)
  28. {
  29. $purchase = Purchase::with('referral', 'customer', 'salesrep')->find($id);
  30. $lead = Lead::where('customer_id', '=', $purchase->customer_id)->first();
  31. $sales_reps = User::getSalesReps();
  32.  
  33. return View::make('kiosk.panels.approve_purchase', array(
  34. 'purchase' => $purchase,
  35. 'sales_reps' => $sales_reps,
  36. 'lead' => $lead
  37. ));
  38. }
  39.  
  40. /**
  41. * Add points to designated referrer
  42. */
  43. public function add_points($id)
  44. {
  45. $purchase = Purchase::find($id);
  46. $referrer = User::find(Input::get('referral_id'));
  47. $points = floatval(substr(str_replace(',', '', Input::get('points')), 1));
  48.  
  49. $errors = array();
  50.  
  51. if ($referrer === null) {
  52. $errors[] = 'Please select a valid referrer.';
  53. }
  54.  
  55. if (!$points) {
  56. $errors[] = 'Please enter the number of points to be added to the referrer.';
  57. } elseif ($points < 0) {
  58. $errors[] = 'Please enter a positive number of points to be added to the referrer.';
  59. }
  60.  
  61. if (empty($errors)) {
  62. $purchase->approved = true;
  63. $purchase->save();
  64. $referrer->points += $points;
  65. $referrer->save();
  66.  
  67. $transaction = new Transaction();
  68. $transaction->customer_id = $referrer->id;
  69. $transaction->amount = $points;
  70. $transaction->description = 'Referred purchase # ' . $purchase->id;
  71. $transaction->stock_repair_number = $purchase->stock_number;
  72. $transaction->referred_redeemed = $purchase->customer->getFullName();
  73. $transaction->authorizer_id = Auth::user()->id;
  74. $transaction->save();
  75.  
  76. Session::flash('message', $referrer->getFullName() . ' has been credited for referring a purchase!');
  77.  
  78. return Redirect::route('kiosk-pending-approvals');
  79. }
  80.  
  81. return Redirect::action('AdminController@approve_purchase', array('id' => $id))->withErrors($errors);
  82. }
  83.  
  84. /**
  85. * Reject points
  86. */
  87. public function reject_points($id)
  88. {
  89. $purchase = Purchase::find($id);
  90. $purchase->reject = true;
  91. $purchase->save();
  92. Session::flash('message', 'Purchase #' . $purchase->id . ' has been rejected for points.');
  93. return Redirect::route('kiosk-pending-approvals');
  94. }
  95.  
  96. /**
  97. * Redeem points from a customer
  98. */
  99. public function redeem_points($id)
  100. {
  101. $customer = User::with('transactions')->find($id);
  102.  
  103. $customer_purchasers = array($customer->getFullName());
  104. if ($customer->approved_purchasers) {
  105. $purchasers = explode("\n", str_replace("\n\r", "\n", $customer->approved_purchasers));
  106. $customer_purchasers = array_merge($customer_purchasers, $purchasers);
  107. }
  108. $purchasers = array();
  109. foreach($customer_purchasers as $value) {
  110. $purchasers[$value] = $value;
  111. }
  112.  
  113. $customer->transactions = Transaction::orderByCreatedAt($customer->transactions, 'desc');
  114. $customer->transactions = Transaction::addGrandTotals($customer->transactions, $customer->points);
  115.  
  116. return View::make('kiosk.panels.redeem_points',
  117. array(
  118. 'customer' => $customer,
  119. 'customer_purchasers' => $purchasers
  120. ));
  121. }
  122.  
  123. /**
  124. * Subtract points and create transaction if
  125. * points are valid
  126. */
  127. public function subtract_points($id)
  128. {
  129. $customer = User::find($id);
  130. $points = floatval(substr(str_replace(',', '', Input::get('points')), 1));
  131. $description = Input::get('description');
  132. $approved_purchaser = Input::get('approved_purchaser');
  133. $repair_number = Input::get('repair_number');
  134.  
  135. $errors = array();
  136.  
  137. if ($points == 0) {
  138. $errors[] = 'Please enter an amount to redeem.';
  139. } elseif ($points < 0) {
  140. $errors[] = 'Please enter a positive amount.';
  141. } elseif ($customer->points - $points < 0) {
  142. $errors[] = $customer->getFullName() . ' does not have enough points.';
  143. }
  144.  
  145. if (!$description) $errors[] = 'Please enter a description.';
  146.  
  147. if (!$repair_number) $errors[] = 'Please enter a repair number.';
  148.  
  149. if (empty($errors)) {
  150. $customer->points -= $points;
  151. $customer->save();
  152.  
  153. $transaction = new Transaction();
  154. $transaction->customer_id = $id;
  155. $transaction->amount = $points * (-1);
  156. $transaction->description = $description;
  157. $transaction->stock_repair_number = $repair_number;
  158. $transaction->referred_redeemed = $approved_purchaser;
  159. // $transaction->authorizer_id = Auth::user()->id;
  160. $transaction->save();
  161.  
  162. Session::flash('message', $customer->getFullName() . " has redeemed $points points!");
  163.  
  164. return Redirect::action('AdminController@redeem_points', array('id' => $id));
  165. }
  166.  
  167. return Redirect::action('AdminController@redeem_points', array('id' => $id))->withErrors($errors)->withInput();
  168. }
  169.  
  170. /**
  171. * Get form with transaction summary
  172. */
  173. public function transaction_summary($id)
  174. {
  175. $customer = User::with('transactions')->find($id);
  176. $customer->transactions = Transaction::orderByCreatedAt($customer->transactions, 'desc');
  177. $customer->transactions = Transaction::addGrandTotals($customer->transactions, $customer->points);
  178.  
  179. return View::make('kiosk.panels.transaction_summary', array(
  180. 'customer' => $customer
  181. ));
  182. }
  183.  
  184. public function accountPage()
  185. {
  186. return View::make('kiosk.account', array(
  187. 'user' => Auth::user()
  188. ));
  189. }
  190.  
  191. public function updateAccount()
  192. {
  193. $rules = array(
  194. 'first_name' => 'required|alpha',
  195. 'last_name' => 'required|alpha',
  196. 'state' => 'alpha|size:2',
  197. 'password' => 'confirmed',
  198. 'password_confirmation' => 'same:password'
  199. );
  200. $validator = Validator::make(Input::all(), $rules);
  201.  
  202. if ($validator->passes()) {
  203. $user = Auth::user();
  204. $user->first_name = Input::get('first_name');
  205. $user->last_name = Input::get('last_name');
  206. $user->city = Input::get('city');
  207. $user->state = Input::get('state');
  208. if (Input::get('password')) $user->password = Hash::make(Input::get('password'));
  209. $user->save();
  210. return Redirect::route('kiosk')->with('message', 'Your account has been updated!');
  211. }
  212.  
  213. return Redirect::route('kiosk_account')->withInput()->withErrors($validator->messages());
  214. }
  215.  
  216. }
Add Comment
Please, Sign In to add comment