Browse Source

stages storage sync added and dinamically loaded on opportunity slides view

robert2206 8 years ago
parent
commit
fbf379238f

+ 1 - 0
www/index.html

@@ -42,6 +42,7 @@
     <script src="js/factories/sales/lead.storage.factory.js"></script>
     <script src="js/factories/sales/lead.sync.factory.js"></script>
     <script src="js/factories/sales/crm.stage.storage.factory.js"></script>
+    <script src="js/factories/sales/crm.stage.sync.factory.js"></script>
     <script src="js/factories/sales/opportunity.storage.factory.js"></script>
     <script src="js/factories/sales/opportunity.sync.factory.js"></script>
 

+ 26 - 29
www/js/controllers/opportunity.controller.js

@@ -2,33 +2,11 @@ angular.module('odoo')
 
 .controller('OpportunitiesController', function (
     $scope,
-    $ionicActionSheet
+    $ionicActionSheet,
+    crmStagesDataFactory
 ) {
 
-    $scope.stages = [
-                        {
-                            title: 'Nuevo'
-                        },
-                        {
-                            title: 'Muerta'
-                        },
-                        {
-                            title: 'Calificación'
-                        },
-                        {
-                            title: 'Propuesta'
-                        },
-                        {
-                            title: 'Negociación'
-                        },
-                        {
-                            title: 'Ganado'
-                        },
-                        {
-                            title: 'Perdido'
-                        }
-                    ];
-
+    $scope.stages = [];
 
     $scope.opportunities = [
                                 {
@@ -58,7 +36,7 @@ angular.module('odoo')
 
                             ];
 
-    $scope.title = 'Nuevo';
+    $scope.title = '';
     $scope.loading = false;
     $scope.selected = -1;
 
@@ -67,10 +45,22 @@ angular.module('odoo')
     });
 
     $scope.$on("$ionicSlides.slideChangeEnd", function (event, data) {
-        console.log($scope.slider);
         $scope.stageChanged(data.slider.activeIndex);
     });
 
+    $scope.$on("$ionicView.enter", function () {
+        $scope.getStages();
+    });
+
+    $scope.getStages = function () {
+        crmStagesDataFactory.sync(function (stages) {
+            $scope.stages = stages;
+            $scope.stageChanged(0);
+        }, function (syncErr) {
+            console.log(syncErr);
+        });
+    }
+
     /**
      * Change the state
      */
@@ -88,8 +78,15 @@ angular.module('odoo')
      * Change stage name on title
      */
     $scope.stageChanged = function (index) {
-        $scope.title = $scope.stages[index].title;
-        $scope.$apply();
+        if (!$scope.stages.length) {
+            return;
+        }
+
+        $scope.title = $scope.stages[index].name;
+
+        if(!$scope.$$phase) {
+            $scope.$apply();
+        }
     }
 
     /**

+ 60 - 0
www/js/factories/sales/crm.stage.storage.factory.js

@@ -0,0 +1,60 @@
+angular.module('odoo')
+
+    /**
+    * -----------------------------------------------------------------------------
+    *  Description:    Local storage manager for crm stage data
+    * -----------------------------------------------------------------------------
+    */
+    .factory('crmStagesStorageFactory', function () {
+
+        // Save crm stage data to local storage
+        var save = function (crmStage, success, error) {
+            var values = [
+                crmStage.remote_id ? crmStage.remote_id : 0,
+                crmStage.name ? crmStage.name : ''
+            ];
+
+            var sql = null;
+
+            if (crmStage.id) {
+                values.push(crmStage.id);
+
+                sql = 'UPDATE crm_stage SET ' +
+                    'remote_id = ?,' +
+                    'modified = 1' +
+                    'modified_date = CURRENT_TIMESTAMP' +
+                    'name = ? ' +
+                    'WHERE id = ?';
+            } else {
+                sql = 'INSERT INTO crm_stage(' +
+                    'remote_id,' +
+                    'modified,' +
+                    'name) ' +
+                    'VALUES (?, 0, ?)';
+            }
+
+            db.executeSql(sql, values, function (result) {
+                success(sql.startsWith('INSERT') ? result.insertId : lead.id);
+            }, function (err) {
+                error(err);
+            });
+        }
+
+        /**
+         * Remove all crm stages from local storage
+         */
+        var removeAll = function (success, error) {
+            var sql = 'DELETE FROM crm_stage';
+
+            db.executeSql(sql, [], function(result) {
+                success(result.rowsAffected);
+            }, function(err) {
+                error(err);
+            });
+        }
+
+        return {
+            save: save,
+            removeAll: removeAll
+        }
+    });

+ 102 - 0
www/js/factories/sales/crm.stage.sync.factory.js

@@ -0,0 +1,102 @@
+angular.module('odoo')
+
+    /**
+     * -----------------------------------------------------------------------------
+     *  Description:    Local storage manager for crm stage data
+     * -----------------------------------------------------------------------------
+     */
+    .factory('crmStagesDataFactory', function (
+        crmStagesStorageFactory,
+        odooInteroperabilityFactory,
+        configFactory,
+        sqlFactory,
+        asyncLoopFactory
+    ) {
+        
+        /**
+         *
+         */
+        var pull = function (id, success, error) {
+            configFactory(function (configuration) {
+                if (id) {
+
+                    odooInteroperabilityFactory.read('crm.case.stage', [['id', '=', id]], configuration, function (response) {
+                        success(response);
+                    }, function (odooErr) {
+                        error(odooErr);
+                    });
+
+                } else {
+
+                    odooInteroperabilityFactory.read('crm.case.stage', [], configuration, function (response) {
+                        success(response);
+                    }, function (odooErr) {
+                        error(odooErr)
+                    });
+
+                }
+            }, function (configErr) {
+                error(configErr);
+            });
+        }
+
+        /**
+         *
+         */
+        var push = function (id, data, success, error) {
+            // TODO
+        }   
+        
+        /**
+         *
+         */
+        var destroy = function (id, success, error) {
+            // TODO
+        }
+
+        /**
+         *
+         */
+        var getAll = function (success, error) {
+            sqlFactory.select('crm_stage', function (stages) {
+                success(stages);
+            }, function (selectErr) {
+                error(selectErr);
+            });
+        }
+
+        /**
+         *
+         */
+        var sync = function (success, error) {
+            pull(0, function (stages) { 
+                crmStagesStorageFactory.removeAll(function () { 
+                    asyncLoopFactory(stages.length, function (loop) {
+                        var data = stages[loop.iteration()];
+
+                        crmStagesStorageFactory.save(data, function () { 
+                            loop.next();
+                        }, function (saveErr) { 
+                            loop.next();
+                        });
+                        
+                    // End loop
+                    }, function () {
+                        success(stages);
+                    });
+                }, function (removeAllErr) {
+                    error(removeAllErr);
+                });
+            }, function (pullErr) { 
+                error(pullErr);
+            });
+        }
+
+        return {
+            pull: pull,
+            push: push,
+            destroy: destroy,
+            sync: sync
+        }
+
+    });