angular.module('odoo.storage.factory', []) .factory('storage', function () { return { // Customer saveCustomer(customer, success, error) { var sql = ''; if (customer.id) { 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 }`; } else { 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 })`; } db.executeSql(sql, [], function(result) { success(sql.startsWith('INSERT') ? result.insertId : customer.id); }, function(err) { error(err); }); }, deleteCustomer(customer, success, error) { if (!customer.id) { error('Customer cannot delete without provide an id'); } var sql = ''; if (customer.remote_id) { sql = `UPDATE partner SET modified = 2 WHERE id = ${ customer.id }`; } else { sql = `DELETE FROM partner WHERE id = ${ customer.id }`; } console.log(sql); db.executeSql(sql, [], function(result) { success(result.rowsAffected); }, function(err) { error(err); }); }, deleteAllCustomers(success, error) { var sql = 'DELETE FROM partner WHERE customer = 1'; db.executeSql(sql, [], function(result) { success(result.rowsAffected); }, function(err) { error(err); }); }, // Users saveUser: function(data, success, error) { var sql = 'INSERT INTO user(remote_id, host, port, database, username, password) VALUES(?, ?, ?, ?, ?, ?)'; db.executeSql(sql, data, function (result) { success(result.insertId); }, function (err) { error(err); }); }, // Utils get: function(tableName, success, error) { var sql = 'SELECT * FROM ' + tableName; db.executeSql(sql, [], function(result) { success(result.rows); }, function(err) { error(err); }); }, getByConstraint: function(tableName, constraint, success, error) { var sql = 'SELECT * FROM ' + tableName + ' WHERE ' + constraint; db.executeSql(sql, [], function(result) { success(result.rows); }, function(err) { error(err); }); }, count: function(tableName, success, error) { var sql = 'SELECT COUNT(*) AS total FROM ' + tableName; db.executeSql(sql, [], function(result) { success(result.rows.item(0).total); }, function(err) { error(err); }); } } });