Bug 12478 - add test cases
[koha.git] / admin / edi_ean_accounts.pl
1 #!/usr/bin/perl
2
3 # Copyright 2012, 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
27 my $input = CGI->new();
28
29 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
30     {
31         template_name   => 'admin/edi_ean_accounts.tt',
32         query           => $input,
33         type            => 'intranet',
34         authnotrequired => 0,
35         flagsrequired   => { acquisition => 'edi_manage' },
36     }
37 );
38
39 my $schema = Koha::Database->new()->schema();
40 my $op     = $input->param('op');
41 $op ||= 'display';
42
43 if ( $op eq 'ean_form' ) {
44     show_ean();
45     $template->param( ean_form => 1 );
46     my @branches = $schema->resultset('Branch')->search(
47         undef,
48         {
49             columns  => [ 'branchcode', 'branchname' ],
50             order_by => 'branchname',
51         }
52     );
53     $template->param( branches => \@branches );
54 }
55 elsif ( $op eq 'delete_confirm' ) {
56     show_ean();
57     $template->param( delete_confirm => 1 );
58 }
59 else {
60     if ( $op eq 'save' ) {
61         my $change = $input->param('id');
62         if ($change) {
63             editsubmit();
64         }
65         else {
66             addsubmit();
67         }
68     }
69     elsif ( $op eq 'delete_confirmed' ) {
70         delsubmit();
71     }
72     my @eans = $schema->resultset('EdifactEan')->search(
73         {},
74         {
75             join => 'branch',
76         }
77     );
78     $template->param( display => 1 );
79     $template->param( eans    => \@eans );
80 }
81
82 $template->param(
83     code_qualifiers => [
84         {
85             code        => '14',
86             description => 'EAN International',
87         },
88         {
89             code        => '31B',
90             description => 'US SAN Agency',
91         },
92         {
93             code        => '91',
94             description => 'Assigned by supplier',
95         },
96         {
97             code        => '92',
98             description => 'Assigned by buyer',
99         },
100     ]
101 );
102
103 output_html_with_http_headers( $input, $cookie, $template->output );
104
105 sub delsubmit {
106     my $id = $input->param('id');
107     my $e = $schema->resultset('EdifactEan')->find( $id );
108     $e->delete if $e;
109     return;
110 }
111
112 sub addsubmit {
113
114     my $new_ean = $schema->resultset('EdifactEan')->new(
115         {
116             branchcode        => $input->param('branchcode'),
117             description       => $input->param('description'),
118             ean               => $input->param('ean'),
119             id_code_qualifier => $input->param('id_code_qualifier'),
120         }
121     );
122     $new_ean->insert();
123     return;
124 }
125
126 sub editsubmit {
127     $schema->resultset('EdifactEan')->find( $input->param('id') )->update(
128         {
129             branchcode        => $input->param('branchcode'),
130             description       => $input->param('description'),
131             ean               => $input->param('ean'),
132             id_code_qualifier => $input->param('id_code_qualifier'),
133         }
134     );
135     return;
136 }
137
138 sub show_ean {
139     my $id = $input->param('id');
140     my $e = $schema->resultset('EdifactEan')->find( $id );
141     $template->param( ean => $e );
142     return;
143 }