123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- 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 });
- });
- },
- push: 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);
- });
- },
- pull: function (model, data, config, success, error) {
- xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, 'create', data]).then(function (response) {
- if (!response || response['faultCode']) {
- error(response);
- return;
- }
- success(response);
- }, function (err) {
- error(response);
- });
- }
- }
- })
- .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('sync', function (storage, odoo) {
- return {
- syncCustomers: function (success, error) {
- storage.get('user', function (users) {
- if (users.length == 1) {
- var userConfig = users.item(0);
- storage.getByConstraint('partner', 'remote_id = 0', function(partners) {
- var data = storage.resultToJSON(partners);
- odoo.pull('res.partner', data, userConfig, function (response) {
- success(response);
- }, function(e3) {
- error(e3);
- });
- }, function (e2) {
- error(e2);
- });
- }
- }, function(e1) {
- error(e1);
- });
- // var sql = 'SELECT * FROM partner WHERE remote_id = 0';
- //
- // db.executeSql(sql, [], function (result) {
- // for (var i = 0; i < result.rows.length; i++) {
- // var customer = result.rows.item(i);
- //
- // s
- // }
- // }, function(err) {
- // error(err);
- // });
- }
- }
- })
- .factory('storage', function () {
- return {
- // Customer
- saveCustomer(data, success, error) {
- console.log(data);
- var sql = 'INSERT INTO partner(\n\
- remote_id,\n\
- name,\n\
- city,\n\
- mobile,\n\
- phone,\n\
- fax,\n\
- email,\n\
- street,\n\
- street2,\n\
- image_medium,\n\
- image_small,\n\
- comment,\n\
- customer,\n\
- employee,\n\
- is_company,\n\
- debit,\n\
- debit_limit,\n\
- opportunity_count,\n\
- contracts_count,\n\
- journal_item_count,\n\
- meeting_count,\n\
- phonecall_count,\n\
- sale_order_count,\n\
- total_invoiced\n\
- ) VALUES(\n\
- ?,\n\
- ?,\n\
- ?,\n\
- ?,\n\
- ?,\n\
- ?,\n\
- ?,\n\
- ?,\n\
- ?,\n\
- ?,\n\
- ?,\n\
- ?,\n\
- ?,\n\
- ?,\n\
- ?,\n\
- ?,\n\
- ?,\n\
- ?,\n\
- ?,\n\
- ?,\n\
- ?,\n\
- ?,\n\
- ?,\n\
- ?\n\
- )';
- db.executeSql(sql, data, function(result) {
- success(result.insertId);
- }, function(err) {
- error(err);
- });
- },
- deleteCustomer(id, success, error) {
- var sql = 'DELETE FROM partner WHERE id = ' + id;
- db.executeSql(sql, [], function(result) {
- success(result.rowsAffected);
- }, function(err) {
- error(err);
- });
- },
- deleteAllCustomers(success, error) {
- var sql = 'DELETE FROM partner WHERE customer = ?;';
- db.executeSql(sql, [1], 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);
- });
- },
- resultToJSON: function(result) {
- var data = [];
- for (var i = 0; i < result.length; i++) {
- data.push(result.item(i));
- }
- return data;
- }
- }
- });
|