Esempio n. 1
0
 /**
  * Process item
  *
  * @param 	void
  * @return 	bool
  */
 public function handle()
 {
     // Get product type info
     $ptId = $this->item['info']->ptId;
     include_once JPATH_BASE . DS . 'components' . DS . 'com_storefront' . DS . 'models' . DS . 'Warehouse.php';
     $warehouse = new StorefrontModelWarehouse();
     $ptIdTypeInfo = $warehouse->getProductTypeInfo($ptId);
     // Run both product model handler and type handler if needed.
     // Model handlers must go first for type handlers to potentially use their updates
     $handlersPath = PATH_CORE . DS . 'components' . DS . 'com_cart' . DS . 'lib' . DS . 'handlers';
     // MODEL HANDLER
     $modelHandlerClass = str_replace(' ', '_', ucwords(strtolower($ptIdTypeInfo['ptModel']))) . '_Model_Handler';
     if (file_exists($handlersPath . DS . 'model' . DS . $modelHandlerClass . '.php')) {
         // Include the parent class
         include_once $handlersPath . DS . 'ModelHandler.php';
         // Include the handler file
         include_once $handlersPath . DS . 'model' . DS . $modelHandlerClass . '.php';
         $modelHandler = new $modelHandlerClass($this->item, $this->crtId);
         $modelHandler->handle();
     }
     // TYPE HANDLER
     $typeHandlerClass = str_replace(' ', '_', ucwords(strtolower($ptIdTypeInfo['ptName']))) . '_Type_Handler';
     //print_r($typeHandlerClass); die;
     if (file_exists($handlersPath . DS . 'type' . DS . $typeHandlerClass . '.php')) {
         // Include the parent class
         include_once $handlersPath . DS . 'TypeHandler.php';
         // Include the handler file
         include_once $handlersPath . DS . 'type' . DS . $typeHandlerClass . '.php';
         $typeHandler = new $typeHandlerClass($this->item, $this->crtId);
         $typeHandler->handle();
     }
     // CUSTOM HANDLERS (if any)
     if (!empty($this->item['meta']['customHandler'])) {
         $customHandler = $this->item['meta']['customHandler'];
         $customHandlerClass = str_replace(' ', '_', ucwords(strtolower($customHandler))) . '_Custom_Handler';
         if (file_exists($handlersPath . DS . 'custom' . DS . $customHandlerClass . '.php')) {
             // Include the parent class
             include_once $handlersPath . DS . 'CustomHandler.php';
             // Include the handler file
             include_once $handlersPath . DS . 'custom' . DS . $customHandlerClass . '.php';
             $customHandler = new $customHandlerClass($this->item, $this->crtId);
             $customHandler->handle();
         }
     }
 }
Esempio n. 2
0
 /**
  * Constructor
  * @param 	Object  Product info
  * @param   int     Cart ID
  * @param   int     User ID
  * @return 	Void
  */
 public static function getAuditor($pInfo, $crtId)
 {
     $pId = $pInfo->pId;
     $warehouse = new StorefrontModelWarehouse();
     // Get product type
     $pType = $warehouse->getProductTypeInfo($pInfo->ptId);
     $type = $pType['ptName'];
     $model = $pType['ptModel'];
     // Find if there are auditors for this product's type and model
     $auditorsPath = PATH_CORE . DS . 'components' . DS . 'com_cart' . DS . 'lib' . DS . 'auditors';
     $auditorClass = str_replace(' ', '_', ucwords(strtolower($model))) . '_Auditor';
     if (file_exists($auditorsPath . DS . $auditorClass . '.php')) {
         // Include the auditor file
         include_once $auditorsPath . DS . $auditorClass . '.php';
         return new $auditorClass($type, $pId, $crtId);
     } else {
         return new BaseAuditor($type);
     }
 }
 public function handle()
 {
     $itemInfo = $this->item['info'];
     // Get user
     require_once JPATH_BASE . DS . 'components' . DS . 'com_cart' . DS . 'models' . DS . 'Cart.php';
     $uId = CartModelCart::getCartUser($this->crtId);
     // Get product type
     require_once PATH_CORE . DS . 'components' . DS . 'com_storefront' . DS . 'models' . DS . 'Warehouse.php';
     $warehouse = new StorefrontModelWarehouse();
     $pType = $warehouse->getProductTypeInfo($itemInfo->ptId);
     $type = $pType['ptName'];
     require_once JPATH_BASE . DS . 'components' . DS . 'com_storefront' . DS . 'models' . DS . 'Memberships.php';
     $subscription = StorefrontModelMemberships::getSubscriptionObject($type, $itemInfo->pId, $uId);
     // Get the expiration for the current subscription (if any)
     $currentExpiration = $subscription->getExpiration();
     // Calculate new expiration
     $newExpires = StorefrontModelMemberships::calculateNewExpiration($currentExpiration, $this->item);
     // Update/Create membership expiration date with new value
     $subscription->setExpiration($newExpires);
 }
