Lines 44-113
sub add {
Link Here
|
44 |
|
44 |
|
45 |
my $body = $c->req->json; |
45 |
my $body = $c->req->json; |
46 |
|
46 |
|
47 |
my $borrowernumber = $body->{borrowernumber}; |
47 |
my $onlyfirst = $body->{onlyfirst}; |
48 |
my $biblionumber = $body->{biblionumber}; |
48 |
my $reserve_group_id; |
49 |
my $itemnumber = $body->{itemnumber}; |
49 |
if ($onlyfirst) { |
50 |
my $branchcode = $body->{branchcode}; |
50 |
$reserve_group_id = AddReserveGroup(); |
51 |
my $expirationdate = $body->{expirationdate}; |
|
|
52 |
|
53 |
my $borrower = Koha::Borrowers->find($borrowernumber); |
54 |
unless ($borrower) { |
55 |
return $c->$cb({error => "Borrower not found"}, 404); |
56 |
} |
57 |
|
58 |
unless ($biblionumber or $itemnumber) { |
59 |
return $c->$cb({ |
60 |
error => "At least one of biblionumber, itemnumber should be given" |
61 |
}, 400); |
62 |
} |
63 |
unless ($branchcode) { |
64 |
return $c->$cb({ |
65 |
error => "Branchcode is required" |
66 |
}, 400); |
67 |
} |
68 |
|
69 |
my $item_biblionumber = C4::Biblio::GetBiblionumberFromItemnumber($itemnumber); |
70 |
if ($biblionumber and $biblionumber != $item_biblionumber) { |
71 |
return $c->$cb({ |
72 |
error => "Item $itemnumber doesn't belong to biblio $biblionumber" |
73 |
}, 400); |
74 |
} |
51 |
} |
75 |
|
52 |
|
76 |
$biblionumber ||= $item_biblionumber; |
53 |
my @possible_reserves; |
77 |
my $biblio = C4::Biblio::GetBiblio($biblionumber); |
54 |
foreach my $reserve (@{ $body->{reserves} }) { |
78 |
|
55 |
my $borrowernumber = $reserve->{borrowernumber}; |
79 |
my $can_reserve = |
56 |
my $biblionumber = $reserve->{biblionumber}; |
80 |
$itemnumber |
57 |
my $itemnumber = $reserve->{itemnumber}; |
81 |
? CanItemBeReserved( $borrowernumber, $itemnumber ) |
58 |
my $branchcode = $reserve->{branchcode}; |
82 |
: CanBookBeReserved( $borrowernumber, $biblionumber ); |
59 |
my $expirationdate = $reserve->{expirationdate}; |
83 |
|
60 |
|
84 |
unless ($can_reserve eq 'OK') { |
61 |
my $borrower = Koha::Borrowers->find($borrowernumber); |
85 |
return $c->$cb({ |
62 |
unless ($borrower) { |
86 |
error => "Reserve cannot be placed. Reason: $can_reserve" |
63 |
return $c->$cb({error => "Borrower not found"}, 404); |
87 |
}, 403); |
64 |
} |
|
|
65 |
|
66 |
unless ($biblionumber or $itemnumber) { |
67 |
return $c->$cb({ |
68 |
error => "At least one of biblionumber, itemnumber should be given" |
69 |
}, 400); |
70 |
} |
71 |
unless ($branchcode) { |
72 |
return $c->$cb({ |
73 |
error => "Branchcode is required" |
74 |
}, 400); |
75 |
} |
76 |
|
77 |
my $item_biblionumber = C4::Biblio::GetBiblionumberFromItemnumber($itemnumber); |
78 |
if ($biblionumber and $biblionumber != $item_biblionumber) { |
79 |
return $c->$cb({ |
80 |
error => "Item $itemnumber doesn't belong to biblio $biblionumber" |
81 |
}, 400); |
82 |
} |
83 |
|
84 |
$biblionumber ||= $item_biblionumber; |
85 |
my $biblio = C4::Biblio::GetBiblio($biblionumber); |
86 |
|
87 |
my $can_reserve = |
88 |
$itemnumber |
89 |
? CanItemBeReserved( $borrowernumber, $itemnumber ) |
90 |
: CanBookBeReserved( $borrowernumber, $biblionumber ); |
91 |
|
92 |
unless ($can_reserve eq 'OK') { |
93 |
return $c->$cb({ |
94 |
error => "Reserve cannot be placed. Reason: $can_reserve" |
95 |
}, 403); |
96 |
} |
97 |
|
98 |
$reserve->{title} = $biblio->{title}; |
99 |
push @possible_reserves, $reserve; |
88 |
} |
100 |
} |
89 |
|
101 |
|
90 |
my $priority = C4::Reserves::CalculatePriority($biblionumber); |
102 |
my @reserves; |
91 |
$itemnumber ||= undef; |
103 |
foreach my $reserve (@possible_reserves) { |
92 |
|
104 |
my $borrowernumber = $reserve->{borrowernumber}; |
93 |
# AddReserve expects date to be in syspref format |
105 |
my $biblionumber = $reserve->{biblionumber}; |
94 |
if ($expirationdate) { |
106 |
my $itemnumber = $reserve->{itemnumber}; |
95 |
$expirationdate = output_pref(dt_from_string($expirationdate, 'iso')); |
107 |
my $branchcode = $reserve->{branchcode}; |
|
|
108 |
my $expirationdate = $reserve->{expirationdate}; |
109 |
|
110 |
my $priority = C4::Reserves::CalculatePriority($biblionumber); |
111 |
$itemnumber ||= undef; |
112 |
|
113 |
# AddReserve expects date to be in syspref format |
114 |
if ($expirationdate) { |
115 |
$expirationdate = output_pref(dt_from_string($expirationdate, 'iso')); |
116 |
} |
117 |
|
118 |
my $reserve_id = C4::Reserves::AddReserve($branchcode, $borrowernumber, |
119 |
$biblionumber, undef, $priority, undef, $expirationdate, undef, |
120 |
$reserve->{title}, $itemnumber, undef, $reserve_group_id); |
121 |
|
122 |
unless ($reserve_id) { |
123 |
return $c->$cb({ |
124 |
error => "Error while placing reserve. See Koha logs for details." |
125 |
}, 500); |
126 |
} |
127 |
|
128 |
push @reserves, C4::Reserves::GetReserve($reserve_id); |
96 |
} |
129 |
} |
97 |
|
130 |
|
98 |
my $reserve_id = C4::Reserves::AddReserve($branchcode, $borrowernumber, |
131 |
return $c->$cb(\@reserves, 201); |
99 |
$biblionumber, undef, $priority, undef, $expirationdate, undef, |
|
|
100 |
$biblio->{title}, $itemnumber); |
101 |
|
102 |
unless ($reserve_id) { |
103 |
return $c->$cb({ |
104 |
error => "Error while placing reserve. See Koha logs for details." |
105 |
}, 500); |
106 |
} |
107 |
|
108 |
my $reserve = C4::Reserves::GetReserve($reserve_id); |
109 |
|
110 |
return $c->$cb($reserve, 201); |
111 |
} |
132 |
} |
112 |
|
133 |
|
113 |
sub edit { |
134 |
sub edit { |