1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- angular.module('odoo.factories', ['xml-rpc'])
- .factory('odoo', function (xmlrpc, methodCallManager) {
- return {
- auth: function (config, success, error) {
- xmlrpc.callMethod(methodCallManager.call('authenticate', config), [config.database, config.username, config.password, {}]).then(function(response) {
- if (!response || response['faultCode']) {
- error(response);
- return;
- }
- success({ id: response, username: config.username, password: config.password });
- });
- },
- fetch: function (model, domain, config, success, error) {
- xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, model, 'search_read', domain]).then(function (response) {
- if (!response || response['faultCode']) {
- error(response);
- return;
- }
- success(response);
- }, function (err) {
- error(err);
- });
- }
- }
- })
- .factory('methodCallManager', function(xmlrpc) {
- return {
- call: function(methodName, configuration) {
- var hostName = configuration.host + ':' + configuration.port;
- if(!hostName.startsWith('http://')) {
- hostName = 'http://' + hostName;
- }
- if(methodName == 'authenticate') {
- xmlrpc.config({ hostName: hostName, pathName: '/xmlrpc/2/common' });
- } else {
- xmlrpc.config({ hostName: hostName, pathName: '/xmlrpc/2/object' });
- }
- return methodName;
- }
- }
- })
- .factory('storage', function () {
- return {
- // Customer
- saveCustomer(data, success, error) {
- var sql = 'INSERT INTO customer(remote_id, name, phone) VALUES(?, ?, ?)';
- db.executeSql(sql, data, function(result) {
- success(result.insertId);
- }, 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);
- });
- },
- 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);
- });
- }
- }
- });
|