Filename | /home/vagrant/kohaclone/Koha/Borrower/Discharge.pm |
Statements | Executed 27 statements in 2.01ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 37µs | 152µs | BEGIN@3 | Koha::Borrower::Discharge::
1 | 1 | 1 | 21µs | 3.31ms | count | Koha::Borrower::Discharge::
1 | 1 | 1 | 16µs | 48µs | BEGIN@10 | Koha::Borrower::Discharge::
1 | 1 | 1 | 12µs | 33µs | BEGIN@4 | Koha::Borrower::Discharge::
1 | 1 | 1 | 12µs | 93µs | BEGIN@5 | Koha::Borrower::Discharge::
1 | 1 | 1 | 12µs | 14µs | BEGIN@12 | Koha::Borrower::Discharge::
1 | 1 | 1 | 10µs | 30µs | BEGIN@13 | Koha::Borrower::Discharge::
1 | 1 | 1 | 7µs | 116µs | BEGIN@9 | Koha::Borrower::Discharge::
1 | 1 | 1 | 100ns | 100ns | BEGIN@6 | Koha::Borrower::Discharge::
1 | 1 | 1 | 100ns | 100ns | BEGIN@8 | Koha::Borrower::Discharge::
0 | 0 | 0 | 0s | 0s | can_be_discharged | Koha::Borrower::Discharge::
0 | 0 | 0 | 0s | 0s | discharge | Koha::Borrower::Discharge::
0 | 0 | 0 | 0s | 0s | generate_as_pdf | Koha::Borrower::Discharge::
0 | 0 | 0 | 0s | 0s | get_pendings | Koha::Borrower::Discharge::
0 | 0 | 0 | 0s | 0s | get_validated | Koha::Borrower::Discharge::
0 | 0 | 0 | 0s | 0s | is_discharged | Koha::Borrower::Discharge::
0 | 0 | 0 | 0s | 0s | request | Koha::Borrower::Discharge::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package Koha::Borrower::Discharge; | ||||
2 | |||||
3 | 2 | 164µs | 2 | 267µs | # spent 152µs (37+115) within Koha::Borrower::Discharge::BEGIN@3 which was called:
# once (37µs+115µs) by CGI::Compile::ROOT::home_vagrant_kohaclone_mainpage_2epl::BEGIN@32 at line 3 # spent 152µs making 1 call to Koha::Borrower::Discharge::BEGIN@3
# spent 115µs making 1 call to Modern::Perl::import |
4 | 2 | 65µs | 2 | 54µs | # spent 33µs (12+21) within Koha::Borrower::Discharge::BEGIN@4 which was called:
# once (12µs+21µs) by CGI::Compile::ROOT::home_vagrant_kohaclone_mainpage_2epl::BEGIN@32 at line 4 # spent 33µs making 1 call to Koha::Borrower::Discharge::BEGIN@4
# spent 21µs making 1 call to CGI::import |
5 | 2 | 88µs | 2 | 174µs | # spent 93µs (12+81) within Koha::Borrower::Discharge::BEGIN@5 which was called:
# once (12µs+81µs) by CGI::Compile::ROOT::home_vagrant_kohaclone_mainpage_2epl::BEGIN@32 at line 5 # spent 93µs making 1 call to Koha::Borrower::Discharge::BEGIN@5
# spent 81µs making 1 call to Exporter::import |
6 | 2 | 100ns | 2 | 100ns | # spent 100ns within Koha::Borrower::Discharge::BEGIN@6 which was called:
# once (100ns+0s) by CGI::Compile::ROOT::home_vagrant_kohaclone_mainpage_2epl::BEGIN@32 at line 6 # spent 100ns making 1 call to Koha::Borrower::Discharge::BEGIN@6
# spent 0s making 1 call to Exporter::import |
7 | |||||
8 | 2 | 300ns | 2 | 100ns | # spent 100ns within Koha::Borrower::Discharge::BEGIN@8 which was called:
# once (100ns+0s) by CGI::Compile::ROOT::home_vagrant_kohaclone_mainpage_2epl::BEGIN@32 at line 8 # spent 100ns making 1 call to Koha::Borrower::Discharge::BEGIN@8
# spent 0s making 1 call to Class::Accessor::import |
9 | 2 | 150µs | 2 | 226µs | # spent 116µs (7+110) within Koha::Borrower::Discharge::BEGIN@9 which was called:
# once (7µs+110µs) by CGI::Compile::ROOT::home_vagrant_kohaclone_mainpage_2epl::BEGIN@32 at line 9 # spent 116µs making 1 call to Koha::Borrower::Discharge::BEGIN@9
# spent 110µs making 1 call to Exporter::import |
10 | 2 | 70µs | 2 | 81µs | # spent 48µs (16+32) within Koha::Borrower::Discharge::BEGIN@10 which was called:
# once (16µs+32µs) by CGI::Compile::ROOT::home_vagrant_kohaclone_mainpage_2epl::BEGIN@32 at line 10 # spent 48µs making 1 call to Koha::Borrower::Discharge::BEGIN@10
# spent 32µs making 1 call to Exporter::import |
11 | |||||
12 | 2 | 38µs | 2 | 17µs | # spent 14µs (12+3) within Koha::Borrower::Discharge::BEGIN@12 which was called:
# once (12µs+3µs) by CGI::Compile::ROOT::home_vagrant_kohaclone_mainpage_2epl::BEGIN@32 at line 12 # spent 14µs making 1 call to Koha::Borrower::Discharge::BEGIN@12
# spent 3µs making 1 call to Class::Accessor::import |
13 | 2 | 1.33ms | 2 | 50µs | # spent 30µs (10+20) within Koha::Borrower::Discharge::BEGIN@13 which was called:
# once (10µs+20µs) by CGI::Compile::ROOT::home_vagrant_kohaclone_mainpage_2epl::BEGIN@32 at line 13 # spent 30µs making 1 call to Koha::Borrower::Discharge::BEGIN@13
# spent 20µs making 1 call to Exporter::import |
14 | |||||
15 | 1 | 67µs | 3 | 286µs | my $rs = Koha::Database->new->schema->resultset('Discharge'); # spent 265µs making 1 call to DBIx::Class::Schema::resultset
# spent 13µs making 1 call to Koha::Database::schema
# spent 8µs making 1 call to Class::Accessor::new |
16 | |||||
17 | # spent 3.31ms (21µs+3.29) within Koha::Borrower::Discharge::count which was called:
# once (21µs+3.29ms) by CGI::Compile::ROOT::home_vagrant_kohaclone_mainpage_2epl::__ANON__[/home/vagrant/kohaclone/mainpage.pl:107] at line 70 of mainpage.pl | ||||
18 | 1 | 700ns | my ($params) = @_; | ||
19 | 1 | 600ns | my $values = {}; | ||
20 | |||||
21 | 1 | 1µs | if( $params->{borrowernumber} ) { | ||
22 | $values->{borrower} = $params->{borrowernumber}; | ||||
23 | } | ||||
24 | 1 | 600ns | if( $params->{pending} ) { | ||
25 | 1 | 2µs | $values->{needed} = { '!=', undef }; | ||
26 | 1 | 800ns | $values->{validated} = undef; | ||
27 | } | ||||
28 | elsif( $params->{validated} ) { | ||||
29 | $values->{validated} = { '!=', undef }; | ||||
30 | } | ||||
31 | |||||
32 | 1 | 28µs | 3 | 3.41ms | return $rs->search( $values )->count; # spent 3.15ms making 1 call to DBIx::Class::ResultSet::count
# spent 143µs making 1 call to DBIx::Class::ResultSet::search
# spent 113µs making 1 call to DBIx::Class::Storage::DBI::Cursor::DESTROY |
33 | } | ||||
34 | |||||
35 | sub can_be_discharged { | ||||
36 | my ($params) = @_; | ||||
37 | return unless $params->{borrowernumber}; | ||||
38 | |||||
39 | my $issues = GetPendingIssues( $params->{borrowernumber} ); | ||||
40 | if( @$issues ) { | ||||
41 | return 0; | ||||
42 | } | ||||
43 | else { | ||||
44 | return 1; | ||||
45 | } | ||||
46 | } | ||||
47 | |||||
48 | sub is_discharged { | ||||
49 | my ($params) = @_; | ||||
50 | return unless $params->{borrowernumber}; | ||||
51 | my $borrowernumber = $params->{borrowernumber}; | ||||
52 | |||||
53 | |||||
54 | my $restricted = Koha::Borrower::Debarments::IsDebarred($borrowernumber); | ||||
55 | my $validated = get_validated({borrowernumber => $borrowernumber}); | ||||
56 | |||||
57 | if ($restricted && $validated) { | ||||
58 | return 1; | ||||
59 | } else { | ||||
60 | return 0; | ||||
61 | } | ||||
62 | } | ||||
63 | |||||
64 | sub request { | ||||
65 | my ($params) = @_; | ||||
66 | my $borrowernumber = $params->{borrowernumber}; | ||||
67 | return unless $borrowernumber; | ||||
68 | return unless can_be_discharged({ borrowernumber => $borrowernumber }); | ||||
69 | |||||
70 | return $rs->create({ | ||||
71 | borrower => $borrowernumber, | ||||
72 | needed => dt_from_string, | ||||
73 | }); | ||||
74 | } | ||||
75 | |||||
76 | sub discharge { | ||||
77 | my ($params) = @_; | ||||
78 | my $borrowernumber = $params->{borrowernumber}; | ||||
79 | return unless $borrowernumber and can_be_discharged( { borrowernumber => $borrowernumber } ); | ||||
80 | |||||
81 | # Cancel reserves | ||||
82 | my @reserves = GetReservesFromBorrowernumber($borrowernumber); | ||||
83 | for my $reserve (@reserves) { | ||||
84 | CancelReserve( { reserve_id => $reserve->{reserve_id} } ); | ||||
85 | } | ||||
86 | |||||
87 | # Debar the member | ||||
88 | Koha::Borrower::Debarments::AddDebarment({ | ||||
89 | borrowernumber => $borrowernumber, | ||||
90 | type => 'DISCHARGE', | ||||
91 | }); | ||||
92 | |||||
93 | # Generate the discharge | ||||
94 | my $discharge = $rs->search({ borrower => $borrowernumber }, { order_by => { -desc => 'needed' }, rows => 1 }); | ||||
95 | if( $discharge->count > 0 ) { | ||||
96 | $discharge->update({ validated => dt_from_string }); | ||||
97 | } | ||||
98 | else { | ||||
99 | $rs->create({ | ||||
100 | borrower => $borrowernumber, | ||||
101 | validated => dt_from_string, | ||||
102 | }); | ||||
103 | } | ||||
104 | } | ||||
105 | |||||
106 | sub generate_as_pdf { | ||||
107 | my ($params) = @_; | ||||
108 | return unless $params->{borrowernumber}; | ||||
109 | |||||
110 | my $letter = C4::Letters::GetPreparedLetter( | ||||
111 | module => 'members', | ||||
112 | letter_code => 'DISCHARGE', | ||||
113 | tables => { borrowers => $params->{borrowernumber}, branches => $params->{'branchcode'}, }, | ||||
114 | ); | ||||
115 | |||||
116 | my $today = output_pref( dt_from_string() ); | ||||
117 | $letter->{'title'} =~ s/<<today>>/$today/g; | ||||
118 | $letter->{'content'} =~ s/<<today>>/$today/g; | ||||
119 | |||||
120 | my $tmpl = C4::Templates::gettemplate('batch/print-notices.tt', 'intranet', new CGI); | ||||
121 | $tmpl->param( | ||||
122 | stylesheet => C4::Context->preference("NoticeCSS"), | ||||
123 | today => $today, | ||||
124 | messages => [$letter], | ||||
125 | ); | ||||
126 | |||||
127 | my $html_path = tmpnam() . '.html'; | ||||
128 | my $pdf_path = tmpnam() . '.pdf'; | ||||
129 | my $html_content = $tmpl->output; | ||||
130 | open my $html_fh, '>:encoding(utf8)', $html_path; | ||||
131 | say $html_fh $html_content; | ||||
132 | close $html_fh; | ||||
133 | my $output = eval { require PDF::FromHTML; return; } || $@; | ||||
134 | if ($output && $params->{testing}) { | ||||
135 | carp $output; | ||||
136 | $pdf_path = undef; | ||||
137 | } | ||||
138 | elsif ($output) { | ||||
139 | die $output; | ||||
140 | } | ||||
141 | else { | ||||
142 | my $pdf = PDF::FromHTML->new( encoding => 'utf-8' ); | ||||
143 | $pdf->load_file( $html_path ); | ||||
144 | $pdf->convert; | ||||
145 | $pdf->write_file( $pdf_path ); | ||||
146 | } | ||||
147 | |||||
148 | return $pdf_path; | ||||
149 | } | ||||
150 | |||||
151 | sub get_pendings { | ||||
152 | my ($params) = @_; | ||||
153 | my $branchcode = $params->{branchcode}; | ||||
154 | my $borrowernumber = $params->{borrowernumber}; | ||||
155 | |||||
156 | my $cond = { | ||||
157 | 'me.needed' => { '!=', undef }, | ||||
158 | 'me.validated' => undef, | ||||
159 | ( defined $borrowernumber ? ( 'me.borrower' => $borrowernumber ) : () ), | ||||
160 | ( defined $branchcode ? ( 'borrower.branchcode' => $branchcode ) : () ), | ||||
161 | }; | ||||
162 | |||||
163 | my @rs = $rs->search( $cond, { join => 'borrower' } ); | ||||
164 | return \@rs; | ||||
165 | } | ||||
166 | |||||
167 | sub get_validated { | ||||
168 | my ($params) = @_; | ||||
169 | my $branchcode = $params->{branchcode}; | ||||
170 | my $borrowernumber = $params->{borrowernumber}; | ||||
171 | |||||
172 | my $cond = { | ||||
173 | 'me.validated' => { '!=', undef }, | ||||
174 | ( defined $borrowernumber ? ( 'me.borrower' => $borrowernumber ) : () ), | ||||
175 | ( defined $branchcode ? ( 'borrower.branchcode' => $branchcode ) : () ), | ||||
176 | }; | ||||
177 | |||||
178 | my @rs = $rs->search( $cond, { join => 'borrower' } ); | ||||
179 | return \@rs; | ||||
180 | } | ||||
181 | |||||
182 | |||||
183 | 1 | 4µs | 1; |