]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/acq/financial/view_funding_source.js
LP2061136 - Stamping 1405 DB upgrade script
[working/Evergreen.git] / Open-ILS / web / js / ui / default / acq / financial / view_funding_source.js
1 dojo.require("dijit.Dialog");
2 dojo.require('dijit.layout.TabContainer');
3 dojo.require('dijit.layout.ContentPane');
4 dojo.require("dijit.form.FilteringSelect");
5 dojo.require("dijit.form.Textarea");
6 dojo.require("dijit.form.CurrencyTextBox");
7 dojo.require('dojox.grid.DataGrid');
8 dojo.require('dojo.data.ItemFileReadStore');
9 dojo.require("fieldmapper.OrgUtils");
10 dojo.require('openils.acq.FundingSource');
11 dojo.require('openils.acq.Fund');
12 dojo.require('openils.Event');
13 dojo.require('openils.Util');
14 dojo.require('openils.widget.AutoGrid');
15     
16 var ses = new OpenSRF.ClientSession('open-ils.acq');
17 var fundingSource = null;
18
19 function resetPage(also_load_grid) {
20     fundingSource = null;
21     fsCreditGrid.isLoaded = false;
22     fsAllocationGrid.isLoaded = false;
23     loadFS(also_load_grid);
24 }
25
26 function getFund(rowIndex, item) {
27     return '';
28     //return '<a href="[% ctx.base_path %]/acq/fund/view/'+fund.id()+'">'+fund.code()+'</a>';
29 }
30
31
32 /** creates a new funding_source_credit from the dialog ----- */
33 function applyFSCredit(fields) {
34     fields.funding_source = fundingSourceID;
35     openils.acq.FundingSource.createCredit(
36         fields, function() { resetPage(loadCreditGrid); }
37     );
38 }
39
40 function applyFSAllocation(fields) {
41     fields.funding_source = fundingSourceID;
42     if(isNaN(fields.amount)) fields.amount = null;
43     openils.acq.Fund.createAllocation(
44         fields, function() { resetPage(loadAllocationGrid); }
45     );
46 }
47
48 /** fetch the fleshed funding source ----- */
49 function loadFS(also_load_grid) {
50     var req = ses.request(
51         'open-ils.acq.funding_source.retrieve', 
52         openils.User.authtoken, fundingSourceID, 
53         {flesh_summary:1, flesh_credits:1,flesh_allocations:1}
54     );
55
56     req.oncomplete = function(r) {
57         var msg = req.recv();
58         fundingSource = msg.content();
59         var evt = openils.Event.parse(fundingSource);
60         if(evt) {
61             alert(evt);
62             return;
63         }
64         loadFSGrid();
65         if (typeof(also_load_grid) == "function")
66             also_load_grid(true /* reset_first */);
67     }
68     req.send();
69
70     new openils.widget.AutoFieldWidget({
71         "fmField": "fund",
72         /* We're not really using LIDs here, we just need some class
73          * that has a fund field to take advantage of AutoFieldWidget's
74          * magic. */
75         "fmClass": "acqlid",
76         "labelFormat": ["${0} (${1})", "code", "year"],
77         "searchFormat": ["${0} (${1})", "code", "year"],
78         "searchFilter": {"active": "t"},
79         "searchOptions": {"order_by" : {"acqf":"year DESC, code"}},
80         "parentNode": dojo.byId("oils-acq-funding-source-fund-allocate"),
81         "orgLimitPerms": ["MANAGE_FUND"], //???
82         "dijitArgs": { "name" : "fund" }
83     }).build(function(w, ww) {});
84 }
85
86 /** Some grid rendering accessor functions ----- */
87 function getOrgInfo(rowIndex, item) {
88     if(!item) return ''; 
89     var owner = this.grid.store.getValue(item, 'owner'); 
90     return fieldmapper.aou.findOrgUnit(owner).shortname();
91
92 }
93
94 function getSummaryInfo(rowIndex) {
95     return new String(fundingSource.summary()[this.field]);
96 }
97
98 function getFund(rowIndex, item) {
99     if(item) {
100         var fId = this.grid.store.getValue(item, 'fund');
101         return openils.acq.Fund.retrieve(fId);
102     }
103 }
104
105 function formatFund(fund) {
106     if(fund) {
107         return '<a href="' + oilsBasePath + '/acq/fund/view/'+fund.id()+'">'+fund.code()+'</a>';
108     }
109 }
110
111 /** builds the summary grid ----- */
112 function loadFSGrid() {
113     if(!fundingSource) return;
114     var store = new dojo.data.ItemFileReadStore({data:acqfs.toStoreData([fundingSource])});
115     fundingSourceGrid.setStore(store);
116     fundingSourceGrid.render();
117 }
118
119
120 /** builds the credits grid ----- */
121 function loadCreditGrid(reset_first) {
122     if (fsCreditGrid.isLoaded) return;
123     if (reset_first) fsCreditGrid.resetStore();
124     fsCreditGrid.loadAll(
125         {"order_by": {"acqfscred": "effective_date DESC"}},
126         {"funding_source": fundingSource.id()}
127     );
128     fsCreditGrid.isLoaded = true;
129 }
130
131 function loadAllocationGrid(reset_first) {
132     if (fsAllocationGrid.isLoaded) return;
133     if (reset_first) fsCreditGrid.resetStore();
134     fsAllocationGrid.loadAll(
135         {"order_by": {"acqfa": "create_time DESC"}},
136         {"funding_source": fundingSource.id()}
137     );
138     fsAllocationGrid.isLoaded = true;
139 }
140
141 openils.Util.addOnLoad(loadFS);