angular .module('EssayApp.controllers') .controller('PricesController', ['$scope', '$window', '$sce', 'dataService', 'orderPriceService', 'RestorePasswordService', 'writerService', '$timeout', 'ngProgress', 'utils', 'I18n', ($scope, $window, $sce, dataService, orderPriceService, restorePasswordService, writerService, $timeout, ngProgress, utils, I18n) -> ngProgress.start() $scope.ready = false $scope.forms = {} $scope._currency = undefined $scope.customer = features: preffered_writer: false writer_level: undefined extras: [] $scope.services = [] $scope.src_languages = [] $scope.dst_languages = [] $scope.countries = [] $scope.currencies = [] $scope.urgencies = [] $scope.data = is_words_num_available: false is_restore_pass_state: false # DEV ONLY $window.prs = $scope dataService.get_rates().then (responce) -> $scope.price = orderPriceService.build() $scope.price.data = responce.data $scope.services = $scope.price.getAvailableServices() $scope.certificates = $scope.price.getAvailableCertificates() $scope.customer.service = $scope.findOrUseDefault(undefined, $scope.services) $scope.price.setService($scope.customer.service) $scope.countries = $scope.price.data.countries $scope.payment_types = $scope.price.data.payment_types $scope.industries = $scope.price.getAvailableIndustries() switch $scope.price.theme when $scope.price.THEMES.CERTIFIED $scope.customer.industry = $scope.findOrUseDefault(undefined, $scope.industries) $scope.customer.certificate = $scope.findOrUseDefault(undefined, $scope.certificates) $scope.price.setCertificate($scope.customer.certificate) else $scope.customer.industry = $scope.findOrUseDefault(undefined, $scope.industries) $scope.price.setIndustry($scope.customer.industry) $scope.packages = $scope.price.data.packages $scope.customer.package = $scope.findOrUseDefault(undefined, $scope.packages) $scope.price.setPackage($scope.customer.package) $scope.currencies = $scope.price.data.rates.currencies $scope.setupCurrency() # features $scope.features = extras: $scope.price.getExtrasFeatures() is_active: (feature)-> return false unless feature?.id? $scope.price?.selected_features?[feature.id]? $scope.urgencies = $scope.price.getAvailableUrgencies() $timeout $scope.setupUrgency, 1, false if $scope.price.queryParams['extras']? $scope.customer.features.extras = $scope.price.readParam('extras', $scope.features.extras, undefined, $scope.price.PARAMS.ARRAY) $scope.customer.features.extras.forEach (feature)-> $scope.price?.toggleFeature(feature.feature_type_id, feature) ngProgress.complete() , (err) -> console.log 'Error', err ngProgress.complete() $scope.orderParams = () -> if $scope.price?.clear_params? && $scope.customer? "?" + $.param($scope.price.clear_params($scope.price.SERIALIZE.MINI, $scope.customer)) $scope.setupCurrency = (forced = undefined)-> if (forced? || $scope._currency?) && $scope.currencies.length for curr in $scope.currencies $scope.customer.currency = curr if curr.currency is (forced || $scope._currency) $scope.customer.currency ||= $scope.findOrUseDefault(undefined, $scope.currencies) $scope.init = (options) -> if options.country_code? _customerCountry = options.country_code if options.key? _serializeKey = options.key if options.customermode? $scope.price.setCustomerMode(options.customermode) if options.currency? $scope._currency = options.currency if options.lifetime_discount? $scope.customer.lifetime_discount = parseFloat(options.lifetime_discount) return false $scope.equals = (obj1, obj2)-> obj1?.id? && obj2?.id? && obj1.id == obj2.id $scope.findOrUseDefault = (id, array, default_value = undefined)-> unless angular.isUndefined(id) for item in array # return founded item return item if id == item?.id # item not found, return default if angular.isUndefined(default_value) # return first if no default_value defined array[0] else default_value $scope.setupLanguageRate = -> _rate = null if (_base_price = $scope.price?.data?.settings?.base_price) > 0 for rate in $scope.price.data.rates.languages if rate.price is _base_price _rate = rate break if !_rate? for rate in $scope.price.data.rates.languages if rate.price > 0 _rate = rate break $scope.price.logic.setLanguageRates([_rate]) $scope.setupUrgency = (force_reset = false)-> $scope.urgencies = $scope.price.getAvailableUrgencies() $scope.customer.urgency = $scope.findOrUseDefault( if force_reset then $scope.customer.service.urgency_type_id else $scope.customer.urgency?.id, $scope.urgencies, $scope.price.findMaxAvailableUrgency() ) $scope.price.setUrgency($scope.customer.urgency) $scope.$watch 'customer.service', (service) -> if service? _theme = $scope.price.theme $scope.price.setService(service) $scope.industries = $scope.price.getAvailableIndustries() $scope.features.extras = $scope.price.getExtrasFeatures() switch $scope.price.theme when $scope.price.THEMES.CERTIFIED # setup certificate $scope.customer.certificate = $scope.findOrUseDefault($scope.customer.certificate?.id, $scope.certificates) $scope.price.setCertificate($scope.customer.certificate) $scope.customer.industry = $scope.findOrUseDefault($scope.customer.industry?.id, $scope.industries) $scope.price.setIndustry($scope.customer.industry) else # clear certificate $scope.customer.certificate = undefined $scope.customer.other_certificate = undefined $scope.customer.industry = $scope.findOrUseDefault($scope.customer.industry?.id, $scope.industries) $scope.price.setIndustry($scope.customer.industry) $scope.setupLanguageRate() $scope.setupUrgency(true) $scope.$watch 'customer.industry', (industry) -> if industry? $scope.price.setIndustry(industry) $scope.setupLanguageRate() $scope.setupUrgency() $scope.$watch 'customer.certificate', (certificate) -> $scope.price?.setCertificate($scope.customer.certificate) $scope.customer.other_certificate = undefined $scope.$watch 'customer.package', (pkg) -> if pkg? $scope.price.setPackage(pkg) $scope.$watch 'customer.urgency', (urgency) -> if urgency? $scope.price.setUrgency(urgency) $scope.$watch 'customer.features.writer_level', (writer_level) -> $scope.price?.toggleFeature($scope.features?.writer_levels?[0]?.feature_type_id, writer_level) false $scope.$watch 'customer.currency', (currency)-> $scope.price?.setCurrency(currency) $scope.$watch '_currency', (currency)-> $scope.setupCurrency(currency) return $scope ])