]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/acq/lineitem/related.js
0f3b91b833f6bd8d58a04d3ac78ccd32b47094d7
[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('fieldmapper.OrgUtils');
8
9 dojo.requireLocalization('openils.acq', 'acq');
10 var localeStrings = dojo.i18n.getLocalization('openils.acq', 'acq');
11
12 var liTable;
13 var identTarget;
14 var bibRecord;
15 var paramPL;
16 var paramPO;
17
18 function fetchLi() {
19     fieldmapper.standardRequest(
20         ["open-ils.acq", "open-ils.acq.lineitem.retrieve.authoritative"], {
21             "async": true,
22             "params": [openils.User.authtoken, targetId, {
23                 "flesh_attrs": true,
24                 "flesh_li_details": true,
25                 "flesh_fund_debit": true,
26                 "flesh_cancel_reason": true
27             }],
28             "oncomplete": function(r) {
29                 var li = openils.Util.readResponse(r);
30                 fetchBib(li.eg_bib_id());
31             }
32         }
33     );
34 }
35
36
37 function fetchRelated() {
38     var method = 'open-ils.acq.lineitems_for_bib.by_lineitem_id';
39     if(identTarget == 'bib')
40         var method = 'open-ils.acq.lineitems_for_bib.by_bib_id';
41
42     var total = 0;
43     fieldmapper.standardRequest(
44         ["open-ils.acq", method], {
45             "async": true,
46             "params": [openils.User.authtoken, targetId, {
47                 "flesh_attrs": true,
48                 "flesh_notes": true,
49                 "flesh_cancel_reason": true
50             }],
51             "onresponse": function(r) {
52                 var resp = openils.Util.readResponse(r);
53                 if (resp) {
54                     total++;
55                     liTable.show("list");
56                     liTable.addLineitem(resp);
57                 }
58             }
59         }
60     );
61 }
62
63 function fetchBib(bibId) {
64     bibId = bibId || targetId;
65     new openils.BibTemplate({ 
66         record : bibId, 
67         org_unit : fieldmapper.aou.findOrgUnit(openils.User.user.ws_ou()).shortname()
68     }).render();
69
70     new openils.PermaCrud().retrieve('bre', bibId, {
71         oncomplete : function(r) {
72             bibRecord = openils.Util.readResponse(r);
73             // render bib details
74             // perhaps we just pull these from the beating heart of bibtemplate
75         }
76     }) 
77 }
78
79 function createLi(oncomplete) {
80     return function() {
81         progressDialog.show();
82         liTable.reset();
83         fieldmapper.standardRequest(
84             ["open-ils.acq", "open-ils.acq.biblio.create_by_id"], {
85                 "params": [
86                     openils.User.authtoken, [bibRecord.id()], {
87                         "flesh_attrs": true,
88                         "flesh_cancel_reason": true,
89                         "flesh_notes": true
90                     }
91                 ],
92                 "async": false,
93                 "onresponse": function(r) {
94                     var li = openils.Util.readResponse(r);
95                     if (typeof(li) == "object") {
96                         liTable.show("list");
97                         liTable.addLineitem(li);
98                         dojo.query(
99                             "input[name='selectbox']", liTable._findLiRow(li)
100                         )[0].checked = true;
101                     }
102                 },
103                 "oncomplete": function() {
104                     progressDialog.hide();
105                     oncomplete();
106                 }
107             }
108         );
109     };
110 }
111
112 function prepareButtons() {
113     addToPlButton.onClick = createLi(
114         function() { /* oncomplete */
115             acqLitSavePlDialog.show();
116         }
117     );
118     addToPoButton.onClick = createLi(
119         function() { /* oncomplete */
120             addToPoDialog.show();
121         }
122     );
123     createPoButton.onClick = createLi(
124         function() { /* oncomplete */
125             liTable._loadPOSelect();
126             acqLitPoCreateDialog.show();
127         }
128     );
129 }
130
131 function load() {
132     var cgi = new openils.CGI();
133
134     identTarget = cgi.param('target');
135     paramPL = cgi.param('pl');
136 //    paramPO = cgi.param('po');
137
138     if (identTarget == 'bib') {
139         fetchBib();
140     } else {
141         fetchLi(); 
142     }
143
144     liTable = new AcqLiTable();
145     liTable.reset();
146     liTable._isRelatedViewer = true;
147
148     prepareButtons();
149     fetchRelated();
150     dojo.connect(addToPoSave, 'onClick', addToPo)
151     openils.Util.registerEnterHandler(addToPoInput.domNode, addToPo);
152 }
153
154 var _addToPoHappened = false;
155 function addToPo(args) {
156     var poId = addToPoInput.attr('value');
157     if (!poId) return false;
158     if (_addToPoHappened) return false;
159
160     var liId =  liTable.getSelected()[0].id();
161     console.log("adding li " + liId + " to PO " + poId);
162
163     // hmm, addToPo is invoked twice for some reason...
164     _addToPoHappened = true;
165
166     fieldmapper.standardRequest(
167         ['open-ils.acq', 'open-ils.acq.purchase_order.add_lineitem'],
168         {   async : true,
169             params : [openils.User.authtoken, poId, liId],
170             oncomplete : function(r) {
171                 var resp = openils.Util.readResponse(r);
172                 if (resp.success) {
173                     location.href = oilsBasePath + '/acq/po/view/' + poId;
174                 } else {
175                     _addToPoHappened = false;
176                     if (resp.error == 'bad-po-state') {
177                         alert(localeStrings.ADD_LI_TO_PO_BAD_PO_STATE);
178                     } else if (resp.error == 'bad-li-state') {
179                         alert(localeStrings.ADD_LI_TO_PO_BAD_LI_STATE);
180                     }
181                 }
182             }
183         }
184     );
185
186     addToPoDialog.hide();
187     return false; // prevent form submission
188 }
189
190 openils.Util.addOnLoad(load);