Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class AdminController extends \BaseController
- {
- /**
- * Find purchase page
- */
- public function pending_approvals()
- {
- $purchases = Purchase::with('customer', 'salesrep', 'referral')
- ->where('approved', '=', false)
- ->where('reject', '=', false)
- ->get();
- $sales_reps = User::getSalesReps();
- return View::make('kiosk.panels.pending_approvals', array(
- 'purchases' => $purchases,
- 'sales_reps' => $sales_reps
- ));
- }
- /**
- * Add referral page (searches for customer
- * and displays purchase
- */
- public function approve_purchase($id)
- {
- $purchase = Purchase::with('referral', 'customer', 'salesrep')->find($id);
- $lead = Lead::where('customer_id', '=', $purchase->customer_id)->first();
- $sales_reps = User::getSalesReps();
- return View::make('kiosk.panels.approve_purchase', array(
- 'purchase' => $purchase,
- 'sales_reps' => $sales_reps,
- 'lead' => $lead
- ));
- }
- /**
- * Add points to designated referrer
- */
- public function add_points($id)
- {
- $purchase = Purchase::find($id);
- $referrer = User::find(Input::get('referral_id'));
- $points = floatval(substr(str_replace(',', '', Input::get('points')), 1));
- $errors = array();
- if ($referrer === null) {
- $errors[] = 'Please select a valid referrer.';
- }
- if (!$points) {
- $errors[] = 'Please enter the number of points to be added to the referrer.';
- } elseif ($points < 0) {
- $errors[] = 'Please enter a positive number of points to be added to the referrer.';
- }
- if (empty($errors)) {
- $purchase->approved = true;
- $purchase->save();
- $referrer->points += $points;
- $referrer->save();
- $transaction = new Transaction();
- $transaction->customer_id = $referrer->id;
- $transaction->amount = $points;
- $transaction->description = 'Referred purchase # ' . $purchase->id;
- $transaction->stock_repair_number = $purchase->stock_number;
- $transaction->referred_redeemed = $purchase->customer->getFullName();
- $transaction->authorizer_id = Auth::user()->id;
- $transaction->save();
- Session::flash('message', $referrer->getFullName() . ' has been credited for referring a purchase!');
- return Redirect::route('kiosk-pending-approvals');
- }
- return Redirect::action('AdminController@approve_purchase', array('id' => $id))->withErrors($errors);
- }
- /**
- * Reject points
- */
- public function reject_points($id)
- {
- $purchase = Purchase::find($id);
- $purchase->reject = true;
- $purchase->save();
- Session::flash('message', 'Purchase #' . $purchase->id . ' has been rejected for points.');
- return Redirect::route('kiosk-pending-approvals');
- }
- /**
- * Redeem points from a customer
- */
- public function redeem_points($id)
- {
- $customer = User::with('transactions')->find($id);
- $customer_purchasers = array($customer->getFullName());
- if ($customer->approved_purchasers) {
- $purchasers = explode("\n", str_replace("\n\r", "\n", $customer->approved_purchasers));
- $customer_purchasers = array_merge($customer_purchasers, $purchasers);
- }
- $purchasers = array();
- foreach($customer_purchasers as $value) {
- $purchasers[$value] = $value;
- }
- $customer->transactions = Transaction::orderByCreatedAt($customer->transactions, 'desc');
- $customer->transactions = Transaction::addGrandTotals($customer->transactions, $customer->points);
- return View::make('kiosk.panels.redeem_points',
- array(
- 'customer' => $customer,
- 'customer_purchasers' => $purchasers
- ));
- }
- /**
- * Subtract points and create transaction if
- * points are valid
- */
- public function subtract_points($id)
- {
- $customer = User::find($id);
- $points = floatval(substr(str_replace(',', '', Input::get('points')), 1));
- $description = Input::get('description');
- $approved_purchaser = Input::get('approved_purchaser');
- $repair_number = Input::get('repair_number');
- $errors = array();
- if ($points == 0) {
- $errors[] = 'Please enter an amount to redeem.';
- } elseif ($points < 0) {
- $errors[] = 'Please enter a positive amount.';
- } elseif ($customer->points - $points < 0) {
- $errors[] = $customer->getFullName() . ' does not have enough points.';
- }
- if (!$description) $errors[] = 'Please enter a description.';
- if (!$repair_number) $errors[] = 'Please enter a repair number.';
- if (empty($errors)) {
- $customer->points -= $points;
- $customer->save();
- $transaction = new Transaction();
- $transaction->customer_id = $id;
- $transaction->amount = $points * (-1);
- $transaction->description = $description;
- $transaction->stock_repair_number = $repair_number;
- $transaction->referred_redeemed = $approved_purchaser;
- // $transaction->authorizer_id = Auth::user()->id;
- $transaction->save();
- Session::flash('message', $customer->getFullName() . " has redeemed $points points!");
- return Redirect::action('AdminController@redeem_points', array('id' => $id));
- }
- return Redirect::action('AdminController@redeem_points', array('id' => $id))->withErrors($errors)->withInput();
- }
- /**
- * Get form with transaction summary
- */
- public function transaction_summary($id)
- {
- $customer = User::with('transactions')->find($id);
- $customer->transactions = Transaction::orderByCreatedAt($customer->transactions, 'desc');
- $customer->transactions = Transaction::addGrandTotals($customer->transactions, $customer->points);
- return View::make('kiosk.panels.transaction_summary', array(
- 'customer' => $customer
- ));
- }
- public function accountPage()
- {
- return View::make('kiosk.account', array(
- 'user' => Auth::user()
- ));
- }
- public function updateAccount()
- {
- $rules = array(
- 'first_name' => 'required|alpha',
- 'last_name' => 'required|alpha',
- 'state' => 'alpha|size:2',
- 'password' => 'confirmed',
- 'password_confirmation' => 'same:password'
- );
- $validator = Validator::make(Input::all(), $rules);
- if ($validator->passes()) {
- $user = Auth::user();
- $user->first_name = Input::get('first_name');
- $user->last_name = Input::get('last_name');
- $user->city = Input::get('city');
- $user->state = Input::get('state');
- if (Input::get('password')) $user->password = Hash::make(Input::get('password'));
- $user->save();
- return Redirect::route('kiosk')->with('message', 'Your account has been updated!');
- }
- return Redirect::route('kiosk_account')->withInput()->withErrors($validator->messages());
- }
- }
Add Comment
Please, Sign In to add comment