File: D:/HostingSpaces/SBogers10/topswtwmobile.komma.pro/wwwroot/js/app/checkout/checkout.ng.js
define(['app/checkout/hiddenBox'], function ($hiddenBox) {
var app = angular.module('checkout', []);
app.factory('Api', ['$http', function ($http) {
return {
getShippingCosts: function (countryIso2, callback) {
return $http.get('/api/shipping-costs/' + countryIso2).success(callback);
}
}
}]);
app.controller('CheckoutCtrl', ['$scope', 'Api', 'translationsService', function ($scope, Api, translationsService) {
$scope.shippingType = null;
//set shippingCostst on load
$scope.setShippingCosts = function () {
var shippingCountry = ($scope.shippingType == 'add-shipping-address') ?
$scope.shippingCountry : $scope.invoiceCountry;
Api.getShippingCosts(shippingCountry, function (data) {
var country = data.shippingCosts.country;
if (typeof country !== typeof undefined && country !== false) {
$scope.shippingCountryExtra = translationsService.get('countries.' + country.iso_2);
$scope.freeShippingCountry = data.shippingCosts.free_shipping;
$('.shippingCost-summary .sub-text').show();
}
else{
$('.shippingCost-summary .sub-text').hide();
}
if (data.shippingCosts.shipping_costs == 0) {
$scope.shippingCosts = 0;
$scope.shippingCostsEx = 0;
return;
}
if (data.shippingCosts.free_shipping && $scope.subTotal >= data.shippingCosts.free_shipping) {
$scope.shippingCosts = 0;
$scope.shippingCostsEx = 0;
} else {
$scope.shippingCosts = data.shippingCosts.shipping_costs;
$scope.shippingCostsEx = $scope.shippingCosts / (1 + ($scope.highestVatPercentage / 100));
}
});
};
$scope.getTotalCosts = function () {
return Number($scope.subTotal) - Number($scope.discount) + Number($scope.shippingCosts);
};
$scope.getTotalCostsEx = function () {
return Number($scope.subTotalEx) - Number($scope.discountEx) + Number($scope.shippingCostsEx);
};
}]);
app.directive('vatValidation', ['$http', function ($http) {
return {
require: 'ngModel',
link: function (scope, ele, attrs, c) {
scope.$watch(attrs.ngModel, function (value) {
var invoiceCountry = scope.invoiceCountry
var vatPattern = new RegExp("^((AT)U[0-9]{8}|(BE)0[0-9]{9}|(BG)[0-9]{9,10}|(CY)[0-9]{8}L|(CZ)[0-9]{8,10}|(DE)[0-9]{9}|(DK)[0-9]{8}|(EE)[0-9]{9}|(EL|GR)[0-9]{9}|(ES)[0-9A-Z][0-9]{7}[0-9A-Z]|(FI)[0-9]{8}|(FR)[0-9A-Z]{2}[0-9]{9}|(GB)([0-9]{9}([0-9]{3})|[A-Z]{2}[0-9]{3})|(HU)[0-9]{8}|(IE)[0-9]S[0-9]{5}L|(IT)[0-9]{11}|(LT)([0-9]{9}|[0-9]{12})|(LU)[0-9]{8}|(LV)[0-9]{11}|(MT)[0-9]{8}|(NL)[0-9]{9}B[0-9]{2}|(PL)[0-9]{10}|(PT)[0-9]{9}|(RO)[0-9]{2,10}|(SE)[0-9]{12}|(SI)[0-9]{8}|(SK)[0-9]{10})$");
if (vatPattern.exec(value)) {
// Todo: Check trough "VIES api"
$http({
method: 'GET',
url: '/check-vat/' + value + '?invoice_country=' + invoiceCountry
}).success(function (response) {
if (response.valid == 1) {
return c.$setValidity('vat', true);
}
return c.$setValidity('vat', false);
}).error(function (response) {
return c.$setValidity('vat', false);
});
}
return c.$setValidity('vat', false);
});
}
}
}]);
return app;
});