angular.module('odoo')
/**
* -----------------------------------------------------------------------------
* Description: Customer list controller
* -----------------------------------------------------------------------------
*/
.controller('CustomersController', function (
$scope,
$ionicModal,
$ionicActionSheet,
$ionicFilterBar,
customersRemoteFactory,
customersStorageFactory,
sqlFactory,
deviceFactory
) {
$scope.loading = false;
$scope.selectedIndex = -1;
$scope.customers = [];
$scope.customer = {};
$scope.search = null;
$ionicModal.fromTemplateUrl('templates/sales/customer.html', {
scope: $scope
}).then(function (modal) {
$scope.modal = modal;
});
/**
*
*/
$scope.$on('$ionicView.enter', function () {
$scope.fill(false);
});
/**
*
*/
$scope.$on('$destroy', function () {
$scope.modal.remove();
});
/**
*
*/
$scope.$on('modal.hidden', function () {
$scope.customer = {};
});
/**
*
*/
$scope.$on('device.shaked', function () {
if ($scope.modal.isShown()) {
deviceFactory.getCurrentPosition(function (position) {
$scope.customer.partner_latitude = position.coords.latitude;
$scope.customer.partner_longitude = position.coords.longitude;
$scope.customer.date_localization = new Date(position.timestamp).toLocaleString();
}, function (err) {
console.log(err);
});
} else {
$scope.fill(false);
}
});
/**
*
*/
$scope.toogleNew = function () {
$scope.modal.show();
}
/**
*
*/
$scope.toogleSearch = function () {
$scope.search = $ionicFilterBar.show({
items: $scope.customers,
update: function (filtered, text) {
$scope.customers = filtered;
}
});
}
/**
*
*/
$scope.fill = function (refresh) {
$scope.loading = !refresh;
customersRemoteFactory.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;
deviceFactory.toast('No se ha podido cargar los datos');
});
}, function (syncErr) {
$scope.getCustomers(function () {
$scope.$broadcast('scroll.refreshComplete');
$scope.loading = false;
}, function (getCustomersError) {
$scope.$broadcast('scroll.refreshComplete');
$scope.loading = false;
deviceFactory.toast('No se ha podido cargar los datos');
});
});
}
/**
*
*/
$scope.getCustomers = function (success, error) {
sqlFactory.selectByConstraint('partner', 'customer = 1 AND modified != 2', 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, e) {
deviceFactory.vibrate();
$scope.selectedIndex = index;
if (index == -1) {
$scope.customer = {};
} else {
$scope.customer = $scope.customers[index];
}
console.log($scope.customer);
$ionicActionSheet.show({
titleText: 'Acciones',
buttons: [
{
text: ' Abrir'
},
{
text: ' Agregar a contactos'
},
{
text: ' Ir a la ubicación'
}
],
destructiveText: ' Eliminar',
cancel: function () {
$scope.customer = {};
$scope.selectedIndex = -1;
},
buttonClicked: function (index) {
$scope.selectedIndex = -1;
switch (index) {
case 0:
$scope.toogleNew();
break;
case 1:
$scope.addContact();
break;
case 2:
$scope.navigate();
break;
default:
$scope.toogleNew();
}
return true;
},
destructiveButtonClicked: function () {
$scope.selectedIndex = -1;
$scope.delete();
return true;
}
});
}
/**
*
*/
$scope.save = function () {
customersStorageFactory.save($scope.customer, function (customerId) {
if (!$scope.customer.id) {
$scope.customer.id = customerId;
$scope.customers.push($scope.customer);
}
$scope.customer = {};
$scope.modal.hide();
console.log('Customer saved');
}, function (error) {
console.log(error);
deviceFactory.toast('No se ha podido guardar el cliente');
});
}
/**
*
*/
$scope.delete = function () {
deviceFactory.confirm('Estás seguro que quieres eliminar este cliente?', 'Confirmar', function (index) {
if (index == 1) {
customersStorageFactory.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) {
console.log(error);
deviceFactory.toast('No se ha podido eliminar el cliente');
});
}
});
}
/**
*
*/
$scope.takePicture = function () {
deviceFactory.takePicture(function (imageData) {
$scope.customer.image_small = imageData;
$scope.customer.image_medium = imageData;
}, function (error) {
console.log(error);
deviceFactory.toast('No se ha podido tomar la foto');
});
}
/**
*
*/
$scope.addContact = function () {
deviceFactory.saveContact($scope.customer, function (result) {
console.log(result);
}, function (error) {
console.log(error);
deviceFactory.toast('Este cliente no tiene contactos que guardar');
})
}
/**
*
*/
$scope.navigate = function () {
deviceFactory.navigate([
$scope.customer.partner_latitude,
$scope.customer.partner_longitude
], function (message) {
console.log(message);
}, function (err) {
console.log(err);
deviceFactory.toast('Este cliente no tiene ubicación que navegar');
})
}
});