]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/acq/FundingSource.js
LP1193095 lineitem batch actions sanity filters
[working/Evergreen.git] / Open-ILS / web / js / dojo / openils / acq / FundingSource.js
1 /* ---------------------------------------------------------------------------
2  * Copyright (C) 2008  Georgia Public Library Service
3  * Bill Erickson <erickson@esilibrary.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * ---------------------------------------------------------------------------
15  */
16
17 if(!dojo._hasResource['openils.acq.FundingSource']) {
18 dojo._hasResource['openils.acq.FundingSource'] = true;
19 dojo.provide('openils.acq.FundingSource');
20 dojo.require('fieldmapper.Fieldmapper');
21 dojo.require('fieldmapper.dojoData');
22
23 /** Declare the FundingSource class with dojo */
24 dojo.declare('openils.acq.FundingSource', null, {
25     /* add instance methods here if necessary */
26 });
27
28 /** cached funding_source objects */
29 openils.acq.FundingSource.cache = {};
30
31 openils.acq.FundingSource.createStore = function(onComplete) {
32     /** Fetches the list of funding_sources and builds a grid from them */
33     var ses = new OpenSRF.ClientSession('open-ils.acq');
34     var req = ses.request('open-ils.acq.funding_source.org.retrieve', 
35         openils.User.authtoken, null, {flesh_summary:1});
36
37     req.oncomplete = function(r) {
38         var msg
39         var items = [];
40         var src = null;
41         while(msg = r.recv()) {
42             src = msg.content();
43             openils.acq.FundingSource.cache[src.id()] = src;
44             items.push(src);
45         }
46         onComplete(acqfs.toStoreData(items));
47     };
48
49     req.send();
50 };
51
52
53
54 /**
55  * Create a new funding source object
56  * @param fields Key/value pairs used to create the new funding source
57  */
58 openils.acq.FundingSource.create = function(fields, onCreateComplete) {
59
60     var fs = new acqfs()
61     for(var field in fields) 
62         fs[field](fields[field]);
63
64     var ses = new OpenSRF.ClientSession('open-ils.acq');
65     var req = ses.request('open-ils.acq.funding_source.create', openils.User.authtoken, fs);
66
67     req.oncomplete = function(r) {
68         var msg = r.recv();
69         var id = msg.content();
70         if(onCreateComplete)
71             onCreateComplete(id);
72     };
73     req.send();
74 };
75
76 /**
77  * Synchronous funding_source retrievel method 
78  */
79 openils.acq.FundingSource.retrieve = function(id) {
80     if(openils.acq.FundingSource.cache[id])
81         return openils.acq.FundingSource.cache[id];
82     openils.acq.FundingSource.cache[id] = fieldmapper.standardRequest(
83         ['open-ils.acq', 'open-ils.acq.funding_source.retrieve'],
84         [openils.User.authtoken, id]
85     );
86     return openils.acq.FundingSource.cache[id];
87 };
88
89
90 openils.acq.FundingSource.createCredit = function(fields, onCreateComplete) {
91
92     var fsc = new acqfscred()
93     for(var field in fields) 
94         fsc[field](fields[field]);
95
96     var ses = new OpenSRF.ClientSession('open-ils.acq');
97     var req = ses.request(
98         'open-ils.acq.funding_source_credit.create', openils.User.authtoken, fsc);
99
100     req.oncomplete = function(r) {
101         var msg = r.recv();
102         var id = msg.content();
103         if(onCreateComplete)
104             onCreateComplete(id);
105     };
106     req.send();
107 };
108
109
110 openils.acq.FundingSource.deleteFromGrid = function(grid, onComplete) {
111     var list = []
112     var selected = grid.selection.getSelected();
113     for(var rowIdx in selected) 
114         list.push(grid.model.getDatum(selected[rowIdx], 0));
115     openils.acq.FundingSource.deleteList(list, onComplete);
116 };
117
118 openils.acq.FundingSource.deleteList = function(list, onComplete) {
119     openils.acq.FundingSource._deleteList(list, 0, onComplete);
120 }
121
122 openils.acq.FundingSource._deleteList = function(list, idx, onComplete) {
123     if(idx >= list.length)    
124         return onComplete();
125
126     var ses = new OpenSRF.ClientSession('open-ils.acq');
127     var req = ses.request('open-ils.acq.funding_source.delete', openils.User.authtoken, list[idx]);
128     delete openils.acq.FundingSource.cache[list[idx]];
129
130     req.oncomplete = function(r) {
131         msg = r.recv()
132         stat = msg.content();
133         /* XXX CHECH FOR EVENT */
134         openils.acq.FundingSource._deleteList(list, ++idx, onComplete);
135     }
136     req.send();
137 };
138
139
140 } /* end dojo._hasResource[] */