Bug 37163: Fix the redirect after deleting a tag from an authority framework to load...
[koha.git] / admin / edi_accounts.pl
1 #!/usr/bin/perl
2
3 # Copyright 2011,2014 Mark Gavillet & PTFS Europe Ltd
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
22 use CGI;
23
24 use C4::Auth qw( get_template_and_user );
25 use C4::Output qw( output_html_with_http_headers );
26 use Koha::Database;
27 use Koha::Encryption;
28 use Koha::Plugins;
29
30 our $input = CGI->new();
31 our $schema = Koha::Database->new()->schema();
32
33 our ( $template, $loggedinuser, $cookie ) = get_template_and_user(
34     {
35         template_name   => 'admin/edi_accounts.tt',
36         query           => $input,
37         type            => 'intranet',
38         flagsrequired   => { acquisition => 'edi_manage' },
39     }
40 );
41
42 my $crypt = Koha::Encryption->new;
43
44 my $op = $input->param('op');
45 $op ||= 'display';
46
47 if ( $op eq 'acct_form' ) {
48     show_account($crypt);
49     $template->param( acct_form => 1 );
50     my @vendors = $schema->resultset('Aqbookseller')->search(
51         undef,
52         {
53             columns => [ 'name', 'id' ],
54             order_by => { -asc => 'name' }
55         }
56     );
57     $template->param( vendors => \@vendors );
58
59     if ( C4::Context->config("enable_plugins") ) {
60         my @plugins = Koha::Plugins->new()->GetPlugins({
61             method => 'edifact',
62         });
63         $template->param( plugins => \@plugins );
64     }
65 }
66 elsif ( $op eq 'delete_confirm' ) {
67     show_account($crypt);
68     $template->param( delete_confirm => 1 );
69 }
70 else {
71     if ( $op eq 'cud-save' ) {
72
73         # validate & display
74         my $id     = $input->param('id');
75         my $password = scalar $input->param('password');
76         $password = $crypt->encrypt_hex($password);
77         my $fields = {
78             description        => scalar $input->param('description'),
79             host               => scalar $input->param('host'),
80             username           => scalar $input->param('username'),
81             password           => $password,
82             upload_port        => scalar $input->param('upload_port'),
83             download_port      => scalar $input->param('download_port'),
84             vendor_id          => scalar $input->param('vendor_id'),
85             upload_directory   => scalar $input->param('upload_directory'),
86             download_directory => scalar $input->param('download_directory'),
87             san                => scalar $input->param('san'),
88             standard           => scalar $input->param('standard'),
89             transport          => scalar $input->param('transport'),
90             quotes_enabled     => $input->param('quotes_enabled') ? 1 : 0,
91             invoices_enabled   => $input->param('invoices_enabled') ? 1 : 0,
92             orders_enabled     => $input->param('orders_enabled') ? 1 : 0,
93             responses_enabled  => $input->param('responses_enabled') ? 1 : 0,
94             auto_orders        => $input->param('auto_orders') ? 1 : 0,
95             id_code_qualifier  => scalar $input->param('id_code_qualifier'),
96             plugin             => scalar $input->param('plugin'),
97         };
98
99         if ($id) {
100             $schema->resultset('VendorEdiAccount')->search(
101                 {
102                     id => $id,
103                 }
104             )->update_all($fields);
105         }
106         else {    # new record
107             $schema->resultset('VendorEdiAccount')->create($fields);
108         }
109     }
110     elsif ( $op eq 'cud-delete_confirmed' ) {
111
112         $schema->resultset('VendorEdiAccount')
113           ->search( { id => scalar $input->param('id'), } )->delete_all;
114     }
115
116     # we do a default dispaly after deletes and saves
117     # as well as when thats all you want
118     $template->param( display => 1 );
119     my @ediaccounts = $schema->resultset('VendorEdiAccount')->search(
120         {},
121         {
122             join => 'vendor',
123         }
124     );
125     $template->param( ediaccounts => \@ediaccounts );
126 }
127
128 $template->param(
129     code_qualifiers => [
130         {
131             code        => '14',
132             description => 'EAN International',
133         },
134         {
135             code        => '31B',
136             description => 'US SAN Agency',
137         },
138         {
139             code        => '91',
140             description => 'Assigned by supplier',
141         },
142         {
143             code        => '92',
144             description => 'Assigned by buyer',
145         },
146     ],
147     standards => [ 'BIC', 'EUR' ]
148 );
149
150 output_html_with_http_headers( $input, $cookie, $template->output );
151
152 sub get_account {
153     my $id = shift;
154
155     my $account = $schema->resultset('VendorEdiAccount')->find($id);
156     if ($account) {
157         return $account;
158     }
159
160     # passing undef will default to add
161     return;
162 }
163
164 sub show_account {
165     my $crypt = shift;
166     my $acct_id = $input->param('id');
167     if ($acct_id) {
168         my $acct = $schema->resultset('VendorEdiAccount')->find($acct_id);
169         $acct->password( $crypt->decrypt_hex($acct->password) );
170         if ($acct) {
171             $template->param( account => $acct );
172         }
173     }
174     return;
175 }