Esempio n. 4
0
 /**
  * Actions to perform after deleting an offering
  *
  * @param      object  $model \Components\Courses\Models\Section
  * @param      boolean $isNew Is this a newly created entry?
  * @return     void
  */
 public function onAfterDeleteCoupon($model)
 {
     if (!$model->exists()) {
         return;
     }
     $warehouse = new StorefrontModelWarehouse();
     try {
         $warehouse->deleteCoupon($model->get('code'));
     } catch (Exception $e) {
         echo 'ERROR: ' . $e->getMessage();
     }
     return;
 }
Esempio n. 5
0
 /**
  * Display default page
  *
  * @return     void
  */
 public function testgroundTask()
 {
     if (0) {
         // CREATE COUPON
         include_once PATH_CORE . DS . 'components' . DS . 'com_storefront' . DS . 'models' . DS . 'StorefrontModelCoupon.php';
         try {
             // Constructor take the coupon code
             $coupon = new StorefrontModelCoupon('hui');
             // Coupon description (shows up in the cart)
             $coupon->setDescription('Test coupon, 10% off product with ID 3');
             // Expiration date
             $coupon->setExpiration('Feb 22, 2022');
             // Number of times coupon can be used (unlimited by default)
             $coupon->setUseLimit(1);
             // Product the coupon will be applied to:
             // first parameter: product ID
             // second parameter [optional, unlimited by default]: max quantity of products coupon will be applied to (if buying multiple)
             $coupon->addObject(3, 1);
             // Action, only 'discount' for now
             // second parameter either percentage ('10%') or absolute dollar value ('20')
             $coupon->setAction('discount', '10%');
             // Add coupon
             $coupon->add();
         } catch (Exception $e) {
             echo 'ERROR: ' . $e->getMessage();
         }
         return;
     }
     if (0) {
         // DELETE COUPON
         include_once PATH_CORE . DS . 'components' . DS . 'com_storefront' . DS . 'models' . DS . 'Warehouse.php';
         $warehouse = new StorefrontModelWarehouse();
         try {
             $warehouse->deleteCoupon('couponcode3');
         } catch (Exception $e) {
             echo 'ERROR: ' . $e->getMessage();
         }
         return;
     }
     if (0) {
         // CREATE NEW COURSE
         include_once PATH_CORE . DS . 'components' . DS . 'com_storefront' . DS . 'models' . DS . 'Course.php';
         $course = new StorefrontModelCourse();
         $course->setName('Name of the course');
         $course->setDescription('Short description');
         $course->setPrice(12.0);
         $course->addToCollection('courses');
         // Membership model: membership duration period (must me in MySQL date format: 1 DAY, 2 MONTH, 3 YEAR...)
         $course->setTimeToLive('1 YEAR');
         // Course alias id
         $course->setCourseId('nanoscaletransistors');
         try {
             // Returns object with values, pId is the new product ID to link to
             $info = $course->add();
             //print_r($info);
         } catch (Exception $e) {
             echo 'ERROR: ' . $e->getMessage();
         }
         return;
     }
     if (0) {
         // GET EXISTING COURSE, modify it and save
         include_once PATH_CORE . DS . 'components' . DS . 'com_storefront' . DS . 'models' . DS . 'Warehouse.php';
         $warehouse = new StorefrontModelWarehouse();
         try {
             // Get course by pID returned with $course->add() above
             $course = $warehouse->getCourse(1);
             $course->setName('Renamed');
             $course->setDescription('New description');
             $course->setPrice(55.22);
             $course->setTimeToLive('10 YEAR');
             $course->update();
         } catch (Exception $e) {
             echo 'ERROR: ' . $e->getMessage();
         }
         return;
     }
     if (0) {
         // UPDATE COURSE by recreating it
         include_once PATH_CORE . DS . 'components' . DS . 'com_storefront' . DS . 'models' . DS . 'StorefrontModelCourse.php';
         $course = new StorefrontModelCourse();
         $course->setName('Operations Management 104');
         $course->setDescription('Operations Management 104 is some kind of test course for now...');
         $course->setPrice(13.05);
         $course->setCourseId(5);
         // Existing course ID (pID returned with $course->add() when the course was created). Must be set to be able to update.
         $course->setId(1023);
         try {
             $info = $course->update();
             //print_r($info);
         } catch (Exception $e) {
             echo 'ERROR: ' . $e->getMessage();
         }
         return;
     }
     if (0) {
         // DELETE COURSE
         include_once PATH_CORE . DS . 'components' . DS . 'com_storefront' . DS . 'models' . DS . 'Warehouse.php';
         $warehouse = new StorefrontModelWarehouse();
         // Delete by existing course ID (pID returned with $course->add() when the course was created)
         $warehouse->deleteProduct(1023);
         return;
     }
 }
