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');
28 /** Declare the Lineitem class with dojo */
29 dojo.declare('openils.acq.Lineitem', null, {
30 /* add instance methods here if necessary */
32 constructor: function(args) {
33 this.lineitem = args.lineitem;
36 findAttr: function(name, type) {
37 var attrs = this.lineitem.attributes();
38 if(!attrs) return null;
39 for(var i = 0; i < attrs.length; i++) {
41 if (attr.attr_type() == type && attr.attr_name() == name)
42 return attr.attr_value();
46 // returns the actual price if available, otherwise estimated price, otherwise null
47 // priority is given to local attrs, then provider attrs, then MARC attrs
48 getPrice: function() {
49 return this.getActualPrice() || this.getEstimatedPrice();
52 // returns the actual price, null if none
53 getActualPrice : function() {
54 return this._getPriceAttr('actual_price');
57 // returns the estimated price, null if none
58 getEstimatedPrice : function() {
59 return this._getPriceAttr('estimated_price');
62 _getPriceAttr : function(attr) {
64 'lineitem_local_attr_definition',
65 'lineitem_provider_attr_definition',
66 'lineitem_marc_attr_definition'
70 if(price = this.findAttr(attr, types[t]))
71 return {price:price, source_type: attr, source_attr: types[t]};
77 update: function(oncomplete) {
78 fieldmapper.standardRequest(
79 ['open-ils.acq', 'open-ils.acq.lineitem.update'],
81 params: [openils.User.authtoken, this.lineitem],
82 oncomplete: function(r) {
83 oncomplete(openils.Event.parse(r.recv().content()));
89 approve: function(oncomplete) {
90 fieldmapper.standardRequest(
91 ['open-ils.acq', 'open-ils.acq.lineitem.approve'],
93 params: [openils.User.authtoken, this.lineitem.id()],
94 oncomplete: function(r) {
95 oncomplete(openils.Event.parse(r.recv().content()));
101 return this.lineitem.id();
105 openils.acq.Lineitem.ModelCache = {};
106 openils.acq.Lineitem.acqlidCache = {};
108 openils.acq.Lineitem.createLIDStore = function(li_id, onComplete) {
109 // Fetches the details of a lineitem and builds a grid
111 function mkStore(r) {
114 while (msg = r.recv()) {
115 var data = msg.content();
116 for (i in data.lineitem_details()) {
117 var lid = data.lineitem_details()[i];
119 openils.acq.Lineitem.acqlidCache[lid.id()] = lid;
123 onComplete(acqlid.toStoreData(items));
126 fieldmapper.standardRequest(
127 ['open-ils.acq', 'open-ils.acq.lineitem.retrieve'],
129 params: [openils.User.authtoken, li_id,
130 {flesh_attrs:1, flesh_li_details:1}],
135 openils.acq.Lineitem.alertOnLIDSet = function(griditem, attr, oldVal, newVal) {
137 var updateDone = function(r) {
138 var stat = r.recv().content();
139 var evt = openils.Event.parse(stat);
142 alert("Error: "+evt.desc);
144 if (attr == "fund") {
146 griditem.fund = oldVal;
147 } else if (attr == "owning_lib") {
148 item.owning_lib(oldVal);
149 griditem.owning_lib = oldVal;
154 if (oldVal == newVal) {
158 item = openils.acq.Lineitem.acqlidCache[griditem.id];
160 if (attr == "fund") {
162 } else if (attr == "owning_lib") {
163 item.owning_lib(newVal);
164 } else if (attr == "cn_label") {
165 item.cn_label(newVal);
166 } else if (attr == "barcode") {
167 item.barcode(newVal);
168 } else if (attr == "location") {
169 item.location(newVal);
171 alert("Unexpected attr in Lineitem.alertOnSet: '"+attr+"'");
175 fieldmapper.standardRequest(
176 ["open-ils.acq", "open-ils.acq.lineitem_detail.update"],
177 { params: [openils.User.authtoken, item],
178 oncomplete: updateDone
182 openils.acq.Lineitem.deleteLID = function(id, onComplete) {
183 fieldmapper.standardRequest(
184 ['open-ils.acq', 'open-ils.acq.lineitem_detail.delete'],
186 params: [openils.User.authtoken, id],
187 oncomplete: function(r) {
189 stat = msg.content();
190 onComplete(openils.Event.parse(stat));
195 openils.acq.Lineitem.createLID = function(fields, onCreateComplete) {
196 var lid = new acqlid()
197 for (var field in fields) {
198 lid[field](fields[field]);
201 fieldmapper.standardRequest(
202 ['open-ils.acq', 'open-ils.acq.lineitem_detail.create'],
204 params: [openils.User.authtoken, lid, {return_obj:1}],
205 oncomplete: function(r) {
207 var obj = msg.content();
208 openils.Event.parse_and_raise(obj);
209 if (onCreateComplete) {
210 onCreateComplete(obj);
216 openils.acq.Lineitem.loadLIDGrid = function(domNode, id, layout) {
217 if (!openils.acq.Lineitem.ModelCache[id]) {
218 openils.acq.Lineitem.createLIDStore(id,
219 function(storeData) {
220 var store = new dojo.data.ItemFileWriteStore({data:storeData});
221 var model = new dojox.grid.data.DojoData(null, store,
222 {rowsPerPage: 20, clientSort:true, query:{id:'*'}});
224 dojo.connect(store, "onSet",
225 openils.acq.Lineitem.alertOnLIDSet);
226 openils.acq.Lineitem.ModelCache[id] = model;
228 domNode.setStructure(layout);
229 domNode.setModel(model);
233 domNode.setModel(openils.acq.Lineitem.ModelCache[id]);
234 domNode.setStructure(layout);