123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- angular.module('odoo')
- /**
- *
- */
- .controller('OpportunitiesController', function (
- $scope,
- $ionicPopup,
- $ionicFilterBar,
- $ionicActionSheet,
- deviceFactory,
- crmStagesDataFactory,
- opportunitiesDataFactory
- ) {
- $scope.loading = false;
- $scope.selectedIndex = -1;
- $scope.search = null;
- $scope.stages = [];
- $scope.opportunities = [];
- $scope.groupedOpportunities = [];
- $scope.stage = {
- previous: null,
- current: null,
- next: null
- };
- $scope.stageToMove = {};
- /**
- *
- */
- $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.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;
- success(opportunities);
- }, function (getAllErr) {
- error(getAllErr);
- });
- });
- }
- /**
- *
- */
- $scope.groupOpportunities = function () {
- $scope.groupedOpportunities = $scope.opportunities.filter(function (item) {
- return item.stage_id == $scope.stage.current.remote_id;
- });
- }
- /**
- *
- */
- $scope.toggleSearch = function () {
- $scope.search = $ionicFilterBar.show({
- items: $scope.groupedOpportunities,
- update: function (filtered, text) {
- $scope.groupedOpportunities = filtered;
- }
- });
- }
- /**
- * 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 () {
- $ionicPopup.show({
- scope: $scope,
- title: 'Mover a',
- templateUrl: 'templates/sales/crm-stages.html',
- buttons: [
- {
- text: 'Mover',
- type: 'button-positive',
- onTap: function (e) {
- // console.log(e);
- $scope.moveOpportunity();
- }
- },
- {
- text: 'Cancelar',
- type: 'button-default',
- onTap: function (e) {
- // e.preventDefault();
- }
- }
- ]
- });
- }
- /**
- *
- */
- $scope.selectStageToMove = function (index) {
- $scope.stageToMove = $scope.stages[index];
- }
- /**
- * Move opportunity to stage
- */
- $scope.moveOpportunity = function () {
- console.log($scope.stageToMove);
- deviceFactory.toast('Se movió la oportunidad a ' + $scope.stageToMove.name);
- }
- /**
- * Open the actionsheet action options
- */
- $scope.openOptions = function (index) {
- deviceFactory.vibrate();
- $scope.selectedIndex = index;
- console.log($scope.opportunities[index]);
- $ionicActionSheet.show({
- titleText: 'Acciones',
- buttons: [
- {
- text: '<i class="icon ion-arrow-expand positive"></i> Abrir'
- },
- {
- text: '<i class="icon ion-forward positive"></i> Mover a'
- }
- ],
- 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.show();
- break;
- case 1:
- $scope.showStagesToMove();
- break;
- default:
- $scope.show();
- }
- return true;
- },
- destructiveButtonClicked: function () {
- $scope.selectedIndex = -1;
- $scope.delete();
- return true;
- }
- });
- }
- });
|