Esempio n. 6
0
 /**
  * Releases locked transaction items back to inventory and marks the transaction status as 'released'
  *
  * @param int Transaction ID
  * @return void
  */
 public static function releaseTransaction($tId)
 {
     $db = App::get('db');
     // Check if the transaction can be released (status is pending)
     // Get info
     $sql = "SELECT t.`tStatus` FROM `#__cart_transactions` t WHERE t.`tId` = {$tId}";
     $db->setQuery($sql);
     $db->query();
     if (!$db->getNumRows()) {
         return false;
     }
     // Get transaction items
     $tItems = self::getTransactionItems($tId);
     /* Go through each item and return the quantity back to inventory if needed */
     require_once JPATH_BASE . DS . 'components' . DS . 'com_storefront' . DS . 'models' . DS . 'Warehouse.php';
     $warehouse = new StorefrontModelWarehouse();
     if (!empty($tItems)) {
         foreach ($tItems as $sId => $itemInfo) {
             $qty = $itemInfo['transactionInfo']->qty;
             $warehouse->updateInventory($sId, $qty, 'add');
         }
     }
     // update status
     self::updateTransactionStatus('released', $tId);
 }
Esempio n. 7
0
 * @copyright Copyright 2005-2015 HUBzero Foundation, LLC.
 * @license   http://opensource.org/licenses/MIT MIT
 */
