]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/circ/patron/regctl.js
LP#1816180 Pending Patron - No Address Type
[Evergreen.git] / Open-ILS / web / js / ui / default / staff / circ / patron / regctl.js
1
2 angular.module('egCoreMod')
3 // toss tihs onto egCoreMod since the page app may vary
4
5 .factory('patronRegSvc', ['$q', 'egCore', 'egLovefield', function($q, egCore, egLovefield) {
6
7     var service = {
8         field_doc : {},            // config.idl_field_doc
9         profiles : [],             // permission groups
10         edit_profiles : [],        // perm groups we can modify
11         sms_carriers : [],
12         user_settings : {},        // applied user settings
13         user_setting_types : {},   // config.usr_setting_type
14         opt_in_setting_types : {}, // config.usr_setting_type for event-def opt-in
15         surveys : [],
16         survey_questions : {},
17         survey_answers : {},
18         survey_responses : {},     // survey.responses for loaded patron in progress
19         stat_cats : [],
20         stat_cat_entry_maps : {},   // cat.id to selected value
21         virt_id : -1,               // virtual ID for new objects
22         init_done : false           // have we loaded our initialization data?
23     };
24
25     // Launch a series of parallel data retrieval calls.
26     service.init = function(scope) {
27
28         // These are fetched with every instance of the page.
29         var page_data = [
30             service.get_user_settings(),
31             service.get_clone_user(),
32             service.get_stage_user()
33         ];
34
35         var common_data = [];
36         if (!service.init_done) {
37             // These are fetched with every instance of the app.
38             common_data = [
39                 service.get_field_doc(),
40                 service.get_perm_groups(),
41                 service.get_ident_types(),
42                 service.get_org_settings(),
43                 service.get_stat_cats(),
44                 service.get_surveys(),
45                 service.get_net_access_levels()
46             ];
47             service.init_done = true;
48         }
49
50         return $q.all(common_data.concat(page_data));
51     };
52
53     service.get_clone_user = function() {
54         if (!service.clone_id) return $q.when();
55         // we could load egUser and use its get() function, but loading
56         // user.js into the standalone register UI would mean creating a
57         // new module, since egUser is not loaded into egCoreMod.  This
58         // is a lot simpler.
59         return egCore.net.request(
60             'open-ils.actor',
61             'open-ils.actor.user.fleshed.retrieve',
62             egCore.auth.token(), service.clone_id, 
63             ['billing_address', 'mailing_address'])
64         .then(function(cuser) {
65             if (e = egCore.evt.parse(cuser)) {
66                 alert(e);
67             } else {
68                 service.clone_user = cuser;
69             }
70         });
71     }
72
73     // When editing a user with addresses linked to other users, fetch
74     // the linked user(s) so we can display their names and edit links.
75     service.get_linked_addr_users = function(addrs) {
76         angular.forEach(addrs, function(addr) {
77             if (addr.usr == service.existing_patron.id()) return;
78             egCore.pcrud.retrieve('au', addr.usr)
79             .then(function(usr) {
80                 addr._linked_owner_id = usr.id();
81                 addr._linked_owner = service.format_name(
82                     usr.family_name(),
83                     usr.first_given_name(),
84                     usr.second_given_name()
85                 );
86             })
87         });
88     }
89
90     service.apply_secondary_groups = function(user_id, group_ids) {
91         return egCore.net.request(
92             'open-ils.actor',
93             'open-ils.actor.user.set_groups',
94             egCore.auth.token(), user_id, group_ids)
95         .then(function(resp) {
96             if (resp == 1) {
97                 return true;
98             } else {
99                 // debugging -- should be no events
100                 alert('linked groups failure ' + egCore.evt.parse(resp));
101             }
102         });
103     }
104
105     service.get_stage_user = function() {
106         if (!service.stage_username) return $q.when();
107
108         // fetch the staged user object
109         return egCore.net.request(
110             'open-ils.actor',
111             'open-ils.actor.user.stage.retrieve.by_username',
112             egCore.auth.token(), 
113             service.stage_username
114         ).then(function(suser) {
115             if (e = egCore.evt.parse(suser)) {
116                 alert(e);
117             } else {
118                 service.stage_user = suser;
119             }
120         }).then(function() {
121
122             if (!service.stage_user) return;
123             var requestor = service.stage_user.user.requesting_usr();
124
125             if (!requestor) return;
126
127             // fetch the requesting user
128             return egCore.net.request(
129                 'open-ils.actor', 
130                 'open-ils.actor.user.retrieve.parts',
131                 egCore.auth.token(),
132                 requestor, 
133                 ['family_name', 'first_given_name', 'second_given_name'] 
134             ).then(function(parts) {
135                 service.stage_user_requestor = 
136                     service.format_name(parts[0], parts[1], parts[2]);
137             })
138         });
139     }
140
141     // See note above about not loading egUser.
142     // TODO: i18n
143     service.format_name = function(last, first, middle) {
144         return last + ', ' + first + (middle ? ' ' + middle : '');
145     }
146
147     service.check_dupe_username = function(usrname) {
148
149         // empty usernames can't be dupes
150         if (!usrname) return $q.when(false);
151
152         // avoid dupe check if username matches the originally loaded usrname
153         if (service.existing_patron) {
154             if (usrname == service.existing_patron.usrname())
155                 return $q.when(false);
156         }
157
158         return egCore.net.request(
159             'open-ils.actor',
160             'open-ils.actor.username.exists',
161             egCore.auth.token(), usrname);
162     }
163
164     //service.check_grp_app_perm = function(grp_id) {
165
166     // determine which user groups our user is not allowed to modify
167     service.set_edit_profiles = function() {
168         var all_app_perms = [];
169         var failed_perms = [];
170
171         // extract the application permissions
172         angular.forEach(service.profiles, function(grp) {
173             if (grp.application_perm())
174                 all_app_perms.push(grp.application_perm());
175         }); 
176
177         // fill in service.edit_profiles by inspecting failed_perms
178         function traverse_grp_tree(grp, failed) {
179             failed = failed || 
180                 failed_perms.indexOf(grp.application_perm()) > -1;
181
182             if (!failed) service.edit_profiles.push(grp);
183
184             angular.forEach(
185                 service.profiles.filter( // children of grp
186                     function(p) { return p.parent() == grp.id() }),
187                 function(child) {traverse_grp_tree(child, failed)}
188             );
189         }
190
191         return egCore.perm.hasPermAt(all_app_perms, true).then(
192             function(perm_orgs) {
193                 angular.forEach(all_app_perms, function(p) {
194                     if (perm_orgs[p].length == 0)
195                         failed_perms.push(p);
196                 });
197
198                 traverse_grp_tree(egCore.env.pgt.tree);
199             }
200         );
201     }
202
203     // resolves to a hash of perm-name => boolean value indicating
204     // wether the user has the permission at org_id.
205     service.has_perms_for_org = function(org_id) {
206
207         var perms_needed = [
208             'UPDATE_USER',
209             'CREATE_USER',
210             'CREATE_USER_GROUP_LINK', 
211             'UPDATE_PATRON_COLLECTIONS_EXEMPT',
212             'UPDATE_PATRON_CLAIM_RETURN_COUNT',
213             'UPDATE_PATRON_CLAIM_NEVER_CHECKED_OUT_COUNT',
214             'UPDATE_PATRON_ACTIVE_CARD',
215             'UPDATE_PATRON_PRIMARY_CARD'
216         ];
217
218         return egCore.perm.hasPermAt(perms_needed, true)
219         .then(function(perm_map) {
220
221             angular.forEach(perms_needed, function(perm) {
222                 perm_map[perm] = 
223                     Boolean(perm_map[perm].indexOf(org_id) > -1);
224             });
225
226             return perm_map;
227         });
228     }
229
230     service.get_surveys = function() {
231         var org_ids = egCore.org.fullPath(egCore.auth.user().ws_ou(), true);
232
233         return egCore.pcrud.search('asv', {
234                 owner : org_ids,
235                 start_date : {'<=' : 'now'},
236                 end_date : {'>=' : 'now'}
237             }, {   
238                 flesh : 2, 
239                 flesh_fields : {
240                     asv : ['questions'], 
241                     asvq : ['answers']
242                 }
243             }, 
244             {atomic : true}
245         ).then(function(surveys) {
246             surveys = surveys.sort(function(a,b) {
247                 return a.name() < b.name() ? -1 : 1 });
248             service.surveys = surveys;
249             angular.forEach(surveys, function(survey) {
250                 angular.forEach(survey.questions(), function(question) {
251                     service.survey_questions[question.id()] = question;
252                     angular.forEach(question.answers(), function(answer) {
253                         service.survey_answers[answer.id()] = answer;
254                     });
255                 });
256             });
257
258             egLovefield.setListInOfflineCache('asv', service.surveys)
259             egLovefield.setListInOfflineCache('asvq', service.survey_questions)
260             egLovefield.setListInOfflineCache('asva', service.survey_answers)
261
262         });
263     }
264
265     service.get_stat_cats = function() {
266         return egCore.net.request(
267             'open-ils.circ',
268             'open-ils.circ.stat_cat.actor.retrieve.all',
269             egCore.auth.token(), egCore.auth.user().ws_ou()
270         ).then(function(cats) {
271             cats = cats.sort(function(a, b) {
272                 return a.name() < b.name() ? -1 : 1});
273             angular.forEach(cats, function(cat) {
274                 cat.entries(
275                     cat.entries().sort(function(a,b) {
276                         return a.value() < b.value() ? -1 : 1
277                     })
278                 );
279             });
280             service.stat_cats = cats;
281             return egLovefield.setStatCatsCache(cats);
282         });
283     };
284
285     service.get_org_settings = function() {
286         return egCore.org.settings([
287             'global.password_regex',
288             'global.juvenile_age_threshold',
289             'patron.password.use_phone',
290             'ui.patron.default_inet_access_level',
291             'ui.patron.default_ident_type',
292             'ui.patron.default_country',
293             'ui.patron.registration.require_address',
294             'circ.holds.behind_desk_pickup_supported',
295             'circ.patron_edit.clone.copy_address',
296             'ui.patron.edit.au.prefix.require',
297             'ui.patron.edit.au.prefix.show',
298             'ui.patron.edit.au.prefix.suggest',
299             'ui.patron.edit.ac.barcode.regex',
300             'ui.patron.edit.au.second_given_name.show',
301             'ui.patron.edit.au.second_given_name.suggest',
302             'ui.patron.edit.au.suffix.show',
303             'ui.patron.edit.au.suffix.suggest',
304             'ui.patron.edit.au.alias.show',
305             'ui.patron.edit.au.alias.suggest',
306             'ui.patron.edit.au.dob.require',
307             'ui.patron.edit.au.dob.show',
308             'ui.patron.edit.au.dob.suggest',
309             'ui.patron.edit.au.dob.calendar',
310             'ui.patron.edit.au.juvenile.show',
311             'ui.patron.edit.au.juvenile.suggest',
312             'ui.patron.edit.au.ident_value.show',
313             'ui.patron.edit.au.ident_value.suggest',
314             'ui.patron.edit.au.ident_value2.show',
315             'ui.patron.edit.au.ident_value2.suggest',
316             'ui.patron.edit.au.email.require',
317             'ui.patron.edit.au.email.show',
318             'ui.patron.edit.au.email.suggest',
319             'ui.patron.edit.au.email.regex',
320             'ui.patron.edit.au.email.example',
321             'ui.patron.edit.au.day_phone.require',
322             'ui.patron.edit.au.day_phone.show',
323             'ui.patron.edit.au.day_phone.suggest',
324             'ui.patron.edit.au.day_phone.regex',
325             'ui.patron.edit.au.day_phone.example',
326             'ui.patron.edit.au.evening_phone.require',
327             'ui.patron.edit.au.evening_phone.show',
328             'ui.patron.edit.au.evening_phone.suggest',
329             'ui.patron.edit.au.evening_phone.regex',
330             'ui.patron.edit.au.evening_phone.example',
331             'ui.patron.edit.au.other_phone.require',
332             'ui.patron.edit.au.other_phone.show',
333             'ui.patron.edit.au.other_phone.suggest',
334             'ui.patron.edit.au.other_phone.regex',
335             'ui.patron.edit.au.other_phone.example',
336             'ui.patron.edit.phone.regex',
337             'ui.patron.edit.phone.example',
338             'ui.patron.edit.au.active.show',
339             'ui.patron.edit.au.active.suggest',
340             'ui.patron.edit.au.barred.show',
341             'ui.patron.edit.au.barred.suggest',
342             'ui.patron.edit.au.master_account.show',
343             'ui.patron.edit.au.master_account.suggest',
344             'ui.patron.edit.au.claims_returned_count.show',
345             'ui.patron.edit.au.claims_returned_count.suggest',
346             'ui.patron.edit.au.claims_never_checked_out_count.show',
347             'ui.patron.edit.au.claims_never_checked_out_count.suggest',
348             'ui.patron.edit.au.alert_message.show',
349             'ui.patron.edit.au.alert_message.suggest',
350             'ui.patron.edit.aua.post_code.regex',
351             'ui.patron.edit.aua.post_code.example',
352             'ui.patron.edit.aua.county.require',
353             'format.date',
354             'ui.patron.edit.default_suggested',
355             'opac.barcode_regex',
356             'opac.username_regex',
357             'sms.enable',
358             'ui.patron.edit.aua.state.require',
359             'ui.patron.edit.aua.state.suggest',
360             'ui.patron.edit.aua.state.show',
361             'ui.admin.work_log.max_entries',
362             'ui.admin.patron_log.max_entries'
363         ]).then(function(settings) {
364             service.org_settings = settings;
365             if (egCore && egCore.env && !egCore.env.aous) {
366                 egCore.env.aous = settings;
367                 console.log('setting egCore.env.aous');
368             }
369             return service.process_org_settings(settings);
370         });
371     };
372
373     // some org settings require the retrieval of additional data
374     service.process_org_settings = function(settings) {
375
376         var promises = [egLovefield.setSettingsCache(settings)];
377
378         if (settings['sms.enable']) {
379             // fetch SMS carriers
380             promises.push(
381                 egCore.pcrud.search('csc', 
382                     {active: 'true'}, 
383                     {'order_by':[
384                         {'class':'csc', 'field':'name'},
385                         {'class':'csc', 'field':'region'}
386                     ]}, {atomic : true}
387                 ).then(function(carriers) {
388                     service.sms_carriers = carriers;
389                 })
390             );
391         } else {
392             // if other promises are added below, this is not necessary.
393             promises.push($q.when());  
394         }
395
396         // other post-org-settings processing goes here,
397         // adding to promises as needed.
398
399         return $q.all(promises);
400     };
401
402     service.get_ident_types = function() {
403         if (egCore.env.cit) {
404             service.ident_types = egCore.env.cit.list;
405             return $q.when();
406         } else {
407             return egCore.pcrud.retrieveAll('cit', {}, {atomic : true})
408             .then(function(types) { 
409                 egCore.env.absorbList(types, 'cit')
410                 service.ident_types = types 
411             });
412         }
413     };
414
415     service.get_net_access_levels = function() {
416         if (egCore.env.cnal) {
417             service.net_access_levels = egCore.env.cnal.list;
418             return $q.when();
419         } else {
420             return egCore.pcrud.retrieveAll('cnal', {}, {atomic : true})
421             .then(function(levels) { 
422                 egCore.env.absorbList(levels, 'cnal')
423                 service.net_access_levels = levels 
424             });
425         }
426     }
427
428     service.get_perm_groups = function() {
429         if (egCore.env.pgt) {
430             service.profiles = egCore.env.pgt.list;
431             return service.set_edit_profiles();
432         } else {
433             return egCore.pcrud.search('pgt', {parent : null}, 
434                 {flesh : -1, flesh_fields : {pgt : ['children']}}
435             ).then(
436                 function(tree) {
437                     egCore.env.absorbTree(tree, 'pgt')
438                     service.profiles = egCore.env.pgt.list;
439                     return service.set_edit_profiles();
440                 }
441             );
442         }
443     }
444
445     service.get_field_doc = function() {
446         var to_cache = [];
447         return egCore.pcrud.search('fdoc', {
448             fm_class: ['au', 'ac', 'aua', 'actsc', 'asv', 'asvq', 'asva']})
449         .then(
450             function () {
451                 return egLovefield.setListInOfflineCache('fdoc', to_cache)
452             },
453             null,
454             function(doc) {
455                 if (!service.field_doc[doc.fm_class()]) {
456                     service.field_doc[doc.fm_class()] = {};
457                 }
458                 service.field_doc[doc.fm_class()][doc.field()] = doc;
459                 to_cache.push(doc);
460             }
461         );
462
463     };
464
465     service.get_user_setting_types = function() {
466
467         // No need to re-fetch the common setting types.
468         if (Object.keys(service.user_setting_types).length) 
469             return $q.when();
470
471         var org_ids = egCore.org.ancestors(egCore.auth.user().ws_ou(), true);
472
473         var static_types = [
474             'circ.holds_behind_desk', 
475             'circ.collections.exempt', 
476             'opac.hold_notify', 
477             'opac.default_phone', 
478             'opac.default_pickup_location', 
479             'opac.default_sms_carrier', 
480             'opac.default_sms_notify'];
481
482         return egCore.pcrud.search('cust', {
483             '-or' : [
484                 {name : static_types}, // common user settings
485                 {name : { // opt-in notification user settings
486                     'in': {
487                         select : {atevdef : ['opt_in_setting']}, 
488                         from : 'atevdef',
489                         // we only care about opt-in settings for 
490                         // event_defs our users encounter
491                         where : {'+atevdef' : {owner : org_ids}}
492                     }
493                 }}
494             ]
495         }, {}, {atomic : true}).then(function(setting_types) {
496
497             egCore.env.absorbList(setting_types, 'cust'); // why not...
498
499             angular.forEach(setting_types, function(stype) {
500                 service.user_setting_types[stype.name()] = stype;
501                 if (static_types.indexOf(stype.name()) == -1) {
502                     service.opt_in_setting_types[stype.name()] = stype;
503                 }
504             });
505         });
506     };
507
508     service.get_user_settings = function() {
509
510         return service.get_user_setting_types()
511         .then(function() {
512
513             var setting_types = Object.values(service.user_setting_types);
514
515             if (service.patron_id) {
516                 // retrieve applied values for the current user 
517                 // for the setting types we care about.
518
519                 var setting_names = 
520                     setting_types.map(function(obj) { return obj.name() });
521
522                 return egCore.net.request(
523                     'open-ils.actor', 
524                     'open-ils.actor.patron.settings.retrieve.authoritative',
525                     egCore.auth.token(),
526                     service.patron_id,
527                     setting_names
528                 ).then(function(settings) {
529                     service.user_settings = settings;
530                 });
531
532             } else {
533
534                 // apply default user setting values
535                 angular.forEach(setting_types, function(stype, index) {
536                     if (stype.reg_default() != undefined) {
537                         var val = stype.reg_default();
538                         if (stype.datatype() == 'bool') {
539                             // A boolean user setting type whose default 
540                             // value starts with t/T is considered 'true',
541                             // false otherwise.
542                             val = Boolean((val+'').match(/^t/i));
543                         }
544                         service.user_settings[stype.name()] = val;
545                     }
546                 });
547             }
548         });
549     }
550
551     service.invalidate_field = function(patron, field) {
552         console.log('Invalidating patron field ' + field);
553
554         return egCore.net.request(
555             'open-ils.actor',
556             'open-ils.actor.invalidate.' + field,
557             egCore.auth.token(), patron.id, null, patron.home_ou.id()
558
559         ).then(function(res) {
560             // clear the invalid value from the form
561             patron[field] = '';
562
563             // update last_xact_id so future save operations
564             // on this patron will be allowed
565             patron.last_xact_id = res.payload.last_xact_id[patron.id];
566         });
567     }
568
569     service.dupe_patron_search = function(patron, type, value) {
570         var search;
571
572         console.log('Dupe search called with "'+ type +'" and value '+ value);
573
574         if (type.match(/phone/)) type = 'phone'; // day_phone, etc.
575
576         switch (type) {
577
578             case 'name':
579                 var fname = patron.first_given_name;   
580                 var lname = patron.family_name;   
581                 if (!(fname && lname)) return $q.when({count:0});
582                 search = {
583                     first_given_name : {value : fname, group : 0},
584                     family_name : {value : lname, group : 0}
585                 };
586                 break;
587
588             case 'email':
589                 search = {email : {value : value, group : 0}};
590                 break;
591
592             case 'ident':
593                 search = {ident : {value : value, group : 2}};
594                 break;
595
596             case 'phone':
597                 search = {phone : {value : value, group : 2}};
598                 break;
599
600             case 'address':
601                 search = {};
602                 angular.forEach(['street1', 'street2', 'city', 'post_code'],
603                     function(field) {
604                         if(value[field])
605                             search[field] = {value : value[field], group: 1};
606                     }
607                 );
608                 break;
609         }
610
611         return egCore.net.request( 
612             'open-ils.actor', 
613             'open-ils.actor.patron.search.advanced',
614             egCore.auth.token(), search, null, null, 1
615         ).then(function(res) {
616             res = res.filter(function(id) {return id != patron.id});
617             return {
618                 count : res.length,
619                 search : search
620             };
621         });
622     }
623
624     service.init_patron = function(current) {
625
626         if (!current)
627             return $q.when(service.init_new_patron());
628
629         service.patron = current;
630         return $q.when(service.init_existing_patron(current));
631     }
632
633     service.ingest_address = function(patron, addr) {
634         addr.valid = addr.valid == 't';
635         addr.within_city_limits = addr.within_city_limits == 't';
636         addr._is_mailing = (patron.mailing_address && 
637             addr.id == patron.mailing_address.id);
638         addr._is_billing = (patron.billing_address && 
639             addr.id == patron.billing_address.id);
640         addr.pending = addr.pending === 't';
641     }
642
643     /*
644      * Existing patron objects reqire some data munging before insertion
645      * into the scope.
646      *
647      * 1. Turn everything into a hash
648      * 2. ... Except certain fields (selectors) whose widgets require objects
649      * 3. Bools must be Boolean, not t/f.
650      */
651     service.init_existing_patron = function(current) {
652
653         service.existing_patron = current;
654
655         var patron = egCore.idl.toHash(current);
656
657         patron.home_ou = egCore.org.get(patron.home_ou.id);
658         patron.expire_date = new Date(Date.parse(patron.expire_date));
659         patron.dob = service.parse_dob(patron.dob);
660         patron.profile = current.profile(); // pre-hash version
661         patron.net_access_level = current.net_access_level();
662         patron.ident_type = current.ident_type();
663         patron.ident_type2 = current.ident_type2();
664         patron.groups = current.groups(); // pre-hash
665
666         angular.forEach(
667             ['juvenile', 'barred', 'active', 'master_account'],
668             function(field) { patron[field] = patron[field] == 't'; }
669         );
670
671         angular.forEach(patron.cards, function(card) {
672             card.active = card.active == 't';
673             if (card.id == patron.card.id) {
674                 patron.card = card;
675                 card._primary = true;
676             }
677         });
678
679         angular.forEach(patron.addresses, 
680             function(addr) { service.ingest_address(patron, addr) });
681
682         // Link replaced address to its pending address.
683         angular.forEach(patron.addresses, function(addr) {
684             if (addr.replaces) {
685                 addr._replaces = patron.addresses.filter(
686                     function(a) {return a.id == addr.replaces})[0];
687             }
688         });
689
690         service.get_linked_addr_users(patron.addresses);
691
692         // Remove stat cat entries that link to out-of-scope stat
693         // cats.  With this, we avoid unnecessarily updating (or worse,
694         // modifying) stat cat values that are not ours to modify.
695         patron.stat_cat_entries = patron.stat_cat_entries.filter(
696             function(map) {
697                 return Boolean(
698                     // service.stat_cats only contains in-scope stat cats.
699                     service.stat_cats.filter(function(cat) { 
700                         return (cat.id() == map.stat_cat.id) })[0]
701                 );
702             }
703         );
704
705         // toss entries for existing stat cat maps into our living 
706         // stat cat entry map, which is modified within the template.
707         angular.forEach(patron.stat_cat_entries, function(map) {
708             service.stat_cat_entry_maps[map.stat_cat.id] = map.stat_cat_entry;
709         });
710
711         service.patron = patron;
712         return patron;
713     }
714
715     service.init_new_patron = function() {
716         var addr = {
717             id : service.virt_id--,
718             isnew : true,
719             valid : true,
720             address_type : egCore.strings.REG_ADDR_TYPE,
721             _is_mailing : true,
722             _is_billing : true,
723             within_city_limits : false,
724             country : service.org_settings['ui.patron.default_country'],
725         };
726
727         var card = {
728             id : service.virt_id--,
729             isnew : true,
730             active : true,
731             _primary : true
732         };
733
734         var user = {
735             isnew : true,
736             active : true,
737             card : card,
738             cards : [card],
739             home_ou : egCore.org.get(egCore.auth.user().ws_ou()),
740             stat_cat_entries : [],
741             groups : [],
742             addresses : [addr]
743         };
744
745         if (service.clone_user)
746             service.copy_clone_data(user);
747
748         if (service.stage_user)
749             service.copy_stage_data(user);
750
751         return user;
752     }
753
754     // dob is always YYYY-MM-DD
755     // Dates of birth do not contain timezone info, which can lead to
756     // inconcistent timezone handling, potentially representing
757     // different points in time, depending on the implementation.
758     // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
759     // See "Differences in assumed time zone"
760     // TODO: move this into egDate ?
761     service.parse_dob = function(dob) {
762         if (!dob) return null;
763         var parts = dob.split('-');
764         return new Date(parts[0], parts[1] - 1, parts[2])
765     }
766
767     service.copy_stage_data = function(user) {
768         var cuser = service.stage_user;
769
770         // copy the data into our new user object
771
772         for (var key in egCore.idl.classes.stgu.field_map) {
773             if (egCore.idl.classes.au.field_map[key] &&
774                 !egCore.idl.classes.stgu.field_map[key].virtual) {
775                 if (cuser.user[key]() !== null)
776                     user[key] = cuser.user[key]();
777             }
778         }
779
780         if (user.home_ou) user.home_ou = egCore.org.get(user.home_ou);
781         if (user.profile) user.profile = egCore.env.pgt.map[user.profile];
782         if (user.ident_type) 
783             user.ident_type = egCore.env.cit.map[user.ident_type];
784         if (user.ident_type2)
785             user.ident_type2 = egCore.env.cit.map[user.ident_type2];
786         user.dob = service.parse_dob(user.dob);
787
788         // Clear the usrname if it looks like a UUID
789         if (user.usrname.replace(/-/g,'').match(/[0-9a-f]{32}/)) 
790             user.usrname = '';
791
792         // Don't use stub address if we have one from the staged user.
793         if (cuser.mailing_addresses.length || cuser.billing_addresses.length)
794             user.addresses = [];
795
796         // is_mailing=false implies is_billing
797         function addr_from_stage(stage_addr) {
798             if (!stage_addr) return;
799             var cls = stage_addr.classname;
800
801             var addr = {
802                 id : service.virt_id--,
803                 usr : user.id,
804                 isnew : true,
805                 valid : true,
806                 address_type : egCore.strings.REG_ADDR_TYPE,
807                 _is_mailing : cls == 'stgma',
808                 _is_billing : cls == 'stgba'
809             };
810
811             user.mailing_address = addr;
812             user.addresses.push(addr);
813
814             for (var key in egCore.idl.classes[cls].field_map) {
815                 if (egCore.idl.classes.aua.field_map[key] &&
816                     !egCore.idl.classes[cls].field_map[key].virtual) {
817                     if (stage_addr[key]() !== null)
818                         addr[key] = stage_addr[key]();
819                 }
820             }
821         }
822
823         addr_from_stage(cuser.mailing_addresses[0]);
824         addr_from_stage(cuser.billing_addresses[0]);
825
826         if (user.addresses.length == 1) {
827             // If there is only one address, 
828             // use it as both mailing and billing.
829             var addr = user.addresses[0];
830             addr._is_mailing = addr._is_billing = true;
831             user.mailing_address = user.billing_address = addr;
832         }
833
834         if (cuser.cards.length) {
835             user.card = {
836                 id : service.virt_id--,
837                 barcode : cuser.cards[0].barcode(),
838                 isnew : true,
839                 active : true,
840                 _primary : true
841             };
842
843             user.cards.push(user.card);
844             if (user.usrname == '') 
845                 user.usrname = card.barcode;
846         }
847
848         angular.forEach(cuser.settings, function(setting) {
849             service.user_settings[setting.setting()] = Boolean(setting.value());
850         });
851     }
852
853     // copy select values from the cloned user to the new user.
854     // user is a hash
855     service.copy_clone_data = function(user) {
856         var clone_user = service.clone_user;
857
858         // flesh the home org locally
859         user.home_ou = egCore.org.get(clone_user.home_ou());
860         if (user.profile) user.profile = egCore.env.pgt.map[user.profile];
861
862         if (!clone_user.billing_address() &&
863             !clone_user.mailing_address())
864             return; // no addresses to copy or link
865
866         // if the cloned user has any addresses, we don't need 
867         // the stub address created in init_new_patron.
868         user.addresses = [];
869
870         var copy_addresses = 
871             service.org_settings['circ.patron_edit.clone.copy_address'];
872
873         var clone_fields = [
874             'day_phone',
875             'evening_phone',
876             'other_phone',
877             'usrgroup'
878         ]; 
879
880         angular.forEach(clone_fields, function(field) {
881             user[field] = clone_user[field]();
882         });
883
884         if (copy_addresses) {
885             var bill_addr, mail_addr;
886
887             // copy the billing and mailing addresses into new addresses
888             function clone_addr(addr) {
889                 var new_addr = egCore.idl.toHash(addr);
890                 new_addr.id = service.virt_id--;
891                 new_addr.usr = user.id;
892                 new_addr.isnew = true;
893                 new_addr.valid = true;
894                 user.addresses.push(new_addr);
895                 return new_addr;
896             }
897
898             if (bill_addr = clone_user.billing_address()) {
899                 var addr = clone_addr(bill_addr);
900                 addr._is_billing = true;
901                 user.billing_address = addr;
902             }
903
904             if (mail_addr = clone_user.mailing_address()) {
905
906                 if (bill_addr && bill_addr.id() == mail_addr.id()) {
907                     user.mailing_address = user.billing_address;
908                     user.mailing_address._is_mailing = true;
909                 } else {
910                     var addr = clone_addr(mail_addr);
911                     addr._is_mailing = true;
912                     user.mailing_address = addr;
913                 }
914
915                 if (!bill_addr) {
916                     // if there is no billing addr, use the mailing addr
917                     user.billing_address = user.mailing_address;
918                     user.billing_address._is_billing = true;
919                 }
920             }
921
922
923         } else {
924
925             // link the billing and mailing addresses
926             var addr;
927             if (addr = clone_user.billing_address()) {
928                 user.billing_address = egCore.idl.toHash(addr);
929                 user.billing_address._is_billing = true;
930                 user.addresses.push(user.billing_address);
931                 user.billing_address._linked_owner_id = clone_user.id();
932                 user.billing_address._linked_owner = service.format_name(
933                     clone_user.family_name(),
934                     clone_user.first_given_name(),
935                     clone_user.second_given_name()
936                 );
937             }
938
939             if (addr = clone_user.mailing_address()) {
940                 if (user.billing_address && 
941                     addr.id() == user.billing_address.id) {
942                     // mailing matches billing
943                     user.mailing_address = user.billing_address;
944                     user.mailing_address._is_mailing = true;
945                 } else {
946                     user.mailing_address = egCore.idl.toHash(addr);
947                     user.mailing_address._is_mailing = true;
948                     user.addresses.push(user.mailing_address);
949                     user.mailing_address._linked_owner_id = clone_user.id();
950                     user.mailing_address._linked_owner = service.format_name(
951                         clone_user.family_name(),
952                         clone_user.first_given_name(),
953                         clone_user.second_given_name()
954                     );
955                 }
956             }
957         }
958     }
959
960     // translate the patron back into IDL form
961     service.save_user = function(phash) {
962
963         var patron = egCore.idl.fromHash('au', phash);
964
965         patron.home_ou(patron.home_ou().id());
966         patron.expire_date(patron.expire_date().toISOString());
967         patron.profile(patron.profile().id());
968         if (patron.dob()) 
969             patron.dob(patron.dob().toISOString().replace(/T.*/,''));
970         if (patron.ident_type()) 
971             patron.ident_type(patron.ident_type().id());
972         if (patron.net_access_level())
973             patron.net_access_level(patron.net_access_level().id());
974
975         angular.forEach(
976             ['juvenile', 'barred', 'active', 'master_account'],
977             function(field) { patron[field](phash[field] ? 't' : 'f'); }
978         );
979
980         var card_hashes = patron.cards();
981         patron.cards([]);
982         angular.forEach(card_hashes, function(chash) {
983             var card = egCore.idl.fromHash('ac', chash)
984             card.usr(patron.id());
985             card.active(chash.active ? 't' : 'f');
986             patron.cards().push(card);
987             if (chash._primary) {
988                 patron.card(card);
989             }
990         });
991
992         var addr_hashes = patron.addresses();
993         patron.addresses([]);
994         angular.forEach(addr_hashes, function(addr_hash) {
995             if (!addr_hash.isnew && !addr_hash.isdeleted) 
996                 addr_hash.ischanged = true;
997             var addr = egCore.idl.fromHash('aua', addr_hash);
998             patron.addresses().push(addr);
999             addr.valid(addr.valid() ? 't' : 'f');
1000             addr.within_city_limits(addr.within_city_limits() ? 't' : 'f');
1001             addr.pending(addr.pending() ? 't' : 'f');
1002             if (addr_hash._is_mailing) patron.mailing_address(addr);
1003             if (addr_hash._is_billing) patron.billing_address(addr);
1004         });
1005
1006         patron.survey_responses([]);
1007         angular.forEach(service.survey_responses, function(answer) {
1008             var question = service.survey_questions[answer.question()];
1009             var resp = new egCore.idl.asvr();
1010             resp.isnew(true);
1011             resp.survey(question.survey());
1012             resp.question(question.id());
1013             resp.answer(answer.id());
1014             resp.usr(patron.id());
1015             resp.answer_date('now');
1016             patron.survey_responses().push(resp);
1017         });
1018         
1019         // re-object-ify the patron stat cat entry maps
1020         var maps = [];
1021         angular.forEach(patron.stat_cat_entries(), function(entry) {
1022             var e = egCore.idl.fromHash('actscecm', entry);
1023             e.stat_cat(e.stat_cat().id);
1024             maps.push(e);
1025         });
1026         patron.stat_cat_entries(maps);
1027
1028         // service.stat_cat_entry_maps maps stats to values
1029         // patron.stat_cat_entries is an array of stat_cat_entry_usr_map's
1030         angular.forEach(
1031             service.stat_cat_entry_maps, function(value, cat_id) {
1032
1033             // see if we already have a mapping for this entry
1034             var existing = patron.stat_cat_entries().filter(
1035                 function(e) { return e.stat_cat() == cat_id })[0];
1036
1037             if (existing) { // we have a mapping
1038                 // if the existing mapping matches the new one,
1039                 // there' nothing left to do
1040                 if (existing.stat_cat_entry() == value) return;
1041
1042                 // mappings differ.  delete the old one and create
1043                 // a new one below.
1044                 existing.isdeleted(true);
1045             }
1046
1047             var newmap = new egCore.idl.actscecm();
1048             newmap.target_usr(patron.id());
1049             newmap.isnew(true);
1050             newmap.stat_cat(cat_id);
1051             newmap.stat_cat_entry(value);
1052             patron.stat_cat_entries().push(newmap);
1053         });
1054
1055         if (!patron.isnew()) patron.ischanged(true);
1056
1057         return egCore.net.request(
1058             'open-ils.actor', 
1059             'open-ils.actor.patron.update',
1060             egCore.auth.token(), patron);
1061     }
1062
1063     service.remove_staged_user = function() {
1064         if (!service.stage_user) return $q.when();
1065         return egCore.net.request(
1066             'open-ils.actor',
1067             'open-ils.actor.user.stage.delete',
1068             egCore.auth.token(),
1069             service.stage_user.user.row_id()
1070         );
1071     }
1072
1073     service.save_user_settings = function(new_user, user_settings) {
1074
1075         var settings = {};
1076         if (service.patron_id) {
1077             // Update all user editor setting values for existing 
1078             // users regardless of whether a value changed.
1079             settings = user_settings;
1080
1081         } else {
1082             // Create settings for all non-null setting values for new patrons.
1083             angular.forEach(user_settings, function(val, key) {
1084                 if (val !== null) settings[key] = val;
1085             });
1086         }
1087
1088         if (Object.keys(settings).length == 0) return $q.when();
1089
1090         return egCore.net.request(
1091             'open-ils.actor',
1092             'open-ils.actor.patron.settings.update',
1093             egCore.auth.token(), new_user.id(), settings
1094         ).then(function(resp) {
1095             return resp;
1096         });
1097     }
1098
1099     // Applies field-specific validation regex's from org settings 
1100     // to form fields.  Be careful not remove any pattern data we
1101     // are not explicitly over-writing in the provided patterns obj.
1102     service.set_field_patterns = function(patterns) {
1103         if (service.org_settings['opac.username_regex']) {
1104             patterns.au.usrname = 
1105                 new RegExp(service.org_settings['opac.username_regex']);
1106         }
1107
1108         if (service.org_settings['ui.patron.edit.ac.barcode.regex']) {
1109             patterns.ac.barcode = 
1110                 new RegExp(service.org_settings['ui.patron.edit.ac.barcode.regex']);
1111         }
1112
1113         if (service.org_settings['global.password_regex']) {
1114             patterns.au.passwd = 
1115                 new RegExp(service.org_settings['global.password_regex']);
1116         }
1117
1118         var phone_reg = service.org_settings['ui.patron.edit.phone.regex'];
1119         if (phone_reg) {
1120             // apply generic phone regex first, replace below as needed.
1121             patterns.au.day_phone = new RegExp(phone_reg);
1122             patterns.au.evening_phone = new RegExp(phone_reg);
1123             patterns.au.other_phone = new RegExp(phone_reg);
1124         }
1125
1126         // the remaining patterns fit a well-known key name pattern
1127
1128         angular.forEach(service.org_settings, function(val, key) {
1129             if (!val) return;
1130             var parts = key.match(/ui.patron.edit\.(\w+)\.(\w+)\.regex/);
1131             if (!parts) return;
1132             var cls = parts[1];
1133             var name = parts[2];
1134             patterns[cls][name] = new RegExp(val);
1135         });
1136     }
1137
1138     return service;
1139 }])
1140
1141 .controller('PatronRegCtrl',
1142        ['$scope','$routeParams','$q','$uibModal','$window','egCore',
1143         'patronSvc','patronRegSvc','egUnloadPrompt','egAlertDialog',
1144         'egWorkLog',
1145 function($scope , $routeParams , $q , $uibModal , $window , egCore ,
1146          patronSvc , patronRegSvc , egUnloadPrompt, egAlertDialog ,
1147          egWorkLog) {
1148
1149     $scope.page_data_loaded = false;
1150     $scope.hold_notify_type = { phone : null, email : null, sms : null };
1151     $scope.clone_id = patronRegSvc.clone_id = $routeParams.clone_id;
1152     $scope.stage_username = 
1153         patronRegSvc.stage_username = $routeParams.stage_username;
1154     $scope.patron_id = 
1155         patronRegSvc.patron_id = $routeParams.edit_id || $routeParams.id;
1156
1157     // for existing patrons, disable barcode input by default
1158     $scope.disable_bc = $scope.focus_usrname = Boolean($scope.patron_id);
1159     $scope.focus_bc = !Boolean($scope.patron_id);
1160     $scope.address_alerts = [];
1161     $scope.dupe_counts = {};
1162
1163     // map of perm name to true/false for perms the logged in user
1164     // has at the currently selected patron home org unit.
1165     $scope.perms = {};
1166
1167     if (!$scope.edit_passthru) {
1168         // in edit more, scope.edit_passthru is delivered to us by
1169         // the enclosing controller.  In register mode, there is 
1170         // no enclosing controller, so we create our own.
1171         $scope.edit_passthru = {};
1172     }
1173
1174     // 0=all, 1=suggested, 2=all
1175     $scope.edit_passthru.vis_level = 0; 
1176
1177     // Apply default values for new patrons during initial registration
1178     // prs is shorthand for patronSvc
1179     function set_new_patron_defaults(prs) {
1180         if (!$scope.patron.passwd) {
1181             // passsword may originate from staged user.
1182             $scope.generate_password();
1183         }
1184         $scope.hold_notify_type.phone = true;
1185         $scope.hold_notify_type.email = true;
1186         $scope.hold_notify_type.sms = false;
1187
1188         // staged users may be loaded w/ a profile.
1189         $scope.set_expire_date();
1190
1191         if (prs.org_settings['ui.patron.default_ident_type']) {
1192             // $scope.patron needs this field to be an object
1193             var id = prs.org_settings['ui.patron.default_ident_type'];
1194             var ident_type = $scope.ident_types.filter(
1195                 function(type) { return type.id() == id })[0];
1196             $scope.patron.ident_type = ident_type;
1197         }
1198         if (prs.org_settings['ui.patron.default_inet_access_level']) {
1199             // $scope.patron needs this field to be an object
1200             var id = prs.org_settings['ui.patron.default_inet_access_level'];
1201             var level = $scope.net_access_levels.filter(
1202                 function(lvl) { return lvl.id() == id })[0];
1203             $scope.patron.net_access_level = level;
1204         }
1205         if (prs.org_settings['ui.patron.default_country']) {
1206             $scope.patron.addresses[0].country = 
1207                 prs.org_settings['ui.patron.default_country'];
1208         }
1209     }
1210
1211     // A null or undefined pattern leads to exceptions.  Before the
1212     // patterns are loaded from the server, default all patterns
1213     // to an innocuous regex.  To avoid re-creating numerous
1214     // RegExp objects, cache the stub RegExp after initial creation.
1215     // note: angular docs say ng-pattern accepts a regexp or string,
1216     // but as of writing, it only works with a regexp object.
1217     // (Likely an angular 1.2 vs. 1.4 issue).
1218     var field_patterns = {au : {}, ac : {}, aua : {}};
1219     $scope.field_pattern = function(cls, field) { 
1220         if (!field_patterns[cls][field])
1221             field_patterns[cls][field] = new RegExp('.*');
1222         return field_patterns[cls][field];
1223     }
1224
1225     // Main page load function.  Kicks off tab init and data loading.
1226     $q.all([
1227
1228         $scope.initTab ? // initTab comes from patron app
1229             $scope.initTab('edit', $routeParams.id) : $q.when(),
1230
1231         patronRegSvc.init(),
1232
1233     ]).then(function(){ return patronRegSvc.init_patron(patronSvc ? patronSvc.current : patronRegSvc.patron ) })
1234       .then(function(patron) {
1235         // called after initTab and patronRegSvc.init have completed
1236         // in standalone mode, we have no patronSvc
1237         var prs = patronRegSvc;
1238         $scope.patron = patron;
1239         $scope.field_doc = prs.field_doc;
1240         $scope.edit_profiles = prs.edit_profiles;
1241         $scope.ident_types = prs.ident_types;
1242         $scope.net_access_levels = prs.net_access_levels;
1243         $scope.user_setting_types = prs.user_setting_types;
1244         $scope.opt_in_setting_types = prs.opt_in_setting_types;
1245         $scope.org_settings = prs.org_settings;
1246         $scope.sms_carriers = prs.sms_carriers;
1247         $scope.stat_cats = prs.stat_cats;
1248         $scope.surveys = prs.surveys;
1249         $scope.survey_responses = prs.survey_responses;
1250         $scope.stat_cat_entry_maps = prs.stat_cat_entry_maps;
1251         $scope.stage_user = prs.stage_user;
1252         $scope.stage_user_requestor = prs.stage_user_requestor;
1253
1254         $scope.user_settings = prs.user_settings;
1255         prs.user_settings = {};
1256
1257         // If a default pickup lib is applied to the patron, apply it 
1258         // to the UI at page load time.  Otherwise, leave the value unset.
1259         if ($scope.user_settings['opac.default_pickup_location']) {
1260             $scope.patron._pickup_lib = egCore.org.get(
1261                 $scope.user_settings['opac.default_pickup_location']);
1262         }
1263
1264         extract_hold_notify();
1265         if ($scope.patron.isnew)
1266             set_new_patron_defaults(prs);
1267
1268         $scope.handle_home_org_changed();
1269
1270         if ($scope.org_settings['ui.patron.edit.default_suggested'])
1271             $scope.edit_passthru.vis_level = 1;
1272
1273         // Stat cats are fetched from open-ils.storage, where 't'==1
1274         $scope.hasRequiredStatCat = prs.stat_cats.filter(
1275                 function(cat) {return cat.required() == 1} ).length > 0;
1276
1277         $scope.page_data_loaded = true;
1278
1279         prs.set_field_patterns(field_patterns);
1280         apply_username_regex();
1281
1282         add_date_watchers();
1283     });
1284
1285     function add_date_watchers() {
1286
1287         $scope.$watch('patron.dob', function(newVal, oldVal) {
1288             // Even though this runs after page data load, there
1289             // are still times when it fires unnecessarily.
1290             if (newVal === oldVal) return;
1291
1292             console.debug('dob change: ' + newVal + ' : ' + oldVal);
1293             maintain_juvenile_flag();
1294         });
1295
1296         // No need to watch expire_date
1297     }
1298
1299
1300     // update the currently displayed field documentation
1301     $scope.set_selected_field_doc = function(cls, field) {
1302         $scope.selected_field_doc = $scope.field_doc[cls][field];
1303     }
1304
1305     // returns the tree depth of the selected profile group tree node.
1306     $scope.pgt_depth = function(grp) {
1307         var d = 0;
1308         while (grp = egCore.env.pgt.map[grp.parent()]) d++;
1309         return d;
1310     }
1311
1312     // IDL fields used for labels in the UI.
1313     $scope.idl_fields = {
1314         au  : egCore.idl.classes.au.field_map,
1315         ac  : egCore.idl.classes.ac.field_map,
1316         aua : egCore.idl.classes.aua.field_map
1317     };
1318
1319     // field visibility cache.  Some fields are universally required.
1320     // 3 == value universally required
1321     // 2 == field is visible by default
1322     // 1 == field is suggested by default
1323     var field_visibility = {};
1324     var default_field_visibility = {
1325         'ac.barcode' : 3,
1326         'au.usrname' : 3,
1327         'au.passwd' :  3,
1328         'au.first_given_name' : 3,
1329         'au.family_name' : 3,
1330         'au.ident_type' : 3,
1331         'au.ident_type2' : 2,
1332         'au.home_ou' : 3,
1333         'au.profile' : 3,
1334         'au.expire_date' : 3,
1335         'au.net_access_level' : 3,
1336         'aua.address_type' : 3,
1337         'aua.post_code' : 3,
1338         'aua.street1' : 3,
1339         'aua.street2' : 2,
1340         'aua.city' : 3,
1341         'aua.county' : 2,
1342         'aua.state' : 2,
1343         'aua.country' : 3,
1344         'aua.valid' : 2,
1345         'aua.within_city_limits' : 2,
1346         'stat_cats' : 1,
1347         'surveys' : 1
1348     }; 
1349
1350     // Returns true if the selected field should be visible
1351     // given the current required/suggested/all setting.
1352     // The visibility flag applied to each field as a result of calling
1353     // this function also sets (via the same flag) the requiredness state.
1354     $scope.show_field = function(field_key) {
1355         // org settings have not been received yet.
1356         if (!$scope.org_settings) return false;
1357
1358         if (field_visibility[field_key] == undefined) {
1359             // compile and cache the visibility for the selected field
1360
1361             var req_set = 'ui.patron.edit.' + field_key + '.require';
1362             var sho_set = 'ui.patron.edit.' + field_key + '.show';
1363             var sug_set = 'ui.patron.edit.' + field_key + '.suggest';
1364
1365             if ($scope.org_settings[req_set]) {
1366                 field_visibility[field_key] = 3;
1367
1368             } else if ($scope.org_settings[sho_set]) {
1369                 field_visibility[field_key] = 2;
1370
1371             } else if ($scope.org_settings[sug_set]) {
1372                 field_visibility[field_key] = 1;
1373             }
1374         }
1375
1376         if (field_visibility[field_key] == undefined) {
1377             // No org settings were applied above.  Use the default
1378             // settings if present or assume the field has no
1379             // visibility flags applied.
1380             field_visibility[field_key] = 
1381                 default_field_visibility[field_key] || 0;
1382         }
1383
1384         return field_visibility[field_key] >= $scope.edit_passthru.vis_level;
1385     }
1386
1387     // See $scope.show_field().
1388     // A field with visbility level 3 means it's required.
1389     $scope.field_required = function(cls, field) {
1390
1391         // Value in the password field is not required
1392         // for existing patrons.
1393         if (field == 'passwd' && $scope.patron && !$scope.patron.isnew) 
1394           return false;
1395
1396         return (field_visibility[cls + '.' + field] == 3);
1397     }
1398
1399     // generates a random 4-digit password
1400     $scope.generate_password = function() {
1401         $scope.patron.passwd = Math.floor(Math.random()*9000) + 1000;
1402     }
1403
1404     $scope.set_expire_date = function() {
1405         if (!$scope.patron.profile) return;
1406         var seconds = egCore.date.intervalToSeconds(
1407             $scope.patron.profile.perm_interval());
1408         var now_epoch = new Date().getTime();
1409         $scope.patron.expire_date = new Date(
1410             now_epoch + (seconds * 1000 /* milliseconds */))
1411         $scope.field_modified();
1412     }
1413
1414     // grp is the pgt object
1415     $scope.set_profile = function(grp) {
1416         $scope.patron.profile = grp;
1417         $scope.set_expire_date();
1418         $scope.field_modified();
1419     }
1420
1421     $scope.invalid_profile = function() {
1422         return !(
1423             $scope.patron && 
1424             $scope.patron.profile && 
1425             $scope.patron.profile.usergroup() == 't'
1426         );
1427     }
1428
1429     $scope.new_address = function() {
1430         var addr = egCore.idl.toHash(new egCore.idl.aua());
1431         patronRegSvc.ingest_address($scope.patron, addr);
1432         addr.id = patronRegSvc.virt_id--;
1433         addr.isnew = true;
1434         addr.valid = true;
1435         addr.within_city_limits = true;
1436         addr.country = $scope.org_settings['ui.patron.default_country'];
1437         $scope.patron.addresses.push(addr);
1438     }
1439
1440     // keep deleted addresses out of the patron object so
1441     // they won't appear in the UI.  They'll be re-inserted
1442     // when the patron is updated.
1443     deleted_addresses = [];
1444     $scope.delete_address = function(id) {
1445
1446         if ($scope.patron.isnew &&
1447             $scope.patron.addresses.length == 1 &&
1448             $scope.org_settings['ui.patron.registration.require_address']) {
1449             egAlertDialog.open(egCore.strings.REG_ADDR_REQUIRED);
1450             return;
1451         }
1452
1453         var addresses = [];
1454         angular.forEach($scope.patron.addresses, function(addr) {
1455             if (addr.id == id) {
1456                 if (id > 0) {
1457                     addr.isdeleted = true;
1458                     deleted_addresses.push(addr);
1459                 }
1460             } else {
1461                 addresses.push(addr);
1462             }
1463         });
1464         $scope.patron.addresses = addresses;
1465     } 
1466
1467     $scope.approve_pending_address = function(addr) {
1468
1469         egCore.net.request(
1470             'open-ils.actor',
1471             'open-ils.actor.user.pending_address.approve',
1472             egCore.auth.token(), addr.id
1473         ).then(function(replaced_id) {
1474             var evt = egCore.evt.parse(replaced_id);
1475             if (evt) { alert(evt); return; }
1476
1477             // Remove the pending address and the replaced address
1478             // from the local list of patron addresses.
1479             var addresses = [];
1480             angular.forEach($scope.patron.addresses, function(a) {
1481                 if (a.id != addr.id && a.id != replaced_id) {
1482                     addresses.push(a);
1483                 }
1484             });
1485             $scope.patron.addresses = addresses;
1486
1487             // Fetch a fresh copy of the modified address from the server.
1488             // and add it back to the list.
1489             egCore.pcrud.retrieve('aua', replaced_id, {}, {authoritative: true})
1490             .then(null, null, function(new_addr) {
1491                 new_addr = egCore.idl.toHash(new_addr);
1492                 patronRegSvc.ingest_address($scope.patron, new_addr);
1493                 $scope.patron.addresses.push(new_addr);
1494             });
1495         });
1496     }
1497
1498     $scope.post_code_changed = function(addr) { 
1499         egCore.net.request(
1500             'open-ils.search', 'open-ils.search.zip', addr.post_code)
1501         .then(function(resp) {
1502             if (!resp) return;
1503             if (resp.city) addr.city = resp.city;
1504             if (resp.state) addr.state = resp.state;
1505             if (resp.county) addr.county = resp.county;
1506             if (resp.alert) alert(resp.alert);
1507         });
1508     }
1509
1510     $scope.replace_card = function() {
1511         $scope.patron.card.active = false;
1512         $scope.patron.card.ischanged = true;
1513         $scope.disable_bc = false;
1514
1515         var new_card = egCore.idl.toHash(new egCore.idl.ac());
1516         new_card.id = patronRegSvc.virt_id--;
1517         new_card.isnew = true;
1518         new_card.active = true;
1519         new_card._primary = 'on';
1520         $scope.patron.card = new_card;
1521
1522         // Remove any previous attempts to replace the card, since they
1523         // may be incomplete or created by accident.
1524         $scope.patron.cards =
1525             $scope.patron.cards.filter(function(c) {return !c.isnew})
1526         $scope.patron.cards.push(new_card);
1527     }
1528
1529     $scope.day_phone_changed = function(phone) {
1530         if (phone && $scope.patron.isnew && 
1531             $scope.org_settings['patron.password.use_phone']) {
1532             $scope.patron.passwd = phone.substr(-4);
1533         }
1534     }
1535
1536     $scope.barcode_changed = function(bc) {
1537         if (!bc) return;
1538         $scope.dupe_barcode = false;
1539         egCore.net.request(
1540             'open-ils.actor',
1541             'open-ils.actor.barcode.exists',
1542             egCore.auth.token(), bc
1543         ).then(function(resp) {
1544             if (resp == '1') { // duplicate card
1545                 $scope.dupe_barcode = true;
1546                 console.log('duplicate barcode detected: ' + bc);
1547             } else {
1548                 if (!$scope.patron.usrname)
1549                     $scope.patron.usrname = bc;
1550                 // No dupe -- A-OK
1551             }
1552         });
1553     }
1554
1555     $scope.cards_dialog = function() {
1556         $uibModal.open({
1557             templateUrl: './circ/patron/t_patron_cards_dialog',
1558             backdrop: 'static',
1559             controller: 
1560                    ['$scope','$uibModalInstance','cards','perms','patron',
1561             function($scope , $uibModalInstance , cards , perms , patron) {
1562                 // scope here is the modal-level scope
1563                 $scope.args = {cards : cards, primary_barcode : null};
1564                 angular.forEach(cards, function(card) {
1565                     if (card.id == patron.card.id) {
1566                         $scope.args.primary_barcode = card.id;
1567                     }
1568                 });
1569                 $scope.perms = perms;
1570                 $scope.ok = function() { $uibModalInstance.close($scope.args) }
1571                 $scope.cancel = function () { $uibModalInstance.dismiss() }
1572             }],
1573             resolve : {
1574                 cards : function() {
1575                     // scope here is the controller-level scope
1576                     return $scope.patron.cards;
1577                 },
1578                 perms : function() {
1579                     return $scope.perms;
1580                 },
1581                 patron : function() {
1582                     return $scope.patron;
1583                 }
1584             }
1585         }).result.then(
1586             function(args) {
1587                 angular.forEach(args.cards, function(card) {
1588                     card.ischanged = true; // assume cards need updating, OK?
1589                     if (card.id == args.primary_barcode) {
1590                         $scope.patron.card = card;
1591                         card._primary = true;
1592                     } else {
1593                         card._primary = false;
1594                     }
1595                 });
1596             }
1597         );
1598     }
1599
1600     $scope.set_addr_type = function(addr, type) {
1601         var addrs = $scope.patron.addresses;
1602         if (addr['_is_'+type]) {
1603             angular.forEach(addrs, function(a) {
1604                 if (a.id != addr.id) a['_is_'+type] = false;
1605             });
1606         } else {
1607             // unchecking mailing/billing means we have to randomly
1608             // select another address to fill that role.  Select the
1609             // first address in the list (that does not match the
1610             // modifed address)
1611             for (var i = 0; i < addrs.length; i++) {
1612                 if (addrs[i].id != addr.id) {
1613                     addrs[i]['_is_' + type] = true;
1614                     break;
1615                 }
1616             }
1617         }
1618     }
1619
1620
1621     // Translate hold notify preferences from the form/scope back into a 
1622     // single user setting value for opac.hold_notify.
1623     function compress_hold_notify() {
1624         var hold_notify_methods = [];
1625         if ($scope.hold_notify_type.phone) {
1626             hold_notify_methods.push('phone');
1627         }
1628         if ($scope.hold_notify_type.email) {
1629             hold_notify_methods.push('email');
1630         }
1631         if ($scope.hold_notify_type.sms) {
1632             hold_notify_methods.push('sms');
1633         }
1634
1635         $scope.user_settings['opac.hold_notify'] = hold_notify_methods.join(':');
1636     }
1637
1638     // dialog for selecting additional permission groups
1639     $scope.secondary_groups_dialog = function() {
1640         $uibModal.open({
1641             templateUrl: './circ/patron/t_patron_groups_dialog',
1642             backdrop: 'static',
1643             controller: 
1644                    ['$scope','$uibModalInstance','linked_groups','pgt_depth',
1645             function($scope , $uibModalInstance , linked_groups , pgt_depth) {
1646
1647                 $scope.pgt_depth = pgt_depth;
1648                 $scope.args = {
1649                     linked_groups : linked_groups,
1650                     edit_profiles : patronRegSvc.edit_profiles,
1651                     new_profile   : patronRegSvc.edit_profiles[0]
1652                 };
1653
1654                 // add a new group to the linked groups list
1655                 $scope.link_group = function($event, grp) {
1656                     var found = false; // avoid duplicates
1657                     angular.forEach($scope.args.linked_groups, 
1658                         function(g) {if (g.id() == grp.id()) found = true});
1659                     if (!found) $scope.args.linked_groups.push(grp);
1660                     $event.preventDefault(); // avoid close
1661                 }
1662
1663                 // remove a group from the linked groups list
1664                 $scope.unlink_group = function($event, grp) {
1665                     $scope.args.linked_groups = 
1666                         $scope.args.linked_groups.filter(function(g) {
1667                         return g.id() != grp.id()
1668                     });
1669                     $event.preventDefault(); // avoid close
1670                 }
1671
1672                 $scope.ok = function() { $uibModalInstance.close($scope.args) }
1673                 $scope.cancel = function () { $uibModalInstance.dismiss() }
1674             }],
1675             resolve : {
1676                 linked_groups : function() { return $scope.patron.groups },
1677                 pgt_depth : function() { return $scope.pgt_depth }
1678             }
1679         }).result.then(
1680             function(args) {
1681
1682                 if ($scope.patron.isnew) {
1683                     // groups must be linked for new patrons after the
1684                     // patron is created.
1685                     $scope.patron.groups = args.linked_groups;
1686                     return;
1687                 }
1688
1689                 // update links groups for existing users in real time.
1690                 var ids = args.linked_groups.map(function(g) {return g.id()});
1691                 patronRegSvc.apply_secondary_groups($scope.patron.id, ids)
1692                 .then(function(success) {
1693                     if (success)
1694                         $scope.patron.groups = args.linked_groups;
1695                 });
1696             }
1697         );
1698     }
1699
1700     function extract_hold_notify() {
1701         var notify = $scope.user_settings['opac.hold_notify'];
1702         if (!notify) return;
1703         $scope.hold_notify_type.phone = Boolean(notify.match(/phone/));
1704         $scope.hold_notify_type.email = Boolean(notify.match(/email/));
1705         $scope.hold_notify_type.sms = Boolean(notify.match(/sms/));
1706     }
1707
1708     $scope.invalidate_field = function(field) {
1709         patronRegSvc.invalidate_field($scope.patron, field);
1710     }
1711
1712     address_alert = function(addr) {
1713         var args = {
1714             street1: addr.street1,
1715             street2: addr.street2,
1716             city: addr.city,
1717             state: addr.state,
1718             county: addr.county,
1719             country: addr.country,
1720             post_code: addr.post_code,
1721             mailing_address: addr._is_mailing,
1722             billing_address: addr._is_billing
1723         }
1724
1725         egCore.net.request(
1726             'open-ils.actor',
1727             'open-ils.actor.address_alert.test',
1728             egCore.auth.token(), egCore.auth.user().ws_ou(), args
1729             ).then(function(res) {
1730                 $scope.address_alerts = res;
1731         });
1732     }
1733
1734     $scope.dupe_value_changed = function(type, value) {
1735         if (!$scope.dupe_search_encoded)
1736             $scope.dupe_search_encoded = {};
1737
1738         $scope.dupe_counts[type] = 0;
1739
1740         patronRegSvc.dupe_patron_search($scope.patron, type, value)
1741         .then(function(res) {
1742             $scope.dupe_counts[type] = res.count;
1743             if (res.count) {
1744                 $scope.dupe_search_encoded[type] = 
1745                     encodeURIComponent(js2JSON(res.search));
1746             } else {
1747                 $scope.dupe_search_encoded[type] = '';
1748             }
1749         });
1750     }
1751
1752     $scope.handle_home_org_changed = function() {
1753         org_id = $scope.patron.home_ou.id();
1754         patronRegSvc.has_perms_for_org(org_id).then(function(map) {
1755             angular.forEach(map, function(v, k) { $scope.perms[k] = v });
1756         });
1757     }
1758
1759     $scope.handle_pulib_changed = function(org) {
1760         if (!$scope.user_settings) return; // still rendering
1761         $scope.user_settings['opac.default_pickup_location'] = org.id();
1762     }
1763
1764     // This is called with every character typed in a form field,
1765     // since that's the only way to gaurantee something has changed.
1766     // See handle_field_changed for ng-change vs. ng-blur.
1767     $scope.field_modified = function() {
1768         // Call attach with every field change, regardless of whether
1769         // it's been called before.  This will allow for re-attach after
1770         // the user clicks through the unload warning. egUnloadPrompt
1771         // will ensure we only attach once.
1772         egUnloadPrompt.attach($scope);
1773     }
1774
1775     // also monitor when form is changed *by the user*, as using
1776     // an ng-change handler doesn't work with eg-date-input
1777     $scope.$watch('reg_form.$pristine', function(newVal, oldVal) {
1778         if (!newVal) egUnloadPrompt.attach($scope);
1779     });
1780
1781     // username regex (if present) must be removed any time
1782     // the username matches the barcode to avoid firing the
1783     // invalid field handlers.
1784     function apply_username_regex() {
1785         var regex = $scope.org_settings['opac.username_regex'];
1786         if (regex) {
1787             if ($scope.patron.card.barcode) {
1788                 // username must match the regex or the barcode
1789                 field_patterns.au.usrname = 
1790                     new RegExp(
1791                         regex + '|^' + $scope.patron.card.barcode + '$');
1792             } else {
1793                 // username must match the regex
1794                 field_patterns.au.usrname = new RegExp(regex);
1795             }
1796         } else {
1797             // username can be any format.
1798             field_patterns.au.usrname = new RegExp('.*');
1799         }
1800     }
1801
1802     // obj could be the patron, an address, etc.
1803     // This is called any time a form field achieves then loses focus.
1804     // It does not necessarily mean the field has changed.
1805     // The alternative is ng-change, but it's called with each character
1806     // typed, which would be overkill for many of the actions called here.
1807     $scope.handle_field_changed = function(obj, field_name) {
1808         var cls = obj.classname; // set by egIdl
1809         var value = obj[field_name];
1810
1811         console.debug('changing field ' + field_name + ' to ' + value);
1812
1813         switch (field_name) {
1814             case 'day_phone' : 
1815                 if ($scope.patron.day_phone && 
1816                     $scope.patron.isnew && 
1817                     $scope.org_settings['patron.password.use_phone']) {
1818                     $scope.patron.passwd = $scope.patron.day_phone.substr(-4);
1819                 }
1820             case 'evening_phone' : 
1821             case 'other_phone' : 
1822                 $scope.dupe_value_changed(field_name, value);
1823                 break;
1824
1825             case 'ident_value':
1826             case 'ident_value2':
1827                 $scope.dupe_value_changed('ident', value);
1828                 break;
1829
1830             case 'first_given_name':
1831             case 'family_name':
1832                 $scope.dupe_value_changed('name', value);
1833                 break;
1834
1835             case 'email':
1836                 $scope.dupe_value_changed('email', value);
1837                 break;
1838
1839             case 'street1':
1840             case 'street2':
1841             case 'city':
1842                 // dupe search on address wants the address object as the value.
1843                 $scope.dupe_value_changed('address', obj);
1844                 address_alert(obj);
1845                 break;
1846
1847             case 'post_code':
1848                 $scope.post_code_changed(obj);
1849                 break;
1850
1851             case 'usrname':
1852                 patronRegSvc.check_dupe_username(value)
1853                 .then(function(yes) {$scope.dupe_username = Boolean(yes)});
1854                 break;
1855
1856             case 'barcode':
1857                 // TODO: finish barcode_changed handler.
1858                 $scope.barcode_changed(value);
1859                 apply_username_regex();
1860                 break;
1861         }
1862     }
1863
1864     // patron.juvenile is set to true if the user was born after
1865     function maintain_juvenile_flag() {
1866         if ( !($scope.patron && $scope.patron.dob) ) return;
1867
1868         var juv_interval = 
1869             $scope.org_settings['global.juvenile_age_threshold'] 
1870             || '18 years';
1871
1872         var base = new Date();
1873
1874         base.setTime(base.getTime() - 
1875             Number(egCore.date.intervalToSeconds(juv_interval) + '000'));
1876
1877         $scope.patron.juvenile = ($scope.patron.dob > base);
1878     }
1879
1880     // returns true (disable) for orgs that cannot have users.
1881     $scope.disable_home_org = function(org_id) {
1882         if (!org_id) return;
1883         var org = egCore.org.get(org_id);
1884         return (
1885             org &&
1886             org.ou_type() &&
1887             org.ou_type().can_have_users() == 'f'
1888         );
1889     }
1890
1891     // returns true (disable) for orgs that cannot have vols (for holds pickup)
1892     $scope.disable_pulib = function(org_id) {
1893         if (!org_id) return;
1894         return !egCore.org.CanHaveVolumes(org_id);
1895     }
1896
1897     // Returns true if the Save and Save & Clone buttons should be disabled.
1898     $scope.edit_passthru.hide_save_actions = function() {
1899         return $scope.patron.isnew ?
1900             !$scope.perms.CREATE_USER : 
1901             !$scope.perms.UPDATE_USER;
1902     }
1903
1904     // Returns true if any input elements are tagged as invalid
1905     // via Angular patterns or required attributes.
1906     function form_has_invalid_fields() {
1907         return $('#patron-reg-container .ng-invalid').length > 0;
1908     }
1909
1910     function form_is_incomplete() {
1911         return (
1912             $scope.dupe_username ||
1913             $scope.dupe_barcode ||
1914             form_has_invalid_fields()
1915         );
1916
1917     }
1918
1919     $scope.edit_passthru.save = function(save_args) {
1920         if (!save_args) save_args = {};
1921
1922         if (form_is_incomplete()) {
1923             // User has not provided valid values for all required fields.
1924             return egAlertDialog.open(egCore.strings.REG_INVALID_FIELDS);
1925         }
1926
1927         // remove page unload warning prompt
1928         egUnloadPrompt.clear();
1929
1930         // toss the deleted addresses back into the patron's list of
1931         // addresses so it's included in the update
1932         $scope.patron.addresses = 
1933             $scope.patron.addresses.concat(deleted_addresses);
1934         
1935         compress_hold_notify();
1936
1937         var updated_user;
1938
1939         patronRegSvc.save_user($scope.patron)
1940         .then(function(new_user) { 
1941             if (new_user && new_user.classname) {
1942                 updated_user = new_user;
1943                 return patronRegSvc.save_user_settings(
1944                     new_user, $scope.user_settings); 
1945             } else {
1946                 var evt = egCore.evt.parse(new_user);
1947
1948                 if (evt && evt.textcode == 'XACT_COLLISION') {
1949                     return egAlertDialog.open(
1950                         egCore.strings.PATRON_EDIT_COLLISION).result;
1951                 }
1952
1953                 // debug only -- should not get here.
1954                 alert('Patron update failed. \n\n' + js2JSON(new_user));
1955             }
1956
1957         }).then(function() {
1958
1959             // only remove the staged user if the update succeeded.
1960             if (updated_user) 
1961                 return patronRegSvc.remove_staged_user();
1962
1963             return $q.when();
1964
1965         }).then(function() {
1966
1967             // linked groups for new users must be created after the new
1968             // user is created.
1969             if ($scope.patron.isnew && 
1970                 $scope.patron.groups && $scope.patron.groups.length) {
1971                 var ids = $scope.patron.groups.map(function(g) {return g.id()});
1972                 return patronRegSvc.apply_secondary_groups(updated_user.id(), ids)
1973             }
1974
1975             return $q.when();
1976
1977         }).then(function() {
1978
1979             if (updated_user) {
1980                 egWorkLog.record(
1981                     $scope.patron.isnew
1982                     ? egCore.strings.EG_WORK_LOG_REGISTERED_PATRON
1983                     : egCore.strings.EG_WORK_LOG_EDITED_PATRON, {
1984                         'action' : $scope.patron.isnew ? 'registered_patron' : 'edited_patron',
1985                         'patron_id' : updated_user.id()
1986                     }
1987                 );
1988             }
1989
1990             // reloading the page means potentially losing some information
1991             // (e.g. last patron search), but is the only way to ensure all
1992             // components are properly updated to reflect the modified patron.
1993             if (updated_user && save_args.clone) {
1994                 // open a separate tab for registering a new 
1995                 // patron from our cloned data.
1996                 var url = 'https://' 
1997                     + $window.location.hostname 
1998                     + egCore.env.basePath 
1999                     + '/circ/patron/register/clone/' 
2000                     + updated_user.id();
2001                 $window.open(url, '_blank').focus();
2002
2003             } else {
2004                 // reload the current page
2005                 $window.location.href = location.href;
2006             }
2007         });
2008     }
2009
2010     $scope.edit_passthru.print = function() {
2011         var print_data = {patron : $scope.patron}
2012
2013         return egCore.print.print({
2014             context : 'default',
2015             template : 'patron_data',
2016             scope : print_data
2017         });
2018     }
2019 }])