123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- angular.module('odoo')
- /**
- *
- */
- .controller('LeadsController', function (
- $q,
- $scope,
- $ionicModal,
- $ionicActionSheet,
- $ionicFilterBar,
- leadsRemoteFactory,
- leadsStorageFactory,
- opportunitiesStorageFactory,
- customersStorageFactory,
- sqlFactory,
- deviceFactory
- ) {
- $scope.loading = false;
- $scope.selectedIndex = -1;
- $scope.leads = [];
- $scope.lead = {};
- $scope.conversion = {
- action: 'convert',
- customer: 'create'
- };
- $scope.search = null;
- $ionicModal.fromTemplateUrl('templates/sales/lead.html', {
- scope: $scope
- }).then(function (modal) {
- $scope.leadModal = modal;
- });
- $ionicModal.fromTemplateUrl('templates/sales/leadToOpportunity.html', {
- scope: $scope
- }).then(function (modal) {
- $scope.leadToOpportunityModal = modal;
- });
- /**
- *
- */
- $scope.$on('$ionicView.enter', function () {
- $scope.fill(false);
- });
- /**
- *
- */
- $scope.$on('$destroy', function () {
- $scope.leadModal.remove();
- $scope.leadToOpportunityModal.remove();
- });
- /**
- *
- */
- $scope.$on('modal.hidden', function () {
- $scope.lead = {};
- });
-
- /**
- *
- */
- $scope.$on('device.shaked', function () {
- if (!$scope.leadModal.isShown()) {
- $scope.fill(false);
- }
- });
- /**
- *
- */
- $scope.fill = function (refresh) {
- $scope.loading = !refresh;
- leadsRemoteFactory.sync(function (leads) {
- console.log('Leads sync correctly and receive ' + leads.length + ' items');
- $scope.getLeads(function () {
- $scope.$broadcast('scroll.refreshComplete');
- $scope.loading = false;
- }, function (getLeadsErr) {
- $scope.$broadcast('scroll.refreshComplete');
- $scope.loading = false;
- deviceFactory.toast('No se pudo obtener las iniciativas');
- });
- }, function (syncErr) {
- $scope.getLeads(function () {
- $scope.$broadcast('scroll.refreshComplete');
- $scope.loading = false;
- }, function (getLeadsErr) {
- $scope.$broadcast('scroll.refreshComplete');
- $scope.loading = false;
- deviceFactory.toast('No se pudo obtener las iniciativas');
- });
- });
- }
- /**
- *
- */
- $scope.getLeads = function (success, error) {
- sqlFactory.selectByConstraint('crm_lead', "type = 'lead' AND modified != 2", function (leads) {
- $scope.leads = [];
- for (var i = 0; i < leads.length; i++) {
- $scope.leads.push(leads.item(i));
- }
- $scope.$apply();
- success();
- }, function (sqlFactoryError) {
- error(sqlFactoryError);
- });
- }
- /**
- *
- */
- $scope.toogleNew = function () {
- $scope.leadModal.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 : null;
- }));
- }, function (err) {
- defer.reject([]);
- });
-
- return defer.promise;
- }
- /**
- *
- */
- $scope.selectCustomer = function (callback) {
- $scope.lead.contact_name = callback.item.name;
- }
- /**
- *
- */
- $scope.deselectCustomer = function (callback) {
- $scope.opportunity.contact_name = null;
- }
- /**
- *
- */
- $scope.toggleSearch = function () {
- $scope.search = $ionicFilterBar.show({
- items: $scope.leads,
- update: function (filtered, text) {
- $scope.leads = filtered;
- }
- });
- }
- /**
- *
- */
- $scope.save = function () {
- leadsStorageFactory.save($scope.lead, function (leadId) {
- if (!$scope.lead.id) {
- $scope.lead.id = leadId;
- $scope.leads.push($scope.lead);
- }
- $scope.lead = {};
- $scope.leadModal.hide();
- console.log('Lead saved');
- }, function (error) {
- console.log(error);
- deviceFactory.toast('No se ha podido guardar la iniciativa');
- });
- }
- /**
- *
- */
- $scope.delete = function () {
- deviceFactory.confirm('Estás seguro que quieres eliminar esta iniciativa?', 'Confirmar', function (index) {
- if (index == 1) {
- leadsStorageFactory.remove($scope.lead, function (affected) {
- if (affected != 0) {
- var index = $scope.leads.indexOf($scope.lead);
- $scope.leads.splice(index, 1);
- $scope.lead = {};
- $scope.$apply();
- }
- }, function (error) {
- console.log(error);
- deviceFactory.toast('No se ha podido eliminar la iniciativa');
- });
- }
- });
- }
- /**
- *
- */
- $scope.convertToOpportunity = function () {
- opportunitiesStorageFactory.save($scope.lead, function (opportunityId) {
- // $scope.leadToOpportunityModal.hide();
- var index = $scope.leads.indexOf($scope.lead);
- $scope.leads.splice(index, 1);
- $scope.lead = {};
- $scope.$apply();
- deviceFactory.toast('Se convirtió la iniciativa');
- }, function (saveErr) {
- console.log(saveErr);
- // $scope.leadToOpportunityModal.hide();
- deviceFactory.toast('No se pudo convertir la iniciativa');
- });
- }
- /**
- *
- */
- $scope.openOptions = function (index) {
- deviceFactory.vibrate();
- $scope.selectedIndex = index;
- if (index == -1) {
- $scope.lead = {};
- } else {
- $scope.lead = $scope.leads[index];
- }
- console.log($scope.lead);
- $ionicActionSheet.show({
- titleText: 'Acciones',
- buttons: [
- {
- text: '<i class="icon ion-arrow-expand positive"></i> Abrir'
- },
- {
- text: '<i class="icon ion-wand positive"></i> Convertir a oportunidad'
- }
- ],
- destructiveText: '<i class="icon ion-trash-a assertive"></i> Eliminar',
- cancel: function () {
- $scope.lead = {};
- $scope.selectedIndex = -1;
- },
- buttonClicked: function (index) {
- $scope.selectedIndex = -1;
- switch (index) {
- case 0:
- $scope.toogleNew();
- break;
- case 1:
- // $scope.leadToOpportunityModal.show();
- $scope.convertToOpportunity();
- break;
- default:
- $scope.toogleNew();
- }
- return true;
- },
- destructiveButtonClicked: function () {
- $scope.selectedIndex = -1;
- $scope.delete();
- return true;
- }
- });
- }
- });
|