'use strict'; angular.module('acufuel') .controller('fuelOrdersController', ['$scope', '$rootScope', '$uibModal', '$filter', '$http', 'NgTableParams','fuelOrdersService','CustomersService','ViewCompanyService', fuelOrdersController]); function fuelOrdersController($scope, $rootScope, $uibModal, $filter, $http, NgTableParams,fuelOrdersService,CustomersService,ViewCompanyService) { $scope.showFuelOrderModal = false; $scope.optionSelected; $scope.orderdata = {} $scope.showLoader = false; $scope.jetShow = []; $scope.marginShow = []; $scope.jetShow[0] = true; $scope.marginShow[0] = true; $scope.data = {}; $scope.order = {}; $scope.dispatchOrder = {}; $scope.dispatchOrder.fuelOrderList = []; $scope.statusFilterOptions = []; $scope.statusFilterOptions.push({ 'id': '', 'title': 'Show All' },{ 'id': 'pending', 'title': 'Pending' },{ 'id': 'invoiced', 'title': 'Invoiced' },{ 'id': 'paid', 'title': 'Paid' },{ 'id': 'cancelled', 'title': 'Cancelled' },{ 'id': 'archived', 'title': 'Archived' } ); getAllCompanies(); function getAllCompanies(){ fuelOrdersService.getAllCompanies().then(function(result) { // console.log('log', result); $scope.companyList = result; for (var i = 0; i < $scope.companyList.length; i++) { if ($scope.companyList[i].companyContact != null) { if ($scope.companyList[i].companyContact.contactNumber != null || $scope.companyList[i].companyContact.contactNumber != undefined) { $scope.companyList[i].newContactNumber = $scope.companyList[i].companyContact.contactNumber; } } if ($scope.companyList[i].primaryContact != null) { if ($scope.companyList[i].primaryContact.firstName != null && $scope.companyList[i].primaryContact.lastName != null) { $scope.companyList[i].primaryContactName = $scope.companyList[i].primaryContact.firstName + ' ' + $scope.companyList[i].primaryContact.lastName; } } if ($scope.companyList[i].margin != null) { if ($scope.companyList[i].margin.marginName != null) { $scope.companyList[i].masterMargin = $scope.companyList[i].margin.id; } } } $scope.displayCompanyList = new NgTableParams({ page: 1, count: 10, }, { data: $scope.companyList }); $scope.showLoader = false; }) } $scope.userProfileId = JSON.parse(localStorage.getItem('userProfileId')) $scope.reset2 = function(){ $("input").val(""); $scope.removeMarginValidation(); } function colourFunction() { var myselect = document.getElementById('colorfulSelectbox'), colour = myselect.options[myselect.selectedIndex].className; myselect.style.background = colour; // console.log('colour', myselect); myselect.blur(); } //setInterval(function(){ // colourFunction(); //}, 1) $scope.attachmentFilterOptions = []; $scope.attachmentFilterOptions.push({ 'id': '', 'title': 'Show All' },{ 'id': '!null', 'title': 'Attachments' },{ 'id': 'null', 'title': 'No Attachments' } ); // $(document).ready(function() { // $scope.showLoader = true; // $('#example').DataTable(); // $scope.showLoader = false; // }); $scope.getOrders = function() { fuelOrdersService.getOrders().then(function(result) { $scope.orderdata = result; for(var i=0;i<$scope.orderdata.length;i++){ $scope.orderdata[i].upliftDateS = new Date($scope.orderdata[i].upliftDate); // var str = "" + $scope.orderdata[i].upliftDateS.getDate() + "/" + ($scope.orderdata[i].upliftDateS.getMonth() + 1) + "/" + $scope.orderdata[i].upliftDateS.getFullYear() var str = "" + ($scope.orderdata[i].upliftDateS.getMonth() + 1) + "/" + $scope.orderdata[i].upliftDateS.getDate() + "/" + $scope.orderdata[i].upliftDateS.getFullYear() // str = str.slice(4,16) $scope.orderdata[i].upliftDateString = str // console.log(str); } $scope.displayFuelOrderList = new NgTableParams({ page: 1, count: 10, }, { data: $scope.orderdata }); $(document).ready(function(){ var myselect = document.getElementsByClassName('colorfulSelectbox'); for (var i = 0; i < myselect.length; i++) { var colourIndex = $(myselect[i]).prop('selectedIndex'); colourIndex = colourIndex + 1; // console.log(colourIndex); var getColor = $('.colorfulSelectbox option:nth-child('+colourIndex+')').css('color'); $(myselect[i]).css('background-color', getColor); // console.log('colour', getColor); myselect[i].blur(); } }) }) } $scope.getOrders(); $scope.optionSelected=''; $scope.onFWSelect = function() { if($scope.optionSelected == 'dt'){ $('#demo-modal-4').css('display', 'block'); $scope.optionSelected=''; } if($scope.optionSelected == 'efo'){ $scope.showLoader = true; var fileName = "orders.csv"; var a = document.createElement("a"); document.body.appendChild(a); fuelOrdersService.exportCompany().then(function(result) { var file = new Blob([result], {type: 'application/csv'}); var fileURL = URL.createObjectURL(file); a.href = fileURL; a.download = fileName; a.click(); $scope.showLoader = false; $scope.optionSelected=''; }) } } $scope.attachmentrowid = "" $scope.attachment = function(id, value, url) { // console.log(id, value) $scope.attachmentrowid = id // $scope.attachmentdeleteid = attachdeleteid if(value == 'uploadAttachment'){ $('#demo-modal-6').css('display', 'block'); }else if(value == 'viewAttachment'){ var win = window.open(url, '_blank'); win.focus(); }else if(value == 'deleteAttachment'){ $('#delete1').css('display', 'block'); } } $scope.cancelDeleteAttachment = function() { $('#delete1').css('display', ''); } $scope.deleteAttachment = function() { $scope.showLoader = true; fuelOrdersService.deleteAttachment($scope.attachmentrowid).then(function(result) { // console.log(result, $scope.attachmentrowid) if(result.success){ toastr.success(''+result.success+'', { closeButton: true }) } }) $scope.showLoader = false; $('#delete1').css('display', ''); } $scope.saveUploadAttachment = function(attachmentData) { $scope.showLoader = true; $scope.data.media = attachmentData $scope.data.id = $scope.attachmentrowid fuelOrdersService.uploadAttachment($scope.data).then(function(result) { // console.log(result) if(result){ // console.log(result.success) toastr.success(''+"Upload Successful"+'', { closeButton: true }) } }) $scope.showLoader = false; $('#demo-modal-6').css('display', 'none'); } $scope.cancelUploadAttachment = function() { $('#demo-modal-6').css('display', 'none'); } $scope.editdata = {}; $scope.editTableRow = function(rowData){ // console.log('row data', rowData); $scope.editdata = rowData; $('#demo-modal-5').css('display', 'block'); } $scope.updateTotal = function(value, valueOf){ if(valueOf == 'v'){ $scope.editdata.total = value * $scope.editdata.invoiced }else if(valueOf == 'i'){ $scope.editdata.total = $scope.editdata.requestedVolume * value } } $scope.addTotal = function(value, valueOf){ if(valueOf == 'v'){ $scope.order.total = value * $scope.order.invoiced }else if(valueOf == 'i'){ $scope.order.total = $scope.order.volume * value } } $scope.addFuelData = function() { $scope.showLoader = true; if ($scope.order.priceQuote === null || $scope.order.priceQuote == '') { // console.log("valid"); $scope.showLoader = false; toastr.error('Please select Company and Price Quote.', { closeButton: true }) }else{ if ($scope.order.upliftDate != '') { $scope.order.upliftDate = new Date($scope.order.upliftDate); $scope.order.upliftDate = $scope.order.upliftDate.getTime(); } if ($scope.order.departingDate != '') { $scope.order.departingDate = new Date($scope.order.departingDate); $scope.order.departingDate = $scope.order.departingDate.getTime(); } // console.log($scope.order.quotePrice); var obj =JSON.parse($scope.order.priceQuote); // console.log('obj.papMargin',obj.papMargin); $scope.order.priceQuote = obj.papMargin; $scope.fuelData = {}; $scope.fuelData.aircraftName = $scope.order.aircraftName $scope.fuelData.companyName = $scope.order.companyName $scope.fuelData.departingDate = $scope.order.departingDate $scope.fuelData.fboCost = $scope.order.fboCost $scope.fuelData.id = $scope.order.id $scope.fuelData.invoiced = $scope.order.invoiced $scope.fuelData.priceQuote = $scope.order.priceQuote $scope.fuelData.volume = $scope.order.volume $scope.fuelData.source = $scope.order.source $scope.fuelData.status = $scope.order.status $scope.fuelData.tierBreak = $scope.order.tierBreak $scope.fuelData.total = $scope.order.total $scope.fuelData.upliftDate = $scope.order.upliftDate $scope.fuelData.companyId = $scope.selectedCompanyId; $scope.dispatchOrder.fuelOrderList.push($scope.fuelData); // console.log('$scope.order', $scope.dispatchOrder); fuelOrdersService.dispathFuelOrder($scope.dispatchOrder).then(function(result) { //console.log("fuel data---",$scope.fuelData) //console.log("oder data",$scope.dispatchOrder) //console.log('result', result); $scope.showLoader = false; $scope.order = {}; $scope.dispatchOrder.fuelOrderList = []; $('#demo-modal-4').css('display', ''); $scope.getOrders(); toastr.success('Fuel Order Dispatched Successfully', { closeButton: true }) }) } } $scope.updateStatus = function(row, status) { $scope.showLoader = true; $scope.fuelData = {}; $scope.fuelData.aircraftName = row.aircraftName $scope.fuelData.companyName = row.companyName $scope.fuelData.departingDate = row.departingDate $scope.fuelData.fboCost = row.fboCost $scope.fuelData.id = row.id $scope.fuelData.invoiced = row.invoiced $scope.fuelData.priceQuote = row.priceQuote $scope.fuelData.volume = row.requestedVolume $scope.fuelData.source = row.source $scope.fuelData.status = status $scope.fuelData.tierBreak = row.tierBreak $scope.fuelData.total = row.total $scope.fuelData.upliftDate = row.upliftDate $scope.dispatchOrder.fuelOrderList.push($scope.fuelData); fuelOrdersService.updateFuelOrder($scope.dispatchOrder).then(function(result) { // console.log('result', result); $scope.showLoader = false; $scope.editdata = {}; $scope.fuelData = {}; $scope.dispatchOrder.fuelOrderList = []; $('#demo-modal-5').css('display', ''); $scope.getOrders(); toastr.success('Fuel Order Updated Successfully', { closeButton: true }); }) } $scope.updateData = function() { $scope.showLoader = true; $scope.fuelData = {}; $scope.fuelData.aircraftName = $scope.editdata.aircraftName $scope.fuelData.companyName = $scope.editdata.companyName $scope.fuelData.departingDate = $scope.editdata.departingDate $scope.fuelData.fboCost = $scope.editdata.fboCost $scope.fuelData.id = $scope.editdata.id $scope.fuelData.invoiced = $scope.editdata.invoiced $scope.fuelData.priceQuote = $scope.editdata.priceQuote $scope.fuelData.volume = $scope.editdata.requestedVolume $scope.fuelData.source = $scope.editdata.source $scope.fuelData.status = $scope.editdata.status $scope.fuelData.tierBreak = $scope.editdata.tierBreak $scope.fuelData.total = $scope.editdata.total $scope.fuelData.upliftDate = $scope.editdata.upliftDate $scope.dispatchOrder.fuelOrderList.push($scope.fuelData); fuelOrdersService.updateFuelOrder($scope.dispatchOrder).then(function(result) { // console.log('result', result); $scope.showLoader = false; $scope.editdata = {}; $('#demo-modal-5').css('display', ''); $scope.getOrders(); toastr.success('Fuel Order Updated Successfully', { closeButton: true }) }) } $scope.getAircraft = function(company){ $scope.selectedCompanyName = company; //$scope.showLoader = true; // console.log("id",company) for (var i = 0; i < $scope.companyList.length; i++) { if ($scope.companyList[i].companyName == company) { if($scope.companyList[i].margin != null && $scope.companyList[i].marginAVGAS != null){ fuelOrdersService.getFuelCost($scope.companyList[i].id).then(function(margins) { $scope.marginList = margins; //console.log('$scope.marginList', $scope.marginList); }) } else if ($scope.companyList[i].margin != null || $scope.companyList[i].marginAVGAS == null) { fuelOrdersService.getATypeFuelPricing($scope.companyList[i].id).then(function(margins) { $scope.marginList = margins; }) } else if ($scope.companyList[i].margin == null || $scope.companyList[i].marginAVGAS != null) { fuelOrdersService.getVTypeFuelPricing($scope.companyList[i].id).then(function(margins) { $scope.marginList = margins; }) } $scope.selectedCompanyId = $scope.companyList[i].id; $scope.marginId = $scope.companyList[i].margin.id; if ($scope.selectedCompanyId != '') { fuelOrdersService.getAircraft($scope.selectedCompanyId).then(function(aircraft) { $scope.aircraftList = aircraft; }) } if ($scope.marginId != '') { fuelOrdersService.getJetTiers($scope.marginId).then(function(tiers) { $scope.tierList = tiers; // $scope.showLoader = false; }) }else{ // $scope.showLoader = false; } } } } $scope.setCost = function(cost){ console.log(cost); if(cost != null) { var obj =JSON.parse(cost); console.log("0bj",obj) $scope.order.fboCost = obj.cost; } } $scope.cancelData = function() { $('#demo-modal-4').css('display', ''); } $scope.canceleditdata = function() { $('#demo-modal-5').css('display', ''); } $scope.companyList = {}; fuelOrdersService.getAllCompanies().then(function(result) { $scope.showLoader = true; $scope.companyList = result; $scope.showLoader = false; }) /*Add a company services API used from customersService */ getCompanyName(); function getCompanyName(){ CustomersService.getCompanyName().then(function(result) { $scope.showLoader = true; $scope.compNameList = result; //console.log("kd",result) $scope.showLoader = false; }) } $scope.marginFilterOptions = []; CustomersService.getJetMargin($scope.userProfileId).then(function(result) { $scope.showLoader = true; $scope.jetMarginList = result; //console.log("jet margin",result,$scope.userProfileId) $scope.marginFilterOptions.push({ 'id': '', 'title': 'Show All' }); for (var i = 0; i < result.length; i++) { $scope.marginFilterOptions.push({ 'id': result[i].id, 'title': result[i].marginName }) } $scope.showLoader = false; }) CustomersService.getAvgMargin($scope.userProfileId).then(function(result) { $scope.avgsMarginList = result; // console.log('avgsMarginList',result) }) $scope.showCompanyError = false; $scope.showMarginError = false; $scope.removeValidation = function(){ $scope.showCompanyError = false; $('.companyNameInput').removeClass('customErrorInput'); if($scope.data.companyName == 'undefined' || $scope.data.companyName == '') { $('.companyNameInput').addClass('customErrorInput'); $scope.showCompanyError = true; } } $scope.removeMarginValidation = function(){ $scope.showMarginError = false; $('.marginSelectBox').removeClass('customErrorInput'); } getData(); function getData(){ $scope.showLoader = true; CustomersService.getAircraftMake().then(function(result) { $scope.aircraftMakeList = result; $scope.showLoader = false; }) } var companyData; $scope.addFirstData = function(sel, step){ $scope.showLoader = true; if($scope.data.companyName == undefined){ $scope.showCompanyError = true; $('.companyNameInput').addClass('customErrorInput'); }else if($scope.data.masterMargin == undefined){ $scope.showMarginError = true; $('.marginSelectBox').addClass('customErrorInput'); }else{ $scope.aircraftDetails = [{ 'tail':'', 'make': '', 'model': '', 'sizeId' : '', 'marginId': $scope.data.masterMargin, 'avgasMarginId': $scope.data.avgasMargin }]; $(sel).trigger('next.m.' + step); getData(); } $scope.showLoader = false; } $scope.addNew = function(){ $scope.showLoader = true; $scope.aircraftDetails.push({ 'tail':'', 'make': '', 'model': '', 'sizeId' : '', 'marginId': $scope.data.masterMargin, 'avgasMarginId': $scope.data.avgasMargin }); $scope.showLoader = false; }; $scope.aircraft = {}; $scope.getModal = function(makeId, index){ $scope.showLoader = true; //console.log(makeId) $scope.aircraft.make = makeId; //var makeId = makeId; CustomersService.getModal($scope.aircraft.make).then(function(result) { $scope.showLoader = false; $scope.aircraftDetails[index].aircraftModalList = result; //$scope.aircraftDetails[index].model = $scope.aircraftModalList[0]; }) } $scope.getSize = function(model, index){ $scope.showLoader = true; CustomersService.getAircraftSize($scope.aircraft.make, model).then(function(result) { $scope.showLoader = false; $scope.aircraftDetails[index].aircraftSizeList = result; //$scope.aircraftDetails[index].size = $scope.aircraftSizeList[0]; }) } $scope.aircraftListData = {}; $scope.addData = []; $scope.saveCompanyData = function(){ CustomersService.addCompany($scope.data).then(function(result) { $scope.accountId = result; $scope.aircraft.accountId = $scope.accountId; for(var i=0; i<$scope.aircraftDetails.length;i++){ $scope.addData.push({ 'tail': $scope.aircraftDetails[i].tail, 'make': $scope.aircraftDetails[i].make, 'model': $scope.aircraftDetails[i].model, 'sizeId' : $scope.aircraftDetails[i].sizeId, 'marginId': $scope.aircraftDetails[i].marginId, 'avgasMarginId': $scope.aircraftDetails[i].avgasMarginId }); } $scope.aircraftListData.aircraftList = $scope.addData; $scope.aircraftListData.accountId = $scope.aircraft.accountId; if($scope.aircraftListData.aircraftList[0].tail == "" || $scope.aircraftListData.aircraftList[0].make == null || $scope.aircraftListData.aircraftList[0].model == null){ $scope.aircraftListData.aircraftList = []; } CustomersService.addAircraft($scope.aircraftListData).then(function(result) { if(result != null && result.success){ toastr.success(''+result.success+'', { closeButton: true }) $('#demo-modal-3').modal('hide'); getAllCompanies(); }else{ toastr.error(''+result.statusText+'', { closeButton: true }) } }); }) } $scope.checkJetWithTail = function(tail, index){ //console.log("tail==============",tail, index) ViewCompanyService.checkJetType(tail).then(function(result) { //console.log("result",result) if(result.jetA == "true"){ $scope.jetShow[index] = false; $scope.marginShow[index] = true; }else{ $scope.jetShow[index] = true; $scope.marginShow[index] = false; } }) } $scope.sourceList = [{source:"Direct Jet-A"},{source:"Direct AVGAS 100LL"}]; $scope.data = {}; }