78a144677d82ec76c5ce114988829ab59a164d88
[Evergreen.git] /
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.Grid');
8
9 dojo.require("fieldmapper.OrgUtils");
10 dojo.require('openils.acq.FundingSource');
11 dojo.require('openils.acq.Fund');
12 dojo.require('openils.Event');
13     
14 var ses = new OpenSRF.ClientSession('open-ils.acq');
15 var fundingSource = null;
16
17 function resetPage() {
18     fundingSource = null;
19     fsCreditGrid.isLoaded = false;
20     fsAllocationGrid.isLoaded = false;
21     loadFS();
22 }
23
24 /** creates a new funding_source_credit from the dialog ----- */
25 function applyFSCredit(fields) {
26     fields.funding_source = fundingSourceID;
27     openils.acq.FundingSource.createCredit(fields, resetPage);
28 }
29
30 function applyFSAllocation(fields) {
31     fields.funding_source = fundingSourceID;
32     if(isNaN(fields.percent)) fields.percent = null;
33     if(isNaN(fields.amount)) fields.amount = null;
34     openils.acq.Fund.createAllocation(fields, resetPage);
35 }
36
37 /** fetch the fleshed funding source ----- */
38 function loadFS() {
39     var req = ses.request(
40         'open-ils.acq.funding_source.retrieve', 
41         openils.User.authtoken, fundingSourceID, 
42         {flesh_summary:1, flesh_credits:1,flesh_allocations:1}
43     );
44
45     req.oncomplete = function(r) {
46         var msg = req.recv();
47         fundingSource = msg.content();
48         var evt = openils.Event.parse(fundingSource);
49         if(evt) {
50             alert(evt);
51             return;
52         }
53         loadFSGrid();
54     }
55     req.send();
56 }
57
58 /** Some grid rendering accessor functions ----- */
59 function getOrgInfo(rowIndex) {
60     data = fundingSourceGrid.model.getRow(rowIndex);
61     if(!data) return;
62     return fieldmapper.aou.findOrgUnit(data.owner).shortname();
63 }
64
65 function getSummaryInfo(rowIndex) {
66     return new String(fundingSource.summary()[this.field]);
67 }
68
69 /** builds the credits grid ----- */
70 function loadFSGrid() {
71     if(!fundingSource) return;
72     var store = new dojo.data.ItemFileReadStore({data:acqfs.toStoreData([fundingSource])});
73     var model = new dojox.grid.data.DojoData(null, store, {rowsPerPage: 20, clientSort: true, query:{id:'*'}});
74     fundingSourceGrid.setModel(model);
75     fundingSourceGrid.update();
76 }
77
78
79 /** builds the credits grid ----- */
80 function loadCreditGrid() {
81     if(fsCreditGrid.isLoaded) return;
82     var store = new dojo.data.ItemFileReadStore({data:acqfa.toStoreData(fundingSource.credits())});
83     var model = new dojox.grid.data.DojoData(null, store, {rowsPerPage: 20, clientSort: true, query:{id:'*'}});
84     fsCreditGrid.setModel(model);
85     fsCreditGrid.update();
86     fsCreditGrid.isLoaded = true;
87 }
88
89 /** builds the allocations grid ----- */
90 function loadAllocationGrid() {
91     if(fsAllocationGrid.isLoaded) return;
92     var store = new dojo.data.ItemFileReadStore({data:acqfa.toStoreData(fundingSource.allocations())});
93     var model = new dojox.grid.data.DojoData(null, store, {rowsPerPage: 20, clientSort: true, query:{id:'*'}});
94     fsAllocationGrid.setModel(model);
95     fsAllocationGrid.update();
96     fsAllocationGrid.isLoaded = true;
97 }
98
99 dojo.addOnLoad(loadFS);