enterFuelOrder.controller.js 8.34 KB
'use strict';

angular.module('acufuel')

.controller('enterFuelOrderController', ['$scope', '$rootScope', '$uibModal', '$filter', '$http', 'enterFuelOrderService', enterFuelOrderController]);

function enterFuelOrderController($scope, $rootScope, $uibModal, $filter, $http, enterFuelOrderService) {
	$scope.showLoader = true;

	$scope.companyList = {};

	enterFuelOrderService.getAllCompanies().then(function(result) {
		$scope.showLoader = false;
		$scope.companyList = result;
		//console.log("===company====",result)
	})

	$scope.order = {};
	$scope.dispatchOrder = {};
	$scope.dispatchOrder.fuelOrderList = [];
	$scope.order.upliftDate = '';
	//$scope.order.departingDate = '';
	$scope.selectedCompanyName = '';
	$scope.selectedCompanyId = '';
	$scope.marginId = '';
	$scope.selectedTier = '';

	$scope.getAircraft = function(company){
		$scope.selectedCompanyName = company;

		
		$scope.showLoader = true;
		for (var i = 0; i < $scope.companyList.length; i++) {
			if ($scope.companyList[i].companyName == company) {
					$scope.order.certificateType = $scope.companyList[i].certificateType;
					$scope.baseTenant = $scope.companyList[i].baseTenant;
					$scope.contractFuelVendor = $scope.companyList[i].contractFuelVendor;
					$scope.fuelerlinxCustomer = $scope.companyList[i].fuelerlinxCustomer;

				if($scope.companyList[i].margin != null && $scope.companyList[i].marginAVGAS != null){
					enterFuelOrderService.getFuelCost($scope.companyList[i].id).then(function(margins) {
						$scope.marginList = margins;
					})
				} else if ($scope.companyList[i].margin != null && $scope.companyList[i].marginAVGAS == null) {
					enterFuelOrderService.getATypeFuelPricing($scope.companyList[i].id).then(function(margins) {
						$scope.marginList = margins;
			        })
				} else if ($scope.companyList[i].margin == null && $scope.companyList[i].marginAVGAS != null) {
					enterFuelOrderService.getVTypeFuelPricing($scope.companyList[i].id).then(function(margins) {
			        	$scope.marginList = margins;
			        })
				} else if ($scope.companyList[i].margin == null && $scope.companyList[i].marginAVGAS == null) {
					enterFuelOrderService.getPapFuelPricing($scope.companyList[i].id).then(function(margins) {
			        	$scope.marginList = margins;
			        })
				}
				$scope.selectedCompanyId = $scope.companyList[i].id;
				
				if ($scope.selectedCompanyId != '') {
					enterFuelOrderService.getAircraft($scope.selectedCompanyId).then(function(aircraft) {
						$scope.aircraftList = aircraft;
					})
				}
				if($scope.companyList[i].margin == null) {
					$scope.tierList = [];
					$scope.tierList.push({
	        		  	'minTierBreak': '0', 'maxTierBreak': '∞'
					});
					$scope.showLoader = false;
				} else {
					$scope.marginId = $scope.companyList[i].margin.id;
					if ($scope.marginId != '') {
						enterFuelOrderService.getJetTiers($scope.marginId).then(function(tiers) {
			                $scope.tierList = tiers;
			                $scope.showLoader = false;
			                $scope.selectedTier = $scope.tierList[0];
			            })
					}else{
						$scope.showLoader = false;
					}
				}
			}
		}

	}

	$scope.tiervalue=function(){
		$scope.order.tierBreak=$scope.selectedTier.minTierBreak+'-'+$scope.selectedTier.maxTierBreak;
	}




	$scope.cancelOrder = function() {
		$scope.order = {};
	}
	
	
	$scope.setCost = function(cost){
		if(cost != null) {
			var obj =JSON.parse(cost);
			$scope.order.fboCost = obj.cost;
		}
	}

    
    $scope.addTotal = function(value, valueOf) {
		if (value != undefined && valueOf != undefined) {
			value = JSON.parse(value)
			$scope.order.total = Math.abs(value.papTotal) * valueOf; 
		}
	}

	$scope.dispatchFuel = function(){
		$scope.showLoader = true;
		
		$scope.order.companyId = $scope.selectedCompanyId;
		$scope.order.companyName = $scope.order.companyName; 
		var aircraftObj =JSON.parse($scope.order.aircraftName);
		$scope.order.aircraftName = aircraftObj.tail;
		$scope.order.make = aircraftObj.make;
		$scope.order.model = aircraftObj.model;
		//$scope.order.fuelOn = $scope.order.fuelOn;

		$scope.order.invoiced = $scope.order.invoiced;
		$scope.order.volume = $scope.order.volume; 
       // $scope.order.source = $scope.order.source; 
        
        $scope.order.total = $scope.order.total;

        var obj =JSON.parse($scope.order.priceQuote);
		$scope.order.priceQuote = obj.papTotal;
		$scope.order.fboCost = obj.cost;
		$scope.order.productName = obj.productName;

		$scope.order.etaTime = $scope.order.etaTime;
        $scope.order.etdTime = $scope.order.etdTime;
        $scope.order.certificateType = $scope.order.certificateType;
		var currentDate = new Date();
        var hours = currentDate.getHours();
        var min = currentDate.getMinutes();
        var sec = currentDate.getSeconds();
        if ($scope.order.upliftDate != '') {
                $scope.order.upliftDate = $scope.order.upliftDate + ' ' + hours + ':' + min + ':' + sec;
    			$scope.order.upliftDate = new Date($scope.order.upliftDate);
    			$scope.order.upliftDate = $scope.order.upliftDate.getTime();
    		}

		 
		if ($scope.order.departingDate != '') {
			$scope.order.departingDate = $scope.order.departingDate + ' ' + hours + ':' + min + ':' + sec;
			$scope.order.departingDate = new Date($scope.order.departingDate);
			$scope.order.departingDate = $scope.order.departingDate.getTime();
		}

		if($scope.order.status === null || $scope.order.status === undefined){

			$scope.order.status = "pending"; //default status

		}else {
			
			$scope.order.status = $scope.order.status;
		}
		if($scope.order.fuelOn === null || $scope.order.fuelOn ===undefined){
			$scope.order.fuelOn = "Arrival"; //default fuelon
		}else {
			$scope.order.fuelOn = $scope.order.fuelOn ;
		}

		if($scope.baseTenant && $scope.contractFuelVendor ){
			$scope.order.source = "Tenant/CAA" ;
		}else if($scope.baseTenant)
		{
			$scope.order.source = "Tenant/Base Customer";
		}else if($scope.contractFuelVendor){
			$scope.order.source = "CAA Member";
		}

		$scope.dispatchOrder.fuelOrderList.push($scope.order);
		
		enterFuelOrderService.dispathFuelOrder($scope.dispatchOrder).then(function(result) {
			$scope.showLoader = false;
			$scope.order = {};
			$scope.dispatchOrder={};
			$scope.order.source='';
			$scope.baseTenant=false;
			$scope.contractFuelVendor=false;
			toastr.success('Fuel Order Created Successfully', {
              closeButton: true
            })
		})
	}


	$scope.etaTimeList=[];
    $scope.etdTimeList =[];

	$scope.etaTimeList  = [{time:"12:00 AM"},{time:"12:30 AM"},{time:"01:00 AM"},{time:"01:30 AM"},{time:"02:00 AM"},{time:"02:30 AM"},{time:"03:00 AM"},{time:"03:30 AM"},
                      {time:"04:00 AM"},{time:"04:30 AM"},{time:"05:00 AM"},{time:"05:30 AM"},{time:"06:00 AM"},{time:"06:30 AM"},{time:"07:00 AM"},{time:"07:30 AM"},
                      {time:"08:00 AM"},{time:"08:30 AM"},{time:"09:00 AM"},{time:"09:30 AM"},{time:"10:00 AM"},{time:"10:30 AM"},{time:"11:00 AM"},{time:"11:30 AM"},
                      {time:"12:00 PM"},{time:"12:30 PM"},{time:"01:00 PM"},{time:"01:30 PM"},{time:"02:00 PM"},{time:"02:30 PM"},{time:"03:00 PM"},{time:"03:30 PM"},
                      {time:"04:00 PM"},{time:"04:30 PM"},{time:"05:00 PM"},{time:"05:30 PM"},{time:"06:00 PM"},{time:"06:30 PM"},{time:"07:00 PM"},{time:"07:30 PM"},
                      {time:"08:00 PM"},{time:"08:30 PM"},{time:"9:00 PM"},{time:"09:30 PM"},{time:"10:00 PM"},{time:"10:30 PM"},{time:"11:00 PM"},{time:"11:30 PM"},];

    $scope.etdTimeList  = [{time:"12:00 AM"},{time:"12:30 AM"},{time:"01:00 AM"},{time:"01:30 AM"},{time:"02:00 AM"},{time:"02:30 AM"},{time:"03:00 AM"},{time:"03:30 AM"},
                      {time:"04:00 AM"},{time:"04:30 AM"},{time:"05:00 AM"},{time:"05:30 AM"},{time:"06:00 AM"},{time:"06:30 AM"},{time:"07:00 AM"},{time:"07:30 AM"},
                      {time:"08:00 AM"},{time:"08:30 AM"},{time:"09:00 AM"},{time:"09:30 AM"},{time:"10:00 AM"},{time:"10:30 AM"},{time:"11:00 AM"},{time:"11:30 AM"},
                      {time:"12:00 PM"},{time:"12:30 PM"},{time:"01:00 PM"},{time:"01:30 PM"},{time:"02:00 PM"},{time:"02:30 PM"},{time:"03:00 PM"},{time:"03:30 PM"},
                      {time:"04:00 PM"},{time:"04:30 PM"},{time:"05:00 PM"},{time:"05:30 PM"},{time:"06:00 PM"},{time:"06:30 PM"},{time:"07:00 PM"},{time:"07:30 PM"},
                      {time:"08:00 PM"},{time:"08:30 PM"},{time:"9:00 PM"},{time:"09:30 PM"},{time:"10:00 PM"},{time:"10:30 PM"},{time:"11:00 PM"},{time:"11:30 PM"},];

}