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