Skip to content

Instantly share code, notes, and snippets.

@romanvieito
Last active August 19, 2016 11:15
Show Gist options
  • Save romanvieito/980fca52fcbaf5397797b95fd832a674 to your computer and use it in GitHub Desktop.
Save romanvieito/980fca52fcbaf5397797b95fd832a674 to your computer and use it in GitHub Desktop.
Order Controller
(function () {
'use strict';
var controllerId = 'orderController';
angular.module('app.customerService')
.controller(controllerId, ['$http', '$scope', '$rootScope', 'lookupsSvc', 'parkingSvc', 'airplaneCustomerSvc', 'fboProfileSvc', 'dataInitService', '$stateParams', '$timeout', orderController]);
function orderController($http, $scope, $rootScope, lookupsSvc, parkingSvc, airplaneCustomerSvc, fboProfileSvc, dataInitService, $stateParams, $timeout) {
var vm = this;
//Augment the vm object with initialized properties
//and default values
vm = dataInitService.populateInitialData(vm, $stateParams, 'orderController');
vm.newRelationAirplaneCustomer = newRelationAirplaneCustomer;
/**
* Get necessary data when controller is loaded
*/
(function load() {
lookupsSvc.getDefault(vm.company.id).then(function (response) {
vm.services = response.data.services;
vm.oils = response.data.oils;
vm.fuelTypes = response.data.fueltype;
}, function (response) {
vm.contentError = errorLoad;
});
parkingSvc.getParking(vm.company.id).then(function (response) {
vm.locations = response.data;
}, function (response) {
vm.contentError = errorLoad;
});
fboProfileSvc.getPreferredSupplier(vm.company.id).then(function (response) {
var auxCat = _.findwhere(response.data, { active: true });
vm.cateringSuppliers.company = _.pluck(auxCat, 'company');
}, function (response) {
vm.contentError = errorLoad;
});
})();
/**
* Post relation airplane - costumer
*/
function newRelationAirplaneCustomer() {
//If exist airplane and customer -> Matched
if (vm.airplaneSelected.id && vm.customerSelected.id) {
matchedAirplaneCustomer();
}
//Else create news airplane and customer -> Matched
else {
addAirplaneCustomer();
}
}
/**
* User search airplane and customer, if exist get ids and just Matched
*/
function matchedAirplaneCustomer() {
var auxAddAirplaneCustomer = {
airplaneId: vm.airplaneSelected.id,
customerId: vm.customerSelected.id,
airplane: null,
customer: null
};
airplaneCustomerSvc.add(auxAddAirplaneCustomer).then(function (response) {
vm.newOrder.airplaneId = vm.airplaneSelected.id;
vm.newOrder.customerId = vm.customerSelected.id;
var options = {
title: "Airplane - Customer",
content: "Linked Successfully!",
color: "#739E73",
iconSmall: "fa fa-check bounce animated",
timeout: 4000
};
showSmallBox(options);
}, function (response) {
vm.contentError = errorPost;
});
}
/**
* User search airplane and customer, if don't exist added
*/
function addAirplaneCustomer() {
var airplaneId = 0,
customerId = 0,
airplane = {},
customer = {},
address,
company,
auxAddAirplaneCustomer;
if ((angular.isUndefined(vm.airplaneSelected.id) || vm.airplaneSelected.id === null)) {
airplane = vm.airplaneSelected;
} else {
airplaneId = airportCust.airplaneId;
airplane = null;
}
if ((angular.isUndefined(vm.customerSelected.id) || vm.customerSelected.id === null)) {
address = vm.address;
company = {
companyName: vm.customerSelected.companyName,
companyCreationUser: vm.user.userId,
addresses: [],
};
//TODO Beta, check requirement, just if user insert and address description
if (address.description && address.description.length > 0) {
company.addresses.push(address);
}
customer.company = company;
} else {
customerId = airportCust.customerId;
customer = null;
}
auxAddAirplaneCustomer = {
airplaneId: airplaneId,
customerId: customerId,
airplane: airplane,
customer: customer
};
airplaneCustomerSvc.add(auxAddAirplaneCustomer).then(function (response) {
vm.airplaneSelected.id = response.data.airplaneId;
vm.customerSelected.id = response.data.customerId;
var options = {
title: "Airplane - Customer",
content: "Added Successfully!",
color: "#739E73",
iconSmall: "fa fa-check bounce animated",
timeout: 4000
};
showSmallBox(options);
}, function (response) {
vm.contentError = errorPost;
});
}
function showSmallBox(options) {
$.smallBox(options);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment