define(['dashboard/dashboard'], function (dashboard) { dashboard.controller('VehicleInspection', ['$scope', '$http', '$window', "$q", "$filter", "Helpers", '$mdDialog', function ($scope, $http, $window, $q, $filter, Helpers, $mdDialog) { $scope.currentStepIndex = 0; function scrollToWindowTop() { $window.scrollTo(0, 0); } //Step Navigation $scope.steps = ['VehicleInspection_ScanLicenceDisk', 'VehicleInspection_VehicleDetails', 'VehicleInspection_ScanDriversLicence', 'VehicleInspection_RegularDriver', 'VehicleInspection_SoundSystem', 'VehicleInspection_Accessories', 'VehicleInspection_Photographs', 'VehicleInspection_DamageDetails', 'VehicleInspection_Summary']; $scope.NavForward = function () { //This check if the navigation is coming from the summary page, //and sets the next index back to the summary so user doesn't have to go through the whole inspection again. if ($scope.summaryIndex) { $scope.currentStepIndex = $scope.summaryIndex; $scope.summaryIndex = null; } else { $scope.currentStepIndex++; } scrollToWindowTop(); } $scope.NavPrevious = function () { //This check if the navigation is coming from the summary page, //and sets the next index back to the summary so user doesn't have to go through the whole inspection again. if ($scope.summaryIndex) { $scope.currentStepIndex = $scope.summaryIndex; $scope.summaryIndex = null; } else { $scope.currentStepIndex--; } scrollToWindowTop(); } //Navigate to any step $scope.GoToStep = function (stepName) { //This sets the summaryIndex to the currentStepIndex when navigating from the summary page to stpe the user wishes to edit $scope.summaryIndex = $scope.currentStepIndex; for (var x = 0; x <= $scope.steps.length; x++) { if ($scope.steps[x] == stepName) { $scope.currentStepIndex = x; scrollToWindowTop(); break; } } } $scope.$watch('currentStepIndex', function () { $scope.currentStep = $scope.steps[$scope.currentStepIndex]; }); //Validation $scope.controllers = {}; $scope.IsCurrentFormInvalid = function () { return $scope.controllers[$scope.currentStep].invalid(); } $scope.FilthifyCurrentForm = function () { $scope.controllers[$scope.currentStep].filthify(); } //Submit the Inspectionstate $scope.SubmitInspection = function () { $scope.InspectionState.Submitted = true; $scope.InspectionState.SubmitInprogress = true; scrollToWindowTop(); for (var x in $scope.InspectionState.Photos) { if ($scope.InspectionState.Photos[x] != null) { //Using the FullFile for the inspection photo, which is the primary key in the images table //the Id is just a substring of the FullFile $scope.InspectionState.InspectionPhotos[x] = $scope.InspectionState.Photos[x].FullFile; } } for (var i = 0; i < $scope.InspectionState.DamageDetails.length; i++) { if ($scope.InspectionState.DamageDetails[i].Photo != null) { //Using the FullFile for the inspection photo, which is the primary key in the images table //the Id is just a substring of the FullFile $scope.InspectionState.DamageDetails[i].PhotoId = $scope.InspectionState.DamageDetails[i].Photo.FullFile; } } $http({ method: 'POST', url: 'DashboardVehicleinspection/Submit', data: $scope.InspectionState }).then(function successCallback(response) { var newLocationData = response.data || response; window.location = newLocationData.url; }, function errorCallback(response) { $scope.InspectionState.SubmitFailed = true; $scope.InspectionState.SubmitSucceeded = false; $scope.InspectionState.SubmitInprogress = false; }); } //When a license disc is scanned try to update the inpectionstate if we could extract data function licenseDiskBarcodeDataChanged(d) { var v = $scope.InspectionState.Vehicle; $scope.VehicleLicenceDiskScanned = false; $scope.InspectionState.Vehicle.VehicleLicenceDisk = true; if (d) { $scope.VehicleLicenceDiskScanned = true; v.VinNumber = d.VinNumber; v.EngineNumber = d.EngineNumber; v.RegistrationNumber = d.RegistrationNumber; v.VehicleNumber = d.VehicleNumber; // The drivers license is made editable if the value retrieved from the AS/400 differs from the license disc if (v.HasLoadedLicenceNumber && v.LicenceNumber != d.LicenceNumber) { v.HasLoadedLicenceNumber = false; } v.LicenceNumber = d.LicenceNumber; //Vehicle regno is the licence number on the disk $scope.InspectionState.ScannedData.RegistrationNumber = d.LicenceNumber; if ($scope.InspectionState.ScannedData.RegistrationNumber && $scope.InspectionState.ScannedData.RegistrationNumber.toUpperCase() != v.OrigLicenceNumber.toUpperCase()) { $scope.InspectionState.MustSendMail = true; } $scope.InspectionState.ScannedData.Make = d.Make; if ($scope.InspectionState.ScannedData.Make && $scope.InspectionState.ScannedData.Make.toUpperCase() != v.ManufacturerDescription.toUpperCase()) { $scope.InspectionState.MustSendMail = true; } $scope.InspectionState.ScannedData.Model = d.Model; if ($scope.InspectionState.ScannedData.Model && $scope.InspectionState.ScannedData.Model.toUpperCase() != v.Model.toUpperCase()) { $scope.InspectionState.MustSendMail = true; } $scope.InspectionState.ScannedData.Description = d.Description; if ($scope.InspectionState.ScannedData.Description && $scope.InspectionState.ScannedData.Description.toUpperCase() != v.Description.toUpperCase()) { $scope.InspectionState.MustSendMail = true; } $scope.InspectionState.ScannedData.ColorCode = d.ColorCode; $scope.InspectionState.ScannedData.Color = d.Color; } else { v.VehicleNumber = null; v.RegistrationNumber = null; v.EngineNumber = null; v.VinNumber = null; // In the event that the licence no was loaded from the AS/400 do not clear it when clearing the image, otherwise someone could opload an image which barcode does not read, clears it and the AS/400 value is gone. if (!v.HasLoadedLicenceNumber) { v.HasLoadedLicenceNumber = null; v.LicenceNumber = null; } } } function driversLicenseBarcodeDataChanged(d) { var v = $scope.InspectionState.RegularDriver; //Keep the state of the origal driver loaded from the AS/400 if (!$scope.OrigRegularDriver) { $scope.OrigRegularDriver = {}; angular.copy(v, $scope.OrigRegularDriver); } var orig = $scope.OrigRegularDriver; if (d) { $scope.InspectionState.ScannedData.Surname = d.LastName; if ($scope.InspectionState.ScannedData.Surname && $scope.InspectionState.ScannedData.Surname.toUpperCase() != v.LastName.toUpperCase()) { $scope.InspectionState.MustSendMail = true; } $scope.InspectionState.ScannedData.IdNumber = d.IdNumber; if ($scope.InspectionState.ScannedData.IdNumber && $scope.InspectionState.ScannedData.IdNumber != v.IdNumber) { $scope.InspectionState.MustSendMail = true; } $scope.InspectionState.ScannedData.LicenseIssueDate = d.IssueDate; if ($scope.InspectionState.ScannedData.LicenseIssueDate && $scope.InspectionState.ScannedData.LicenseIssueDate != v.InceptionDate) { $scope.InspectionState.MustSendMail = true; } v.VehicleCode = d.VehicleCode, v.DriverCertNum = d.DriverCertNum, v.RestArtGlasses = d.RestArtGlasses, v.RestArtLimbs = d.RestArtLimbs, v.DriverCardIssNum = d.DriverCardIssNum.toString(), //toString otherwise the input whould reject the value. v.CountryOfLic = d.CountryOfLic, v.DateLicValidFrom = d.DateLicValidFrom, v.DateLicValidTo = d.DateLicValidTo } else { angular.copy(orig, v); //Reset Regular Driver as retrieved from the AS/400 } } $scope.driversLicenseDateFromChanged = function (to, from) { var dateTo = $scope.InspectionState.RegularDriver.DateLicValidTo; if (typeof to !== "undefined" && to != null) { var toYear = to.getFullYear() + 5; dateTo = new Date(to); $scope.InspectionState.RegularDriver.DateLicValidTo = new Date(dateTo.setFullYear(toYear) - 1); } } $scope.$watch("InspectionState.Photos.VehicleLicenceDisk.LicenseDiscBarcodeData", licenseDiskBarcodeDataChanged, true); $scope.$watch("InspectionState.Photos.DriversLicenceBack.DriversLicenseBarcodeData", driversLicenseBarcodeDataChanged, true); $scope.InspectionState = window._InspectionState; //Lookups and function to get current descriptions for selected. $scope.RegularDriverLicenseTypes = window._RegularDriverLicenseTypes; $scope.RegularDriverLicenseTypeDescription = function () { var matches = $filter("filter")($scope.RegularDriverLicenseTypes, { "Code": $scope.InspectionState.RegularDriver.LicenceType }); if (matches && matches != null && matches.length == 1) { return matches[0].Description; } else { return ""; } } $scope.VehicleColours = window._VehicleColours; $scope.VehicleColourDescription = function () { var matches = $filter("filter")($scope.VehicleColours, { "Code": $scope.InspectionState.Vehicle.Colour }); if (matches && matches != null && matches.length == 1) { return matches[0].Description; } else { return ""; } } //Terms and conditions var termsDialogTemplate = Helpers.dialog("termsDialog"); function dialogFunction(template) { return function dialogFunctionFunction(event) { $mdDialog.show({ clickOutsideToClose: true, disableParentScroll: false, scope: $scope, preserveScope: true, targetEvent: event, template: template, controller: ['$scope', '$mdDialog', function ($scope, $mdDialog) { $scope.accept = $mdDialog.hide; }] }); } } $scope.showTermsAndConditions = dialogFunction(termsDialogTemplate); }]) .controller('VehicleInspection_ScanLicenceDisk', ['$scope', function ($scope) { $scope.controllers['VehicleInspection_ScanLicenceDisk'] = { 'invalid': function () { return $scope.ScanLicenceDiskForm.$invalid; }, 'filthify': function () { $scope.filthify($scope.ScanLicenceDiskForm); } } }]) .controller('VehicleInspection_RegularDriver', ['$scope', function ($scope) { $scope.controllers['VehicleInspection_RegularDriver'] = { 'invalid': function () { return $scope.RegularDriverForm.$invalid; }, 'filthify': function () { $scope.filthify($scope.RegularDriverForm); $scope.filthify($scope.RegularDriverLicenceForm); } } $scope.curDate = new Date(); $scope.InspectionState.RegularDriver.DriverCardIssNum = 1; function checkHasPdp() { if (!$scope.InspectionState.RegularDriver.HasPdp) { $scope.InspectionState.RegularDriver.DatePermitValidTo = null; } } $scope.$watch("InspectionState.RegularDriver.HasPdp", checkHasPdp); }]) .controller('VehicleInspection_ScanDriversLicence', ['$scope', function ($scope) { $scope.controllers['VehicleInspection_ScanDriversLicence'] = { 'invalid': function () { return $scope.ScanDriversLicenceForm.$invalid; }, 'filthify': function () { $scope.filthify($scope.ScanDriversLicenceForm); } } }]) .controller('VehicleInspection_VehicleDetails', ['$scope', function ($scope) { $scope.controllers['VehicleInspection_VehicleDetails'] = { 'invalid': function () { return $scope.VehicleDetailsForm.$invalid; }, 'filthify': function () { $scope.filthify($scope.VehicleDetailsForm); } } }]) .controller('VehicleInspection_Accessories', ['$scope', function ($scope) { $scope.accessorySelection = true; //Select at least one checkbox from acccesories function accessoryChanged(v) { var accessories = $scope.InspectionState.VehicleAccesories; $scope.accessorySelection = !(accessories.HasAirCon || accessories.HasAirFresher || accessories.HasBootSpoiler || accessories.HasBullBars || accessories.HasCanopy || accessories.HasCompleteAeroKit || accessories.HasElectricAerial || accessories.HasElecticMirror || accessories.HasFrontSpoiler || accessories.HasLouvres || accessories.HasRollBars || accessories.HasRoofRack || accessories.HasSpotlights || accessories.HasSunroof || accessories.HasTintedWindows || accessories.HasTowbar || accessories.HasVehiclePhone || accessories.HasWinch || accessories.HasWindDeflector || accessories.HasFogSpots || accessories.HasSatNav || accessories.HasLeatherSeats || accessories.HasSmashGrabFilm || accessories.HasFullSkirt || accessories.HasBluetooth || accessories.HasNudgeBar || accessories.HasRainSensors || accessories.HasNoAccessories || v); } //In addition to checking validity unselect all other accessories first if checked function noAccessoriesChanged(v) { if (v == true) { var accessories = $scope.InspectionState.VehicleAccesories; accessories.HasAirCon = false; accessories.HasAirFresher = false; accessories.HasBootSpoiler = false; accessories.HasBullBars = false; accessories.HasCanopy = false; accessories.HasCompleteAeroKit = false; accessories.HasElectricAerial = false; accessories.HasElecticMirror = false; accessories.HasFrontSpoiler = false; accessories.HasLouvres = false; accessories.HasRollBars = false; accessories.HasRoofRack = false; accessories.HasSpotlights = false; accessories.HasSunroof = false; accessories.HasTintedWindows = false; accessories.HasTowbar = false; accessories.HasVehiclePhone = false; accessories.HasWinch = false; accessories.HasWindDeflector = false; accessories.HasFogSpots = false; accessories.HasSatNav = false; accessories.HasLeatherSeats = false; accessories.HasSmashGrabFilm = false; accessories.HasFullSkirt = false; accessories.HasBluetooth = false; accessories.HasNudgeBar = false; accessories.HasRainSensors = false; } accessoryChanged(v); } $scope.$watch("InspectionState.VehicleAccesories.HasAirCon", accessoryChanged); $scope.$watch("InspectionState.VehicleAccesories.HasAirFresher", accessoryChanged); $scope.$watch("InspectionState.VehicleAccesories.HasBootSpoiler", accessoryChanged); $scope.$watch("InspectionState.VehicleAccesories.HasBullBars", accessoryChanged); $scope.$watch("InspectionState.VehicleAccesories.HasCanopy", accessoryChanged); $scope.$watch("InspectionState.VehicleAccesories.HasCompleteAeroKit", accessoryChanged); $scope.$watch("InspectionState.VehicleAccesories.HasElectricAerial", accessoryChanged); $scope.$watch("InspectionState.VehicleAccesories.HasElecticMirror", accessoryChanged); $scope.$watch("InspectionState.VehicleAccesories.HasFrontSpoiler", accessoryChanged); $scope.$watch("InspectionState.VehicleAccesories.HasLouvres", accessoryChanged); $scope.$watch("InspectionState.VehicleAccesories.HasRollBars", accessoryChanged); $scope.$watch("InspectionState.VehicleAccesories.HasRoofRack", accessoryChanged); $scope.$watch("InspectionState.VehicleAccesories.HasSpotlights", accessoryChanged); $scope.$watch("InspectionState.VehicleAccesories.HasSunroof", accessoryChanged); $scope.$watch("InspectionState.VehicleAccesories.HasTintedWindows", accessoryChanged); $scope.$watch("InspectionState.VehicleAccesories.HasTowbar", accessoryChanged); $scope.$watch("InspectionState.VehicleAccesories.HasVehiclePhone", accessoryChanged); $scope.$watch("InspectionState.VehicleAccesories.HasWinch", accessoryChanged); $scope.$watch("InspectionState.VehicleAccesories.HasWindDeflector", accessoryChanged); $scope.$watch("InspectionState.VehicleAccesories.HasFogSpots", accessoryChanged); $scope.$watch("InspectionState.VehicleAccesories.HasSatNav", accessoryChanged); $scope.$watch("InspectionState.VehicleAccesories.HasLeatherSeats", accessoryChanged); $scope.$watch("InspectionState.VehicleAccesories.HasSmashGrabFilm", accessoryChanged); $scope.$watch("InspectionState.VehicleAccesories.HasFullSkirt", accessoryChanged); $scope.$watch("InspectionState.VehicleAccesories.HasNudgeBar", accessoryChanged); $scope.$watch("InspectionState.VehicleAccesories.HasRainSensors", accessoryChanged); $scope.$watch("InspectionState.VehicleAccesories.HasNoAccessories", noAccessoriesChanged); $scope.controllers['VehicleInspection_Accessories'] = { 'invalid': function () { return $scope.AccessoriesForm.$invalid; }, 'filthify': function () { $scope.filthify($scope.AccessoriesForm); } } }]) .controller('VehicleInspection_SoundSystem', ['$scope', function ($scope) { $scope.controllers['VehicleInspection_SoundSystem'] = { 'invalid': function () { return $scope.SoundSystemForm.$invalid; }, 'filthify': function () { $scope.filthify($scope.SoundSystemForm); } } //Disable other sound options and set to know if no sound system in place function hasSoundSystemChanged(v) { if (v == false) { $scope.InspectionState.SoundSystem.HasCdShuttle = null; $scope.InspectionState.SoundSystem.IsFactoryFitted = null; } } $scope.$watch("InspectionState.SoundSystem.HasSoundSystem", hasSoundSystemChanged) }]) .controller('VehicleInspection_Photographs', ['$scope', function ($scope) { $scope.controllers['VehicleInspection_Photographs'] = { 'invalid': function () { return $scope.PhotoForm.$invalid; }, 'filthify': function () { $scope.filthify($scope.PhotoForm); } } }]) .controller('VehicleInspection_DamageDetails', ['$scope', function ($scope) { $scope.controllers['VehicleInspection_DamageDetails'] = { 'invalid': function () { return $scope.DamageDetailsForm.$invalid; }, 'filthify': function () { $scope.filthify($scope.DamageDetailsForm); } } $scope.InspectionState.DamageDetails = $scope.InspectionState.DamageDetails || []; $scope.damageIndex = ($scope.InspectionState.DamageDetails.length > 0) ? $scope.InspectionState.DamageDetails.length - 1 : 0; $scope.$watch('InspectionState.DamageDetails[damageIndex].Type', function (newVal) { //This clears the area field if the type of damage is not body, as in our app only body has an area of damage if (newVal != "Body" && newVal) { $scope.InspectionState.DamageDetails[$scope.damageIndex].Area = null; } }); //when the currently selected Vehicle changes $scope.ChangeIndex = function (index) { $scope.damageIndex = index; }; //when a Vehicle is removed $scope.RemoveItem = function (index) { $scope.InspectionState.DamageDetails.splice(index, 1); if (index <= $scope.damageIndex && $scope.damageIndex > 0) { $scope.damageIndex--; } }; //when a Vehicle is added $scope.AddItem = function AddItem() { if (!$scope.InspectionState.DamageDetails) { $scope.InspectionState.DamageDetails = [] } $scope.damageIndex = $scope.InspectionState.DamageDetails.length; $scope.InspectionState.DamageDetails[$scope.damageIndex] = { Type: '', Area: null, Photo: '', Comment: '' }; }; }]) .controller('VehicleInspection_Summary', ['$scope', function ($scope) { $scope.controllers['VehicleInspection_Summary'] = { 'invalid': function () { return $scope.SummaryForm.$invalid; }, 'filthify': function () { $scope.filthify($scope.SummaryForm); } } }]); });