// No direct access
defined('_HZEXEC_') or die;
$offerings = $this->course->offerings(array('available' => true, 'sort' => 'publish_up'));
if ($offerings) {
    $offering = $offerings->fetch('first');
} else {
    $offering = new \Components\Courses\Models\Offering(0, $this->course->get('id'));
}
if ($offering->exists()) {
    $params = new \Hubzero\Config\Registry($offering->get('params'));
    $product = null;
    if ($params->get('store_product_id', 0)) {
        $warehouse = new StorefrontModelWarehouse();
        // Get course by pID returned with $course->add() above
        try {
            $product = $warehouse->getCourse($params->get('store_product_id', 0));
        } catch (Exception $e) {
            echo 'ERROR: ' . $e->getMessage();
        }
    }
    $url = $offering->link() . '&task=enroll';
    if ($product && $product->data->id) {
        $url = 'index.php?option=com_cart';
        //index.php?option=com_storefront/product/' . $product->pId;
    }
    ?>
			<table>
				<tbody>
Esempio n. 8
0
 /**
  * Attempts to rebuild a transaction if there is enough inventory and prices have not changed
  *
  * @param void
  * @return bool Success or failure
  */
 private function rebuildTransaction()
 {
     // Go through each item in the transaction and see if it is available in quantity needed and if price has not changed
     $tItems = $this->getTransactionItems($this->cart->tId);
     foreach ($tItems as $item) {
         // Check price
         if ($item['info']->sPrice != $item['transactionInfo']->tiPrice) {
             // price changed
             return false;
         }
         // Check inventory
         if ($item['info']->sInventory < $item['transactionInfo']->qty) {
             // Not enough inventory
             return false;
         }
     }
     // lock transaction items
     include_once PATH_CORE . DS . 'components' . DS . 'com_storefront' . DS . 'models' . DS . 'Warehouse.php';
     $warehouse = new StorefrontModelWarehouse();
     foreach ($tItems as $sId => $item) {
         $warehouse->updateInventory($sId, $item['transactionInfo']->qty, 'subtract');
     }
     parent::updateTransactionStatus('pending', $this->cart->tId);
     return true;
 }
Esempio n. 9
0
 /**
  * Verify course
  *
  * @param  string		action (optional)
  * @return bool			true on sucess
  */
 public function verify($action = NULL)
 {
     // If action is 'add', make sure that course id/alias is unique
     if ($action == 'add') {
         $warehouse = new StorefrontModelWarehouse();
         $courseIdExists = $warehouse->getCourseByAlias($this->getCourseId());
         if ($courseIdExists) {
             throw new Exception(Lang::txt('Course with this alias already exists.'));
         }
     }
     parent::verify($action);
 }
Esempio n. 10
0
 /**
  * Add coupon to the warehouse
  *
  * @param  void
  * @return object	info
  */
 public function add()
 {
     $this->verify();
     include_once __DIR__ . DS . 'Warehouse.php';
     $warehouse = new StorefrontModelWarehouse();
     return $warehouse->addCoupon($this);
 }
Esempio n. 11
0
 /**
  * Update product info
  *
  * @param  void
  * @return object	info
  */
 public function update()
 {
     // For single product update SKU must save the original SKU ID (since SKU was generated automatically)
     // Find the SKU ID for this product and save
     include_once PATH_CORE . DS . 'components' . DS . 'com_storefront' . DS . 'models' . DS . 'Warehouse.php';
     $warehouse = new StorefrontModelWarehouse();
     $sku = $warehouse->getProductSkus($this->data->id);
     // Must be just one SKU
     if (sizeof($sku) != 1) {
         throw new Exception(Lang::txt('Only one SKU is allowed'));
     }
     $skuId = $sku[0];
     // save product sku with the current ID to resave the changes with this ID
     $sku = $this->getSku()->setId($skuId);
     return parent::update();
 }
Esempio n. 12
0
 public function addTask()
 {
     $response = new stdClass();
     $response->status = 'ok';
     include_once JPATH_COMPONENT . DS . 'models' . DS . 'cart.php';
     $cart = new CartModelCurrentCart();
     // update cart
     $updateCartRequest = Request::getVar('updateCart', false, 'post');
     $pIds = Request::getVar('pId', false, 'post');
     //print_r($pIds); die;
     // If pIds are posted, convert them to SKUs
     if (!empty($pIds)) {
         $skus = array();
         include_once JPATH_BASE . DS . 'components' . DS . 'com_storefront' . DS . 'models' . DS . 'Warehouse.php';
         $warehouse = new StorefrontModelWarehouse();
         foreach ($pIds as $pId => $qty) {
             $product_skus = $warehouse->getProductSkus($pId);
             // must be only one sku to work
             if (sizeof($product_skus) != 1) {
                 // each pId must map to one SKU, otherwise ignored
                 continue;
             }
             $skus[$product_skus[0]] = $qty;
         }
     } else {
         $skus = Request::getVar('skus', false, 'post');
     }
     //print_r($skus); die;
     // Initialize errors array
     $errors = array();
     if ($updateCartRequest && $skus) {
         // Turn off syncing to prevent redundant session update queries
         $cart->setSync(false);
         foreach ($skus as $sId => $qty) {
             try {
                 $cart->update($sId, $qty);
             } catch (Exception $e) {
                 $errors[] = $e->getMessage();
             }
         }
     }
     // add coupon if needed
     $addCouponRequest = Request::getVar('addCouponCode', false, 'post');
     $couponCode = Request::getVar('couponCode', false, 'post');
     if ($addCouponRequest && $couponCode) {
         // Sync cart before pontial coupons applying
         $cart->getCartInfo(true);
         // Initialize errors array
         $errors = array();
         // Add coupon
         try {
             $cart->addCoupon($couponCode);
         } catch (Exception $e) {
             $errors[] = $e->getMessage();
         }
     }
     if (!empty($errors)) {
         $response->status = 'error';
         $response->errors = $errors;
     }
     echo htmlspecialchars(json_encode($response), ENT_NOQUOTES);
     die;
 }
Esempio n. 13
0
 /**
  * Display default page
  *
  * @return     void
  */
 public function homeTask()
 {
     $cart = new CartModelCurrentCart();
     // Initialize errors array
     $errors = array();
     // Update cart if needed
     $updateCartRequest = Request::getVar('updateCart', false, 'post');
     // If pIds are posted, convert them to SKUs
     $pIds = Request::getVar('pId', false, 'post');
     //print_r($pIds); die;
     $skus = Request::getVar('skus', false, 'post');
     if ($updateCartRequest && ($pIds || $skus)) {
         if (!empty($pIds)) {
             $skus = array();
             include_once PATH_CORE . DS . 'components' . DS . 'com_storefront' . DS . 'models' . DS . 'Warehouse.php';
             $warehouse = new StorefrontModelWarehouse();
             foreach ($pIds as $pId => $qty) {
                 $product_skus = $warehouse->getProductSkus($pId);
                 // each pId must map to one SKU, otherwise ignored, since there is no way which SKU is being added
                 // Must be only one sku...
                 if (sizeof($product_skus) != 1) {
                     continue;
                 }
                 $skus[$product_skus[0]] = $qty;
             }
         } else {
             if (!is_array($skus)) {
                 $skus = array($skus => 1);
             }
         }
         //print_r($skus); die;
         // Turn off syncing to prevent redundant session update queries
         $cart->setSync(false);
         foreach ($skus as $sId => $qty) {
             try {
                 $cart->update($sId, $qty);
             } catch (Exception $e) {
                 $cart->setMessage($e->getMessage(), 'error');
             }
         }
         // set flag to redirect
         $redirect = true;
         if ($cart->hasMessages()) {
             $redirect = false;
         }
     } else {
         $allPost = Request::request();
         foreach ($allPost as $var => $val) {
             if ($val == 'delete') {
                 $toDelete = explode('_', $var);
                 //print_r($toDelete);	die;
                 if ($toDelete[0] == 'delete') {
                     $sId = $toDelete[1];
                     // Delete the requested item by setting its QTY to zero
                     $redirect = true;
                     try {
                         $cart->update($sId, 0);
                     } catch (Exception $e) {
                         $cart->setMessage($e->getMessage(), 'error');
                         $redirect = false;
                     }
                 }
             }
         }
     }
     // Add coupon if needed
     $addCouponRequest = Request::getVar('addCouponCode', false, 'post');
     $couponCode = Request::getVar('couponCode', false, 'post');
     if ($addCouponRequest && $couponCode) {
         // Sync cart before pontial coupons applying
         $cart->getCartInfo(true);
         // Add coupon
         try {
             $cart->addCoupon($couponCode);
         } catch (Exception $e) {
             $cart->setMessage($e->getMessage(), 'error');
         }
         // set flag to redirect
         $redirect = true;
         if ($cart->hasMessages()) {
             $redirect = false;
         }
     }
     // Check for express add to cart
     if (!empty($redirect) && $redirect) {
         // If this is an express checkout (go to the confirm page right away) there shouldn't be any items in the cart
         // Since redirect is set, there are no errors
         $expressCheckout = Request::getVar('expressCheckout', false, 'post');
         // make sure the cart is empty
         if ($expressCheckout && !empty($skus) && $cart->isEmpty()) {
             // Get the latest synced cart info, it will also enable cart syncing that was turned off before
             $cart->getCartInfo(true);
             // Redirect directly to checkout, skip the cart page
             $redirect_url = Route::url('index.php?option=' . 'com_cart') . DS . 'checkout';
             App::redirect($redirect_url);
         }
         // prevent resubmitting form by refresh
         // redirect to cart
         $redirect_url = Route::url('index.php?option=' . 'com_cart');
         App::redirect($redirect_url);
     }
     // Get the latest synced cart info, it will also enable cart syncing that was turned off before
     $cartInfo = $cart->getCartInfo(true);
     $this->view->cartInfo = $cartInfo;
     // Handle coupons
     $couponPerks = $cart->getCouponPerks();
     //print_r($couponPerks); die;
     $this->view->couponPerks = $couponPerks;
     // Handle memberships
     $membershipInfo = $cart->getMembershipInfo();
     //print_r($membershipInfo); die;
     $this->view->membershipInfo = $membershipInfo;
     // At this point the cart is lifted and may have some issues/errors (say, after merging), get them
     if ($cart->hasMessages()) {
         $cartMessages = $cart->getMessages();
         $this->view->notifications = $cartMessages;
     }
     $this->view->display();
 }
Esempio n. 14
0
 /**
  * Update product info
  *
  * @param  void
  * @return object	info
  */
 public function update()
 {
     include_once __DIR__ . DS . 'Warehouse.php';
     $warehouse = new StorefrontModelWarehouse();
     return $warehouse->updateProduct($this);
 }