File: D:/HostingSpaces/SBogers10/topswtw.komma.pro/wwwroot/js/app/checkout/checkoutCart.ng.js
define([
'app/checkout/productSessionService.ng',
'app/checkout/smallAfterCommaFilter.ng',
'app/checkout/couponCodeValidator.ng'
],
function (productSessionService, smallAfterCommaFilter, couponCodeValidator) {
'use strict';
var app = angular.module('checkoutCart', []);
app.factory('productSessionService', productSessionService);
app.filter('smallAfterCommaFilter', smallAfterCommaFilter);
app.directive('couponCodeValidator', couponCodeValidator);
app.run(['cart', 'cartItem', 'productSessionService', function (cart, cartItem, productSessionService) {
cart.init();
}]);
app.service('cart', ['cartItem', 'productSessionService', 'translationsService', function (cartItem, productSessionService, translationsService) {
this.init = function () {
this.activeCouponDiscount = null;
this.$cart = {
couponCode: '',
batchId: null,
shipping: null,
freeShipping: null,
items: [],
discounts: []
};
};
this.addItem = function (id, name, price, quantity, data) {
var item = this.getItemById(id);
if (item) {
item.setQuantity(quantity, false);
} else {
var newItem = new cartItem(id, name, price, quantity, data);
this.$cart.items.push(newItem);
}
};
this.setBatchId = function (id) {
this.batchId = id;
};
this.setCouponCode = function (code) {
this.couponCode = code;
};
this.addDiscount = function (discountRules) {
this.$cart.discounts.push(discountRules);
};
this.setError = function (type) {
this.removeSuccess()
switch (type) {
case 'coupon':
//load the element
var e = angular.element(document.querySelector('.error-coupon-holder .data'));
//set html
e.html(translationsService.get('cart.coupon_error'));
//show
e.parent().removeClass('hide');
break;
}
}
this.removeError = function (type) {
//load the element
var e = angular.element(document.querySelector('.error-coupon-holder'));
e.addClass('hide');
}
this.removeSuccess = function (type) {
return;
//load the element
var e = angular.element(document.querySelector('.messages .ajax.success'));
e.addClass('hide');
}
this.setSucces = function (type) {
//remove the error
this.removeError();
return;
switch (type) {
case 'coupon':
//load the element
var e = angular.element(document.querySelector('.messages .ajax.success span.data'));
//set html
e.html(translationsService.get('cart.coupon_success'));
//show
e.parent().removeClass('hide');
break;
}
}
this.getItemById = function (id) {
var items = this.getCart().items;
var itemToReturn = null;
angular.forEach(items, function (item) {
if (item.getId() === id) {
itemToReturn = item;
}
});
return itemToReturn;
};
this.setShipping = function (shipping) {
this.$cart.shipping = shipping;
return this.getShipping();
};
this.getShipping = function () {
if (this.getCart().items.length == 0) return 0;
if (this.orderHasNoShippingCosts()) return 0;
if (this.$cart.freeShipping != null && this.$cart.freeShipping <= this.getSubTotal()) return 0;
return this.getCart().shipping;
};
this.setFreeShipping = function (freeShipping) {
this.$cart.freeShipping = freeShipping;
return this.getShipping();
};
this.getFreeShipping = function () {
return this.$cart.freeShipping;
};
this.setCart = function (cart) {
this.$cart = cart;
return this.getCart();
};
this.getCart = function () {
return this.$cart;
};
this.getItems = function () {
return this.getCart().items;
};
this.getItemIds = function () {
var ids = [];
var items = this.getCart().items;
for (var i = 0; i < items.length; i++) {
ids.push(items[i].getId());
}
return ids;
};
this.getDiscountObjects = function () {
return this.getCart().discounts;
};
this.getTotalItems = function () {
var count = 0;
angular.forEach(this.getItems(), function (item) {
count += item.getQuantity();
});
return count;
};
this.getTotalUniqueItems = function () {
return this.getCart().items.length;
};
this.getTotalDiscountInPercentages = function () {
return ((1 - (this.getSubTotal() / this.getSubtotalWithoutDiscount())) * 100).toFixed(2);
};
this.getSubtotalWithoutDiscount = function () {
var total = 0;
angular.forEach(this.getItems(), function (item) {
total += item.getTotalWithoutDiscount();
});
return +parseFloat(total).toFixed(2);
};
this.getSubTotal = function () {
var total = 0;
angular.forEach(this.getItems(), function (item) {
total += item.getTotal();
});
return +parseFloat(total).toFixed(2);
};
this.totalCost = function () {
return +parseFloat(
(this.getSubTotal() + this.getShipping()) - this.getOrderDiscount()
).toFixed(2);
};
this.removeItem = function (index) {
var id = this.$cart.items[index].getId();
var $this = this;
productSessionService.remove(id, function () {
$this.$cart.items.splice(index, 1);
});
};
this.removeById = function (id) {
var $this = this;
angular.forEach(this.getItems(), function (item, index) {
if (item.getId() === id) {
$this.removeItem(index);
}
});
};
this.removeByIdWithEvent = function (id, $event) {
$event.preventDefault();
this.removeById(id);
};
this.empty = function () {
this.$cart.items = [];
};
this.getActiveDiscountObjects = function () {
var subTotal = this.getSubTotal();
var activeDiscountObjects = [];
var discountObjects = this.getDiscountObjects();
this.activeCouponDiscount = null;
for (var i in discountObjects) {
var discountObject = discountObjects[i];
if (
(discountObject.price_min <= subTotal || discountObject.price_min == null) &&
(discountObject.price_max >= subTotal || discountObject.price_max == null) &&
((discountObject.custom_coupon_code == null) || (discountObject.custom_coupon_code == this.couponCode)) &&
((discountObject.coupon_batch_id == null) || (discountObject.coupon_batch_id == this.batchId))
) {
activeDiscountObjects.push(discountObject);
// Check if coupon code is used
if (
((this.couponCode) && (discountObject.custom_coupon_code == this.couponCode)) ||
((this.batchId) && (discountObject.coupon_batch_id == this.batchId))
) {
this.activeCouponDiscount = discountObject;
}
}
}
return activeDiscountObjects;
};
this.getOrderDiscount = function () {
var price = this.getSubTotal();
var originalPrice = price;
var discounts = this.getActiveDiscountObjects();
// calculates discount in order of the sorted array (by priority)
for (var index in discounts) {
var discount = discounts[index];
if (discount.discount_percentage) {
price *= (1 - (discount.discount_percentage / 100));
}
if (discount.discount_absolute) {
price -= discount.discount_absolute;
}
if (discount.is_final == 1) {
break;
}
}
return +parseFloat(originalPrice - price).toFixed(2);
};
this.getOrderDiscountPercentage = function () {
var price = this.getSubTotal();
var originalPrice = price;
var discounts = this.getActiveDiscountObjects();
// calculates discount in order of the sorted array (by priority)
for (var index in discounts) {
var discount = discounts[index];
if (discount.discount_percentage) {
price *= (1 - (discount.discount_percentage / 100));
}
if (discount.discount_absolute) {
price -= discount.discount_absolute;
}
if (discount.is_final == 1) {
break;
}
}
if (price == originalPrice) return 0;
return +parseFloat(100 / (originalPrice / (originalPrice - price))).toFixed(2);
};
this.orderHasNoShippingCosts = function () {
var products = this.getItems();
for (var index in products) {
var product = products[index];
if (product.hasNoShippingCosts()) return true;
}
var discounts = this.getActiveDiscountObjects();
// calculates discount in order of the sorted array (by priority)
for (var index in discounts) {
var discount = discounts[index];
if (discount.discount_no_shipping_costs == 1) return true;
}
return false;
};
this.toObject = function () {
if (this.getItems().length === 0) return false;
var items = [];
angular.forEach(this.getItems(), function (item) {
items.push(item.toObject());
});
return {
shipping: this.getShipping(),
subTotal: this.getSubTotal(),
totalCosts: this.totalCost(),
items: items
}
};
}]);
app.factory('cartItem', ['productSessionService', 'translationsService', function (productSessionService, translationsService) {
var item = function (id, name, price, quantity, data) {
this.activeCouponDiscount = null;
this.setId(id);
this.setName(name);
this.setPrice(price);
this.setQuantity(quantity);
this.setData(data);
this._couponCode = '';
};
item.prototype.setId = function (id) {
if (id) this._id = id;
};
item.prototype.getId = function () {
return this._id;
};
item.prototype.setName = function (name) {
if (name) this._name = name;
};
item.prototype.getName = function () {
return this._name;
};
item.prototype.setBatchId = function (id) {
this._batchId = id;
};
item.prototype.setCouponCode = function (code) {
if (code) this._couponCode = code;
};
item.prototype.setPrice = function (price) {
var priceFloat = parseFloat(price);
if (priceFloat && priceFloat > 0) {
this._price = priceFloat;
}
};
item.prototype.getPrice = function () {
return this._price;
};
item.prototype.setQuantity = function (quantity, relative) {
var $this = this;
var quantityInt = parseInt(quantity);
var newQuantity = 1;
if (quantityInt % 1 === 0) {
if (relative) {
newQuantity = parseInt($this._quantity) + quantityInt;
} else {
newQuantity = quantityInt;
}
if (newQuantity < 1) newQuantity = 1;
}
productSessionService.set(this.getId(), newQuantity, function (data) {
$this._quantity = newQuantity;
});
};
item.prototype.setQuantityWithEvent = function (quantity, relative, $event) {
if(quantity == null) return false;
$event.preventDefault();
this.setQuantity(quantity, relative);
};
item.prototype.setQuantityOnChange = function (quantity, relative) {
if(!quantity) return '';
this.setQuantity(quantity, relative);
};
item.prototype.getQuantity = function () {
return parseInt(this._quantity);
};
item.prototype.setData = function (data) {
if (data) this._data = data;
};
item.prototype.getData = function () {
if (this._data) return this._data;
};
item.prototype.getImage = function () {
if (this.getData() &&
this.getData().images &&
this.getData().images[0] &&
this.getData().images[0].small_image_url) {
return this.getData().images[0].small_image_url;
}
//if(this.getData().images[0].small_image_url) return this.getData().images[0].small_image_url;
return '/images/structure/noImage.jpg';
};
item.prototype.getTotalWithoutDiscount = function () {
return +parseFloat(this.getQuantity() * this.getPrice()).toFixed(2);
};
item.prototype.getTotal = function () {
return +parseFloat((this.getQuantity() * this.getPrice()) - this.getDiscount()).toFixed(2);
};
item.prototype.toObject = function () {
return {
id: this.getId(),
name: this.getName(),
price: this.getPrice(),
quantity: this.getQuantity(),
data: this.getData(),
total: this.getTotal()
};
};
item.prototype.hasNoShippingCosts = function () {
var discounts = this.getActiveDiscountObjects();
// calculates discount in order of the sorted array (by priority)
for (var index in discounts) {
var discount = discounts[index];
if (discount.discount_no_shipping_costs) {
return true;
}
if (discount.is_final == 1) {
break;
}
}
return false;
};
item.prototype.getDiscount = function () {
var price = this.getTotalWithoutDiscount();
var originalPrice = price;
var discounts = this.getActiveDiscountObjects();
// calculates discount in order of the sorted array (by priority)
for (var index in discounts) {
var discount = discounts[index];
if (discount.discount_percentage) {
price *= (1 - (discount.discount_percentage / 100));
}
/*
if(discount.discount_fraction){
price -= (discount.discount_fraction * this.getPrice());
}
*/
if (discount.discount_absolute) {
price -= discount.discount_absolute;
}
if (discount.is_final == 1) {
break;
}
}
return +parseFloat(originalPrice - price).toFixed(2);
};
// Tops only
item.prototype.getPercentageDiscountsOnly = function () {
var calc = 100;
var discounts = this.getActiveDiscountObjects();
for (var index in discounts) {
var discount = discounts[index];
if (discount.discount_percentage) {
calc *= (1 - (discount.discount_percentage / 100));
}
}
return (100 - calc) + '%';
};
// Tops only
item.prototype.getFractionDiscountsOnly = function () {
var forFree = 0;
var discounts = this.getActiveDiscountObjects();
//Tops only
forFree = Math.floor((this._quantity) / 10); //TODO This was enabled in the past. But when you enable this, things like free products for coupon codes arent visible anymore.
for (var index in discounts) {
var discount = discounts[index];
if (discount.custom_coupon_code !== null) {
forFree += discount.discount_fraction;
}
}
if (!forFree) return;
var unit = '';
if (this._data.is_filter_set == 1) {
unit = translationsService.get('cart.set' + (forFree > 1 ? 's' : ''));
} else if (this._data.is_filter_set == 0) {
unit = translationsService.get('cart.filter' + (forFree > 1 ? 's' : ''));
} else {
unit = translationsService.get('cart.piece' + (forFree > 1 ? 's' : ''));
}
return "+" + forFree + " " + unit + " " + translationsService.get('cart.for_free');
};
item.prototype.getAbsoluteDiscountsOnly = function () {
var absolute = 0;
var discounts = this.getActiveDiscountObjects();
for (var index in discounts) {
var discount = discounts[index];
if (discount.discount_absolute) {
absolute += discount.discount_absolute;
}
}
if (!absolute) return;
return (absolute / 100);
};
// Tops only
item.prototype.getActiveDiscountObjects = function () {
if (!this._data.discounts) return;
var activeDiscounts = [];
this.activeCouponDiscount = null;
for (var i in this._data.discounts) {
var discount = this._data.discounts[i];
if (
(this.getQuantity() >= (discount.quantity_min ? discount.quantity_min : 0)) &&
(this.getQuantity() <= (discount.quantity_max ? discount.quantity_max : 999999)) &&
((!discount.custom_coupon_code) || (discount.custom_coupon_code == this._couponCode)) &&
((!discount.coupon_batch_id) || (discount.coupon_batch_id == this._batchId))
) {
activeDiscounts.push(discount);
// Check if coupon code is used
if (
((this._couponCode) && (discount.custom_coupon_code == this._couponCode)) ||
((this._batchId) && discount.coupon_batch_id == this._batchId)
) {
this.activeCouponDiscount = discount;
}
if (discount.is_final == 1) break;
}
}
activeDiscounts.sort(function (a, b) {
a = parseInt(a.priority);
b = parseInt(b.priority);
return a - b;
});
return activeDiscounts;
};
item.prototype.getDiscountsForCartBar = function () {
if (!this._data.discounts) return;
var activeDiscounts = [];
for (var i in this._data.discounts) {
var discount = this._data.discounts[i];
if (
(discount.quantity_min) &&
(!discount.custom_coupon_code) &&
(!discount.coupon_batch_id) &&
(discount.in_carrousel == 1)
) {
activeDiscounts.push(discount);
}
}
activeDiscounts.sort(function (a, b) {
// Not tested...
a = parseInt(a.priority);
b = parseInt(b.priority);
return a - b;
});
return activeDiscounts;
};
return item;
}]);
app.controller('cartController', ['$scope', 'cart', 'productSessionService', 'translationsService', function ($scope, cart, productSessionService, translationsService) {
var cartModel = this.cartModel = cart;
$scope.couponCode = '';
$scope.activeCouponDiscount = null;
$scope.initCartWithBaseUrl = function (cartBaseUrl, country_iso) {
productSessionService.setBaseUrl(cartBaseUrl);
productSessionService.load()
.then(function (result) {
angular.forEach(result.data.products, function (record) {
cart.addItem(
record.data.id,
record.data.name,
record.data.price,
record.quantity,
record.data
)
});
angular.forEach(result.data.discounts, function (record) {
cart.addDiscount(record)
});
cart.setShipping(result.data.shippingCosts[country_iso].shipping_costs);
cart.setFreeShipping(result.data.shippingCosts[country_iso].free_shipping)
});
};
$scope.$watch('couponCode', function () {
//if field is empty do nothing
if ($scope.couponCode == '') return;
//get the products
var products = cart.getItems();
//loop trough the products
for (var i in products) {
//set the hex_sah1 from the coupon product, if it is the same return code??
products[i].setCouponCode(hex_sha1($scope.couponCode));
}
cart.setCouponCode($scope.couponCode); // Todo: also hex_sha1
var recently_added = false;
if (cart.activeCouponDiscount && cart.activeCouponDiscount.couponCode == $scope.couponCode) {
cart.setSucces('coupon');
recently_added = true;
}
productSessionService.checkCode($scope.couponCode, function (data) {
if (!data) {
cart.setError('coupon');
return;
}
var activeCouponDiscount = null;
var batchId = $.parseJSON(data);
for (var i in products) {
products[i].setBatchId(batchId)
}
cart.setBatchId(batchId);
for (var i in products) {
// Check if one of the products as an activeCouponDiscount
if (products[i].activeCouponDiscount) {
activeCouponDiscount = products[i].activeCouponDiscount;
}
}
$scope.activeCouponDiscount = cart.activeCouponDiscount ? cart.activeCouponDiscount : activeCouponDiscount;
if ($scope.activeCouponDiscount == null && recently_added == false) {
cart.setError('coupon');
}
else {
cart.setSucces('coupon');
}
setTimeout(function () {
productSessionService.reCheckCoupon(products)
}, 1, products)
});
});
productSessionService.reCheckCoupon = function (products) {
var activeCouponDiscount = null;
for (var i in products) {
// Check if one of the products as an activeCouponDiscount
if (products[i].activeCouponDiscount) {
activeCouponDiscount = products[i].activeCouponDiscount;
}
}
$scope.activeCouponDiscount = cart.activeCouponDiscount ? cart.activeCouponDiscount : activeCouponDiscount;
if ($scope.activeCouponDiscount == null) {
cart.setError('coupon');
}
else {
cart.setSucces('coupon');
}
}
this.getTotalQuantity = function () {
if (cart.getTotalItems())
return cart.getTotalItems();
return 0;
};
this.getProducts = function () {
return cart.getItems();
};
// Callback for update methods
var updateCallback = function (quantity) {
if (quantity == 0) {
delete $scope.products[$scope.product.data.id];
}
else {
$scope.products[$scope.product.data.id].quantity = parseInt(quantity);
}
};
}
])
;
return app;
})
;