]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/acq/Lineitem.js
Added a lineitem summary view to describe details about related
[working/Evergreen.git] / Open-ILS / web / js / dojo / openils / acq / Lineitem.js
1 /* ---------------------------------------------------------------------------
2  * Copyright (C) 2008  Georgia Public Library Service
3  * David J. Fiander <david@fiander.info>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * ---------------------------------------------------------------------------
15  */
16
17 if(!dojo._hasResource['openils.acq.Lineitem']) {
18 dojo._hasResource['openils.acq.Lineitem'] = true;
19 dojo.provide('openils.acq.Lineitem');
20 dojo.require('dojo.data.ItemFileWriteStore');
21 dojo.require('fieldmapper.dojoData');
22 dojo.require('openils.User');
23 dojo.require('openils.Event');
24 dojo.require('openils.Util');
25
26 dojo.requireLocalization('openils.acq', 'acq');
27 var localeStrings = dojo.i18n.getLocalization('openils.acq', 'acq');
28
29
30 /** Declare the Lineitem class with dojo */
31 dojo.declare('openils.acq.Lineitem', null, {
32     /* add instance methods here if necessary */
33
34     constructor: function(args) {
35         this.lineitem = args.lineitem;
36     },
37
38     findAttr: function(name, type) {
39         var attrs = this.lineitem.attributes();
40         if(!attrs) return null;
41         for(var i = 0; i < attrs.length; i++) {
42             var attr = attrs[i];
43             if (attr.attr_type() == type && attr.attr_name() == name) 
44                 return attr.attr_value();
45         }
46     },
47
48     // returns the actual price if available, otherwise estimated price, otherwise null
49     // priority is given to local attrs, then provider attrs, then MARC attrs
50     getPrice: function() {
51         return this.getActualPrice() || this.getEstimatedPrice();
52     },
53
54     // returns the actual price, null if none
55     getActualPrice : function() {
56         return this._getPriceAttr('actual_price');
57     },
58
59     // returns the estimated price, null if none
60     getEstimatedPrice : function() {
61         return this._getPriceAttr('estimated_price');
62     },
63
64     _getPriceAttr : function(attr) {
65         var types = [
66             'lineitem_local_attr_definition', 
67             'lineitem_provider_attr_definition', 
68             'lineitem_marc_attr_definition'
69         ];
70
71         for(var t in types) {
72             if(price = this.findAttr(attr, types[t]))
73                 return {price:price, source_type: attr, source_attr: types[t]};
74         }
75
76         return null;
77     },
78
79     update: function(oncomplete) {
80         fieldmapper.standardRequest(
81             ['open-ils.acq', 'open-ils.acq.lineitem.update'],
82             {   async: true,
83                 params: [openils.User.authtoken, this.lineitem],
84                 oncomplete: function(r) {
85                     oncomplete(openils.Event.parse(r.recv().content()));
86                 }
87             }
88         );
89     },
90
91     approve: function(oncomplete) {
92         fieldmapper.standardRequest(
93             ['open-ils.acq', 'open-ils.acq.lineitem.approve'],
94             {  async: true,
95                params: [openils.User.authtoken, this.lineitem.id()],
96                oncomplete: function(r) {
97                    oncomplete(openils.Event.parse(r.recv().content()));
98                }
99             });
100     },
101
102     id: function() {
103         return this.lineitem.id();
104     },
105 });
106
107 openils.acq.Lineitem.identDisplayFields = ['isbn', 'upc', 'issn'];
108 openils.acq.Lineitem.fetchAndRender = function(liId, args, callback) {
109
110     // below are the args needed for rendering the basic summary template
111     args = dojo.mixin(args || {}, {
112         clear_marc : true, 
113         flesh_attrs : true,
114         flesh_po : true,
115         flesh_pl : true,
116         flesh_order_summary : true
117     });
118
119     fieldmapper.standardRequest(
120         ['open-ils.acq', 'open-ils.acq.lineitem.retrieve'],
121         {
122             params : [ openils.User.authtoken, liId, args ],
123
124             oncomplete : function(r) {
125                 var lineitem = openils.Util.readResponse(r);
126                 if(!lineitem) return;
127
128                 var wrapper = new openils.acq.Lineitem({lineitem : lineitem});
129                 var type = 'lineitem_marc_attr_definition';
130
131                 var idents = [];
132                 dojo.forEach(
133                     openils.acq.Lineitem.identDisplayFields,
134                     function(ident) { 
135                         if(attr = wrapper.findAttr(ident, type))
136                             idents.push(attr)
137                     }
138                 );
139
140                 var po = lineitem.purchase_order();
141                 var li = lineitem.picklist();
142
143                 var displayString = dojo.string.substitute(
144                     localeStrings.LINEITEM_SUMMARY, [
145                         wrapper.findAttr('title', type) || '',
146                         wrapper.findAttr('author', type) || '',
147                         idents.join(',') || '',
148                         lineitem.order_summary().item_count() || '0',
149                         lineitem.order_summary().recv_count() || '0',
150                         Number(lineitem.estimated_unit_price()).toFixed(2) | '',
151                         lineitem.order_summary().estimated_amount() || '0.00',
152                         lineitem.order_summary().invoice_count() || '0',
153                         lineitem.order_summary().claim_count() || '0',
154                         lineitem.order_summary().cancel_count() || '0',
155                         lineitem.id(),
156                         oilsBasePath,
157                         (po) ? po.id() : '',
158                         (po) ? po.name() : '',
159                         (li) ? li.id() : '',
160                         (li) ? li.name() : '',
161                         lineitem.order_summary().encumbrance_amount() || '0.00',
162                         lineitem.order_summary().paid_amount() || '0.00',
163                     ]
164                 );
165
166                 callback(lineitem, displayString);
167             }
168         }
169     );
170 }
171
172 openils.acq.Lineitem.attrDefs = null;
173
174 openils.acq.Lineitem.fetchAttrDefs = function(onload) {
175     if(openils.acq.Lineitem.attrDefs)
176         return onload(openils.acq.Lineitem.attrDefs);
177     fieldmapper.standardRequest(
178         ['open-ils.acq', 'open-ils.acq.lineitem_attr_definition.retrieve.all'],
179         {   async: true, 
180             params: [openils.User.authtoken],
181             oncomplete: function(r) {
182                 openils.acq.Lineitem.attrDefs = 
183                     openils.Util.readResponse(r);
184                 onload(openils.acq.Lineitem.attrDefs);
185             }
186         }
187     );
188 }
189
190
191 openils.acq.Lineitem.ModelCache = {};
192 openils.acq.Lineitem.acqlidCache = {};
193
194 openils.acq.Lineitem.createLIDStore = function(li_id, onComplete) {
195     // Fetches the details of a lineitem and builds a grid
196
197     function mkStore(r) {
198         var msg;
199         var items = [];
200         while (msg = r.recv()) {
201             var data = msg.content();
202             for (i in data.lineitem_details()) {
203                 var lid = data.lineitem_details()[i];
204                 items.push(lid);
205                 openils.acq.Lineitem.acqlidCache[lid.id()] = lid;
206             }
207         }
208
209         onComplete(acqlid.toStoreData(items));
210     }
211
212     fieldmapper.standardRequest(
213         ['open-ils.acq', 'open-ils.acq.lineitem.retrieve'],
214         { async: true,
215           params: [openils.User.authtoken, li_id,
216                    {flesh_attrs:1, flesh_li_details:1}],
217           oncomplete: mkStore
218         });
219 };
220
221 openils.acq.Lineitem.alertOnLIDSet = function(griditem, attr, oldVal, newVal) {
222     var item;
223     var updateDone = function(r) {
224         var stat = r.recv().content();
225         var evt = openils.Event.parse(stat);
226
227         if (evt) {
228             alert("Error: "+evt.desc);
229             console.dir(evt);
230             if (attr == "fund") {
231                 item.fund(oldVal);
232                 griditem.fund = oldVal;
233             } else if (attr ==  "owning_lib") {
234                 item.owning_lib(oldVal);
235                 griditem.owning_lib = oldVal;
236             }
237         }
238     };
239
240     if (oldVal == newVal) {
241         return;
242     }
243
244     item = openils.acq.Lineitem.acqlidCache[griditem.id];
245     
246     if (attr == "fund") {
247         item.fund(newVal);
248     } else if (attr ==  "owning_lib") {
249         item.owning_lib(newVal);
250     } else if (attr ==  "cn_label") {
251         item.cn_label(newVal);
252     } else if (attr ==  "barcode") {
253         item.barcode(newVal);
254     } else if (attr ==  "location") {
255         item.location(newVal);
256     } else {
257         alert("Unexpected attr in Lineitem.alertOnSet: '"+attr+"'");
258         return;
259     }
260
261     fieldmapper.standardRequest(
262         ["open-ils.acq", "open-ils.acq.lineitem_detail.update"],
263         { params: [openils.User.authtoken, item],
264           oncomplete: updateDone
265         });
266 };
267
268 openils.acq.Lineitem.deleteLID = function(id, onComplete) {
269     fieldmapper.standardRequest(
270         ['open-ils.acq', 'open-ils.acq.lineitem_detail.delete'],
271         {   async: true,
272             params: [openils.User.authtoken, id],
273             oncomplete: function(r) {
274                 msg = r.recv()
275                 stat = msg.content();
276                 onComplete(openils.Event.parse(stat));
277             }
278     });
279 };
280
281 openils.acq.Lineitem.createLID = function(fields, onCreateComplete) {
282     var lid = new acqlid()
283     for (var field in fields) {
284         lid[field](fields[field]);
285     }
286
287     fieldmapper.standardRequest(
288         ['open-ils.acq', 'open-ils.acq.lineitem_detail.create'],
289         { async: true,
290           params: [openils.User.authtoken, lid, {return_obj:1}],
291           oncomplete: function(r) {
292               var msg = r.recv();
293           var obj = msg.content();
294           openils.Event.parse_and_raise(obj);
295               if (onCreateComplete) {
296                     onCreateComplete(obj);
297               }
298           }
299         });
300 };
301
302 openils.acq.Lineitem.loadLIDGrid = function(domNode, id, layout) {
303     if (!openils.acq.Lineitem.ModelCache[id]) {
304         openils.acq.Lineitem.createLIDStore(id,
305                 function(storeData) {
306                     var store = new dojo.data.ItemFileWriteStore({data:storeData});
307                     var model = new dojox.grid.data.DojoData(null, store,
308                         {rowsPerPage: 20, clientSort:true, query:{id:'*'}});
309
310                     dojo.connect(store, "onSet",
311                                  openils.acq.Lineitem.alertOnLIDSet);
312                     openils.acq.Lineitem.ModelCache[id] = model;
313
314                     domNode.setStructure(layout);
315                     domNode.setModel(model);
316                     domNode.update();
317                 });
318     } else {
319         domNode.setModel(openils.acq.Lineitem.ModelCache[id]);
320         domNode.setStructure(layout);
321         domNode.update();
322         domNode.refresh();
323     }
324 };
325 }