Project

General

Profile

Actions

Feature #442

closed

Koha notices & slips test script

Added by Lari Taskula about 3 years ago. Updated about 3 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Target version:
-
Start date:
17.02.2021
Due date:
% Done:

100%

Estimated time:
Spent time:
Sponsored-by:
Koha Bugzilla Bug#:

Description

Save this code into a slip_tester.pl file, edit lettercode and mtt to your liking, then run perl slip_tester.pl

use Modern::Perl;

use POSIX qw/strftime/;
use Encode;
use C4::Context;
use C4::Letters;
use Koha::DateUtils;
use Koha::Biblios;
use Koha::Checkouts;
use Koha::Patrons;

### EDIT THESE TO YOUR LIKING ###
my $lettercode = 'ODUE'; # CHECKOUT CHECKIN DUE ODUE PREDUE
my $mtt = 'email';

### IF YOU WANT TO TARGET SOME SPECIFIC PATRONS OR BIBLIOS, EDIT THESE ###
### OTHERWISE I WILL PICK SOME RANDOM VALUES FOR YOU ###
my $issue = Koha::Checkouts->search->next;
my $patron = Koha::Patrons->search({ borrowernumber => $issue->borrowernumber })->next;
my $borrowernumber = $patron->borrowernumber;
my $itemnumber = $issue->itemnumber;
my $biblionumber = Koha::Items->find($issue->itemnumber)->biblionumber;
my $branchcode = $patron->branchcode;

### STOP EDITING ####
my $dbh = C4::Context->dbh();
my $sth = $dbh->prepare(<<'END_SQL');
SELECT biblio.*, items.*
  FROM items,biblio,issues
  WHERE biblio.biblionumber=items.biblionumber
    AND biblio.biblionumber = ?
END_SQL

my $itemscontent = join(',',qw( date_due title author barcode ));
my @item_content_fields = split(/,/,$itemscontent);
my $titles = "";
$sth->execute($biblionumber);
 while ( my $item_info = $sth->fetchrow_hashref()) {
      my @item_info = map { $_ =~ /^date|date$/ ? format_date($item_info->{$_}) : $item_info->{$_} || '' } @item_content_fields;
      $titles .= join("\t",@item_info) . "\n";
    }
my %branch_info = get_branch_info( $borrowernumber );
my $letter;

$letter = parse_letter(
            {
                letter_code    => $lettercode,
                borrowernumber => $borrowernumber,
                biblionumber => $biblionumber,
                itemnumber => $itemnumber,
                substitute     => {
                    count           => 5,
                    'items.content' => $titles,
                    %branch_info,
                },
                branchcode             => $branchcode,
                message_transport_type => $mtt,
            }
          );

print Encode::encode("utf-8", $letter->{content}."\n");
sub parse_letter {
    my $params = shift;
    foreach my $required ( qw( letter_code borrowernumber ) ) {
        return unless exists $params->{$required};
    }
    my $patron = Koha::Patrons->find( $params->{borrowernumber} );

    my %table_params = ( 'borrowers' => $params->{'borrowernumber'} );

    if ( my $p = $params->{'branchcode'} ) {
        $table_params{'branches'} = $p;
    }
    if ( my $p = $params->{'itemnumber'} ) {
        $table_params{'issues'} = $p;
        $table_params{'items'} = $p;
    }
    if ( my $p = $params->{'biblionumber'} ) {
        $table_params{'biblio'} = $p;
        $table_params{'biblioitems'} = $p;
    }

    return C4::Letters::GetPreparedLetter (
        module => 'circulation',
        letter_code => $params->{'letter_code'},
        branchcode => $table_params{'branches'},
        lang => $patron->lang,
        substitute => $params->{'substitute'},
        tables     => \%table_params,
        message_transport_type => $params->{message_transport_type},
    );
}

sub get_branch_info {
    my ( $borrowernumber ) = @_;

    ## Get branch info for borrowers home library.
    my $borrower_details = C4::Members::GetMember( borrowernumber => $borrowernumber );
    my $borrower_branchcode = $borrower_details->{'branchcode'};
    my $branch = Koha::Libraries->find( $borrower_branchcode )->unblessed;
    my %branch_info;
    foreach my $key( keys %$branch ) {
        $branch_info{"branches.$key"} = $branch->{$key};
    }

    return %branch_info;
}

sub format_date {
    my $date_string = shift;
    my $dt=dt_from_string($date_string);
    return output_pref($dt);
}

Actions #1

Updated by Lari Taskula about 3 years ago

  • Status changed from New to Resolved
  • % Done changed from 0 to 100
Actions #2

Updated by Lari Taskula about 3 years ago

  • Description updated (diff)
Actions #3

Updated by Lari Taskula about 3 years ago

  • Status changed from Resolved to Closed
Actions #4

Updated by Lari Taskula about 3 years ago

  • Description updated (diff)
Actions

Also available in: Atom PDF