/**
  * Returns an singleton instance of this class
  *
  * @param object $config
  * @param object $args
  * @return
  */
 public static function getInstance($config, $args)
 {
     if (self::$instance == null) {
         self::$instance = new BundleItemsManager($config, $args);
     }
     return self::$instance;
 }
 /**
  * Initializes DB mappers
  *
  * @param object $config
  * @param object $args
  * @return
  */
 function __construct($config, $args)
 {
     $this->config = $config;
     $this->args = $args;
     $this->mapper = CustomerCartMapper::getInstance();
     $this->bundleItemsManager = BundleItemsManager::getInstance($this->config, $this->args);
     $this->itemManager = ItemManager::getInstance($this->config, $this->args);
 }
 private function initVars()
 {
     $customer = $this->getCustomer();
     if ($customer) {
         $this->customerEmail = strtolower($customer->getEmail());
         $this->customerCartManager = CustomerCartManager::getInstance($this->config, $this->args);
         $this->bundleItemsManager = BundleItemsManager::getInstance($this->config, $this->args);
         $this->itemManager = ItemManager::getInstance($this->config, $this->args);
     }
 }
 public function service()
 {
     $customer = $this->getCustomer();
     $customerEmail = strtolower($customer->getEmail());
     $customerCartManager = CustomerCartManager::getInstance($this->config, $this->args);
     $bundleItemsManager = BundleItemsManager::getInstance($this->config, $this->args);
     $itemManager = ItemManager::getInstance($this->config, $this->args);
     $userLevel = $this->sessionManager->getUser()->getLevel();
     $user_id = $this->getUserId();
     $add_count = 1;
     if (isset($_REQUEST['add_count'])) {
         $add_count = $this->secure($_REQUEST['add_count']);
     }
     if (isset($_REQUEST['bundle_items_ids'])) {
         $bundle_items_ids = $this->secure($_REQUEST['bundle_items_ids']);
     } else {
         $jsonArr = array('status' => "err", "errText" => "System error: Try to add empty bundle to cart!");
         echo json_encode($jsonArr);
         return false;
     }
     $userManager = UserManager::getInstance($this->config, $this->args);
     $vipCustomer = $userManager->isVipAndVipEnabled($customer);
     if ($vipCustomer) {
         $discount = floatval($this->getCmsVar('vip_pc_configurator_discount'));
     } else {
         $discount = floatval($this->getCmsVar('pc_configurator_discount'));
     }
     $bundle_display_name_id = 287;
     //means "computer"
     $bundle_items_ids_array = explode(',', $bundle_items_ids);
     $itemsDto = $itemManager->getItemsForOrder($bundle_items_ids_array, $user_id, $userLevel);
     if (count($itemsDto) !== count($bundle_items_ids_array)) {
         $jsonArr = array('status' => "err", "errText" => "Some items are not available!");
         echo json_encode($jsonArr);
         return false;
     }
     $last_dealer_price = 0;
     foreach ($itemsDto as $key => $itemDto) {
         $last_dealer_price += $itemDto->getDealerPrice();
     }
     if (isset($_REQUEST['replace_cart_row_id']) && $_REQUEST['replace_cart_row_id'] > 0) {
         $replace_cart_row_id = $_REQUEST['replace_cart_row_id'];
         $dto = $customerCartManager->selectByPK($replace_cart_row_id);
         if ($dto) {
             $replaceing_bundle_id = $dto->getBundleId();
             $addedItemId = $customerCartManager->updateById($replace_cart_row_id, $customerEmail, 0, $replaceing_bundle_id, $last_dealer_price, $discount, $dto->getCount());
             $bundleItemsManager->deleteBundle($replaceing_bundle_id);
         } else {
             $jsonArr = array('status' => "err", "errText" => "System Error: Bundle is not available in your cart!");
             echo json_encode($jsonArr);
             return false;
         }
     }
     $bundle_id = $bundleItemsManager->createBundle($bundle_display_name_id, $bundle_items_ids, "", $replaceing_bundle_id);
     if (!isset($addedItemId)) {
         $addedItemId = $customerCartManager->addToCart($customerEmail, 0, $bundle_id, $last_dealer_price, $add_count);
     }
     ///////////start add build fee if it should be add///////////////////
     $bundleItems = $customerCartManager->getCustomerCart($customerEmail, $user_id, $userLevel, $addedItemId);
     //list($bpAMD, $bpUSD, $specialFeesTotalAMD) = $bundleItemsManager->calcBundlePriceForCustomerWithoutDiscount($bundleItems, $userLevel);
     $specialFeesManager = SpecialFeesManager::getInstance($this->config, $this->args);
     /* 	var_dump($bpAMD , intval($this->getCmsVar('pc_buid_fee_free_amd_over')));
         var_dump($bpAMD < intval($this->getCmsVar('pc_buid_fee_free_amd_over')));
         exit; */
     $pccm = PcConfiguratorManager::getInstance($this->config, $this->args);
     $bundleProfitWithoutDiscountUSD = 0;
     if ($userLevel != UserGroups::$ADMIN && $userLevel != UserGroups::$COMPANY) {
         $bundleProfitWithoutDiscountUSD = $bundleItemsManager->calcBundleProfitWithDiscount($bundleItems, $discount);
     }
     $pcBuildFee = $pccm->calcPcBuildFee($bundleProfitWithoutDiscountUSD);
     $pcBuildFeeId = $specialFeesManager->getPcBuildFee()->getId();
     $special_fees = array($pcBuildFeeId => $pcBuildFee);
     if ($pcBuildFee > 0) {
         $bundleItemsManager->addSpecialFeesToBundle($bundle_id, $bundle_display_name_id, $special_fees);
     }
     ///////////end add  build fee if it should be add///////////////////
     $totalCount = $customerCartManager->getCustomerCartTotalCount($customerEmail);
     $jsonArr = array('status' => "ok", "cart_items_count" => $totalCount);
     echo json_encode($jsonArr);
     return true;
 }
 public function load()
 {
     $customer = $this->getCustomer();
     $customerEmail = strtolower($customer->getEmail());
     $customerCartManager = CustomerCartManager::getInstance($this->config, $this->args);
     $checkoutManager = CheckoutManager::getInstance($this->config, $this->args);
     $bundleItemsManager = BundleItemsManager::getInstance($this->config, $this->args);
     $itemManager = ItemManager::getInstance($this->config, $this->args);
     $userLevel = $this->sessionManager->getUser()->getLevel();
     $user_id = $this->sessionManager->getUser()->getId();
     $cartItemsDtos = $customerCartManager->getCustomerCart($customerEmail, $user_id, $userLevel);
     $pv = $checkoutManager->getPriceVariety($cartItemsDtos, $userLevel);
     $discountAvailable = $checkoutManager->isDiscountAvailableForAtleastOneItem($cartItemsDtos);
     $groupedCartItems = $customerCartManager->groupBundleItemsInArray($cartItemsDtos);
     $cartChanges = $customerCartManager->getCustomerCartItemsChanges($groupedCartItems);
     $customerCartChangesMessages = $checkoutManager->getCustomerCartChangesMessages($cartChanges);
     if (!empty($customerCartChangesMessages)) {
         $this->addParam('customerMessages', $customerCartChangesMessages);
     }
     //all cart items, bundle items grouped in sub array
     $cartTotalDealsDiscountAMD = 0;
     if (!isset($_REQUEST['cho_payment_type']) || $_REQUEST['cho_payment_type'] != 'cash') {
         $_REQUEST['cho_promo_codes'] = '';
     }
     $cho_include_vat = isset($_REQUEST['cho_include_vat']) ? $this->secure($_REQUEST['cho_include_vat']) : null;
     if (isset($_REQUEST['cho_promo_codes']) && !empty($_REQUEST['cho_promo_codes'])) {
         $cho_promo_codes = $this->secure($_REQUEST['cho_promo_codes']);
         $cho_promo_codes_arrray = explode(',', $cho_promo_codes);
         $validPromoDiscount = $checkoutManager->applyAllItemsPromoOnCartItems($groupedCartItems, $cho_promo_codes_arrray, $cho_include_vat);
         $existingDealsPromoCodesArray = $checkoutManager->applyDealsDiscountsOnCartItems($groupedCartItems, $cho_promo_codes_arrray, $cho_include_vat);
         $existingDealsPromoCodesArray[] = $validPromoDiscount;
         $_REQUEST['cho_promo_codes'] = implode(',', $existingDealsPromoCodesArray);
         $cartTotalDealsDiscountAMD = $checkoutManager->getCartTotalDealsDiscountAMD($groupedCartItems);
         $this->addParam("cartTotalDealsDiscountAMD", $cartTotalDealsDiscountAMD);
     }
     list($grandTotalAMD, $grandTotalUSD) = $customerCartManager->calcCartTotal($groupedCartItems, true, $userLevel, $cho_include_vat);
     $allItemsAreAvailable = $customerCartManager->areAllItemsAvailableInCustomerCart($groupedCartItems);
     //discount available for at leat one item in the cart
     $this->addParam('discountAvailable', $discountAvailable);
     //priceVariety the price variety in customer cart. Can be 'amd', 'usd' or 'both';
     $this->addParam('priceVariety', $pv);
     $this->addParam('cartItems', $groupedCartItems);
     $this->addParam('itemManager', $itemManager);
     $this->addParam('checkoutManager', $checkoutManager);
     $this->addParam('allItemsAreAvailable', $allItemsAreAvailable);
     $this->addParam('emptyCart', empty($cartItemsDtos));
     $this->addParam('bundleItemsManager', $bundleItemsManager);
     //cart grand totla included discounts, this is the final value that customer should pay for his cart
     $this->addParam('grandTotalAMD', $grandTotalAMD);
     $this->addParam('grandTotalUSD', $grandTotalUSD);
     if (strtolower($_REQUEST['cho_shipping_region']) != 'yerevan' || $_REQUEST['billing_is_different_checkbox'] && strtolower($_REQUEST['cho_billing_region']) != 'yerevan') {
         $this->addParam('shipping_billing_region_ok', false);
     } else {
         $this->addParam('shipping_billing_region_ok', true);
     }
     $shippingCost = 0;
     if (isset($_REQUEST['cho_do_shipping']) && $_REQUEST['cho_do_shipping'] == 1) {
         $specialFeesManager = SpecialFeesManager::getInstance($this->config, $this->args);
         if ($grandTotalAMD < intval($this->getCmsVar('shipping_in_yerevan_free_amd_over'))) {
             $region = $this->secure($_REQUEST['cho_shipping_region']);
             $shippingCostDto = $specialFeesManager->getShippingCost($region);
             $shippingCost = intval(isset($shippingCostDto) ? intval($shippingCostDto->getPrice()) : -1);
         } else {
             $shippingCost = 0;
         }
         $this->addParam('shipping_cost', $shippingCost);
     }
     $grandTotalAMDWithShipping = intval($grandTotalAMD + ($shippingCost > 0 ? $shippingCost : 0));
     if ($userLevel === UserGroups::$USER) {
         $userPoints = $customer->getPoints();
         $usablePoints = 0;
         if ($userPoints > 0 && $grandTotalAMDWithShipping > 0) {
             $this->addParam('user_points_applicable', 'true');
             if ($userPoints > $grandTotalAMDWithShipping) {
                 $usablePoints = $grandTotalAMDWithShipping;
             } else {
                 $usablePoints = $userPoints;
             }
             $this->addParam('usablePoints', $usablePoints);
         }
     }
     if (isset($_REQUEST['cho_payment_type']) && $_REQUEST['cho_payment_type'] == 'credit') {
         $_REQUEST['cho_apply_user_points'] = 0;
     }
     if (isset($_REQUEST['cho_apply_user_points']) && $_REQUEST['cho_apply_user_points'] == 1) {
         $grandTotalAMDWithShipping -= $usablePoints;
     }
     $this->addParam('grandTotalAMDWithShipping', $grandTotalAMDWithShipping);
     $this->addParam('final_step', 'true');
     if (isset($_REQUEST['cho_payment_type']) && $_REQUEST['cho_payment_type'] == 'credit') {
         $cho_credit_supplier_id = $this->secure($_REQUEST['cho_credit_supplier_id']);
         $cho_selected_deposit_amount = $this->secure($_REQUEST['cho_selected_deposit_amount']);
         $cho_selected_credit_months = $this->secure($_REQUEST['cho_selected_credit_months']);
         $creditSuppliersManager = CreditSuppliersManager::getInstance($this->config, $this->args);
         $allCreditSuppliersDtos = $creditSuppliersManager->getAllCreditSuppliers();
         $creditSuppliersIds = $creditSuppliersManager->getSuppliersIdsArray($allCreditSuppliersDtos);
         $creditSuppliersInMapArrayById = $creditSuppliersManager->getCreditSuppliersInMapArrayById($allCreditSuppliersDtos);
         $creditSupplierDto = $creditSuppliersInMapArrayById[$cho_credit_supplier_id];
         //var_dump($creditSupplierDto);
         $creditSupplierDisplayName = $this->getPhrase($creditSupplierDto->getDisplayNameId());
         $this->addParam('creditSupplierDisplayName', $creditSupplierDisplayName);
         $creditManager = CreditManager::getInstance($this->config, $this->args);
         $commission = $creditSupplierDto->getCommission();
         $annualInterestPercent = floatval($creditSupplierDto->getAnnualInterestPercent());
         $annualInterestPercent += floatval($creditSupplierDto->getAnnualCommision());
         $monthlyPaymentAmount = $creditManager->calcCredit($grandTotalAMD, $cho_selected_deposit_amount, $annualInterestPercent, $cho_selected_credit_months, $commission);
         $this->addParam("monthlyPaymentAmount", round($monthlyPaymentAmount));
     }
     $this->addParam('req_params', $_REQUEST);
     $paymentType = $_REQUEST['cho_payment_type'];
     $payment_option_values = explode(',', $this->getCmsVar('payment_option_values'));
     $payment_options_display_names_ids = explode(',', $this->getCmsVar('payment_options_display_names_ids'));
     $index = array_search($paymentType, $payment_option_values);
     $paymentTypeDisplayNameId = $payment_options_display_names_ids[$index];
     $this->addParam('paymentTypeDisplayNameId', $paymentTypeDisplayNameId);
     $this->addParam('maxItemCartCount', intval($this->getCmsVar('max_item_cart_count')));
     $userManager = UserManager::getInstance($this->config, $this->args);
     $vipCustomer = $userManager->isVipAndVipEnabled($customer);
     $this->addParam('vip_enabled', $vipCustomer ? 1 : 0);
 }
 private function initManagers()
 {
     $this->itemManager = ItemManager::getInstance($this->config, $this->args);
     $this->customerCartManager = CustomerCartManager::getInstance($this->config, $this->args);
     $this->ordersManager = OrdersManager::getInstance($this->config, $this->args);
     $this->bundleItemsManager = BundleItemsManager::getInstance($this->config, $this->args);
     $this->specialFeesManager = SpecialFeesManager::getInstance($this->config, $this->args);
     $this->orderDetailsManager = OrderDetailsManager::getInstance($this->config, $this->args);
     $this->checkoutManager = CheckoutManager::getInstance($this->config, $this->args);
 }
 public function load()
 {
     $customer = $this->getCustomer();
     if (!isset($customer)) {
         return false;
     }
     $userManager = UserManager::getInstance($this->config, $this->args);
     $vipCustomer = $userManager->isVipAndVipEnabled($customer);
     if ($vipCustomer) {
         $pccDiscount = floatval($this->getCmsVar('vip_pc_configurator_discount'));
     } else {
         $pccDiscount = floatval($this->getCmsVar('pc_configurator_discount'));
     }
     $this->addParam('req_params', $_REQUEST);
     $customerEmail = strtolower($customer->getEmail());
     $customerCartManager = CustomerCartManager::getInstance($this->config, $this->args);
     $checkoutManager = CheckoutManager::getInstance($this->config, $this->args);
     $this->bundleItemsManager = BundleItemsManager::getInstance($this->config, $this->args);
     $this->itemManager = ItemManager::getInstance($this->config, $this->args);
     if (isset($_REQUEST['id']) && isset($_REQUEST['count'])) {
         //this is the case when user change the items count from his cart
         //TODO Check if user has item in his cart
         $cartItemId = $this->secure($_REQUEST['id']);
         $cartItemCount = intval($this->secure($_REQUEST['count']));
         if ($cartItemCount > 0) {
             $customerCartManager->setCartElementCount($cartItemId, $cartItemCount);
         } else {
             $customerCartManager->deleteCartElement($cartItemId);
         }
     }
     $userLevel = $this->getUserLevel();
     $user_id = $this->getUserId();
     $_cartItemsDtos = $customerCartManager->getCustomerCart($customerEmail, $user_id, $userLevel);
     $_groupedCartItems = $customerCartManager->groupBundleItemsInArray($_cartItemsDtos);
     $checkoutManager->setCartItemsDiscount($_groupedCartItems, $searchDiscount, $pccDiscount);
     $cartItemsDtos = $customerCartManager->getCustomerCart($customerEmail, $user_id, $userLevel);
     $pv = $checkoutManager->getPriceVariety($cartItemsDtos, $userLevel);
     $discountAvailable = $checkoutManager->isDiscountAvailableForAtleastOneItem($cartItemsDtos);
     $groupedCartItems = $customerCartManager->groupBundleItemsInArray($cartItemsDtos);
     $cartChanges = $customerCartManager->getCustomerCartItemsChanges($groupedCartItems);
     $customerCartManager->setCustomerCartItemsPriceChangesToCurrentItemPrices($groupedCartItems);
     $customerCartChangesMessages = $checkoutManager->getCustomerCartChangesMessages($cartChanges);
     //all cart items, bundle items grouped in sub array
     $cho_include_vat = $this->secure($_REQUEST['cho_include_vat']);
     if (!empty($_REQUEST['cho_promo_codes'])) {
         $cho_promo_codes = $this->secure($_REQUEST['cho_promo_codes']);
         $cho_promo_codes_arrray = explode(',', $cho_promo_codes);
         $validPromoDiscount = $checkoutManager->applyAllItemsPromoOnCartItems($groupedCartItems, $cho_promo_codes_arrray, $cho_include_vat);
         $existingDealsPromoCodesArray = $checkoutManager->applyDealsDiscountsOnCartItems($groupedCartItems, $cho_promo_codes_arrray, $cho_include_vat);
         $existingDealsPromoCodesArray[] = $validPromoDiscount;
         $_REQUEST['cho_promo_codes'] = implode(',', $existingDealsPromoCodesArray);
     }
     list($grandTotalAMD, $grandTotalUSD) = $customerCartManager->calcCartTotal($groupedCartItems, true, $userLevel, $cho_include_vat);
     $all_non_bundle_items_has_vat = $customerCartManager->checkAllNonBundleItemsHasVatPrice($groupedCartItems);
     if (!$all_non_bundle_items_has_vat && $cho_include_vat == 1) {
         $customerCartChangesMessages[] = $this->getPhraseSpan(566);
     }
     $this->addParam("all_non_bundle_items_has_vat", $all_non_bundle_items_has_vat);
     $this->addParam("minimum_order_amount_exceed", $grandTotalAMD >= intval($this->getCmsVar("minimum_order_amount_amd")));
     $this->addParam("minimum_order_amount_amd", $this->getCmsVar("minimum_order_amount_amd"));
     if (!empty($customerCartChangesMessages)) {
         $this->addParam('customerMessages', $customerCartChangesMessages);
     }
     $allItemsAreAvailable = $customerCartManager->areAllItemsAvailableInCustomerCart($groupedCartItems);
     //discount available for at leat one item in the cart
     $this->addParam('discountAvailable', $discountAvailable);
     //priceVariety the price variety in customer cart. Can be 'amd', 'usd' or 'both';
     $this->addParam('priceVariety', $pv);
     $this->addParam('checkoutManager', $checkoutManager);
     //all cart items, bundle items grouped in sub array
     $this->addParam('cartItems', $groupedCartItems);
     $this->addParam('itemManager', $this->itemManager);
     $this->addParam('allItemsAreAvailable', $allItemsAreAvailable);
     $this->addParam('emptyCart', empty($cartItemsDtos));
     $this->addParam('bundleItemsManager', $this->bundleItemsManager);
     //cart grand totla included discounts, this is the final value that customer should pay for his cart
     $this->addParam('grandTotalAMD', $grandTotalAMD);
     $this->addParam('grandTotalUSD', $grandTotalUSD);
     $this->addParam('maxItemCartCount', intval($this->getCmsVar('max_item_cart_count')));
 }