| Line 0
          
      
      
        Link Here | 
          
            
              | 0 | -  | 1 | #!/usr/bin/perl | 
            
              |  |  | 2 |  | 
            
              | 3 | use Modern::Perl; | 
            
              | 4 |  | 
            
              | 5 | use Test::More tests => 3; | 
            
              | 6 | use Test::MockModule; | 
            
              | 7 |  | 
            
              | 8 | use C4::Biblio; | 
            
              | 9 | use C4::Items; | 
            
              | 10 | use C4::Members; | 
            
              | 11 | use C4::Branch; | 
            
              | 12 | use C4::Category; | 
            
              | 13 | use C4::Circulation; | 
            
              | 14 |  | 
            
              | 15 | use Koha::DateUtils qw( dt_from_string output_pref ); | 
            
              | 16 | use DateTime::Duration; | 
            
              | 17 |  | 
            
              | 18 | use MARC::Record; | 
            
              | 19 |  | 
            
              | 20 | my $dbh = C4::Context->dbh; | 
            
              | 21 | $dbh->{AutoCommit} = 0; | 
            
              | 22 | $dbh->{RaiseError} = 1; | 
            
              | 23 |  | 
            
              | 24 | $dbh->do(q|DELETE FROM issues|); | 
            
              | 25 | $dbh->do(q|DELETE FROM borrowers|); | 
            
              | 26 | $dbh->do(q|DELETE FROM items|); | 
            
              | 27 | $dbh->do(q|DELETE FROM branches|); | 
            
              | 28 | $dbh->do(q|DELETE FROM biblio|); | 
            
              | 29 | $dbh->do(q|DELETE FROM categories|); | 
            
              | 30 | $dbh->do(q|DELETE FROM letter|); | 
            
              | 31 |  | 
            
              | 32 | my $branchcode = 'B'; | 
            
              | 33 | ModBranch( { add => 1, branchcode => $branchcode, branchname => 'Branch' } ); | 
            
              | 34 |  | 
            
              | 35 | my $categorycode = 'C'; | 
            
              | 36 | $dbh->do( "INSERT INTO categories(categorycode) VALUES(?)", | 
            
              | 37 |     undef, $categorycode ); | 
            
              | 38 |  | 
            
              | 39 | my %item_branch_infos = ( | 
            
              | 40 |     homebranch    => $branchcode, | 
            
              | 41 |     holdingbranch => $branchcode, | 
            
              | 42 | ); | 
            
              | 43 |  | 
            
              | 44 | my $slip_content = <<EOS; | 
            
              | 45 | Checked out: | 
            
              | 46 | <checkedout> | 
            
              | 47 | Title: <<biblio.title>> | 
            
              | 48 | Barcode: <<items.barcode>> | 
            
              | 49 | Date due: <<issues.date_due>> | 
            
              | 50 | </checkedout> | 
            
              | 51 |  | 
            
              | 52 | Overdues: | 
            
              | 53 | <overdue> | 
            
              | 54 | Title: <<biblio.title>> | 
            
              | 55 | Barcode: <<items.barcode>> | 
            
              | 56 | Date due: <<issues.date_due>> | 
            
              | 57 | </overdue> | 
            
              | 58 | EOS | 
            
              | 59 |  | 
            
              | 60 | $dbh->do(q| | 
            
              | 61 |     INSERT INTO letter( module, code, branchcode, name, is_html, title, content, message_transport_type) VALUES ('circulation', 'ISSUESLIP', '', 'Issue Slip', 0, 'Issue Slip', ?, 'email') | 
            
              | 62 | |, {}, $slip_content); | 
            
              | 63 |  | 
            
              | 64 | my $quick_slip_content = <<EOS; | 
            
              | 65 | Checked out today: | 
            
              | 66 | <checkedout> | 
            
              | 67 | Title: <<biblio.title>> | 
            
              | 68 | Barcode: <<items.barcode>> | 
            
              | 69 | Date due: <<issues.date_due>> | 
            
              | 70 | </checkedout> | 
            
              | 71 | EOS | 
            
              | 72 |  | 
            
              | 73 | $dbh->do(q| | 
            
              | 74 |     INSERT INTO letter( module, code, branchcode, name, is_html, title, content, message_transport_type) VALUES ('circulation', 'ISSUEQSLIP', '', 'Issue Quick Slip', 0, 'Issue Quick Slip', ?, 'email') | 
            
              | 75 | |, {}, $quick_slip_content); | 
            
              | 76 |  | 
            
              | 77 | my ( $title1, $title2 ) = ( 'My title 1', 'My title 2' ); | 
            
              | 78 | my ( $barcode1, $barcode2 ) = ('BC0101', 'BC0202' ); | 
            
              | 79 | my $record = MARC::Record->new; | 
            
              | 80 | $record->append_fields( | 
            
              | 81 |     MARC::Field->new( | 
            
              | 82 |         245, '1', '4', | 
            
              | 83 |             a => $title1, | 
            
              | 84 |     ), | 
            
              | 85 | ); | 
            
              | 86 | my ($biblionumber1) = AddBiblio( $record, '' ); | 
            
              | 87 | my $itemnumber1 = | 
            
              | 88 |   AddItem( { barcode => $barcode1, %item_branch_infos }, $biblionumber1 ); | 
            
              | 89 |  | 
            
              | 90 | $record = MARC::Record->new; | 
            
              | 91 | $record->append_fields( | 
            
              | 92 |     MARC::Field->new( | 
            
              | 93 |         245, '1', '4', | 
            
              | 94 |             a => $title2, | 
            
              | 95 |     ), | 
            
              | 96 | ); | 
            
              | 97 | my ($biblionumber2) = AddBiblio( $record, '' ); | 
            
              | 98 | my $itemnumber2 = | 
            
              | 99 |   AddItem( { barcode => $barcode2, %item_branch_infos }, $biblionumber2 ); | 
            
              | 100 |  | 
            
              | 101 | my $borrowernumber = | 
            
              | 102 |   AddMember( categorycode => $categorycode, branchcode => $branchcode ); | 
            
              | 103 | my $borrower = GetMember( borrowernumber => $borrowernumber ); | 
            
              | 104 |  | 
            
              | 105 | my $module = new Test::MockModule('C4::Context'); | 
            
              | 106 | $module->mock( 'userenv', sub { { branch => $branchcode } } ); | 
            
              | 107 |  | 
            
              | 108 | my $today = dt_from_string; | 
            
              | 109 | my $yesterday = dt_from_string->subtract_duration( DateTime::Duration->new( days => 1 ) ); | 
            
              | 110 |  | 
            
              | 111 | subtest 'Issue slip' => sub { | 
            
              | 112 |     plan tests => 3; | 
            
              | 113 |  | 
            
              | 114 |     subtest 'Empty slip' => sub { | 
            
              | 115 |         plan tests => 1; | 
            
              | 116 |         my $slip = IssueSlip( $branchcode, $borrowernumber ); | 
            
              | 117 |         my $empty_slip = <<EOS; | 
            
              | 118 | Checked out: | 
            
              | 119 |  | 
            
              | 120 |  | 
            
              | 121 | Overdues: | 
            
              | 122 |  | 
            
              | 123 | EOS | 
            
              | 124 |         is( $slip->{content}, $empty_slip, 'No checked out or overdues return an empty slip, it should return undef (FIXME)' ); | 
            
              | 125 |     }; | 
            
              | 126 |  | 
            
              | 127 |     subtest 'Daily loans' => sub { | 
            
              | 128 |         plan tests => 2; | 
            
              | 129 |         # Test 1: No overdue | 
            
              | 130 |         my $today_daily = $today->clone->set( hour => 23, minute => 59 ); | 
            
              | 131 |         my $today_daily_as_formatted = output_pref( $today_daily ); | 
            
              | 132 |         my $yesterday_daily = $yesterday->clone->set( hour => 23, minute => 59 ); | 
            
              | 133 |         my $yesterday_daily_as_formatted = output_pref( $yesterday_daily ); | 
            
              | 134 |  | 
            
              | 135 |         my ( $date_due, $issue_date, $slip, $expected_slip ); | 
            
              | 136 |         $date_due = $today_daily; | 
            
              | 137 |         $issue_date = $today_daily->clone->subtract_duration( DateTime::Duration->new( minutes => 1 ) ); | 
            
              | 138 |         AddIssue( $borrower, $barcode1, $date_due, undef, $issue_date ); | 
            
              | 139 |         $date_due = $today_daily; | 
            
              | 140 |         $issue_date = $yesterday_daily; | 
            
              | 141 |         AddIssue( $borrower, $barcode2, $date_due, undef, $issue_date ); | 
            
              | 142 |  | 
            
              | 143 |         $expected_slip = <<EOS; | 
            
              | 144 | Checked out: | 
            
              | 145 |  | 
            
              | 146 | Title: $title1 | 
            
              | 147 | Barcode: $barcode1 | 
            
              | 148 | Date due: $today_daily_as_formatted | 
            
              | 149 |  | 
            
              | 150 |  | 
            
              | 151 | Title: $title2 | 
            
              | 152 | Barcode: $barcode2 | 
            
              | 153 | Date due: $today_daily_as_formatted | 
            
              | 154 |  | 
            
              | 155 |  | 
            
              | 156 | Overdues: | 
            
              | 157 |  | 
            
              | 158 | EOS | 
            
              | 159 |         $slip = IssueSlip( $branchcode, $borrowernumber ); | 
            
              | 160 |         is( $slip->{content}, $expected_slip , 'IssueSlip should return a slip with 2 checkouts'); | 
            
              | 161 |  | 
            
              | 162 |         AddReturn( $barcode1, $branchcode ); | 
            
              | 163 |         AddReturn( $barcode2, $branchcode ); | 
            
              | 164 |  | 
            
              | 165 |         # Test 2: 1 Overdue | 
            
              | 166 |         $date_due = $today_daily; | 
            
              | 167 |         $issue_date = $today_daily->clone->subtract_duration( DateTime::Duration->new( minutes => 1 ) ); | 
            
              | 168 |         AddIssue( $borrower, $barcode1, $date_due, undef, $issue_date ); | 
            
              | 169 |         $date_due = $yesterday_daily; | 
            
              | 170 |         $issue_date = $yesterday_daily; | 
            
              | 171 |         AddIssue( $borrower, $barcode2, $date_due, undef, $issue_date ); | 
            
              | 172 |  | 
            
              | 173 |         $expected_slip = <<EOS; | 
            
              | 174 | Checked out: | 
            
              | 175 |  | 
            
              | 176 | Title: $title1 | 
            
              | 177 | Barcode: $barcode1 | 
            
              | 178 | Date due: $today_daily_as_formatted | 
            
              | 179 |  | 
            
              | 180 |  | 
            
              | 181 | Overdues: | 
            
              | 182 |  | 
            
              | 183 | Title: $title2 | 
            
              | 184 | Barcode: $barcode2 | 
            
              | 185 | Date due: $yesterday_daily_as_formatted | 
            
              | 186 |  | 
            
              | 187 | EOS | 
            
              | 188 |         $slip = IssueSlip( $branchcode, $borrowernumber ); | 
            
              | 189 |         is( $slip->{content}, $expected_slip, 'IssueSlip should return a slip with 1 checkout and 1 overdue'); | 
            
              | 190 |  | 
            
              | 191 |         AddReturn( $barcode1, $branchcode ); | 
            
              | 192 |         AddReturn( $barcode2, $branchcode ); | 
            
              | 193 |     }; | 
            
              | 194 |  | 
            
              | 195 |     subtest 'Hourly loans' => sub { | 
            
              | 196 |         plan tests => 2; | 
            
              | 197 |         # Test 1: No overdue | 
            
              | 198 |         my ( $date_due_in_time, $date_due_in_time_as_formatted, $date_due_in_late, $date_due_in_late_as_formatted, $issue_date, $slip, $expected_slip ); | 
            
              | 199 |         # Assuming today is not hour = 23 and minute = 59 | 
            
              | 200 |         $date_due_in_time = $today->clone->set(hour => $today->hour + 1); | 
            
              | 201 |         $date_due_in_time_as_formatted = output_pref( $date_due_in_time ); | 
            
              | 202 |         $issue_date = $today->clone->subtract_duration( DateTime::Duration->new( minutes => 1 ) ); | 
            
              | 203 |         AddIssue( $borrower, $barcode1, $date_due_in_time, undef, $issue_date ); | 
            
              | 204 |         $issue_date = $yesterday->clone; | 
            
              | 205 |         AddIssue( $borrower, $barcode2, $date_due_in_time, undef, $issue_date ); | 
            
              | 206 |  | 
            
              | 207 |         $expected_slip = <<EOS; | 
            
              | 208 | Checked out: | 
            
              | 209 |  | 
            
              | 210 | Title: $title1 | 
            
              | 211 | Barcode: $barcode1 | 
            
              | 212 | Date due: $date_due_in_time_as_formatted | 
            
              | 213 |  | 
            
              | 214 |  | 
            
              | 215 | Title: $title2 | 
            
              | 216 | Barcode: $barcode2 | 
            
              | 217 | Date due: $date_due_in_time_as_formatted | 
            
              | 218 |  | 
            
              | 219 |  | 
            
              | 220 | Overdues: | 
            
              | 221 |  | 
            
              | 222 | EOS | 
            
              | 223 |         $slip = IssueSlip( $branchcode, $borrowernumber ); | 
            
              | 224 |         is( $slip->{content}, $expected_slip, 'IssueSlip should return a slip with 2 checkouts' ); | 
            
              | 225 |  | 
            
              | 226 |         AddReturn( $barcode1, $branchcode ); | 
            
              | 227 |         AddReturn( $barcode2, $branchcode ); | 
            
              | 228 |  | 
            
              | 229 |         # Test 2: 1 Overdue | 
            
              | 230 |         $date_due_in_time = $today->clone->set(hour => $today->hour + 1); | 
            
              | 231 |         $date_due_in_time_as_formatted = output_pref( $date_due_in_time ); | 
            
              | 232 |         $issue_date = $today->clone->subtract_duration( DateTime::Duration->new( minutes => 1 ) ); | 
            
              | 233 |         AddIssue( $borrower, $barcode1, $date_due_in_time, undef, $issue_date ); | 
            
              | 234 |         $date_due_in_late = $today->clone->set(hour => $today->hour - 1); | 
            
              | 235 |         $date_due_in_late_as_formatted = output_pref( $date_due_in_late ); | 
            
              | 236 |         $issue_date = $yesterday->clone; | 
            
              | 237 |         AddIssue( $borrower, $barcode2, $date_due_in_late, undef, $issue_date ); | 
            
              | 238 |  | 
            
              | 239 |         $expected_slip = <<EOS; | 
            
              | 240 | Checked out: | 
            
              | 241 |  | 
            
              | 242 | Title: $title1 | 
            
              | 243 | Barcode: $barcode1 | 
            
              | 244 | Date due: $date_due_in_time_as_formatted | 
            
              | 245 |  | 
            
              | 246 |  | 
            
              | 247 | Overdues: | 
            
              | 248 |  | 
            
              | 249 | Title: $title2 | 
            
              | 250 | Barcode: $barcode2 | 
            
              | 251 | Date due: $date_due_in_late_as_formatted | 
            
              | 252 |  | 
            
              | 253 | EOS | 
            
              | 254 |         $slip = IssueSlip( $branchcode, $borrowernumber ); | 
            
              | 255 |         is( $slip->{content}, $expected_slip, 'IssueSlip should return a slip with 1 checkout and 1 overdue' ); | 
            
              | 256 |  | 
            
              | 257 |         AddReturn( $barcode1, $branchcode ); | 
            
              | 258 |         AddReturn( $barcode2, $branchcode ); | 
            
              | 259 |     }; | 
            
              | 260 |  | 
            
              | 261 | }; | 
            
              | 262 |  | 
            
              | 263 | subtest 'Quick slip' => sub { | 
            
              | 264 |     plan tests => 3; | 
            
              | 265 |  | 
            
              | 266 |     subtest 'Empty slip' => sub { | 
            
              | 267 |         plan tests => 1; | 
            
              | 268 |         my $slip = IssueSlip( $branchcode, $borrowernumber, 'quick slip' ); | 
            
              | 269 |         my $empty_slip = <<EOS; | 
            
              | 270 | Checked out today: | 
            
              | 271 |  | 
            
              | 272 | EOS | 
            
              | 273 |         is( $slip->{content}, $empty_slip, 'No checked out or overdues return an empty slip, it should return undef (FIXME)' ); | 
            
              | 274 |     }; | 
            
              | 275 |  | 
            
              | 276 |     subtest 'Daily loans' => sub { | 
            
              | 277 |         plan tests => 2; | 
            
              | 278 |         # Test 1: No overdue | 
            
              | 279 |         my $today_daily = $today->clone->set( hour => 23, minute => 59 ); | 
            
              | 280 |         my $today_daily_as_formatted = output_pref( $today_daily ); | 
            
              | 281 |         my $yesterday_daily = $yesterday->clone->set( hour => 23, minute => 59 ); | 
            
              | 282 |         my $yesterday_daily_as_formatted = output_pref( $yesterday_daily ); | 
            
              | 283 |  | 
            
              | 284 |         my ( $date_due, $issue_date, $slip, $expected_slip ); | 
            
              | 285 |         $date_due = $today_daily; | 
            
              | 286 |         $issue_date = $today_daily->clone->subtract_duration( DateTime::Duration->new( minutes => 1 ) ); | 
            
              | 287 |         AddIssue( $borrower, $barcode1, $date_due, undef, $issue_date ); | 
            
              | 288 |         $date_due = $today_daily; | 
            
              | 289 |         $issue_date = $yesterday_daily; | 
            
              | 290 |         AddIssue( $borrower, $barcode2, $date_due, undef, $issue_date ); | 
            
              | 291 |  | 
            
              | 292 |         $expected_slip = <<EOS; | 
            
              | 293 | Checked out today: | 
            
              | 294 |  | 
            
              | 295 | Title: $title1 | 
            
              | 296 | Barcode: $barcode1 | 
            
              | 297 | Date due: $today_daily_as_formatted | 
            
              | 298 |  | 
            
              | 299 | EOS | 
            
              | 300 |         $slip = IssueSlip( $branchcode, $borrowernumber, 'quick slip' ); | 
            
              | 301 |         is( $slip->{content}, $expected_slip, 'IssueSlip should return 2 checkouts for today'); | 
            
              | 302 |  | 
            
              | 303 |         AddReturn( $barcode1, $branchcode ); | 
            
              | 304 |         AddReturn( $barcode2, $branchcode ); | 
            
              | 305 |  | 
            
              | 306 |         # Test 2: 1 Overdue | 
            
              | 307 |         $date_due = $today_daily; | 
            
              | 308 |         $issue_date = $today_daily->clone->subtract_duration( DateTime::Duration->new( minutes => 1 ) ); | 
            
              | 309 |         AddIssue( $borrower, $barcode1, $date_due, undef, $issue_date ); | 
            
              | 310 |         $date_due = $yesterday_daily; | 
            
              | 311 |         $issue_date = $yesterday_daily; | 
            
              | 312 |         AddIssue( $borrower, $barcode2, $date_due, undef, $issue_date ); | 
            
              | 313 |  | 
            
              | 314 |         $expected_slip = <<EOS; | 
            
              | 315 | Checked out today: | 
            
              | 316 |  | 
            
              | 317 | Title: $title1 | 
            
              | 318 | Barcode: $barcode1 | 
            
              | 319 | Date due: $today_daily_as_formatted | 
            
              | 320 |  | 
            
              | 321 | EOS | 
            
              | 322 |         $slip = IssueSlip( $branchcode, $borrowernumber, 'quickslip' ); | 
            
              | 323 |         is( $slip->{content}, $expected_slip ); | 
            
              | 324 |     }; | 
            
              | 325 |  | 
            
              | 326 |     subtest 'Hourly loans' => sub { | 
            
              | 327 |         plan tests => 2; | 
            
              | 328 |         # Test 1: No overdue | 
            
              | 329 |         my ( $date_due_in_time, $date_due_in_time_as_formatted, $date_due_in_late, $date_due_in_late_as_formatted, $issue_date, $slip, $expected_slip ); | 
            
              | 330 |         # Assuming today is not hour = 23 and minute = 59 | 
            
              | 331 |         $date_due_in_time = $today->clone->set(hour => $today->hour + 1); | 
            
              | 332 |         $date_due_in_time_as_formatted = output_pref( $date_due_in_time ); | 
            
              | 333 |         $issue_date = $today->clone->subtract_duration( DateTime::Duration->new( minutes => 1 ) ); | 
            
              | 334 |         AddIssue( $borrower, $barcode1, $date_due_in_time, undef, $issue_date ); | 
            
              | 335 |         $issue_date = $yesterday->clone; | 
            
              | 336 |         AddIssue( $borrower, $barcode2, $date_due_in_time, undef, $issue_date ); | 
            
              | 337 |  | 
            
              | 338 |         $expected_slip = <<EOS; | 
            
              | 339 | Checked out today: | 
            
              | 340 |  | 
            
              | 341 | Title: $title1 | 
            
              | 342 | Barcode: $barcode1 | 
            
              | 343 | Date due: $date_due_in_time_as_formatted | 
            
              | 344 |  | 
            
              | 345 | EOS | 
            
              | 346 |         $slip = IssueSlip( $branchcode, $borrowernumber, 'quickslip' ); | 
            
              | 347 |         is( $slip->{content}, $expected_slip ); | 
            
              | 348 |  | 
            
              | 349 |         AddReturn( $barcode1, $branchcode ); | 
            
              | 350 |         AddReturn( $barcode2, $branchcode ); | 
            
              | 351 |  | 
            
              | 352 |         # Test 2: 1 Overdue | 
            
              | 353 |         $date_due_in_time = $today->clone->set(hour => $today->hour + 1); | 
            
              | 354 |         $date_due_in_time_as_formatted = output_pref( $date_due_in_time ); | 
            
              | 355 |         $issue_date = $today->clone->subtract_duration( DateTime::Duration->new( minutes => 1 ) ); | 
            
              | 356 |         AddIssue( $borrower, $barcode1, $date_due_in_time, undef, $issue_date ); | 
            
              | 357 |         $date_due_in_late = $today->clone->set(hour => $today->hour - 1); | 
            
              | 358 |         $date_due_in_late_as_formatted = output_pref( $date_due_in_late ); | 
            
              | 359 |         $issue_date = $yesterday->clone; | 
            
              | 360 |         AddIssue( $borrower, $barcode2, $date_due_in_late, undef, $issue_date ); | 
            
              | 361 |  | 
            
              | 362 |         $expected_slip = <<EOS; | 
            
              | 363 | Checked out today: | 
            
              | 364 |  | 
            
              | 365 | Title: $title1 | 
            
              | 366 | Barcode: $barcode1 | 
            
              | 367 | Date due: $date_due_in_time_as_formatted | 
            
              | 368 |  | 
            
              | 369 | EOS | 
            
              | 370 |         $slip = IssueSlip( $branchcode, $borrowernumber, 'quickslip' ); | 
            
              | 371 |         is( $slip->{content}, $expected_slip ); | 
            
              | 372 |  | 
            
              | 373 |         AddReturn( $barcode1, $branchcode ); | 
            
              | 374 |         AddReturn( $barcode2, $branchcode ); | 
            
              | 375 |     }; | 
            
              | 376 |  | 
            
              | 377 | }; | 
            
              | 378 |  | 
            
              | 379 | subtest 'bad calls' => sub { | 
            
              | 380 |     plan tests => 2; | 
            
              | 381 |     AddIssue( $borrower, $barcode1, $today, undef, $yesterday ); | 
            
              | 382 |     my $slip = IssueSlip(); | 
            
              | 383 |     isnt( $slip, undef, 'IssueSlip should return if no param passed FIXME, should return undef' ); | 
            
              | 384 |     my $empty_slip = <<EOS; | 
            
              | 385 | Checked out: | 
            
              | 386 |  | 
            
              | 387 |  | 
            
              | 388 | Overdues: | 
            
              | 389 |  | 
            
              | 390 | EOS | 
            
              | 391 |  | 
            
              | 392 |     $slip = IssueSlip(undef, $borrowernumber+1); | 
            
              | 393 |     is( $slip->{content}, $empty_slip, 'IssueSlip should not return an empty slip if the borrowernumber passed in param does not exist. But it is what it does for now (FIXME)' ); | 
            
              | 394 | }; |