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