(function () { 'use strict'; angular .module('LyncPix.ShoppingCartModule') .controller('ShoppingCartController', ShoppingCartController); /** @ngInject */ function ShoppingCartController($log, $rootScope, $timeout, toastr, ProductCategories, Products, ShoppingCart, Currency, Client, Config) { var vm = this; var UGUID = '38A5B679-B290-48C0-A828-36C8F6727D15'; $log.debug('ShoppingCartController Activated'); activate(); function activate() { vm.data = { count: 1, options: {} }; vm.productOptionsMap = {}; loadProductCategories(); loadCurrency(); loadManager(); vm.selectCategory = selectCategory; vm.selectProduct = selectProduct; vm.loadProducts = loadProducts; vm.loadProduct = loadProduct; vm.optionChanged = optionChanged; vm.addToCart = addToCart; vm.imageAdded = imageAdded; vm.removeImage = removeImage; vm.fileUploadValidate = fileUploadValidate; vm.closeCroppingMode = closeCroppingMode; vm.aspectRatioChanged = aspectRatioChanged; } vm.epocaManagers = Config.epocaManagers; vm.isEpocaManager = function () { return vm.manager && vm.epocaManagers.indexOf(vm.manager.id) > -1; } var modal = $('#modal_customize_item'); modal.on("shown.bs.modal",function (e) { modal.find('.customize-image').hide(); if (vm.productCategories.length == 1) { selectCategory(vm.productCategories[0]); loadProducts(vm.productCategories[0].id); $('#CurrentCustomizeImage_2').show(); } else { $('#CurrentCustomizeImage_1').show(); } }) function loadManager() { Client.loadSapakManager(UGUID).then(function (manager) { vm.manager = manager; }); } vm.categories = []; function filterCategoriesWithoutProducts(categories) { /* 15/11/2023 - recursive function to get categories function getNext() { if (categories.length) { return categories.shift(); } return ''; } let category = getNext(); if (category == '') { vm.productCategories = vm.categories; vm.showFourProductCategoties = 'false'; if (vm.productCategories.length <= 4) { vm.showFourProductCategoties = 'true'; } if (vm.productCategories.length && !vm.data.category) { selectCategory(vm.productCategories[0]); } if(vm.categories.length == 0){ $('.print-it-image').hide(); } modal.find('.customize-image').hide(); if(vm.productCategories.length == 1){ let category = vm.categories[0]; selectCategory(category.id); loadProducts(category.id); $('#CurrentCustomizeImage_2').show(); } else { $('#CurrentCustomizeImage_1').show(); } return false; } Products.getAllSimple(category.id, undefined, UGUID) .then(function (products) { console.log(products); products = products.filter(function (row) { return row.maxImages <= 1; }); if (products.length > 0) { vm.categories.push(category); } filterCategoriesWithoutProducts(categories); }); */ } function loadProductCategories() { ProductCategories.getAll(undefined, UGUID) .then(function (productCategories) { //lets filter out all the categories without any product filterCategoriesWithoutProducts(productCategories); }); } function loadCurrency() { Currency.get(undefined, UGUID) .then(function (currency) { vm.currency = currency; }); } function loadProducts(categoryID, event) { vm.products = []; Products.getAllSimple(categoryID, undefined, UGUID) .then(function (products) { vm.products = products.filter(function (row) { return row.maxImages <= 1; }); if (vm.products.length == 1) { vm.product = vm.products[0]; loadProduct(vm.product.id); selectProduct(vm.products[0]); var target = $('#CurrentCustomizeImage_3'); if(typeof event == typeof undefined && vm.productCategories.length == 1){ var cur = $('#CurrentCustomizeImage_2'); target.find('.btn-back').hide(); cur.hide(); } else { let btn = $(event.target); let back_btn = target.find('.btn-back'); var common = btn.attr("data-common"); //jump directly to step 3 $("." + common).hide(); //config the back button back_btn.data('traget', 'CurrentCustomizeImage_1').attr('data-traget', 'CurrentCustomizeImage_1'); } target.show(); return false; } else { let back_btn = $('#CurrentCustomizeImage_3').find('.btn-back'); back_btn.data('traget', 'CurrentCustomizeImage_2').attr('data-traget', 'CurrentCustomizeImage_2'); } vm.showFourProducts = 'false'; if (vm.products.length <= 4) { vm.showFourProducts = 'true'; } if (vm.products.length && !vm.data.product) { selectProduct(vm.products[0]); } }); } function loadProduct(id) { vm.submitted = false; Products.get(id, undefined, UGUID) .then(function (product) { vm.isCroppingMode = false; vm.data.product = product; vm.data.product.properties.options = vm.data.product.properties.options.map(function (e) { if (e.hidden) { var parts = String(e.hidden).split(' -> '); e.parentName = String(parts[0]).trim(); e.parentValue = String(parts[1]).trim(); } return e; }); vm.productOptionsMap = vm.data.product.properties.options.reduce(function (prev, cur) { prev[cur.name] = cur; return prev; }, {}); vm.productOptionsMap.color = {type: 'combo'}; $timeout(optionChanged, 500); }); } function selectCategory(category) { if (vm.data.category && vm.data.category.id !== category.id) { vm.data.product = undefined; } vm.data.category = category; } function selectProduct(product) { if (!vm.data.product || (vm.data.product && vm.data.product.id !== product.id)) { vm.data.options = {}; } vm.data.price = 0; vm.submitted = false; vm.data.product = product; } vm.optionHasImages = function (option) { let ret = option.values.filter(function (row) { return row.hasOwnProperty('image') && (row.image != null || row.images != ''); }).length > 0; return ret; } vm.needsAllSpace = function (option) { return option.name.length > 35 || (['radio', 'checkbox'].indexOf(option.type.toLowerCase()) > -1 && vm.optionHasImages(option)); } vm.isOptionVisible = function (option) { return isParentVisible(option); /*if (!option.hasOwnProperty('hidden') || false == option.hidden) { return true; } return !option.hidden || (option.parentName && vm.data.options[option.parentName] && option.parentValue && vm.data.options[option.parentName] === option.parentValue);*/ } function getColorPrice(color) { let prices = vm.data.product.properties.values; for (let i in prices) { let price = prices[i]; if (price.hasOwnProperty('color') && price.color.toLowerCase() == color.toLowerCase()) { return price.price - (price.discountPrice || 0); } } return 0; } function getOption(name) { let options = vm.data.product.properties.options; for (let i in options) { let opt = options[i]; if (opt.name.toLowerCase() === name.toLowerCase()) { return opt; } } return ''; } function isParentVisible(option) { if (!option.hasOwnProperty('hidden') || option.hidden == false) { // console.log(option); //option does not have parent // console.log('howdy!'); return true; } let options = vm.data.options; if (options[option.parentName] != option.parentValue) { //different parent selected // console.log('Different parent selected.'); return false; } let parent = getOption(option.parentName); if (!parent.hasOwnProperty('parentName')) { //not a case but to be sure return true; } //see if parent of parent is visible or not let visible = isParentVisible(parent); // console.log('parent visibility: ',visible); return visible; } function getPriceFromOptionValue(option, value) { let val = option.values; return val.reduce(function (sum, row) { if (row.title.toLowerCase() == value.toLowerCase() && row.hasOwnProperty('fixedPrice')) { sum += row.fixedPrice; } return sum; }, 0); } function getSelectedOptionPrice(value, option) { if (option.hasOwnProperty('hidden') && option.hidden != false) { //check if parent is visible if (vm.data.options[option.parentName] == option.parentValue && isParentVisible(option)) { let opt = angular.copy(option, {}); opt.hidden = false; return getSelectedOptionPrice(value, opt); } //if parent is not visible the price of the selected option should be 0 return 0; } if (!option.hasOwnProperty('values')) { return 0; } let prices = option.values; for (let i in prices) { let price = prices[i]; let title = price.hasOwnProperty('title') ? price.title.toLowerCase() : ''; if (angular.isObject(value)) { let p = 0; for (let k in value) { let l = value[k]; if (l == true) { let sum = getPriceFromOptionValue(option, k); p += (sum || price.fixedPrice || 0); } } return p; } else if (title == value.toLowerCase()) { return price.fixedPrice || 0; } } return 0; } function optionChanged(opt) { vm.data.price = vm.data.product.price; let sum = 0; for (let i in vm.data.options) { let v = vm.data.options[i]; if (i.toLowerCase() == 'color') { sum += getColorPrice(v); } else { //not really necessary, since the data is already present in the function argument //but just to be sure let option = getOption(i); if (option != '') { sum += getSelectedOptionPrice(v, option); } } } vm.data.price += sum; //dont want to mess up with variant. // i dont really know what variant is. //but we dont need this part for price calculation //so uncommented it var result = vm.data.product.properties.values; for (var i in vm.data.options) { if ( typeof vm.productOptionsMap[i] !== typeof undefined && ['combo', 'radio'].indexOf(vm.productOptionsMap[i].type) !== -1 && ( (!vm.productOptionsMap[i].parentName && !vm.productOptionsMap[i].parentValue) || (vm.data.options[vm.productOptionsMap[i].parentName] === vm.productOptionsMap[i].parentValue) ) ) { result = result.filter(function (e) { return e[i] === vm.data.options[i]; }); } } vm.data.variant = result[0]; /*var fixedPrice = 0; for (var i in vm.data.options) { if (vm.data.options[i]) { if (typeof vm.productOptionsMap[i] !== typeof undefined && vm.productOptionsMap[i].type === 'combo' && vm.productOptionsMap[i].values) { for (var j = 0; j < vm.productOptionsMap[i].values.length; j++) { if (vm.productOptionsMap[i].values[j].title !== vm.data.options[i]) { delete vm.data.options[vm.productOptionsMap[i].values[j].childOption]; } else if (vm.productOptionsMap[i].values[j].fixedPrice) { fixedPrice += vm.productOptionsMap[i].values[j].fixedPrice; } } } } }*/ /*vm.data.price = typeof vm.data.variant !== typeof undefined && typeof vm.data.variant.price !== typeof undefined ? vm.data.variant.price : vm.data.product.price; vm.data.price += fixedPrice;*/ } function imageAdded($file, $message, optionName) { if ($message) { var msg = angular.fromJson($message); if (msg.success) { $timeout(function () { if (!vm.data.options[optionName]) { vm.data.options[optionName] = []; } vm.data.options[optionName].push(msg.filename); $file.cancel(); }, 0); } } } function removeImage(idx, option) { vm.data.options[option.name].splice(idx, 1); } function fileUploadValidate($file) { var isValid = !!{png: 1, gif: 1, jpg: 1, jpeg: 1}[$file.getExtension()]; if (!isValid) { toastr.error('Please upload image in format PNG, JPEG or GIF.', 'Error'); } isValid = isValid && ($file.size <= 3 * 1024 * 1024); if (!isValid) { toastr.error('Uploaded image size must be less than 3MB.', 'Error'); } return isValid; } function addToCart(form) { vm.submitted = true; if (form.$invalid || (vm.data.product.colors.values.length && !vm.data.options.color)) { toastr.error('The highlighted properties of the product must be selected. Please select proper values for these properties to continue.', 'Error'); return false; } if (vm.data.product.croppingProperty && vm.data.options[vm.data.product.croppingProperty] && !vm.isCroppingMode) { vm.isCroppingMode = true; vm.picture = $('#hiddenCurrentCustomizeImageURL').val(); cropperInitialize(); } else { saveToCart(); } } vm.addHiddenProperties = function () { let props = vm.data.product.properties; if(typeof props === typeof undefined || !props.options){ return false; } for(let i in props.options){ let prop = props.options[i]; if(prop.type != 'hidden'){ continue; } vm.data.options[prop.name] = prop.value; } } function saveToCart() { vm.picture = $('#hiddenCurrentCustomizeImageURL').val(); vm.addHiddenProperties(); ShoppingCart.saveOne({ productID: vm.data.product.id, count: vm.data.count, totalPrice: vm.data.price, variant: (angular.isArray(vm.data.variant) ? vm.data.variant[0] : vm.data.variant) || {}, options: (angular.isArray(vm.data.options) ? vm.data.options[0] : vm.data.options) || {}, picture: vm.picture, croppingData: vm.croppingData, uguid: UGUID }) .then(function (res) { $('#modal_customize_item').modal('hide'); vm.data = { count: 1, options: {} }; vm.productOptionsMap = {}; if (vm.productCategories.length) { selectCategory(vm.productCategories[0]); } $rootScope.$emit('ShoppingCartRefreshed', true); var image = $('') $('body').append(image); var target = $('#dropdown_ddCart'); image.animate({ 'top': target.position().top + 10, 'left': target.offset().left + 100, 'width': 30, 'height': 30 }, 1000, 'easeInOutExpo'); setTimeout(function () { image.animate({ 'opacity': 0, height: 0, width: 0 }, 500, 'easeInOutExpo'); }, 1000); setTimeout(function () { target.effect('shake', {times: 2}, 200); }, 1500); }); } function closeCroppingMode() { vm.isCroppingMode = false; } function cropperInitialize() { $.fn.cropper.setDefaults({ checkCrossOrigin: false, scalable: false, zoomable: false, responsive: true }); optionChanged(); $('#image-crop-print').cropper('destroy'); $timeout(function () { $('#image-crop-print').cropper({ maximize: true, responsive: true, aspectRatio: vm.aspectRatio, crop: function (e) { // Output the result data for cropping image. vm.croppingData = { x: e.x, y: e.y, width: e.width, height: e.height }; var $preview = $('.preview-crop-print'); var imageData = $(this).cropper('getImageData'); var previewAspectRatio = e.width / e.height; var previewWidth = $preview.width(); var previewHeight = previewWidth / previewAspectRatio; var imageScaledRatio = e.width / previewWidth; $preview.height(previewHeight).find('img').css({ width: imageData.naturalWidth / imageScaledRatio, height: imageData.naturalHeight / imageScaledRatio, left: -e.x / imageScaledRatio, top: -e.y / imageScaledRatio }); } }); aspectRatioChanged(); }, 100); } function aspectRatioChanged() { var size = vm.data.options[vm.data.product.croppingProperty]; if (size) { var parts = []; if (size.indexOf('x') !== -1) { parts = size.split('x'); } else { parts = size.split('X'); } if (parts.length === 2) { var width = +parts[0]; var height = +parts[1]; vm.aspectRatio = width / height; $('#image-crop-print').cropper('setAspectRatio', vm.aspectRatio); } else { vm.data.options[vm.data.product.croppingProperty] = vm.productOptionsMap[vm.data.product.croppingProperty].values[0].croppingProperty; size = vm.data.options[vm.data.product.croppingProperty].toLowerCase().split('x'); if(size.length == 2){ var height = size.pop(); var width = size.pop(); vm.aspectRatio = width / height; $('#image-crop-print').cropper('setAspectRatio', vm.aspectRatio); } } } } } })();