angular.module(
'odoo.customer.controller',
[
'odoo.interoperability.factory',
'odoo.storage.factory',
'ngCordova'
]
)
.controller('CustomersController', function (
$scope,
$log,
$ionicLoading,
$ionicModal,
$ionicActionSheet,
$ionicPopup,
$cordovaContacts,
$cordovaCamera,
sync,
odoo,
storage
) {
// Objects
$scope.customer = {};
$scope.customers = [];
$ionicModal.fromTemplateUrl('templates/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
$scope.fill = function () {
$ionicLoading.show();
sync.syncCustomers(function (customers) {
console.log('Customers sync correctly and receive ' + customers.length + ' items');
$scope.getCustomers(function () {
$scope.$broadcast('scroll.refreshComplete');
$ionicLoading.hide();
}, function (getCustomersError) {
$ionicPopup.alert({ title: 'No se pudo obtener los clientes' });
});
}, function (syncCustomersError) {
console.error(syncCustomersError);
$scope.getCustomers(function () {
$scope.$broadcast('scroll.refreshComplete');
$ionicLoading.hide();
}, function (getCustomersError) {
$ionicPopup.alert({ title: 'No se pudo obtener los clientes' });
});
});
}
$scope.getCustomers = function (success, error) {
storage.getByConstraint('partner', 'customer = 1', function (customers) {
console.log(customers);
$scope.customers = [];
for (var i = 0; i < customers.length; i++) {
$scope.customers.push(customers.item(i));
}
$scope.$apply();
success();
}, function (customerGetByConstraintError) {
error(customerGetByConstraintError);
});
}
$scope.openOptions = function(index) {
if (index == -1) {
$scope.customer = {};
} else {
$scope.customer = $scope.customers[index];
}
$log.info('Customer selected => ' + JSON.stringify($scope.customer));
$ionicActionSheet.show({
titleText: 'Acciones',
buttons: [
{
text: ' Detallar'
},
{
text: ' Agregar a contactos'
}
],
destructiveText: 'Eliminar',
cancel: function() {
$scope.customer = {};
$log.info('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;
}
});
}
$scope.save = function() {
storage.saveCustomer($scope.customer, function (customerId) {
if (!$scope.customer.id) {
$scope.customer.id = customerId;
$scope.customers.push($scope.customer);
}
$scope.customer = {};
$scope.customerModal.hide();
$log.info('Customer saved');
}, function (error) {
$ionicPopup.alert({ title: 'No se ha podido guardar el cliente', template: JSON.stringify(error) });
$log.error(JSON.stringify(error));
});
}
$scope.takePicture = function () {
var options = {
quality: 75,
destinationType: Camera.DestinationType.DATA_URL,
sourceType: Camera.PictureSourceType.CAMERA,
allowEdit: true,
encodingType: Camera.EncodingType.JPEG,
targetWidth: 300,
targetHeight: 300,
popoverOptions: CameraPopoverOptions,
saveToPhotoAlbum: false,
correctOrientation:true
};
$cordovaCamera.getPicture(options).then(function(imageData) {
$scope.customer.image_small = imageData;
$scope.customer.image_medium = imageData;
}, function(err) {
$log.error(JSON.stringify(err));
});
}
$scope.delete = function () {
$ionicPopup.confirm({
title: 'Confirmar',
template: 'Estás seguro que quieres eliminar este cliente?'
}).then(function (confirmation) {
if(confirmation) {
storage.deleteCustomer($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) });
$log.error(JSON.stringify(error));
});
}
});
}
$scope.show = function () {
$scope.customerModal.show();
}
$scope.addContact = function () {
if(!$scope.customer.mobile && !$scope.customer.phone && !$scope.customer.email) {
$ionicPopup.alert({ title: 'No se puede guardar el contacto', template: 'El cliente no tiene registrado un teléfono, celular y email'});
return;
}
var contact = {
name: {
givenName: $scope.customer.name,
familyName: '',
formatted: ''
},
nickname: '',
phoneNumbers: [
{
value: $scope.customer.phone,
type: 'phone'
},
{
value: $scope.customer.mobile,
type: 'mobile'
}
],
emails: [
{
value: $scope.customer.email,
type: 'home'
}
],
addresses: [
{
type: 'home',
formatted: '',
streetAddress: $scope.customer.street,
locality: $scope.customer.city,
region: '',
postalCode: '',
country: 'Paraguay'
}
],
ims: null,
organizations: null,
birthday: null,
note: null,
photos: null,
categories: null,
urls: null
};
$cordovaContacts.save(contact).then(function (result) {
$ionicPopup.alert({ title: 'Contacto creado con éxito' });
}, function (err) {
$ionicPopup.alert({ title: 'No se puedo eliminar el cliente', template: JSON.stringify(error) });
$log.error(JSON.stringify(err));
});
}
});