Bug 33568: Fix columns shift when pref are off
[koha.git] / acqui / updatesupplier.pl
1 #!/usr/bin/perl
2
3 #script to show suppliers and orders
4 #written by chris@katipo.co.nz 23/2/2000
5
6 # Copyright 2000-2002 Katipo Communications
7 # Copyright 2008-2009 BibLibre SARL
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it
12 # under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 3 of the License, or
14 # (at your option) any later version.
15 #
16 # Koha is distributed in the hope that it will be useful, but
17 # WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23
24 =head1 NAME
25
26 updatesupplier.pl
27
28 =head1 DESCRIPTION
29
30 this script allow to update or create (if id == 0)
31 a supplier. This script is called from acqui/supplier.pl.
32
33 =head1 CGI PARAMETERS
34
35 All informations regarding this supplier are listed on input parameter.
36 Here is the list :
37
38 supplier, id, company, company_postal, physical, company_phone,
39 physical, company_phone, company_fax, website, company_email, notes,
40 status, publishers_imprints, list_currency, gst, list_gst, invoice_gst,
41 discount, tax_rate, contact_name, contact_position, contact_phone,
42 contact_altphone, contact_fax, contact_email, contact_notes,
43 contact_claimacquisition, contact_claimissues, contact_acqprimary,
44 contact_serialsprimary.
45
46 =cut
47
48 use Modern::Perl;
49 use List::MoreUtils qw( any );
50 use C4::Context;
51 use C4::Auth qw( checkauth );
52
53 use C4::Output;
54
55 use Koha::Acquisition::Bookseller::Contacts;
56 use Koha::Acquisition::Booksellers;
57 use CGI qw ( -utf8 );
58
59 my $input = CGI->new;
60
61 checkauth( $input, 0, { acquisition => 'vendors_manage' }, 'intranet' );
62
63 my $op = $input->param('op') // q{};
64
65 my $booksellerid = $input->param('booksellerid');
66
67 my $address   = $input->param('physical');
68 my @addresses = split( '\n', $address );
69
70 my %data;
71 $data{'id'} = $booksellerid;
72
73 $data{'name'}          = $input->param('company');
74 $data{'postal'}        = $input->param('company_postal');
75 $data{'address1'}      = $addresses[0];
76 $data{'address2'}      = $addresses[1];
77 $data{'address3'}      = $addresses[2];
78 $data{'address4'}      = $addresses[3];
79 $data{'phone'}         = $input->param('company_phone');
80 $data{'accountnumber'} = $input->param('accountnumber');
81 $data{'type'}          = $input->param('vendor_type');
82 $data{'fax'}           = $input->param('company_fax');
83 $data{'url'}           = $input->param('website');
84 $data{'notes'}         = $input->param('notes');
85 $data{'active'}        = $input->param('status');
86 $data{'listprice'}     = $input->param('list_currency');
87 $data{'invoiceprice'}  = $input->param('invoice_currency');
88 $data{'gstreg'}        = $input->param('gst');
89 $data{'listincgst'}    = $input->param('list_gst');
90 $data{'invoiceincgst'} = $input->param('invoice_gst');
91 $data{'tax_rate'}      = $input->param('tax_rate');          #have to transform this into fraction so it's easier to use
92 $data{'discount'}      = $input->param('discount');
93 $data{deliverytime}    = $input->param('deliverytime');
94 $data{'active'}        = $input->param('status');
95
96 my @aliases = $input->multi_param('alias');
97 my @contacts;
98 my %contact_info;
99
100 foreach (
101     qw(id name position phone altphone fax email notes orderacquisition claimacquisition claimissues acqprimary serialsprimary)
102     )
103 {
104     $contact_info{$_} = [ $input->multi_param( 'contact_' . $_ ) ];
105 }
106
107 for my $cnt ( 0 .. scalar( @{ $contact_info{'id'} } ) ) {
108     my %contact;
109     my $real_contact;
110     foreach (
111         qw(id name position phone altphone fax email notes orderacquisition claimacquisition claimissues acqprimary serialsprimary)
112         )
113     {
114         $contact{$_} = $contact_info{$_}->[$cnt];
115         $real_contact = 1 if $contact{$_};
116     }
117     push @contacts, \%contact if $real_contact;
118 }
119
120 if ( $op eq 'cud-add' ) {
121     my $bookseller;
122     if ( $data{id} ) {
123
124         # Update
125         $bookseller = Koha::Acquisition::Booksellers->find( $data{id} )->set( \%data )->store;
126
127         # Delete existing contacts
128         $bookseller->contacts->delete;
129     } else {
130
131         # Insert
132         delete $data{id};    # Remove the key if exists
133         $bookseller = Koha::Acquisition::Bookseller->new( \%data )->store;
134         $data{id} = $bookseller->id;
135     }
136
137     # Insert contacts
138     for my $contact (@contacts) {
139         $contact->{booksellerid} = $data{id};
140         Koha::Acquisition::Bookseller::Contact->new($contact)->store;
141     }
142
143     # Insert aliases
144     $bookseller->aliases( [ map { { alias => $_ } } @aliases ] );
145
146     # Insert interfaces
147     my @interface_counters = $input->multi_param('interface_counter');
148     my @interfaces;
149     for my $counter (@interface_counters) {
150         my $interface = {};
151         for my $attr (qw(name type uri login password account_email notes)) {
152             my $v = $input->param("interface_${attr}_${counter}");
153             $interface->{$attr} = $v;
154         }
155         push @interfaces, $interface if any { defined && length } values %$interface;
156     }
157     $bookseller->interfaces( \@interfaces );
158
159     #redirect to booksellers.pl
160     print $input->redirect( "booksellers.pl?booksellerid=" . $data{id} );
161 } else {
162     print $input->redirect("supplier.pl?op=enter");    # fail silently.
163 }