Browse Source

leads fixed bugs

robert2206 8 years ago
parent
commit
3e5d551fce

+ 1 - 1
config.xml

@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <widget id="com.eiru.odoo" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
-  <name>Odoo</name>
+  <name>Odoo by Eiru</name>
   <description>
         Mobile Odoo
     </description>

+ 1 - 0
www/js/app.js

@@ -70,6 +70,7 @@ angular.module('odoo',['ionic', 'ngCordova', 'xml-rpc', 'jett.ionic.filter.bar']
                       'priority' INTEGER DEFAULT 0,\n\
                       'probability' INTEGER DEFAULT 0,\n\
                       'type' VARCHAR(20) DEFAULT NULL,\n\
+                      'user_id' INTEGER DEFAULT 0,\n\
                       'partner_id' INTEGER DEFAULT 0);");
     });
 })

+ 6 - 9
www/js/controllers/configuration.controller.js

@@ -5,11 +5,11 @@ angular.module('odoo')
     $state,
     $ionicLoading,
     $ionicPopup,
-    odoo,
-    sqlFactory,
-    userStorage
+    userStorageFactory,
+    odooInteroperabilityFactory,
+    sqlFactory
 ) {
-    $scope.config = { host: '192.168.2.102', port: 8069, database: 'odoo', username: 'admin', password: 'admin' }
+    $scope.config = { host: '192.168.43.170', port: 8069, database: 'odoo', username: 'admin', password: 'admin' }
 
     // Apply configuration for app
     $scope.configure = function () {
@@ -17,8 +17,8 @@ angular.module('odoo')
 
         sqlFactory.count('user', function (total) {
             if (total == 0) {
-                odoo.auth($scope.config, function (user) {
-                    userStorage.save(
+                odooInteroperabilityFactory.auth($scope.config, function (user) {
+                    userStorageFactory.save(
                         [
                             user.id,
                             $scope.config.host,
@@ -30,11 +30,9 @@ angular.module('odoo')
 
                             $ionicLoading.hide();
                             $state.go('app.main');
-                            console.log(newId);
                     }, function (error) {
 
                         $ionicLoading.hide();
-                        console.log(error);
                     });
                 }, function (error) {
                     $ionicLoading.hide();
@@ -52,7 +50,6 @@ angular.module('odoo')
             }
         }, function (error) {
             $ionicLoading.hide();
-            console.log(error);
         });
     }
 });

+ 10 - 11
www/js/controllers/customer.controller.js

@@ -5,11 +5,11 @@ angular.module('odoo')
     $ionicModal,
     $ionicActionSheet,
     $ionicPopup,
-    customers,
+    customersRemoteFactory,
+    customersStorageFactory,
     sqlFactory,
-    customerStorage,
-    camera,
-    contact
+    cameraFactory,
+    contactFactory
 ) {
     $scope.loading = false;
     $scope.customers = [];
@@ -38,7 +38,7 @@ angular.module('odoo')
     $scope.fill = function (refresh = false) {
         $scope.loading = !refresh;
 
-        customers.sync(function (customers) {
+        customersRemoteFactory.sync(function (customers) {
             console.log('Customers sync correctly and receive ' + customers.length + ' items');
 
             $scope.getCustomers(function () {
@@ -50,8 +50,7 @@ angular.module('odoo')
 
                 $ionicPopup.alert({ title: 'No se pudo obtener los clientes' });
             });
-        }, function (syncCustomersError) {
-            console.error(syncCustomersError);
+        }, function (syncErr) {
 
             $scope.getCustomers(function () {
                 $scope.$broadcast('scroll.refreshComplete');
@@ -129,7 +128,7 @@ angular.module('odoo')
 
     // Save a customer data on local database
     $scope.save = function() {
-        customerStorage.save($scope.customer, function (customerId) {
+        customersStorageFactory.save($scope.customer, function (customerId) {
             if (!$scope.customer.id) {
                 $scope.customer.id = customerId;
                 $scope.customers.push($scope.customer);
@@ -151,7 +150,7 @@ angular.module('odoo')
             template: 'Estás seguro que quieres eliminar este cliente?'
         }).then(function (confirmation) {
             if(confirmation) {
-                customerStorage.remove($scope.customer, function (affected) {
+                customersStorageFactory.remove($scope.customer, function (affected) {
                     if (affected != 0) {
                         var index = $scope.customers.indexOf($scope.customer);
                         $scope.customers.splice(index, 1);
@@ -174,7 +173,7 @@ angular.module('odoo')
 
     // Take a picture from camera for customer profile
     $scope.takePicture = function () {
-        camera.takePicture(function (imageData) {
+        cameraFactory.takePicture(function (imageData) {
             $scope.customer.image_small = imageData;
             $scope.customer.image_medium = imageData;
         }, function (error) {
@@ -184,7 +183,7 @@ angular.module('odoo')
 
     // Add customer data contact to device mobile contacts
     $scope.addContact = function () {
-        contact.save($scope.customer, function (result) {
+        contactFactory.save($scope.customer, function (result) {
             $ionicPopup.alert({ title: 'Contacto guardado' });
         }, function (error) {
             console.log(error);

+ 0 - 2
www/js/controllers/lead.controller.js

@@ -82,8 +82,6 @@ angular.module('odoo')
                 $scope.leads.push(leads.item(i));
             }
 
-            console.log($scope.leads);
-
             $scope.$apply();
             success();
         }, function (sqlFactoryError) {

+ 1 - 1
www/js/factories/odoo.factory.js

@@ -1,6 +1,6 @@
 angular.module('odoo')
 
-.factory('odoo', function (xmlrpc, methodCallManager) {
+.factory('odooInteroperabilityFactory', function (xmlrpc, methodCallManager) {
     return {
         auth: function (config, success, error) {
             xmlrpc.callMethod(methodCallManager.call('authenticate', config), [config.database, config.username, config.password, {}]).then(function(response) {

+ 2 - 2
www/js/factories/sales/customer.storage.factory.js

@@ -5,8 +5,8 @@ angular.module('odoo')
  *  Description:    Local storage manager for customers data
  * -----------------------------------------------------------------------------
  */
-.factory('customerStorage', function () {
-    
+.factory('customersStorageFactory', function () {
+
     // Save customer data to local storage
     var save = function (customer, success, error) {
         var sql = '';

+ 233 - 117
www/js/factories/sales/customer.sync.factory.js

@@ -1,125 +1,241 @@
 angular.module('odoo')
 
-.factory('customers', function (sqlFactory, customerStorage, asyncLoop, odoo) {
-    return {
-        sync: function (success, error) {
-            // Get current user saved on mobile
-            sqlFactory.select('user', function (users) {
-                if (users.length == 1) {
-                    var userConfig = users.item(0);
-
-                    // 1. Transfer all new data from mobile to server
-                    sqlFactory.selectByConstraint('partner', 'remote_id = 0 AND customer = 1', function(newPartners) {
-                        asyncLoop(newPartners.length, function (loop) {
-                            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();
-                            }, function (odooCreateError) {
-                                loop.break();
-                            });
+.factory('customersRemoteFactory', function (
+    customersStorageFactory,
+    odooInteroperabilityFactory,
+    configFactory,
+    sqlFactory,
+    asyncLoopFactory
+) {
+
+    // Pull server customers data
+    var pull = function (id, success, error) {
+        configFactory(function (configuration) {
+            if (id) {
+
+                odooInteroperabilityFactory.read('res.partner', [['id', '=', id]], configuration, function (response) {
+                    success(response);
+                }, function (odooErr) {
+                    error(odooErr);
+                });
+
+            } else {
+
+                odooInteroperabilityFactory.read('res.partner', [['customer', '=', true]], configuration, function (response) {
+                    success(response);
+                }, function (odooErr) {
+                    error(odooErr)
+                });
+
+            }
+        }, function (configErr) {
+            error(configErr);
+        });
+    }
 
-                        // End loop
-                        }, function() {
-                            // 2. Transfer all modified data from mobile to server
-                            sqlFactory.selectByConstraint('partner', 'remote_id != 0 AND customer = 1 AND modified = 1', function (modifiedPartners) {
-                                asyncLoop(modifiedPartners.length, function (loop) {
-                                    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);
-
-                                            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) {
-                                                    loop.next();
-                                                });
-                                            } else {
-                                                loop.next();
-                                            }
-                                        } else {
-                                            loop.next();
-                                        }
-                                    }, function(odooReadError) {
-                                        console.error(odooReadError);
-                                        loop.next();
-                                    });
-
-                                // End loop
-                                }, function () {
-                                    // 3. Delete server data from mobile
-                                    sqlFactory.selectByConstraint('partner', 'remote_id != 0 AND customer = 1 AND modified = 2', function (deletedPartners) {
-                                        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.next();
-                                            });
-
-                                        // End loop
-                                        }, function () {
-                                            // 4. Download updated data from server to mobile
-                                            odoo.read('res.partner', [['customer', '=', true]], userConfig, function (updatedPartners) {
-                                                customerStorage.removeAll(function () {
-                                                    asyncLoop(updatedPartners.length, function (loop) {
-                                                        var data = updatedPartners[loop.iteration()];
-
-                                                        // Set id for save on local database
-                                                        data.remote_id = data.id;
-                                                        delete data.id;
-
-                                                        customerStorage.save(data, function (customerId) {
-                                                            loop.next();
-                                                        } ,function (saveCustomerError) {
-                                                            console.error(saveCustomerError);
-                                                            loop.next();
-                                                        });
-                                                    }, function () {
-                                                        success(updatedPartners);
-                                                    });
-                                                }, function (deleteAllCustomersError) {
-                                                    error(deleteAllCustomersError);
-                                                });
-                                            }, function (odooReadError) {
-                                                error(odooReadError);
-                                            });
-                                        });
-                                    }, function (getDeletedPartnersError) {
-                                        error(getDeletedPartnersError);
-                                    });
-                                });
-                            }, function (getModifiedPartnersError) {
-                                error(getModifiedPartnersError);
+    // Push customers data to server
+    var push = function (id, data, success, error) {
+
+        // Avoid odoo server warning message
+        delete data.id;
+        delete data.remote_id;
+        delete data.modified;
+
+        configFactory(function (configuration) {
+            if (id) {
+
+                pull(id, function (response) {
+                    if (response.length == 1) {
+                        var remoteData = response[0];
+
+                        var remoteDate = new Date(remoteData.__last_update);
+                        var localDate = new Date(data.modifiedDate);
+
+                        delete data.modifiedDate;
+
+                        if (localDate > remoteDate) {
+
+                            odooInteroperabilityFactory.write('res.partner', id, data, configuration, function (response) {
+                                success(response);
+                            }, function (odooErr) {
+                                error(odooErr);
                             });
-                        });
-                    }, function (partnerGetByConstraintError) {
-                        error(partnerGetByConstraintError);
+
+                        } else {
+                            success(response);
+                        }
+
+                    } else {
+                        success(response);
+                    }
+
+                }, function (pullErr) {
+                    error(pullErr);
+                });
+
+            } else {
+
+                delete data.modifiedDate;
+
+                odooInteroperabilityFactory.create('res.partner', data, configuration, function (response) {
+                    success(response);
+                }, function (odooErr) {
+                    error(odooErr);
+                });
+
+            }
+        }, function (configFactoryErr) {
+            error(configFactoryErr);
+        });
+    }
+
+    // Destroy server customer data
+    var destroy = function(id, success, error) {
+        configFactory(function (configuration) {
+
+            odooInteroperabilityFactory.unlink('res.partner', id, configuration, function (response) {
+                success(response);
+            }, function (odooInteroperabilityFactoryErr) {
+                error(odooInteroperabilityFactoryErr);
+            });
+
+        }, function (configFactoryErr) {
+            error(configFactoryErr);
+        });
+    }
+
+    // Get local customer data
+    var get = function (constraint, success, error) {
+        sqlFactory.selectByConstraint('partner', constraint, function (customers) {
+            success(customers);
+        }, function (err) {
+            error(err);
+        });
+    }
+
+
+    // Send all new data from local to remote server
+    var syncNewData = function (success, error) {
+        get('remote_id = 0 AND customer = 1', function (newCustomers) {
+            asyncLoopFactory(newCustomers.length, function (loop) {
+                var data = newCustomers.item(loop.iteration());
+
+                push(null, data, function (response) {
+                    loop.next();
+                }, function (pushErr) {
+                    loop.break();
+                });
+
+            // End loop
+            }, function () {
+                success(newCustomers);
+            });
+        }, function (getErr) {
+            error(getErr);
+        });
+    }
+
+    // Update all data from server
+    var syncUpdatedData = function (success, error) {
+        get('remote_id != 0 AND customer = 1 AND modified = 1', function (updatedCustomers) {
+            asyncLoopFactory(updatedCustomers.length, function (loop) {
+                var data = updatedCustomers.item(loop.iteration());
+
+                push(data.remote_id, data, function (response) {
+                    loop.next();
+                }, function () {
+                    loop.next();
+                });
+
+            // End loop
+            }, function () {
+                success(updatedCustomers);
+            });
+        }, function (getErr) {
+            error(getErr);
+        });
+    }
+
+    // Delete all data
+    var syncDeletedData = function (success, error) {
+        get('remote_id != 0 AND customer = 1 AND modified = 2', function (deletedCustomers) {
+            asyncLoopFactory(deletedCustomers.length, function (loop) {
+                var id = deletedCustomers.item(loop.iteration()).remote_id;
+
+                destroy(id, function (response) {
+                    loop.next();
+                }, function (destroyErr) {
+                    loop.next();
+                });
+
+            // End loop
+            }, function () {
+                success(deletedCustomers);
+            });
+        }, function(getErr) {
+            error(getErr);
+        });
+    }
+
+    // Download all sync data from server
+    var downloadSyncData = function (success, error) {
+        pull(null, function (customers) {
+            console.log(customers);
+            customersStorageFactory.removeAll(function () {
+                asyncLoopFactory(customers.length, function (loop) {
+                    var data = customers[loop.iteration()];
+
+                    data.remote_id = data.id;
+                    delete data.id;
+
+                    customersStorageFactory.save(data, function (customerId) {
+                        loop.next();
+                    }, function (saveErr) {
+                        loop.next();
                     });
-                }
-            }, function(userGetError) {
-                error(userGetError);
+
+                // End loop
+                }, function () {
+                    success(customers);
+                });
+
+            }, function (removeAllErr) {
+                error(removeAllErr);
             });
-        }
+
+        }, function (pullErr) {
+            error(pullErr);
+        });
+    }
+
+    // Sync customers data between remote and local storage
+    var sync = function (success, error) {
+        syncNewData(function () {
+
+            syncUpdatedData(function () {
+
+                syncDeletedData(function () {
+
+                    downloadSyncData(function (data) {
+                        success(data);
+                    }, function (downloadErr) {
+                        error(downloadErr);
+                    });
+                }, function (syncErr) {
+                    error(syncErr);
+                });
+            }, function (syncErr) {
+                error(syncErr);
+            });
+        }, function (syncErr) {
+            error(syncErr);
+        });
+    }
+
+    return {
+        pull: pull,
+        push: push,
+        destroy: destroy,
+        sync: sync
     }
 });

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

@@ -4,15 +4,74 @@ angular.module('odoo')
 
     // Save lead data to local storage
     var save = function (lead, success, error) {
-        var values = [lead.remote_id ? lead.remote_id : 0, 1, lead.name ? lead.name : '', lead.description ? lead.description : '', lead.contact_name ? lead.contact_name : '', lead.phone ? lead.phone : '', lead.mobile ? lead.mobile : '', lead.fax ? lead.fax : '', lead.street ? lead.street : '', lead.street2 ? lead.street2 : '', lead.meeting_count ? lead.meeting_count : 0, lead.message_bounce ? lead.message_bounce : 0, lead.planned_cost ? lead.planned_cost : 0, lead.planned_revenue ? lead.planned_revenue : 0, lead.priority ? lead.priority : 2, lead.probability ? lead.probability : 0, lead.type ? lead.type : 'lead', lead.partner_id ? lead.partner_id : 0];
+        var values = [
+                        lead.remote_id ? lead.remote_id : 0,
+                        1,
+                        lead.name ? lead.name : '',
+                        lead.description ? lead.description : '',
+                        lead.contact_name ? lead.contact_name : '',
+                        lead.phone ? lead.phone : '',
+                        lead.mobile ? lead.mobile : '',
+                        lead.fax ? lead.fax : '',
+                        lead.street ? lead.street : '',
+                        lead.street2 ? lead.street2 : '',
+                        lead.meeting_count ? lead.meeting_count : 0,
+                        lead.message_bounce ? lead.message_bounce : 0,
+                        lead.planned_cost ? lead.planned_cost : 0,
+                        lead.planned_revenue ? lead.planned_revenue : 0,
+                        lead.priority ? lead.priority : 2,
+                        lead.probability ? lead.probability : 0,
+                        lead.type ? lead.type : 'lead',
+                        lead.partner_id ? lead.partner_id : 0
+                    ];
+
         var sql = null;
 
         if (lead.id) {
             values.push(lead.id);
 
-            sql = 'UPDATE lead SET remote_id = ?, modified = ?, modifiedDate = CURRENT_TIMESTAMP, name = ?, description = ?, contact_name = ?, phone = ?, mobile = ?, fax = ?, street = ?, street2 = ?, meeting_count = ?, message_bounce = ?, planned_cost = ?, planned_revenue = ?, priority = ?, probability = ?, type = ?, partner_id = ? WHERE id = ?';
+            sql = 'UPDATE lead SET ' +
+                  'remote_id = ?,' +
+                  'modified = ?,' +
+                  'modifiedDate = CURRENT_TIMESTAMP,' +
+                  'name = ?,' +
+                  'description = ?,' +
+                  'contact_name = ?,' +
+                  'phone = ?,' +
+                  'mobile = ?,' +
+                  'fax = ?,' +
+                  'street = ?,' +
+                  'street2 = ?,' +
+                  'meeting_count = ?,' +
+                  'message_bounce = ?,' +
+                  'planned_cost = ?,' +
+                  'planned_revenue = ?,' +
+                  'priority = ?,' +
+                  'probability = ?,' +
+                  'type = ?,' +
+                  'partner_id = ? ' +
+                  'WHERE id = ?';
         } else {
-            sql = 'INSERT INTO lead(remote_id, modified, name, description, contact_name, phone, mobile, fax, street, street2, meeting_count, message_bounce, planned_cost, planned_revenue, priority, probability, type, partner_id) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
+            sql = 'INSERT INTO lead(' +
+                  'remote_id,' +
+                  'modified,' +
+                  'name,' +
+                  'description,' +
+                  'contact_name,' +
+                  'phone,' +
+                  'mobile,' +
+                  'fax,' +
+                  'street,' +
+                  'street2,' +
+                  'meeting_count,' +
+                  'message_bounce,' +
+                  'planned_cost,' +
+                  'planned_revenue,' +
+                  'priority,' +
+                  'probability,' +
+                  'type,' +
+                  'partner_id) ' +
+                  'VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
         }
 
         db.executeSql(sql, values, function (result) {
@@ -41,7 +100,7 @@ angular.module('odoo')
     }
 
     var removeAll = function (success, error) {
-        var sql = "DELETE FROM lead WHERE type = 'opportunity'";
+        var sql = "DELETE FROM lead WHERE type = 'lead'";
 
         db.executeSql(sql, [], function(result) {
             success(result.rowsAffected);

+ 203 - 170
www/js/factories/sales/lead.sync.factory.js

@@ -1,76 +1,115 @@
 angular.module('odoo')
 
-.factory('leadsRemoteFactory', function (config, odoo, sqlFactory, leadsStorageFactory, asyncLoop) {
-    var server = {};
-    var local = {};
-
-    // Retrieve leads data from server
-    server.retrieve = function (success, error) {
-        config(function (configuration) {
-            odoo.read('crm.lead', [['active', '=', true]], configuration, function (leads) {
-                success(leads);
-            }, function (odooErr) {
-                error(odooErr);
-            });
-        }, function (configErr) {
-            error(configErr);
-        });
-    }
+.factory('leadsRemoteFactory', function (
+    leadsStorageFactory,
+    odooInteroperabilityFactory,
+    configFactory,
+    sqlFactory,
+    asyncLoopFactory
+) {
+
+    // Pull server leads data
+    var pull = function (id, success, error) {
+        configFactory(function (configuration) {
+            if (id) {
+
+                odooInteroperabilityFactory.read('crm.lead', [['id', '=', id]], configuration, function (response) {
+                    success(response);
+                }, function (odooErr) {
+                    error(odooErr)
+                });
 
-    // Retrieve lead by id
-    server.retrieveById = function (id, success, error) {
-        config(function (configuration) {
-            odoo.read('crm.lead', [['id', '=', id]], configuration, function (response) {
-                success(response);
-            }, function (odooReadErr) {
-                error(odooReadErr)
-            });
-        }, function (configErr) {
-            error(configErr);
-        });
-    }
+            } else {
 
-    // Update leads remote data
-    server.update = function (id, data, success, error) {
-        config(function (configuration) {
-            odoo.write('crm.lead', id, data, configuration, function (response) {
-                success(response);
-            }, function (odooWriteErr) {
-                error(odooWriteErr);
-            });
+                odooInteroperabilityFactory.read('crm.lead', [['active', '=', true]], configuration, function (response) {
+                    success(response);
+                }, function (odooErr) {
+                    error(odooErr);
+                });
+
+            }
         }, function (configErr) {
             error(configErr);
         });
     }
 
-    // Remove lead from server
-    server.remove = function (id, success, error) {
-        config(function (configuration) {
-            odoo.unlink('crm.lead', id, configuration, function (response) {
-                success(response);
-            }, function (odooUnlinkErr) {
-                error(odooUnlinkErr);
-            });
+    // Push leads data to server
+    var push = function (id, data, success, error) {
+
+        // Avoid odoo server warning message
+        delete data.id;
+        delete data.remote_id;
+        delete data.modified;
+        delete data.priority;
+
+        configFactory(function (configuration) {
+
+            if (id) {
+
+                pull(id, function (response) {
+                    if (response.length == 1) {
+                        var remoteData = response[0];
+
+                        var remoteDate = new Date(remoteData.__last_update);
+                        var localDate = new Date(data.modifiedDate);
+
+                        delete data.modifiedDate;
+
+                        if (localDate > remoteDate) {
+
+                            odooInteroperabilityFactory.write('crm.lead', id, data, configuration, function (response) {
+                                success(response);
+                            }, function (odooErr) {
+                                error(odooErr);
+                            });
+
+                        } else {
+                            console.log('No Actualizar');
+                            success(response);
+                        }
+
+                    } else {
+                        success(response);
+                    }
+
+                }, function (pullErr) {
+                    error(pullErr);
+                });
+
+            } else {
+
+                delete data.modifiedDate;
+
+                odooInteroperabilityFactory.create('crm.lead', data, configuration, function (response) {
+                    success(response);
+                }, function (odooErr) {
+                    error(odooErr);
+                });
+
+            }
+
         }, function (configErr) {
             error(configErr);
         });
     }
 
-    // Create leads on remote data
-    server.send = function (data, success, error) {
-        config(function (configuration) {
-            odoo.create('crm.lead', data, configuration, function (response) {
+    // Destroy server lead data
+    var destroy = function (id, success, error) {
+        configFactory(function (configuration) {
+
+            odooInteroperabilityFactory.unlink('crm.lead', id, configuration, function (response) {
                 success(response);
-            }, function (odooCreateErr) {
-                error(odooCreateErr);
+            }, function (odooErr) {
+                error(odooErr);
             });
+
         }, function (configErr) {
             error(configErr);
         });
     }
 
-    // Select leads from local storage
-    local.retrieve = function (contraint, success, error) {
+    // Get local lead data
+    var get = function (contraint, success, error) {
         sqlFactory.selectByConstraint('lead', contraint, function (leads) {
             success(leads);
         }, function (err) {
@@ -78,138 +117,132 @@ angular.module('odoo')
         });
     }
 
-    // Sync leads data between local and remote storage
-    var sync = function (success, error) {
-        local.retrieve("remote_id = 0 AND type = 'lead'", function (newLeads) {
-            console.log(newLeads);
-            asyncLoop(newLeads.length, function (loop) {
+    // Send all new data from local to remote server
+    var syncNewData = function (success, error) {
+        get("remote_id = 0 AND type = 'lead'", function (newLeads) {
+            asyncLoopFactory(newLeads.length, function (loop) {
                 var data = newLeads.item(loop.iteration());
 
-                // Avoid odoo server warning message
-                delete data.id;
-                delete data.remote_id;
-                delete data.modified;
-                delete data.modifiedDate;
-                delete data.priority;
+                push(null, data, function (response) {
+                    loop.next();
+                }, function (pushErr) {
+                    loop.next();
+                });
+
+            // End loop
+            }, function () {
+                success(newLeads);
+            });
+        }, function (getErr) {
+            error(getErr);
+        });
+    }
 
-                server.send(data, function (response) {
-                    console.log(response);
+    // Update all data from server
+    var syncUpdatedData = function (success, error) {
+        get("remote_id != 0 AND modified = 1 AND type = 'lead'", function (updatedLeads) {
+            asyncLoopFactory(updatedLeads.length, function (loop) {
+                var data = updatedLeads.item(loop.iteration());
+
+                push(data.remote_id, data, function (response) {
+                    loop.next();
+                }, function (pushErr) {
                     loop.next();
-                }, function (sendErr) {
-                    loop.break();
                 });
+
             // End loop
             }, function () {
-                console.log('New data sended success');
-
-                local.retrieve("remote_id != 0 AND modified = 1 AND type = 'lead'", function (modifiedLeads) {
-                    asyncLoop(modifiedLeads.length, function (loop) {
-                        var localData = modifiedLeads.item(loop.iteration());
-
-                        server.retrieveById(localData.remote_id, function (leads) {
-                            if (leads.length == 1) {
-                                var remoteData = leads[0];
-
-                                var remoteModifiedDate = new Date(remoteData.__last_update);
-                                var localModifiedDate = new Date(localData.modifiedDate);
-
-
-                                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;
-                                    delete localData.priority;
-
-                                    server.update(id, localData, function (response) {
-                                        loop.next();
-                                    }, function (updateErr) {
-                                        console.log(updateErr);
-                                        loop.next();
-                                    });
-                                } else {
-                                    loop.next();
-                                }
-                            } {
-                                loop.next();
-                            }
-                        }, function (retrieveByIdErr) {
-                            console.log(retrieveByIdErr);
-                            loop.next();
-                        });
-
-                    // End loop
-                    }, function () {
-                        console.log('Modified data sended success');
-
-                        local.retrieve("remote_id != 0 AND modified = 2 AND type = 'lead'", function (deletedLeads) {
-                            asyncLoop(deletedLeads.length, function (loop) {
-                                var id = deletedLeads.item(loop.iteration()).remote_id;
-
-                                server.remove(id, function (response) {
-                                    loop.next();
-                                }, function (removeErr) {
-                                    console.log(removeErr);
-                                    loop.next();
-                                });
-
-                            // End loop
-                            }, function () {
-
-                                console.log('Remote data deleted success');
-                                server.retrieve(function (updatedLeads) {
-                                    console.log(updatedLeads);
-
-                                    leadsStorageFactory.removeAll(function () {
-                                        console.log('Removed All');
-
-                                        asyncLoop(updatedLeads.length, function (loop) {
-                                            var data = updatedLeads[loop.iteration()];
-
-                                            // Set id for save on local database
-                                            data.remote_id = data.id;
-                                            delete data.id;
-
-                                            leadsStorageFactory.save(data, function (leadId) {
-                                                loop.next();
-                                            }, function (saveLeadErr) {
-                                                console.log(saveLeadErr);
-                                                loop.next();
-                                            });
-
-                                        // End loop
-                                        }, function () {
-
-                                            console.log('Remote data downloaded success');
-                                            success(updatedLeads);
-                                        });
-                                    }, function (removeLeadsErr) {
-                                        error(removeLeadsErr);
-                                    });
-                                }, function (retrieveErr) {
-                                    error(retrieveErr);
-                                });
-                            });
-                        }, function (selectErr) {
-                            error(selectErr);
-                        });
+                success(updatedLeads);
+            });
+        }, function(getErr) {
+            error(getErr);
+        });
+    }
+
+    // Delete all data
+    var syncDeletedData = function (success, error) {
+        get("remote_id != 0 AND modified = 2 AND type = 'lead'", function (deletedLeads) {
+            asyncLoopFactory(deletedLeads.length, function (loop) {
+                var id = deletedLeads.item(loop.iteration()).remote_id;
+
+                destroy(id, function (response) {
+                    success(response);
+                }, function (destroyErr) {
+                    error(destroyErr);
+                });
+
+            // End loop
+            }, function () {
+                success(deletedLeads);
+            });
+        }, function (getErr) {
+            error(getErr);
+        });
+    }
+
+    // Download all sync data from server
+    var downloadSyncData = function (success, error) {
+        pull(null, function (leads) {
+
+            leadsStorageFactory.removeAll(function () {
+                asyncLoopFactory(leads.length, function (loop) {
+                    var data = leads[loop.iteration()];
+
+                    data.remote_id = data.id;
+                    delete data.id;
+
+                    leadsStorageFactory.save(data, function (leadId) {
+                        loop.next();
+                    }, function (saveErr) {
+                        loop.next();
+                    });
+
+                // End loop
+                }, function () {
+                    success(leads);
+                });
+
+            }, function (removeAllErr) {
+                error(removeAllErr);
+            });
+
+        }, function (pushErr) {
+            error(pushErr);
+        });
+    }
+
+    // Sync all leads data between local and remote data
+    var sync = function (success, error) {
+        syncNewData(function () {
+
+            // console.log("New data ok");
+            syncUpdatedData(function () {
+
+                // console.log("Updated data ok");
+                syncDeletedData(function () {
+
+                    // console.log("Deleted data ok");
+                    downloadSyncData(function (data) {
+                        // console.log("Download data ok");
+                        success(data);
+                    }, function (downloadErr) {
+                        error(downloadErr);
                     });
-                }, function (selectErr) {
-                    error(selectErr);
+                }, function (syncErr) {
+                    error(syncErr);
                 });
+            }, function (syncErr) {
+                error(syncErr);
             });
-        }, function (selectErr) {
-            error(selectErr);
+        }, function (syncErr) {
+            error(syncErr);
         });
     }
 
     return {
-        retrieve: server.retrieve,
-        update: server.update,
-        send: server.send,
+        pull: pull,
+        push: push,
+        destroy: destroy,
         sync: sync
     }
 });

+ 1 - 1
www/js/factories/user.storage.factory.js

@@ -5,7 +5,7 @@ angular.module('odoo')
  *  Description:    Local storage manager for users data
  * -----------------------------------------------------------------------------
  */
-.factory('userStorage', function () {
+.factory('userStorageFactory', function () {
 
     // Save user data to local storage
     var save = function (data, success, error) {

+ 4 - 4
www/js/factories/utils.factory.js

@@ -51,7 +51,7 @@ angular.module('odoo')
  *  Description:    Get user configuration from local database
  * -----------------------------------------------------------------------------
  */
-.factory('config', function (sqlFactory) {
+.factory('configFactory', function (sqlFactory) {
     return function (success, error) {
         sqlFactory.select('user', function (users) {
             if (users.length == 1) {
@@ -75,7 +75,7 @@ angular.module('odoo')
  *  Description:    Async loop util v2
  * -----------------------------------------------------------------------------
  */
-.factory('asyncLoop', function () {
+.factory('asyncLoopFactory', function () {
     return function (iterations, func, callback) {
                 var index = 0;
                 var done = false;
@@ -115,7 +115,7 @@ angular.module('odoo')
  *  Description:    Native camera manager
  * -----------------------------------------------------------------------------
  */
-.factory('camera', function ($cordovaCamera) {
+.factory('cameraFactory', function ($cordovaCamera) {
 
     // Take a picture using native camera
     var takePicture = function (success, error) {
@@ -149,7 +149,7 @@ angular.module('odoo')
  *  Description:    Native contacts manager
  * -----------------------------------------------------------------------------
  */
-.factory('contact', function ($cordovaContacts) {
+.factory('contactFactory', function ($cordovaContacts) {
 
     // Save customer information to device contacts
     var save = function (customer, success, error) {