Exemple #1
0
        public void ValidateCart(Boolean removeZeroQtyItems = false)
        {
            PurchaseInfo = NBrightBuyUtils.ProcessEventProvider(EventActions.ValidateCartBefore, PurchaseInfo);

            var    itemList          = GetCartItemList();
            Double subtotalcost      = 0;
            Double totaldealerbonus  = 0;
            Double totaldiscount     = 0;
            Double totalsalediscount = 0;
            Double totalqty          = 0;
            Double totalweight       = 0;
            Double totalunitcost     = 0;

            // Calculate Promotions
            // Calculate first, so if we add items the price is calculated
            if (PromoInterface.Instance() != null)
            {
                var promol = PromoInterface.ProviderList;
                foreach (var p in promol)
                {
                    PurchaseInfo = PromoInterface.Instance(p.Key).CalculatePromotion(PortalId, PurchaseInfo);
                    itemList     = GetCartItemList(); // get any new items
                }
            }

            var strXml = "<items>";

            foreach (var info in itemList)
            {
                // check product still exists and remove if deleted, altered or disabled.

                var cartItem = ValidateCartItem(PortalId, UserId, info, removeZeroQtyItems);
                if (cartItem != null)
                {
                    strXml            += cartItem.XMLData;
                    totalunitcost     += info.GetXmlPropertyDouble("genxml/unitcost");
                    subtotalcost      += info.GetXmlPropertyDouble("genxml/totalcost");
                    totaldealerbonus  += info.GetXmlPropertyDouble("genxml/totaldealerbonus");
                    totaldiscount     += info.GetXmlPropertyDouble("genxml/totaldiscount");
                    totalsalediscount += info.GetXmlPropertyDouble("genxml/salediscount");
                    totalqty          += info.GetXmlPropertyDouble("genxml/qty");
                    totalweight       += info.GetXmlPropertyDouble("genxml/totalweight");
                }
            }
            strXml += "</items>";
            PurchaseInfo.RemoveXmlNode("genxml/items");
            PurchaseInfo.AddXmlNode(strXml, "items", "genxml");
            PopulateItemList(); // put changed items and prices back into base class for saving to DB

            // calculate totals

            var promototaldiscount = (totaldiscount - totalsalediscount);

            PurchaseInfo.SetXmlPropertyDouble("genxml/totalqty", totalqty);
            PurchaseInfo.SetXmlPropertyDouble("genxml/totalweight", totalweight);
            PurchaseInfo.SetXmlPropertyDouble("genxml/totalunitcost", totalunitcost);
            PurchaseInfo.SetXmlPropertyDouble("genxml/subtotalcost", subtotalcost);
            PurchaseInfo.SetXmlPropertyDouble("genxml/subtotal", subtotalcost);
            PurchaseInfo.SetXmlPropertyDouble("genxml/appliedsubtotal", (subtotalcost + totalsalediscount));


            // calc any voucher amounts
            var    discountcode    = PurchaseInfo.GetXmlProperty("genxml/extrainfo/genxml/textbox/promocode");
            Double voucherDiscount = 0;

            if (DiscountCodeInterface.Instance() != null)
            {
                PurchaseInfo    = DiscountCodeInterface.Instance().CalculateVoucherAmount(PortalId, UserId, PurchaseInfo, discountcode);
                voucherDiscount = PurchaseInfo.GetXmlPropertyDouble("genxml/voucherdiscount");
            }
            promototaldiscount += voucherDiscount;
            totaldiscount      += voucherDiscount;

            PurchaseInfo.SetXmlPropertyDouble("genxml/applieddiscount", totaldiscount);

            PurchaseInfo.SetXmlPropertyDouble("genxml/totaldealerbonus", totaldealerbonus);

            PurchaseInfo.SetXmlPropertyDouble("genxml/totaldiscount", totaldiscount);
            PurchaseInfo.SetXmlPropertyDouble("genxml/totalsalediscount", totalsalediscount);


            //add shipping
            Double shippingcost       = 0;
            Double shippingdealercost = 0;
            var    shippingkey        = PurchaseInfo.GetXmlProperty("genxml/extrainfo/genxml/radiobuttonlist/shippingprovider");
            var    currentcartstage   = PurchaseInfo.GetXmlProperty("genxml/currentcartstage");

            if (currentcartstage == "cartaddress" || currentcartstage == "cartsummary") // can only calc shipping on this stage.
            {
                ShippingInterface shipprov = null;
                shipprov = ShippingInterface.Instance(shippingkey);
                if (shipprov != null)
                {
                    PurchaseInfo       = shipprov.CalculateShipping(PurchaseInfo);
                    shippingcost       = PurchaseInfo.GetXmlPropertyDouble("genxml/shippingcost");
                    shippingdealercost = PurchaseInfo.GetXmlPropertyDouble("genxml/shippingdealercost");
                }
                PurchaseInfo.SetXmlPropertyDouble("genxml/appliedshipping", AppliedCost(shippingcost, shippingdealercost));
            }
            else
            {
                // clear the provider if not cartshipping stage
                PurchaseInfo.SetXmlProperty("genxml/extrainfo/genxml/radiobuttonlist/shippingprovider", "");
            }


            //add tax
            Double appliedtax     = 0;
            var    taxproviderkey = PurchaseInfo.GetXmlProperty("genxml/extrainfo/genxml/hidden/taxproviderkey");
            var    taxprov        = TaxInterface.Instance(taxproviderkey);

            if (taxprov != null)
            {
                PurchaseInfo = taxprov.Calculate(PurchaseInfo);
                appliedtax   = PurchaseInfo.GetXmlPropertyDouble("genxml/appliedtax");
            }

            //cart full total
            var total = (subtotalcost + shippingcost + appliedtax) - promototaldiscount;

            if (total < 0)
            {
                total = 0;
            }
            PurchaseInfo.SetXmlPropertyDouble("genxml/total", total);
            PurchaseInfo.SetXmlPropertyDouble("genxml/appliedtotal", total);

            if (PurchaseInfo.GetXmlProperty("genxml/clientmode") == "True")
            {
                // user not editor, so stop edit mode.
                if (!UserController.Instance.GetCurrentUserInfo().IsInRole("Administrators") && !UserController.Instance.GetCurrentUserInfo().IsInRole(StoreSettings.ManagerRole) && !UserController.Instance.GetCurrentUserInfo().IsInRole(StoreSettings.EditorRole))
                {
                    PurchaseInfo.SetXmlProperty("genxml/clientmode", "False");
                }
            }

            PurchaseInfo = NBrightBuyUtils.ProcessEventProvider(EventActions.ValidateCartAfter, PurchaseInfo);

            SavePurchaseData();
        }