]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/acq/po/view_po.js
LP#1510641 Always show actions selector in PO view
[working/Evergreen.git] / Open-ILS / web / js / ui / default / acq / po / view_po.js
1 dojo.require("dijit.form.Button");
2 dojo.require("dojo.string");
3 dojo.require('dijit.layout.ContentPane');
4 dojo.require('openils.PermaCrud');
5
6 var pcrud = new openils.PermaCrud();
7 var PO = null;
8 var liTable;
9 var poItemTable;
10 var poNoteTable;
11 var invoiceLinkDialogManager;
12
13 function AcqPoNoteTable() {
14     var self = this;
15
16     this.notesTbody = dojo.byId("acq-po-notes-tbody");
17     this.notesRow = this.notesTbody.removeChild(dojo.byId("acq-po-notes-row"));
18
19     dojo.byId("acq-po-notes-back-button").onclick = function() { self.hide(); };
20     dojo.byId("acq-po-view-notes").onclick = function() { self.show(); };
21
22     /* widgets' event properties are cased likeThis */
23     acqPoCreateNoteSubmit.onClick = function() {
24         if (!acqPoCreateNoteText.attr("value")) return;
25
26         /* prep new note */
27         var note = new acqpon();
28         note.vendor_public(
29             Boolean(acqPoCreateNoteVendorPublic.attr('checked'))
30         );
31         note.value(acqPoCreateNoteText.attr("value"));
32         note.purchase_order(PO.id());
33         note.isnew(true);
34
35         /* save it */
36         self.updatePoNotes(note);
37
38         /* reset fields for next use */
39         acqPoCreateNoteText.attr("value", "");
40         acqPoCreateNoteVendorPublic.attr("checked", false);
41     };
42
43     this.drawPoNote = function(note) {
44         if (note.isdeleted())
45             return;
46
47         var row = dojo.clone(this.notesRow);
48
49         nodeByName("value", row).innerHTML = note.value();
50
51         if (openils.Util.isTrue(note.vendor_public()))
52             nodeByName("vendor_public", row).innerHTML =
53                 localeStrings.VENDOR_PUBLIC;
54
55         nodeByName("delete", row).onclick = function() {
56             note.isdeleted(true);
57             self.notesTbody.removeChild(row);
58             self.updatePoNotes();
59         };
60
61         if (note.edit_time()) {
62             nodeByName("edit_time", row).innerHTML =
63                 dojo.date.locale.format(
64                     dojo.date.stamp.fromISOString(note.edit_time()),
65                     {"formatLength": "short"}
66                 );
67         }
68
69         self.notesTbody.appendChild(row);
70     };
71
72     this.drawPoNotes = function() {
73         /* sort */
74         PO.notes(
75             PO.notes().sort(
76                 function(a, b) {
77                     return (a.edit_time() < b.edit_time()) ? 1 : -1;
78                 }
79             )
80         );
81
82         /* remove old renderings of notes */
83         dojo.empty(this.notesTbody);
84
85         PO.notes().forEach(function(o) { self.drawPoNote(o); });
86     };
87
88     this.updatePoNotesCount = function() {
89         dojo.byId("acq-po-view-notes").innerHTML =
90             "(" + PO.notes().length + ")";
91     };
92
93     this.updatePoNotes = function(newNote) {
94         var notes = newNote ?
95             [newNote] :
96             PO.notes().filter(
97                 function(o) {
98                     if (o.ischanged() || o.isnew() || o.isdeleted())
99                         return o;
100                 }
101             );
102
103         if (notes.length < 1)
104             return;
105
106         progressDialog.show();
107
108         fieldmapper.standardRequest(
109             ["open-ils.acq", "open-ils.acq.po_note.cud.batch"], {
110                 "async": true,
111                 "params": [openils.User.authtoken, notes],
112                 "onresponse": function(r) {
113                     var resp = openils.Util.readResponse(r);
114                     if (resp) {
115                         progressDialog.update(resp);
116
117                         if (!resp.note.isdeleted()) {
118                             resp.note.isnew(false);
119                             resp.note.ischanged(false);
120                             PO.notes().push(resp.note);
121                         }
122                     }
123                 },
124                 "oncomplete": function() {
125                     if (!newNote) {
126                         /* remove the old changed notes */
127                         var list = [];
128                         PO.notes(
129                             PO.notes().filter(
130                                 function(o) {
131                                     return (!(
132                                         o.ischanged() || o.isnew() ||
133                                         o.isdeleted()
134                                     ));
135                                 }
136                             )
137                         );
138                     }
139
140                     progressDialog.hide();
141                     self.updatePoNotesCount();
142                     self.drawPoNotes();
143                 }
144             }
145         );
146     };
147
148     this.hide = function() {
149         openils.Util.hide("acq-po-notes-div");
150         liTable.show("list");
151         poItemTable.show();
152     };
153
154     this.show = function() {
155         liTable.hide();
156         poItemTable.hide();
157         self.drawPoNotes();
158         openils.Util.show("acq-po-notes-div");
159     };
160 }
161
162 function updatePoState(po_info) {
163     var data = po_info[PO.id()];
164     if (data) {
165         for (var key in data)
166             PO[key](data[key]);
167         renderPo();
168     }
169 }
170
171 function cancellationUpdater(r) {
172     var r = openils.Util.readResponse(r);
173     if (r) {
174         if (r.po) updatePoState(r.po);
175         if (r.li) {
176             for (var id in r.li) {
177                 liTable.liCache[id].state(r.li[id].state);
178                 liTable.liCache[id].cancel_reason(r.li[id].cancel_reason);
179                 liTable.updateLiState(liTable.liCache[id]);
180             }
181         }
182         if (r.lid && liTable.copyCache) {
183             for (var id in r.lid) {
184                 if (liTable.copyCache[id]) {
185                     liTable.copyCache[id].cancel_reason(
186                         r.lid[id].cancel_reason
187                     );
188                     liTable.updateLidState(liTable.copyCache[id]);
189                 }
190             }
191         }
192     }
193 }
194
195 function makeProviderLink(node, provider) {
196     return dojo.create(
197         "a", {
198             "href": oilsBasePath + "/conify/global/acq/provider/" + provider.id(),
199             "innerHTML": provider.name() + " (" + provider.code() + ")",
200         },
201         node,
202         "only"
203     );
204 }
205 function makePrepayWidget(node, prepay) {
206     if (prepay) {
207         openils.Util.addCSSClass(node, "oils-acq-po-prepay");
208         node.innerHTML = localeStrings.YES;
209     } else {
210         openils.Util.removeCSSClass(node, "oils-acq-po-prepay");
211         node.innerHTML = localeStrings.NO;
212     }
213 }
214
215 function makeCancelWidget(node, labelnode) {
216     openils.Util.hide("acq-po-choose-cancel-reason");
217
218     if (PO.cancel_reason()) {
219         labelnode.innerHTML = localeStrings.CANCEL_REASON;
220         node.innerHTML = PO.cancel_reason().description() + " (" +
221             PO.cancel_reason().label() + ")";
222     } else if (["on-order", "pending"].indexOf(PO.state()) == -1) {
223         dojo.destroy(this.oldTip);
224         labelnode.innerHTML = "";
225         node.innerHTML = "";
226     } else {
227         dojo.destroy(this.oldTip);
228         labelnode.innerHTML = localeStrings.CANCEL;
229         node.innerHTML = "";
230         if (!acqPoCancelReasonSubmit._prepared) {
231             var widget = new openils.widget.AutoFieldWidget({
232                 "fmField": "cancel_reason",
233                 "fmClass": "acqpo",
234                 "parentNode": dojo.byId("acq-po-cancel-reason"),
235                 "orgLimitPerms": ["CREATE_PURCHASE_ORDER"],
236                 "forceSync": true,
237                 "searchOptions" : {order_by : {"acqcr":"label"}}
238             });
239             widget.build(
240                 function(w, ww) {
241                     acqPoCancelReasonSubmit.onClick = function() {
242                         if (w.attr("value")) {
243                             if (confirm(localeStrings.PO_CANCEL_CONFIRM)) {
244                                 fieldmapper.standardRequest(
245                                     ["open-ils.acq",
246                                         "open-ils.acq.purchase_order.cancel"],
247                                     {
248                                         "params": [
249                                             openils.User.authtoken,
250                                             PO.id(), 
251                                             w.attr("value")
252                                         ],
253                                         "async": true,
254                                         "oncomplete": cancellationUpdater
255                                     }
256                                 );
257                             }
258                         }
259                     };
260                     acqPoCancelReasonSubmit._prepared = true;
261                 }
262             );
263         }
264         openils.Util.show("acq-po-choose-cancel-reason", "inline");
265     }
266 }
267
268 function prepareInvoiceFeatures() {
269     /* show the count of related invoices on the "view invoices" button */
270     fieldmapper.standardRequest(
271         ["open-ils.acq", "open-ils.acq.invoice.unified_search.atomic"], {
272             "params": [
273                 openils.User.authtoken,
274                 {"acqpo":[{"id": PO.id()}]},
275                 null,
276                 null,
277                 {"id_list": true}
278             ],
279             "async": true,
280             "oncomplete": function(r) {
281                 dojo.byId("acq-po-view-invoice-count").innerHTML =
282                     openils.Util.readResponse(r).length;
283             }
284         }
285     );
286
287     /* view invoices button */
288     dijit.byId("acq-po-view-invoice-link").onClick = function() {
289         location.href = oilsBasePath + "/acq/search/unified?so=" +
290             base64Encode({"acqpo":[{"id": PO.id()}]}) +
291             "&rt=invoice";
292     };
293
294     /* create invoice button */
295     dijit.byId("acq-po-create-invoice-link").onClick = function() {
296         location.href = oilsBasePath +
297             "/acq/invoice/view?create=1&attach_po=" + PO.id();
298     };
299
300     openils.Util.show("acq-po-invoice-stuff", "table-cell");
301 }
302
303 function setSummaryAmounts() {
304     dojo.byId("acq-po-view-total-enc").innerHTML = PO.amount_encumbered().toFixed(2);
305     dojo.byId("acq-po-view-total-spent").innerHTML = PO.amount_spent().toFixed(2);
306     dojo.byId("acq-po-view-total-estimated").innerHTML = PO.amount_estimated().toFixed(2);
307 }
308
309 function refreshPOSummaryAmounts() {
310     fieldmapper.standardRequest(
311         ['open-ils.acq', 
312             'open-ils.acq.purchase_order.retrieve.authoritative'],
313         {   async: true,
314             params: [openils.User.authtoken, poId, {
315                 "flesh_price_summary": true
316             }],
317             oncomplete: function(r) {
318                 // update the global PO instead of replacing it, since other 
319                 // code outside our control may be referencing it.
320                 var po = openils.Util.readResponse(r);
321                 PO.amount_encumbered(po.amount_encumbered());
322                 PO.amount_spent(po.amount_spent());
323                 PO.amount_estimated(po.amount_estimated());
324                 setSummaryAmounts();
325             }
326         }
327     );
328 }
329
330 /* renderPo() is the best place to add tests that depend on PO-state
331  * (or simple ordered-or-not? checks) to enable/disable UI elements
332  * across the whole interface. */
333 function renderPo() {
334     var po_state = PO.state();
335     dojo.byId("acq-po-view-id").innerHTML = PO.id();
336     dojo.byId("acq-po-view-name").innerHTML = PO.name();
337     makeProviderLink(
338         dojo.byId("acq-po-view-provider"),
339         PO.provider()
340     );
341
342     setSummaryAmounts();
343     dojo.byId("acq-po-view-total-li").innerHTML = PO.lineitem_count();
344     dojo.byId("acq-po-view-state").innerHTML = po_state; // TODO i18n
345
346     if(PO.order_date()) {
347         openils.Util.show('acq-po-activated-on', 'inline');
348         liTable.enableActionsDropdownOptions("ao"); /* activated */
349         liTable.initBatchUpdater(["item_count", "distribution_formula"]);
350
351         dojo.byId('acq-po-activated-on').innerHTML = 
352             dojo.string.substitute(
353                 localeStrings.PO_ACTIVATED_ON, [
354                     openils.Util.timeStamp(PO.order_date(), {formatLength:'short'})
355                 ]
356             );
357         /* These are handled another way now */
358 //        if (po_state == "on-order" || po_state == "cancelled") {
359 //            dojo.removeAttr('receive_lineitems', 'disabled');
360 //        } else if(po_state == "received") {
361 //            dojo.removeAttr('rollback_receive_lineitems', 'disabled');
362 //        }
363
364         /* cancel widgets only make sense for activate (ordered) POs */
365         makeCancelWidget(
366             dojo.byId("acq-po-view-cancel-reason"),
367             dojo.byId("acq-po-cancel-label")
368         );
369
370         /* likewise for invoice features */
371         openils.Util.show("acq-po-invoice-label", "table-cell");
372         prepareInvoiceFeatures();
373     } else {
374         /* These things only make sense for not-ordered-yet POs */
375
376         liTable.initBatchUpdater();
377         liTable.enableActionsDropdownOptions("po");
378
379         openils.Util.show("acq-po-zero-activate-label", "table-cell");
380         openils.Util.show("acq-po-zero-activate", "table-cell");
381
382         openils.Util.show("acq-po-item-table-controls");
383     }
384
385     makePrepayWidget(
386         dojo.byId("acq-po-view-prepay"),
387         openils.Util.isTrue(PO.prepayment_required())
388     );
389     // dojo.byId("acq-po-view-notes").innerHTML = PO.notes().length;
390     poNoteTable.updatePoNotesCount();
391
392     if (po_state == "pending") {
393         checkCouldActivatePo();
394         if (PO.lineitem_count() > 1)
395             openils.Util.show("acq-po-split");
396     } else {
397         if (PO.order_date()) {
398             dojo.byId("acq-po-activate-checking").innerHTML = localeStrings.PO_ALREADY_ACTIVATED;
399             checkCouldBlanketFinalize();
400         } else {
401             dojo.byId("acq-po-activate-checking").innerHTML = localeStrings.NO;
402         }
403     }
404
405     // XXX we probably don't *always* need to do this...
406     poItemTable.reset();
407     PO.po_items().forEach(
408         function(po_item) { poItemTable.addItem(po_item); }
409     );
410     poItemTable.show();
411
412     dojo.attr(
413         "acq-po-view-history", "href",
414         oilsBasePath + "/acq/po/history/" + PO.id()
415     );
416     openils.Util.show("acq-po-view-history", "inline");
417
418     
419     /* if we got here from the search/invoice page with a focused LI,
420      * return to the previous page with the same LI focused */
421     var cgi = new openils.CGI();
422     var source = cgi.param('source');
423     var focus_li = cgi.param('focus_li');
424     if (focus_li && source) {
425         dojo.forEach(
426             ['search', 'invoice'], // perhaps a wee bit too loose
427             function(srcType) {
428                 if (source.match(new RegExp(srcType))) {
429                     openils.Util.show('acq-po-return-to-' + srcType);
430                     var newCgi = new openils.CGI({url : source});
431                     newCgi.param('focus_li', focus_li);
432                     dojo.byId('acq-po-return-to-' + srcType + '-button').onclick = function() {
433                         location.href = newCgi.url();
434                     }
435                 }
436             }
437         );
438     }
439 }
440
441
442 function init() {
443     /* set up li table */
444     liTable = new AcqLiTable();
445     liTable.reset();
446     liTable.isPO = poId;
447     liTable.poUpdateCallback = updatePoState;
448
449     /* set up po notes table */
450     poNoteTable = new AcqPoNoteTable();
451
452     /* retrieve data and populate */
453     fieldmapper.standardRequest(
454         ['open-ils.acq', 'open-ils.acq.purchase_order.retrieve'],
455         {   async: true,
456             params: [openils.User.authtoken, poId, {
457                 "flesh_provider": true,
458                 "flesh_price_summary": true,
459                 "flesh_lineitem_count": true,
460                 "flesh_notes": true,
461                 "flesh_po_items": true
462             }],
463             oncomplete: function(r) {
464                 PO = openils.Util.readResponse(r); /* save PO globally */
465
466                 /* po item table */
467                 poItemTable = new PoItemTable(PO, pcrud);
468
469                 liTable.testOrderIdentPerms( PO.ordering_agency(), init2);
470
471                 renderPo();
472             }
473         }
474     );
475
476 }
477
478 function init2() {
479
480     fieldmapper.standardRequest(
481         ['open-ils.acq', 'open-ils.acq.lineitem.search'],
482         {   async: true,
483             params: [
484                 openils.User.authtoken, 
485                 [{purchase_order:poId}, {"order_by": {"jub": "id ASC"}}], 
486                 {flesh_attrs:true, flesh_notes:true, flesh_cancel_reason:true, clear_marc:true}
487             ],
488             onresponse: function(r) {
489                 liTable.show('list');
490                 var li = openils.Util.readResponse(r);
491                 liTable.addLineitem(li);
492             },
493
494             oncomplete : function() {
495                 if (liFocus) liTable.drawCopies(liFocus);
496             }
497         }
498     );
499
500     pcrud.search(
501         'acqedim', 
502         {purchase_order : poId}, 
503         {
504             id_list : true,
505             oncomplete : function(r) {
506                 var resp = openils.Util.readResponse(r);
507                 // TODO: I18n
508                 if(resp) {
509                     dojo.byId('acq-po-view-edi-messages').innerHTML = '(' + resp.length + ')';
510                     dojo.byId('acq-po-view-edi-messages').setAttribute('href', oilsBasePath + '/acq/po/edi_messages/' + poId);
511                 } else {
512                     dojo.byId('acq-po-view-edi-messages').innerHTML = '0';
513                     dojo.byId('acq-po-view-edi-messages').setAttribute('href', '');
514                 }
515             }
516         }
517     );
518 }
519
520 function checkCouldBlanketFinalize() {
521
522     if (PO.state() == 'received') return;
523
524     var inv_types = [];
525
526     // get the unique set of invoice item type IDs
527     PO.po_items().forEach(function(item) { 
528         if (inv_types.indexOf(item.inv_item_type()) == -1)
529             inv_types.push(item.inv_item_type());
530     });
531
532     if (inv_types.length == 0) return;
533
534     pcrud.search('aiit', 
535         {code : inv_types, blanket : 't'}, {
536         oncomplete : function(r) {
537             r = openils.Util.readResponse(r);
538             if (r.length == 0) return;
539             openils.Util.show(dojo.byId('acq-po-finalize-links'), 'inline');
540         }
541     });
542 }
543
544 function checkCouldActivatePo() {
545     var d = dojo.byId("acq-po-activate-checking");
546     var a = dojo.byId("acq-po-activate-links");  /* <span> not <a> now, but no diff */
547     d.innerHTML = localeStrings.PO_CHECKING;
548     var warnings = [];
549     var stops = [];
550     var other = [];
551
552     fieldmapper.standardRequest(
553         ["open-ils.acq", "open-ils.acq.purchase_order.activate.dry_run"], {
554             "params": [
555                 openils.User.authtoken,
556                 PO.id(),
557                 null,  // vandelay options
558                 {zero_copy_activate : dojo.byId('acq-po-activate-zero-copies').checked}
559             ],
560             "async": true,
561             "onresponse": function(r) {
562                 if ((r = openils.Util.readResponse(r, true /* eventOk */))) {
563                     if (typeof(r.textcode) != "undefined") {
564                         switch(r.textcode) {
565                             case "ACQ_FUND_EXCEEDS_STOP_PERCENT":
566                                 stops.push(r);
567                                 break;
568                             case "ACQ_FUND_EXCEEDS_WARN_PERCENT":
569                                 warnings.push(r);
570                                 break;
571                             default:
572                                 other.push(r);
573                         }
574                     }
575                 }
576             },
577             "oncomplete": function() {
578                 /* XXX in the future, this might be tweaked to display info
579                  * about more than one stop or warning event from the ML. */
580                 if (!(warnings.length || stops.length || other.length)) {
581                     d.innerHTML = localeStrings.PO_COULD_ACTIVATE;
582                     openils.Util.show(a, "inline");
583                     activatePoButton.attr("disabled", false);
584                 } else {
585                     if (other.length) {
586                         /* XXX make the textcode part a tooltip one day */
587                         d.innerHTML = localeStrings.NO + ": " +
588                             other[0].desc + " (" + other[0].textcode + ")";
589                         openils.Util.hide(a);
590                         
591                         if (other[0].textcode == 'ACQ_LINEITEM_NO_COPIES') {
592                             // when LIs w/ zero LIDs are present, list them
593                             fieldmapper.standardRequest(
594                                 [   'open-ils.acq', 
595                                     'open-ils.acq.purchase_order.no_copy_lineitems.id_list.authoritative.atomic' ],
596                                 {   async : true, 
597                                     params : [openils.User.authtoken, poId],
598                                     oncomplete : function(r) {
599                                         var ids = openils.Util.readResponse(r);
600                                         d.innerHTML += ' (' + ids + ')';
601                                     }
602                                 }
603                             );
604                         }
605                     } else if (stops.length) {
606                         d.innerHTML =
607                             dojo.string.substitute(
608                                 localeStrings.PO_STOP_BLOCKS_ACTIVATION, [
609                                     stops[0].payload.fund.code(),
610                                     stops[0].payload.fund.year()
611                                 ]
612                             );
613                         openils.Util.hide(a);
614                     } else {
615                         PO._warning_hack = true;
616                         d.innerHTML =
617                             dojo.string.substitute(
618                                 localeStrings.PO_WARNING_NO_BLOCK_ACTIVATION, [
619                                     warnings[0].payload.fund.code(),
620                                     warnings[0].payload.fund.year()
621                                 ]
622                             );
623                         openils.Util.show(a, "inline");
624                         activatePoButton.attr("disabled", false);
625                     }
626                 }
627             }
628         }
629     );
630 }
631
632 function finalizePo() {
633
634     if (!confirm(localeStrings.FINALIZE_PO)) return;
635
636     finalizePoButton.attr('disabled', true);
637
638     fieldmapper.standardRequest(
639         ['open-ils.acq', 'open-ils.acq.purchase_order.blanket.finalize'],
640         {   async : true,
641             params : [openils.User.authtoken, PO.id()],
642             oncomplete : function(r) {
643                 if (openils.Util.readResponse(r) == 1) 
644                     location.href = location.href;
645             }
646         }
647     );
648 }
649
650 function activatePo(noAssets) {
651     activatePoButton.attr("disabled", true);
652     activatePoNoAssetsButton.attr("disabled", true);
653
654     if (openils.Util.isTrue(PO.prepayment_required())) {
655         if (!confirm(localeStrings.PREPAYMENT_REQUIRED_REMINDER)) {
656             activatePoButton.attr("disabled", false);
657             return false;
658         }
659     }
660
661     if (PO._warning_hack) {
662         if (!confirm(localeStrings.PO_FUND_WARNING_CONFIRM)) {
663             activatePoButton.attr("disabled", false);
664             return false;
665         }
666     }
667
668     if (noAssets) {
669         // no need for AssetCreator when assets are not desired
670         activatePoStage2(true);
671     } else {
672         liTable.showAssetCreator(activatePoStage2);
673     }
674 }
675
676 function activatePoStage2(noAssets) {
677
678     var want_refresh = false;
679     progressDialog.show(true);
680     progressDialog.attr("title", localeStrings.PO_ACTIVATING);
681     fieldmapper.standardRequest(
682         ["open-ils.acq", "open-ils.acq.purchase_order.activate"], {
683             "async": true,
684             "params": [
685                 openils.User.authtoken,
686                 PO.id(),
687                 null,  // vandelay options
688                 {
689                     no_assets : noAssets, // no bibs/volumes/copies required
690                     zero_copy_activate : dojo.byId('acq-po-activate-zero-copies').checked
691                 }
692             ],
693             "onresponse": function(r) {
694                 activatePoButton.attr("disabled", false);
695                 want_refresh = Boolean(openils.Util.readResponse(r));
696             },
697             "oncomplete": function() {
698                 progressDialog.hide();
699                 progressDialog.attr("title", "");
700                 if (want_refresh)
701                     location.href = location.href;
702             }
703         }
704     );
705 }
706
707 function splitPo() {
708     progressDialog.show(true);
709     try {
710         var list;
711         fieldmapper.standardRequest(
712             ['open-ils.acq', 'open-ils.acq.purchase_order.split_by_lineitems'],
713             {   async: true,
714                 params: [openils.User.authtoken, PO.id()],
715                 onresponse : function(r) {
716                     list = openils.Util.readResponse(r);
717                 },
718                 oncomplete : function() {
719                     progressDialog.hide();
720                     if (list) {
721                         location.href = oilsBasePath + '/acq/po/search/' +
722                             list.join(",");
723                     }
724                 }
725             }
726         );
727     } catch(E) {
728         progressDialog.hide();
729         alert(E);
730     }
731 }
732
733 function updatePoName() {
734     var value = prompt('Enter new purchase order name:', PO.name()); // TODO i18n
735     if(!value || value == PO.name()) return;
736
737     var orgs = fieldmapper.aou.descendantNodeList(PO.ordering_agency(), true);
738     new openils.PermaCrud().search('acqpo', 
739         {
740             id : {'<>' : PO.id()},
741             name : value, 
742             ordering_agency : orgs
743         },
744         {async : true, oncomplete : function(r) {
745             var po = openils.Util.readResponse(r);
746             openils.Util.hide('acq-dupe-po-name');
747
748             if (po && (po = po[0])) {
749                 var link = dojo.byId('acq-dupe-po-link');
750                 openils.Util.show('acq-dupe-po-name');
751                 var dupe_path = '/acq/po/view/' + po.id();
752
753                 if (window.xulG) {
754
755                     if (window.IAMBROWSER) {
756                         // TODO: integration
757
758                     } else {
759                         // XUL client
760                         link.onclick = function() {
761
762                             var loc = xulG.url_prefix('XUL_BROWSER?url=') + 
763                                 window.encodeURIComponent( 
764                                 xulG.url_prefix('EG_WEB_BASE' + dupe_path)
765                             );
766
767                             xulG.new_tab(loc, 
768                                 {tab_name: '', browser:false},
769                                 {
770                                     no_xulG : false, 
771                                     show_nav_buttons : true, 
772                                     show_print_button : true, 
773                                 }
774                             );
775                         }
776                     }
777
778                 } else {
779                     link.onclick = function() {
780                         window.open(oilsBasePath + dupe_path, '_blank').focus();
781                     }
782                 }
783
784             } else {
785                 updatePoName2(value);
786             }
787        }} 
788     );
789  
790 }
791
792 function updatePoName2(value) {
793     PO.name(value);
794     pcrud.update(PO, {
795         oncomplete : function(r, cudResults) {
796             var stat = cudResults[0];
797             if(stat)
798                 dojo.byId('acq-po-view-name').innerHTML = value;
799         }
800     });
801 }
802
803 openils.Util.addOnLoad(init);