opportunity.sync.factory.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. angular.module('odoo')
  2. /**
  3. * -----------------------------------------------------------------------------
  4. * Description: Local storage manager for crm stage data
  5. * -----------------------------------------------------------------------------
  6. */
  7. .factory('opportunitiesDataFactory', function (
  8. opportunitiesStorageFactory,
  9. leadsRemoteFactory,
  10. odooFactory,
  11. sqlFactory,
  12. asyncLoopFactory
  13. ) {
  14. /**
  15. *
  16. */
  17. var pull = function (id, success, error) {
  18. if (id) {
  19. odooFactory.read('crm.lead', [['id', '=', id]], function (response) {
  20. success(response);
  21. }, function (odooErr) {
  22. error(odooErr);
  23. });
  24. } else {
  25. odooFactory.read('crm.lead', [['type', '=', 'opportunity']], function (response) {
  26. success(response);
  27. }, function (odooErr) {
  28. error(odooErr);
  29. });
  30. }
  31. }
  32. /**
  33. *
  34. */
  35. var push = function (id, data, success, error) {
  36. leadsRemoteFactory.push(id, data, function (response) {
  37. success(response);
  38. }, function (err) {
  39. error(err);
  40. });
  41. }
  42. /**
  43. *
  44. */
  45. var destroy = function (id, success, error) {
  46. leadsRemoteFactory.destroy(id, function (response) {
  47. success(response);
  48. }, function (err) {
  49. error(err);
  50. });
  51. }
  52. /**
  53. *
  54. */
  55. var changeStage = function (opportunity, stageId, success, error) {
  56. opportunity.stage_id = stageId;
  57. opportunitiesStorageFactory.save(opportunity, function (opportunityId) {
  58. success(opportunityId);
  59. }, function (err) {
  60. error(err);
  61. });
  62. }
  63. /**
  64. *
  65. */
  66. var save = function (opportunity, success, error) {
  67. opportunitiesStorageFactory.save(opportunity, function (opportunityId) {
  68. success(opportunityId);
  69. }, function (err) {
  70. error(err);
  71. });
  72. }
  73. /**
  74. *
  75. */
  76. var get = function (constraint, success, error) {
  77. sqlFactory.selectByConstraint('crm_lead', constraint, function (leads) {
  78. success(leads);
  79. }, function (err) {
  80. error(err);
  81. });
  82. }
  83. /**
  84. *
  85. */
  86. var getAll = function (success, error) {
  87. get("type = 'opportunity' AND modified != 2", function (opportunities) {
  88. var data = [];
  89. for (var i = 0; i < opportunities.length; i++) {
  90. data.push(opportunities.item(i));
  91. }
  92. success(data);
  93. }, function (selectErr) {
  94. error(selectErr);
  95. });
  96. }
  97. /**
  98. *
  99. */
  100. var remove = function (opportunity, success, error) {
  101. opportunitiesStorageFactory.remove(opportunity, function (affected) {
  102. success(affected);
  103. }, function (err) {
  104. error(err);
  105. });
  106. }
  107. /**
  108. *
  109. */
  110. var syncNewData = function (success, error) {
  111. get("remote_id = 0 AND type = 'opportunity'", function (newOpportunities) {
  112. asyncLoopFactory(newOpportunities.length, function (loop) {
  113. var data = newOpportunities.item(loop.iteration());
  114. push(null, data, function (response) {
  115. loop.next();
  116. }, function (pushErr) {
  117. loop.next();
  118. });
  119. // end loop
  120. }, function () {
  121. success(newOpportunities);
  122. });
  123. }, function (getErr) {
  124. error(getErr);
  125. });
  126. }
  127. /**
  128. *
  129. */
  130. var syncUpdatedData = function (success, error) {
  131. get("remote_id != 0 AND modified = 1 AND type = 'opportunity'", function (updatedOpportunities) {
  132. asyncLoopFactory(updatedOpportunities.length, function (loop) {
  133. var data = updatedOpportunities.item(loop.iteration());
  134. push(data.remote_id, data, function (response) {
  135. loop.next();
  136. }, function (pushErr) {
  137. loop.next();
  138. });
  139. // end loop
  140. }, function () {
  141. success(updatedOpportunities);
  142. });
  143. }, function (getErr) {
  144. error(getErr);
  145. });
  146. }
  147. /**
  148. *
  149. */
  150. var syncDeletedData = function (success, error) {
  151. get("remote_id != 0 AND modified = 2 AND type = 'opportunity'", function (deletedOpportunities) {
  152. asyncLoopFactory(deletedOpportunities.length, function (loop) {
  153. var id = deletedOpportunities.item(loop.iteration()).remote_id;
  154. destroy(id, function (response) {
  155. loop.next();
  156. }, function (destroyErr) {
  157. loop.next();
  158. });
  159. // end loop
  160. }, function () {
  161. success(deletedOpportunities);
  162. });
  163. }, function (getErr) {
  164. error(getErr);
  165. });
  166. }
  167. /**
  168. *
  169. */
  170. var downloadSyncData = function (success, error) {
  171. pull(null, function (opportunities) {
  172. opportunitiesStorageFactory.removeAll(function () {
  173. asyncLoopFactory(opportunities.length, function (loop) {
  174. var data = opportunities[loop.iteration()];
  175. data.remote_id = data.id;
  176. delete data.id;
  177. opportunitiesStorageFactory.save(data, function (opportunityId) {
  178. loop.next();
  179. }, function (saveErr) {
  180. loop.next();
  181. });
  182. }, function () {
  183. success(opportunities);
  184. });
  185. }, function (removeAllErr) {
  186. console.log(removeAllErr);
  187. error(removeAllErr);
  188. });
  189. }, function (pullErr) {
  190. error(pullErr);
  191. });
  192. }
  193. /**
  194. *
  195. */
  196. var sync = function (success, error) {
  197. syncNewData(function () {
  198. console.log('opportunities --> new data ok');
  199. syncUpdatedData(function () {
  200. console.log('opportunities --> updated data ok');
  201. syncDeletedData(function () {
  202. console.log('opportunities --> deleted data ok');
  203. downloadSyncData(function (data) {
  204. console.log('opportunities --> download data ok');
  205. success(data);
  206. }, function (syncErr) {
  207. error(syncErr);
  208. });
  209. }, function (syncErr) {
  210. error(syncErr);
  211. });
  212. }, function (syncErr) {
  213. error(syncErr);
  214. });
  215. }, function (syncErr) {
  216. error(syncErr);
  217. });
  218. }
  219. return {
  220. pull: pull,
  221. push: push,
  222. destroy: destroy,
  223. save: save,
  224. changeStage: changeStage,
  225. getAll: getAll,
  226. remove: remove,
  227. sync: sync
  228. }
  229. });