|
@@ -1,5 +1,42 @@
|
|
|
angular.module('odoo.factories', ['xml-rpc'])
|
|
|
|
|
|
+.factory('util', function () {
|
|
|
+ return {
|
|
|
+ asyncLoop : function (iterations, func, callback) {
|
|
|
+ var index = 0;
|
|
|
+ var done = false;
|
|
|
+ var loop = {
|
|
|
+ next: function () {
|
|
|
+ if (done) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (index < iterations) {
|
|
|
+ index++;
|
|
|
+ func(loop);
|
|
|
+
|
|
|
+ } else {
|
|
|
+ done = true;
|
|
|
+ callback();
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ iteration: function() {
|
|
|
+ return index - 1;
|
|
|
+ },
|
|
|
+
|
|
|
+ break: function() {
|
|
|
+ done = true;
|
|
|
+ callback();
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ loop.next();
|
|
|
+ return loop;
|
|
|
+ }
|
|
|
+ }
|
|
|
+})
|
|
|
+
|
|
|
.factory('odoo', function (xmlrpc, methodCallManager) {
|
|
|
return {
|
|
|
auth: function (config, success, error) {
|
|
@@ -10,9 +47,11 @@ angular.module('odoo.factories', ['xml-rpc'])
|
|
|
}
|
|
|
|
|
|
success({ id: response, username: config.username, password: config.password });
|
|
|
+ }, function (xmlrpcError) {
|
|
|
+ error(xmlrpcError);
|
|
|
});
|
|
|
},
|
|
|
- push: function (model, domain, config, success, error) {
|
|
|
+ read: 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);
|
|
@@ -20,20 +59,44 @@ angular.module('odoo.factories', ['xml-rpc'])
|
|
|
}
|
|
|
|
|
|
success(response);
|
|
|
- }, function (err) {
|
|
|
- error(err);
|
|
|
+ }, function (xmlrpcError) {
|
|
|
+ error(xmlrpcError);
|
|
|
});
|
|
|
},
|
|
|
- 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) {
|
|
|
+ create: function (model, data, config, success, error) {
|
|
|
+ xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, model, 'create', data]).then(function (response) {
|
|
|
if (!response || response['faultCode']) {
|
|
|
error(response);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
success(response);
|
|
|
- }, function (err) {
|
|
|
- error(response);
|
|
|
+ }, function (xmlrpcError) {
|
|
|
+ error(xmlrpcError);
|
|
|
+ });
|
|
|
+ },
|
|
|
+ write: function (model, data, config, success, error) {
|
|
|
+ xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, model, 'write', data], function (response) {
|
|
|
+ if (!response || response['faultCode']) {
|
|
|
+ error(response);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ success(response);
|
|
|
+ }, function (xmlrpcError) {
|
|
|
+ error(xmlrpcError);
|
|
|
+ });
|
|
|
+ },
|
|
|
+ unlink: function (model, id, config, success, error) {
|
|
|
+ xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, model, 'unlink', id], function (response) {
|
|
|
+ if (!response || response['faultCode']) {
|
|
|
+ error(response);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ success(response);
|
|
|
+ }, function (xmlrpcError) {
|
|
|
+ error(xmlrpcError);
|
|
|
});
|
|
|
}
|
|
|
}
|
|
@@ -59,39 +122,90 @@ angular.module('odoo.factories', ['xml-rpc'])
|
|
|
}
|
|
|
})
|
|
|
|
|
|
-.factory('sync', function (storage, odoo) {
|
|
|
+.factory('sync', function (storage, odoo, util) {
|
|
|
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);
|
|
|
+ // 1. Transfer all new data from mobile to server
|
|
|
+ storage.getByConstraint('partner', 'remote_id = 0', function(newPartners) {
|
|
|
+ util.asyncLoop(newPartners.length, function (loop) {
|
|
|
+ var data = [newPartners.item(loop.iteration())];
|
|
|
+
|
|
|
+ odoo.create('res.partner', data, userConfig, function (response) {
|
|
|
+ loop.next();
|
|
|
+ }, function (odooCreateError) {
|
|
|
+ loop.break();
|
|
|
+ });
|
|
|
+
|
|
|
+ // End loop
|
|
|
+ }, function() {
|
|
|
+ // 2. Transfer all modified data from mobile to server
|
|
|
+ storage.getByConstraint('partner', '', function (modifiedPartners) {
|
|
|
+ util.asyncLoop(modifiedPartners.length, function (loop) {
|
|
|
+ var data = [modifiedPartners.item(loop.iteration())];
|
|
|
+
|
|
|
+ odoo.write('res.partner', data, userConfig, function (response) {
|
|
|
+ loop.next();
|
|
|
+ }, function (odooWriteError) {
|
|
|
+ loop.break();
|
|
|
+ });
|
|
|
|
|
|
- odoo.pull('res.partner', data, userConfig, function (response) {
|
|
|
- success(response);
|
|
|
- }, function(e3) {
|
|
|
- error(e3);
|
|
|
+ // End loop
|
|
|
+ }, function () {
|
|
|
+ // 3. Delete server data from mobile
|
|
|
+ storage.getByConstraint('partner', '', function (deletedPartners) {
|
|
|
+ util.asyncLoop(deletedPartners.length, function (loop) {
|
|
|
+ var id = deletedPartners.item(loop.iteration()).remote_id;
|
|
|
+
|
|
|
+ odoo.unlink('res.partner', id, userConfig, function (response) {
|
|
|
+ loop.next();
|
|
|
+ }, function(odooUnlinkError) {
|
|
|
+ loop.break();
|
|
|
+ });
|
|
|
+
|
|
|
+ // End loop
|
|
|
+ }, function () {
|
|
|
+ // 4. Download updated data from server to mobile
|
|
|
+ odoo.read('res.partner', [[['active' = True]]], userConfig, function (updatedPartners) {
|
|
|
+ storage.deleteAllCustomers(function () {
|
|
|
+ util.asyncLoop(updatedPartners.length, funtion (loop) {
|
|
|
+ var data = updatedPartners.item(loop.iteration());
|
|
|
+
|
|
|
+ storage.saveCustomer(data, function () {
|
|
|
+ loop.next();
|
|
|
+ }, function (saveCustomerError) {
|
|
|
+ loop.break();
|
|
|
+ })
|
|
|
+
|
|
|
+ // End loop
|
|
|
+ }, function () {
|
|
|
+ success(updatedPartners);
|
|
|
+ });
|
|
|
+ }, function (deleteAllCustomersError) {
|
|
|
+ error(deleteAllCustomersError);
|
|
|
+ });
|
|
|
+ }, function (odooReadError) {
|
|
|
+ error(odooReadError);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }, function (getDeletedPartnersError) {
|
|
|
+ error(getDeletedPartnersError);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }, function (getModifiedPartnersError) {
|
|
|
+ error(getModifiedPartnersError);
|
|
|
+ });
|
|
|
});
|
|
|
- }, function (e2) {
|
|
|
- error(e2);
|
|
|
+ }, function (partnerGetByConstraintError) {
|
|
|
+ error(partnerGetByConstraintError);
|
|
|
});
|
|
|
}
|
|
|
- }, function(e1) {
|
|
|
- error(e1);
|
|
|
+ }, function(userGetError) {
|
|
|
+ error(userGetError);
|
|
|
});
|
|
|
- // 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);
|
|
|
- // });
|
|
|
}
|
|
|
}
|
|
|
})
|
|
@@ -99,63 +213,17 @@ angular.module('odoo.factories', ['xml-rpc'])
|
|
|
.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);
|
|
|
+ saveCustomer(customer, success, error) {
|
|
|
+ var sql = '';
|
|
|
+
|
|
|
+ if (customer.id) {
|
|
|
+ sql = `UPDATE partner SET remote_id = ${ customer.remote_id ? customer.remote_id : 0 }, 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 (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, 0, 0, 0, 0, 0, 0, 0, 0, 0)`;
|
|
|
+ }
|
|
|
+
|
|
|
+ db.executeSql(sql, [], function(result) {
|
|
|
+ success(sql.startsWith('INSERT') ? result.insertId : customer.id);
|
|
|
}, function(err) {
|
|
|
error(err);
|
|
|
});
|