storage.factory.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. angular.module('odoo')
  2. .factory('storage', function () {
  3. return {
  4. // Customer
  5. saveCustomer(customer, success, error) {
  6. var sql = '';
  7. if (customer.id) {
  8. 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 }`;
  9. } else {
  10. 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 })`;
  11. }
  12. db.executeSql(sql, [], function(result) {
  13. success(sql.startsWith('INSERT') ? result.insertId : customer.id);
  14. }, function(err) {
  15. error(err);
  16. });
  17. },
  18. deleteCustomer(customer, success, error) {
  19. if (!customer.id) {
  20. error('Customer cannot delete without provide an id');
  21. }
  22. var sql = '';
  23. if (customer.remote_id) {
  24. sql = `UPDATE partner SET modified = 2 WHERE id = ${ customer.id }`;
  25. } else {
  26. sql = `DELETE FROM partner WHERE id = ${ customer.id }`;
  27. }
  28. console.log(sql);
  29. db.executeSql(sql, [], function(result) {
  30. success(result.rowsAffected);
  31. }, function(err) {
  32. error(err);
  33. });
  34. },
  35. deleteAllCustomers(success, error) {
  36. var sql = 'DELETE FROM partner WHERE customer = 1';
  37. db.executeSql(sql, [], function(result) {
  38. success(result.rowsAffected);
  39. }, function(err) {
  40. error(err);
  41. });
  42. },
  43. // Users
  44. saveUser: function(data, success, error) {
  45. var sql = 'INSERT INTO user(remote_id, host, port, database, username, password) VALUES(?, ?, ?, ?, ?, ?)';
  46. db.executeSql(sql, data, function (result) {
  47. success(result.insertId);
  48. }, function (err) {
  49. error(err);
  50. });
  51. },
  52. // Utils
  53. get: function(tableName, success, error) {
  54. var sql = 'SELECT * FROM ' + tableName;
  55. db.executeSql(sql, [], function(result) {
  56. success(result.rows);
  57. }, function(err) {
  58. error(err);
  59. });
  60. },
  61. getByConstraint: function(tableName, constraint, success, error) {
  62. var sql = 'SELECT * FROM ' + tableName + ' WHERE ' + constraint;
  63. db.executeSql(sql, [], function(result) {
  64. success(result.rows);
  65. }, function(err) {
  66. error(err);
  67. });
  68. },
  69. count: function(tableName, success, error) {
  70. var sql = 'SELECT COUNT(*) AS total FROM ' + tableName;
  71. db.executeSql(sql, [], function(result) {
  72. success(result.rows.item(0).total);
  73. }, function(err) {
  74. error(err);
  75. });
  76. }
  77. }
  78. });