Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[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 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     my $plugins_enabled = C4::Context->preference('UseKohaPlugins') && C4::Context->config("enable_plugins");
56     $template->param( plugins_enabled => $plugins_enabled );
57
58     if ( $plugins_enabled ) {
59         my @plugins = Koha::Plugins->new()->GetPlugins({
60             method => 'edifact',
61         });
62         $template->param( plugins => \@plugins );
63     }
64 }
65 elsif ( $op eq 'delete_confirm' ) {
66     show_account();
67     $template->param( delete_confirm => 1 );
68 }
69 else {
70     if ( $op eq 'save' ) {
71
72         # validate & display
73         my $id     = $input->param('id');
74         my $fields = {
75             description        => scalar $input->param('description'),
76             host               => scalar $input->param('host'),
77             username           => scalar $input->param('username'),
78             password           => scalar $input->param('password'),
79             vendor_id          => scalar $input->param('vendor_id'),
80             upload_directory   => scalar $input->param('upload_directory'),
81             download_directory => scalar $input->param('download_directory'),
82             san                => scalar $input->param('san'),
83             transport          => scalar $input->param('transport'),
84             quotes_enabled     => $input->param('quotes_enabled') ? 1 : 0,
85             invoices_enabled   => $input->param('invoices_enabled') ? 1 : 0,
86             orders_enabled     => $input->param('orders_enabled') ? 1 : 0,
87             responses_enabled  => $input->param('responses_enabled') ? 1 : 0,
88             auto_orders        => $input->param('auto_orders') ? 1 : 0,
89             id_code_qualifier  => scalar $input->param('id_code_qualifier'),
90             plugin             => scalar $input->param('plugin'),
91         };
92
93         if ($id) {
94             $schema->resultset('VendorEdiAccount')->search(
95                 {
96                     id => $id,
97                 }
98             )->update_all($fields);
99         }
100         else {    # new record
101             $schema->resultset('VendorEdiAccount')->create($fields);
102         }
103     }
104     elsif ( $op eq 'delete_confirmed' ) {
105
106         $schema->resultset('VendorEdiAccount')
107           ->search( { id => scalar $input->param('id'), } )->delete_all;
108     }
109
110     # we do a default dispaly after deletes and saves
111     # as well as when thats all you want
112     $template->param( display => 1 );
113     my @ediaccounts = $schema->resultset('VendorEdiAccount')->search(
114         {},
115         {
116             join => 'vendor',
117         }
118     );
119     $template->param( ediaccounts => \@ediaccounts );
120 }
121
122 $template->param(
123     code_qualifiers => [
124         {
125             code        => '14',
126             description => 'EAN International',
127         },
128         {
129             code        => '31B',
130             description => 'US SAN Agency',
131         },
132         {
133             code        => '91',
134             description => 'Assigned by supplier',
135         },
136         {
137             code        => '92',
138             description => 'Assigned by buyer',
139         },
140     ]
141 );
142
143 output_html_with_http_headers( $input, $cookie, $template->output );
144
145 sub get_account {
146     my $id = shift;
147
148     my $account = $schema->resultset('VendorEdiAccount')->find($id);
149     if ($account) {
150         return $account;
151     }
152
153     # passing undef will default to add
154     return;
155 }
156
157 sub show_account {
158     my $acct_id = $input->param('id');
159     if ($acct_id) {
160         my $acct = $schema->resultset('VendorEdiAccount')->find($acct_id);
161         if ($acct) {
162             $template->param( account => $acct );
163         }
164     }
165     return;
166 }