angular.module('odoo')
.controller('CustomersController', function (
$scope,
$ionicModal,
$ionicActionSheet,
$ionicPopup,
customers,
sqlFactory,
customerStorage,
camera,
contact
) {
$scope.loading = false;
$scope.customers = [];
$scope.customer = {};
$ionicModal.fromTemplateUrl('templates/sales/customer.html', {
scope: $scope,
animation: 'slide-in-up'
}).then(function(modal) {
$scope.customerModal = modal;
});
$scope.$on('$ionicView.enter', function () {
$scope.fill();
});
$scope.$on('$destroy', function() {
$scope.customerModal.remove();
});
$scope.$on('modal.hidden', function() {
$scope.customer = {};
});
// Fill view list from data readed from local database
$scope.fill = function (refresh = false) {
$scope.loading = !refresh;
customers.sync(function (customers) {
console.log('Customers sync correctly and receive ' + customers.length + ' items');
$scope.getCustomers(function () {
$scope.$broadcast('scroll.refreshComplete');
$scope.loading = false;
}, function (getCustomersError) {
$scope.$broadcast('scroll.refreshComplete');
$scope.loading = false;
$ionicPopup.alert({ title: 'No se pudo obtener los clientes' });
});
}, function (syncCustomersError) {
console.error(syncCustomersError);
$scope.getCustomers(function () {
$scope.$broadcast('scroll.refreshComplete');
$scope.loading = false;
}, function (getCustomersError) {
$scope.$broadcast('scroll.refreshComplete');
$scope.loading = false;
$ionicPopup.alert({ title: 'No se pudo obtener los clientes' });
});
});
}
// Get filtered customers for correct data visualization
$scope.getCustomers = function (success, error) {
sqlFactory.selectByConstraint('partner', 'customer = 1 AND modified != 2', function (customers) {
$scope.customers = [];
for (var i = 0; i < customers.length; i++) {
$scope.customers.push(customers.item(i));
}
$scope.$apply();
success();
}, function (customerGetByConstraintError) {
error(customerGetByConstraintError);
});
}
// Show action sheet for customer options
$scope.openOptions = function(index) {
if (index == -1) {
$scope.customer = {};
} else {
$scope.customer = $scope.customers[index];
}
console.log('Customer selected => ' + JSON.stringify($scope.customer));
$ionicActionSheet.show({
titleText: 'Acciones',
buttons: [
{
text: ' Abrir'
},
{
text: ' Agregar a contactos'
}
],
destructiveText: ' Eliminar',
cancel: function() {
$scope.customer = {};
console.log('ActionSheet canceled');
},
buttonClicked: function(index) {
switch (index) {
case 0:
$scope.show();
break;
case 1:
$scope.addContact();
break;
default:
$scope.show();
}
return true;
},
destructiveButtonClicked: function() {
$scope.delete();
return true;
}
});
}
// Save a customer data on local database
$scope.save = function() {
customerStorage.save($scope.customer, function (customerId) {
if (!$scope.customer.id) {
$scope.customer.id = customerId;
$scope.customers.push($scope.customer);
}
$scope.customer = {};
$scope.customerModal.hide();
console.log('Customer saved');
}, function (error) {
$ionicPopup.alert({ title: 'No se ha podido guardar el cliente', template: JSON.stringify(error) });
console.log(JSON.stringify(error));
});
}
// Delete a customer from local database
$scope.delete = function () {
$ionicPopup.confirm({
title: 'Confirmar',
template: 'Estás seguro que quieres eliminar este cliente?'
}).then(function (confirmation) {
if(confirmation) {
customerStorage.remove($scope.customer, function (affected) {
if (affected != 0) {
var index = $scope.customers.indexOf($scope.customer);
$scope.customers.splice(index, 1);
$scope.customer = {};
$scope.$apply();
}
}, function (error) {
$ionicPopup.alert({ title: 'No se puedo eliminar el cliente', template: JSON.stringify(error) });
console.log(JSON.stringify(error));
});
}
});
}
// Show customer data in modal
$scope.show = function () {
$scope.customerModal.show();
}
// Take a picture from camera for customer profile
$scope.takePicture = function () {
camera.takePicture(function (imageData) {
$scope.customer.image_small = imageData;
$scope.customer.image_medium = imageData;
}, function (error) {
console.log(error);
});
}
// Add customer data contact to device mobile contacts
$scope.addContact = function () {
contact.save($scope.customer, function (result) {
$ionicPopup.alert({ title: 'Contacto guardado' });
}, function (error) {
console.log(error);
})
}
});