]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/acq/financial/view_funding_source.js
LP#1350371 PO name on create w/ dupe detect
[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
71 /** Some grid rendering accessor functions ----- */
72 function getOrgInfo(rowIndex, item) {
73     if(!item) return ''; 
74     var owner = this.grid.store.getValue(item, 'owner'); 
75     return fieldmapper.aou.findOrgUnit(owner).shortname();
76
77 }
78
79 function getSummaryInfo(rowIndex) {
80     return new String(fundingSource.summary()[this.field]);
81 }
82
83 function getFund(rowIndex, item) {
84     if(item) {
85         var fId = this.grid.store.getValue(item, 'fund');
86         return openils.acq.Fund.retrieve(fId);
87     }
88 }
89
90 function formatFund(fund) {
91     if(fund) {
92         return '<a href="' + oilsBasePath + '/acq/fund/view/'+fund.id()+'">'+fund.code()+'</a>';
93     }
94 }
95
96 /** builds the summary grid ----- */
97 function loadFSGrid() {
98     if(!fundingSource) return;
99     var store = new dojo.data.ItemFileReadStore({data:acqfs.toStoreData([fundingSource])});
100     fundingSourceGrid.setStore(store);
101     fundingSourceGrid.render();
102 }
103
104
105 /** builds the credits grid ----- */
106 function loadCreditGrid(reset_first) {
107     if (fsCreditGrid.isLoaded) return;
108     if (reset_first) fsCreditGrid.resetStore();
109     fsCreditGrid.loadAll(
110         {"order_by": {"acqfscred": "effective_date DESC"}},
111         {"funding_source": fundingSource.id()}
112     );
113     fsCreditGrid.isLoaded = true;
114 }
115
116 function loadAllocationGrid(reset_first) {
117     if (fsAllocationGrid.isLoaded) return;
118     if (reset_first) fsCreditGrid.resetStore();
119     fsAllocationGrid.loadAll(
120         {"order_by": {"acqfa": "create_time DESC"}},
121         {"funding_source": fundingSource.id()}
122     );
123     fsAllocationGrid.isLoaded = true;
124 }
125
126 openils.Util.addOnLoad(loadFS);