123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- 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: '<i class="icon ion-arrow-expand positive"></i> Abrir'
- },
- {
- text: '<i class="icon ion-archive positive"></i> Agregar a contactos'
- },
- {
- text: '<i class="icon ion-navigate positive"></i> Ir a la ubicación'
- }
- ],
- destructiveText: '<i class="icon ion-trash-a assertive"></i> 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');
- })
- }
- });
|