Bug 33105: (QA follow-up) REST API
[koha.git] / acqui / vendor_issues.pl
1 #!/usr/bin/perl
2
3 # Copyright 2023 Koha Development Team
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21 use CGI qw ( -utf8 );
22 use Try::Tiny;
23 use C4::Context;
24 use C4::Auth qw( get_template_and_user );
25 use C4::Output qw( output_html_with_http_headers );
26
27 use Koha::Acquisition::Booksellers;
28
29 my $input        = CGI->new;
30 my $booksellerid = $input->param('booksellerid');
31 my $issue_id     = $input->param('issue_id');
32 my $op           = $input->param('op') || 'list';
33 my @messages;
34
35 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
36     {
37         template_name => "acqui/vendor_issues.tt",
38         query         => $input,
39         type          => "intranet",
40         flagsrequired => { acquisition => 'issues_manage' },
41     }
42 );
43
44 my $issue;
45 if ( $issue_id ) {
46     $issue = Koha::Acquisition::Bookseller::Issues->find($issue_id);
47     $booksellerid = $issue->vendor_id;
48 }
49 my $vendor = Koha::Acquisition::Booksellers->find($booksellerid);
50
51 if ( $op eq 'add_form' || $op eq 'show' ) {
52     $template->param( issue => $issue );
53 } elsif ( $op eq 'add_validate' ) {
54     my $type       = $input->param('type');
55     my $started_on = $input->param('started_on');
56     my $ended_on   = $input->param('ended_on');
57     my $notes      = $input->param('notes');
58
59     if ($issue_id) {
60         try {
61             $issue->set(
62                 {
63                     type       => $type,
64                     started_on => $started_on,
65                     ended_on   => $ended_on,
66                     notes      => $notes
67                 }
68             )->store;
69             push @messages, { type => 'message', code => 'success_on_update' };
70         } catch {
71             push @messages, { type => 'error', code => 'error_on_update' };
72         };
73     } else {
74         try {
75             Koha::Acquisition::Bookseller::Issue->new(
76                 {
77                     vendor_id  => $booksellerid,
78                     type       => $type,
79                     started_on => $started_on,
80                     ended_on   => $ended_on,
81                     notes      => $notes,
82                 }
83             )->store;
84             push @messages, { type => 'message', code => 'success_on_insert' };
85         } catch {
86             push @messages, { type => 'error', code => 'error_on_insert' };
87         };
88     }
89     $op = 'list';
90 } elsif ( $op eq 'delete_confirm' ) {
91     $template->param( issue => $issue );
92 } elsif ( $op eq 'delete_confirmed' ) {
93     try {
94         $issue->delete;
95         push @messages, { type => 'message', code => 'success_on_delete' };
96     } catch {
97         push @messages, { type => 'error', code => 'error_on_delete' };
98     };
99     $op = 'list';
100 }
101
102 if ( $op eq 'list' ) {
103     $template->param( issues_count => $vendor->issues->search->count );
104 }
105
106 $template->param(
107     messages     => \@messages,
108     op           => $op,
109     vendor       => $vendor,
110     booksellerid => $vendor->id,    # Used by vendor-menu.inc
111 );
112
113 output_html_with_http_headers $input, $cookie, $template->output;