/**
  * @param $customerRepository
  * @return int
  */
 public static function getDefaultCountry(CustomerRepository $customerRepository = null)
 {
     // if country ID is in the GET then obviously, that
     if (isset($_GET['countryId']) && is_numeric($_GET['countryId'])) {
         return (int) $_GET['countryId'];
     }
     if ($customerRepository) {
         try {
             $customer = $customerRepository->find();
             // get top address
             /* @var $customer Customer */
             $addresses = $customer->getAddresses();
             if (count($addresses) > 0) {
                 return $addresses[0]->getCountryId();
             }
         } catch (CustomerNotFoundException $e) {
             // get company default country
             $clientInfo = Config::get('clientInfo');
             if ($clientInfo->country_id) {
                 return $clientInfo->country_id;
             }
         }
     }
     // use UK as super-default if company default not set
     return 79;
 }
 /**
  * @param OrderRepository $orderRepository
  * @param CustomerRepository $customerRepository
  * @param $orderId
  * @return \Illuminate\Contracts\View\View
  */
 public function view(OrderRepository $orderRepository, CustomerRepository $customerRepository, $orderId)
 {
     $data['customer'] = $customerRepository->find();
     $data['order'] = $orderRepository->find($orderId);
     $data['defaultAddress'] = $data['order']->getBillingAddress();
     $data['pageTitle'] = trans('vendirun::product.viewOrder');
     return View::make('vendirun::customer.orders.view', $data);
 }
 /**
  * @param CustomerRepository $customerRepository
  * @return \Illuminate\Http\RedirectResponse
  */
 public function save(CustomerRepository $customerRepository)
 {
     $customer = $customerRepository->find();
     $data = Request::all();
     $data['id'] = $data['addressId'];
     $customer->addAddress(new Address($data));
     $customerRepository->save($customer);
     return Redirect::route('vendirun.customer.account');
 }
 /**
  * @param CustomerRepository $customerRepository
  * @return \Illuminate\Http\RedirectResponse
  */
 public function recalculateShipping(CustomerRepository $customerRepository)
 {
     $customer = $customerRepository->find();
     $countryId = NULL;
     $countryId = CustomerHelper::getDefaultCountry($customerRepository);
     if (Request::has('countryId')) {
         $countryId = Request::get('countryId');
     }
     if (Request::has('shippingaddressId')) {
         $customerCountryId = $customer->getAddressFromAddressId(Request::get('shippingaddressId'));
         if ($customerCountryId) {
             $countryId = $customerCountryId->getCountryId();
         }
     }
     return Redirect::route('vendirun.checkout', ['countryId' => $countryId, 'shippingTypeId' => Request::get('shippingTypeId', NULL)])->withInput();
 }
 /**
  * @param ProductRepository $productRepository
  * @param CustomerRepository $customerRepository
  * @param $productId
  * @return \Illuminate\Contracts\View\View
  */
 public function index(ProductRepository $productRepository, CustomerRepository $customerRepository, $productId)
 {
     $data = [];
     try {
         $data['product'] = $productRepository->find($productId);
     } catch (FailResponseException $e) {
         if (App::environment() == 'production') {
             abort(404);
         }
     }
     try {
         $data['customer'] = $customerRepository->find();
     } catch (CustomerNotFoundException $e) {
         // this exception means we're not logged in. No problem
     }
     $data['recommend'] = true;
     $data['pageTitle'] = trans('vendirun::product.sendProductToFriend');
     return View::make('vendirun::product.recommend', $data);
 }
 /**
  * @param CustomerRepository $customerRepository
  * @return \Illuminate\Contracts\View\View
  */
 public function index(CustomerRepository $customerRepository)
 {
     $data['pageTitle'] = trans('vendirun::customer.account');
     $data['customer'] = $customerRepository->find();
     return View::make('vendirun::customer.account.index', $data);
 }
 /**
  * Recommend a Friend
  * @param IlRequest $request
  * @param CustomerRepository $customerRepository
  * @return mixed
  */
 public function recommendAFriend(IlRequest $request, CustomerRepository $customerRepository)
 {
     $this->validate($request, ['fullName' => 'required', 'emailAddress' => 'required|email', 'fullNameFriend' => 'required', 'emailAddressFriend' => 'required|email']);
     $params['property_id'] = Request::get('property_id', null);
     $params['product_id'] = Request::get('product_id', null);
     // generate correct link
     $link = route(LocaleHelper::localePrefix() . 'vendirun.home');
     if (Request::has('propertyId')) {
         $link = route(LocaleHelper::localePrefix() . 'vendirun.propertyView', ['id' => Request::get('propertyId'), 'name' => htmlentities(Request::get('property'))]);
     }
     if (Request::has('productId')) {
         $link = route(LocaleHelper::localePrefix() . 'vendirun.productView', ['id' => Request::get('productId'), 'name' => htmlentities(Request::get('product'))]);
     }
     $customerFactory = new CustomerFactory();
     try {
         $originator = $customerRepository->find();
     } catch (CustomerNotFoundException $e) {
         $originator = $customerFactory->make(null, Request::get('fullName'), Request::get('emailAddress'));
         $customerRepository->save($originator);
     }
     $receiver = $customerFactory->make(null, Request::get('fullNameFriend'), Request::get('emailAddressFriend'));
     $customerRepository->save($receiver);
     try {
         $customerRepository->recommendFriend($originator, $receiver, $link);
         Session::flash('vendirun-alert-success', 'Thank you for recommending this page to your friend! We\'ve sent them an email on your behalf.');
     } catch (FailResponseException $e) {
         Session::flash('vendirun-alert-error', 'Unable to send email to your friend. Please try again.');
     }
     return Redirect::back();
 }