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

(-)a/C4/Circulation.pm (-130 / +141 lines)
Lines 1338-1511 AddIssue does the following things : Link Here
1338
1338
1339
sub AddIssue {
1339
sub AddIssue {
1340
    my ( $borrower, $barcode, $datedue, $cancelreserve, $issuedate, $sipmode, $params ) = @_;
1340
    my ( $borrower, $barcode, $datedue, $cancelreserve, $issuedate, $sipmode, $params ) = @_;
1341
1341
    my $onsite_checkout = $params && $params->{onsite_checkout} ? 1 : 0;
1342
    my $onsite_checkout = $params && $params->{onsite_checkout} ? 1 : 0;
1342
    my $auto_renew = $params && $params->{auto_renew};
1343
    my $auto_renew = $params && $params->{auto_renew};
1343
    my $dbh = C4::Context->dbh;
1344
    my $dbh          = C4::Context->dbh;
1344
    my $barcodecheck=CheckValidBarcode($barcode);
1345
    my $barcodecheck = CheckValidBarcode($barcode);
1345
1346
1346
    my $issue;
1347
    my $issue;
1347
1348
1348
    if ($datedue && ref $datedue ne 'DateTime') {
1349
    if ( $datedue && ref $datedue ne 'DateTime' ) {
1349
        $datedue = dt_from_string($datedue);
1350
        $datedue = dt_from_string($datedue);
1350
    }
1351
    }
1352
1351
    # $issuedate defaults to today.
1353
    # $issuedate defaults to today.
1352
    if ( ! defined $issuedate ) {
1354
    if ( !defined $issuedate ) {
1353
        $issuedate = DateTime->now(time_zone => C4::Context->tz());
1355
        $issuedate = DateTime->now( time_zone => C4::Context->tz() );
1354
    }
1356
    }
1355
    else {
1357
    else {
1356
        if ( ref $issuedate ne 'DateTime') {
1358
        if ( ref $issuedate ne 'DateTime' ) {
1357
            $issuedate = dt_from_string($issuedate);
1359
            $issuedate = dt_from_string($issuedate);
1358
1360
1359
        }
1361
        }
1360
    }
1362
    }
1361
	if ($borrower and $barcode and $barcodecheck ne '0'){#??? wtf
1363
1362
		# find which item we issue
1364
    # Stop here if the patron or barcode doesn't exist
1363
		my $item = GetItem('', $barcode) or return;	# if we don't get an Item, abort.
1365
    if ( $borrower && $barcode && $barcodecheck ) {
1364
		my $branch = _GetCircControlBranch($item,$borrower);
1366
        # find which item we issue
1365
		
1367
        my $item = GetItem( '', $barcode )
1366
		# get actual issuing if there is one
1368
          or return;    # if we don't get an Item, abort.
1367
		my $actualissue = GetItemIssue( $item->{itemnumber});
1369
1368
		
1370
        my $branch = _GetCircControlBranch( $item, $borrower );
1369
		# get biblioinformation for this item
1371
1370
		my $biblio = GetBiblioFromItemNumber($item->{itemnumber});
1372
        # get actual issuing if there is one
1371
		
1373
        my $actualissue = GetItemIssue( $item->{itemnumber} );
1372
		#
1374
1373
		# check if we just renew the issue.
1375
        # get biblioinformation for this item
1374
		#
1376
        my $biblio = GetBiblioFromItemNumber( $item->{itemnumber} );
1375
		if ($actualissue->{borrowernumber} eq $borrower->{'borrowernumber'}) {
1377
1376
		    $datedue = AddRenewal(
1378
        # check if we just renew the issue.
1377
			$borrower->{'borrowernumber'},
1379
        if ( $actualissue->{borrowernumber} eq $borrower->{'borrowernumber'} ) {
1378
			$item->{'itemnumber'},
1380
            $datedue = AddRenewal(
1379
			$branch,
1381
                $borrower->{'borrowernumber'},
1380
			$datedue,
1382
                $item->{'itemnumber'},
1381
			$issuedate, # here interpreted as the renewal date
1383
                $branch,
1382
			);
1384
                $datedue,
1383
		}
1385
                $issuedate,    # here interpreted as the renewal date
1384
		else {
1386
            );
1385
        # it's NOT a renewal
1387
        }
1386
			if ( $actualissue->{borrowernumber}) {
1388
        else {
1387
				# This book is currently on loan, but not to the person
1389
            # it's NOT a renewal
1388
				# who wants to borrow it now. mark it returned before issuing to the new borrower
1390
            if ( $actualissue->{borrowernumber} ) {
1391
                # This book is currently on loan, but not to the person
1392
                # who wants to borrow it now. mark it returned before issuing to the new borrower
1389
                my ( $allowed, $message ) = CanBookBeReturned( $item, C4::Context->userenv->{branch} );
1393
                my ( $allowed, $message ) = CanBookBeReturned( $item, C4::Context->userenv->{branch} );
1390
                return unless $allowed;
1394
                return unless $allowed;
1391
				AddReturn(
1395
                AddReturn( $item->{'barcode'}, C4::Context->userenv->{'branch'} );
1392
					$item->{'barcode'},
1396
            }
1393
					C4::Context->userenv->{'branch'}
1394
				);
1395
			}
1396
1397
1397
            MoveReserve( $item->{'itemnumber'}, $borrower->{'borrowernumber'}, $cancelreserve );
1398
            MoveReserve( $item->{'itemnumber'}, $borrower->{'borrowernumber'}, $cancelreserve );
1398
			# Starting process for transfer job (checking transfert and validate it if we have one)
1399
1399
            my ($datesent) = GetTransfers($item->{'itemnumber'});
1400
            # Starting process for transfer job (checking transfert and validate it if we have one)
1401
            my ($datesent) = GetTransfers( $item->{'itemnumber'} );
1400
            if ($datesent) {
1402
            if ($datesent) {
1401
        # 	updating line of branchtranfert to finish it, and changing the to branch value, implement a comment for visibility of this case (maybe for stats ....)
1403
                # updating line of branchtranfert to finish it, and changing the to branch value, implement a comment for visibility of this case (maybe for stats ....)
1402
                my $sth =
1404
                my $sth = $dbh->prepare(
1403
                    $dbh->prepare(
1404
                    "UPDATE branchtransfers 
1405
                    "UPDATE branchtransfers 
1405
                        SET datearrived = now(),
1406
                        SET datearrived = now(),
1406
                        tobranch = ?,
1407
                        tobranch = ?,
1407
                        comments = 'Forced branchtransfer'
1408
                        comments = 'Forced branchtransfer'
1408
                    WHERE itemnumber= ? AND datearrived IS NULL"
1409
                    WHERE itemnumber= ? AND datearrived IS NULL"
1409
                    );
1410
                );
1410
                $sth->execute(C4::Context->userenv->{'branch'},$item->{'itemnumber'});
1411
                $sth->execute( C4::Context->userenv->{'branch'},
1412
                    $item->{'itemnumber'} );
1411
            }
1413
            }
1412
1414
1413
        # If automatic renewal wasn't selected while issuing, set the value according to the issuing rule.
1415
            # If automatic renewal wasn't selected while issuing, set the value according to the issuing rule.
1414
        unless ($auto_renew) {
1416
            unless ($auto_renew) {
1415
            my $issuingrule = GetIssuingRule($borrower->{categorycode}, $item->{itype}, $branch);
1417
                my $issuingrule = GetIssuingRule( $borrower->{categorycode}, $item->{itype}, $branch );
1416
            $auto_renew = $issuingrule->{auto_renew};
1418
                $auto_renew = $issuingrule->{auto_renew};
1417
        }
1419
            }
1418
1420
1419
        # Record in the database the fact that the book was issued.
1421
            # Record in the database the fact that the book was issued.
1420
        unless ($datedue) {
1422
            unless ($datedue) {
1421
            my $itype = ( C4::Context->preference('item-level_itypes') ) ? $biblio->{'itype'} : $biblio->{'itemtype'};
1423
                my $itype =
1422
            $datedue = CalcDateDue( $issuedate, $itype, $branch, $borrower );
1424
                  ( C4::Context->preference('item-level_itypes') )
1425
                  ? $biblio->{'itype'}
1426
                  : $biblio->{'itemtype'};
1427
                $datedue = CalcDateDue( $issuedate, $itype, $branch, $borrower );
1423
1428
1424
        }
1425
        $datedue->truncate( to => 'minute');
1426
1427
        $issue = Koha::Database->new()->schema()->resultset('Issue')->create(
1428
            {
1429
                borrowernumber  => $borrower->{'borrowernumber'},
1430
                itemnumber      => $item->{'itemnumber'},
1431
                issuedate       => $issuedate->strftime('%Y-%m-%d %H:%M:%S'),
1432
                date_due        => $datedue->strftime('%Y-%m-%d %H:%M:%S'),
1433
                branchcode      => C4::Context->userenv->{'branch'},
1434
                onsite_checkout => $onsite_checkout,
1435
                auto_renew      => $auto_renew ? 1 : 0
1436
            }
1429
            }
1437
        );
1430
            $datedue->truncate( to => 'minute' );
1438
1431
1439
        if ( C4::Context->preference('ReturnToShelvingCart') ) { ## ReturnToShelvingCart is on, anything issued should be taken off the cart.
1432
            $issue = Koha::Database->new()->schema()->resultset('Issue')->create(
1440
          CartToShelf( $item->{'itemnumber'} );
1433
                {
1441
        }
1434
                    borrowernumber => $borrower->{'borrowernumber'},
1442
        $item->{'issues'}++;
1435
                    itemnumber     => $item->{'itemnumber'},
1443
        if ( C4::Context->preference('UpdateTotalIssuesOnCirc') ) {
1436
                    issuedate      => $issuedate->strftime('%Y-%m-%d %H:%M:%S'),
1444
            UpdateTotalIssues($item->{'biblionumber'}, 1);
1437
                    date_due       => $datedue->strftime('%Y-%m-%d %H:%M:%S'),
1445
        }
1438
                    branchcode     => C4::Context->userenv->{'branch'},
1439
                    onsite_checkout => $onsite_checkout,
1440
                    auto_renew      => $auto_renew ? 1 : 0
1441
                }
1442
              );
1446
1443
1447
        ## If item was lost, it has now been found, reverse any list item charges if necessary.
1444
            if ( C4::Context->preference('ReturnToShelvingCart') ) {
1448
        if ( $item->{'itemlost'} ) {
1445
                # ReturnToShelvingCart is on, anything issued should be taken off the cart.
1449
            if ( C4::Context->preference('RefundLostItemFeeOnReturn' ) ) {
1446
                CartToShelf( $item->{'itemnumber'} );
1450
                _FixAccountForLostAndReturned( $item->{'itemnumber'}, undef, $item->{'barcode'} );
1447
            }
1448
            $item->{'issues'}++;
1449
            if ( C4::Context->preference('UpdateTotalIssuesOnCirc') ) {
1450
                UpdateTotalIssues( $item->{'biblionumber'}, 1 );
1451
            }
1451
            }
1452
        }
1453
1452
1454
        ModItem({ issues           => $item->{'issues'},
1453
            ## If item was lost, it has now been found, reverse any list item charges if necessary.
1455
                  holdingbranch    => C4::Context->userenv->{'branch'},
1454
            if ( $item->{'itemlost'} ) {
1456
                  itemlost         => 0,
1455
                if ( C4::Context->preference('RefundLostItemFeeOnReturn') ) {
1457
                  datelastborrowed => DateTime->now(time_zone => C4::Context->tz())->ymd(),
1456
                    _FixAccountForLostAndReturned( $item->{'itemnumber'}, undef, $item->{'barcode'} );
1458
                  onloan           => $datedue->ymd(),
1457
                }
1459
                }, $item->{'biblionumber'}, $item->{'itemnumber'});
1458
            }
1460
        ModDateLastSeen( $item->{'itemnumber'} );
1461
1459
1462
        # If it costs to borrow this book, charge it to the patron's account.
1460
            ModItem(
1463
        my ( $charge, $itemtype ) = GetIssuingCharges(
1461
                {
1464
            $item->{'itemnumber'},
1462
                    issues        => $item->{'issues'},
1465
            $borrower->{'borrowernumber'}
1463
                    holdingbranch => C4::Context->userenv->{'branch'},
1466
        );
1464
                    itemlost      => 0,
1467
        if ( $charge > 0 ) {
1465
                    onloan        => $datedue->ymd(),
1468
            AddIssuingCharge(
1466
                    datelastborrowed => DateTime->now( time_zone => C4::Context->tz() )->ymd(),
1469
                $item->{'itemnumber'},
1467
                },
1470
                $borrower->{'borrowernumber'}, $charge
1468
                $item->{'biblionumber'},
1469
                $item->{'itemnumber'}
1471
            );
1470
            );
1472
            $item->{'charge'} = $charge;
1471
            ModDateLastSeen( $item->{'itemnumber'} );
1473
        }
1474
1472
1475
        # Record the fact that this book was issued.
1473
           # If it costs to borrow this book, charge it to the patron's account.
1476
        &UpdateStats({
1474
            my ( $charge, $itemtype ) = GetIssuingCharges( $item->{'itemnumber'}, $borrower->{'borrowernumber'} );
1477
                      branch => C4::Context->userenv->{'branch'},
1475
            if ( $charge > 0 ) {
1478
                      type => ( $onsite_checkout ? 'onsite_checkout' : 'issue' ),
1476
                AddIssuingCharge( $item->{'itemnumber'}, $borrower->{'borrowernumber'}, $charge );
1479
                      amount => $charge,
1477
                $item->{'charge'} = $charge;
1480
                      other => ($sipmode ? "SIP-$sipmode" : ''),
1478
            }
1481
                      itemnumber => $item->{'itemnumber'},
1482
                      itemtype => $item->{'itype'},
1483
                      borrowernumber => $borrower->{'borrowernumber'},
1484
                      ccode => $item->{'ccode'}}
1485
        );
1486
1479
1487
        # Send a checkout slip.
1480
            # Record the fact that this book was issued.
1488
        my $circulation_alert = 'C4::ItemCirculationAlertPreference';
1481
            &UpdateStats(
1489
        my %conditions = (
1482
                {
1490
            branchcode   => $branch,
1483
                    branch => C4::Context->userenv->{'branch'},
1491
            categorycode => $borrower->{categorycode},
1484
                    type => ( $onsite_checkout ? 'onsite_checkout' : 'issue' ),
1492
            item_type    => $item->{itype},
1485
                    amount         => $charge,
1493
            notification => 'CHECKOUT',
1486
                    other          => ( $sipmode ? "SIP-$sipmode" : '' ),
1494
        );
1487
                    itemnumber     => $item->{'itemnumber'},
1495
        if ($circulation_alert->is_enabled_for(\%conditions)) {
1488
                    itemtype       => $item->{'itype'},
1496
            SendCirculationAlert({
1489
                    borrowernumber => $borrower->{'borrowernumber'},
1497
                type     => 'CHECKOUT',
1490
                    ccode          => $item->{'ccode'}
1498
                item     => $item,
1491
                }
1499
                borrower => $borrower,
1492
            );
1500
                branch   => $branch,
1493
1501
            });
1494
            # Send a checkout slip.
1495
            my $circulation_alert = 'C4::ItemCirculationAlertPreference';
1496
            my %conditions        = (
1497
                branchcode   => $branch,
1498
                categorycode => $borrower->{categorycode},
1499
                item_type    => $item->{itype},
1500
                notification => 'CHECKOUT',
1501
            );
1502
            if ( $circulation_alert->is_enabled_for( \%conditions ) ) {
1503
                SendCirculationAlert(
1504
                    {
1505
                        type     => 'CHECKOUT',
1506
                        item     => $item,
1507
                        borrower => $borrower,
1508
                        branch   => $branch,
1509
                    }
1510
                );
1511
            }
1502
        }
1512
        }
1503
    }
1504
1513
1505
    logaction("CIRCULATION", "ISSUE", $borrower->{'borrowernumber'}, $biblio->{'itemnumber'})
1514
        logaction(
1506
        if C4::Context->preference("IssueLog");
1515
            "CIRCULATION", "ISSUE",
1507
  }
1516
            $borrower->{'borrowernumber'},
1508
  return $issue;
1517
            $biblio->{'itemnumber'}
1518
        ) if C4::Context->preference("IssueLog");
1519
    }
1520
    return $issue;
1509
}
1521
}
1510
1522
1511
=head2 GetLoanLength
1523
=head2 GetLoanLength
1512
- 

Return to bug 16534