Acq: several interface improvements
[working/Evergreen.git] / Open-ILS / web / js / ui / default / acq / lineitem / related.js
1 dojo.require("openils.acq.Lineitem");
2 dojo.require("openils.Util");
3 dojo.require("openils.XUL");
4 dojo.require("openils.CGI");
5 dojo.require("openils.PermaCrud");
6 dojo.require('openils.BibTemplate');
7 dojo.require('openils.widget.PCrudAutocompleteBox');
8 dojo.require('fieldmapper.OrgUtils');
9
10 dojo.requireLocalization('openils.acq', 'acq');
11 var localeStrings = dojo.i18n.getLocalization('openils.acq', 'acq');
12
13 var liTable;
14 var identTarget;
15 var bibRecord;
16 var paramPL;
17 var paramPO;
18
19 function fetchLi() {
20     fieldmapper.standardRequest(
21         ["open-ils.acq", "open-ils.acq.lineitem.retrieve.authoritative"], {
22             "async": true,
23             "params": [openils.User.authtoken, targetId, {
24                 "flesh_attrs": true,
25                 "flesh_li_details": true,
26                 "flesh_fund_debit": true,
27                 "flesh_cancel_reason": true
28             }],
29             "oncomplete": function(r) {
30                 var li = openils.Util.readResponse(r);
31                 fetchBib(li.eg_bib_id());
32             }
33         }
34     );
35 }
36
37
38 function fetchRelated() {
39     var method = 'open-ils.acq.lineitems_for_bib.by_lineitem_id';
40     if(identTarget == 'bib')
41         var method = 'open-ils.acq.lineitems_for_bib.by_bib_id';
42
43     var total = 0;
44     fieldmapper.standardRequest(
45         ["open-ils.acq", method], {
46             "async": true,
47             "params": [openils.User.authtoken, targetId, {
48                 "flesh_attrs": true,
49                 "flesh_notes": true,
50                 "flesh_cancel_reason": true
51             }],
52             "onresponse": function(r) {
53                 var resp = openils.Util.readResponse(r);
54                 if (resp) {
55                     total++;
56                     liTable.show("list");
57                     liTable.addLineitem(resp);
58                 }
59             }
60         }
61     );
62 }
63
64 function fetchBib(bibId) {
65     bibId = bibId || targetId;
66     new openils.BibTemplate({ 
67         record : bibId, 
68         org_unit : fieldmapper.aou.findOrgUnit(openils.User.user.ws_ou()).shortname()
69     }).render();
70
71     new openils.PermaCrud().retrieve('bre', bibId, {
72         oncomplete : function(r) {
73             bibRecord = openils.Util.readResponse(r);
74             // render bib details
75             // perhaps we just pull these from the beating heart of bibtemplate
76         }
77     }) 
78 }
79
80 function createLi(oncomplete) {
81     return function() {
82         progressDialog.show();
83         liTable.reset();
84         fieldmapper.standardRequest(
85             ["open-ils.acq", "open-ils.acq.biblio.create_by_id"], {
86                 "params": [
87                     openils.User.authtoken, [bibRecord.id()], {
88                         "flesh_attrs": true,
89                         "flesh_cancel_reason": true,
90                         "flesh_notes": true
91                     }
92                 ],
93                 "async": false,
94                 "onresponse": function(r) {
95                     var li = openils.Util.readResponse(r);
96                     if (typeof(li) == "object") {
97                         liTable.show("list");
98                         liTable.addLineitem(li);
99                         dojo.query(
100                             "input[name='selectbox']", liTable._findLiRow(li)
101                         )[0].checked = true;
102                     }
103                 },
104                 "oncomplete": function() {
105                     progressDialog.hide();
106                     oncomplete();
107                 }
108             }
109         );
110     };
111 }
112
113 function prepareButtons() {
114     addToPlButton.onClick = createLi(
115         function() { /* oncomplete */
116             acqLitSavePlDialog.show();
117         }
118     );
119     addToPoButton.onClick = createLi(
120         function() { /* oncomplete */
121             addToPoDialog.show();
122         }
123     );
124     createPoButton.onClick = createLi(
125         function() { /* oncomplete */
126             liTable._loadPOSelect();
127             acqLitPoCreateDialog.show();
128         }
129     );
130 }
131
132 function load() {
133     var cgi = new openils.CGI();
134
135     identTarget = cgi.param('target');
136     paramPL = cgi.param('pl');
137 //    paramPO = cgi.param('po');
138
139     if (identTarget == 'bib') {
140         fetchBib();
141     } else {
142         fetchLi(); 
143     }
144
145     liTable = new AcqLiTable();
146     liTable.enableActionsDropdownOptions("vp");
147     liTable.reset();
148     liTable._isRelatedViewer = true;
149
150     prepareButtons();
151     fetchRelated();
152
153     /* addToPoDialog now requires this function be defined to tell it what
154      * lineitem IDs to add to the PO.  Part of making it reusable in more
155      * places. */
156     addToPoDialog._get_li = function() {
157         return liTable.getSelected()[0].id();
158     };
159 }
160
161 openils.Util.addOnLoad(load);