mainAppModule.controller('cazareBileteCtrl', function($scope,ajaxService,browserInfoService){ $scope.jsonRequestUrl = ""; $scope.doc = { baseUrl : "", link : "" }; $scope.selectValues = { nrSerie : "Selectati nr serie", dataIntrare : "Selectati data", prevDataIntrare : "TOATE" }; $scope.selectOptions = { nrSerii : [$scope.selectValues.nrSerie], dateIntrare : [$scope.selectValues.dataIntrare] }; $scope.successSearch = null; $scope.template = { additResetableInfo : null, selectedItemLocal : { queuePosition : -1, presDate : null, nrCam : -1, nrBilet : null, serieBilet : null } }; $scope.info = { items : null, totalItemsCount : -1, totalPageNum : -1, groupSelectOptions : null, pageSelectOptions : null, crtPageGroup : -1, crtPageNum : -1, dateIntrare : null, taken : null }; $scope.additResetableInfo = { search : { resultMessage : "", activateReset : false, judet : "", nrBilet : "", serieBilet : "", cnp : "" }, status : { message : "", type : "" }, inputErrors : { count : 0, show : false, exists : false, map : {} }, isFutureChecked : browserInfoService.getCBNativeOnChValue(), selectedItems : { local : {}, server : {}, queue : [] } }; $scope.onExportExcel = function(){ $search = $scope.successSearch; $queryEntity = null; if($search != null){ $queryEntity = { column : "judet", value : $search.judet, opInLink : null, opType : 1, lads : [{ column : "nrBilet", value : $search.nrBilet, opInLink : "and", opType : 1, lads : [] },{ column : "serieBilet", value : $search.serieBilet, opInLink : "and", opType : 1, lads : [] },{ column : "cnp", value : $search.cnp, opInLink : "and", opType : 1, lads : [] }] }; } $params = { queryEntity : $queryEntity, pageNum : $scope.info.crtPageNum, pageGroup : $scope.info.crtPageGroup, addit : null }; $params = angular.toJson($params); $nrSerie = $scope.selectValues.nrSerie === "Selectati nr serie" ? "" : $scope.selectValues.nrSerie; $dataIntrare = $scope.selectValues.dataIntrare === "Selectati data" || $scope.selectValues.dataIntrare === "TOATE" ? "" : $scope.selectValues.dataIntrare; $scope.doc.link = $scope.doc.baseUrl + "¶msInfo=" + $params + "&nrSerie=" + $nrSerie + "&dataIntrare=" + $dataIntrare; }; $scope.onEnterKey = function(event){ if(event.which == 13){ var params = { type : "cauta", searchValues : null, nrSerie : $scope.selectValues.nrSerie }; if($scope.selectValues.dataIntrare != "Selectati data" && $scope.selectValues.dataIntrare != "TOATE") params.dataIntrare = $scope.selectValues.dataIntrare; $searchValues = []; $searchValues.push($scope.additResetableInfo.search.judet); $searchValues.push($scope.additResetableInfo.search.nrBilet); $searchValues.push($scope.additResetableInfo.search.serieBilet); $searchValues.push($scope.additResetableInfo.search.cnp); params.searchValues = angular.toJson($searchValues); ajaxService.sendXTR($scope.jsonRequestUrl,$.param(params)) .then(function(result){ $scope.onResolveCauta(result,angular.copy($scope.additResetableInfo.search)); }, function(status){ console.log("Eroare la request XHR trimis pentru cautare bilet :"); console.log(status); }); } }; $scope.onResolveCauta = function(result,search){ $scope.info.items = result.items; $scope.info.totalItemsCount = result.totalItemsCount; $scope.info.totalPageNum = result.totalPageNum; $scope.info.groupSelectOptions = result.groupSelectOptions; $scope.info.pageSelectOptions = result.pageSelectOptions; $scope.info.crtPageGroup = result.crtPageGroup; $scope.info.crtPageNum = result.crtPageNum; $scope.info.taken = result.taken; $scope.template.additResetableInfo.search.activateReset = true; $scope.additResetableInfo = angular.copy($scope.template.additResetableInfo); $scope.additResetableInfo.search = search; $scope.additResetableInfo.search.activateReset = true; $scope.additResetableInfo.search.resultMessage = "Numar bilete ramase : " + ($scope.info.totalItemsCount == null ? 0 : $scope.info.totalItemsCount); $scope.successSearch = angular.copy($scope.additResetableInfo.search); $("#datePickerActivator").trigger("special-change"); }; $scope.anuleazaFiltrare = function(){ $scope.info.crtPageGroup = 5; $scope.info.crtPageNum = 1; $scope.successSearch = null; $scope.template.additResetableInfo.search.activateReset = false; $scope.additResetableInfo = angular.copy($scope.template.additResetableInfo); $scope.onChangePage(); }; $scope.reseteaza = function(){ var queue = $scope.additResetableInfo.selectedItems.queue; if(queue.length > 0){ var keyOfCrtSel = queue[queue.length - 1]; var trId = keyOfCrtSel.replace(",","_") + "_tr"; $("#" + trId).removeAttr("class"); } angular.forEach($scope.info.items,function(item,key){ item.check = false; },null); var search = null; if($scope.successSearch != null) search = $scope.additResetableInfo.search; $scope.additResetableInfo = angular.copy($scope.template.additResetableInfo); if(search != null){ $scope.additResetableInfo.search = search; } $(".nr_cam_input").val(""); $(".nr_cam_input").prop("disabled",true); $(".date_picker").val(""); $(".date_picker").prop("disabled",true); }; $scope.salveaza = function(){ var params = { type : "salveaza", crtPageGroup : $scope.info.crtPageGroup, selectedItems : angular.toJson($scope.additResetableInfo.selectedItems.server), nrSerie : $scope.selectValues.nrSerie }; if($scope.selectValues.dataIntrare != "Selectati data" && $scope.selectValues.dataIntrare != "TOATE") params.dataIntrare = $scope.selectValues.dataIntrare; if($scope.successSearch != null){ params.searchValues = []; params.searchValues.push($scope.successSearch.judet); params.searchValues.push($scope.successSearch.nrBilet); params.searchValues.push($scope.successSearch.serieBilet); params.searchValues.push($scope.successSearch.cnp); params.searchValues = angular.toJson(params.searchValues); } var selectedItemsD = {}; var selectedItems = $scope.additResetableInfo.selectedItems; angular.forEach(selectedItems.local,function(item,key){ selectedItemsD[key] = { presDate : item.presDate, nrCam : item.nrCam, nrBilet : item.nrBilet, serieBilet : item.serieBilet }; },null); params.selectedItemsD = angular.toJson(selectedItemsD); ajaxService.sendXTR($scope.jsonRequestUrl,$.param(params)) .then(function(result){ $scope.onResolveSalveaza(result); }, function(status){ console.log("Eroare la request XHR pentru salvare date selectate:"); console.log(status); }); }; $scope.onResolveSalveaza = function(result){ $scope.info.items = result.items; $scope.info.totalItemsCount = result.totalItemsCount; $scope.info.totalPageNum = result.totalPageNum; $scope.info.groupSelectOptions = result.groupSelectOptions; $scope.info.pageSelectOptions = result.pageSelectOptions; $scope.info.crtPageGroup = result.crtPageGroup; $scope.info.crtPageNum = result.crtPageNum; $scope.info.taken = result.taken; var countTaken = 0; if(result.taken != null) angular.forEach(result.taken,function(item,key){ countTaken++; },null); var queueLen = $scope.additResetableInfo.selectedItems.queue.length; var search = null; if($scope.successSearch != null) search = $scope.additResetableInfo.search; $scope.additResetableInfo = angular.copy($scope.template.additResetableInfo); if(search != null){ $scope.additResetableInfo.search = search; $scope.additResetableInfo.search.resultMessage = "Numar bilete ramase : " + ($scope.info.totalItemsCount == null ? 0 : $scope.info.totalItemsCount); } if(queueLen == countTaken){ $scope.additResetableInfo.status.message = "Toate biletele selectate au fost intre timp tranzitate in alte stari"; $scope.additResetableInfo.status.type = "ERROR"; } else if(countTaken > 0){ $scope.additResetableInfo.status.message = "Salvarea a fost efectuata cu succes. Unele bilete au fost intre timp tranzitate in alte stari"; $scope.additResetableInfo.status.type = "SUCCESS"; } else if(countTaken == 0){ $scope.additResetableInfo.status.message = "Salvarea a fost efectuata cu succes pentru toate biletele selectate"; $scope.additResetableInfo.status.type = "SUCCESS"; } $("#datePickerActivator").trigger("special-change"); }; $scope.onChangePresDate = function(inputDateElem){ var id = inputDateElem.id; var jQueryElem = $("#" + id); var presDateCrtStr = jQueryElem.val(); var index = jQueryElem.parent().attr("id").split("_")[2]; var splitArray = id.split("_"); var key = splitArray[0] + "," + splitArray[1]; var presDateRef = new Date($scope.info.items[index].dataIntrare); var presDateCrt = new Date(presDateCrtStr); var selectedItems = $scope.additResetableInfo.selectedItems; $scope.$apply(function(){ if(presDateCrt < presDateRef){ if($scope.additResetableInfo.inputErrors.map[key] == undefined){ $scope.additResetableInfo.inputErrors.exists = true; $scope.additResetableInfo.inputErrors.count++; $scope.additResetableInfo.inputErrors.map[key] = { numeComplet : $scope.info.items[index].numeComplet, presDateRef : $scope.info.items[index].dataIntrare, presDateCrt : presDateCrtStr, isPresDateError : true, nrCam : selectedItems.local[key].nrCam, isNrCamError : false }; } else{ $scope.additResetableInfo.inputErrors.map[key].isPresDateError = true; $scope.additResetableInfo.inputErrors.map[key].presDateCrt = presDateCrtStr; } } else{ if($scope.additResetableInfo.inputErrors.map[key] != undefined) $scope.additResetableInfo.inputErrors.map[key].isPresDateError = false; var nrCam = selectedItems.local[key].nrCam; if(!isNaN(nrCam)){ if(parseInt(nrCam) > 0 && parseInt(nrCam) + "" == nrCam){ delete $scope.additResetableInfo.inputErrors.map[key]; var count = 0; angular.forEach($scope.additResetableInfo.inputErrors.map,function(item,key){ count++; },null); if(count == 0) $scope.additResetableInfo.inputErrors.exists = false; $scope.additResetableInfo.inputErrors.count = count; } } } $scope.additResetableInfo.selectedItems.local[key].presDate = presDateCrtStr; }); }; $scope.onChangeNrCam = function(inputNrCamElem){ var id = inputNrCamElem.id; var jQueryElem = $("#" + id); var nrCamStr = jQueryElem.val(); var splitArray = id.split("_"); var key = splitArray[0] + "," + splitArray[1]; var index = jQueryElem.parent().attr("id").split("_")[2]; var isValid = true; var selectedItems = $scope.additResetableInfo.selectedItems; $isNrCamError = false; $scope.$apply(function(){ if(!isNaN(nrCamStr)){ if(parseInt(nrCamStr) > 0 && parseInt(nrCamStr) + "" == nrCamStr){ if($scope.additResetableInfo.inputErrors.map[key] != undefined) $scope.additResetableInfo.inputErrors.map[key].isNrCamError = false; var presDateRef = new Date($scope.info.items[index].dataIntrare); var presDateCrt = new Date(selectedItems.local[key].presDate); if(presDateCrt >= presDateRef){ delete $scope.additResetableInfo.inputErrors.map[key]; var count = 0; angular.forEach($scope.additResetableInfo.inputErrors.map,function(item,key){ count++; },null); if(count == 0) $scope.additResetableInfo.inputErrors.exists = false; $scope.additResetableInfo.inputErrors.count = count; } else isValid = false; } else{ $isNrCamError = true; isValid = false; } } else{ $isNrCamError = true; isValid = false; } if(!isValid){ if($scope.additResetableInfo.inputErrors.map[key] == undefined){ $scope.additResetableInfo.inputErrors.exists = true; $scope.additResetableInfo.inputErrors.count++; $scope.additResetableInfo.inputErrors.map[key] = { numeComplet : $scope.info.items[index].numeComplet, presDateRef : $scope.info.items[index].dataIntrare, presDateCrt : selectedItems.local[key].presDate, isPresDateError : false, nrCam : nrCamStr, isNrCamError : true }; } else{ $scope.additResetableInfo.inputErrors.map[key].isNrCamError = $isNrCamError; $scope.additResetableInfo.inputErrors.map[key].nrCam = nrCamStr; } } $scope.additResetableInfo.selectedItems.local[key].nrCam = nrCamStr; }); }; $scope.onChangeGroup = function(){ var params = { type : "onChangeGroup", crtPageGroup : $scope.info.crtPageGroup, selectedItems : angular.toJson($scope.additResetableInfo.selectedItems.server), nrSerie : $scope.selectValues.nrSerie }; if($scope.selectValues.dataIntrare != "Selectati data" && $scope.selectValues.dataIntrare != "TOATE") params.dataIntrare = $scope.selectValues.dataIntrare; if($scope.successSearch != null){ params.searchValues = []; params.searchValues.push($scope.successSearch.judet); params.searchValues.push($scope.successSearch.nrBilet); params.searchValues.push($scope.successSearch.serieBilet); params.searchValues.push($scope.successSearch.cnp); params.searchValues = angular.toJson(params.searchValues); } ajaxService.sendXTR($scope.jsonRequestUrl,$.param(params)) .then(function(result){ $scope.onResolveChangePG(result); }, function(status){ console.log("Eroare la request XHR pentru date de schimbare pagina onChangeGroup :"); console.log(status); }); }; $scope.onChangePage = function(){ var params = { type : "onChangePage", crtPageGroup : $scope.info.crtPageGroup, crtPageNum : $scope.info.crtPageNum, selectedItems : angular.toJson($scope.additResetableInfo.selectedItems.server), nrSerie : $scope.selectValues.nrSerie }; if($scope.selectValues.dataIntrare != "Selectati data" && $scope.selectValues.dataIntrare != "TOATE") params.dataIntrare = $scope.selectValues.dataIntrare; if($scope.successSearch != null){ params.searchValues = []; params.searchValues.push($scope.successSearch.judet); params.searchValues.push($scope.successSearch.nrBilet); params.searchValues.push($scope.successSearch.serieBilet); params.searchValues.push($scope.successSearch.cnp); params.searchValues = angular.toJson(params.searchValues); } ajaxService.sendXTR($scope.jsonRequestUrl,$.param(params)) .then(function(result){ $scope.onResolveChangePG(result); }, function(status){ console.log("Eroare la request XHR pentru date de schimbare pagina onChangePage :"); console.log(status); }); }; $scope.onResolveChangePG = function(result){ var selectedItems = $scope.additResetableInfo.selectedItems; var queue = selectedItems.queue; $scope.info.items = result.items; angular.forEach($scope.info.items,function(item,key){ if($scope.additResetableInfo.selectedItems.server[item.idBiletSpa + "," + item.idJudetSpa] != undefined) item.check = true; },null); $scope.info.totalItemsCount = result.totalItemsCount; $scope.info.totalPageNum = result.totalPageNum; $scope.info.groupSelectOptions = result.groupSelectOptions; $scope.info.pageSelectOptions = result.pageSelectOptions; $scope.info.crtPageGroup = result.crtPageGroup; $scope.info.crtPageNum = result.crtPageNum; $scope.info.taken = result.taken; $scope.additResetableInfo.status.message = ""; $scope.additResetableInfo.status.type = ""; if($scope.info.taken != null){ angular.forEach($scope.info.taken,function(itemTaken, keyTaken){ var deleteQIndex = -1; angular.forEach(selectedItems.local,function(itemLocal, keyLocal){ if(keyLocal == keyTaken){ deleteQIndex = itemLocal.queuePosition; } },null); delete $scope.additResetableInfo.inputErrors.map[keyTaken]; delete selectedItems.local[keyTaken]; delete selectedItems.server[keyTaken]; for(var i = deleteQIndex + 1;i < queue.length;++i) selectedItems.local[queue[i]].queuePosition--; queue.splice(deleteQIndex,1); },null); var count = 0; angular.forEach($scope.additResetableInfo.inputErrors.map,function(item,key){ count++; },null); if(count == 0) $scope.additResetableInfo.inputErrors.exists = false; $scope.additResetableInfo.inputErrors.count = count; } if($scope.successSearch != null) $scope.additResetableInfo.search.resultMessage = "Numar bilete ramase : " + ($scope.info.totalItemsCount == null ? 0 : $scope.info.totalItemsCount); if(queue.length > 0){ var toEnableDPArray = []; angular.forEach(queue,function(itemAsKey,key){ this.push({ selectorPrefix : itemAsKey.replace(",","_"), presDate : selectedItems.local[itemAsKey].presDate, nrCam : selectedItems.local[itemAsKey].nrCam }); },toEnableDPArray); $("#datePickerAndLastSelActivator").val(angular.toJson({ lastSelTrId : queue[queue.length - 1].replace(",","_") + "_tr", toEnableDP : toEnableDPArray })); $("#datePickerAndLastSelActivator").trigger("special-change"); } $("#datePickerActivator").trigger("special-change"); }; $scope.onChangeCB = function(cbElement){ var id = cbElement.id; var index = id.split("_")[2]; var key = $scope.info.items[index].idBiletSpa + "," + $scope.info.items[index].idJudetSpa; var selectedItems = $scope.additResetableInfo.selectedItems; $scope.info.taken = null; $scope.additResetableInfo.status.message = ""; $scope.additResetableInfo.status.type = ""; $scope.$apply(function(){ if(selectedItems.local[key] == undefined){ if(selectedItems.queue.length > 0){ var keyOfLastSel = selectedItems.queue[selectedItems.queue.length - 1]; var trId = keyOfLastSel.replace(",","_") + "_tr"; $("#" + trId).removeAttr("class"); } selectedItems.local[key] = angular.copy($scope.template.selectedItemLocal); selectedItems.server[key] = "1"; selectedItems.queue.push(key); selectedItems.local[key].queuePosition = selectedItems.queue.length - 1; selectedItems.local[key].presDate = $scope.info.items[index].dataIntrare; selectedItems.local[key].nrCam = ""; selectedItems.local[key].nrBilet = $scope.info.items[index].nrBilet; selectedItems.local[key].serieBilet = $scope.info.items[index].serieBilet; var selectorPrefix = "#" + key.replace(",","_"); $(selectorPrefix + "_tr").attr("class","last_selected_tr"); $presDateInput = $(selectorPrefix + "_date"); $presDateInput.prop("disabled", false); $presDateInput.datepicker("setDate",new Date($scope.info.items[index].dataIntrare)); $nrCamInput = $(selectorPrefix + "_nrCam"); $nrCamInput.prop("disabled", false); $nrCamInput.val(""); $scope.additResetableInfo.inputErrors.count++; $scope.additResetableInfo.inputErrors.exists = true; $scope.additResetableInfo.inputErrors.map[key] = { numeComplet : $scope.info.items[index].numeComplet, presDateRef : $scope.info.items[index].dataIntrare, presDateCrt : $scope.info.items[index].dataIntrare, isPresDateError : false, nrCam : selectedItems.local[key].nrCam, isNrCamError : true }; }else{ var deleteQIndex = selectedItems.local[key].queuePosition; var queue = selectedItems.queue; var selectorPrefix = "#" + key.replace(",","_"); $(selectorPrefix + "_tr").removeAttr('class'); $presDateInput = $(selectorPrefix + "_date"); $presDateInput.prop("disabled", true); $presDateInput.val(""); $nrCamInput = $(selectorPrefix + "_nrCam"); $nrCamInput.prop("disabled", true); $nrCamInput.val(""); delete $scope.additResetableInfo.inputErrors.map[key]; var count = 0; angular.forEach($scope.additResetableInfo.inputErrors.map,function(item,key){ count++; },null); if(count == 0) $scope.additResetableInfo.inputErrors.exists = false; $scope.additResetableInfo.inputErrors.count = count; delete selectedItems.server[key]; delete selectedItems.local[key]; for(var i = deleteQIndex + 1;i < queue.length;++i) selectedItems.local[queue[i]].queuePosition--; queue.splice(deleteQIndex,1); if(queue.length > 0){ var keyOfPrevSel = queue[queue.length - 1]; var trId = keyOfPrevSel.replace(",","_") + "_tr"; $("#" + trId).attr("class","last_selected_tr"); } } }); }; $scope.onTrMouseClick = function(elementArray){ var id = elementArray[0].id; var splitArray = id.split("_"); var key = splitArray[0] + "," + splitArray[1]; var selectedItems = $scope.additResetableInfo.selectedItems; $scope.info.taken = null; if(selectedItems.server[key] != undefined && selectedItems.queue[selectedItems.queue.length - 1] != key){ $("#" + id).attr('class',"last_selected_tr"); var deleteQIndex = selectedItems.local[key].queuePosition; var queue = selectedItems.queue; var keyOfLastSel = queue[queue.length - 1]; var trId = keyOfLastSel.replace(",","_") + "_tr"; $("#" + trId).removeAttr("class"); for(var i = deleteQIndex + 1;i < queue.length;++i) selectedItems.local[queue[i]].queuePosition--; queue.splice(deleteQIndex,1); queue.push(key); selectedItems.local[key].queuePosition = queue.length - 1; } }; $scope.onTrMouseOver = function(trElement){ var id = trElement.id; var splitArray = id.split("_"); var key = splitArray[0] + "," + splitArray[1]; var selectedItems = $scope.additResetableInfo.selectedItems; if(selectedItems.server[key] != undefined && selectedItems.queue[selectedItems.queue.length - 1] != key){ $("#" + id).attr('class','selected_mouse_over'); } }; $scope.onTrMouseLeave = function(trElement){ var id = trElement.id; var splitArray = id.split("_"); var key = splitArray[0] + "," + splitArray[1]; var selectedItems = $scope.additResetableInfo.selectedItems; if(selectedItems.server[key] != undefined && selectedItems.queue[selectedItems.queue.length - 1] != key){ $("#" + id).removeAttr('class'); } }; $scope.retreiveNrSerii = function(){ $scope.template.additResetableInfo = angular.copy($scope.additResetableInfo); var params = { type : "nrSerii" }; ajaxService.sendXTR($scope.jsonRequestUrl,$.param(params)) .then(function(result){ $scope.onResolveNrSerii(result); }, function(status){ console.log("Eroare la request XHR pentru numar serii :"); console.log(status); }); }; $scope.onResolveNrSerii = function(result){ $scope.selectOptions.nrSerii = result; $scope.selectOptions.nrSerii[0] = $scope.selectValues.nrSerie; }; $scope.onChangeNrSerie = function(){ if($scope.selectOptions.nrSerii[0] == "Selectati nr serie") $scope.selectOptions.nrSerii.splice(0,1); var params = { type : "initTabela", nrSerie : $scope.selectValues.nrSerie }; ajaxService.sendXTR($scope.jsonRequestUrl,$.param(params)) .then(function(result){ $scope.onResolveInitTabela(result); }, function(status){ console.log("Eroare la request XHR pentru date de initializare la onChangeNrSerie :"); console.log(status); }); }; $scope.onChangeDataIntrare = function(){ if($scope.selectOptions.dateIntrare[0] == "Selectati data") $scope.selectOptions.dateIntrare.splice(0,1); if($scope.selectValues.dataIntrare != "TOATE" || $scope.selectValues.dataIntrare == "TOATE" && $scope.selectValues.prevDataIntrare != "TOATE"){ $scope.selectValues.prevDataIntrare = $scope.selectValues.dataIntrare; var params = { type : "initTabela", nrSerie : $scope.selectValues.nrSerie, dataIntrare : $scope.selectValues.dataIntrare }; ajaxService.sendXTR($scope.jsonRequestUrl,$.param(params)) .then(function(result){ $scope.onResolveInitTabela(result); }, function(status){ console.log("Eroare la request XHR pentru date de initializare onChangeDataIntrare :"); console.log(status); }); } }; $scope.onResolveInitTabela = function(result){ if($scope.successSearch != null){ $scope.successSearch = null; $scope.template.additResetableInfo.search.activateReset = false; } $scope.additResetableInfo = angular.copy($scope.template.additResetableInfo); $scope.info = result; if($scope.info.dateIntrare != null){ $scope.selectValues.prevDataIntrare = "TOATE"; $scope.selectValues.dataIntrare = "Selectati data"; $scope.selectOptions.dateIntrare = $scope.info.dateIntrare; $scope.selectOptions.dateIntrare[0] = $scope.selectValues.dataIntrare; } $("#datePickerActivator").trigger("special-change"); }; });