Bug 36439: Remove duplicate section in columns_settings
[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 => 'issue_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 unless ($vendor) {
52     print $input->redirect("/cgi-bin/koha/errors/404.pl");
53     exit;
54 }
55
56 if ( $op eq 'add_form' || $op eq 'cud-show' ) {
57     $template->param( issue => $issue );
58 } elsif ( $op eq 'cud-add_validate' ) {
59     my $type       = $input->param('type');
60     my $started_on = $input->param('started_on');
61     my $ended_on   = $input->param('ended_on');
62     my $notes      = $input->param('notes');
63
64     if ($issue_id) {
65         try {
66             $issue->set(
67                 {
68                     type       => $type,
69                     started_on => $started_on,
70                     ended_on   => $ended_on,
71                     notes      => $notes
72                 }
73             )->store;
74             push @messages, { type => 'message', code => 'success_on_update' };
75         } catch {
76             push @messages, { type => 'error', code => 'error_on_update' };
77         };
78     } else {
79         try {
80             Koha::Acquisition::Bookseller::Issue->new(
81                 {
82                     vendor_id  => $booksellerid,
83                     type       => $type,
84                     started_on => $started_on,
85                     ended_on   => $ended_on,
86                     notes      => $notes,
87                 }
88             )->store;
89             push @messages, { type => 'message', code => 'success_on_insert' };
90         } catch {
91             push @messages, { type => 'error', code => 'error_on_insert' };
92         };
93     }
94     $op = 'list';
95 } elsif ( $op eq 'delete_confirm' ) {
96     $template->param( issue => $issue );
97 } elsif ( $op eq 'cud-delete_confirmed' ) {
98     try {
99         $issue->delete;
100         push @messages, { type => 'message', code => 'success_on_delete' };
101     } catch {
102         push @messages, { type => 'error', code => 'error_on_delete' };
103     };
104     $op = 'list';
105 }
106
107 if ( $op eq 'list' ) {
108     $template->param( issues_count => $vendor->issues->search->count );
109 }
110
111 $template->param(
112     messages     => \@messages,
113     op           => $op,
114     vendor       => $vendor,
115     booksellerid => $vendor->id,    # Used by vendor-menu.inc
116 );
117
118 output_html_with_http_headers $input, $cookie, $template->output;