angular.module('odoo') /** * */ .controller('OpportunitiesController', function ( $q, $scope, $ionicPopup, $ionicModal, $stateParams, $ionicFilterBar, $ionicActionSheet, deviceFactory, crmStagesDataFactory, opportunitiesDataFactory, customersStorageFactory ) { // ======================================================================================================= $scope.loading = false; $scope.selectedIndex = -1; $scope.search = null; $scope.stages = []; $scope.stagesToMove = []; $scope.opportunity = {}; $scope.opportunities = []; $scope.groupedOpportunities = []; $scope.stage = { previous: null, current: null, next: null }; // ======================================================================================================= /** * */ $ionicModal.fromTemplateUrl('templates/sales/opportunity.html', { scope: $scope }).then(function (modal) { $scope.modal = modal; }); /** * */ $scope.$on('$ionicSlides.sliderInitialized', function (event, data) { $scope.slider = data.slider; }); /** * */ $scope.$on('$ionicSlides.slideChangeStart', function (event, data) { $scope.loading = true; }); /** * */ $scope.$on('$ionicSlides.slideChangeEnd', function (event, data) { $scope.loading = false; $scope.stageChanged(data.slider.activeIndex); }); /** * */ $scope.$on('$ionicView.enter', function () { $scope.initialize(); }); /** * */ $scope.$on('$destroy', function () { $scope.modal.remove(); }); /** * */ $scope.initialize = function () { $scope.loading = true; $scope.getStages(function (stages) { $scope.slider.updateLoop(); $scope.getOpportunities(function (opportunities) { $scope.loading = false; $scope.groupOpportunities(); $scope.$apply(); }, function (err) { $scope.loading = false; }); }, function (err) { $scope.loading = false; deviceFactory.toast('No se ha podido cargar las oportunidades'); }); } /** * */ $scope.getStages = function (success, error) { crmStagesDataFactory.sync(function (stages) { $scope.stages = stages; $scope.stageChanged(0); success(stages); }, function (syncErr) { crmStagesDataFactory.getAll(function (stages) { $scope.stages = stages; $scope.stageChanged(0); success(stages); }, function (getAllErr) { error(getAllErr); }); }); } /** * */ $scope.getOpportunities = function (success, error) { opportunitiesDataFactory.sync(function (opportunities) { $scope.opportunities = opportunities; success(opportunities); }, function (syncErr) { opportunitiesDataFactory.getAll(function (opportunities) { $scope.opportunities = opportunities; // $scope.opportunities = opportunities.filter(function (item) { // if ($routeParams.id) { // return item.partner_id == $routeParams.id; // } else { // return true; // } // }); success(opportunities); }, function (getAllErr) { console.log(getAllErr); error(getAllErr); }); }); } /** * */ $scope.groupOpportunities = function () { $scope.groupedOpportunities = $scope.opportunities.filter(function (item) { return angular.isArray(item.stage_id) ? item.stage_id[0] == $scope.stage.current.remote_id : item.stage_id == $scope.stage.current.remote_id; }); } /** * */ $scope.filterStages = function () { $scope.stagesToMove = $scope.stages.filter(function (item) { return item.id != $scope.stage.current.id; }); } /** * */ $scope.toogleNew = function () { if ($scope.stages.length > 0) { $scope.opportunity.stage_id = $scope.opportunity.stage_id || $scope.stages[0].remote_id; } $scope.modal.show(); } /** * */ $scope.getCustomersSuggestions = function (query) { var defer = $q.defer(); customersStorageFactory.getAll(function (customers) { defer.resolve(customers.filter(function (item) { return item.name.toLowerCase().indexOf(query) != -1 && item.remote_id != 0 ? item : null; })); }, function (err) { defer.reject([]); }); return defer.promise; } /** * */ $scope.selectCustomer = function (callback) { $scope.opportunity.contact_name = callback.item.name; } /** * */ $scope.deselectCustomer = function (callback) { $scope.opportunity.contact_name = null; } /** * */ $scope.toggleSearch = function () { $scope.search = $ionicFilterBar.show({ items: $scope.groupedOpportunities, update: function (filtered, text) { $scope.groupedOpportunities = filtered; } }); } /** * */ $scope.save = function () { opportunitiesDataFactory.save($scope.opportunity, function (opportunityId) { $scope.modal.hide(); console.log($scope.opportunity); if (!$scope.opportunity.id) { $scope.opportunity.id = opportunityId; $scope.opportunities.push($scope.opportunity); } $scope.groupOpportunities(); $scope.opportunity = {}; $scope.$apply(); deviceFactory.toast('Nueva opportunidad creada'); }, function (err) { console.log(err); deviceFactory.toast('No se ha podido guardar la opportunidad'); }); } /** * */ $scope.delete = function () { deviceFactory.confirm('Estás seguro que quieres eliminar ésta oportunidad?', 'Confirmar', function (index) { if (index == 1) { opportunitiesDataFactory.remove($scope.opportunity, function (affected) { if (affected != 0) { var index = $scope.opportunities.indexOf($scope.opportunity); $scope.opportunities.splice(index, 1); $scope.opportunity = {}; $scope.groupOpportunities(); $scope.$apply(); } }, function (err) { console.log(err); deviceFactory.toast('No se ha podido eliminar la oportunidad'); }); } }); } /** * Change the state */ $scope.changeStage = function (index) { switch (index) { case 0: $scope.slider.slidePrev(); break; case 1: $scope.slider.slideTo(0); break; case 2: $scope.slider.slideNext(); break; } } /** * Change stage name on title */ $scope.stageChanged = function (index) { if (!$scope.stages.length) { return; } $scope.stage.previous = index - 1 >= 0 ? $scope.stages[index - 1] : $scope.stages[index]; $scope.stage.current = $scope.stages[index]; $scope.stage.next = index + 1 <= $scope.stages.length ? $scope.stages[index + 1] : $scope.stages[index]; $scope.groupOpportunities(); if (!$scope.$$phase) { $scope.$apply(); } } /** * Show availables stages to move */ $scope.showStagesToMove = function () { $scope.filterStages(); $scope.selectStage = $ionicPopup.show({ scope: $scope, title: 'Mover a', templateUrl: 'templates/sales/crm-stages.html', }); } /** * Move opportunity to stage */ $scope.moveToStage = function (index) { $scope.selectStage.close(); var opportunityToMove = $scope.groupedOpportunities[$scope.selectedIndex]; var indexToUpdate = $scope.opportunities.indexOf(opportunityToMove); var stageToMove = $scope.stagesToMove[index]; $scope.selectedIndex = -1; $scope.loading = true; opportunitiesDataFactory.changeStage(opportunityToMove, stageToMove.remote_id, function (opportunityId) { $scope.opportunities[indexToUpdate] = opportunityToMove; $scope.groupOpportunities(); $scope.$apply(); $scope.loading = false; deviceFactory.toast('Se movió la oportunidad a ' + $scope.stagesToMove[index].name); }, function (err) { $scope.loading = false; deviceFactory.toast('No se ha podido mover la oportunidad'); }); } /** * Open the actionsheet action options */ $scope.openOptions = function (index) { deviceFactory.vibrate(); $scope.selectedIndex = index; if (index == -1) { $scope.opportunity = {}; } else { $scope.opportunity = $scope.groupedOpportunities[index]; } console.log($scope.opportunity); $ionicActionSheet.show({ titleText: 'Acciones', buttons: [ { text: ' Abrir' }, { text: ' Mover a' } ], destructiveText: ' Eliminar', cancel: function () { $scope.customer = {}; $scope.selectedIndex = -1; }, buttonClicked: function (index) { switch (index) { case 0: $scope.selectedIndex = -1; $scope.toogleNew(); break; case 1: $scope.showStagesToMove(); break; } return true; }, destructiveButtonClicked: function () { $scope.selectedIndex = -1; $scope.delete(); return true; } }); } });