factories.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. angular.module('odoo.factories', ['xml-rpc'])
  2. .factory('odoo', function (xmlrpc, methodCallManager) {
  3. return {
  4. auth: function (config, success, error) {
  5. xmlrpc.callMethod(methodCallManager.call('authenticate', config), [config.database, config.username, config.password, {}]).then(function(response) {
  6. if (!response || response['faultCode']) {
  7. error(response);
  8. return;
  9. }
  10. success({ id: response, username: config.username, password: config.password });
  11. });
  12. },
  13. push: function (model, domain, config, success, error) {
  14. xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, model, 'search_read', domain]).then(function (response) {
  15. if (!response || response['faultCode']) {
  16. error(response);
  17. return;
  18. }
  19. success(response);
  20. }, function (err) {
  21. error(err);
  22. });
  23. },
  24. pull: function (model, data, config, success, error) {
  25. xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, 'create', data]).then(function (response) {
  26. if (!response || response['faultCode']) {
  27. error(response);
  28. return;
  29. }
  30. success(response);
  31. }, function (err) {
  32. error(response);
  33. });
  34. }
  35. }
  36. })
  37. .factory('methodCallManager', function(xmlrpc) {
  38. return {
  39. call: function(methodName, configuration) {
  40. var hostName = configuration.host + ':' + configuration.port;
  41. if(!hostName.startsWith('http://')) {
  42. hostName = 'http://' + hostName;
  43. }
  44. if(methodName == 'authenticate') {
  45. xmlrpc.config({ hostName: hostName, pathName: '/xmlrpc/2/common' });
  46. } else {
  47. xmlrpc.config({ hostName: hostName, pathName: '/xmlrpc/2/object' });
  48. }
  49. return methodName;
  50. }
  51. }
  52. })
  53. .factory('sync', function (storage, odoo) {
  54. return {
  55. syncCustomers: function (success, error) {
  56. storage.get('user', function (users) {
  57. if (users.length == 1) {
  58. var userConfig = users.item(0);
  59. storage.getByConstraint('partner', 'remote_id = 0', function(partners) {
  60. var data = storage.resultToJSON(partners);
  61. odoo.pull('res.partner', data, userConfig, function (response) {
  62. success(response);
  63. }, function(e3) {
  64. error(e3);
  65. });
  66. }, function (e2) {
  67. error(e2);
  68. });
  69. }
  70. }, function(e1) {
  71. error(e1);
  72. });
  73. // var sql = 'SELECT * FROM partner WHERE remote_id = 0';
  74. //
  75. // db.executeSql(sql, [], function (result) {
  76. // for (var i = 0; i < result.rows.length; i++) {
  77. // var customer = result.rows.item(i);
  78. //
  79. // s
  80. // }
  81. // }, function(err) {
  82. // error(err);
  83. // });
  84. }
  85. }
  86. })
  87. .factory('storage', function () {
  88. return {
  89. // Customer
  90. saveCustomer(data, success, error) {
  91. console.log(data);
  92. var sql = 'INSERT INTO partner(\n\
  93. remote_id,\n\
  94. name,\n\
  95. city,\n\
  96. mobile,\n\
  97. phone,\n\
  98. fax,\n\
  99. email,\n\
  100. street,\n\
  101. street2,\n\
  102. image_medium,\n\
  103. image_small,\n\
  104. comment,\n\
  105. customer,\n\
  106. employee,\n\
  107. is_company,\n\
  108. debit,\n\
  109. debit_limit,\n\
  110. opportunity_count,\n\
  111. contracts_count,\n\
  112. journal_item_count,\n\
  113. meeting_count,\n\
  114. phonecall_count,\n\
  115. sale_order_count,\n\
  116. total_invoiced\n\
  117. ) VALUES(\n\
  118. ?,\n\
  119. ?,\n\
  120. ?,\n\
  121. ?,\n\
  122. ?,\n\
  123. ?,\n\
  124. ?,\n\
  125. ?,\n\
  126. ?,\n\
  127. ?,\n\
  128. ?,\n\
  129. ?,\n\
  130. ?,\n\
  131. ?,\n\
  132. ?,\n\
  133. ?,\n\
  134. ?,\n\
  135. ?,\n\
  136. ?,\n\
  137. ?,\n\
  138. ?,\n\
  139. ?,\n\
  140. ?,\n\
  141. ?\n\
  142. )';
  143. db.executeSql(sql, data, function(result) {
  144. success(result.insertId);
  145. }, function(err) {
  146. error(err);
  147. });
  148. },
  149. deleteCustomer(id, success, error) {
  150. var sql = 'DELETE FROM partner WHERE id = ' + id;
  151. db.executeSql(sql, [], function(result) {
  152. success(result.rowsAffected);
  153. }, function(err) {
  154. error(err);
  155. });
  156. },
  157. deleteAllCustomers(success, error) {
  158. var sql = 'DELETE FROM partner WHERE customer = ?;';
  159. db.executeSql(sql, [1], function(result) {
  160. success(result.rowsAffected);
  161. }, function(err) {
  162. error(err);
  163. });
  164. },
  165. // Users
  166. saveUser: function(data, success, error) {
  167. var sql = 'INSERT INTO user(remote_id, host, port, database, username, password) VALUES(?, ?, ?, ?, ?, ?)';
  168. db.executeSql(sql, data, function (result) {
  169. success(result.insertId);
  170. }, function (err) {
  171. error(err);
  172. });
  173. },
  174. // Utils
  175. get: function(tableName, success, error) {
  176. var sql = 'SELECT * FROM ' + tableName;
  177. db.executeSql(sql, [], function(result) {
  178. success(result.rows);
  179. }, function(err) {
  180. error(err);
  181. });
  182. },
  183. getByConstraint: function(tableName, constraint, success, error) {
  184. var sql = 'SELECT * FROM ' + tableName + ' WHERE ' + constraint;
  185. db.executeSql(sql, [], function(result) {
  186. success(result.rows);
  187. }, function(err) {
  188. error(err);
  189. });
  190. },
  191. count: function(tableName, success, error) {
  192. var sql = 'SELECT COUNT(*) AS total FROM ' + tableName;
  193. db.executeSql(sql, [], function(result) {
  194. success(result.rows.item(0).total);
  195. }, function(err) {
  196. error(err);
  197. });
  198. },
  199. resultToJSON: function(result) {
  200. var data = [];
  201. for (var i = 0; i < result.length; i++) {
  202. data.push(result.item(i));
  203. }
  204. return data;
  205. }
  206. }
  207. });