factories.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. angular.module('odoo.factories', ['xml-rpc'])
  2. .factory('util', function () {
  3. return {
  4. asyncLoop : function (iterations, func, callback) {
  5. var index = 0;
  6. var done = false;
  7. var loop = {
  8. next: function () {
  9. if (done) {
  10. return;
  11. }
  12. if (index < iterations) {
  13. index++;
  14. func(loop);
  15. } else {
  16. done = true;
  17. callback();
  18. }
  19. },
  20. iteration: function() {
  21. return index - 1;
  22. },
  23. break: function() {
  24. done = true;
  25. callback();
  26. }
  27. };
  28. loop.next();
  29. return loop;
  30. }
  31. }
  32. })
  33. .factory('odoo', function (xmlrpc, methodCallManager) {
  34. return {
  35. auth: function (config, success, error) {
  36. xmlrpc.callMethod(methodCallManager.call('authenticate', config), [config.database, config.username, config.password, {}]).then(function(response) {
  37. if (!response || response['faultCode']) {
  38. error(response);
  39. return;
  40. }
  41. success({ id: response, username: config.username, password: config.password });
  42. }, function (xmlrpcError) {
  43. error(xmlrpcError);
  44. });
  45. },
  46. read: function (model, domain, config, success, error) {
  47. xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, model, 'search_read', domain]).then(function (response) {
  48. if (!response || response['faultCode']) {
  49. error(response);
  50. return;
  51. }
  52. success(response);
  53. }, function (xmlrpcError) {
  54. error(xmlrpcError);
  55. });
  56. },
  57. create: function (model, data, config, success, error) {
  58. xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, model, 'create', data]).then(function (response) {
  59. if (!response || response['faultCode']) {
  60. error(response);
  61. return;
  62. }
  63. success(response);
  64. }, function (xmlrpcError) {
  65. error(xmlrpcError);
  66. });
  67. },
  68. write: function (model, data, config, success, error) {
  69. xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, model, 'write', data], function (response) {
  70. if (!response || response['faultCode']) {
  71. error(response);
  72. return;
  73. }
  74. success(response);
  75. }, function (xmlrpcError) {
  76. error(xmlrpcError);
  77. });
  78. },
  79. unlink: function (model, id, config, success, error) {
  80. xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, model, 'unlink', id], function (response) {
  81. if (!response || response['faultCode']) {
  82. error(response);
  83. return;
  84. }
  85. success(response);
  86. }, function (xmlrpcError) {
  87. error(xmlrpcError);
  88. });
  89. }
  90. }
  91. })
  92. .factory('methodCallManager', function(xmlrpc) {
  93. return {
  94. call: function(methodName, configuration) {
  95. var hostName = configuration.host + ':' + configuration.port;
  96. if(!hostName.startsWith('http://')) {
  97. hostName = 'http://' + hostName;
  98. }
  99. if(methodName == 'authenticate') {
  100. xmlrpc.config({ hostName: hostName, pathName: '/xmlrpc/2/common' });
  101. } else {
  102. xmlrpc.config({ hostName: hostName, pathName: '/xmlrpc/2/object' });
  103. }
  104. return methodName;
  105. }
  106. }
  107. })
  108. .factory('sync', function (storage, odoo, util) {
  109. return {
  110. syncCustomers: function (success, error) {
  111. storage.get('user', function (users) {
  112. if (users.length == 1) {
  113. var userConfig = users.item(0);
  114. // 1. Transfer all new data from mobile to server
  115. storage.getByConstraint('partner', 'remote_id = 0', function(newPartners) {
  116. util.asyncLoop(newPartners.length, function (loop) {
  117. var data = [newPartners.item(loop.iteration())];
  118. odoo.create('res.partner', data, userConfig, function (response) {
  119. loop.next();
  120. }, function (odooCreateError) {
  121. loop.break();
  122. });
  123. // End loop
  124. }, function() {
  125. // 2. Transfer all modified data from mobile to server
  126. storage.getByConstraint('partner', '', function (modifiedPartners) {
  127. util.asyncLoop(modifiedPartners.length, function (loop) {
  128. var data = [modifiedPartners.item(loop.iteration())];
  129. odoo.write('res.partner', data, userConfig, function (response) {
  130. loop.next();
  131. }, function (odooWriteError) {
  132. loop.break();
  133. });
  134. // End loop
  135. }, function () {
  136. // 3. Delete server data from mobile
  137. storage.getByConstraint('partner', '', function (deletedPartners) {
  138. util.asyncLoop(deletedPartners.length, function (loop) {
  139. var id = deletedPartners.item(loop.iteration()).remote_id;
  140. odoo.unlink('res.partner', id, userConfig, function (response) {
  141. loop.next();
  142. }, function(odooUnlinkError) {
  143. loop.break();
  144. });
  145. // End loop
  146. }, function () {
  147. // 4. Download updated data from server to mobile
  148. odoo.read('res.partner', [[['active' = True]]], userConfig, function (updatedPartners) {
  149. storage.deleteAllCustomers(function () {
  150. util.asyncLoop(updatedPartners.length, funtion (loop) {
  151. var data = updatedPartners.item(loop.iteration());
  152. storage.saveCustomer(data, function () {
  153. loop.next();
  154. }, function (saveCustomerError) {
  155. loop.break();
  156. })
  157. // End loop
  158. }, function () {
  159. success(updatedPartners);
  160. });
  161. }, function (deleteAllCustomersError) {
  162. error(deleteAllCustomersError);
  163. });
  164. }, function (odooReadError) {
  165. error(odooReadError);
  166. });
  167. });
  168. }, function (getDeletedPartnersError) {
  169. error(getDeletedPartnersError);
  170. });
  171. });
  172. }, function (getModifiedPartnersError) {
  173. error(getModifiedPartnersError);
  174. });
  175. });
  176. }, function (partnerGetByConstraintError) {
  177. error(partnerGetByConstraintError);
  178. });
  179. }
  180. }, function(userGetError) {
  181. error(userGetError);
  182. });
  183. }
  184. }
  185. })
  186. .factory('storage', function () {
  187. return {
  188. // Customer
  189. saveCustomer(customer, success, error) {
  190. var sql = '';
  191. if (customer.id) {
  192. sql = `UPDATE partner SET remote_id = ${ customer.remote_id ? customer.remote_id : 0 }, name = ${ customer.name ? '"' + customer.name + '"' : null }, city = ${ customer.city ? '"' + customer.city + '"' : null }, mobile = ${ customer.mobile ? '"' + customer.mobile + '"' : null }, phone = ${ customer.phone ? '"' + customer.phone + '"' : null }, fax = ${ customer.fax ? '"' + customer.fax + '"' : null }, email = ${ customer.email ? '"' + customer.email + '"' : null }, street = ${ customer.street ? '"' + customer.street + '"' : null }, street2 = ${ customer.street2 ? '"' + customer.street2 + '"' : null }, image_medium = ${ customer.image_medium ? '"' + customer.image_medium + '"' : null }, image_small = ${ customer.image_small ? '"' + customer.image_small + '"' : null }, comment = ${ customer.comment ? '"' + customer.comment + '"' : null }, customer = ${ customer.customer ? customer.customer : 1 }, employee = ${ customer.employee ? customer.employee : 0 }, is_company = ${ customer.is_company ? customer.is_company : 0 }, debit = ${ customer.debit ? customer.debit : 0 }, debit_limit = ${ customer.debit_limit ? customer.debit_limit : 0 }, opportunity_count = ${ customer.opportunity_count ? customer.opportunity_count : 0 }, contracts_count = ${ customer.contracts_count ? customer.contracts_count : 0 }, journal_item_count = ${ customer.journal_item_count ? customer.journal_item_count : 0 }, meeting_count = ${ customer.meeting_count ? customer.meeting_count : 0 }, phonecall_count = ${ customer.phonecall_count ? customer.phonecall_count : 0 }, sale_order_count = ${ customer.sale_order_count ? customer.sale_order_count : 0 }, total_invoiced = ${ customer.total_invoiced ? customer.total_invoiced : 0 } WHERE id = ${ customer.id }`;
  193. } else {
  194. sql = `INSERT INTO partner(remote_id, name, city, mobile, phone, fax, email, street, street2, image_medium, image_small, comment, customer, employee, is_company, debit, debit_limit, opportunity_count, contracts_count, journal_item_count, meeting_count, phonecall_count, sale_order_count, total_invoiced) VALUES (0, ${ customer.name ? '"' + customer.name + '"' : null }, ${ customer.city ? '"' + customer.city + '"' : null }, ${ customer.mobile ? '"' + customer.mobile + '"' : null }, ${ customer.phone ? '"' + customer.phone + '"' : null }, ${ customer.fax ? '"' + customer.fax + '"' : null }, ${ customer.email ? '"' + customer.email + '"' : null }, ${ customer.street ? '"' + customer.street + '"' : null }, ${ customer.street2 ? '"' + customer.street2 + '"' : null }, ${ customer.image_medium ? '"' + customer.image_medium + '"' : null }, ${ customer.image_small ? '"' + customer.image_small + '"' : null }, ${ customer.comment ? '"' + customer.comment + '"' : null }, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)`;
  195. }
  196. db.executeSql(sql, [], function(result) {
  197. success(sql.startsWith('INSERT') ? result.insertId : customer.id);
  198. }, function(err) {
  199. error(err);
  200. });
  201. },
  202. deleteCustomer(id, success, error) {
  203. var sql = 'DELETE FROM partner WHERE id = ' + id;
  204. db.executeSql(sql, [], function(result) {
  205. success(result.rowsAffected);
  206. }, function(err) {
  207. error(err);
  208. });
  209. },
  210. deleteAllCustomers(success, error) {
  211. var sql = 'DELETE FROM partner WHERE customer = ?;';
  212. db.executeSql(sql, [1], function(result) {
  213. success(result.rowsAffected);
  214. }, function(err) {
  215. error(err);
  216. });
  217. },
  218. // Users
  219. saveUser: function(data, success, error) {
  220. var sql = 'INSERT INTO user(remote_id, host, port, database, username, password) VALUES(?, ?, ?, ?, ?, ?)';
  221. db.executeSql(sql, data, function (result) {
  222. success(result.insertId);
  223. }, function (err) {
  224. error(err);
  225. });
  226. },
  227. // Utils
  228. get: function(tableName, success, error) {
  229. var sql = 'SELECT * FROM ' + tableName;
  230. db.executeSql(sql, [], function(result) {
  231. success(result.rows);
  232. }, function(err) {
  233. error(err);
  234. });
  235. },
  236. getByConstraint: function(tableName, constraint, success, error) {
  237. var sql = 'SELECT * FROM ' + tableName + ' WHERE ' + constraint;
  238. db.executeSql(sql, [], function(result) {
  239. success(result.rows);
  240. }, function(err) {
  241. error(err);
  242. });
  243. },
  244. count: function(tableName, success, error) {
  245. var sql = 'SELECT COUNT(*) AS total FROM ' + tableName;
  246. db.executeSql(sql, [], function(result) {
  247. success(result.rows.item(0).total);
  248. }, function(err) {
  249. error(err);
  250. });
  251. },
  252. resultToJSON: function(result) {
  253. var data = [];
  254. for (var i = 0; i < result.length; i++) {
  255. data.push(result.item(i));
  256. }
  257. return data;
  258. }
  259. }
  260. });