LP#1350371 PO name on create w/ dupe detect
[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 dojo.require("openils.widget.AutoGrid");
17
18 var fund = null;
19 var tagManager;
20 var xferManager;
21
22 function getSummaryInfo(rowIndex, item) {
23     if(!item) return'';
24     return new String(fund.summary()[this.field]);
25 }
26
27 function createAllocation(fields) {
28     fields.fund = fundID;
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
41 function getFundingSource(rowIndex, item) {
42     if(item) {
43         var fsId = this.grid.store.getValue(item, 'funding_source');
44         return openils.acq.FundingSource.retrieve(fsId);
45     }
46 }
47
48 function formatFundingSource(fs) {
49     if(fs) {
50         return '<a href="' + oilsBasePath + '/acq/funding_source/view/'+fs.id()+'">'+fs.code()+'</a>';
51     }
52 }
53
54 function getXferDest(rowIndex, item) {
55     if(!item) return '';
56     var xfer_destination = this.grid.store.getValue(item, 'xfer_destination');
57     if(!(item && xfer_destination)) return '';
58     return xfer_destination;
59 }
60
61 function loadFundGrid() {
62     var store = new dojo.data.ItemFileReadStore({data:acqf.toStoreData([fund])});
63     fundGrid.setStore(store);
64     fundGrid.render();
65 }
66
67 function loadAllocationGrid() {
68     if(fundAllocationGrid.isLoaded) return;
69     /* XXX If we want to show allocating user with a username instead of just
70      * ID#, the following pcrud search will have to be replaced with an API
71      * call. */
72     fundAllocationGrid.loadAll({order_by : {acqfa :  'create_time DESC'}}, {fund : fundID});
73     fundAllocationGrid.isLoaded = true;
74 }
75
76 function loadDebitGrid() {
77     if(fundDebitGrid.isLoaded) return;
78     fundDebitGrid.loadAll({order_by : {acqfdeb :  'create_time DESC'}}, {fund : fundID});
79     fundDebitGrid.isLoaded = true;
80 }
81
82 function fetchFund() {
83     fieldmapper.standardRequest(
84         ['open-ils.acq', 'open-ils.acq.fund.retrieve'],
85         {   async: true,
86             params: [
87                 openils.User.authtoken, fundID, 
88                 {flesh_summary:1, flesh_tags:1} 
89             ],
90             oncomplete: function(r) {
91                 fund = r.recv().content();
92                 loadFundGrid(fund);
93             }
94         }
95     );
96 }
97
98 function TransferManager() {
99     var self = this;
100
101     this._init = function() {
102         new openils.widget.AutoFieldWidget({
103             "fmField": "fund",
104             /* We're not really using LIDs here, we just need some class
105              * that has a fund field to take advantage of AutoFieldWidget's
106              * magic. */
107             "fmClass": "acqlid",
108             "labelFormat": ["${0} (${1})", "code", "year"],
109             "searchFormat": ["${0} (${1})", "code", "year"],
110             "searchFilter": {"active": "t"}, /* consider making it possible
111                                                 to select inactive? */
112             "parentNode": dojo.byId("oils-acq-fund-xfer-d-selector"),
113             "orgLimitPerms": ["ADMIN_ACQ_FUND"], /* XXX is there a more
114                                                     appropriate permission
115                                                     for this? */
116             "dijitArgs": {
117                 "onChange": function() {
118                     openils.Util[
119                         this.item.currency_type == fund.currency_type() ?
120                             "hide" : "show"
121                     ]("oils-acq-fund-xfer-dest-amount", "table-row");
122                 }
123             },
124             "forceSync": true
125         }).build(function(w, ww) { self.fundSelector = w; });
126
127         dijit.byId("oils-acq-fund-xfer-same-o-d").onChange = function() {
128             dijit.byId("oils-acq-fund-xfer-d-amount").attr(
129                 "disabled", this.attr("checked")
130             );
131         }
132     };
133
134     this._init();
135
136     this.clearFundSelector = function() {
137         if (this.fundSelector.attr("value"))
138             this.fundSelector.attr("value", "");
139     };
140
141     this.setFundName = function(fund) {
142         dojo.byId("oils-acq-fund-xfer-name-fund").innerHTML =
143             fund.code() + " (" + fund.year() + ") / " + fund.name();
144     };
145
146     this.submit = function() {
147         var values = xferDialog.getValues();
148         var dfund = this.fundSelector.item;
149         var dfund_id = typeof(dfund.id) == "object" ? dfund.id[0] : dfund.id;
150
151         if (dfund_id == fund.id()) {
152             alert(localeStrings.FUND_XFER_SAME_SOURCE_AND_DEST);
153             return false;
154         }
155         if (confirm(localeStrings.FUND_XFER_CONFIRM)) {
156             fieldmapper.standardRequest(
157                 ["open-ils.acq", "open-ils.acq.funds.transfer_money"], {
158                     "params": [
159                         openils.User.authtoken,
160                         fund.id(),
161                         values.o_amount,
162                         dfund_id,
163                         (dfund.currency_type != fund.currency_type() &&
164                             values.same_o_d.length) ? null : values.d_amount,
165                         values.note
166                     ],
167                     "async": true,
168                     "oncomplete": function(r) {
169                         if (openils.Util.readResponse(r) == 1) {
170                             location.href = location.href;
171                         }
172                     }
173                 }
174             );
175         }
176     };
177 }
178
179 function load() {
180     tagManager = new TagManager(dojo.byId("oils-acq-tag-manager-display"));
181     tagManager.prepareTagSelector(tagSelector);
182
183     xferManager = new TransferManager();
184
185     fetchFund();
186 }
187
188 openils.Util.addOnLoad(load);