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');
26 dojo.requireLocalization('openils.acq', 'acq');
27 var localeStrings = dojo.i18n.getLocalization('openils.acq', 'acq');
30 /** Declare the Lineitem class with dojo */
31 dojo.declare('openils.acq.Lineitem', null, {
32 /* add instance methods here if necessary */
34 constructor: function(args) {
35 this.lineitem = args.lineitem;
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++) {
43 if (attr.attr_type() == type && attr.attr_name() == name)
44 return attr.attr_value();
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();
54 // returns the actual price, null if none
55 getActualPrice : function() {
56 return this._getPriceAttr('actual_price');
59 // returns the estimated price, null if none
60 getEstimatedPrice : function() {
61 return this._getPriceAttr('estimated_price');
64 _getPriceAttr : function(attr) {
66 'lineitem_local_attr_definition',
67 'lineitem_provider_attr_definition',
68 'lineitem_marc_attr_definition'
72 if(price = this.findAttr(attr, types[t]))
73 return {price:price, source_type: attr, source_attr: types[t]};
79 update: function(oncomplete) {
80 fieldmapper.standardRequest(
81 ['open-ils.acq', 'open-ils.acq.lineitem.update'],
83 params: [openils.User.authtoken, this.lineitem],
84 oncomplete: function(r) {
85 oncomplete(openils.Event.parse(r.recv().content()));
91 approve: function(oncomplete) {
92 fieldmapper.standardRequest(
93 ['open-ils.acq', 'open-ils.acq.lineitem.approve'],
95 params: [openils.User.authtoken, this.lineitem.id()],
96 oncomplete: function(r) {
97 oncomplete(openils.Event.parse(r.recv().content()));
103 return this.lineitem.id();
107 openils.acq.Lineitem.identDisplayFields = ['isbn', 'upc', 'issn'];
108 openils.acq.Lineitem.fetchAndRender = function(liId, args, callback) {
110 // below are the args needed for rendering the basic summary template
111 args = dojo.mixin(args || {}, {
116 flesh_order_summary : true
119 fieldmapper.standardRequest(
120 ['open-ils.acq', 'open-ils.acq.lineitem.retrieve'],
122 params : [ openils.User.authtoken, liId, args ],
124 oncomplete : function(r) {
125 var lineitem = openils.Util.readResponse(r);
126 if(!lineitem) return;
128 var wrapper = new openils.acq.Lineitem({lineitem : lineitem});
129 var type = 'lineitem_marc_attr_definition';
133 openils.acq.Lineitem.identDisplayFields,
135 if(attr = wrapper.findAttr(ident, type))
140 var po = lineitem.purchase_order();
141 var li = lineitem.picklist();
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',
158 (po) ? po.name() : '',
160 (li) ? li.name() : '',
161 lineitem.order_summary().encumbrance_amount() || '0.00',
162 lineitem.order_summary().paid_amount() || '0.00',
166 callback(lineitem, displayString);
172 openils.acq.Lineitem.attrDefs = null;
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'],
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);
191 openils.acq.Lineitem.ModelCache = {};
192 openils.acq.Lineitem.acqlidCache = {};
194 openils.acq.Lineitem.createLIDStore = function(li_id, onComplete) {
195 // Fetches the details of a lineitem and builds a grid
197 function mkStore(r) {
200 while (msg = r.recv()) {
201 var data = msg.content();
202 for (i in data.lineitem_details()) {
203 var lid = data.lineitem_details()[i];
205 openils.acq.Lineitem.acqlidCache[lid.id()] = lid;
209 onComplete(acqlid.toStoreData(items));
212 fieldmapper.standardRequest(
213 ['open-ils.acq', 'open-ils.acq.lineitem.retrieve'],
215 params: [openils.User.authtoken, li_id,
216 {flesh_attrs:1, flesh_li_details:1}],
221 openils.acq.Lineitem.alertOnLIDSet = function(griditem, attr, oldVal, newVal) {
223 var updateDone = function(r) {
224 var stat = r.recv().content();
225 var evt = openils.Event.parse(stat);
228 alert("Error: "+evt.desc);
230 if (attr == "fund") {
232 griditem.fund = oldVal;
233 } else if (attr == "owning_lib") {
234 item.owning_lib(oldVal);
235 griditem.owning_lib = oldVal;
240 if (oldVal == newVal) {
244 item = openils.acq.Lineitem.acqlidCache[griditem.id];
246 if (attr == "fund") {
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);
257 alert("Unexpected attr in Lineitem.alertOnSet: '"+attr+"'");
261 fieldmapper.standardRequest(
262 ["open-ils.acq", "open-ils.acq.lineitem_detail.update"],
263 { params: [openils.User.authtoken, item],
264 oncomplete: updateDone
268 openils.acq.Lineitem.deleteLID = function(id, onComplete) {
269 fieldmapper.standardRequest(
270 ['open-ils.acq', 'open-ils.acq.lineitem_detail.delete'],
272 params: [openils.User.authtoken, id],
273 oncomplete: function(r) {
275 stat = msg.content();
276 onComplete(openils.Event.parse(stat));
281 openils.acq.Lineitem.createLID = function(fields, onCreateComplete) {
282 var lid = new acqlid()
283 for (var field in fields) {
284 lid[field](fields[field]);
287 fieldmapper.standardRequest(
288 ['open-ils.acq', 'open-ils.acq.lineitem_detail.create'],
290 params: [openils.User.authtoken, lid, {return_obj:1}],
291 oncomplete: function(r) {
293 var obj = msg.content();
294 openils.Event.parse_and_raise(obj);
295 if (onCreateComplete) {
296 onCreateComplete(obj);
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:'*'}});
310 dojo.connect(store, "onSet",
311 openils.acq.Lineitem.alertOnLIDSet);
312 openils.acq.Lineitem.ModelCache[id] = model;
314 domNode.setStructure(layout);
315 domNode.setModel(model);
319 domNode.setModel(openils.acq.Lineitem.ModelCache[id]);
320 domNode.setStructure(layout);