Jelajahi Sumber

shake event added for cool manipulating some apps functions like gps locations and data updates

robert 8 tahun lalu
induk
melakukan
e711d330f6

+ 3 - 1
package.json

@@ -23,7 +23,9 @@
     "ionic-plugin-keyboard",
     "cordova-plugin-camera",
     "cordova-plugin-contacts",
-    "cordova-plugin-vibration"
+    "cordova-plugin-vibration",
+    "cordova-plugin-geolocation",
+    "cordova-plugin-device-motion"
   ],
   "cordovaPlatforms": [
     "android"

+ 4 - 1
www/js/app.js

@@ -14,7 +14,8 @@ angular.module(
     .run(function (
         $ionicPlatform,
         $state,
-        $localStorage
+        $localStorage,
+        deviceFactory
     ) {
 
         $ionicPlatform.ready(function () {
@@ -37,6 +38,8 @@ angular.module(
                     $state.go('configuration');
                 }
             }
+
+            deviceFactory.detectShake();
         });
     })
 

+ 17 - 5
www/js/controllers/customer.controller.js

@@ -1,7 +1,9 @@
 angular.module('odoo')
 
     /**
-     *
+     * -----------------------------------------------------------------------------
+     *  Description:    Customer list controller
+     * -----------------------------------------------------------------------------
      */
     .controller('CustomersController', function (
         $scope,
@@ -12,15 +14,15 @@ angular.module('odoo')
         customersStorageFactory,
         sqlFactory,
         cameraFactory,
-        contactFactory
+        contactFactory,
+        deviceFactory
     ) {
         $scope.loading = false;
         $scope.customers = [];
         $scope.customer = {};
 
         $ionicModal.fromTemplateUrl('templates/sales/customer.html', {
-            scope: $scope,
-            animation: 'slide-in-up'
+            scope: $scope
         }).then(function (modal) {
             $scope.customerModal = modal;
         });
@@ -37,6 +39,16 @@ angular.module('odoo')
             $scope.customer = {};
         });
 
+        $scope.$on('device.shaked', function () { 
+            if ($scope.customerModal.isShown()) {
+                deviceFactory.getCurrentPosition(function (position) { 
+                    console.log(position);
+                }, function (err) {
+                    console.log(err);
+                });
+            }
+        });
+
         /**
          *
          */
@@ -99,7 +111,7 @@ angular.module('odoo')
                 $scope.customer = $scope.customers[index];
             }
 
