]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/item/missing_pieces.js
06fad21abd0b37734847a848eaac0984d8ee6e3d
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / cat / item / missing_pieces.js
1 angular.module('egItemMissingPieces',
2     ['ngRoute', 'ui.bootstrap', 'egCoreMod','egUiMod'])
3
4 .controller('MissingPiecesCtrl',
5        ['$scope','$q','$window','$location','egCore','egConfirmDialog','egAlertDialog','egCirc',
6 function($scope , $q , $window , $location , egCore , egConfirmDialog , egAlertDialog , egCirc) {
7     
8     $scope.selectMe = true; // focus text input
9     $scope.args = {};
10
11     function get_copy(barcode) {
12
13         return egCore.net.request(
14             'open-ils.actor',
15             'open-ils.actor.get_barcodes',
16             egCore.auth.token(), egCore.auth.user().ws_ou(), 
17             'asset', barcode)
18
19         .then(function(resp) { // get_barcodes
20
21             if (evt = egCore.evt.parse(resp)) {
22                 console.error(evt.toString());
23                 return $q.reject();
24             }
25
26             if (!resp || !resp[0]) {
27                 $scope.bcNotFound = barcode;
28                 $scope.selectMe = true;
29                 return $q.reject();
30             }
31
32             return egCore.pcrud.search('acp', {id : resp[0].id}, {
33                 flesh : 3, 
34                 flesh_fields : {
35                     acp : ['call_number'],
36                     acn : ['record'],
37                     bre : ['simple_record']
38                 },
39                 select : { 
40                     // avoid fleshing MARC on the bre
41                     // note: don't add simple_record.. not sure why
42                     bre : ['id']
43                 } 
44             })
45         })
46     }
47
48     function mark_missing_pieces(copy) {
49
50         egConfirmDialog.open(
51             egCore.strings.CONFIRM_MARK_MISSING_TITLE,
52             egCore.strings.CONFIRM_MARK_MISSING_BODY, {
53             barcode : copy.barcode(), 
54             title : copy.call_number().record().simple_record().title()
55
56         }).result.then(function() {
57
58             // kick off mark missing
59             return egCore.net.request(
60                 'open-ils.circ',
61                 'open-ils.circ.mark_item_missing_pieces',
62                 egCore.auth.token(), copy.id()
63             )
64
65         }).then(function(resp) {
66             var evt = egCore.evt.parse(resp); // should always produce event
67
68             if (evt.textcode == 'ACTION_CIRCULATION_NOT_FOUND') {
69                 return egAlertDialog.open(
70                     egCore.strings.CIRC_NOT_FOUND, {barcode : copy.barcode()});
71             }
72
73             var payload = evt.payload;
74
75             // TODO: open copy editor inline?  new tab?
76
77             // print the missing pieces slip
78             var promise = $q.when();
79             if (payload.slip) {
80                 // wait for completion, since it may spawn a confirm dialog
81                 promise = egCore.print.print({
82                     context : 'default', 
83                     content_type : 'text/html',
84                     content : payload.slip.template_output().data()
85                 });
86             }
87
88             if (payload.letter) {
89                 $scope.letter = payload.letter.template_output().data();
90             }
91
92             // apply patron penalty
93             if (payload.circ) {
94                 promise.then(function() {
95                     egCirc.create_penalty(payload.circ.usr())
96                 });
97             }  
98
99         });
100     }
101
102     $scope.print_letter = function() {
103         egCore.print.print({
104             context : 'mail',
105             content_type : 'text/plain',
106             content : $scope.letter
107         });
108     }
109
110     // find the item by barcode, then proceed w/ missing pieces
111     $scope.submitBarcode = function(args) {
112
113         $scope.bcNotFound = null;
114         if (!args.barcode) return;
115
116         $scope.selectMe = false;
117         $scope.letter = null;
118
119         get_copy(args.barcode).then(mark_missing_pieces);
120     }
121
122 }])
123