public function view() { $codeerror = false; $codesuccess = false; if ($this->isPost()) { if ($this->post('action') == 'code' && $this->post('code')) { $codesuccess = StoreCart::storeCode($this->post('code')); $codeerror = !$codesuccess; } if ($this->post('action') == 'update') { $data = $this->post(); $result = StoreCart::update($data); $added = $result['added']; $returndata = array('success' => true, 'quantity' => (int) $data['pQty'], 'action' => 'update', 'added' => $added); } if ($this->post('action') == 'clear') { StoreCart::clear(); $returndata = array('success' => true, 'action' => 'clear'); } if ($this->post('action') == 'remove') { $data = $this->post(); $result = StoreCart::remove($data['instance']); $returndata = array('success' => true, 'action' => 'remove'); } } $this->set('actiondata', $returndata); $this->set('codeerror', $codeerror); $this->set('codesuccess', $codesuccess); $this->set('cart', StoreCart::getCart()); $this->set('discounts', StoreCart::getDiscounts()); $this->set('total', StoreCalculator::getSubTotal()); $this->addHeaderItem("\n <script type=\"text/javascript\">\n var PRODUCTMODAL = '" . View::url('/productmodal') . "';\n var CARTURL = '" . View::url('/cart') . "';\n var CHECKOUTURL = '" . View::url('/checkout') . "';\n </script>\n "); $this->requireAsset('javascript', 'vivid-store'); $this->requireAsset('css', 'vivid-store'); $discountsWithCodesExist = StoreDiscountRule::discountsWithCodesExist(); $this->set("discountsWithCodesExist", $discountsWithCodesExist); }
public static function getTotals() { $subTotal = self::getSubTotal(); $taxes = StoreTax::getTaxes(); $addedTaxTotal = 0; $includedTaxTotal = 0; $taxCalc = Config::get('vividstore.calculation'); if ($taxes) { foreach ($taxes as $tax) { if ($taxCalc != 'extract') { $addedTaxTotal += $tax['taxamount']; } else { $includedTaxTotal += $tax['taxamount']; } } } $shippingTotal = self::getShippingTotal(); $discountedSubtotal = $subTotal; $discounts = StoreCart::getDiscounts(); foreach ($discounts as $discount) { if ($discount->drDeductFrom == 'subtotal') { if ($discount->drDeductType == 'value') { $discountedSubtotal -= $discount->drValue; } if ($discount->drDeductType == 'percentage') { $discountedSubtotal -= $discount->drPercentage / 100 * $discountedSubtotal; } } } $total = $discountedSubtotal + $addedTaxTotal + $shippingTotal; foreach ($discounts as $discount) { if ($discount->drDeductFrom == 'total') { if ($discount->drDeductType == 'value') { $total -= $discount->drValue; } if ($discount->drDeductType == 'percentage') { $total -= $discount->drPercentage / 100 * $total; } } } return array('subTotal' => $subTotal, 'taxes' => $taxes, 'taxTotal' => $addedTaxTotal + $includedTaxTotal, 'shippingTotal' => $shippingTotal, 'total' => $total); }
public function add($data, $pm, $status = null) { $db = Database::get(); //get who ordered it $customer = new Customer(); //what time is it? $dt = Core::make('helper/date'); $now = $dt->getLocalDateTime(); //get the price details $smID = \Session::get('smID'); if ($smID > 0) { $sm = ShippingMethod::getByID($smID); $shippingMethodTypeName = $sm->getShippingMethodType()->getShippingMethodTypeName(); $shippingMethodName = $sm->getName(); $smName = $shippingMethodTypeName . ": " . $shippingMethodName; } else { $smName = "No Shipping Method"; } $shipping = VividCart::getShippingTotal(); $taxes = Tax::getTaxes(); $totals = VividCart::getTotals(); $total = $totals['total']; $taxCalc = Config::get('vividstore.calculation'); $taxTotal = array(); $taxIncludedTotal = array(); $taxLabels = array(); foreach ($taxes as $tax) { if ($taxCalc == 'extract') { $taxIncludedTotal[] = $tax['taxamount']; } else { $taxTotal[] = $tax['taxamount']; } $taxLabels[] = $tax['name']; } $taxTotal = implode(',', $taxTotal); $taxIncludedTotal = implode(',', $taxIncludedTotal); $taxLabels = implode(',', $taxLabels); //get payment method $pmName = $pm->getPaymentMethodName(); //add the order $vals = array($customer->getUserID(), $now, $pmName, $smName, $shipping, $taxTotal, $taxIncludedTotal, $taxLabels, $total); $db->Execute("INSERT INTO VividStoreOrders(cID,oDate,pmName,smName,oShippingTotal,oTax,oTaxIncluded,oTaxName,oTotal) VALUES (?,?,?,?,?,?,?,?,?)", $vals); $oID = $db->lastInsertId(); $order = Order::getByID($oID); if ($status) { $order->updateStatus($status); } else { $order->updateStatus(OrderStatus::getStartingStatus()->getHandle()); } $email = $customer->getEmail(); $billing_first_name = $customer->getValue("billing_first_name"); $billing_last_name = $customer->getValue("billing_last_name"); $billing_address = $customer->getValueArray("billing_address"); $billing_phone = $customer->getValue("billing_phone"); $shipping_first_name = $customer->getValue("shipping_first_name"); $shipping_last_name = $customer->getValue("shipping_last_name"); $shipping_address = $customer->getValueArray("shipping_address"); $order->setAttribute("email", $email); $order->setAttribute("billing_first_name", $billing_first_name); $order->setAttribute("billing_last_name", $billing_last_name); $order->setAttribute("billing_address", $billing_address); $order->setAttribute("billing_phone", $billing_phone); if ($smID) { $order->setAttribute("shipping_first_name", $shipping_first_name); $order->setAttribute("shipping_last_name", $shipping_last_name); $order->setAttribute("shipping_address", $shipping_address); } $customer->setLastOrderID($oID); //add the order items $cart = VividCart::getCart(); foreach ($cart as $cartItem) { $taxes = Tax::getTaxForProduct($cartItem); $taxProductTotal = array(); $taxProductIncludedTotal = array(); $taxProductLabels = array(); foreach ($taxes as $tax) { if ($taxCalc == 'extract') { $taxProductIncludedTotal[] = $tax['taxamount']; } else { $taxProductTotal[] = $tax['taxamount']; } $taxProductLabels[] = $tax['name']; } $taxProductTotal = implode(',', $taxProductTotal); $taxProductIncludedTotal = implode(',', $taxProductIncludedTotal); $taxProductLabels = implode(',', $taxProductLabels); OrderItem::add($cartItem, $oID, $taxProductTotal, $taxProductIncludedTotal, $taxProductLabels); } $discounts = VividCart::getDiscounts(); if ($discounts) { foreach ($discounts as $discount) { $order->addDiscount($discount, VividCart::getCode()); } } //if the payment method is not external, go ahead and complete the order. if (!$pm->external) { $order->completeOrder(); } return $order; }
public function view() { $pkg = Package::getByHandle('vivid_store'); $customer = new StoreCustomer(); $this->set('customer', $customer); $guestCheckout = Config::get('vividstore.guestCheckout'); $this->set('guestCheckout', $guestCheckout ? $guestCheckout : 'off'); $this->set('requiresLogin', StoreCart::requiresLogin()); if (StoreCart::getTotalItemsInCart() == 0) { $this->redirect("/cart/"); } $this->set('form', Core::make("helper/form")); $allcountries = Core::make('helper/lists/countries')->getCountries(); $db = Loader::db(); $ak = UserAttributeKey::getByHandle('billing_address'); $row = $db->GetRow('select akHasCustomCountries, akDefaultCountry from atAddressSettings where akID = ?', array($ak->getAttributeKeyID())); $defaultBillingCountry = $row['akDefaultCountry']; if ($row['akHasCustomCountries'] == 1) { $availableBillingCountries = $db->GetCol('select country from atAddressCustomCountries where akID = ?', array($ak->getAttributeKeyID())); $billingCountries = array(); foreach ($availableBillingCountries as $countrycode) { $billingCountries[$countrycode] = $allcountries[$countrycode]; } } else { $billingCountries = $allcountries; } $ak = UserAttributeKey::getByHandle('shipping_address'); $row = $db->GetRow('select akHasCustomCountries, akDefaultCountry from atAddressSettings where akID = ?', array($ak->getAttributeKeyID())); $defaultShippingCountry = $row['akDefaultCountry']; if ($row['akHasCustomCountries'] == 1) { $availableShippingCountries = $db->GetCol('select country from atAddressCustomCountries where akID = ?', array($ak->getAttributeKeyID())); $shippingCountries = array(); foreach ($availableShippingCountries as $countrycode) { $shippingCountries[$countrycode] = $allcountries[$countrycode]; } } else { $shippingCountries = $allcountries; } $discountsWithCodesExist = StoreDiscountRule::discountsWithCodesExist(); $this->set("discountsWithCodesExist", $discountsWithCodesExist); $this->set('cart', StoreCart::getCart()); $this->set('discounts', StoreCart::getDiscounts()); $this->set('hasCode', StoreCart::hasCode()); $this->set("billingCountries", $billingCountries); $this->set("shippingCountries", $shippingCountries); $this->set("defaultBillingCountry", $defaultBillingCountry); $this->set("defaultShippingCountry", $defaultShippingCountry); $this->set("states", Core::make('helper/lists/states_provinces')->getStates()); $totals = StoreCalculator::getTotals(); $this->set('subtotal', $totals['subTotal']); $this->set('taxes', $totals['taxes']); $this->set('taxtotal', $totals['taxTotal']); $this->set('shippingtotal', $totals['shippingTotal']); $this->set('total', $totals['total']); $this->set('shippingEnabled', StoreCart::isShippable()); $this->addHeaderItem("\n <script type=\"text/javascript\">\n var PRODUCTMODAL = '" . View::url('/productmodal') . "';\n var CARTURL = '" . View::url('/cart') . "';\n var CHECKOUTURL = '" . View::url('/checkout') . "';\n </script>\n "); $this->requireAsset('javascript', 'vivid-store'); $this->requireAsset('css', 'vivid-store'); $this->addFooterItem("\n <script type=\"text/javascript\">\n \$(function() {\n vividStore.loadViaHash();\n });\n </script>\n "); $enabledMethods = StorePaymentMethod::getEnabledMethods(); $availableMethods = array(); foreach ($enabledMethods as $em) { $emmc = $em->getMethodController(); if ($totals['total'] >= $emmc->getPaymentMinimum() && $totals['total'] <= $emmc->getPaymentMaximum()) { $availableMethods[] = $em; } } $this->set("enabledPaymentMethods", $availableMethods); }