]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/acq/Fund.js
LP#1350371 PO name on create w/ dupe detect
[working/Evergreen.git] / Open-ILS / web / js / dojo / openils / acq / Fund.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.Fund']) {
18 dojo._hasResource['openils.acq.Fund'] = true;
19 dojo.provide('openils.acq.Fund');
20 dojo.require('fieldmapper.Fieldmapper');
21 dojo.require('fieldmapper.dojoData');
22 dojo.require('openils.Event');
23 dojo.require('openils.Util');
24
25 /** Declare the Fund class with dojo */
26 dojo.declare('openils.acq.Fund', null, {
27     /* add instance methods here if necessary */
28 });
29
30 openils.acq.Fund.cache = {};
31 openils.acq.Fund._cachecomplete = false;
32
33 openils.acq.Fund.createStore = function(onComplete, limitPerm) {
34     /** Fetches the list of funds and builds a grid from them */
35
36     function mkStore(r) {
37         var src;
38         var items = [];
39         while(src = openils.Util.readResponse(r)) {
40             openils.acq.Fund.cache[src.id()] = src;
41             items.push(src);
42         }
43             openils.acq.Fund._cachecomplete = true;
44         onComplete(acqf.toStoreData(items));
45     }
46
47     fieldmapper.standardRequest(
48         ['open-ils.acq', 'open-ils.acq.fund.org.retrieve'],
49         {   async: true,
50             params: [openils.User.authtoken, null, {flesh_summary:1, limit_perm:limitPerm}],
51             oncomplete: mkStore
52         }
53     );
54 };
55
56 /**
57  * Create a new fund
58  * @param fields Key/value pairs used to create the new fund
59  */
60 openils.acq.Fund.create = function(fields, onCreateComplete) {
61
62     var fund = new acqf()
63     for(var field in fields) 
64         fund[field](fields[field]);
65
66     fieldmapper.standardRequest(
67         ['open-ils.acq', 'open-ils.acq.fund.create'],
68         {   async: true,
69             params: [openils.User.authtoken, fund],
70             oncomplete: function(r) {
71                 var msg = r.recv();
72                 var id = msg.content();
73                 if(onCreateComplete)
74                     onCreateComplete(id);
75             }
76         }
77     );
78 };
79
80
81 openils.acq.Fund.createAllocation = function(fields, onCreateComplete) {
82     var alloc = new acqfa()
83     for(var field in fields) 
84         alloc[field](fields[field]);
85     fieldmapper.standardRequest(
86         ['open-ils.acq', 'open-ils.acq.fund_allocation.create'],
87         {
88             async: true,
89             params: [openils.User.authtoken, alloc],
90             oncomplete: function(r) {
91                 var msg = r.recv();
92                 var id = msg.content();
93                 if(onCreateComplete)
94                     onCreateComplete(id);
95             }
96         }
97     );
98 };
99
100
101 /**
102  * Synchronous fund retrievel method 
103  */
104 openils.acq.Fund.retrieve = function(id) {
105     if(openils.acq.Fund.cache[id])
106         return openils.acq.Fund.cache[id];
107     openils.acq.Fund.cache[id] = fieldmapper.standardRequest(
108         ['open-ils.acq', 'open-ils.acq.fund.retrieve'],
109         [openils.User.authtoken, id]
110     );
111     return openils.acq.Fund.cache[id];
112 };
113
114
115 openils.acq.Fund.deleteFromGrid = function(grid, onComplete) {
116     var list = []
117     var selected = grid.selection.getSelected();
118     for(var rowIdx in selected) 
119         list.push(grid.model.getDatum(selected[rowIdx], 0));
120     openils.acq.Fund.deleteList(list, onComplete);
121 };
122
123 openils.acq.Fund.deleteList = function(list, onComplete) {
124     openils.acq.Fund._deleteList(list, 0, onComplete);
125 }
126
127 openils.acq.Fund._deleteList = function(list, idx, onComplete) {
128     if(idx >= list.length)    
129         return onComplete();
130
131     var fundId = list[idx];
132     delete openils.acq.Fund.cache[list[idx]];
133
134     fieldmapper.standardRequest(
135         ['open-ils.acq', 'open-ils.acq.fund.delete'],
136         {   async: true,
137             params: [openils.User.authtoken, fundId],
138             oncomplete: function(r) {
139                 stat = r.recv().content();
140                 /* XXX CHECH FOR EVENT */
141                 openils.acq.Fund._deleteList(list, ++idx, onComplete);
142             }
143         }
144     );
145 };
146
147 openils.acq.Fund.nameMapping = function(oncomplete) {
148     var ids = [];
149     var names = [];
150     var buildMap = function() {
151         for (var i in openils.acq.Fund.cache) {
152             var item = openils.acq.Fund.cache[i];
153             ids.push(item.id());
154             names.push(item.name());
155             oncomplete(ids, names);
156         }
157     };
158
159     if (openils.acq.Fund._cachecomplete) {
160         buildMap(oncomplete);
161     } else {
162         openils.acq.Fund.createStore(buildMap);
163     }
164 };
165
166 /**
167   * Sets the store for an existing openils.widget.FundFilteringSelect 
168   * using the funds where the user has the requested permission.
169   * @param perm The permission to check
170   * @param selector The pre-created dijit.form.FilteringSelect object.  
171   */
172
173 openils.acq.Fund.storeCache = [];
174
175 openils.acq.Fund.buildPermFundSelector = function(perm, selector) {
176     dojo.require('dojo.data.ItemFileReadStore');
177
178     function hookupStore(store) {
179         selector.store = store;
180         selector.startup();
181     }
182
183     function buildPicker(r) {
184         var msg;
185         var fundList = [];
186         while (msg = r.recv()) {
187             var fund = msg.content();
188             fundList.push(fund);
189         }
190
191         var store = new dojo.data.ItemFileReadStore({data:acqf.toStoreData(fundList)});
192
193         hookupStore(store);
194         openils.acq.Fund.storeCache[perm] = store;
195     }
196
197     if (openils.acq.Fund.storeCache[perm]) {
198         hookupStore(openils.acq.Fund.storeCache[perm]);
199     } else {
200         fieldmapper.standardRequest(
201             ['open-ils.acq', 'open-ils.acq.fund.org.retrieve'],
202             {   params: [openils.User.authtoken, null,
203                          {flesh_summary:1, limit_perm:perm}],
204                 oncomplete: buildPicker,
205                 async: true
206             }
207         )
208     }
209 }
210
211 }