|
@@ -1,232 +1,235 @@
|
|
|
angular.module('odoo')
|
|
|
|
|
|
-.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)
|
|
|
- });
|
|
|
+ .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)
|
|
|
+ });
|
|
|
|
|
|
- } else {
|
|
|
+ } else {
|
|
|
|
|
|
- odooInteroperabilityFactory.read('crm.lead', [['type', '=', 'lead']], configuration, function (response) {
|
|
|
- success(response);
|
|
|
- }, function (odooErr) {
|
|
|
- error(odooErr);
|
|
|
- });
|
|
|
+ odooInteroperabilityFactory.read('crm.lead', [['type', '=', 'lead']], configuration, function (response) {
|
|
|
+ success(response);
|
|
|
+ }, function (odooErr) {
|
|
|
+ error(odooErr);
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+ }, function (configErr) {
|
|
|
+ error(configErr);
|
|
|
+ });
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
- }, function (configErr) {
|
|
|
- error(configErr);
|
|
|
- });
|
|
|
- }
|
|
|
+ // Push leads data to server
|
|
|
+ var push = function (id, data, success, error) {
|
|
|
|
|
|
- // 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;
|
|
|
|
|
|
- // Avoid odoo server warning message
|
|
|
- delete data.id;
|
|
|
- delete data.remote_id;
|
|
|
- delete data.modified;
|
|
|
- delete data.priority;
|
|
|
+ configFactory(function (configuration) {
|
|
|
|
|
|
- configFactory(function (configuration) {
|
|
|
+ if (id) {
|
|
|
|
|
|
- if (id) {
|
|
|
+ pull(id, function (response) {
|
|
|
+ if (response.length == 1) {
|
|
|
+ var remoteData = response[0];
|
|
|
|
|
|
- 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);
|
|
|
|
|
|
- var remoteDate = new Date(remoteData.__last_update);
|
|
|
- var localDate = new Date(data.modifiedDate);
|
|
|
+ delete data.modifiedDate;
|
|
|
|
|
|
- delete data.modifiedDate;
|
|
|
+ if (localDate > remoteDate) {
|
|
|
|
|
|
- if (localDate > remoteDate) {
|
|
|
+ odooInteroperabilityFactory.write('crm.lead', id, data, configuration, function (response) {
|
|
|
+ success(response);
|
|
|
+ }, function (odooErr) {
|
|
|
+ error(odooErr);
|
|
|
+ });
|
|
|
|
|
|
- odooInteroperabilityFactory.write('crm.lead', id, data, configuration, function (response) {
|
|
|
+ } else {
|
|
|
success(response);
|
|
|
- }, function (odooErr) {
|
|
|
- error(odooErr);
|
|
|
- });
|
|
|
+ }
|
|
|
|
|
|
} else {
|
|
|
- console.log('No Actualizar');
|
|
|
success(response);
|
|
|
}
|
|
|
|
|
|
- } else {
|
|
|
+ }, function (pullErr) {
|
|
|
+ error(pullErr);
|
|
|
+ });
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ delete data.modifiedDate;
|
|
|
+
|
|
|
+ odooInteroperabilityFactory.create('crm.lead', data, configuration, function (response) {
|
|
|
success(response);
|
|
|
- }
|
|
|
+ }, function (odooErr) {
|
|
|
+ error(odooErr);
|
|
|
+ });
|
|
|
|
|
|
- }, function (pullErr) {
|
|
|
- error(pullErr);
|
|
|
- });
|
|
|
+ }
|
|
|
|
|
|
- } else {
|
|
|
+ }, function (configErr) {
|
|
|
+ error(configErr);
|
|
|
+ });
|
|
|
+ }
|
|
|
|
|
|
- delete data.modifiedDate;
|
|
|
+ // Destroy server lead data
|
|
|
+ var destroy = function (id, success, error) {
|
|
|
+ configFactory(function (configuration) {
|
|
|
|
|
|
- odooInteroperabilityFactory.create('crm.lead', data, configuration, function (response) {
|
|
|
+ odooInteroperabilityFactory.unlink('crm.lead', id, configuration, function (response) {
|
|
|
success(response);
|
|
|
}, function (odooErr) {
|
|
|
error(odooErr);
|
|
|
});
|
|
|
|
|
|
- }
|
|
|
+ }, function (configErr) {
|
|
|
+ error(configErr);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // Get local lead data
|
|
|
+ var get = function (contraint, success, error) {
|
|
|
+ sqlFactory.selectByConstraint('lead', contraint, function (leads) {
|
|
|
+ success(leads);
|
|
|
+ }, function (err) {
|
|
|
+ error(err);
|
|
|
+ });
|
|
|
+ }
|
|
|
|
|
|
- }, function (configErr) {
|
|
|
- error(configErr);
|
|
|
- });
|
|
|
- }
|
|
|
+ // Send all new data from local to remote server
|
|
|
+ var syncNewData = function (success, error) {
|
|
|
+ get('remote_id = 0', function (newLeads) {
|
|
|
+ asyncLoopFactory(newLeads.length, function (loop) {
|
|
|
+ var data = newLeads.item(loop.iteration());
|
|
|
|
|
|
- // Destroy server lead data
|
|
|
- var destroy = function (id, success, error) {
|
|
|
- configFactory(function (configuration) {
|
|
|
+ push(null, data, function (response) {
|
|
|
+ loop.next();
|
|
|
+ }, function (pushErr) {
|
|
|
+ loop.next();
|
|
|
+ });
|
|
|
|
|
|
- odooInteroperabilityFactory.unlink('crm.lead', id, configuration, function (response) {
|
|
|
- success(response);
|
|
|
- }, function (odooErr) {
|
|
|
- error(odooErr);
|
|
|
+ // End loop
|
|
|
+ }, function () {
|
|
|
+ success(newLeads);
|
|
|
+ });
|
|
|
+ }, function (getErr) {
|
|
|
+ error(getErr);
|
|
|
});
|
|
|
+ }
|
|
|
|
|
|
- }, function (configErr) {
|
|
|
- error(configErr);
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- // Get local lead data
|
|
|
- var get = function (contraint, success, error) {
|
|
|
- sqlFactory.selectByConstraint('lead', contraint, function (leads) {
|
|
|
- success(leads);
|
|
|
- }, function (err) {
|
|
|
- error(err);
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- // Send all new data from local to remote server
|
|
|
- var syncNewData = function (success, error) {
|
|
|
- get('remote_id = 0', function (newLeads) {
|
|
|
- asyncLoopFactory(newLeads.length, function (loop) {
|
|
|
- var data = newLeads.item(loop.iteration());
|
|
|
-
|
|
|
- push(null, data, function (response) {
|
|
|
- loop.next();
|
|
|
- }, function (pushErr) {
|
|
|
- loop.next();
|
|
|
- });
|
|
|
+ // Update all data from server
|
|
|
+ var syncUpdatedData = function (success, error) {
|
|
|
+ get("remote_id != 0 AND modified = 1", function (updatedLeads) {
|
|
|
+ asyncLoopFactory(updatedLeads.length, function (loop) {
|
|
|
+ var data = updatedLeads.item(loop.iteration());
|
|
|
|
|
|
- // End loop
|
|
|
- }, function () {
|
|
|
- success(newLeads);
|
|
|
- });
|
|
|
- }, function (getErr) {
|
|
|
- error(getErr);
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- // Update all data from server
|
|
|
- var syncUpdatedData = function (success, error) {
|
|
|
- get("remote_id != 0 AND modified = 1", 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();
|
|
|
- });
|
|
|
+ push(data.remote_id, data, function (response) {
|
|
|
+ loop.next();
|
|
|
+ }, function (pushErr) {
|
|
|
+ loop.next();
|
|
|
+ });
|
|
|
|
|
|
- // End loop
|
|
|
- }, function () {
|
|
|
- success(updatedLeads);
|
|
|
- });
|
|
|
- }, function(getErr) {
|
|
|
- error(getErr);
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- // Delete all data
|
|
|
- var syncDeletedData = function (success, error) {
|
|
|
- get('remote_id != 0 AND modified = 2', function (deletedLeads) {
|
|
|
- asyncLoopFactory(deletedLeads.length, function (loop) {
|
|
|
- var id = deletedLeads.item(loop.iteration()).remote_id;
|
|
|
-
|
|
|
- destroy(id, function (response) {
|
|
|
- loop.next();
|
|
|
- }, function (destroyErr) {
|
|
|
- loop.next();
|
|
|
+ // End loop
|
|
|
+ }, function () {
|
|
|
+ success(updatedLeads);
|
|
|
});
|
|
|
-
|
|
|
- // End loop
|
|
|
- }, function () {
|
|
|
- success(deletedLeads);
|
|
|
+ }, function (getErr) {
|
|
|
+ error(getErr);
|
|
|
});
|
|
|
- }, function (getErr) {
|
|
|
- error(getErr);
|
|
|
- });
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- // Download all sync data from server
|
|
|
- var downloadSyncData = function (success, error) {
|
|
|
- pull(null, function (leads) {
|
|
|
+ // Delete all data
|
|
|
+ var syncDeletedData = function (success, error) {
|
|
|
+ get('remote_id != 0 AND modified = 2', function (deletedLeads) {
|
|
|
+ asyncLoopFactory(deletedLeads.length, function (loop) {
|
|
|
+ var id = deletedLeads.item(loop.iteration()).remote_id;
|
|
|
|
|
|
- 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) {
|
|
|
+ destroy(id, function (response) {
|
|
|
loop.next();
|
|
|
- }, function (saveErr) {
|
|
|
+ }, function (destroyErr) {
|
|
|
loop.next();
|
|
|
});
|
|
|
|
|
|
- // End loop
|
|
|
+ // End loop
|
|
|
}, function () {
|
|
|
- success(leads);
|
|
|
+ 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 (removeAllErr) {
|
|
|
- error(removeAllErr);
|
|
|
+ }, function (pushErr) {
|
|
|
+ error(pushErr);
|
|
|
});
|
|
|
+ }
|
|
|
|
|
|
- }, function (pushErr) {
|
|
|
- error(pushErr);
|
|
|
- });
|
|
|
- }
|
|
|
+ // Sync all leads data between local and remote data
|
|
|
+ var sync = function (success, error) {
|
|
|
+ syncNewData(function () {
|
|
|
|
|
|
- // Sync all leads data between local and remote data
|
|
|
- var sync = function (success, error) {
|
|
|
- syncNewData(function () {
|
|
|
+ console.log("leads --> new data ok");
|
|
|
+ syncUpdatedData(function () {
|
|
|
|
|
|
- // console.log("New data ok");
|
|
|
- syncUpdatedData(function () {
|
|
|
+ console.log("leads --> updated data ok");
|
|
|
+ syncDeletedData(function () {
|
|
|
|
|
|
- // console.log("Updated data ok");
|
|
|
- syncDeletedData(function () {
|
|
|
+ console.log("leads -> deleted data ok");
|
|
|
+ downloadSyncData(function (data) {
|
|
|
|
|
|
- // console.log("Deleted data ok");
|
|
|
- downloadSyncData(function (data) {
|
|
|
- // console.log("Download data ok");
|
|
|
- success(data);
|
|
|
- }, function (downloadErr) {
|
|
|
- error(downloadErr);
|
|
|
+ console.log("leads -> download data ok");
|
|
|
+ success(data);
|
|
|
+ }, function (downloadErr) {
|
|
|
+ error(downloadErr);
|
|
|
+ });
|
|
|
+ }, function (syncErr) {
|
|
|
+ error(syncErr);
|
|
|
});
|
|
|
}, function (syncErr) {
|
|
|
error(syncErr);
|
|
@@ -234,15 +237,12 @@ angular.module('odoo')
|
|
|
}, function (syncErr) {
|
|
|
error(syncErr);
|
|
|
});
|
|
|
- }, function (syncErr) {
|
|
|
- error(syncErr);
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- return {
|
|
|
- pull: pull,
|
|
|
- push: push,
|
|
|
- destroy: destroy,
|
|
|
- sync: sync
|
|
|
- }
|
|
|
-});
|
|
|
+ }
|
|
|
+
|
|
|
+ return {
|
|
|
+ pull: pull,
|
|
|
+ push: push,
|
|
|
+ destroy: destroy,
|
|
|
+ sync: sync
|
|
|
+ }
|
|
|
+ });
|