factories.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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, id, data, config, success, error) {
  69. xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, model, 'write', [[id], data]]).then(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]]]).then(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. // Get current user saved on mobile
  112. storage.get('user', function (users) {
  113. if (users.length == 1) {
  114. var userConfig = users.item(0);
  115. // 1. Transfer all new data from mobile to server
  116. storage.getByConstraint('partner', 'remote_id = 0 AND customer = 1', function(newPartners) {
  117. util.asyncLoop(newPartners.length, function (loop) {
  118. var data = newPartners.item(loop.iteration());
  119. // Avoid odoo server warning message
  120. delete data.id;
  121. delete data.remote_id;
  122. delete data.modified;
  123. delete data.modifiedDate;
  124. odoo.create('res.partner', data, userConfig, function (response) {
  125. loop.next();
  126. }, function (odooCreateError) {
  127. loop.break();
  128. });
  129. // End loop
  130. }, function() {
  131. // 2. Transfer all modified data from mobile to server
  132. storage.getByConstraint('partner', 'remote_id != 0 AND customer = 1 AND modified = 1', function (modifiedPartners) {
  133. util.asyncLoop(modifiedPartners.length, function (loop) {
  134. var localData = modifiedPartners.item(loop.iteration());
  135. odoo.read('res.partner', [['id', '=', localData.remote_id]], userConfig, function (response) {
  136. if (response.length == 1) {
  137. var remoteData = response[0];
  138. var remoteModifiedDate = new Date(remoteData.__last_update);
  139. var localModifiedDate = new Date(localData.modifiedDate);
  140. if (localModifiedDate > remoteModifiedDate) {
  141. var id = localData.remote_id;
  142. // Avoid odoo server warning message
  143. delete localData.id;
  144. delete localData.remote_id;
  145. delete localData.modified;
  146. delete localData.modifiedDate;
  147. odoo.write('res.partner', id, localData, userConfig, function (response) {
  148. loop.next();
  149. }, function (odooWriteError) {
  150. console.error(odooWriteError);
  151. loop.next();
  152. });
  153. } else {
  154. loop.next();
  155. }
  156. } else {
  157. loop.next();
  158. }
  159. }, function(odooReadError) {
  160. console.error(odooReadError);
  161. loop.next();
  162. });
  163. // End loop
  164. }, function () {
  165. // 3. Delete server data from mobile
  166. storage.getByConstraint('partner', 'remote_id != 0 AND customer = 1 AND modified = 2', function (deletedPartners) {
  167. util.asyncLoop(deletedPartners.length, function (loop) {
  168. var id = deletedPartners.item(loop.iteration()).remote_id;
  169. odoo.unlink('res.partner', id, userConfig, function (response) {
  170. loop.next();
  171. }, function (odooUnlinkError) {
  172. console.error(odooUnlinkError);
  173. loop.next();
  174. });
  175. // End loop
  176. }, function () {
  177. // 4. Download updated data from server to mobile
  178. odoo.read('res.partner', [['customer', '=', true]], userConfig, function (updatedPartners) {
  179. storage.deleteAllCustomers(function () {
  180. util.asyncLoop(updatedPartners.length, function (loop) {
  181. var data = updatedPartners[loop.iteration()];
  182. // Set id for save on local database
  183. data.remote_id = data.id;
  184. delete data.id;
  185. storage.saveCustomer(data, function (customerId) {
  186. loop.next();
  187. } ,function (saveCustomerError) {
  188. console.error(saveCustomerError);
  189. loop.next();
  190. });
  191. }, function () {
  192. success(updatedPartners);
  193. });
  194. }, function (deleteAllCustomersError) {
  195. error(deleteAllCustomersError);
  196. });
  197. }, function (odooReadError) {
  198. error(odooReadError);
  199. });
  200. });
  201. }, function (getDeletedPartnersError) {
  202. error(getDeletedPartnersError);
  203. });
  204. });
  205. }, function (getModifiedPartnersError) {
  206. error(getModifiedPartnersError);
  207. });
  208. });
  209. }, function (partnerGetByConstraintError) {
  210. error(partnerGetByConstraintError);
  211. });
  212. }
  213. }, function(userGetError) {
  214. error(userGetError);
  215. });
  216. }
  217. }
  218. })
  219. .factory('storage', function () {
  220. return {
  221. // Customer
  222. saveCustomer(customer, success, error) {
  223. var sql = '';
  224. if (customer.id) {
  225. sql = `UPDATE partner SET remote_id = ${ customer.remote_id ? customer.remote_id : 0 }, modified = 1, modifiedDate = CURRENT_TIMESTAMP, 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 }`;
  226. } else {
  227. 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 (${ customer.remote_id ? customer.remote_id : 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, ${ customer.debit ? customer.debit : 0 }, ${ customer.debit_limit ? customer.debit_limit : 0 }, ${ customer.opportunity_count ? customer.opportunity_count : 0 }, ${ customer.contracts_count ? customer.contracts_count : 0 }, ${ customer.journal_item_count ? customer.journal_item_count : 0 }, ${ customer.meeting_count ? customer.meeting_count : 0 }, ${ customer.phonecall_count ? customer.phonecall_count : 0 }, ${ customer.sale_order_count ? customer.sale_order_count : 0 }, ${ customer.total_invoiced ? customer.total_invoiced : 0 })`;
  228. }
  229. db.executeSql(sql, [], function(result) {
  230. success(sql.startsWith('INSERT') ? result.insertId : customer.id);
  231. }, function(err) {
  232. error(err);
  233. });
  234. },
  235. deleteCustomer(customer, success, error) {
  236. if (!customer.id) {
  237. error('Customer cannot delete without provide an id');
  238. }
  239. var sql = '';
  240. if (customer.remote_id) {
  241. sql = `UPDATE partner SET modified = 2 WHERE id = ${ customer.id }`;
  242. } else {
  243. sql = `DELETE FROM partner WHERE id = ${ customer.id }`;
  244. }
  245. console.log(sql);
  246. db.executeSql(sql, [], function(result) {
  247. success(result.rowsAffected);
  248. }, function(err) {
  249. error(err);
  250. });
  251. },
  252. deleteAllCustomers(success, error) {
  253. var sql = 'DELETE FROM partner WHERE customer = 1';
  254. db.executeSql(sql, [], function(result) {
  255. success(result.rowsAffected);
  256. }, function(err) {
  257. error(err);
  258. });
  259. },
  260. // Users
  261. saveUser: function(data, success, error) {
  262. var sql = 'INSERT INTO user(remote_id, host, port, database, username, password) VALUES(?, ?, ?, ?, ?, ?)';
  263. db.executeSql(sql, data, function (result) {
  264. success(result.insertId);
  265. }, function (err) {
  266. error(err);
  267. });
  268. },
  269. // Utils
  270. get: function(tableName, success, error) {
  271. var sql = 'SELECT * FROM ' + tableName;
  272. db.executeSql(sql, [], function(result) {
  273. success(result.rows);
  274. }, function(err) {
  275. error(err);
  276. });
  277. },
  278. getByConstraint: function(tableName, constraint, success, error) {
  279. var sql = 'SELECT * FROM ' + tableName + ' WHERE ' + constraint;
  280. db.executeSql(sql, [], function(result) {
  281. success(result.rows);
  282. }, function(err) {
  283. error(err);
  284. });
  285. },
  286. count: function(tableName, success, error) {
  287. var sql = 'SELECT COUNT(*) AS total FROM ' + tableName;
  288. db.executeSql(sql, [], function(result) {
  289. success(result.rows.item(0).total);
  290. }, function(err) {
  291. error(err);
  292. });
  293. }
  294. }
  295. });