Bug 10402: Use an object for contacts
[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 under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 2 of the License, or (at your option) any later
15 # version.
16 #
17 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License along
22 # with Koha; if not, write to the Free Software Foundation, Inc.,
23 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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_contact_name,
41 company_contact_position, contact_phone, contact_phone_2, contact_fax,
42 company_email, contact_notes, notes, status, publishers_imprints,
43 list_currency, gst, list_gst, invoice_gst, discount, gstrate.
44
45 =cut
46
47 use strict;
48 use warnings;
49 use List::Util;
50 use C4::Context;
51 use C4::Auth;
52
53 use C4::Bookseller qw( ModBookseller AddBookseller );
54 use C4::Bookseller::Contact;
55 use C4::Biblio;
56 use C4::Output;
57 use CGI;
58
59 my $input=new CGI;
60 my ($template, $loggedinuser, $cookie) = get_template_and_user(
61         {   template_name   => "",
62                 query           => $input,
63                 type            => "intranet",
64                 authnotrequired => 0,
65                 flagsrequired   => { acquisition => 'vendors_manage' },
66                 debug           => 1,
67         }
68 );
69
70 #print $input->header();
71 my $booksellerid=$input->param('booksellerid');
72 #print startpage;
73 my %data;
74 $data{'id'}=$booksellerid;
75
76 $data{'name'}=$input->param('company');
77 $data{'postal'}=$input->param('company_postal');
78 my $address=$input->param('physical');
79 my @addresses=split('\n',$address);
80 $data{'address1'}=$addresses[0];
81 $data{'address2'}=$addresses[1];
82 $data{'address3'}=$addresses[2];
83 $data{'address4'}=$addresses[3];
84 $data{'phone'}=$input->param('company_phone');
85 $data{'accountnumber'}=$input->param('accountnumber');
86 $data{'fax'}=$input->param('company_fax');
87 $data{'url'}=$input->param('website');
88 # warn "".$data{'contnotes'};
89 $data{'notes'}=$input->param('notes');
90 $data{'active'}=$input->param('status');
91
92 $data{'listprice'}=$input->param('list_currency');
93 $data{'invoiceprice'}=$input->param('invoice_currency');
94 $data{'gstreg'}=$input->param('gst');
95 $data{'listincgst'}=$input->param('list_gst');
96 $data{'invoiceincgst'}=$input->param('invoice_gst');
97 #have to transform this into fraction so it's easier to use
98 $data{'gstrate'} = $input->param('gstrate');
99 $data{'discount'} = $input->param('discount');
100 $data{deliverytime} = $input->param('deliverytime');
101 $data{'active'}=$input->param('status');
102 my @contacts;
103 my %contact_info;
104
105 foreach (qw(id name position phone altphone fax email notes)) {
106     $contact_info{$_} = [ $input->param('contact_' . $_) ];
107 }
108
109 for my $cnt (0..scalar(@{$contact_info{'id'}})) {
110     my %contact;
111     foreach (qw(id name position phone altphone fax email notes)) {
112         $contact{$_} = $contact_info{$_}->[$cnt];
113     }
114     push @contacts, C4::Bookseller::Contact->new(\%contact);
115 }
116
117 if($data{'name'}) {
118         if ($data{'id'}){
119         ModBookseller(\%data, \@contacts);
120         } else {
121         $data{id}=AddBookseller(\%data, \@contacts);
122         }
123     #redirect to booksellers.pl
124     print $input->redirect("booksellers.pl?booksellerid=".$data{id});
125 } else {
126     print $input->redirect("supplier.pl?op=enter"); # fail silently.
127 }