/**
  * function to get summary of balin checkout
  * 
  * 1. Get Session Cart & transaction
  * 2. Generate breadcrumb
  * 3. Generate view
  * @return view
  */
 public function get()
 {
     //1.Get Session Cart & transaction
     $carts = Session::get('carts');
     $APIUser = new APIUser();
     $order = $APIUser->getMeOrderInCart(['user_id' => Session::get('whoami')['id']]);
     if ($order['status'] != 'success') {
         Session::forget('carts');
         \App::abort(404);
     }
     //1a. get my point
     $my_point = $APIUser->getMeDetail(['user_id' => Session::get('whoami')['id']]);
     //1b. get my address
     $my_address = $APIUser->getMeAddress(['user_id' => Session::get('whoami')['id']]);
     //1c. get list product extension
     $APIProductExtension = new APIProductExtension();
     $product_extension = $APIProductExtension->getIndex();
     if ($product_extension['status'] != 'success') {
         $product_extension = null;
     }
     //1d. get list product extension
     $APICourier = new APICourier();
     $courier = $APICourier->getIndex();
     if ($courier['status'] != 'success') {
         $courier = null;
     }
     //2. Generate breadcrumb
     $breadcrumb = ['Checkout' => route('my.balin.checkout.get')];
     $this->page_attributes->breadcrumb = array_merge($this->page_attributes->breadcrumb, $breadcrumb);
     //3. Generate view
     $this->page_attributes->data = ['carts' => $carts, 'order' => $order, 'my_point' => $my_point['data']['total_point'], 'my_address' => $my_address['data']['data'], 'product_extension' => $product_extension, 'courier' => $courier['data']['data']];
     $this->page_attributes->subtitle = 'Checkout';
     $this->page_attributes->source = $this->page_attributes->source . 'index';
     return $this->generateView();
 }
 /**
  * function to generate view redeem point for balin
  *
  * @return view
  */
 public function index()
 {
     $APIUser = new APIUser();
     $me_detail = $APIUser->getMeDetail(['user_id' => Session::get('whoami')['id']]);
     $this->page_attributes->data = ['me' => $me_detail];
     $this->page_attributes->subtitle = 'Redeem Code';
     $this->page_attributes->breadcrumb = array_merge($this->page_attributes->breadcrumb);
     $this->page_attributes->source = $this->page_attributes->source . 'index';
     return $this->generateView();
 }
 /**
  * function to store invitation for others
  *
  * @param referral_code
  */
 public function store()
 {
     /* get for redirect route to */
     $APIUser = new APIUser();
     $whoami = $APIUser->getMeDetail(['user_id' => Session::get('whoami')['id']]);
     $invitations = [];
     $emails = explode(',', Input::get('emails'));
     $rules = ['email' => 'required|email|max:255'];
     foreach ($emails as $key => $value) {
         $invitation['email'] = trim($value);
         $validator = Validator::make($invitation, $rules);
         if (!$validator->passes()) {
             $this->errors = ['Email "' . $invitation['email'] . '" tidak sah'];
             return $this->generateRedirectRoute('my.balin.redeem.index');
         }
         $invitations[] = $invitation['email'];
     }
     /* array parameter to API */
     $data = ['user_id' => Session::get('whoami')['id'], 'invitations' => $emails];
     $result = $APIUser->postMeInvitation($data);
     if (isset($result['message'])) {
         $this->errors = $result['message'];
     } else {
         $infos = [];
         foreach ($this->balin['info'] as $key => $value) {
             $infos[$value['type']] = $value['value'];
         }
         $infos['action'] = route(env('ROUTE_BALIN_INVITATION_GET'), $whoami['data']['code_referral']);
         $mail = new APISendMail();
         $result = $mail->invitation($whoami['data'], $emails, $infos);
         if (isset($result['message'])) {
             $this->errors = $result['message'];
         }
         $this->page_attributes->success = 'Anda telah mengirimkan  ' . count($emails) . ' undangan kepada teman Anda';
     }
     return $this->generateRedirectRoute('my.balin.redeem.index');
 }
 /**
  * function to resend activation mail
  * 
  * @return redirect
  */
 public function activation()
 {
     //1. Get Me
     $APIUser = new APIUser();
     $whoami = $APIUser->getMeDetail(['user_id' => Session::get('whoami')['id']]);
     //2. Whoami
     if (!$whoami['data']['is_active'] && $whoami['data']['activation_link'] != '') {
         $infos = [];
         foreach ($this->balin['info'] as $key => $value) {
             $infos[$value['type']] = $value['value'];
         }
         $infos['action'] = route(env('ROUTE_BALIN_CLAIM_VOUCHER'), $whoami['data']['activation_link']);
         $mail = new APISendMail();
         $result = $mail->welcomemail($whoami['data'], $infos);
         if (isset($result['message'])) {
             $this->errors = $result['message'];
         }
     } else {
         $this->errors = 'Akun anda sudah di aktivasi.';
     }
     $this->page_attributes->success = "Email aktivasi sudah dikirim.";
     return $this->generateRedirectRoute('my.balin.profile');
 }
 /**
  * function to post login information
  *
  * @param email and password
  */
 public function postLogin()
 {
     //check user data login
     $api_url = '/oauth/access_token';
     $api_data = ['email' => Input::get('email'), 'password' => Input::get('password'), 'grant_type' => 'password', 'client_id' => env('CLIENT_ID'), 'client_secret' => env('CLIENT_SECRET')];
     $api = new API();
     $result = json_decode($api->post($api_url, $api_data), true);
     if ($result['status'] == "success") {
         $API_me = new APIUser();
         Session::put('API_token_private', $result['data']['token']['token']);
         $whoami = $API_me->getMeDetail(['user_id' => $result['data']['me']['id'], 'token' => Session::get('API_token_private')]);
         Session::put('whoami', $whoami['data']);
         Session::put('API_token', Session::get('API_token_private'));
         //check user before login carts
         if (!Session::has('carts')) {
             $me_order_in_cart = $API_me->getMeOrderInCart(['user_id' => Session::get('whoami')['id']]);
             if ($me_order_in_cart['status'] == 'success') {
                 $carts = $me_order_in_cart;
                 $temp_carts = [];
                 foreach ($carts['data']['transactiondetails'] as $k => $v) {
                     $temp_carts[$v['varian']['product_id']] = ['product_id' => $v['varian']['product_id'], 'slug' => $v['varian']['product']['slug'], 'name' => $v['varian']['product']['name'], 'discount' => $v['discount'], 'current_stock' => $v['varian']['current_stock'], 'thumbnail' => $v['varian']['product']['thumbnail'], 'price' => $v['price']];
                     $temp_varian = ['varian_id' => $v['varian_id'], 'sku' => $v['varian']['sku'], 'quantity' => $v['quantity'], 'size' => $v['varian']['size'], 'current_stock' => $v['varian']['current_stock'], 'message' => null];
                     $temp_carts[$v['varian']['product_id']]['varians'][$v['varian']['id']] = $temp_varian;
                 }
                 Session::put('carts', $temp_carts);
             }
         } else {
             if (count(Session::get('carts')) != 0) {
                 /* SET API TOKEN USE TOKEN PRIVATE */
                 $temp_carts = ['id' => '', 'user_id' => Session::get('whoami')['id'], 'transact_at' => date('Y-m-d H:i:s'), 'transactiondetails' => [], 'transactionlogs' => ['id' => '', 'status' => 'cart', 'change_at' => '', 'notes' => ''], 'payment' => [], 'shipment' => []];
                 $session_cart = Session::get('carts');
                 $temp_varian = [];
                 foreach ($session_cart as $k => $v) {
                     foreach ($v['varians'] as $k2 => $v2) {
                         $temp_varian[] = ['id' => '', 'transaction_id' => '', 'quantity' => $v2['quantity'], 'price' => $v['price'], 'discount' => $v['discount'], 'varian_id' => $v2['varian_id'], 'varians' => ['id' => $v2['varian_id'], 'product_id' => $k, 'sku' => $v2['sku'], 'size' => $v2['size']]];
                     }
                 }
                 $temp_carts['transactiondetails'] = $temp_varian;
                 $temp_carts['status'] = 'cart';
                 $API_order = new APIUser();
                 $result = $API_order->postMeOrder($temp_carts);
                 // result
                 if (isset($result['message'])) {
                     $error = $result['message'];
                 }
             }
         }
         if (Session::has('redirect_url')) {
             $redirect = Session::get('redirect_url');
             Session::forget('redirect_url');
             return Redirect::to($redirect);
         }
         return Redirect::route('my.balin.redeem.index');
     }
     return Redirect::route('balin.get.login', ['type' => 'login'])->withErrors($result['message'])->with('msg-type', 'danger');
 }