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');
21 dojo.require('dojo.data.ItemFileWriteStore');
22 dojo.require('dojox.grid.Grid');
23 dojo.require('dojox.grid.compat._data.model');
24 dojo.require('fieldmapper.dojoData');
25 dojo.require('openils.User');
26 dojo.require('openils.Event');
27 dojo.require('openils.Util');
29 /** Declare the Lineitem class with dojo */
30 dojo.declare('openils.acq.Lineitem', null, {
31 /* add instance methods here if necessary */
33 constructor: function(args) {
34 this.lineitem = args.lineitem;
37 findAttr: function(name, type) {
38 var attrs = this.lineitem.attributes();
39 if(!attrs) return null;
40 for(var i = 0; i < attrs.length; i++) {
42 if (attr.attr_type() == type && attr.attr_name() == name)
43 return attr.attr_value();
47 // returns the actual price if available, otherwise estimated price, otherwise null
48 // priority is given to local attrs, then provider attrs, then MARC attrs
49 getPrice: function() {
50 return this.getActualPrice() || this.getEstimatedPrice();
53 // returns the actual price, null if none
54 getActualPrice : function() {
55 return this._getPriceAttr('actual_price');
58 // returns the estimated price, null if none
59 getEstimatedPrice : function() {
60 return this._getPriceAttr('estimated_price');
63 _getPriceAttr : function(attr) {
65 'lineitem_local_attr_definition',
66 'lineitem_provider_attr_definition',
67 'lineitem_marc_attr_definition'
71 if(price = this.findAttr(attr, types[t]))
72 return {price:price, source_type: attr, source_attr: types[t]};
78 update: function(oncomplete) {
79 fieldmapper.standardRequest(
80 ['open-ils.acq', 'open-ils.acq.lineitem.update'],
82 params: [openils.User.authtoken, this.lineitem],
83 oncomplete: function(r) {
84 oncomplete(openils.Event.parse(r.recv().content()));
90 approve: function(oncomplete) {
91 fieldmapper.standardRequest(
92 ['open-ils.acq', 'open-ils.acq.lineitem.approve'],
94 params: [openils.User.authtoken, this.lineitem.id()],
95 oncomplete: function(r) {
96 oncomplete(openils.Event.parse(r.recv().content()));
102 return this.lineitem.id();
106 openils.acq.Lineitem.attrDefs = null;
108 openils.acq.Lineitem.fetchAttrDefs = function(onload) {
109 if(openils.acq.Lineitem.attrDefs)
110 return onload(openils.acq.Lineitem.attrDefs);
111 fieldmapper.standardRequest(
112 ['open-ils.acq', 'open-ils.acq.lineitem_attr_definition.retrieve.all'],
114 params: [openils.User.authtoken],
115 oncomplete: function(r) {
116 openils.acq.Lineitem.attrDefs =
117 openils.Util.readResponse(r);
118 onload(openils.acq.Lineitem.attrDefs);
125 openils.acq.Lineitem.ModelCache = {};
126 openils.acq.Lineitem.acqlidCache = {};
128 openils.acq.Lineitem.createLIDStore = function(li_id, onComplete) {
129 // Fetches the details of a lineitem and builds a grid
131 function mkStore(r) {
134 while (msg = r.recv()) {
135 var data = msg.content();
136 for (i in data.lineitem_details()) {
137 var lid = data.lineitem_details()[i];
139 openils.acq.Lineitem.acqlidCache[lid.id()] = lid;
143 onComplete(acqlid.toStoreData(items));
146 fieldmapper.standardRequest(
147 ['open-ils.acq', 'open-ils.acq.lineitem.retrieve'],
149 params: [openils.User.authtoken, li_id,
150 {flesh_attrs:1, flesh_li_details:1}],
155 openils.acq.Lineitem.alertOnLIDSet = function(griditem, attr, oldVal, newVal) {
157 var updateDone = function(r) {
158 var stat = r.recv().content();
159 var evt = openils.Event.parse(stat);
162 alert("Error: "+evt.desc);
164 if (attr == "fund") {
166 griditem.fund = oldVal;
167 } else if (attr == "owning_lib") {
168 item.owning_lib(oldVal);
169 griditem.owning_lib = oldVal;
174 if (oldVal == newVal) {
178 item = openils.acq.Lineitem.acqlidCache[griditem.id];
180 if (attr == "fund") {
182 } else if (attr == "owning_lib") {
183 item.owning_lib(newVal);
184 } else if (attr == "cn_label") {
185 item.cn_label(newVal);
186 } else if (attr == "barcode") {
187 item.barcode(newVal);
188 } else if (attr == "location") {
189 item.location(newVal);
191 alert("Unexpected attr in Lineitem.alertOnSet: '"+attr+"'");
195 fieldmapper.standardRequest(
196 ["open-ils.acq", "open-ils.acq.lineitem_detail.update"],
197 { params: [openils.User.authtoken, item],
198 oncomplete: updateDone
202 openils.acq.Lineitem.deleteLID = function(id, onComplete) {
203 fieldmapper.standardRequest(
204 ['open-ils.acq', 'open-ils.acq.lineitem_detail.delete'],
206 params: [openils.User.authtoken, id],
207 oncomplete: function(r) {
209 stat = msg.content();
210 onComplete(openils.Event.parse(stat));
215 openils.acq.Lineitem.createLID = function(fields, onCreateComplete) {
216 var lid = new acqlid()
217 for (var field in fields) {
218 lid[field](fields[field]);
221 fieldmapper.standardRequest(
222 ['open-ils.acq', 'open-ils.acq.lineitem_detail.create'],
224 params: [openils.User.authtoken, lid, {return_obj:1}],
225 oncomplete: function(r) {
227 var obj = msg.content();
228 openils.Event.parse_and_raise(obj);
229 if (onCreateComplete) {
230 onCreateComplete(obj);
236 openils.acq.Lineitem.loadLIDGrid = function(domNode, id, layout) {
237 if (!openils.acq.Lineitem.ModelCache[id]) {
238 openils.acq.Lineitem.createLIDStore(id,
239 function(storeData) {
240 var store = new dojo.data.ItemFileWriteStore({data:storeData});
241 var model = new dojox.grid.data.DojoData(null, store,
242 {rowsPerPage: 20, clientSort:true, query:{id:'*'}});
244 dojo.connect(store, "onSet",
245 openils.acq.Lineitem.alertOnLIDSet);
246 openils.acq.Lineitem.ModelCache[id] = model;
248 domNode.setStructure(layout);
249 domNode.setModel(model);
253 domNode.setModel(openils.acq.Lineitem.ModelCache[id]);
254 domNode.setStructure(layout);