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