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