]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/acq/Lineitem.js
get the acq part of trunk working with dojo-1.2
[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
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
28 /** Declare the Lineitem class with dojo */
29 dojo.declare('openils.acq.Lineitem', null, {
30     /* add instance methods here if necessary */
31
32     constructor: function(args) {
33         this.lineitem = args.lineitem;
34     },
35
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++) {
40             var attr = attrs[i];
41             if (attr.attr_type() == type && attr.attr_name() == name) 
42                 return attr.attr_value();
43         }
44     },
45
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();
50     },
51
52     // returns the actual price, null if none
53     getActualPrice : function() {
54         return this._getPriceAttr('actual_price');
55     },
56
57     // returns the estimated price, null if none
58     getEstimatedPrice : function() {
59         return this._getPriceAttr('estimated_price');
60     },
61
62     _getPriceAttr : function(attr) {
63         var types = [
64             'lineitem_local_attr_definition', 
65             'lineitem_provider_attr_definition', 
66             'lineitem_marc_attr_definition'
67         ];
68
69         for(var t in types) {
70             if(price = this.findAttr(attr, types[t]))
71                 return {price:price, source_type: attr, source_attr: types[t]};
72         }
73
74         return null;
75     },
76
77     update: function(oncomplete) {
78         fieldmapper.standardRequest(
79             ['open-ils.acq', 'open-ils.acq.lineitem.update'],
80             {   async: true,
81                 params: [openils.User.authtoken, this.lineitem],
82                 oncomplete: function(r) {
83                     oncomplete(openils.Event.parse(r.recv().content()));
84                 }
85             }
86         );
87     },
88
89     approve: function(oncomplete) {
90         fieldmapper.standardRequest(
91             ['open-ils.acq', 'open-ils.acq.lineitem.approve'],
92             {  async: true,
93                params: [openils.User.authtoken, this.lineitem.id()],
94                oncomplete: function(r) {
95                    oncomplete(openils.Event.parse(r.recv().content()));
96                }
97             });
98     },
99
100     id: function() {
101         return this.lineitem.id();
102     },
103 });
104
105 openils.acq.Lineitem.ModelCache = {};
106 openils.acq.Lineitem.acqlidCache = {};
107
108 openils.acq.Lineitem.createLIDStore = function(li_id, onComplete) {
109     // Fetches the details of a lineitem and builds a grid
110
111     function mkStore(r) {
112         var msg;
113         var items = [];
114         while (msg = r.recv()) {
115             var data = msg.content();
116             for (i in data.lineitem_details()) {
117                 var lid = data.lineitem_details()[i];
118                 items.push(lid);
119                 openils.acq.Lineitem.acqlidCache[lid.id()] = lid;
120             }
121         }
122
123         onComplete(acqlid.toStoreData(items));
124     }
125
126     fieldmapper.standardRequest(
127         ['open-ils.acq', 'open-ils.acq.lineitem.retrieve'],
128         { async: true,
129           params: [openils.User.authtoken, li_id,
130                    {flesh_attrs:1, flesh_li_details:1}],
131           oncomplete: mkStore
132         });
133 };
134
135 openils.acq.Lineitem.alertOnLIDSet = function(griditem, attr, oldVal, newVal) {
136     var item;
137     var updateDone = function(r) {
138         var stat = r.recv().content();
139         var evt = openils.Event.parse(stat);
140
141         if (evt) {
142             alert("Error: "+evt.desc);
143             console.dir(evt);
144             if (attr == "fund") {
145                 item.fund(oldVal);
146                 griditem.fund = oldVal;
147             } else if (attr ==  "owning_lib") {
148                 item.owning_lib(oldVal);
149                 griditem.owning_lib = oldVal;
150             }
151         }
152     };
153
154     if (oldVal == newVal) {
155         return;
156     }
157
158     item = openils.acq.Lineitem.acqlidCache[griditem.id];
159     
160     if (attr == "fund") {
161         item.fund(newVal);
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);
170     } else {
171         alert("Unexpected attr in Lineitem.alertOnSet: '"+attr+"'");
172         return;
173     }
174
175     fieldmapper.standardRequest(
176         ["open-ils.acq", "open-ils.acq.lineitem_detail.update"],
177         { params: [openils.User.authtoken, item],
178           oncomplete: updateDone
179         });
180 };
181
182 openils.acq.Lineitem.deleteLID = function(id, onComplete) {
183     fieldmapper.standardRequest(
184         ['open-ils.acq', 'open-ils.acq.lineitem_detail.delete'],
185         {   async: true,
186             params: [openils.User.authtoken, id],
187             oncomplete: function(r) {
188                 msg = r.recv()
189                 stat = msg.content();
190                 onComplete(openils.Event.parse(stat));
191             }
192     });
193 };
194
195 openils.acq.Lineitem.createLID = function(fields, onCreateComplete) {
196     var lid = new acqlid()
197     for (var field in fields) {
198         lid[field](fields[field]);
199     }
200
201     fieldmapper.standardRequest(
202         ['open-ils.acq', 'open-ils.acq.lineitem_detail.create'],
203         { async: true,
204           params: [openils.User.authtoken, lid, {return_obj:1}],
205           oncomplete: function(r) {
206               var msg = r.recv();
207           var obj = msg.content();
208           openils.Event.parse_and_raise(obj);
209               if (onCreateComplete) {
210                     onCreateComplete(obj);
211               }
212           }
213         });
214 };
215
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:'*'}});
223
224                     dojo.connect(store, "onSet",
225                                  openils.acq.Lineitem.alertOnLIDSet);
226                     openils.acq.Lineitem.ModelCache[id] = model;
227
228                     domNode.setStructure(layout);
229                     domNode.setModel(model);
230                     domNode.update();
231                 });
232     } else {
233         domNode.setModel(openils.acq.Lineitem.ModelCache[id]);
234         domNode.setStructure(layout);
235         domNode.update();
236         domNode.refresh();
237     }
238 };
239 }