/** * 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 CheckoutManager($config, $args); } return self::$instance; }
public function load() { $payment_option_values = explode(',', $this->getCmsVar('payment_option_values')); $payment_options_display_names_ids = explode(',', $this->getCmsVar('payment_options_display_names_ids')); $this->addParam('payment_options_display_names_ids', $payment_options_display_names_ids); $this->addParam('payment_option_values', $payment_option_values); if (isset($_REQUEST['cho_payment_type']) && $_REQUEST['cho_payment_type'] == 'credit') { $checkoutManager = CheckoutManager::getInstance($this->config, $this->args); list($grandTotalAMD, $grandTotalUSD) = $checkoutManager->getCustomerCartGrandTotals($this->getCustomer(), $this->sessionManager->getUser(), $this->getUserLevel()); $this->addParam("grandTotalAMD", $grandTotalAMD); $this->addParam("grandTotalUSD", $grandTotalUSD); //credit supplier $creditSuppliersManager = CreditSuppliersManager::getInstance($this->config, $this->args); $allCreditSuppliers = $creditSuppliersManager->getAllCreditSuppliers(); $allCreditSuppliers = $creditSuppliersManager->getCreditSuppliersInMapArrayById($allCreditSuppliers); $creditSuppliersDisplayNamesIds = $creditSuppliersManager->getSuppliersDisplayNameIdsArray($allCreditSuppliers); $creditSuppliersDisplayNames = $this->getPhrases($creditSuppliersDisplayNamesIds); $defaultCreditSupplierDto = reset($allCreditSuppliers); $selected_credit_supplier_id = $defaultCreditSupplierDto->getId(); if (isset($_REQUEST['cho_credit_supplier_id'])) { $selected_credit_supplier_id = $_REQUEST['cho_credit_supplier_id']; } $_REQUEST['cho_credit_supplier_id'] = $selected_credit_supplier_id; $this->addParam("creditSuppliersIds", array_keys($allCreditSuppliers)); $this->addParam("creditSuppliersDisplayNames", $creditSuppliersDisplayNames); $selectedCreditSupplierDto = $allCreditSuppliers[$selected_credit_supplier_id]; //credit supplier possible months $possibleCreditMonths = explode(',', $selectedCreditSupplierDto->getPossibleCreditMonths()); $cho_selected_credit_months = $possibleCreditMonths[0]; if (isset($_REQUEST['cho_selected_credit_months'])) { $cho_selected_credit_months = $_REQUEST['cho_selected_credit_months']; } if (!in_array($cho_selected_credit_months, $possibleCreditMonths)) { $cho_selected_credit_months = $possibleCreditMonths[0]; } $_REQUEST['cho_selected_credit_months'] = $cho_selected_credit_months; $this->addParam("possibleCreditMonths", $possibleCreditMonths); //deposit amount $cho_selected_deposit_amount = 0; if (isset($_REQUEST['cho_selected_deposit_amount'])) { $cho_selected_deposit_amount = intval($this->secure($_REQUEST['cho_selected_deposit_amount'])); } $_REQUEST['cho_selected_deposit_amount'] = $cho_selected_deposit_amount; //credit supplier interest $commission = $selectedCreditSupplierDto->getCommission(); $annualInterestPercent = floatval($selectedCreditSupplierDto->getAnnualInterestPercent()); $credit_supplier_annual_commision = floatval($selectedCreditSupplierDto->getAnnualCommision()); $this->addParam("credit_supplier_interest_percent", $annualInterestPercent); $this->addParam("credit_supplier_annual_commision", $credit_supplier_annual_commision); //credit monthly payment calculation $creditManager = CreditManager::getInstance($this->config, $this->args); $monthlyPayment = $creditManager->calcCredit($grandTotalAMD, $cho_selected_deposit_amount, $annualInterestPercent + $credit_supplier_annual_commision, $cho_selected_credit_months, $commission); $this->addParam("credit_monthly_payment", round($monthlyPayment)); $this->addParam("minimum_credit_amount", intval($selectedCreditSupplierDto->getMinimumCreditAmount())); $grandTotalAmdWithCommission = intval($grandTotalAMD / (1 - $commission / 100)); $this->addParam("grandTotalAmdWithCommission", $grandTotalAmdWithCommission); } $this->addParam('req_params', $_REQUEST); }
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'))); }
/** * Returns total of the cart items in following format * array($grandTotalAMD, $grandTotalUSD) * @param $groupedCartItems should have getCustomerCart function format and * then group the result item using groupBundleItemsInArray function. */ public function calcCartTotal($groupedCartItems, $withPromoDiscount = false, $userLevel, $includedVat = 0) { $checkoutManager = CheckoutManager::getInstance($this->config, $this->args); assert(is_array($groupedCartItems)); if (empty($groupedCartItems)) { return null; } $grandTotalAMD = 0; $grandTotalUSD = 0; foreach ($groupedCartItems as $key => $groupedCartItem) { if (is_array($groupedCartItem)) { //so it's bundle item assert(!empty($groupedCartItem)); $bundleItemTotalDealsDiscountAMD = 0; if ($withPromoDiscount == true) { $bundleItemTotalDealsDiscountAMD = $checkoutManager->getBundleItemTotalDealsDiscountAMD($groupedCartItem); $discountParam = floatval(1 - floatval($groupedCartItem[0]->getDiscount() / 100)); } else { $discountParam = 1; } list($bundleTotAMD, $bundleTotUSD, $specialFeesTotalAMD) = $this->bundleItemsManager->calcBundlePriceForCustomerWithoutDiscount($groupedCartItem, $userLevel); $bundleCount = intval($groupedCartItem[0]->getCount()); $grandTotalAMD += $bundleTotAMD * $bundleCount * $discountParam + $bundleCount * $specialFeesTotalAMD - $bundleItemTotalDealsDiscountAMD * $bundleCount; $grandTotalUSD += $bundleTotUSD * $bundleCount; } else { if ($groupedCartItem->getItemAvailable() == 0) { continue; } if ($userLevel == UserGroups::$ADMIN || $userLevel == UserGroups::$COMPANY || $groupedCartItem->getIsDealerOfThisCompany() == 1) { $itemTotalPrice = floatval($includedVat == 1 ? $groupedCartItem->getVatPrice() : $groupedCartItem->getDealerPrice()) * intval($groupedCartItem->getCount()); } else { $itemTotalPrice = floatval($includedVat == 1 ? $groupedCartItem->getCustomerVatItemPrice() : $groupedCartItem->getCustomerItemPrice()) * intval($groupedCartItem->getCount()); } $dealDiscountAmd = 0; if ($withPromoDiscount == true) { if ($groupedCartItem->getDealDiscountAmd() > 0) { $dealDiscountAmd = $groupedCartItem->getDealDiscountAmd(); } $discountParam = floatval(1 - floatval($groupedCartItem->getDiscount() / 100)); } else { $discountParam = 1; } if ($userLevel == UserGroups::$ADMIN || $userLevel == UserGroups::$COMPANY || $groupedCartItem->getIsDealerOfThisCompany() == 1) { $grandTotalUSD += $itemTotalPrice; } else { $grandTotalAMD += $this->itemManager->exchangeFromUsdToAMD($itemTotalPrice) * $discountParam - $dealDiscountAmd * intval($groupedCartItem->getCount()); } } } return array(intval($grandTotalAMD), $grandTotalUSD); }