opportunity.sync.factory.js 8.5 KB

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