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(); }); // 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); }); }); }, function (partnerGetByConstraintError) { error(partnerGetByConstraintError); }); } }, function(userGetError) { error(userGetError); }); } } });