Commit ade3a927 authored by bensinober's avatar bensinober
Browse files

DEICH-5845: koha transfers - rewrite Deichman::Item::Transfer to use location

parent 39dbde72
......@@ -2,6 +2,7 @@ package Deichman::Item::Transfer;
use Modern::Perl;
use Deichman::Exception;
use Deichman::Spore::DB;
use parent "Deichman::Item";
sub new {
......@@ -14,51 +15,40 @@ sub new {
return $self;
}
sub Read {
my ($self) = @_;
$self->{transfer} or return;
my $dbh = $self->dbh;
my $q = "SELECT * FROM branchtransfers WHERE branchtransfer_id = ?";
my $sth = $dbh->prepare($q);
$sth->execute($self->{transfer}->{branchtransfer_id}) or Deichman::Exception::Item->throw($dbh->errstr);
my $transfer = $sth->fetchrow_hashref() or Deichman::Exception::Item::Transfer::NotFound->throw();
$self->{transfer} = $transfer;
return $self;
}
sub GetActive {
my ($self) = @_;
$self->{item} or return;
my $dbh = $self->dbh;
my $q = "SELECT * FROM branchtransfers WHERE itemnumber = ? AND datearrived IS NULL";
my $sth = $dbh->prepare($q);
$sth->execute($self->{item}->{itemnumber}) or Deichman::Exception::Item->throw($dbh->errstr);
my $transfer = $sth->fetchrow_hashref();
$self->{transfer} = $transfer;
$self->{item} or return $self;
$self->{location} or $self->Deichman::Spore::DB::GetLocation();
my $regexp = s/\..*//r; # Strip lazy everything after first period
my $goes = $self->{location}->{goes} ? $self->{location}->{goes} =~ $regexp : undef;
my $seen = $self->{location}->{seen} ? $self->{location}->{seen} =~ $regexp : undef;
my $co = $self->{location}->{co} ? $self->{location}->{co} =~ $regexp : undef;
my $lives = $self->{location}->{lives} ? $self->{location}->{lives} =~ $regexp : undef;
my $home = $co ? $co : $lives;
if (($goes and $goes ne $home) or
($seen and $seen ne $home)) {
$self->{transfer} = { tobranch => $home, frombranch => $seen };
}
return $self;
}
# The only logical "adding" of a trasfer would be to set goes to some other branch
# this is probably never needed
sub Add {
my ($self, $fromBranch, $toBranch) = @_;
$self->{item} or return;
$self->{item} or return $sef;
($fromBranch and $toBranch) or Deichman::Exception::Item::Transfer::ArgumentError->throw();
$self->Deichman::Item::Transfer::FinishActive(); # Close any open transfers first
my $dbh = $self->dbh;
my $q = "INSERT INTO branchtransfers (itemnumber,datesent,frombranch,tobranch) VALUES (?,NOW(),?,?)";
my $sth = $dbh->prepare($q);
$sth->execute($self->{item}->{itemnumber}, $fromBranch, $toBranch) or Deichman::Exception::Item->throw($dbh->errstr);
$self->{transfer}->{branchtransfer_id} = $sth->last_insert_id();
return $self->Deichman::Item::Transfer::Read;
my $loc = { goes => $toBranch };
$self->Deichman::Spore::DB::UpsertLocation($loc);
return $self;
}
# Finish any open transfer by itemnumber, we never want more than one
# The only logical "finishing" of a trasfer would be to set goes undef
sub FinishActive {
my ($self) = @_;
$self->{item} or return;
my $dbh = $self->dbh;
my $q = "UPDATE branchtransfers SET datearrived = NOW() WHERE itemnumber = ? and datearrived IS NULL";
my $sth = $dbh->prepare($q);
$sth->execute($self->{item}->{itemnumber});
my $loc = { goes => undef };
$self->Deichman::Spore::DB::UpsertLocation($loc);
return $self;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment