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