opportunity.storage.factory.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. angular.module('odoo')
  2. .factory('opportunitiesStorageFactory', function () {
  3. var save = function (opportunity, success, error) {
  4. var values = [
  5. opportunity.remote_id ? opportunity.remote_id : 0,
  6. 1, opportunity.name ? opportunity.name : '',
  7. opportunity.description ? opportunity.description : '',
  8. opportunity.contact_name ? opportunity.contact_name : '',
  9. opportunity.phone ? opportunity.phone : '',
  10. opportunity.mobile ? opportunity.mobile : '',
  11. opportunity.fax ? opportunity.fax : '',
  12. opportunity.street ? opportunity.street : '',
  13. opportunity.street2 ? opportunity.street2 : '',
  14. opportunity.meeting_count ? opportunity.meeting_count : 0,
  15. opportunity.message_bounce ? opportunity.message_bounce : 0,
  16. opportunity.planned_cost ? opportunity.planned_cost : 0,
  17. opportunity.planned_revenue ? opportunity.planned_revenue : 0,
  18. opportunity.priority ? opportunity.priority : 2,
  19. opportunity.probability ? opportunity.probability : 0,
  20. (opportunity.type == undefined || opportunity.type == 'lead') ? 'opportunity' : opportunity.type,
  21. opportunity.partner_id ? opportunity.partner_id : 0
  22. ];
  23. var sql = null;
  24. if (opportunity.id) {
  25. values.push(opportunity.id);
  26. sql = 'UPDATE lead SET ' +
  27. 'remote_id = ?,' +
  28. 'modified = ?,' +
  29. 'modifiedDate = CURRENT_TIMESTAMP,' +
  30. 'name = ?,' +
  31. 'description = ?,' +
  32. 'contact_name = ?,' +
  33. 'phone = ?,' +
  34. 'mobile = ?,' +
  35. 'fax = ?,' +
  36. 'street = ?,' +
  37. 'street2 = ?,' +
  38. 'meeting_count = ?,' +
  39. 'message_bounce = ?,' +
  40. 'planned_cost = ?,' +
  41. 'planned_revenue = ?,' +
  42. 'priority = ?,' +
  43. 'probability = ?,' +
  44. 'type = ?,' +
  45. 'partner_id = ? ' +
  46. 'WHERE id = ?';
  47. } else {
  48. sql = 'INSERT INTO lead(' +
  49. 'remote_id,' +
  50. 'modified,' +
  51. 'name,' +
  52. 'description,' +
  53. 'contact_name,' +
  54. 'phone,' +
  55. 'mobile,' +
  56. 'fax,' +
  57. 'street,' +
  58. 'street2,' +
  59. 'meeting_count,' +
  60. 'message_bounce,' +
  61. 'planned_cost,' +
  62. 'planned_revenue,' +
  63. 'priority,' +
  64. 'probability,' +
  65. 'type,' +
  66. 'partner_id) ' +
  67. 'VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
  68. }
  69. db.executeSql(sql, values, function (result) {
  70. success(sql.startsWith('INSERT') ? result.insertId : opportunity.id);
  71. }, function (err) {
  72. error(err);
  73. });
  74. }
  75. // Remove opportunity from local storage
  76. var remove = function (opportunity, success, error) {
  77. var values = [opportunity.id];
  78. var sql = null;
  79. if (opportunity.remote_id) {
  80. sql = 'UPDATE lead SET modified = 2 WHERE id = ?';
  81. } else {
  82. sql = 'DELETE FROM lead WHERE id = ?'
  83. }
  84. db.executeSql(sql, values, function (result) {
  85. success(result.rowsAffected);
  86. }, function(err) {
  87. error(err);
  88. });
  89. }
  90. var removeAll = function (success, error) {
  91. var sql = "DELETE FROM lead WHERE type = 'opportunity'";
  92. db.executeSql(sql, [], function(result) {
  93. success(result.rowsAffected);
  94. }, function(err) {
  95. error(err);
  96. });
  97. }
  98. return {
  99. save: save,
  100. remove: remove,
  101. removeAll: removeAll
  102. }
  103. });