LP#1350371 PO name on create w/ dupe detect
[working/Evergreen.git] / Open-ILS / web / js / ui / default / acq / po / create.js
1 dojo.require("openils.widget.EditDialog");
2 dojo.require("openils.widget.EditPane");
3 dojo.require("openils.PermaCrud");
4 dojo.require("openils.User");
5 dojo.require("fieldmapper.OrgUtils");
6
7 dojo.requireLocalization('openils.acq', 'acq');
8 var localeStrings = dojo.i18n.getLocalization('openils.acq', 'acq');
9
10 var editDialog, selectedAgency, currentName;
11
12 function toPoListing() {
13     location.href = oilsBasePath + "/acq/search/unified?ca=po";
14 }
15
16 function toOnePo(id) {
17     location.href = oilsBasePath + "/acq/po/view/" + id;
18 }
19
20 openils.Util.addOnLoad(
21     function() {
22
23         // apply here in case the selector never changes
24         // (i.e. no onchange fires).
25         selectedAgency = openils.User.user.ws_ou();
26
27         // called each time the PO name changes
28         function name_changed(new_name) {
29             currentName = new_name;
30
31             console.debug('checking for PO name collision + "' 
32                 + currentName + '" at ' + selectedAgency);
33
34             if (!new_name) { // name cleared
35                 editDialog.editPane.saveButton.attr('disabled', false);
36                 return;
37             }
38
39             // disable Save option pending confirmation of uniqueness
40             editDialog.editPane.saveButton.attr('disabled', true);
41
42             var orgs = fieldmapper.aou.descendantNodeList(selectedAgency, true);
43             new openils.PermaCrud().search('acqpo', 
44                 {name : new_name, ordering_agency : orgs},
45                 {async : true, oncomplete : function(r) {
46                     var po = openils.Util.readResponse(r);
47                     var tbody = editDialog.editPane.table.getElementsByTagName('tbody')[0];
48
49                     // remove any previous dupe warning row
50                     dojo.forEach(tbody.getElementsByTagName('tr'), function(row) {
51                         if (row) { // sometimes row is undefined??
52                             if (row.getAttribute('dupe-po-row'))
53                                 tbody.removeChild(row);
54                         }
55                     });
56
57                     if (po && (po = po[0])) {
58                         // duplicate found.  add info row to create dialog
59
60                         var parent_row;
61                         dojo.forEach(tbody.getElementsByTagName('tr'), function(row) {
62                             if (row.getAttribute('fmfield') == 'name')
63                                 parent_row = row;
64                         });
65
66                         var new_tr = dojo.create('tr', {'dupe-po-row' : 1});
67                         var link = dojo.create('a', {
68                             href : 'javascript:;', 
69                             innerHTML : localeStrings.DUPE_PO_NAME_LINK
70                         });
71
72                         var dupe_path = '/acq/po/view/' + po.id();
73
74                         if (window.xulG) {
75
76                             if (window.IAMBROWSER) {
77                                 // TODO: integration
78
79                             } else {
80                                 // XUL client
81                                 link.onclick = function() {
82
83                                     var loc = xulG.url_prefix('XUL_BROWSER?url=') + 
84                                         window.encodeURIComponent( 
85                                         xulG.url_prefix('EG_WEB_BASE' + dupe_path)
86                                     );
87
88                                     xulG.new_tab(loc, 
89                                         {tab_name: '', browser:false},
90                                         {
91                                             no_xulG : false, 
92                                             show_nav_buttons : true, 
93                                             show_print_button : true, 
94                                         }
95                                     );
96                                 }
97                             }
98
99                         } else {
100                             link.onclick = function() {
101                                 window.open(oilsBasePath + dupe_path, '_blank').focus();
102                             }
103                         }
104
105                         new_tr.appendChild(dojo.create('td', 
106                             {innerHTML : localeStrings.DUPE_PO_NAME_MSG}));
107                         var link_td = dojo.create('td');
108                         link_td.appendChild(link);
109                         new_tr.appendChild(link_td);
110                         tbody.insertBefore(new_tr, parent_row.nextSibling);
111
112                     } else {
113                         editDialog.editPane.saveButton.attr('disabled', false);
114                     }
115                 }}
116             );
117         }
118
119         function agency_changed(val) {
120             selectedAgency = val;
121             if (currentName) {
122                 // if the ordering agency changes, re-run the dupe name check.
123                 name_changed(currentName);
124             }
125         }
126
127         editDialog = new openils.widget.EditDialog({
128             "editPane": new openils.widget.EditPane({
129                 "fmObject": new acqpo(),
130                 /* After realizing how many fields should be excluded from this
131                  * interface because users shouldn't set them arbitrarily,
132                  * it hardly seems like using these Edit widgets gives much
133                  * much advantage over a hardcoded interface. */
134                 "suppressFields": [
135                     "create_time", "edit_time", "editor", "order_date",
136                     "owner", "cancel_reason", "creator", "state"
137                 ],
138                 "fieldOrder": ["ordering_agency", "name", "provider"],
139                 "mode": "create",
140                 overrideWidgetArgs : {
141                     provider : { dijitArgs : { store_options : { base_filter : { active :"t" } } } },
142                     ordering_agency : { 
143                         orgDefaultsToWs : true,
144                         dijitArgs : {onChange : agency_changed}
145                     },
146                     name : {dijitArgs : {onChange : name_changed}}
147                 },
148                 "onSubmit": function(po) {
149                     fieldmapper.standardRequest(
150                         ["open-ils.acq", "open-ils.acq.purchase_order.create"],{
151                             "async": false,
152                             "params": [openils.User.authtoken, po],
153                             "onresponse": function(r) {
154                                 toOnePo(
155                                     openils.Util.readResponse(r).
156                                     purchase_order.id()
157                                 );
158                             }
159                         }
160                     );
161                 },
162                 "onCancel": function() {
163                     editDialog.hide();
164                     toPoListing();
165                     /* I'd rather do window.close() or xulG.close_tab(),
166                      * but neither of those seem to work here. */
167                 }
168             })
169         });
170         editDialog.startup();
171         editDialog.show();
172
173         // modify the label of the 'name' field to make it more clear it's optional
174         var row = dojo.query('[fmfield=name]', editDialog.editPane.table)[0];
175         var name_td = row.getElementsByTagName('td')[0];
176         name_td.innerHTML = dojo.string.substitute(
177             localeStrings.PO_NAME_OPTIONAL,
178             [name_td.innerHTML]
179         );
180     }
181 );