Line 0
Link Here
|
|
|
1 |
#!/usr/bin/perl |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
use CGI; |
6 |
use C4::Bookseller; |
7 |
use C4::Auth; |
8 |
use C4::Koha; |
9 |
use C4::Output; |
10 |
use C4::Context; |
11 |
use C4::Acquisition; |
12 |
use C4::Letters; |
13 |
use C4::Branch; # GetBranches |
14 |
use C4::Members qw/GetMember/; |
15 |
use C4::Dates qw/format_date/; |
16 |
use C4::Reports; |
17 |
|
18 |
my $input = new CGI; |
19 |
my ($template, $loggedinuser, $cookie) = get_template_and_user({ |
20 |
template_name => "acqui/returnorder.tt", |
21 |
query => $input, |
22 |
type => "intranet", |
23 |
authnotrequired => 0, |
24 |
flagsrequired => {acquisition => 'order_receive'}, |
25 |
debug => 1, |
26 |
}); |
27 |
|
28 |
my $supplierid = $input->param('supplierid') || undef; # we don't want "" or 0 |
29 |
my $delay = $input->param('delay'); |
30 |
my $branch = $input->param('branch'); |
31 |
my $op = $input->param('op'); |
32 |
|
33 |
my @errors = (); |
34 |
$delay = 30 unless defined $delay; |
35 |
unless ($delay =~ /^\d{1,3}$/) { |
36 |
push @errors, {delay_digits => 1, bad_delay => $delay}; |
37 |
$delay = 30; #default value for delay |
38 |
} |
39 |
|
40 |
if ($op and $op eq "findsupplier") |
41 |
{ |
42 |
my $supplier = $input->param('supplier'); |
43 |
my @suppliers = C4::Bookseller::GetBookSeller($supplier); |
44 |
my $count = scalar @suppliers; |
45 |
my @loop_suppliers; |
46 |
for ( my $i = 0 ; $i < $count ; $i++ ) { |
47 |
my $orders = GetBasketsByBookseller( $suppliers[$i]->{'id'}, {groupby => "aqbasket.basketno", orderby => "aqbasket.basketname"} ); |
48 |
my $ordcount = scalar @$orders; |
49 |
my %line; |
50 |
|
51 |
$line{supplierid} = $suppliers[$i]->{'id'}; |
52 |
$line{name} = $suppliers[$i]->{'name'}; |
53 |
$line{active} = $suppliers[$i]->{'active'}; |
54 |
my @loop_basket; |
55 |
my $uid; |
56 |
$uid = GetMember(borrowernumber => $loggedinuser)->{userid} if $loggedinuser; |
57 |
my $i3 = 0; |
58 |
for ( my $i2 = 0 ; $i2 < $ordcount ; $i2++ ) { |
59 |
if ( $orders->[$i2]{'authorisedby'} == $loggedinuser || haspermission($uid, { flagsrequired => { 'acquisition' => '*' } } ) ) { |
60 |
my %inner_line; |
61 |
my @orders_items = GetOrders($orders->[$i2]{'basketno'}); |
62 |
$inner_line{basketno} = $orders->[$i2]{'basketno'}; |
63 |
$inner_line{basketname} = $orders->[$i2]{'basketname'}; |
64 |
$inner_line{total} = scalar @orders_items; |
65 |
$inner_line{authorisedby} = $orders->[$i2]{'authorisedby'}; |
66 |
my $authby = GetMember(borrowernumber => $orders->[$i2]{'authorisedby'}); |
67 |
$inner_line{surname} = $authby->{'firstname'}; |
68 |
$inner_line{firstname} = $authby->{'surname'}; |
69 |
$inner_line{creationdate} = format_date( $orders->[$i2]{'creationdate'} ); |
70 |
$inner_line{closedate} = format_date( $orders->[$i2]{'closedate'} ); |
71 |
$inner_line{uncertainprice} = $orders->[$i2]{'uncertainprice'}; |
72 |
$inner_line{orders_items} = \@orders_items; |
73 |
$inner_line{odd} = $i3 % 2; |
74 |
if ($inner_line{total} > 0) { |
75 |
push @loop_basket, \%inner_line; |
76 |
$i3++; |
77 |
} |
78 |
} |
79 |
} |
80 |
$line{loop_basket} = \@loop_basket; |
81 |
push @loop_suppliers, \%line; |
82 |
} |
83 |
|
84 |
#use Data::Dumper; |
85 |
#die Dumper \@loop_suppliers; |
86 |
|
87 |
my @letters; |
88 |
my $letters=GetLetters({module=>'rma',code=>'RMA'}); |
89 |
foreach (@$letters){ |
90 |
push @letters, {code=>$_->{code},name=>$_->{name}}; |
91 |
} |
92 |
$template->param(letters=>\@letters) if (@letters); |
93 |
$template->param(suppliers => \@loop_suppliers); |
94 |
$template->param(suppliername => $input->param('supplier')); |
95 |
} |
96 |
|
97 |
if ($op and $op eq "send_alert"){ |
98 |
my @ordernums = $input->param("generate_for"); |
99 |
my $output = $input->param("output"); |
100 |
if (@ordernums){ |
101 |
if ($output eq "email"){ |
102 |
SendAlerts('rma',\@ordernums,$input->param("letter_code")); |
103 |
} |
104 |
else { |
105 |
my $dbh = C4::Context->dbh; |
106 |
my $basename = $input->param("basename"); |
107 |
my $sep = $input->param("sep"); |
108 |
print $input->header( |
109 |
-type => 'application/vnd.sun.xml.calc', |
110 |
-encoding => 'utf-8', |
111 |
-attachment => "$basename.csv", |
112 |
-name => "$basename.csv" |
113 |
); |
114 |
my $strsth = "SELECT aqorders.*,aqbasket.*,biblio.*,biblioitems.*, bs.* |
115 |
FROM aqorders |
116 |
LEFT JOIN aqbasket on aqbasket.basketno=aqorders.basketno |
117 |
LEFT JOIN biblio on aqorders.biblionumber=biblio.biblionumber |
118 |
LEFT JOIN biblioitems on aqorders.biblionumber=biblioitems.biblionumber |
119 |
JOIN aqbooksellers bs on bs.id = booksellerid |
120 |
where aqorders.ordernumber IN (". join( ",", @ordernums ) . ")"; |
121 |
my $sthorders = $dbh->prepare($strsth); |
122 |
$sthorders->execute; |
123 |
my $dataorders = $sthorders->fetchall_arrayref( {} ); |
124 |
print "aqbooksellers.name" . $sep; |
125 |
print "aqbooksellers.address1" . $sep; |
126 |
print "aqbooksellers.address2" . $sep; |
127 |
print "aqbooksellers.address3" . $sep; |
128 |
print "aqbooksellers.address4" . $sep; |
129 |
print "aqbooksellers.phone" . $sep; |
130 |
print "aqorders.ordernumber" . $sep; |
131 |
print "biblioitems.isbn" . $sep; |
132 |
print "biblio.title" . $sep; |
133 |
print "biblio.author" . $sep; |
134 |
print "aqorders.quantity" . $sep; |
135 |
print "aqorders.listprice" . $sep; |
136 |
print "\n"; |
137 |
foreach my $data (@$dataorders) { |
138 |
print _surround($data->{name}) . $sep; |
139 |
print _surround($data->{address1}) . $sep; |
140 |
print _surround($data->{address2}) . $sep; |
141 |
print _surround($data->{address3}) . $sep; |
142 |
print _surround($data->{address4}) . $sep; |
143 |
print _surround($data->{phone}) . $sep; |
144 |
print _surround($data->{ordernumber}) . $sep; |
145 |
print _surround($data->{isbn}) . $sep; |
146 |
print _surround($data->{title}) . $sep; |
147 |
print _surround($data->{author}) . $sep; |
148 |
print _surround($data->{quantity}) . $sep; |
149 |
print _surround($data->{listprice}) . $sep; |
150 |
print "\n"; |
151 |
} |
152 |
|
153 |
exit(1); |
154 |
} |
155 |
} |
156 |
} |
157 |
|
158 |
sub _surround { |
159 |
my $string = shift || ""; |
160 |
$string =~ s/"/""/g; |
161 |
return "\"$string\""; |
162 |
} |
163 |
|
164 |
my $CGIextChoice = CGI::scrolling_list( |
165 |
-name => 'MIME', |
166 |
-id => 'MIME', |
167 |
-values => ['CSV'], # FIXME translation |
168 |
-size => 1, |
169 |
-multiple => 0 |
170 |
); |
171 |
|
172 |
my $CGIsepChoice = C4::Reports::GetDelimiterChoices; |
173 |
|
174 |
$template->param(ERROR_LOOP => \@errors) if (@errors); |
175 |
$template->param( |
176 |
CGIextChoice => $CGIextChoice, |
177 |
CGIsepChoice => $CGIsepChoice, |
178 |
delay => $delay, |
179 |
intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"), |
180 |
); |
181 |
output_html_with_http_headers $input, $cookie, $template->output; |