4b457a745fcd44b0e1d621e2d5516d19ba0087ed
[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     switch(this.index) {
67         case 2: return new String(fundingSource.summary().balance);
68         case 3: return new String(fundingSource.summary().credit_total);
69         case 4: return new String(fundingSource.summary().allocation_total);
70     }
71 }
72
73 /** builds the credits grid ----- */
74 function loadFSGrid() {
75     if(!fundingSource) return;
76     var store = new dojo.data.ItemFileReadStore({data:acqfs.toStoreData([fundingSource])});
77     var model = new dojox.grid.data.DojoData(null, store, {rowsPerPage: 20, clientSort: true, query:{id:'*'}});
78     fundingSourceGrid.setModel(model);
79     fundingSourceGrid.update();
80 }
81
82
83 /** builds the credits grid ----- */
84 function loadCreditGrid() {
85     if(fsCreditGrid.isLoaded) return;
86     var store = new dojo.data.ItemFileReadStore({data:acqfa.toStoreData(fundingSource.credits())});
87     var model = new dojox.grid.data.DojoData(null, store, {rowsPerPage: 20, clientSort: true, query:{id:'*'}});
88     fsCreditGrid.setModel(model);
89     fsCreditGrid.update();
90     fsCreditGrid.isLoaded = true;
91 }
92
93 /** builds the allocations grid ----- */
94 function loadAllocationGrid() {
95     if(fsAllocationGrid.isLoaded) return;
96     var store = new dojo.data.ItemFileReadStore({data:acqfa.toStoreData(fundingSource.allocations())});
97     var model = new dojox.grid.data.DojoData(null, store, {rowsPerPage: 20, clientSort: true, query:{id:'*'}});
98     fsAllocationGrid.setModel(model);
99     fsAllocationGrid.update();
100     fsAllocationGrid.isLoaded = true;
101 }
102
103 dojo.addOnLoad(loadFS);