customer.sync.factory.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. angular.module('odoo')
  2. .factory('customers', function (sqlFactory, customerStorage, asyncLoop, odoo) {
  3. return {
  4. sync: function (success, error) {
  5. // Get current user saved on mobile
  6. sqlFactory.select('user', function (users) {
  7. if (users.length == 1) {
  8. var userConfig = users.item(0);
  9. // 1. Transfer all new data from mobile to server
  10. sqlFactory.selectByConstraint('partner', 'remote_id = 0 AND customer = 1', function(newPartners) {
  11. asyncLoop(newPartners.length, function (loop) {
  12. var data = newPartners.item(loop.iteration());
  13. // Avoid odoo server warning message
  14. delete data.id;
  15. delete data.remote_id;
  16. delete data.modified;
  17. delete data.modifiedDate;
  18. odoo.create('res.partner', data, userConfig, function (response) {
  19. loop.next();
  20. }, function (odooCreateError) {
  21. loop.break();
  22. });
  23. // End loop
  24. }, function() {
  25. // 2. Transfer all modified data from mobile to server
  26. sqlFactory.selectByConstraint('partner', 'remote_id != 0 AND customer = 1 AND modified = 1', function (modifiedPartners) {
  27. asyncLoop(modifiedPartners.length, function (loop) {
  28. var localData = modifiedPartners.item(loop.iteration());
  29. odoo.read('res.partner', [['id', '=', localData.remote_id]], userConfig, function (response) {
  30. if (response.length == 1) {
  31. var remoteData = response[0];
  32. var remoteModifiedDate = new Date(remoteData.__last_update);
  33. var localModifiedDate = new Date(localData.modifiedDate);
  34. if (localModifiedDate > remoteModifiedDate) {
  35. var id = localData.remote_id;
  36. // Avoid odoo server warning message
  37. delete localData.id;
  38. delete localData.remote_id;
  39. delete localData.modified;
  40. delete localData.modifiedDate;
  41. odoo.write('res.partner', id, localData, userConfig, function (response) {
  42. loop.next();
  43. }, function (odooWriteError) {
  44. loop.next();
  45. });
  46. } else {
  47. loop.next();
  48. }
  49. } else {
  50. loop.next();
  51. }
  52. }, function(odooReadError) {
  53. console.error(odooReadError);
  54. loop.next();
  55. });
  56. // End loop
  57. }, function () {
  58. // 3. Delete server data from mobile
  59. sqlFactory.selectByConstraint('partner', 'remote_id != 0 AND customer = 1 AND modified = 2', function (deletedPartners) {
  60. asyncLoop(deletedPartners.length, function (loop) {
  61. var id = deletedPartners.item(loop.iteration()).remote_id;
  62. odoo.unlink('res.partner', id, userConfig, function (response) {
  63. loop.next();
  64. }, function (odooUnlinkError) {
  65. loop.next();
  66. });
  67. // End loop
  68. }, function () {
  69. // 4. Download updated data from server to mobile
  70. odoo.read('res.partner', [['customer', '=', true]], userConfig, function (updatedPartners) {
  71. customerStorage.removeAll(function () {
  72. asyncLoop(updatedPartners.length, function (loop) {
  73. var data = updatedPartners[loop.iteration()];
  74. // Set id for save on local database
  75. data.remote_id = data.id;
  76. delete data.id;
  77. customerStorage.save(data, function (customerId) {
  78. loop.next();
  79. } ,function (saveCustomerError) {
  80. console.error(saveCustomerError);
  81. loop.next();
  82. });
  83. }, function () {
  84. success(updatedPartners);
  85. });
  86. }, function (deleteAllCustomersError) {
  87. error(deleteAllCustomersError);
  88. });
  89. }, function (odooReadError) {
  90. error(odooReadError);
  91. });
  92. });
  93. }, function (getDeletedPartnersError) {
  94. error(getDeletedPartnersError);
  95. });
  96. });
  97. }, function (getModifiedPartnersError) {
  98. error(getModifiedPartnersError);
  99. });
  100. });
  101. }, function (partnerGetByConstraintError) {
  102. error(partnerGetByConstraintError);
  103. });
  104. }
  105. }, function(userGetError) {
  106. error(userGetError);
  107. });
  108. }
  109. }
  110. });