angular.module('kalgudiApp').controller('assertActivityController', ['$scope', '$state', '$rootScope', 'pageServices', '$injector', 'profileService', 'kalgudiLiterals', function ($scope, $state, $rootScope, pageServices, $injector, profileService, kalgudiLiterals) { $scope.isSubscribersPickerActive = false; $scope.isreportActivityActive = false; $scope.contentliterals = kalgudiLiterals; $scope.activityTypes = [ { 'activityName': "Farm Activity" }, { 'activityName': "FLD" }, { 'activityName': "Land Preparation" }, { 'activityName': "Pre-Planting" }, { 'activityName': "Inter Cropping" }, { 'activityName': "Input Management" }, { 'activityName': "Intercultural Operations" }, { 'activityName': "Pest & Disease Management" } ]; $scope.reportActivity = { 'product': {}, 'activityType': "", 'subActivityType':"", 'subject': "", 'description': "", 'activityPerformedOn': "", 'livestockInfo': {}, 'attachments': [] }; $scope.animalFeed = { 'fooder': {} } $scope.animalOutput ={ 'milkingInfo': {} } $scope.clearReportActivity = function () { $scope.reportActivity = { 'product': {}, 'activityType': "", 'subActivityType':"", 'subject': "", 'description': "", 'activityPerformedOn': "", 'livestockInfo': {}, 'attachments': [] }; $scope.animalFeed = {}; $scope.animalOutput = {}; } $scope.changeActivityType = function() { $scope.reportActivity.subActivityType = ''; } $rootScope.programId = $state.params.programId; $scope.selectedAssert = {}; $scope.productData = []; //Flags for form product of asset selection $scope.stockFlag = false; $scope.cropFlag = false; if ($state.params.from == "digitalAssistance" || (!detectmob() && $state.current.name == 'programs.activities')) { $scope.isAssisted = true; $scope.selectedSubscriber = {}; } else { $scope.isAssisted = false; // if user want to report any activity for himself $scope.selectedSubscriber = $rootScope.loggedInUserBusinessProfile; $scope.products = $scope.selectedSubscriber.lstOfUserBusinessDetailsInfo[0].lstOfProducts; $scope.products = _.filter($scope.products, function (product) { return product.productId != "000000037555" }); } //Makeing Form show and hide based on user selection productType or $scope.checkType = function (object) { if (object.type == "LIVESTOCK") { $scope.stockFlag = true; $scope.cropFlag = false; } else if (object.productName != '') { $scope.cropFlag = true; $scope.stockFlag = false; } else { $scope.stockFlag = false; $scope.cropFlag = false; } } //initalize dynamically inject dependency function init() { if (detectmob()) { lazysync = $injector.get('lazysync'); } else { $uibModal = $injector.get('$uibModal'); } if (!detectmob()) { $scope.selectedSubscriber = {}; } } init(); $scope.selectSubscriber = function () { $scope.isSubscribersPickerActive = !$scope.isSubscribersPickerActive; $scope.isreportActivityActive = !$scope.isreportActivityActive; } var datePickerCallback = function (val) { if (typeof (val) === 'undefined') { ; } else { if (typeof (val) === "number") { $scope.datepickerObject.activityPerformedOn.setHours(0); $scope.datepickerObject.activityPerformedOn.setMinutes(0); var date = $scope.datepickerObject.activityPerformedOn.getTime(); $scope.datepickerObject.activityPerformedOn = new Date(date + (val * 1000)); } else { $scope.datepickerObject.activityPerformedOn = val; } $scope.datepickerObject.inputEpochTime = (($scope.datepickerObject.activityPerformedOn.getHours() * 60 * 60) + ($scope.datepickerObject.activityPerformedOn.getMinutes() * 60)); if (!$scope.datepickerObject.inputEpochTime) { $scope.datepickerObject.inputEpochTime = 60; } } }; $scope.datepickerObject = { /*from: new Date(),*/ to: new Date(), todayLabel: 'Today', closeLabel: 'Close', inputDate: "", step: "1", format: "12", callback: function (val) { //Mandatory datePickerCallback(val); } } /*date object for thr webapp*/ $scope.AvailableProgramsDate = new Date(); $scope.ExpireDate = new Date(); $scope.dateFormat = 'dd-MMM-yy'; $scope.availableProgramsDateOptions = { formatYear: 'yy', startingDay: 1, minDate: new Date(1995, 5, 22), maxDate: $scope.ExpireDate }; $scope.ProramsavailableDatePopupForToDate = { opened: false } $scope.openDatePrograms = function () { $scope.ProramsavailableDatePopupForToDate.opened = !$scope.ProramsavailableDatePopupForToDate.opened; } $scope.datepickerObject.activityPerformedOn = new Date(); /*date object for thr webapp*/ $scope.saveActivity = function () { // Form validation if ($scope.isAssisted && !$scope.selectedSubscriber.profileKey) { $rootScope.commonTopErrorAlert($scope.contentliterals.searchByKey('PROGRAM_PLEASE_SELECT_ASSIST_MEMBER')); return; } if (!$scope.reportActivity.product.productId) { $rootScope.commonTopErrorAlert($scope.contentliterals.searchByKey('PROGRAM_PLEASE_SELECT_CROP_TO_REPORT')); return; } if (!$scope.reportActivity.subject) { $rootScope.commonTopErrorAlert($scope.contentliterals.searchByKey('PROGRAM_SUBJECT_IS_MENDATORY')); return; } $rootScope.spinerisActive = true; for (var i in $scope.reportActivity.attachments) { delete $scope.reportActivity.attachments[i].localFile; delete $scope.reportActivity.attachments[i].name; delete $scope.reportActivity.attachments[i].size; } // request object var request = { "activityType": $scope.reportActivity.activityType.activityName, "authorId": $scope.selectedSubscriber.profileKey, "authorName": $scope.selectedSubscriber.firstName, "authorProfilePic": $scope.selectedSubscriber.profilePicUrl, "activityPerformedOn": $scope.datepickerObject.activityPerformedOn, "subject": $scope.reportActivity.subject, "description": $scope.reportActivity.description, "attachments": $scope.reportActivity.attachments, "product": $scope.reportActivity.product } // if logged in user is reporting any activity on behalf of other users if ($scope.selectedSubscriber.profileKey != $rootScope.loggedInUserBusinessProfile.profileKey) { request.assistantDetails = { "profileKey": $rootScope.loggedInUserBusinessProfile.profileKey, "firstName": $rootScope.loggedInUserBusinessProfile.firstName, "profilePicURL": $rootScope.loggedInUserBusinessProfile.profilePicUrl } } // If you have geo locations if (LATITUDELONGITUDEVALUES && LATITUDELONGITUDEVALUES.length >= 2) { // Append geo locations to request object request.geoLocation = { latitude: LATITUDELONGITUDEVALUES[0], longitude: LATITUDELONGITUDEVALUES[1] } } callReportActivityService(request); } // Function to submit asset of subscriber $scope.saveAsset = function () { if($scope.reportActivity.subActivityType == 'Fooder' ){ $scope.reportActivity.livestockInfo.animalFeed = $scope.animalFeed; } else if($scope.reportActivity.subActivityType == 'MilkYield' ){ $scope.reportActivity.livestockInfo.animalOutput = $scope.animalOutput; } // request object var request = { "activityType": $scope.reportActivity.activityType, "subActivityType": $scope.reportActivity.subActivityType, "authorId": $scope.selectedSubscriber.profileKey, "authorName": $scope.selectedSubscriber.firstName, "authorProfilePic": $scope.selectedSubscriber.profilePicUrl, "activityPerformedOn": $scope.datepickerObject.activityPerformedOn, "subject": $scope.reportActivity.subject, "description": $scope.reportActivity.description, "attachments": $scope.reportActivity.attachments, "product": $scope.reportActivity.product.productInfo, "livestockInfo": $scope.reportActivity.livestockInfo } // if logged in user is reporting any activity on behalf of other users if ($scope.selectedSubscriber.profileKey != $rootScope.loggedInUserBusinessProfile.profileKey) { request.assistantDetails = { "profileKey": $rootScope.loggedInUserBusinessProfile.profileKey, "firstName": $rootScope.loggedInUserBusinessProfile.firstName, "profilePicURL": $rootScope.loggedInUserBusinessProfile.profilePicUrl } } callReportActivityService(request); } /** * Function to call report activity service. * * @request Request payload object to send to the service. */ callReportActivityService = function (request) { // If lazy sync is required if (detectmob() && doLazySync(request, lazysync.STAGING_TYPE.REPORT_ACTIVITY)) { $scope.selectedSubscriber = undefined; $rootScope.commonTopErrorAlert($scope.contentliterals.searchByKey('PROGRAM_YOUR_ACTIVITY_REPORTED_LATER')); $rootScope.commentValidationFlag = false; window.history.back(); return; } pageServices.reportActivity($rootScope.programId, request) .then( function (reportActivityResponse) { $rootScope.spinerisActive = false; $scope.selectedSubscriber = undefined; $rootScope.commentValidationFlag = false; if (detectmob()) { $rootScope.commonTopErrorAlert($scope.contentliterals.searchByKey('PROGRAM_ACTIVITY_REPORTED_SUCCESSFULLY')); window.history.back(); } else { $scope.selectedSubscriberText = ''; $scope.reportActivity.activityType = $scope.activityTypes[0]; $scope.reportActivity.subject = ''; $scope.reportActivity.description = ''; $scope.datepickerObject.activityPerformedOn = new Date(); $scope.reportActivity.attachments = []; $rootScope.$broadcast('shareUpdatesinactity') $rootScope.showSuccessMessage('Report Activity', $scope.contentliterals.searchByKey('PROGRAM_ACTIVITY_REPORTED_SUCCESSFULLY')); } }, function (error) { $rootScope.spinerisActive = false; $rootScope.showErrorMessage('Report Activity', $scope.contentliterals.searchByKey('PROGRAM_SERVER_BUSY_TRY_LATER')); $rootScope.commonTopErrorAlert($scope.contentliterals.searchByKey('PROGRAM_SERVER_BUSY_TRY_LATER')); } ); } $scope.isFormDirty = function () { $rootScope.commentValidationFlag = true; } $scope.deselectAttachment = function (index) { $scope.reportActivity.attachments.splice(index, 1); } $rootScope.$on("subscriberDetails", function (event, subscriberDetail) { $scope.selectedSubscriber = subscriberDetail; $scope.products = $scope.selectedSubscriber.lstOfUserBusinessDetailsInfo[0].lstOfProducts; $scope.products = _.filter($scope.products, function (product) { return product.productId != "000000037555" }); $scope.selectSubscriber(); if ($scope.products.length > 0) { $scope.productData = $scope.products; } $scope.getAssetDetails(); }); $rootScope.$on("uploadedS3URL", function (event, imageFile) { $scope.reportActivity.attachments.push(imageFile); }); /** * Checks for network availability. If there is no active internet connectivity, * it add request object to localStorage to sync it later. * * @request Request object to sync. * @SYNC_TYPE Constant of type lazySync.STAGING_TYPE * * @return Return true if lazy sync will be done for current request, * otherwise returns false. */ doLazySync = function (request, SYNC_TYPE) { /* * If there is no network then keep the request object and make service call later. */ if (!$rootScope.networkAvaliable) { lazysync.addToStaging(request, SYNC_TYPE); // Make sure that spinners are not active $rootScope.spinerisActive = false; // Yes lazy sync will be done return true; } // No lazy sync will not be done return false; } $scope.openAssistanceMembers = function (size, indexValue) { var modalInstance = $uibModal.open({ templateUrl: 'webapp/themes/html/shared/assistedmembersConnects.html', size: 'lg', controller: 'kalgudiProgramsContacts', resolve: { 'selectedSubscriber': $scope.selectedSubscriber } }); modalInstance.result.then(function (selectedConnects) { console.log(selectedConnects); $scope.selectedSubscriber = selectedConnects; $scope.selectedSubscriberText = selectedConnects.firstName; $scope.products = $scope.selectedSubscriber.lstOfUserBusinessDetailsInfo[0].lstOfProducts; $scope.products = _.filter($scope.products, function (product) { return product.productId != "000000037555" }); $scope.selectSubscriber(); //$scope.connectsListForPost(selectedConnects); }, function () { }); }; $scope.uploadImages = function (uploadType) { var tempObj = {}; $scope.ImageHideflag = true; tempObj.uploadType = uploadType; var modalInstance = $uibModal.open({ templateUrl: $rootScope.themebasepathvalue + 'shared/uploadMultimedia.html', controller: 'uploadMultimediaController', windowClass: 'app-modal-window', size: 'lg', resolve: { items: function () { return tempObj; } } }); modalInstance.result.then(function (uploadedMedia) { console.log(uploadedMedia); if (!$scope.reportActivity.attachments) { $scope.reportActivity.attachments = uploadedMedia; } else { for (var i in uploadedMedia) { $scope.reportActivity.attachments.push(uploadedMedia[i]); } } }); } $scope.deselectImage = function (index, value) { $scope.reportActivity.attachments.splice(index, 1); } /** * Add assets of selectedSubscriber to the array */ addToProductData = function(data){ if (($scope.productData.length >= 0 && data[0].name) || data[0].name) { $scope.productData = $scope.productData.concat(data); } } /** * Calls a service to get asstes of selectedSubscriber * * @returns Promise object that made the service call. */ $scope.getAssetDetails = function() { $scope.clearReportActivity(); profileService.getAsset($scope.selectedSubscriber.profileKey, 15, 0) .then( function(response) { addToProductData(response); }, function(error) { addToProductData(getAssetList()); } ); } /** * Gets asstes of subscriber from localStorage. * * @return Returns list of assrts subscribers. */ getAssetList = function() { var localKey = 'getAsset?id=' + $scope.selectedSubscriber.profileKey; assetList = localStorage[localKey]; if(assetList) return JSON.parse(assetList); else return undefined; } } ]);