-            console.log('Customer selected => ' + JSON.stringify($scope.customer));
+            console.log('Customer selected => ' + angular.toJson($scope.customer));
 
             $ionicActionSheet.show({
                 titleText: 'Acciones',

+ 6 - 19
www/js/controllers/lead.controller.js

@@ -27,15 +27,13 @@ angular.module('odoo')
 
 
         $ionicModal.fromTemplateUrl('templates/sales/lead.html', {
-            scope: $scope,
-            animation: 'slide-in-up'
+            scope: $scope
         }).then(function (modal) {
             $scope.leadModal = modal;
         });
 
         $ionicModal.fromTemplateUrl('templates/sales/leadToOpportunity.html', {
-            scope: $scope,
-            animation: 'slide-in-up'
+            scope: $scope
         }).then(function (modal) {
             $scope.leadToOpportunityModal = modal;
         });
@@ -114,26 +112,15 @@ angular.module('odoo')
          */
         $scope.save = function () {
             leadsStorageFactory.save($scope.lead, function (leadId) {
-                console.log($scope.lead);
-                
+        
                 if (!$scope.lead.id) {
                     $scope.lead.id = leadId;
                     $scope.leads.push($scope.lead);
                 }
 
-                $scope.leadModal.hide();
-                $scope.loading = true;
-                console.log($scope.lead);
-
-                leadsRemoteFactory.push($scope.lead.id, $scope.lead, function (response) {
-                    $scope.lead = {};
-                    $scope.loading = false;
-                    console.log('Lead saved');
-                }, function (pushErr) {
-                    $scope.lead = {};
-                    $scope.loading = false;
-                    console.log(pushErr);
-                });
+                $scope.lead = {};
+                $scope.loading = false;
+                console.log('Lead saved');
 
             }, function (error) {
                 $ionicPopup.alert({ title: 'No se ha podido guardar la iniciativa', template: JSON.stringify(error) });

+ 3 - 1
www/js/controllers/main.controller.js

@@ -1,5 +1,7 @@
 angular.module('odoo')
 
 .controller('MainController', function ($scope) {
-    console.log('main controller');
+    $scope.$on('device.shaked', function () { 
+        console.log('Shake move it !!!')
+    });
 });

+ 7 - 1
www/js/factories/database.factory.js

@@ -1,7 +1,9 @@
 angular.module('odoo')
 
     /**
-     *
+     * -----------------------------------------------------------------------------
+     *  Description:    Database tables automatically create
+     * -----------------------------------------------------------------------------
      */
     .factory('databaseFactory', function (
         odooFactory,
@@ -35,6 +37,10 @@ angular.module('odoo')
                         statement += key + ' NUMERIC DEFAULT 0,\n';
                     }
 
+                    if (model[key].type == 'binary') {
+                        statement += key + ' BLOB DEFAULT NULL,\n';
+                    }
+
                     if (model[key].type == 'char' || model[key].type == 'text' || model[key].type == 'datetime') {
                         statement += key + ' TEXT DEFAULT NULL,\n';
                     }

+ 45 - 56
www/js/factories/sales/customer.storage.factory.js

@@ -5,7 +5,9 @@ angular.module('odoo')
      *  Description:    Local storage manager for customers data
      * -----------------------------------------------------------------------------
      */
-    .factory('customersStorageFactory', function () {
+    .factory('customersStorageFactory', function ($localStorage) {
+
+        var configuration = $localStorage;
 
         /**
          *
@@ -16,53 +18,42 @@ angular.module('odoo')
             if (data.id) {
                 query = squel.update()
                     .table('partner')
-                    .set('remote_id', data.remote_id)
+                    .set('remote_id', data.remote_id ? data.remote_id : 0)
                     .set('modified', 1)
-                    .set('modifiedDate', 'CURRENT_TIMESTAMP', { dontQuote: true })
                     .set('name', data.name)
-                    .set('city', data.city)
-                    .set('mobile', data.mobile)
-                    .set('phone', data.phone)
-                    .set('fax', data.fax)
-                    .set('email', data.email)
-                    .set('street', data.street)
-                    .set('street2', data.street2)
-                    .set('image_medium', data.image_medium)
-                    .set('image_small', data.image_small)
-                    .set('comment', data.comment)
-                    .set('customer', data.customer)
-                    .set('employee', data.employee)
-                    .set('is_company', data.is_company)
-                    .set('debit', data.debit)
-                    .set('debit_limit', data.debit_limit)
-                    .set('opportunity_count', data.opportunity_count)
-                    .set('contracts_count', data.contracts_count)
-                    .set('journal_item_count', data.journal_item_count)
-                    .set('meeting_count', data.meeting_count)
-                    .set('phonecall_count', data.phonecall_count)
-                    .set('sale_order_count', data.sale_order_count)
-                    .set('total_invoiced', data.total_invoiced)
-                    .where('id', data.id);
+                    .set('city', data.city ? data.city : null)
+                    .set('mobile', data.mobile ? data.mobile : null)
+                    .set('phone', data.phone ? data.phone : null)
+                    .set('fax', data.fax ? data.fax : null)
+                    .set('email', data.email ? data.email : null)
+                    .set('street', data.street ? data.street : null)
+                    .set('street2', data.street2 ? data.street2 : null)
+                    .set('image_medium', data.image_medium ? data.image_medium : null)
+                    .set('image_small', data.image_small ? data.image_small : null)
+                    .set('comment', data.comment ? data.comment : null)
+                    .set('customer', 1)
+                    .set('employee', 0)
+                    .set('is_company', 0)
+                    .where('id = ?', data.id)
+                    .toParam();
             } else {
-                 query = squel.insert()
+                query = squel.insert()
                     .into('partner')
-                    .set('remote_id', data.remote_id)
-                    .set('modified', 0)
-                    .set('modifiedDate', 'CURRENT_TIMESTAMP', { dontQuote: true })
+                    .set('remote_id', data.remote_id ? data.remote_id : 0)
                     .set('name', data.name)
-                    .set('city', data.city)
-                    .set('mobile', data.mobile)
-                    .set('phone', data.phone)
-                    .set('fax', data.fax)
-                    .set('email', data.email)
-                    .set('street', data.street)
-                    .set('street2', data.street2)
-                    .set('image_medium', data.image_medium)
-                    .set('image_small', data.image_small)
-                    .set('comment', data.comment)
-                    .set('customer', data.customer)
-                    .set('employee', data.employee)
-                    .set('is_company', data.is_company)
+                    .set('city', data.city ? data.city : null)
+                    .set('mobile', data.mobile ? data.mobile : null)
+                    .set('phone', data.phone ? data.phone : null)
+                    .set('fax', data.fax ? data.fax : null)
+                    .set('email', data.email ? data.email : null)
+                    .set('street', data.street ? data.street : null)
+                    .set('street2', data.street2 ? data.street2 : null)
+                    .set('image_medium', data.image_medium ? data.image_medium : null)
+                    .set('image_small', data.image_small ? data.image_small : null)
+                    .set('comment', data.comment ? data.comment : null)
+                    .set('customer', 1)
+                    .set('employee', 0)
+                    .set('is_company', 0)
                     .set('debit', data.debit)
                     .set('debit_limit', data.debit_limit)
                     .set('opportunity_count', data.opportunity_count)
@@ -71,14 +62,18 @@ angular.module('odoo')
                     .set('meeting_count', data.meeting_count)
                     .set('phonecall_count', data.phonecall_count)
                     .set('sale_order_count', data.sale_order_count)
-                    .set('total_invoiced', data.total_invoiced);
+                    .set('total_invoiced', data.total_invoiced)
+                    .set('type', data.type ? data.type : 'contact')
+                    .set('active', data.active ? data.active : 1)
+                    .set('company_id', angular.isArray(data.company_id) ? data.company_id[0] : configuration.company_id)
+                    .set('create_uid', angular.isArray(data.create_uid) ? data.create_uid[0] : configuration.id)
+                    .toParam();
             }
 
-            query = query.toParam();
-
             db.executeSql(query.text, query.values, function (result) {
-                success(query.text.startsWith('INSERT') ? result.insertId : customer.id);
+                success(query.text.startsWith('INSERT') ? result.insertId : data.id);
             }, function (err) {
+                console.log(err);
                 error(err);
             });
         };
@@ -87,27 +82,21 @@ angular.module('odoo')
          *
          */
         var remove = function (data, success, error) {
-            if (!data.id) {
-                error('Customer cannot delete without provide an id');
-            }
-
             var query = null;
 
             if (data.remote_id) {
                 query = squel.update()
                     .table('partner')
                     .set('modified', 2)
-                    .where('id', data.id)
+                    .where('id = ?', data.id)
                     .toParam();
             } else {
                 query = squel.delete()
                     .from('partner')
-                    .where('id', data.id)
+                    .where('id = ?', data.id)
                     .toParam();
             }
 
-            query = query.toParam();
-
             db.executeSql(query.text, query.values, function (result) {
                 success(result.rowsAffected);
             }, function (err) {
@@ -121,7 +110,7 @@ angular.module('odoo')
         var removeAll = function (success, error) {
             var query = squel.delete()
                 .from('partner')
-                .where('customer', 1)
+                .where('customer = ?', 1)
                 .toParam();
 
             db.executeSql(query.text, query.values, function (result) {

+ 16 - 10
www/js/factories/sales/customer.sync.factory.js

@@ -1,7 +1,9 @@
 angular.module('odoo')
 
     /**
-     *
+     * -----------------------------------------------------------------------------
+     *  Description:    Customers data sync manager
+     * -----------------------------------------------------------------------------
      */
     .factory('customersRemoteFactory', function (
         customersStorageFactory,
@@ -42,6 +44,7 @@ angular.module('odoo')
             delete data.id;
             delete data.remote_id;
             delete data.modified;
+            delete data.notify_email;
 
             if (id) {
 
@@ -50,9 +53,9 @@ angular.module('odoo')
                         var remoteData = response[0];
 
                         var remoteDate = new Date(remoteData.__last_update);
-                        var localDate = new Date(data.modifiedDate);
+                        var localDate = new Date(data.modified_date);
 
-                        delete data.modifiedDate;
+                        delete data.modified_date;
 
                         if (localDate > remoteDate) {
 
@@ -76,7 +79,7 @@ angular.module('odoo')
 
             } else {
 
-                delete data.modifiedDate;
+                delete data.modified_date;
 
                 odooFactory.create('res.partner', data, function (response) {
                     success(response);
@@ -91,11 +94,10 @@ angular.module('odoo')
          *
          */
         var destroy = function (id, success, error) {
-
             odooFactory.unlink('res.partner', id, function (response) {
                 success(response);
-            }, function (odooInteroperabilityFactoryErr) {
-                error(odooInteroperabilityFactoryErr);
+            }, function (unlinkErr) {
+                error(unlinkErr);
             });
         }
 
@@ -148,7 +150,7 @@ angular.module('odoo')
                         loop.next();
                     });
 
-                    // End loop
+                    // end loop
                 }, function () {
                     success(updatedCustomers);
                 });
@@ -185,7 +187,6 @@ angular.module('odoo')
          */
         var downloadSyncData = function (success, error) {
             pull(null, function (customers) {
-  
                 customersStorageFactory.removeAll(function () {
                     asyncLoopFactory(customers.length, function (loop) {
                         var data = customers[loop.iteration()];
@@ -199,7 +200,7 @@ angular.module('odoo')
                             loop.next();
                         });
 
-                        // End loop
+                        // end loop
                     }, function () {
                         success(customers);
                     });
@@ -219,11 +220,16 @@ angular.module('odoo')
         var sync = function (success, error) {
             syncNewData(function () {
 
+                console.log("customers --> new data ok");
                 syncUpdatedData(function () {
 
+                    console.log("customers --> updated data ok");
                     syncDeletedData(function () {
 
+                        console.log("customers -> deleted data ok");
                         downloadSyncData(function (data) {
+
+                            console.log("customers -> download data ok");
                             success(data);
                         }, function (downloadErr) {
                             error(downloadErr);

+ 4 - 3
www/js/factories/sales/lead.storage.factory.js

@@ -1,8 +1,9 @@
 angular.module('odoo')
 
-    .factory('leadsStorageFactory', function (
-        $localStorage
-    ) {
+    /**
+     *
+     */
+    .factory('leadsStorageFactory', function ($localStorage) {
 
         var configuration = $localStorage;
 

+ 99 - 0
www/js/factories/utils.factory.js

@@ -160,6 +160,105 @@ angular.module('odoo')
         }
     })
 
+    /**
+    * -----------------------------------------------------------------------------
+    *  Description:    Native device functions manager
+    * -----------------------------------------------------------------------------
+    */
+    .factory('deviceFactory', function (
+        $timeout,
+        $rootScope,
+        $cordovaGeolocation,
+        $cordovaDeviceMotion,
+        $cordovaToast,
+        $cordovaVibration
+    ) {
+        var vibrateDuration = 100;
+
+        /**
+         *
+         */
+        var getCurrentPosition = function (success, error) {
+            var options = {
+                timeout: 10000,
+                enableHighAccuracy: false
+            }
+
+            $cordovaToast.showShortBottom('Obteniendo localización');
+            $cordovaVibration.vibrate(vibrateDuration);
+
+            $cordovaGeolocation.getCurrentPosition(options).then(function (position) {
+                $cordovaVibration.vibrate(vibrateDuration);
+                $cordovaToast.showShortBottom('Localización obtenida con éxito');
+
+                success(position);
+            }, function (err) {
+                $cordovaVibration.vibrate(vibrateDuration);
+                $cordovaToast.showLongBottom('No se pudo obtener la localización, revise si su GPS está activo');
+
+                error(err);
+            });
+        }
+
+        /**
+         *
+         */
+        var detectShake = function () {
+            var previousMeasurements = {};
+
+            var options = {
+                frequency: 100,
+                deviation: 25
+            }
+
+            var watcher = null;
+
+            var startWatching = function () {
+                watcher = $cordovaDeviceMotion.watchAcceleration(options);
+
+                watcher.then(null, function (err) {
+                    console.log(err);
+                }, function (currentMeasurements) {
+                    evaluateShake(currentMeasurements);
+                });
+            }
+
+            var evaluateShake = function (currentMeasurements) {
+                var measurements = {};
+
+                if (previousMeasurements.x) {
+                    measurements.x = Math.abs(previousMeasurements.x, currentMeasurements.x);
+                    measurements.y = Math.abs(previousMeasurements.y, currentMeasurements.y);
+                    measurements.z = Math.abs(previousMeasurements.x, currentMeasurements.z);
+                }
+
+                if (measurements.x + measurements.y + measurements.z > options.deviation) {
+                    stopWatching();
+                    previousMeasurements = {};
+                } else {
+                    previousMeasurements = currentMeasurements;
+                }
+            }
+
+            var stopWatching = function () {
+                watcher.clearWatch();
+
+                $timeout(function () {
+                    startWatching();
+                }, 1500);
+
+                $rootScope.$broadcast('device.shaked');
+            }
+
+            startWatching();
+        }
+
+        return {
+            getCurrentPosition: getCurrentPosition,
+            detectShake: detectShake
+        }
+    })
+
     /**
      * -----------------------------------------------------------------------------
      *  Description:    Native contacts manager