﻿var Step3LicenseViewHelper = (function() {
    function fnLicenseViewHelperConstructor(oNewView) {
        /****************************************************/
        /*                                                  */
        /*                                                  */
        /*                 Private Variables                */
        /*                                                  */
        /*                                                  */
        /****************************************************/

        var oView;

        /****************************************************/
        /*                                                  */
        /*                                                  */
        /*            Class Level Private Methods           */
        /*                                                  */
        /*                                                  */
        /****************************************************/

        function getCurrencySign(iCurrency) {
            var sCurrencySign;

            switch (iCurrency) {
                case 2:
                    sCurrencySign = "\u00A3";
                    break;
                case 4:
                    sCurrencySign = "\u20AC";
                    break;
                default:
                    sCurrencySign = "$";
                    break;
            }

            return sCurrencySign;
        }

        /****************************************************/
        /*                                                  */
        /*                                                  */
        /*          Class Level Priveleged Methods          */
        /*                                                  */
        /*                                                  */
        /****************************************************/

        this.addAllLicenseItems = function(aLicenseItems, hNetSuiteItems, iCurrency) {
            var sCurrencySign = getCurrencySign(iCurrency);

            var fTotalPrice = 0.00;

            aLicenseItems.each(function(oLicenseItem, iIndex) {
                var iId = oLicenseItem.getId();
                var iQuantity = oLicenseItem.getQuantity();
                if (iId && iId > 0 && iQuantity && iQuantity > 0) {
                    var oNetSuiteItem = hNetSuiteItems.get(iId);
                    var fAmount = oNetSuiteItem.getPrices().get(iCurrency).getAmount();
                    var fSubTotal = iQuantity * fAmount;
                    fSubTotal = parseInt(fSubTotal);
                    oView.addLicenseItem(iQuantity, oNetSuiteItem.getSKU() + " - " + oNetSuiteItem.getDescription(), sCurrencySign + NumericUtilities.formatNumberWithCommas(fAmount), sCurrencySign + NumericUtilities.formatNumberWithCommas(fSubTotal));
                    fTotalPrice += fSubTotal;
                }
            });

            return fTotalPrice;
        }

        this.addAllPromotions = function(aLicenseItems, hNetSuiteItems, hPromotions, iCurrency, oRegion) {
            var sCurrencySign = getCurrencySign(iCurrency);

            var fTotalSavings = 0.00;

            aLicenseItems.each(function(oLicenseItem, iIndex) {
                var iId = oLicenseItem.getId();
                var iQuantity = oLicenseItem.getQuantity();
                if (iId && iId > 0 && iQuantity && iQuantity > 0) {
                    var oNetSuiteItem = hNetSuiteItems.get(iId);
                    var oPrice = oNetSuiteItem.getPrices().get(iCurrency);
                    var fBasePrice = oPrice.getAmount();
                    var fSalePrice = fBasePrice;
                    var aPricePromotions = oPrice.getPromotions();
                    aPricePromotions.each(function(oPricePromotion, iIndex) {
                        var oPromotion = hPromotions.get(oPricePromotion.getId());
                        var fPreviousSalePrice = fSalePrice;
                        var fAmount = oPromotion.getAmount();
                        if (oPromotion.getIsPercentage())
                            fSalePrice = Math.abs(fSalePrice * (100 + fAmount) / 100);
                        else
                            fSalePrice += fAmount;
                        fSalePrice = parseInt(fSalePrice);
                        var fLicenseItemSavings = iQuantity * (fPreviousSalePrice - fSalePrice);
                        fLicenseItemSavings = parseInt(fLicenseItemSavings);
                        var dPromotionEndDate = oPromotion.getEndDate();
                        var iMonth = dPromotionEndDate.getMonth() + 1;

                        // This case added for local differences in some countries
                        // 'Africa', 'Asia', 'Europe', 'Middle East', 'North America', 'South America', 'United Kingdom'

                        var dateYear = dPromotionEndDate.getFullYear();
                        var dateMonth = (iMonth < 10 ? "0" : "") + iMonth.toString();
                        var dateDay = dPromotionEndDate.getDate();
                        var dateBlock = '';

                        switch (oRegion.getName()) {
                            case 'North America':
                                dateBlock = dateMonth + "/" + dateDay + "/" + dateYear;
                                break;
                            case 'Asia':
                                dateBlock = dateYear + "/" + dateMonth + "/" + dateDay;
                                break;
                            case 'South America':
                                dateBlock = dateYear + "/" + dateMonth + "/" + dateDay;
                                break;
                            default:
                                dateBlock = dateDay + "/" + dateMonth + "/" + dateYear;
                        }

                        var thirtydays = ((((30 * 24) * 60) * 60) * 1000);
                        //oView.addPromotion("dateBlock=" + dateBlock + "<br />dPromotionEndDate=" + Date.parse(dPromotionEndDate) + "<br />currentExpirationDate=" + Date.parse(currentExpirationDate), sCurrencySign + NumericUtilities.formatNumberWithCommas(fLicenseItemSavings));
                        if (Date.parse(dPromotionEndDate) < (Date.parse(currentExpirationDate) + thirtydays)) {
                            oView.addPromotion("Purchase " + oNetSuiteItem.getDescription() + " by " + dateBlock + " to Save", sCurrencySign + NumericUtilities.formatNumberWithCommas(fLicenseItemSavings));
                        }
                        else {
                            oView.addPromotion("Purchase " + oNetSuiteItem.getDescription() + " to Save", sCurrencySign + NumericUtilities.formatNumberWithCommas(fLicenseItemSavings));
                        }
                    });
                    fTotalSavings += iQuantity * (fBasePrice - fSalePrice);
                }
            });

            return fTotalSavings;
        }

        this.updateSummary = function(aLicenseItems, hNetSuiteItems, hPromotions, oRegion) {
            oView.clearLicenseItemList();
            oView.clearPromotionList();
            var iCurrency = oRegion.getCurrency();
            var sCurrencySign = getCurrencySign(iCurrency);
            var fMSRPPrice = this.addAllLicenseItems(aLicenseItems, hNetSuiteItems, iCurrency);
            var fPromotionsTotal = this.addAllPromotions(aLicenseItems, hNetSuiteItems, hPromotions, iCurrency, oRegion);

            oView.setMSRPPrice(sCurrencySign + NumericUtilities.formatNumberWithCommas(fMSRPPrice));
            if (fPromotionsTotal == 0) {
                oView.hideTotalPrice();
            }
            else {
                var fTotalPrice = fMSRPPrice - fPromotionsTotal;
                oView.setTotalPrice(sCurrencySign + NumericUtilities.formatNumberWithCommas(fTotalPrice));
                oView.showTotalPrice();
            }
        }

        /****************************************************/
        /*                                                  */
        /*                                                  */
        /*                 Initialize Class                 */
        /*                                                  */
        /*                                                  */
        /****************************************************/

        oView = oNewView;
    }

    return fnLicenseViewHelperConstructor;
})();