]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/acq/financial/view_fund.js
aedddf1e8fd5b6a6ed586b2c564901ca4bc37233
[working/Evergreen.git] / Open-ILS / web / js / ui / default / acq / financial / view_fund.js
1 dojo.require("dijit.Dialog");
2 dojo.require('dijit.form.FilteringSelect');
3 dojo.require('dijit.layout.TabContainer');
4 dojo.require('dijit.layout.ContentPane');
5 dojo.require('dojox.grid.DataGrid');
6 dojo.require('dijit.form.CurrencyTextBox');
7 dojo.require("dijit.form.CheckBox");
8 dojo.require('dojo.data.ItemFileReadStore');
9 dojo.require("fieldmapper.OrgUtils");
10 dojo.require('openils.acq.Fund');
11 dojo.require('openils.acq.FundingSource');
12 dojo.require('openils.Event');
13 dojo.require('openils.User');
14 dojo.require('openils.Util');
15 dojo.require("openils.widget.AutoFieldWidget");
16
17 var fund = null;
18 var tagManager;
19 var xferManager;
20
21 function getSummaryInfo(rowIndex, item) {
22     if(!item) return'';
23     return new String(fund.summary()[this.field]);
24 }
25
26 function createAllocation(fields) {
27     fields.fund = fundID;
28     if(isNaN(fields.percent)) fields.percent = null;
29     if(isNaN(fields.amount)) fields.amount = null;
30     openils.acq.Fund.createAllocation(fields, 
31         function(r){location.href = location.href;});
32 }
33 function getOrgInfo(rowIndex, item) {
34     if(!item) return ''; 
35     var owner = this.grid.store.getValue(item, 'org'); 
36     return fieldmapper.aou.findOrgUnit(owner).shortname();
37
38 }
39
40 function getXferDest(rowIndex, item) {
41     if(!item) return '';
42     var xfer_destination = this.grid.store.getValue(item, 'xfer_destination');
43     if(!(item && xfer_destination)) return '';
44     return xfer_destination;
45 }
46
47 function loadFundGrid() {
48     var store = new dojo.data.ItemFileReadStore({data:acqf.toStoreData([fund])});
49     fundGrid.setStore(store);
50     fundGrid.render();
51 }
52
53 function loadAllocationGrid() {
54     if(fundAllocationGrid.isLoaded) return;
55     var store = new dojo.data.ItemFileReadStore({data:acqfa.toStoreData(fund.allocations())});
56     fundAllocationGrid.setStore(store);
57     fundAllocationGrid.render();
58     fundAllocationGrid.isLoaded = true;
59 }
60
61 function loadDebitGrid() {
62     if(fundDebitGrid.isLoaded) return;
63     var store = new dojo.data.ItemFileReadStore({data:acqfa.toStoreData(fund.debits())});
64     fundDebitGrid.setStore(store);
65     fundDebitGrid.render();
66     fundDebitGrid.isLoaded = true;
67 }
68
69 function fetchFund() {
70     fieldmapper.standardRequest(
71         ['open-ils.acq', 'open-ils.acq.fund.retrieve'],
72         {   async: true,
73             params: [
74                 openils.User.authtoken, fundID, 
75                 {flesh_summary:1, flesh_allocations:1, flesh_debits:1, flesh_tags:1} 
76                 /* TODO grab allocations and debits only on as-needed basis */
77             ],
78             oncomplete: function(r) {
79                 fund = r.recv().content();
80                 loadFundGrid(fund);
81             }
82         }
83     );
84 }
85
86 function TransferManager() {
87     var self = this;
88
89     this._init = function() {
90         new openils.widget.AutoFieldWidget({
91             "fmField": "fund",
92             /* We're not really using LIDs here, we just need some class
93              * that has a fund field to take advantage of AutoFieldWidget's
94              * magic. */
95             "fmClass": "acqlid",
96             "labelFormat": ["${0} (${1})", "code", "year"],
97             "searchFormat": ["${0} (${1})", "code", "year"],
98             "searchFilter": {"active": "t"}, /* consider making it possible
99                                                 to select inactive? */
100             "parentNode": dojo.byId("oils-acq-fund-xfer-d-selector"),
101             "orgLimitPerms": ["ADMIN_ACQ_FUND"], /* XXX is there a more
102                                                     appropriate permission
103                                                     for this? */
104             "dijitArgs": {
105                 "onChange": function() {
106                     openils.Util[
107                         this.item.currency_type == fund.currency_type() ?
108                             "hide" : "show"
109                     ]("oils-acq-fund-xfer-dest-amount", "table-row");
110                 }
111             },
112             "forceSync": true
113         }).build(function(w, ww) { self.fundSelector = w; });
114
115         dijit.byId("oils-acq-fund-xfer-same-o-d").onChange = function() {
116             dijit.byId("oils-acq-fund-xfer-d-amount").attr(
117                 "disabled", this.attr("checked")
118             );
119         }
120     };
121
122     this._init();
123
124     this.clearFundSelector = function() {
125         if (this.fundSelector.attr("value"))
126             this.fundSelector.attr("value", "");
127     };
128
129     this.setFundName = function(fund) {
130         dojo.byId("oils-acq-fund-xfer-name-fund").innerHTML =
131             fund.code() + " (" + fund.year() + ") / " + fund.name();
132     };
133
134     this.submit = function() {
135         var values = xferDialog.getValues();
136         var dfund = this.fundSelector.item;
137         var dfund_id = typeof(dfund.id) == "object" ? dfund.id[0] : dfund.id;
138
139         if (dfund_id == fund.id()) {
140             alert(localeStrings.FUND_XFER_SAME_SOURCE_AND_DEST);
141             return false;
142         }
143         if (confirm(localeStrings.FUND_XFER_CONFIRM)) {
144             fieldmapper.standardRequest(
145                 ["open-ils.acq", "open-ils.acq.funds.transfer_money"], {
146                     "params": [
147                         openils.User.authtoken,
148                         fund.id(),
149                         values.o_amount,
150                         dfund_id,
151                         (dfund.currency_type != fund.currency_type() &&
152                             values.same_o_d.length) ? null : values.d_amount,
153                         values.note
154                     ],
155                     "async": true,
156                     "oncomplete": function(r) {
157                         if (openils.Util.readResponse(r) == 1) {
158                             location.href = location.href;
159                         }
160                     }
161                 }
162             );
163         }
164     };
165 }
166
167 function load() {
168     tagManager = new TagManager(dojo.byId("oils-acq-tag-manager-display"));
169     tagManager.prepareTagSelector(tagSelector);
170
171     xferManager = new TransferManager();
172
173     fetchFund();
174 }
175
176 openils.Util.addOnLoad(load);