| 1 | package Koha::Contrib::Tamil::Claimer; | ||
| 2 | $Koha::Contrib::Tamil::Claimer::VERSION = '0.074'; | ||
| 3 | # ABSTRACT: Claim overdues | ||
| 4 | use Moose; | ||
| 5 | |||
| 6 | use feature ":5.10"; | ||
| 7 | use YAML; | ||
| 8 | use C4::Context; | ||
| 9 | use C4::Letters; | ||
| 10 | |||
| 11 | |||
| 12 | has rule => ( is => 'rw', isa => 'HashRef' ); | ||
| 13 | |||
| 14 | |||
| 15 | # Is the process effective. If not, output the result. | ||
| 16 | has doit => ( is => 'rw', isa => 'Bool', default => 0 ); | ||
| 17 | |||
| 18 | # To who sent the notification, rather that the borrower email | ||
| 19 | has to => ( is => 'rw', isa => 'Str', default => '' ); | ||
| 20 | |||
| 21 | |||
| 22 | |||
| 23 | sub BUILD { | ||
| 24 | my $self = shift; | ||
| 25 | |||
| 26 | my $claim_string = C4::Context->preference("ClaimRules"); | ||
| 27 | unless ($claim_string) { | ||
| 28 | say "ClaimRules system preference not found"; | ||
| 29 | exit; | ||
| 30 | } | ||
| 31 | |||
| 32 | my $rule = {}; | ||
| 33 | for ( split /\n/, $claim_string ) { | ||
| 34 | next if $_ eq '' || /^#/; | ||
| 35 | my ($branch, $itype, $day, $action, $notice) = split; | ||
| 36 | unless ($branch && $itype && $day && $action && $notice) { | ||
| 37 | say "Bad rule: $_"; | ||
| 38 | next; | ||
| 39 | } | ||
| 40 | my $branchcode = $branch eq '*' ? '' : $branch; | ||
| 41 | $rule->{"$branch-$itype-$day"} = { | ||
| 42 | action => $action, | ||
| 43 | notice => $notice, | ||
| 44 | letter => C4::Letters::getletter('circulation', $notice, $branchcode), | ||
| 45 | }; | ||
| 46 | } | ||
| 47 | unless ( keys %$rule ) { | ||
| 48 | say "There isn't any valid rule in ClaimRules system preference"; | ||
| 49 | exit; | ||
| 50 | } | ||
| 51 | $self->rule($rule); | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | # Substitute letter placeholder with fields values | ||
| 56 | sub substitute_placeholder { | ||
| 57 | my ($content, $record) = @_; # $record contient le prĂȘt | ||
| 58 | while ( 1 ) { | ||
| 59 | last unless $content =~ /<<(.*?)>>/; | ||
| 60 | my $name = $1; | ||
| 61 | if ( exists $record->{$name} ) { | ||
| 62 | my $value = $record->{$name}; | ||
| 63 | $content =~ s/<<$name>>/$value/; | ||
| 64 | next; | ||
| 65 | } | ||
| 66 | if ( $name =~ /.*\.(.*)/ ) { | ||
| 67 | my $short_name = $1; | ||
| 68 | if ( exists $record->{$short_name} ) { | ||
| 69 | my $value = $record->{$short_name} || ''; | ||
| 70 | $value = substr($value, 0, 10) | ||
| 71 | if $short_name eq 'date_due' && | ||
| 72 | $value =~ /23:59:00/; | ||
| 73 | $content =~ s/<<$name>>/$value/; | ||
| 74 | next; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | $content =~ s/<<$name>>/unknown $name/; | ||
| 78 | } | ||
| 79 | return $content; | ||
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | # | ||
| 84 | # Claim a specific group of issues | ||
| 85 | # | ||
| 86 | sub claim { | ||
| 87 | my ($self, $borrowernumber, $items, $letter) = @_; | ||
| 88 | |||
| 89 | # On the claim, branch info come from borrower home branch | ||
| 90 | my $dbh = C4::Context->dbh(); | ||
| 91 | my $sth = $dbh->prepare(" | ||
| 92 | SELECT borrowers.*, branches.*, categories.* | ||
| 93 | FROM borrowers, branches, categories | ||
| 94 | WHERE borrowers.borrowernumber = ? | ||
| 95 | AND categories.categorycode = borrowers.categorycode | ||
| 96 | AND branches.branchcode = borrowers.branchcode | ||
| 97 | "); | ||
| 98 | $sth->execute($borrowernumber); | ||
| 99 | my $borr = $sth->fetchrow_hashref; | ||
| 100 | |||
| 101 | # Skip issue from borrower of specific category | ||
| 102 | return unless $borr->{overduenoticerequired}; | ||
| 103 | |||
| 104 | my $sql = " | ||
| 105 | SELECT items.*, | ||
| 106 | itemtypes.*, | ||
| 107 | branches.branchname AS item_branch, | ||
| 108 | biblio.*, | ||
| 109 | biblioitems.*, | ||
| 110 | issues.date_due, | ||
| 111 | issues.issuedate, | ||
| 112 | TO_DAYS(NOW())-TO_DAYS(date_due) AS overdue_days | ||
| 113 | FROM issues, items, itemtypes, branches, biblio, biblioitems | ||
| 114 | WHERE issues.itemnumber IN (" . join(',', @$items) . ") | ||
| 115 | AND items.itemnumber = issues.itemnumber | ||
| 116 | AND itemtypes.itemtype = items.itype | ||
| 117 | AND branches.branchcode = items.holdingbranch | ||
| 118 | AND biblio.biblionumber = items.biblionumber | ||
| 119 | AND biblioitems.biblionumber = items.biblionumber"; | ||
| 120 | $sth = $dbh->prepare($sql); | ||
| 121 | $sth->execute; | ||
| 122 | my %let = %$letter; | ||
| 123 | my $content = $let{content}; | ||
| 124 | my ($icontent) = $content =~ /<items>(.*)<\/items>/s; | ||
| 125 | die "No placeholder <items></items> in " . $letter->{code} | ||
| 126 | unless $icontent; | ||
| 127 | my $buffer = ''; | ||
| 128 | while ( my $issue = $sth->fetchrow_hashref ) { | ||
| 129 | my $ibuffer = $icontent; | ||
| 130 | $buffer .= substitute_placeholder($ibuffer, $issue); | ||
| 131 | } | ||
| 132 | $content =~ s/(.*)<items>.*<\/items>(.*)$/$1$buffer$2/s; | ||
| 133 | |||
| 134 | # Substitute borrowers placeholder | ||
| 135 | $content = substitute_placeholder( $content, $borr ); | ||
| 136 | |||
| 137 | $let{content} = $content; | ||
| 138 | if ( $self->doit ) { | ||
| 139 | my $to_address = $self->to || $borr->{email}; | ||
| 140 | C4::Letters::EnqueueLetter( { | ||
| 141 | letter => \%let, | ||
| 142 | borrowernumber => $borrowernumber, | ||
| 143 | message_transport_type => 'email', | ||
| 144 | to_address => $to_address, | ||
| 145 | from_address => $borr->{branchemail}, | ||
| 146 | } ); | ||
| 147 | } | ||
| 148 | else { | ||
| 149 | print "CLAIM TO: ", $borr->{firstname}, " ", $borr->{surname}, "\n", | ||
| 150 | "Letter: ", $let{code}, " - ", $let{name}, "\n", | ||
| 151 | '-' x 72, "\n", | ||
| 152 | $content, "\n\n"; | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | |||
| 157 | sub handle_borrower { | ||
| 158 | my ($self, $borrower) = @_; | ||
| 159 | return unless $borrower->{borrowernumber}; | ||
| 160 | |||
| 161 | while ( my ($action, $action_href) = each %{$borrower->{action}} ) { | ||
| 162 | while ( my ($notice, $notice_href) = each %$action_href ) { | ||
| 163 | if ( $action == 1 ) { | ||
| 164 | $self->claim( | ||
| 165 | $borrower->{borrowernumber}, | ||
| 166 | $notice_href->{items}, $notice_href->{letter} ); | ||
| 167 | } | ||
| 168 | elsif ( $action == 2 ) { | ||
| 169 | # Debarre | ||
| 170 | } | ||
| 171 | } | ||
| 172 | } | ||
| 173 | } | ||
| 174 | |||
| 175 | |||
| 176 | sub claim_all_overdues { | ||
| 177 | my $self = shift; | ||
| 178 | my $dbh = C4::Context->dbh(); | ||
| 179 | my $sth = $dbh->prepare(" | ||
| 180 | SELECT borrowers.borrowernumber, | ||
| 181 | issues.itemnumber, | ||
| 182 | issues.branchcode, | ||
| 183 | itype, | ||
| 184 | TO_DAYS(NOW())-TO_DAYS(date_due) AS day | ||
| 185 | FROM issues, borrowers, items | ||
| 186 | WHERE date_due < NOW() | ||
| 187 | AND borrowers.borrowernumber = issues.borrowernumber | ||
| 188 | AND items.itemnumber = issues.itemnumber | ||
| 189 | ORDER BY borrowers.borrowernumber | ||
| 190 | "); | ||
| 191 | $sth->execute; | ||
| 192 | my $borrower = { borrowernumber => 0 }; | ||
| 193 | while ( my ($borrowernumber, $itemnumber, $branch, $itype, $day) | ||
| 194 | = $sth->fetchrow ) | ||
| 195 | { | ||
| 196 | $itype ||= ''; | ||
| 197 | #say "borrowernumber = $borrowernumber"; | ||
| 198 | #say "day = $day"; | ||
| 199 | my $rule = $self->rule->{"$branch-$itype-$day"} || | ||
| 200 | $self->rule->{"*-$itype-$day"} || | ||
| 201 | $self->rule->{"$branch-*-$day"} || | ||
| 202 | $self->rule->{"*-*-$day"}; | ||
| 203 | next unless $rule; | ||
| 204 | if ( $borrowernumber != $borrower->{borrowernumber} ) { | ||
| 205 | $self->handle_borrower( $borrower ); | ||
| 206 | $borrower = { borrowernumber => $borrowernumber, action => {} }; | ||
| 207 | } | ||
| 208 | #say "rule = ", Dump($rule); | ||
| 209 | my $action = $borrower->{action}->{$rule->{action}} ||= {}; | ||
| 210 | my $notice = $action->{$rule->{notice}} | ||
| 211 | ||= { letter => $rule->{letter}, items => [] }; | ||
| 212 | my $items = $notice->{items}; | ||
| 213 | push @$items, $itemnumber; | ||
| 214 | } | ||
| 215 | $self->handle_borrower( $borrower ); | ||
| 216 | } | ||
| 217 | |||
| 218 | __PACKAGE__->meta->make_immutable; | ||
| 219 | 1; | ||
| 220 | |||
| 221 | __END__ |