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