123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- angular.module('odoo')
- /**
- * -----------------------------------------------------------------------------
- * Description: Local storage manager for crm stage data
- * -----------------------------------------------------------------------------
- */
- .factory('opportunitiesDataFactory', function (
- opportunitiesStorageFactory,
- leadsRemoteFactory,
- depsRevolverFactory,
- odooFactory,
- sqlFactory,
- asyncLoopFactory
- ) {
- /**
- *
- */
- var pull = function (id, success, error) {
- if (id) {
- odooFactory.read('crm.lead', [['id', '=', id]], function (response) {
- success(response);
- }, function (odooErr) {
- error(odooErr);
- });
- } else {
- odooFactory.read('crm.lead', [['type', '=', 'opportunity']], function (response) {
- success(response);
- }, function (odooErr) {
- error(odooErr);
- });
- }
- }
- /**
- *
- */
- var push = function (id, data, success, error) {
- delete data.user_email;
- leadsRemoteFactory.push(id, data, function (response) {
- success(response);
- }, function (err) {
- error(err);
- });
- }
- /**
- *
- */
- var destroy = function (id, success, error) {
- leadsRemoteFactory.destroy(id, function (response) {
- success(response);
- }, function (err) {
- error(err);
- });
- }
- /**
- *
- */
- var changeStage = function (opportunity, stageId, success, error) {
- opportunity.stage_id = stageId;
- opportunitiesStorageFactory.save(opportunity, function (opportunityId) {
- success(opportunityId);
- }, function (err) {
- error(err);
- });
- }
- /**
- *
- */
- var save = function (opportunity, success, error) {
- opportunitiesStorageFactory.save(opportunity, function (opportunityId) {
- success(opportunityId);
- }, function (err) {
- error(err);
- });
- }
- /**
- *
- */
- var get = function (constraint, success, error) {
- sqlFactory.selectByConstraint('crm_lead', constraint, function (leads) {
- success(leads);
- }, function (err) {
- error(err);
- });
- }
- /**
- *
- */
- var getAll = function (success, error) {
- get("type = 'opportunity' AND modified != 2", function (opportunities) {
- var data = [];
- for (var i = 0; i < opportunities.length; i++) {
- data.push(opportunities.item(i));
- }
- success(data);
- }, function (selectErr) {
- error(selectErr);
- });
- }
- /**
- *
- */
- var remove = function (opportunity, success, error) {
- opportunitiesStorageFactory.remove(opportunity, function (affected) {
- success(affected);
- }, function (err) {
- error(err);
- });
- }
- /**
- *
- */
- var syncNewData = function (success, error) {
- get("remote_id = 0 AND type = 'opportunity'", function (newOpportunities) {
- asyncLoopFactory(newOpportunities.length, function (loop) {
- var data = newOpportunities.item(loop.iteration());
- push(null, data, function (response) {
- loop.next();
- }, function (pushErr) {
- loop.next();
- });
- // end loop
- }, function () {
- success(newOpportunities);
- });
- }, function (getErr) {
- error(getErr);
- });
- }
- /**
- *
- */
- var syncUpdatedData = function (success, error) {
- get("remote_id != 0 AND modified = 1 AND type = 'opportunity'", function (updatedOpportunities) {
- asyncLoopFactory(updatedOpportunities.length, function (loop) {
- var data = updatedOpportunities.item(loop.iteration());
- console.log(data);
- push(data.remote_id, data, function (response) {
- console.log(response);
- loop.next();
- }, function (pushErr) {
- console.log(pushErr);
- loop.next();
- });
- // end loop
- }, function () {
- success(updatedOpportunities);
- });
- }, function (getErr) {
- error(getErr);
- });
- }
- /**
- *
- */
- var syncDeletedData = function (success, error) {
- get("remote_id != 0 AND modified = 2 AND type = 'opportunity'", function (deletedOpportunities) {
- asyncLoopFactory(deletedOpportunities.length, function (loop) {
- var id = deletedOpportunities.item(loop.iteration()).remote_id;
- destroy(id, function (response) {
- loop.next();
- }, function (destroyErr) {
- loop.next();
- });
- // end loop
- }, function () {
- success(deletedOpportunities);
- });
- }, function (getErr) {
- error(getErr);
- });
- }
- /**
- *
- */
- var downloadSyncData = function (success, error) {
- pull(null, function (opportunities) {
- opportunitiesStorageFactory.removeAll(function () {
- asyncLoopFactory(opportunities.length, function (loop) {
- var data = opportunities[loop.iteration()];
- data.remote_id = data.id;
- delete data.id;
- depsRevolverFactory.resolveCustomer(data.partner_id, function (customerId) {
- data.partner_id = customerId;
- opportunitiesStorageFactory.save(data, function (opportunityId) {
- data.id = opportunityId;
- loop.next();
- }, function (saveErr) {
- loop.next();
- });
-
- }, function (err) {
- loop.next();
- });
- }, function () {
- success(opportunities);
- });
- }, function (removeAllErr) {
- console.log(removeAllErr);
- error(removeAllErr);
- });
- }, function (pullErr) {
- error(pullErr);
- });
- }
- /**
- *
- */
- var sync = function (success, error) {
- syncNewData(function () {
- console.log('opportunities --> new data ok');
- syncUpdatedData(function () {
- console.log('opportunities --> updated data ok');
- syncDeletedData(function () {
- console.log('opportunities --> deleted data ok');
- downloadSyncData(function (data) {
- console.log('opportunities --> download data ok');
- success(data);
- }, function (syncErr) {
- error(syncErr);
- });
- }, function (syncErr) {
- error(syncErr);
- });
- }, function (syncErr) {
- error(syncErr);
- });
- }, function (syncErr) {
- error(syncErr);
- });
- }
- return {
- pull: pull,
- push: push,
- destroy: destroy,
- save: save,
- changeStage: changeStage,
- getAll: getAll,
- remove: remove,
- sync: sync
- }
- });
|