1 /* ---------------------------------------------------------------------------
2 * Copyright (C) 2008 Georgia Public Library Service
3 * David J. Fiander <david@fiander.info>
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.
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 * ---------------------------------------------------------------------------
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');
28 dojo.requireLocalization('openils.acq', 'acq');
29 var localeStrings = dojo.i18n.getLocalization('openils.acq', 'acq');
32 /** Declare the Lineitem class with dojo */
33 dojo.declare('openils.acq.Lineitem', null, {
34 /* add instance methods here if necessary */
36 constructor: function(args) {
37 this.lineitem = args.lineitem;
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++) {
45 if (attr.attr_type() == type && attr.attr_name() == name)
46 return attr.attr_value();
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();
56 // returns the actual price, null if none
57 getActualPrice : function() {
58 return this._getPriceAttr('actual_price');
61 // returns the estimated price, null if none
62 getEstimatedPrice : function() {
63 return this._getPriceAttr('estimated_price');
66 _getPriceAttr : function(attr) {
68 'lineitem_local_attr_definition',
69 'lineitem_provider_attr_definition',
70 'lineitem_marc_attr_definition'
74 if(price = this.findAttr(attr, types[t]))
75 return {price:price, source_type: attr, source_attr: types[t]};
81 update: function(oncomplete) {
82 fieldmapper.standardRequest(
83 ['open-ils.acq', 'open-ils.acq.lineitem.update'],
85 params: [openils.User.authtoken, this.lineitem],
86 oncomplete: function(r) {
87 oncomplete(openils.Event.parse(r.recv().content()));
93 approve: function(oncomplete) {
94 fieldmapper.standardRequest(
95 ['open-ils.acq', 'open-ils.acq.lineitem.approve'],
97 params: [openils.User.authtoken, this.lineitem.id()],
98 oncomplete: function(r) {
99 oncomplete(openils.Event.parse(r.recv().content()));
105 return this.lineitem.id();
109 openils.acq.Lineitem.identDisplayFields = ['isbn', 'upc', 'issn'];
110 openils.acq.Lineitem.fetchAndRender = function(liId, args, callback) {
112 // below are the args needed for rendering the basic summary template
113 args = dojo.mixin(args || {}, {
118 flesh_order_summary : true
121 fieldmapper.standardRequest(
122 ['open-ils.acq', 'open-ils.acq.lineitem.retrieve.authoritative'],
124 params : [ openils.User.authtoken, liId, args ],
126 oncomplete : function(r) {
127 var lineitem = openils.Util.readResponse(r);
128 if(!lineitem) return;
130 var wrapper = new openils.acq.Lineitem({lineitem : lineitem});
131 var type = 'lineitem_marc_attr_definition';
135 openils.acq.Lineitem.identDisplayFields,
137 if(attr = wrapper.findAttr(ident, type))
142 var po = lineitem.purchase_order();
143 var pl = lineitem.picklist();
147 if(pl.name() == '') // special pl
151 liLink = oilsBasePath + '/acq/po/view/' + po.id() + '/' + lineitem.id();
152 if(po.order_date()) {
153 var date = dojo.date.stamp.fromISOString(po.order_date());
155 orderDate = dojo.date.locale.format(date, {selector:'date'});
160 var displayString = dojo.string.substitute(
161 localeStrings.LINEITEM_SUMMARY, [
162 wrapper.findAttr('title', type) || '',
163 wrapper.findAttr('author', type) || '',
164 idents.join(',') || '',
165 lineitem.order_summary().item_count() || '0',
166 lineitem.order_summary().recv_count() || '0',
167 Number(lineitem.estimated_unit_price()).toFixed(2) | '',
168 lineitem.order_summary().estimated_amount() || '0.00',
169 lineitem.order_summary().invoice_count() || '0',
170 lineitem.order_summary().claim_count() || '0',
171 lineitem.order_summary().cancel_count() || '0',
175 (po) ? po.name() : '',
177 (pl) ? pl.name() : '',
178 lineitem.order_summary().encumbrance_amount() || '0.00',
179 lineitem.order_summary().paid_amount() || '0.00',
182 (po) ? 'foo' : '', // forces class='hiddenfoo' i.e. not hidden
183 (pl) ? 'foo' : '', // ditto
187 callback(lineitem, displayString);
193 openils.acq.Lineitem.attrDefs = null;
195 openils.acq.Lineitem.fetchAttrDefs = function(onload) {
196 if(openils.acq.Lineitem.attrDefs)
197 return onload(openils.acq.Lineitem.attrDefs);
198 fieldmapper.standardRequest(
199 ['open-ils.acq', 'open-ils.acq.lineitem_attr_definition.retrieve.all'],
201 params: [openils.User.authtoken],
202 oncomplete: function(r) {
203 openils.acq.Lineitem.attrDefs =
204 openils.Util.readResponse(r);
205 onload(openils.acq.Lineitem.attrDefs);
212 openils.acq.Lineitem.ModelCache = {};
213 openils.acq.Lineitem.acqlidCache = {};
215 openils.acq.Lineitem.createLIDStore = function(li_id, onComplete) {
216 // Fetches the details of a lineitem and builds a grid
218 function mkStore(r) {
221 while (msg = r.recv()) {
222 var data = msg.content();
223 for (i in data.lineitem_details()) {
224 var lid = data.lineitem_details()[i];
226 openils.acq.Lineitem.acqlidCache[lid.id()] = lid;
230 onComplete(acqlid.toStoreData(items));
233 fieldmapper.standardRequest(
234 ['open-ils.acq', 'open-ils.acq.lineitem.retrieve'],
236 params: [openils.User.authtoken, li_id,
237 {flesh_attrs:1, flesh_li_details:1}],
242 openils.acq.Lineitem.alertOnLIDSet = function(griditem, attr, oldVal, newVal) {
244 var updateDone = function(r) {
245 var stat = r.recv().content();
246 var evt = openils.Event.parse(stat);
249 alert("Error: "+evt.desc);
251 if (attr == "fund") {
253 griditem.fund = oldVal;
254 } else if (attr == "owning_lib") {
255 item.owning_lib(oldVal);
256 griditem.owning_lib = oldVal;
261 if (oldVal == newVal) {
265 item = openils.acq.Lineitem.acqlidCache[griditem.id];
267 if (attr == "fund") {
269 } else if (attr == "owning_lib") {
270 item.owning_lib(newVal);
271 } else if (attr == "cn_label") {
272 item.cn_label(newVal);
273 } else if (attr == "barcode") {
274 item.barcode(newVal);
275 } else if (attr == "location") {
276 item.location(newVal);
278 alert("Unexpected attr in Lineitem.alertOnSet: '"+attr+"'");
282 fieldmapper.standardRequest(
283 ["open-ils.acq", "open-ils.acq.lineitem_detail.update"],
284 { params: [openils.User.authtoken, item],
285 oncomplete: updateDone
289 openils.acq.Lineitem.deleteLID = function(id, onComplete) {
290 fieldmapper.standardRequest(
291 ['open-ils.acq', 'open-ils.acq.lineitem_detail.delete'],
293 params: [openils.User.authtoken, id],
294 oncomplete: function(r) {
296 stat = msg.content();
297 onComplete(openils.Event.parse(stat));
302 openils.acq.Lineitem.createLID = function(fields, onCreateComplete) {
303 var lid = new acqlid()
304 for (var field in fields) {
305 lid[field](fields[field]);
308 fieldmapper.standardRequest(
309 ['open-ils.acq', 'open-ils.acq.lineitem_detail.create'],
311 params: [openils.User.authtoken, lid, {return_obj:1}],
312 oncomplete: function(r) {
314 var obj = msg.content();
315 openils.Event.parse_and_raise(obj);
316 if (onCreateComplete) {
317 onCreateComplete(obj);
323 openils.acq.Lineitem.loadLIDGrid = function(domNode, id, layout) {
324 if (!openils.acq.Lineitem.ModelCache[id]) {
325 openils.acq.Lineitem.createLIDStore(id,
326 function(storeData) {
327 var store = new dojo.data.ItemFileWriteStore({data:storeData});
328 var model = new dojox.grid.data.DojoData(null, store,
329 {rowsPerPage: 20, clientSort:true, query:{id:'*'}});
331 dojo.connect(store, "onSet",
332 openils.acq.Lineitem.alertOnLIDSet);
333 openils.acq.Lineitem.ModelCache[id] = model;
335 domNode.setStructure(layout);
336 domNode.setModel(model);
340 domNode.setModel(openils.acq.Lineitem.ModelCache[id]);
341 domNode.setStructure(layout);