123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- 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: '<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) {
-
- 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;
- }
- });
- }
- });
|