View | Details | Raw Unified | Return to bug 18796
Collapse All | Expand All

(-)a/C4/Letters.pm (+79 lines)
Lines 23-28 use MIME::Lite; Link Here
23
use Carp qw( carp croak );
23
use Carp qw( carp croak );
24
use Template;
24
use Template;
25
use Module::Load::Conditional qw( can_load );
25
use Module::Load::Conditional qw( can_load );
26
use PDF::FromHTML;
26
27
27
use Try::Tiny qw( catch try );
28
use Try::Tiny qw( catch try );
28
29
Lines 40-45 use Koha::DateUtils qw( dt_from_string output_pref ); Link Here
40
use Koha::Patrons;
41
use Koha::Patrons;
41
use Koha::SMTP::Servers;
42
use Koha::SMTP::Servers;
42
use Koha::Subscriptions;
43
use Koha::Subscriptions;
44
use Koha::Acquisition::Booksellers;
43
45
44
our (@ISA, @EXPORT_OK);
46
our (@ISA, @EXPORT_OK);
45
BEGIN {
47
BEGIN {
Lines 583-588 sub SendAlerts { Link Here
583
    return 1;
585
    return 1;
584
}
586
}
585
587
588
=head2 PrintClaimIssueNotice
589
590
Returns a 'claimissues' notice for given serial ids and letter code as PDF
591
592
    my $pdf = PrintClaimIssueNotice(\@serialids, $letter_code);
593
594
    print $cgi->header('application/pdf');
595
    print $pdf;
596
597
598
Returns undef if:
599
600
=over
601
602
=item * a parameter is missing
603
604
=item * C<@serialids> is empty
605
606
=item * C<@serialids> does not contain any existing serial id
607
608
=item * C<$letter_code> does not exist
609
610
=back
611
612
=cut
613
614
sub PrintClaimIssueNotice {
615
    my ($serialids, $letter_code) = @_;
616
617
    return unless ref $serialids eq 'ARRAY';
618
    return unless @$serialids > 0;
619
    return unless $letter_code;
620
621
    my $dbh = C4::Context->dbh;
622
623
    my $serialids_str = join(',', ('?') x @$serialids);
624
    my $serials = $dbh->selectall_arrayref(qq{
625
        SELECT subscription.*, biblio.*, serial.*
626
        FROM serial
627
        LEFT JOIN subscription ON serial.subscriptionid = subscription.subscriptionid
628
        LEFT JOIN biblio ON serial.biblionumber = biblio.biblionumber
629
        WHERE serial.serialid IN ($serialids_str)
630
    }, {Slice => {}}, @$serialids);
631
632
    return unless @$serials;
633
634
    my $booksellerid = $serials->[0]->{aqbooksellerid};
635
    my $bookseller = Koha::Acquisition::Booksellers->find($booksellerid);
636
637
    my $userenv = C4::Context->userenv;
638
    my $letter = GetPreparedLetter(
639
        module => 'claimissues',
640
        letter_code => $letter_code,
641
        branchcode => $userenv->{branch},
642
        message_transport_type => 'print',
643
        tables => {
644
            'branches'    => $userenv->{branch},
645
            'aqbooksellers' => $bookseller->unblessed,
646
        },
647
        repeat => $serials,
648
        want_librarian => 1,
649
    ) or return;
650
651
    my $content = $letter->{content};
652
    if ($letter->{is_html}) {
653
        $content = _wrap_html($content, $letter->{title});
654
    }
655
656
    my $output = '';
657
    my $pdf = PDF::FromHTML->new(encoding => 'utf-8');
658
    $pdf->load_file(\$content);
659
    $pdf->convert();
660
    $pdf->write_file(\$output);
661
662
    return $output;
663
}
664
586
=head2 GetPreparedLetter( %params )
665
=head2 GetPreparedLetter( %params )
587
666
588
    %params hash:
667
    %params hash:
(-)a/cpanfile (+1 lines)
Lines 133-138 recommends 'AnyEvent', '5.0'; Link Here
133
recommends 'AnyEvent::HTTP', '2.13';
133
recommends 'AnyEvent::HTTP', '2.13';
134
recommends 'Archive::Extract', '0.60';
134
recommends 'Archive::Extract', '0.60';
135
recommends 'Archive::Zip', '1.30';
135
recommends 'Archive::Zip', '1.30';
136
recommends 'CAM::PDF', '1.60';
136
recommends 'CGI::Session::Driver::memcached', '0.04';
137
recommends 'CGI::Session::Driver::memcached', '0.04';
137
recommends 'DBD::SQLite2', '0.33';
138
recommends 'DBD::SQLite2', '0.33';
138
recommends 'Devel::Cover', '0.89';
139
recommends 'Devel::Cover', '0.89';
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/claims.tt (-1 / +24 lines)
Lines 206-212 Link Here
206
                    </select>
206
                    </select>
207
                    <input type="hidden" name="op" value="send_alert" />
207
                    <input type="hidden" name="op" value="send_alert" />
208
                    <input type="hidden" name="supplierid" value="[% supplierid | html %]" />
208
                    <input type="hidden" name="supplierid" value="[% supplierid | html %]" />
209
                    <input type="submit" name="submit" class="button" value="Send notification" />
209
                    <div class="btn-group">
210
                        <button type="submit" class="btn btn-default btn-xs">Send notification</button>
211
                        <button type="button" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
212
                            <span class="caret"></span>
213
                            <span class="sr-only">Toggle dropdown</span>
214
                        </button>
215
                        <ul class="dropdown-menu">
216
                            <li><a id="print-notification" href="#">Print notification</a></li>
217
                        </ul>
218
                    </div>
210
                </fieldset>
219
                </fieldset>
211
            [% END %]
220
            [% END %]
212
        </form>
221
        </form>
Lines 337-342 Link Here
337
            });
