Advertisement
Guest User

Untitled

a guest
Aug 1st, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 32.96 KB | None | 0 0
  1. function isEmpty(value) {
  2. return angular.isUndefined(value) || "" === value || null === value || value !== value
  3. }
  4. var app = angular.module("quickShip", ["ngRoute", "ngResource", "ngStorage", "ngAnimate", "ui.bootstrap", "toggle-switch", "ngSanitize", "ngTagsInput", "ngDialog"]);
  5. angular.module("quickShip").config(["$routeProvider", "$httpProvider", "$anchorScrollProvider", "tagsInputConfigProvider", "$locationProvider", function($routeProvider, $httpProvider, $anchorScrollProvider, tagsInputConfigProvider, $locationProvider) {
  6. $routeProvider.when("/quickship", {
  7. title: "Personal information",
  8. templateUrl: window.Configure.base_url + "FRONTEND/js/modules/personal.html",
  9. controller: "PersonalCtrl"
  10. }).when("/new-shipment", {
  11. title: "Personal information",
  12. templateUrl: window.Configure.base_url + "FRONTEND/js/modules/personal.html",
  13. controller: "PersonalCtrl"
  14. }).when("/new-shipment/:param", {
  15. title: "Personal information",
  16. templateUrl: window.Configure.base_url + "FRONTEND/js/modules/personal.html",
  17. controller: "PersonalCtrl"
  18. }).when("/additional-options", {
  19. title: "Additional Options",
  20. templateUrl: window.Configure.base_url + "FRONTEND/js/modules/additional_options.html",
  21. controller: "AdditionalOptionsCtrl"
  22. }).when("/package-details", {
  23. title: "Package Details",
  24. templateUrl: window.Configure.base_url + "FRONTEND/js/modules/package_details.html",
  25. controller: "PackageDetailsCtrl"
  26. }).when("/shipment-rates", {
  27. title: "Shipment Rates",
  28. templateUrl: window.Configure.base_url + "FRONTEND/js/modules/shipment_rates.html",
  29. controller: "ShipmentRatesCtrl"
  30. }).when("/payment-options", {
  31. title: "Payment Options",
  32. templateUrl: window.Configure.base_url + "FRONTEND/js/modules/payment_options.html",
  33. controller: "PaymentOptionsCtrl"
  34. }).otherwise({
  35. redirectTo: "/quickship"
  36. }), $httpProvider.defaults.cache = !1, $httpProvider.defaults.headers.common["X-From-Angularjs"] = !0, $anchorScrollProvider.disableAutoScrolling(), tagsInputConfigProvider.setDefaults("tagsInput", {
  37. minLength: 1
  38. }), $locationProvider.html5Mode(!0)
  39. }]), angular.module("quickShip").controller("MainCtrl", ["$scope", "$rootScope", "$window", "QuickshipSvc", function($scope, $rootScope, $window, QuickshipSvc) {
  40. $scope.title = "MainCtrl", $scope.Configure = $window.Configure, $scope.quickship = QuickshipSvc, $rootScope.firstLoad = !0, $scope.init = function() {
  41. $scope.quickship.configure = $scope.Configure, $rootScope.firstLoad, $scope.quickship.loadSettings(), $rootScope.firstLoad = !1
  42. }, $scope.init()
  43. }]), angular.module("quickShip").controller("PersonalCtrl", ["$scope", "$location", "$window", function($scope, $location, $window) {
  44. $scope.title = "PersonalCtrl", $scope.$parent.quickship.currentStep = 1, $scope.goNext = function(theForm) {
  45. return theForm.$valid === !1 ? ($scope.$parent.quickship.validatesPersonalDetails = !1, !1) : ($scope.$parent.quickship.validatesPersonalDetails = !0, void $scope.$parent.quickship.updatePersonalInfo().then(function(res) {
  46. return $scope.$parent.quickship.$storage.personalDetailsCompleted = !0, $location.path("/additional-options")
  47. }, function(err) {
  48. alert("Error occurred: " + err)
  49. }))
  50. }, $scope.init = function() {
  51. $window.scrollTo(0, 0)
  52. }, $scope.init()
  53. }]), angular.module("quickShip").controller("AdditionalOptionsCtrl", ["$scope", "$location", "$window", function($scope, $location, $window) {
  54. $scope.title = "AdditionalOptionsCtrl", $scope.$parent.quickship.currentStep = 2, $scope.goPrev = function() {
  55. return $location.path("/")
  56. }, $scope.goNext = function(simpleSteps) {
  57. return $scope.$parent.quickship.$storage.simpleSteps = simpleSteps, $scope.$parent.quickship.$storage.quickship.amount = 1, $scope.$parent.quickship.$storage.quickship.packages = [{
  58. tracking_code: "",
  59. carrier: "",
  60. consolidated: [],
  61. weight_unit: "lb",
  62. size_unit: "in",
  63. items: [{
  64. description: "",
  65. weight_unit: "lb",
  66. price_value: null,
  67. info: ""
  68. }]
  69. }], $location.path("/package-details")
  70. }, $scope.init = function() {
  71. return null === $scope.$parent.quickship.$storage.simpleSteps ? ($location.path("/"), !0) : void $window.scrollTo(0, 0)
  72. }, $scope.init()
  73. }]), angular.module("quickShip").controller("PackageDetailsCtrl", ["$scope", "$location", "$window", "ngDialog", function($scope, $location, $window, ngDialog) {
  74. $scope.title = "PackageDetailsCtrl", $scope.$parent.quickship.currentStep = 3, $scope.countriesOfOrigin = [], $scope.activeDetailTab = 0, $scope.validationStarted = !1, $scope.getWeightTitle = function() {
  75. return $scope.$parent.quickship.consolidatePackages === !0 ? "Total Weight *" : "Weight *"
  76. }, $scope.goNext = function(frm) {
  77. if ($scope.validationStarted = !0, frm.$valid === !1) return $scope.$parent.quickship.validatesPackageDetails = !1, !1;
  78. if ($scope.validateWeights() === !1) return $scope.$parent.quickship.validatesPackageDetails = !1, !1;
  79. if ($scope.$parent.quickship.validatesPackageDetails = !0, $scope.$parent.quickship.$storage.simpleSteps)
  80. if ($scope.$parent.quickship.$storage.quickship.consolidatePackages) {
  81. var newPackagesNum, currentPackagesNum = $scope.$parent.quickship.$storage.quickship.packages.length;
  82. newPackagesNum = currentPackagesNum % 5 == 0 ? currentPackagesNum / 5 : Math.floor(currentPackagesNum / 5) + 1;
  83. for (var newPackages = [], smallPackageCount = 0, i = 0; i < newPackagesNum; i++) {
  84. for (var consolidated = [], items = [], weight = 0, j = 1; j <= 5 && (consolidated.push({
  85. tracking_code: $scope.$parent.quickship.$storage.quickship.packages[smallPackageCount].tracking_code,
  86. carrier: $scope.$parent.quickship.$storage.quickship.packages[smallPackageCount].carrier
  87. }), items = items.concat($scope.$parent.quickship.$storage.quickship.packages[smallPackageCount].items), weight += 1, smallPackageCount++, smallPackageCount != currentPackagesNum); j++);
  88. newPackages.push({
  89. weight_unit: "lb",
  90. size_unit: "in",
  91. weight: weight,
  92. length: 24,
  93. width: 14,
  94. height: 12,
  95. tracking_code: "",
  96. consolidated: [],
  97. items: []
  98. }), newPackages[i].consolidated = consolidated, newPackages[i].items = items
  99. }
  100. $scope.$parent.quickship.$storage.quickship.small_packages = $scope.$parent.quickship.$storage.quickship.packages, $scope.$parent.quickship.$storage.quickship.packages = newPackages
  101. } else angular.forEach($scope.$parent.quickship.$storage.quickship.packages, function(item) {
  102. item.consolidated = [], item.weight_unit = "lb", item.size_unit = "in", item.weight = 1, item.width = 6, item.height = 6, item.length = 6
  103. });
  104. else $scope.$parent.quickship.$storage.quickship.consolidatePackages && angular.forEach($scope.$parent.quickship.$storage.quickship.packages, function(pkg) {
  105. pkg.size_unit = "in", pkg.length = 24, pkg.width = 14, pkg.height = 12
  106. });
  107. return $location.path("/shipment-rates")
  108. }, $scope.goBack = function() {
  109. return $location.path("/additional-options")
  110. }, $scope.hasValidWeight = function(form, index) {
  111. return !!$scope.$parent.quickship.$storage.quickship.consolidatePackages || ($scope.validationStarted === !1 || !($scope.$parent.quickship.$storage.quickship.packages[index].weight < .01) && (!form["package_" + index + "_weight"].$error.required && void 0))
  112. }, $scope.validateWeights = function() {
  113. var is_valid = !0;
  114. return $scope.$parent.quickship.$storage.simpleSteps === !0 || (!!$scope.$parent.quickship.$storage.quickship.consolidatePackages || (angular.forEach($scope.$parent.quickship.$storage.quickship.packages, function(item) {
  115. if (parseFloat(item.weight) < .01) return !1
  116. }), is_valid))
  117. }, $scope.deleteItem = function(package_index, index) {
  118. $scope.$parent.quickship.$storage.quickship.packages[package_index].items.splice(index, 1)
  119. }, $scope.addItem = function(index) {
  120. $scope.$parent.quickship.$storage.quickship.packages[index].items.push({
  121. description: "",
  122. weight_unit: "lb",
  123. price_value: null
  124. })
  125. }, $scope.addPackage = function() {
  126. return $scope.$parent.quickship.addPackage()
  127. }, $scope.updateSimplePackages = function() {
  128. $scope.$parent.quickship.updateSimplePackages(), $scope.activeDetailTab = 0
  129. }, $scope.deletePackage = function(package_index) {
  130. $scope.$parent.quickship.$storage.quickship.packages.splice(package_index, 1), $scope.activeDetailTab = 0
  131. }, $scope.setActiveTab = function(tab_index) {
  132. $scope.activeDetailTab = tab_index
  133. }, $scope.hasInvalidTabData = function(package_index, form) {
  134. for (var count_items = $scope.$parent.quickship.$storage.quickship.packages[package_index].items.length, hasErrors = !1, i = 0; i < count_items; i++) "undefined" != typeof form["packages_" + package_index + "_items_" + i + "_country_id"] && form["packages_" + package_index + "_items_" + i + "_country_id"].$invalid === !0 && (hasErrors = !0), "undefined" != typeof form["packages_" + package_index + "_items_" + i + "_description"] && form["packages_" + package_index + "_items_" + i + "_description"].$invalid === !0 && (hasErrors = !0), "undefined" != typeof form["packages_" + package_index + "_items_" + i + "_quantity"] && form["packages_" + package_index + "_items_" + i + "_quantity"].$invalid === !0 && (hasErrors = !0), "undefined" != typeof form["packages_" + package_index + "_items_" + i + "_type_id"] && form["packages_" + package_index + "_items_" + i + "_type_id"].$invalid === !0 && (hasErrors = !0), "undefined" != typeof form["packages_" + package_index + "_items_" + i + "_value"] && form["packages_" + package_index + "_items_" + i + "_value"].$invalid === !0 && (hasErrors = !0), "undefined" != typeof form["packages_" + package_index + "_items_" + i + "_weight"] && form["packages_" + package_index + "_items_" + i + "_weight"].$invalid === !0 && (hasErrors = !0), "undefined" != typeof form["packages_" + package_index + "_items_" + i + "_weight_unit"] && form["packages_" + package_index + "_items_" + i + "_weight_unit"].$invalid === !0 && (hasErrors = !0);
  135. return hasErrors
  136. }, $scope.addConsolidatedPackage = function(package_index) {
  137. return $scope.$parent.quickship.addConsolidatedPackage(package_index)
  138. }, $scope.deleteConsolidatedPackage = function(package_index, index) {
  139. $scope.$parent.quickship.$storage.quickship.packages[package_index].consolidated.splice(index, 1), $scope.updateWeight(package_index)
  140. }, $scope.updateWeight = function(package_index) {
  141. var sum_weight = 0,
  142. sum_weight_unit = $scope.$parent.quickship.$storage.quickship.packages[package_index].weight_unit;
  143. angular.forEach($scope.$parent.quickship.$storage.quickship.packages[package_index].consolidated, function(value, index) {
  144. parseFloat(value.weight) && (sum_weight += $scope.$parent.quickship.calculateWeight(value.weight, value.unit, sum_weight_unit))
  145. }), $scope.$parent.quickship.$storage.quickship.packages[package_index].weight = sum_weight
  146. }, $scope.init = function() {
  147. return $scope.$parent.quickship.$storage.personalDetailsCompleted === !1 ? $location.path("/") : ("undefined" != typeof $scope.$parent.quickship.$storage.countries && null !== $scope.$parent.quickship.$storage.countries || $scope.$parent.quickship.loadSettings(), $scope.updateSimplePackages(), void $window.scrollTo(0, 0))
  148. }, $scope.init()
  149. }]), angular.module("quickShip").controller("ShipmentRatesCtrl", ["$scope", "$location", "$window", function($scope, $location, $window) {
  150. $scope.title = "ShipmentRatesCtrl", $scope.currentRateGroup = 0, $scope.orderBy = null, $scope.orderByReverse = !1, $scope.$parent.quickship.currentStep = 4, $scope.loadingRates = !1, $scope.oneAtTime = !1, $scope.isFirstOpen = !0, $scope.loadRates = function() {
  151. return null === $scope.$parent.quickship.$storage.quickship.packages[0].items[0].price_value ? $location.path("/") : ($scope.loadingRates = !0, void $scope.$parent.quickship.loadRates().then(function() {
  152. $scope.loadingRates = !1
  153. }, function() {
  154. return alert("ERROR: Unable to get shipping rates. Try again in a few seconds"), $scope.loadingRates = !1, $location.path("/package-details")
  155. }))
  156. }, $scope.goNext = function() {
  157. return $location.path("/payment-options")
  158. }, $scope.goBack = function() {
  159. return $scope.$parent.quickship.$storage.simpleSteps && $scope.$parent.quickship.$storage.quickship.consolidatePackages && ($scope.$parent.quickship.$storage.quickship.packages = $scope.$parent.quickship.$storage.quickship.small_packages), $location.path("/package-details")
  160. }, $scope.init = function() {
  161. return "undefined" == typeof $scope.$parent.quickship.$storage.billing.email || null === $scope.$parent.quickship.$storage.billing.email ? $location.path("/") : ($scope.loadRates(), void $window.scrollTo(0, 0))
  162. }, $scope.init()
  163. }]), angular.module("quickShip").controller("PaymentOptionsCtrl", ["$scope", "$location", "$window", "ngDialog", function($scope, $location, $window, ngDialog) {
  164. $scope.title = "PaymentOptionsCtrl", $scope.$parent.quickship.currentStep = 5, $scope.openTermsConditionsDialog = function() {
  165. ngDialog.open({
  166. template: window.Configure.base_url + "FRONTEND/js/modules/terms_and_conditions.html",
  167. className: "ngdialog-theme-default custom-width"
  168. })
  169. }, $scope.goBack = function() {
  170. return $scope.$parent.quickship.$storage.simpleSteps ? $location.path("/package-details") : $location.path("/shipment-rates")
  171. }, $scope.init = function() {
  172. return null === $scope.$parent.quickship.$storage.quickship.packages[0].items[0].price_value ? $location.path("/") : void $window.scrollTo(0, 0)
  173. }, $scope.init()
  174. }]), angular.module("quickShip").controller("YourUsAddressCtrl", ["$scope", "$window", function($scope, $window) {
  175. $scope.name = "YourUsAddressCtrl", $scope.$parent.quickship.currentStep = 6, $scope.init = function() {
  176. $scope.$parent.quickship.address.first_name = $scope.$parent.quickship.$storage.user.first_name, $scope.$parent.quickship.address.last_name = $scope.$parent.quickship.$storage.user.last_name, $scope.$parent.quickship.address.address1 = $scope.$parent.quickship.$storage.address1, $scope.$parent.quickship.address.address2 = $scope.$parent.quickship.$storage.address2, $scope.$parent.quickship.address.address3 = $scope.$parent.quickship.$storage.address3, $scope.$parent.quickship.reset(), $window.scrollTo(0, 0)
  177. }, $scope.init()
  178. }]), angular.module("quickShip").directive("ngMin", function() {
  179. return {
  180. restrict: "A",
  181. require: "ngModel",
  182. link: function(scope, elem, attr, ctrl) {
  183. scope.$watch(attr.ngMin, function() {
  184. ctrl.$setViewValue(ctrl.$viewValue)
  185. });
  186. var minValidator = function(value) {
  187. var min = scope.$eval(attr.ngMin) || 0;
  188. return !isEmpty(value) && value < min ? void ctrl.$setValidity("ngMin", !1) : (ctrl.$setValidity("ngMin", !0), value)
  189. };
  190. ctrl.$parsers.push(minValidator), ctrl.$formatters.push(minValidator)
  191. }
  192. }
  193. }), angular.module("quickShip").directive("magicField", function($compile, $timeout) {
  194. return {
  195. scope: {},
  196. template: '<div class="field"></div>',
  197. transclude: !0,
  198. replace: !0,
  199. restrict: "E",
  200. controller: function($scope, $element) {
  201. $scope.showLabel = !1
  202. },
  203. link: function(scope, element, attrs, ctrl, transclude) {
  204. transclude(scope, function(clone, scope) {
  205. element.append(clone)
  206. });
  207. var input = element.find("input")[0] || element.find("textarea")[0] || element.find("select")[0];
  208. element.find("label")[0];
  209. input = angular.element(input), input.on("focus blur keyup", function(e) {
  210. "focus" == e.type ? (scope.showLabel = !0, input.attr("placeholder", "")) : "blur" === e.type ? scope.showLabel = !0 : input.val().length > 0 ? (scope.showLabel = !0, input.attr("placehoder", "")) : scope.showLabel = !0, $timeout(function() {
  211. scope.$apply()
  212. }, 0)
  213. })
  214. }
  215. }
  216. }), angular.module("quickShip").directive("plusPrefix", function() {
  217. return {
  218. restrict: "A",
  219. scope: {
  220. ngModel: "="
  221. },
  222. link: function(scope, element, attrs, controller) {
  223. element.bind("keyup change focus", function() {
  224. scope.$apply(function() {
  225. "undefined" == typeof scope.ngModel && (scope.ngModel = ""), scope.ngModel = scope.ngModel.replace("+", ""), scope.ngModel.length >= 0 && (scope.ngModel = "+" + scope.ngModel.replace(/[^\d]/g, ""))
  226. })
  227. })
  228. }
  229. }
  230. }), angular.module("quickShip").directive("moneyPrefix", function() {
  231. return {
  232. restrict: "A",
  233. scope: {
  234. ngModel: "="
  235. },
  236. link: function(scope, element, attrs, controller) {
  237. element.bind("keyup change focus", function() {
  238. scope.$apply(function() {
  239. return "undefined" == typeof scope.ngModel || null == scope.ngModel ? (scope.ngModel = "", !1) : (scope.ngModel = scope.ngModel.replace("$", ""), void(scope.ngModel.length >= 0 && (scope.ngModel = "$" + scope.ngModel.replace(/[^\d.]/g, ""))))
  240. })
  241. })
  242. }
  243. }
  244. }), angular.module("quickShip").directive("dynamicName", function($compile, $parse) {
  245. return {
  246. restrict: "A",
  247. terminal: !0,
  248. priority: 1e5,
  249. link: function(scope, elem) {
  250. var name = $parse(elem.attr("dynamic-name"))(scope);
  251. elem.removeAttr("dynamic-name"), elem.attr("name", name), $compile(elem)(scope)
  252. }
  253. }
  254. }), angular.module("quickShip").config(["$provide", function($provide) {
  255. $provide.decorator("ngModelDirective", function($delegate) {
  256. var ngModel = $delegate[0],
  257. controller = ngModel.controller;
  258. return ngModel.controller = ["$scope", "$element", "$attrs", "$injector", function(scope, element, attrs, $injector) {
  259. var $interpolate = $injector.get("$interpolate");
  260. attrs.$set("name", $interpolate(attrs.name || "")(scope)), $injector.invoke(controller, this, {
  261. $scope: scope,
  262. $element: element,
  263. $attrs: attrs
  264. })
  265. }], $delegate
  266. }), $provide.decorator("formDirective", function($delegate) {
  267. var form = $delegate[0],
  268. controller = form.controller;
  269. return form.controller = ["$scope", "$element", "$attrs", "$injector", function(scope, element, attrs, $injector) {
  270. var $interpolate = $injector.get("$interpolate");
  271. attrs.$set("name", $interpolate(attrs.name || attrs.ngForm || "")(scope)), $injector.invoke(controller, this, {
  272. $scope: scope,
  273. $element: element,
  274. $attrs: attrs
  275. })
  276. }], $delegate
  277. })
  278. }]), jQuery("dl.accordion dt").click(function() {
  279. jQuery(this).toggleClass("active")
  280. }), angular.module("quickShip").service("QuickshipSvc", ["$http", "$q", "$localStorage", "$rootScope", function($http, $q, $localStorage, $rootScope) {
  281. var service = this;
  282. return service.configure = null, service.currentStep = 1, service.processingPayment = !1, service.settingsLoaded = !1, service.validatesPersonalDetails = !0, service.validatesPackageDetails = !0, service.shippingRatesSelected = [], service.address = {}, service.defaultOptions = {
  283. shippingRates: [],
  284. shippingRatesSelected: [],
  285. billing_shipping: !0,
  286. simpleSteps: !0,
  287. personalDetailsCompleted: !1,
  288. countries: null,
  289. itemTypes: null,
  290. transactionHash: null,
  291. payment_type: "Paypal",
  292. billing: {
  293. first_name: "",
  294. last_name: "",
  295. phone: "",
  296. address2: ""
  297. },
  298. shipping: {
  299. address2: ""
  300. },
  301. quickship: {
  302. special_instructions: "",
  303. shipping_options: {
  304. cheapest: !1,
  305. fastest: !1
  306. },
  307. packagesExpedited: !1,
  308. repackaged: !1,
  309. packagesInsurance: !1,
  310. consolidatePackages: !1,
  311. requestPhoto: !1,
  312. amount: 1,
  313. packages: [{
  314. tracking_code: "",
  315. consolidated: [],
  316. weight_unit: "lb",
  317. size_unit: "in",
  318. items: [{
  319. description: "",
  320. weight_unit: "lb",
  321. price_value: null,
  322. info: ""
  323. }]
  324. }]
  325. }
  326. }, service.$storage = $localStorage.$default(service.defaultOptions), service.updateSimplePackages = function() {
  327. if (service.$storage.simpleSteps) {
  328. var currentPackagesNum = service.$storage.quickship.packages.length,
  329. newPackagesNum = service.$storage.quickship.amount,
  330. diff = newPackagesNum - currentPackagesNum;
  331. if (newPackagesNum < 1) return !1;
  332. if (newPackagesNum < currentPackagesNum) service.$storage.quickship.packages.splice(newPackagesNum, Math.abs(diff));
  333. else if (diff > 0)
  334. for (var i = 0; i < diff; i++) service.$storage.quickship.packages.push({
  335. items: [{
  336. description: "",
  337. weight_unit: "lb",
  338. price_value: null,
  339. info: ""
  340. }]
  341. })
  342. }
  343. }, service.loadSettings = function() {
  344. service.loadSettingsApi().then(function(res) {
  345. if (service.$storage.read_only_packages = res.read_only_packages, res.read_only_packages && null != res.transaction && "" != res.transaction.Transaction.id)
  346. for (service.$storage.quickship.packages = [], i = 0; i < res.transaction.Shipment.length; i++) service.$storage.quickship.packages.push({
  347. tracking_code: res.transaction.Shipment[i].Package.in_tracking,
  348. consolidated: !1,
  349. carrier: res.transaction.Shipment[i].Package.carrier,
  350. weight_unit: "" != res.transaction.Shipment[i].Package.weight_lb ? "lb" : "kg",
  351. size_unit: "" != res.transaction.Shipment[i].Package.width_in ? "in" : "cm",
  352. weight: "" != res.transaction.Shipment[i].Package.weight_lb ? res.transaction.Shipment[i].Package.weight_lb : res.transaction.Shipment[i].Package.weight_in,
  353. width: "" != res.transaction.Shipment[i].Package.width_in ? res.transaction.Shipment[i].Package.width_in : res.transaction.Shipment[i].Package.width_cm,
  354. height: "" != res.transaction.Shipment[i].Package.height_in ? res.transaction.Shipment[i].Package.height_in : res.transaction.Shipment[i].Package.height_cm,
  355. length: "" != res.transaction.Shipment[i].Package.length_in ? res.transaction.Shipment[i].Package.length_in : res.transaction.Shipment[i].Package.length_cm,
  356. items: [{
  357. description: "",
  358. weight_unit: "lb",
  359. price_value: null,
  360. info: ""
  361. }]
  362. });
  363. if (null != res.user && "" != res.user.User.id) {
  364. console.log(res);
  365. service.$storage.billing.first_name = res.user.User.first_name, service.$storage.billing.last_name = res.user.User.last_name, service.$storage.billing.email = res.user.User.email, service.$storage.billing.phone = res.user.User.phone;
  366. var billing_index = null,
  367. shipping_index = null;
  368. if (1 === res.user.Address.length) billing_index = 0, shipping_index = null;
  369. else if (res.user.Address.length > 1)
  370. for (2 === res.user.Address[0].address_type_id ? (billing_index = 0, shipping_index = 1) : (shipping_index = 0, billing_index = 1), i = 0; i < res.user.Address.length; i++) 1 == res.user.Address[i].default_billing && (billing_index = i), 1 == res.user.Address[i].default_shipping && (shipping_index = i);
  371. null != billing_index && (service.$storage.billing.address1 = res.user.Address[billing_index].address1, service.$storage.billing.address2 = res.user.Address[billing_index].address2, service.$storage.billing.city = res.user.Address[billing_index].city, service.$storage.billing.state = res.user.Address[billing_index].adm_division, service.$storage.billing.postal_code = res.user.Address[billing_index].postal_code, service.$storage.billing.country = res.user.Address[billing_index].country_id, service.$storage.billing_shipping = null == shipping_index, null != shipping_index && (service.$storage.shipping.first_name = res.user.Address[shipping_index].firstname, service.$storage.shipping.last_name = res.user.Address[shipping_index].lastname, service.$storage.shipping.address1 = res.user.Address[shipping_index].address1, service.$storage.shipping.address2 = res.user.Address[shipping_index].address2, service.$storage.shipping.city = res.user.Address[shipping_index].city, service.$storage.shipping.state = res.user.Address[shipping_index].adm_division, service.$storage.shipping.postal_code = res.user.Address[shipping_index].postal_code, service.$storage.shipping.country = res.user.Address[shipping_index].country_id, service.$storage.shipping.email = res.user.User.email, service.$storage.shipping.phone = res.user.User.phone))
  372. }
  373. service.$storage.countries = res.countries, service.$storage.itemTypes = res.item_types, service.$storage.handling_fee = res.handling_fee, service.settingsLoaded = !0, $rootScope.$broadcast("event:loaded-api")
  374. }, function(err) {
  375. alert("Error occurred: " + err)
  376. })
  377. }, service.loadSettingsApi = function() {
  378. var defer = $q.defer();
  379. return $http.post(service.configure.base_url + "quickship/api_settings.json", null).success(function(res) {
  380. defer.resolve(res)
  381. }).error(function(err) {
  382. defer.reject(err)
  383. }), defer.promise
  384. }, service.updatePersonalInfo = function() {
  385. var defer = $q.defer();
  386. return $http.post(service.configure.base_url + "quickship/step1.json", service.$storage).success(function(res) {
  387. defer.resolve(res)
  388. }).error(function(err) {
  389. defer.reject(err)
  390. }), defer.promise
  391. }, service.addPackage = function() {
  392. return service.$storage.quickship.packages.push({
  393. consolidated: [],
  394. weight_unit: "lb",
  395. size_unit: "in",
  396. items: [{
  397. description: "",
  398. weight_unit: "lb",
  399. price_value: null,
  400. info: ""
  401. }]
  402. })
  403. }, service.addConsolidatedPackage = function(package_index) {
  404. var currentItemsNum = service.$storage.quickship.packages[package_index].consolidated.length,
  405. defaultData = {
  406. weight: null,
  407. unit: "lb"
  408. };
  409. currentItemsNum < 5 ? service.$storage.quickship.packages[package_index].consolidated.push(defaultData) : (service.addPackage(), service.$storage.quickship.packages[service.$storage.quickship.packages.length - 1].consolidated.push(defaultData))
  410. }, service.cleanupConsolidated = function() {
  411. angular.forEach(service.$storage.quickship.packages, function(value, index) {
  412. service.$storage.quickship.packages[index].consolidated = []
  413. })
  414. }, service.updateConsolidated = function() {
  415. service.$storage.quickship.consolidatePackages === !1 && service.cleanupConsolidated()
  416. }, service.loadRates = function() {
  417. var defer = $q.defer();
  418. return service.$storage.shippingRates = [], service.$storage.shippingRatesSelected = [], $http.post(service.configure.base_url + "quickship/api_rates.json", service.$storage).success(function(res) {
  419. "object" == typeof res && (service.$storage.shippingRates = res), defer.resolve(res)
  420. }).error(function(err) {
  421. defer.reject(err)
  422. }), defer.promise
  423. }, service.pay_handling = function() {
  424. var params = {
  425. description: "Package handling fee",
  426. price: service.$storage.handling_fee,
  427. steps: service.simpleSteps === !0 ? "simple" : "full",
  428. shipment: service.$storage
  429. };
  430. service.processingPayment = !0, $http.post(service.configure.base_url + "quickship/payment.json", params).success(function(res) {
  431. setTimeout(function(){
  432. console.log('done');
  433. res.ok ? window.location = res.approval_link : (alert("Error, please try again"), service.processingPayment = !1)
  434. }, 7000);
  435.  
  436. }).error(function(err) {
  437. alert("Error, please try again"), service.processingPayment = !1
  438. })
  439. }, service.isSelectedItem = function(key, selected_id) {
  440. return key == selected_id
  441. }, service.getSimpleStepsAmountMin = function(amount) {
  442. return service.$storage.simpleSteps === !0 && amount
  443. }, service.selectService = function(package_index, shipping_rate) {
  444. service.$storage.shippingRatesSelected[package_index] = {
  445. package: package_index,
  446. shipping_rate_id: shipping_rate.rate_id,
  447. shipment_id: shipping_rate.shipment_id,
  448. rate: shipping_rate,
  449. price: shipping_rate.rate,
  450. price_num: shipping_rate.rate_num
  451. }, service.$storage.quickship.packages.length > package_index + 1 ? (document.getElementById("divPackage" + (package_index + 1)).scrollIntoView(), angular.element($(".step-4")).scope().currentRateGroup = package_index + 1) : console.log(service.$storage)
  452. }, service.getShippingCharge = function() {
  453. if (service.$storage.simpleSteps === !0) return service.$storage.handling_fee;
  454. var total = 0;
  455. return service.$storage.shippingRatesSelected.length > 0 && angular.forEach(service.$storage.shippingRatesSelected, function(value) {
  456. var price = parseFloat(value.price.replace(" USD", ""));
  457. total += price
  458. }), service.$storage.quickship.packagesInsurance === !0 && (total += .01 * total), total
  459. }, service.hasConsolidatedPackages = function(index) {
  460. var has_consolidated = !1;
  461. return "undefined" != typeof service.$storage.quickship.packages[index] && "undefined" != typeof service.$storage.quickship.packages[index].consolidated && service.$storage.quickship.packages[index].consolidated.length > 0 && (has_consolidated = !0), has_consolidated
  462. }, service.cleanup = function() {
  463. var personal_info = service.$storage.user,
  464. billing = service.$storage.billing,
  465. shipping = service.$storage.shipping;
  466. service.reset(), service.$storage.user = personal_info, service.$storage.billing = billing, service.$storage.shipping = shipping
  467. }, service.calculateHandlingFee = function() {
  468. var amount = 0;
  469. return angular.forEach(service.$storage.quickship.packages, function(value, index) {
  470. amount++
  471. }), service.$storage.handling_fee * amount
  472. }, service.calculateTotal = function() {
  473. let totalAmount = 0;
  474. for (i = 0; i < service.$storage.shippingRatesSelected.length; i++){
  475. totalAmount = service.$storage.shippingRatesSelected[i].price_num + totalAmount;
  476. }
  477. return totalAmount.toFixed(2);
  478. }, service.calculateWeight = function(orig_weight, orig_weight_unit, dest_weight_unit) {
  479. if (!parseFloat(orig_weight)) return 0;
  480. if (orig_weight = parseFloat(orig_weight), orig_weight_unit === dest_weight_unit) return orig_weight;
  481. var new_amount = "lb" === dest_weight_unit ? 2.2046 * orig_weight : orig_weight / 2.2046;
  482. return new_amount = +(Math.round(new_amount + "e+2") + "e-2")
  483. }, service.reset = function() {
  484. service.$storage.$reset(), service.$storage = $localStorage.$default(service.defaultOptions)
  485. }, service
  486. }]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement