Bug 15630 - Make Edifact module pluggable
[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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22 use CGI;
23 use C4::Auth;
24 use C4::Output;
25 use Koha::Database;
26 use Koha::Plugins;
27
28 my $input = CGI->new();
29
30 my ( $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 my $schema = Koha::Database->new()->schema();
43
44 if ( $op eq 'acct_form' ) {
45     show_account();
46     $template->param( acct_form => 1 );
47     my @vendors = $schema->resultset('Aqbookseller')->search(
48         undef,
49         {
50             columns => [ 'name', 'id' ],
51             order_by => { -asc => 'name' }
52         }
53     );
54     $template->param( vendors => \@vendors );
55
56     my @plugins = Koha::Plugins->new()->GetPlugins('edifact');
57     $template->param( plugins => \@plugins );
58 }
59 elsif ( $op eq 'delete_confirm' ) {
60     show_account();
61     $template->param( delete_confirm => 1 );
62 }
63 else {
64     if ( $op eq 'save' ) {
65
66         # validate & display
67         my $id     = $input->param('id');
68         my $fields = {
69             description        => $input->param('description'),
70             host               => $input->param('host'),
71             username           => $input->param('username'),
72             password           => $input->param('password'),
73             vendor_id          => $input->param('vendor_id'),
74             upload_directory   => $input->param('upload_directory'),
75             download_directory => $input->param('download_directory'),
76             san                => $input->param('san'),
77             transport          => $input->param('transport'),
78             quotes_enabled     => defined $input->param('quotes_enabled'),
79             invoices_enabled   => defined $input->param('invoices_enabled'),
80             orders_enabled     => defined $input->param('orders_enabled'),
81             responses_enabled  => defined $input->param('responses_enabled'),
82             auto_orders        => defined $input->param('auto_orders'),
83             id_code_qualifier  => $input->param('id_code_qualifier'),
84             plugin             => $input->param('plugin'),
85         };
86
87         if ($id) {
88             $schema->resultset('VendorEdiAccount')->search(
89                 {
90                     id => $id,
91                 }
92             )->update_all($fields);
93         }
94         else {    # new record
95             $schema->resultset('VendorEdiAccount')->create($fields);
96         }
97     }
98     elsif ( $op eq 'delete_confirmed' ) {
99
100         $schema->resultset('VendorEdiAccount')
101           ->search( { id => $input->param('id'), } )->delete_all;
102     }
103
104     # we do a default dispaly after deletes and saves
105     # as well as when thats all you want
106     $template->param( display => 1 );
107     my @ediaccounts = $schema->resultset('VendorEdiAccount')->search(
108         {},
109         {
110             join => 'vendor',
111         }
112     );
113     $template->param( ediaccounts => \@ediaccounts );
114 }
115
116 $template->param(
117     code_qualifiers => [
118         {
119             code        => '14',
120             description => 'EAN International',
121         },
122         {
123             code        => '31B',
124             description => 'US SAN Agency',
125         },
126         {
127             code        => '91',
128             description => 'Assigned by supplier',
129         },
130         {
131             code        => '92',
132             description => 'Assigned by buyer',
133         },
134     ]
135 );
136
137 output_html_with_http_headers( $input, $cookie, $template->output );
138
139 sub get_account {
140     my $id = shift;
141
142     my $account = $schema->resultset('VendorEdiAccount')->find($id);
143     if ($account) {
144         return $account;
145     }
146
147     # passing undef will default to add
148     return;
149 }
150
151 sub show_account {
152     my $acct_id = $input->param('id');
153     if ($acct_id) {
154         my $acct = $schema->resultset('VendorEdiAccount')->find($acct_id);
155         if ($acct) {
156             $template->param( account => $acct );
157         }
158     }
159     return;
160 }