346
            });
338
        }
347
        }
339
    </script>
348
    </script>
349
350
    <script>
351
        $(document).ready(function() {
352
            $('#print-notification').on('click', function(e) {
353
                e.preventDefault();
354
                var form = $(this).parents('form')
355
                var op = form.find('input[name="op"]');
356
357
                op.val('print_alert');
358
                form.submit();
359
                op.val('send_alert');
360
            });
361
        });
362
    </script>
340
[% END %]
363
[% END %]
341
364
342
[% INCLUDE 'intranet-bottom.inc' %]
365
[% INCLUDE 'intranet-bottom.inc' %]
(-)a/serials/claims.pl (-17 / +30 lines)
Lines 55-79 for my $s (@{$supplierlist} ) { Link Here
55
55
56
my $additional_fields = Koha::AdditionalFields->search( { tablename => 'subscription', searchable => 1 } );
56
my $additional_fields = Koha::AdditionalFields->search( { tablename => 'subscription', searchable => 1 } );
57
57
58
my @serialnums=$input->multi_param('serialid');
58
my @serialnums = $input->multi_param('serialid');
59
if (@serialnums) { # i.e. they have been flagged to generate claims
59
if (@serialnums) { # i.e. they have been flagged to generate claims
60
    my $err;
60
    my $op = $input->param('op');
61
    eval {
61
    my $letter_code = $input->param('letter_code');
62
        $err = SendAlerts('claimissues',\@serialnums,$input->param("letter_code"));
62
63
        if ( not ref $err or not exists $err->{error} ) {
63
    if ($op eq 'print_alert') {
64
            C4::Serials::updateClaim( \@serialnums );
64
        my $pdf = C4::Letters::PrintClaimIssueNotice(\@serialnums, $letter_code);
65
        }
65
        print $input->header(
66
    };
66
            -type => 'application/pdf',
67
    if ( $@ ) {
67
            -attachment => 'claims.pdf',
68
        $template->param(error_claim => $@);
68
        );
69
    } elsif ( ref $err and exists $err->{error} ) {
69
        print $pdf;
70
        if ( $err->{error} eq "no_email" ) {
70
        exit;
71
            $template->param( error_claim => 'no_vendor_email' );
72
        } elsif ( $err->{error} =~ m|Bad or missing From address| ) {
73
            $template->param( error_claim => 'bad_or_missing_sender' );
74
        }
75
    } else {
71
    } else {
76
        $template->param( info_claim => 1 );
72
        my $err;
73
        eval {
74
            $err = SendAlerts('claimissues',\@serialnums, $letter_code);
75
            if ( not ref $err or not exists $err->{error} ) {
76
                C4::Serials::updateClaim( \@serialnums );
77
            }
78
        };
79
        if ( $@ ) {
80
            $template->param(error_claim => $@);
81
        } elsif ( ref $err and exists $err->{error} ) {
82
            if ( $err->{error} eq "no_email" ) {
83
                $template->param( error_claim => 'no_vendor_email' );
84
            } elsif ( $err->{error} =~ m|Bad or missing From address| ) {
85
                $template->param( error_claim => 'bad_or_missing_sender' );
86
            }
87
        } else {
88
            $template->param( info_claim => 1 );
89
        }
77
    }
90
    }
78
}
91
}
79
92
(-)a/t/db_dependent/Letters.t (-2 / +41 lines)
Lines 18-24 Link Here
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
use Test::More tests => 83;
21
use Test::More tests => 84;
22
use Test::MockModule;
22
use Test::MockModule;
23
use Test::Warn;
23
use Test::Warn;
24
use Test::Exception;
24
use Test::Exception;
Lines 52-57 use t::lib::TestBuilder; Link Here
52
use Koha::Database;
52
use Koha::Database;
53
use Koha::DateUtils qw( dt_from_string output_pref );
53
use Koha::DateUtils qw( dt_from_string output_pref );
54
use Koha::Acquisition::Booksellers;
54
use Koha::Acquisition::Booksellers;
55
use Koha::Acquisition::Bookseller;
55
use Koha::Acquisition::Bookseller::Contacts;
56
use Koha::Acquisition::Bookseller::Contacts;
56
use Koha::Acquisition::Orders;
57
use Koha::Acquisition::Orders;
57
use Koha::Libraries;
58
use Koha::Libraries;
Lines 970-972 subtest 'Test message_id parameter for SendQueuedMessages' => sub { Link Here
970
    is( $message_1->{status}, 'pending', 'Message 1 status is unchanged' ); # Must be 'failed'
971
    is( $message_1->{status}, 'pending', 'Message 1 status is unchanged' ); # Must be 'failed'
971
    is( $message_2->{status}, 'sent', 'Valid KohaAdminEmailAddress => status sent' );
972
    is( $message_2->{status}, 'sent', 'Valid KohaAdminEmailAddress => status sent' );
972
};
973
};
973
- 
974
975
subtest 'PrintClaimIssueNotice' => sub {
976
    plan tests => 6;
977
978
    my $bookseller = Koha::Acquisition::Bookseller->new({ name => 'Bookie' })->store();
979
    my $subscriptionid = NewSubscription(undef, "", $bookseller->id, 0, undef,
980
        $biblionumber, '2013-01-01', 1, undef, undef, undef, undef, undef,
981
        undef, undef, undef, undef, 1, undef, undef, '2013-01-01', undef, 1,
982
        undef, undef, 0, undef, 0, undef, undef, 0, undef, '2013-12-31', 0);
983
    my ($serials_count, @serials) = GetSerials($subscriptionid);
984
985
    my $letter = 'CLAIMSERIAL';
986
    my $content = 'Date:<<subscription.firstacquidate>>';
987
    $dbh->do(qq{
988
        INSERT INTO letter
989
        (module, code, name, is_html, title, content, message_transport_type)
990
        VALUES
991
        ('claimissues', '$letter', 'Claim', 1, 'Claim', '$content', 'print')
992
    });
993
994
    my @serialids = map { $_->{serialid} } @serials;
995
    is(C4::Letters::PrintClaimIssueNotice(), undef, 'returns undef with no args');
996
    is(C4::Letters::PrintClaimIssueNotice(\@serialids), undef, 'returns undef with no letter code');
997
    is(C4::Letters::PrintClaimIssueNotice(undef, $letter), undef, 'returns undef with no serial ids');
998
    is(C4::Letters::PrintClaimIssueNotice([], $letter), undef, 'returns undef with no serial ids');
999
1000
    my $pdf = C4::Letters::PrintClaimIssueNotice(\@serialids, $letter);
1001
    is(substr($pdf, 0, 4), '%PDF', 'output is a PDF');
1002
1003
    SKIP: {
1004
        eval { require CAM::PDF };
1005
1006
        skip "CAM::PDF is not installed", 1 if $@;
1007
1008
        my $cam = CAM::PDF->new($pdf);
1009
        my $text = $cam->getPageContent(1);
1010
        like($text, qr/Date:2013-01-01/, 'PDF contains Date:2013-01-01');
1011
    }
1012
};

Return to bug 18796