| Filename | /home/vagrant/kohaclone/Koha/Borrower/Discharge.pm |
| Statements | Executed 27 statements in 6.81ms |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 22µs | 73µs | Koha::Borrower::Discharge::BEGIN@6 |
| 1 | 1 | 1 | 21µs | 85µs | Koha::Borrower::Discharge::BEGIN@3 |
| 1 | 1 | 1 | 18µs | 7.31ms | Koha::Borrower::Discharge::count |
| 1 | 1 | 1 | 18µs | 170µs | Koha::Borrower::Discharge::BEGIN@9 |
| 1 | 1 | 1 | 15µs | 26µs | Koha::Borrower::Discharge::BEGIN@8 |
| 1 | 1 | 1 | 14µs | 122µs | Koha::Borrower::Discharge::BEGIN@5 |
| 1 | 1 | 1 | 13µs | 31µs | Koha::Borrower::Discharge::BEGIN@4 |
| 1 | 1 | 1 | 12µs | 42µs | Koha::Borrower::Discharge::BEGIN@10 |
| 1 | 1 | 1 | 10µs | 30µs | Koha::Borrower::Discharge::BEGIN@13 |
| 1 | 1 | 1 | 10µs | 12µs | Koha::Borrower::Discharge::BEGIN@12 |
| 0 | 0 | 0 | 0s | 0s | Koha::Borrower::Discharge::can_be_discharged |
| 0 | 0 | 0 | 0s | 0s | Koha::Borrower::Discharge::discharge |
| 0 | 0 | 0 | 0s | 0s | Koha::Borrower::Discharge::generate_as_pdf |
| 0 | 0 | 0 | 0s | 0s | Koha::Borrower::Discharge::get_pendings |
| 0 | 0 | 0 | 0s | 0s | Koha::Borrower::Discharge::get_validated |
| 0 | 0 | 0 | 0s | 0s | Koha::Borrower::Discharge::is_discharged |
| 0 | 0 | 0 | 0s | 0s | Koha::Borrower::Discharge::request |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | package Koha::Borrower::Discharge; | ||||
| 2 | |||||
| 3 | 2 | 101µs | 2 | 150µs | # spent 85µs (21+64) within Koha::Borrower::Discharge::BEGIN@3 which was called:
# once (21µs+64µs) by CGI::Compile::ROOT::home_vagrant_kohaclone_mainpage_2epl::BEGIN@32 at line 3 # spent 85µs making 1 call to Koha::Borrower::Discharge::BEGIN@3
# spent 64µs making 1 call to Modern::Perl::import |
| 4 | 2 | 59µs | 2 | 50µs | # spent 31µs (13+19) within Koha::Borrower::Discharge::BEGIN@4 which was called:
# once (13µs+19µs) by CGI::Compile::ROOT::home_vagrant_kohaclone_mainpage_2epl::BEGIN@32 at line 4 # spent 31µs making 1 call to Koha::Borrower::Discharge::BEGIN@4
# spent 19µs making 1 call to CGI::import |
| 5 | 2 | 5.04ms | 2 | 230µs | # spent 122µs (14+108) within Koha::Borrower::Discharge::BEGIN@5 which was called:
# once (14µs+108µs) by CGI::Compile::ROOT::home_vagrant_kohaclone_mainpage_2epl::BEGIN@32 at line 5 # spent 122µs making 1 call to Koha::Borrower::Discharge::BEGIN@5
# spent 108µs making 1 call to Exporter::import |
| 6 | 2 | 108µs | 2 | 123µs | # spent 73µs (22+50) within Koha::Borrower::Discharge::BEGIN@6 which was called:
# once (22µs+50µs) by CGI::Compile::ROOT::home_vagrant_kohaclone_mainpage_2epl::BEGIN@32 at line 6 # spent 73µs making 1 call to Koha::Borrower::Discharge::BEGIN@6
# spent 50µs making 1 call to Exporter::import |
| 7 | |||||
| 8 | 2 | 48µs | 2 | 38µs | # spent 26µs (15+12) within Koha::Borrower::Discharge::BEGIN@8 which was called:
# once (15µs+12µs) by CGI::Compile::ROOT::home_vagrant_kohaclone_mainpage_2epl::BEGIN@32 at line 8 # spent 26µs making 1 call to Koha::Borrower::Discharge::BEGIN@8
# spent 12µs making 1 call to Class::Accessor::import |
| 9 | 2 | 201µs | 2 | 322µs | # spent 170µs (18+152) within Koha::Borrower::Discharge::BEGIN@9 which was called:
# once (18µs+152µs) by CGI::Compile::ROOT::home_vagrant_kohaclone_mainpage_2epl::BEGIN@32 at line 9 # spent 170µs making 1 call to Koha::Borrower::Discharge::BEGIN@9
# spent 152µs making 1 call to Exporter::import |
| 10 | 2 | 67µs | 2 | 72µs | # spent 42µs (12+30) within Koha::Borrower::Discharge::BEGIN@10 which was called:
# once (12µs+30µs) by CGI::Compile::ROOT::home_vagrant_kohaclone_mainpage_2epl::BEGIN@32 at line 10 # spent 42µs making 1 call to Koha::Borrower::Discharge::BEGIN@10
# spent 30µs making 1 call to Exporter::import |
| 11 | |||||
| 12 | 2 | 34µs | 2 | 15µs | # spent 12µs (10+3) within Koha::Borrower::Discharge::BEGIN@12 which was called:
# once (10µs+3µs) by CGI::Compile::ROOT::home_vagrant_kohaclone_mainpage_2epl::BEGIN@32 at line 12 # spent 12µs making 1 call to Koha::Borrower::Discharge::BEGIN@12
# spent 3µs making 1 call to Class::Accessor::import |
| 13 | 2 | 1.09ms | 2 | 49µs | # spent 30µs (10+19) within Koha::Borrower::Discharge::BEGIN@13 which was called:
# once (10µs+19µ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 19µs making 1 call to Exporter::import |
| 14 | |||||
| 15 | 1 | 34µs | 3 | 302µs | my $rs = Koha::Database->new->schema->resultset('Discharge'); # spent 289µs making 1 call to DBIx::Class::Schema::resultset
# spent 8µs making 1 call to Class::Accessor::new
# spent 6µs making 1 call to Koha::Database::schema |
| 16 | |||||
| 17 | # spent 7.31ms (18µs+7.29) within Koha::Borrower::Discharge::count which was called:
# once (18µs+7.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 | 600ns | my ($params) = @_; | ||
| 19 | 1 | 500ns | my $values = {}; | ||
| 20 | |||||
| 21 | 1 | 800ns | 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 | 24µs | 3 | 7.35ms | return $rs->search( $values )->count; # spent 7.16ms making 1 call to DBIx::Class::ResultSet::count
# spent 138µs making 1 call to DBIx::Class::ResultSet::search
# spent 62µ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; |