customer.storage.factory.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. angular.module('odoo')
  2. /**
  3. * -----------------------------------------------------------------------------
  4. * Description: Local storage manager for customers data
  5. * -----------------------------------------------------------------------------
  6. */
  7. .factory('customerStorage', function () {
  8. // Save customer data to local storage
  9. var save = function (customer, success, error) {
  10. var sql = '';
  11. if (customer.id) {
  12. 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 }`;
  13. } else {
  14. 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 })`;
  15. }
  16. db.executeSql(sql, [], function(result) {
  17. success(sql.startsWith('INSERT') ? result.insertId : customer.id);
  18. }, function(err) {
  19. error(err);
  20. });
  21. };
  22. // Delete customer from local storage
  23. var remove = function (customer, success, error) {
  24. if (!customer.id) {
  25. error('Customer cannot delete without provide an id');
  26. }
  27. var sql = '';
  28. if (customer.remote_id) {
  29. sql = `UPDATE partner SET modified = 2 WHERE id = ${ customer.id }`;
  30. } else {
  31. sql = `DELETE FROM partner WHERE id = ${ customer.id }`;
  32. }
  33. console.log(sql);
  34. db.executeSql(sql, [], function(result) {
  35. success(result.rowsAffected);
  36. }, function(err) {
  37. error(err);
  38. });
  39. };
  40. // Delete all customers from local storage
  41. var removeAll = function (success, error) {
  42. var sql = 'DELETE FROM partner WHERE customer = 1';
  43. db.executeSql(sql, [], function(result) {
  44. success(result.rowsAffected);
  45. }, function(err) {
  46. error(err);
  47. });
  48. };
  49. return {
  50. save: save,
  51. remove: remove,
  52. removeAll: removeAll
  53. }
  54. });