浏览代码

data sync feature added

robert2206 8 年之前
父节点
当前提交
018377bb03
共有 3 个文件被更改,包括 87 次插入42 次删除
  1. 3 1
      www/js/app.js
  2. 4 3
      www/js/controllers.js
  3. 80 38
      www/js/factories.js

+ 3 - 1
www/js/app.js

@@ -28,7 +28,7 @@ angular.module('odoo', ['ionic', 'odoo.controllers'])
             // StatusBar.styleBlackTranslucent();
         }
 
-        db = window.sqlitePlugin.openDatabase({name: 'odoo.db', location: 'default'});
+        db = window.sqlitePlugin.openDatabase({ name: 'odoo.db', location: 'default' });
         db.executeSql("CREATE TABLE IF NOT EXISTS 'user' (\n\
                        'id' INTEGER NOT NULL  PRIMARY KEY AUTOINCREMENT,\n\
                        'remote_id' INTEGER DEFAULT 0,\n\
@@ -40,6 +40,8 @@ angular.module('odoo', ['ionic', 'odoo.controllers'])
         db.executeSql("CREATE TABLE IF NOT EXISTS 'partner' (\n\
                        'id' INTEGER DEFAULT NULL PRIMARY KEY AUTOINCREMENT,\n\
                        'remote_id' INTEGER DEFAULT 0,\n\
+                       'modified' INTEGER DEFAULT 0,\n\
+                       'modifiedDate' TEXT DEFAULT CURRENT_TIMESTAMP,\n\
                        'name' VARCHAR(35) DEFAULT NULL,\n\
                        'city' VARCHAR(35) DEFAULT NULL,\n\
                        'mobile' VARCHAR(20) DEFAULT NULL,\n\

+ 4 - 3
www/js/controllers.js

@@ -41,7 +41,7 @@ angular.module('odoo.controllers', ['odoo.factories', 'ngCordova'])
         $ionicLoading.show();
 
         sync.syncCustomers(function (customers) {
-            $log.info(customers);
+            console.log('Customers sync correctly and receive ' + customers.length + ' items');
 
             $scope.getCustomers(function () {
                 $scope.$broadcast('scroll.refreshComplete');
@@ -50,7 +50,7 @@ angular.module('odoo.controllers', ['odoo.factories', 'ngCordova'])
                 $ionicPopup.alert({ title: 'No se pudo obtener los clientes' });
             });
         }, function (syncCustomersError) {
-            $log.error(syncCustomersError);
+            console.error(syncCustomersError);
 
             $scope.getCustomers(function () {
                 $scope.$broadcast('scroll.refreshComplete');
@@ -63,6 +63,7 @@ angular.module('odoo.controllers', ['odoo.factories', 'ngCordova'])
 
     $scope.getCustomers = function (success, error) {
         storage.getByConstraint('partner', 'customer = 1', function (customers) {
+            console.log(customers);
             $scope.customers = [];
 
             for (var i = 0; i < customers.length; i++) {
@@ -165,7 +166,7 @@ angular.module('odoo.controllers', ['odoo.factories', 'ngCordova'])
             template: 'Estás seguro que quieres eliminar este cliente?'
         }).then(function (confirmation) {
             if(confirmation) {
-                storage.deleteCustomer($scope.customer.id, function (affected) {
+                storage.deleteCustomer($scope.customer, function (affected) {
                     if (affected != 0) {
                         var index = $scope.customers.indexOf($scope.customer);
                         $scope.customers.splice(index, 1);

+ 80 - 38
www/js/factories.js

@@ -52,7 +52,7 @@ angular.module('odoo.factories', ['xml-rpc'])
             });
         },
         read: function (model, domain, config, success, error) {
-            xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, model, 'search_read', domain]).then(function (response) {
+            xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, model, 'search_read', [domain]]).then(function (response) {
                 if (!response || response['faultCode']) {
                     error(response);
                     return;
@@ -64,7 +64,7 @@ angular.module('odoo.factories', ['xml-rpc'])
             });
         },
         create: function (model, data, config, success, error) {
-            xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, model, 'create', data]).then(function (response) {
+            xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, model, 'create', [data]]).then(function (response) {
                 if (!response || response['faultCode']) {
                     error(response);
                     return;
@@ -75,8 +75,8 @@ angular.module('odoo.factories', ['xml-rpc'])
                 error(xmlrpcError);
             });
         },
-        write: function (model, data, config, success, error) {
-            xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, model, 'write', data], function (response) {
+        write: function (model, id, data, config, success, error) {
+            xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, model, 'write', [[id], data]]).then(function (response) {
                 if (!response || response['faultCode']) {
                     error(response);
                     return;
@@ -88,7 +88,7 @@ angular.module('odoo.factories', ['xml-rpc'])
             });
         },
         unlink: function (model, id, config, success, error) {
-            xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, model, 'unlink', id], function (response) {
+            xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, model, 'unlink', [[id]]]).then(function (response) {
                 if (!response || response['faultCode']) {
                     error(response);
                     return;
@@ -125,14 +125,21 @@ angular.module('odoo.factories', ['xml-rpc'])
 .factory('sync', function (storage, odoo, util) {
     return {
         syncCustomers: function (success, error) {
+            // Get current user saved on mobile
             storage.get('user', function (users) {
                 if (users.length == 1) {
                     var userConfig = users.item(0);
 
                     // 1. Transfer all new data from mobile to server
-                    storage.getByConstraint('partner', 'remote_id = 0', function(newPartners) {
+                    storage.getByConstraint('partner', 'remote_id = 0 AND customer = 1', function(newPartners) {
                         util.asyncLoop(newPartners.length, function (loop) {
-                            var data = [newPartners.item(loop.iteration())];
+                            var data = newPartners.item(loop.iteration());
+
+                            // Avoid odoo server warning message
+                            delete data.id;
+                            delete data.remote_id;
+                            delete data.modified;
+                            delete data.modifiedDate;
 
                             odoo.create('res.partner', data, userConfig, function (response) {
                                 loop.next();
@@ -143,44 +150,75 @@ angular.module('odoo.factories', ['xml-rpc'])
                         // End loop
                         }, function() {
                             // 2. Transfer all modified data from mobile to server
-                            storage.getByConstraint('partner', '', function (modifiedPartners) {
+                            storage.getByConstraint('partner', 'remote_id != 0 AND customer = 1 AND modified = 1', function (modifiedPartners) {
                                 util.asyncLoop(modifiedPartners.length, function (loop) {
-                                    var data = [modifiedPartners.item(loop.iteration())];
+                                    var localData = modifiedPartners.item(loop.iteration());
+
+                                    odoo.read('res.partner', [['id', '=', localData.remote_id]], userConfig, function (response) {
+                                        if (response.length == 1) {
+                                            var remoteData = response[0];
+
+                                            var remoteModifiedDate = new Date(remoteData.__last_update);
+                                            var localModifiedDate = new Date(localData.modifiedDate);
 
-                                    odoo.write('res.partner', data, userConfig, function (response) {
+                                            if (localModifiedDate > remoteModifiedDate) {
+                                                var id = localData.remote_id;
+
+                                                // Avoid odoo server warning message
+                                                delete localData.id;
+                                                delete localData.remote_id;
+                                                delete localData.modified;
+                                                delete localData.modifiedDate;
+
+                                                odoo.write('res.partner', id, localData, userConfig, function (response) {
+                                                    loop.next();
+                                                }, function (odooWriteError) {
+                                                    console.error(odooWriteError);
+                                                    loop.next();
+                                                });
+                                            } else {
+                                                loop.next();
+                                            }
+                                        } else {
+                                            loop.next();
+                                        }
+                                    }, function(odooReadError) {
+                                        console.error(odooReadError);
                                         loop.next();
-                                    }, function (odooWriteError) {
-                                        loop.break();
                                     });
 
                                 // End loop
                                 }, function () {
                                     // 3. Delete server data from mobile
-                                    storage.getByConstraint('partner', '', function (deletedPartners) {
+                                    storage.getByConstraint('partner', 'remote_id != 0 AND customer = 1 AND modified = 2', function (deletedPartners) {
                                         util.asyncLoop(deletedPartners.length, function (loop) {
                                             var id = deletedPartners.item(loop.iteration()).remote_id;
 
                                             odoo.unlink('res.partner', id, userConfig, function (response) {
                                                 loop.next();
-                                            }, function(odooUnlinkError) {
-                                                loop.break();
+                                            }, function (odooUnlinkError) {
+                                                console.error(odooUnlinkError);
+                                                loop.next();
                                             });
 
                                         // End loop
                                         }, function () {
                                             // 4. Download updated data from server to mobile
-                                            odoo.read('res.partner', [[['active' = True]]], userConfig, function (updatedPartners) {
+                                            odoo.read('res.partner', [['customer', '=', true]], userConfig, function (updatedPartners) {
                                                 storage.deleteAllCustomers(function () {
-                                                    util.asyncLoop(updatedPartners.length, funtion (loop) {
-                                                        var data = updatedPartners.item(loop.iteration());
+                                                    util.asyncLoop(updatedPartners.length, function (loop) {
+                                                        var data = updatedPartners[loop.iteration()];
 
-                                                        storage.saveCustomer(data, function () {
-                                                            loop.next();
-                                                        }, function (saveCustomerError) {
-                                                            loop.break();
-                                                        })
+                                                        // Set id for save on local database
+                                                        data.remote_id = data.id;
+                                                        delete data.id;
 
-                                                    // End loop
+                                                        storage.saveCustomer(data, function (customerId) {
+                                                            loop.next();
+                                                        } ,function (saveCustomerError) {
+                                                            console.error(saveCustomerError);
+                                                            loop.next();
+                                                        });
                                                     }, function () {
                                                         success(updatedPartners);
                                                     });
@@ -217,9 +255,9 @@ angular.module('odoo.factories', ['xml-rpc'])
             var sql = '';
 
             if (customer.id) {
-                sql = `UPDATE partner SET remote_id = ${ customer.remote_id ? customer.remote_id : 0  }, name = ${ customer.name ? '"' + customer.name + '"' : null }, city = ${ customer.city ? '"' + customer.city + '"' : null }, mobile = ${ customer.mobile ? '"' + customer.mobile + '"' : null }, phone = ${ customer.phone ? '"' + customer.phone + '"' : null }, fax = ${ customer.fax ? '"' + customer.fax + '"' : null }, email = ${ customer.email ? '"' + customer.email + '"' : null }, street = ${ customer.street ? '"' + customer.street + '"' : null }, street2 = ${ customer.street2 ? '"' + customer.street2 + '"' : null }, image_medium = ${ customer.image_medium ? '"' + customer.image_medium + '"' : null }, image_small = ${ customer.image_small ? '"' + customer.image_small + '"' : null }, comment = ${ customer.comment ? '"' + customer.comment + '"' : null }, customer = ${ customer.customer ? customer.customer : 1 }, employee = ${ customer.employee ? customer.employee : 0 }, is_company = ${ customer.is_company ? customer.is_company : 0 }, debit = ${ customer.debit ? customer.debit : 0 }, debit_limit = ${ customer.debit_limit ? customer.debit_limit : 0 }, opportunity_count = ${ customer.opportunity_count ? customer.opportunity_count : 0 }, contracts_count = ${ customer.contracts_count ? customer.contracts_count : 0 }, journal_item_count = ${ customer.journal_item_count ? customer.journal_item_count : 0 }, meeting_count = ${ customer.meeting_count ? customer.meeting_count : 0 }, phonecall_count = ${ customer.phonecall_count ? customer.phonecall_count : 0 }, sale_order_count = ${ customer.sale_order_count ? customer.sale_order_count : 0 }, total_invoiced = ${ customer.total_invoiced ? customer.total_invoiced : 0 } WHERE id = ${ customer.id }`;
+                sql = `UPDATE partner SET remote_id = ${ customer.remote_id ? customer.remote_id : 0  }, modified = 1, modifiedDate = CURRENT_TIMESTAMP, name = ${ customer.name ? '"' + customer.name + '"' : null }, city = ${ customer.city ? '"' + customer.city + '"' : null }, mobile = ${ customer.mobile ? '"' + customer.mobile + '"' : null }, phone = ${ customer.phone ? '"' + customer.phone + '"' : null }, fax = ${ customer.fax ? '"' + customer.fax + '"' : null }, email = ${ customer.email ? '"' + customer.email + '"' : null }, street = ${ customer.street ? '"' + customer.street + '"' : null }, street2 = ${ customer.street2 ? '"' + customer.street2 + '"' : null }, image_medium = ${ customer.image_medium ? '"' + customer.image_medium + '"' : null }, image_small = ${ customer.image_small ? '"' + customer.image_small + '"' : null }, comment = ${ customer.comment ? '"' + customer.comment + '"' : null }, customer = ${ customer.customer ? customer.customer : 1 }, employee = ${ customer.employee ? customer.employee : 0 }, is_company = ${ customer.is_company ? customer.is_company : 0 }, debit = ${ customer.debit ? customer.debit : 0 }, debit_limit = ${ customer.debit_limit ? customer.debit_limit : 0 }, opportunity_count = ${ customer.opportunity_count ? customer.opportunity_count : 0 }, contracts_count = ${ customer.contracts_count ? customer.contracts_count : 0 }, journal_item_count = ${ customer.journal_item_count ? customer.journal_item_count : 0 }, meeting_count = ${ customer.meeting_count ? customer.meeting_count : 0 }, phonecall_count = ${ customer.phonecall_count ? customer.phonecall_count : 0 }, sale_order_count = ${ customer.sale_order_count ? customer.sale_order_count : 0 }, total_invoiced = ${ customer.total_invoiced ? customer.total_invoiced : 0 } WHERE id = ${ customer.id }`;
             } else {
-                sql = `INSERT INTO partner(remote_id, name, city, mobile, phone, fax, email, street, street2, image_medium, image_small, comment, customer, employee, is_company, debit, debit_limit, opportunity_count, contracts_count, journal_item_count, meeting_count, phonecall_count, sale_order_count, total_invoiced) VALUES (0, ${ customer.name ? '"' + customer.name + '"' : null }, ${ customer.city ? '"' + customer.city + '"' : null }, ${ customer.mobile ? '"' + customer.mobile + '"' : null }, ${ customer.phone ? '"' + customer.phone + '"' : null }, ${ customer.fax ? '"' + customer.fax + '"' : null }, ${ customer.email ? '"' + customer.email + '"' : null }, ${ customer.street ? '"' + customer.street + '"' : null }, ${ customer.street2 ? '"' + customer.street2 + '"' : null }, ${ customer.image_medium ? '"' + customer.image_medium + '"' : null }, ${ customer.image_small ? '"' + customer.image_small + '"' : null }, ${ customer.comment ? '"' + customer.comment + '"' : null }, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)`;
+                sql = `INSERT INTO partner(remote_id, name, city, mobile, phone, fax, email, street, street2, image_medium, image_small, comment, customer, employee, is_company, debit, debit_limit, opportunity_count, contracts_count, journal_item_count, meeting_count, phonecall_count, sale_order_count, total_invoiced) VALUES (${ customer.remote_id ? customer.remote_id : 0 }, ${ customer.name ? '"' + customer.name + '"' : null }, ${ customer.city ? '"' + customer.city + '"' : null }, ${ customer.mobile ? '"' + customer.mobile + '"' : null }, ${ customer.phone ? '"' + customer.phone + '"' : null }, ${ customer.fax ? '"' + customer.fax + '"' : null }, ${ customer.email ? '"' + customer.email + '"' : null }, ${ customer.street ? '"' + customer.street + '"' : null }, ${ customer.street2 ? '"' + customer.street2 + '"' : null }, ${ customer.image_medium ? '"' + customer.image_medium + '"' : null }, ${ customer.image_small ? '"' + customer.image_small + '"' : null }, ${ customer.comment ? '"' + customer.comment + '"' : null }, 1, 0, 0, ${ customer.debit ? customer.debit : 0 }, ${ customer.debit_limit ? customer.debit_limit : 0 }, ${ customer.opportunity_count ? customer.opportunity_count : 0 }, ${ customer.contracts_count ? customer.contracts_count : 0 }, ${ customer.journal_item_count ? customer.journal_item_count : 0 }, ${ customer.meeting_count ? customer.meeting_count : 0 }, ${ customer.phonecall_count ? customer.phonecall_count : 0 }, ${ customer.sale_order_count ? customer.sale_order_count : 0 }, ${ customer.total_invoiced ? customer.total_invoiced : 0 })`;
             }
 
             db.executeSql(sql, [], function(result) {
@@ -228,8 +266,20 @@ angular.module('odoo.factories', ['xml-rpc'])
                 error(err);
             });
         },
-        deleteCustomer(id, success, error) {
-            var sql = 'DELETE FROM partner WHERE id = ' + id;
+        deleteCustomer(customer, success, error) {
+            if (!customer.id) {
+                error('Customer cannot delete without provide an id');
+            }
+
+            var sql = '';
+
+            if (customer.remote_id) {
+                sql = `UPDATE partner SET modified = 2 WHERE id = ${ customer.id }`;
+            } else {
+                sql = `DELETE FROM partner WHERE id = ${ customer.id }`;
+            }
+
+            console.log(sql);
 
             db.executeSql(sql, [], function(result) {
                 success(result.rowsAffected);
@@ -238,9 +288,9 @@ angular.module('odoo.factories', ['xml-rpc'])
             });
         },
         deleteAllCustomers(success, error) {
-            var sql = 'DELETE FROM partner WHERE customer = ?;';
+            var sql = 'DELETE FROM partner WHERE customer = 1';
 
-            db.executeSql(sql, [1], function(result) {
+            db.executeSql(sql, [], function(result) {
                 success(result.rowsAffected);
             }, function(err) {
                 error(err);
@@ -283,14 +333,6 @@ angular.module('odoo.factories', ['xml-rpc'])
             }, function(err) {
                 error(err);
             });
-        },
-        resultToJSON: function(result) {
-            var data = [];
-            for (var i = 0; i < result.length; i++) {
-                data.push(result.item(i));
-            }
-
-            return data;
         }
     }
 });