|
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 = GetMember(borrowernumber => $loggedinuser)->{userid} if $loggedinuser; |
| 56 |
my $i3 = 0; |
| 57 |
for ( my $i2 = 0 ; $i2 < $ordcount ; $i2++ ) { |
| 58 |
if ( $orders->[$i2]{'authorisedby'} == $loggedinuser || haspermission($uid, { flagsrequired => { 'acquisition' => '*' } } ) ) { |
| 59 |
my %inner_line; |
| 60 |
my @orders_items = GetOrders($orders->[$i2]{'basketno'}); |
| 61 |
$inner_line{basketno} = $orders->[$i2]{'basketno'}; |
| 62 |
$inner_line{basketname} = $orders->[$i2]{'basketname'}; |
| 63 |
$inner_line{total} = scalar @orders_items; |
| 64 |
$inner_line{authorisedby} = $orders->[$i2]{'authorisedby'}; |
| 65 |
my $authby = GetMember(borrowernumber => $orders->[$i2]{'authorisedby'}); |
| 66 |
$inner_line{surname} = $authby->{'firstname'}; |
| 67 |
$inner_line{firstname} = $authby->{'surname'}; |
| 68 |
$inner_line{creationdate} = format_date( $orders->[$i2]{'creationdate'} ); |
| 69 |
$inner_line{closedate} = format_date( $orders->[$i2]{'closedate'} ); |
| 70 |
$inner_line{uncertainprice} = $orders->[$i2]{'uncertainprice'}; |
| 71 |
$inner_line{orders_items} = \@orders_items; |
| 72 |
$inner_line{odd} = $i3 % 2; |
| 73 |
if ($inner_line{total} > 0) { |
| 74 |
push @loop_basket, \%inner_line; |
| 75 |
$i3++; |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
$line{loop_basket} = \@loop_basket; |
| 80 |
push @loop_suppliers, \%line; |
| 81 |
} |
| 82 |
|
| 83 |
#use Data::Dumper; |
| 84 |
#die Dumper \@loop_suppliers; |
| 85 |
|
| 86 |
my @letters; |
| 87 |
my $letters=GetLetters("rma"); |
| 88 |
foreach (keys %$letters){ |
| 89 |
push @letters, {code=>$_,name=>$letters->{$_}}; |
| 90 |
} |
| 91 |
$template->param(letters=>\@letters) if (@letters); |
| 92 |
$template->param(suppliers => \@loop_suppliers); |
| 93 |
$template->param(suppliername => $input->param('supplier')); |
| 94 |
} |
| 95 |
|
| 96 |
if ($op and $op eq "send_alert"){ |
| 97 |
my @ordernums = $input->param("generate_for"); |
| 98 |
my $output = $input->param("output"); |
| 99 |
if ($output eq "email"){ |
| 100 |
SendAlerts('rma',\@ordernums,$input->param("letter_code")); |
| 101 |
} |
| 102 |
else { |
| 103 |
|
| 104 |
my $dbh = C4::Context->dbh; |
| 105 |
my $basename = $input->param("basename"); |
| 106 |
my $sep = $input->param("sep"); |
| 107 |
print $input->header( |
| 108 |
-type => 'application/vnd.sun.xml.calc', |
| 109 |
-encoding => 'utf-8', |
| 110 |
-attachment => "$basename.csv", |
| 111 |
-name => "$basename.csv" |
| 112 |
); |
| 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 |
|
| 125 |
print "aqbooksellers.name" . $sep; |
| 126 |
print "aqbooksellers.address1" . $sep; |
| 127 |
print "aqbooksellers.address2" . $sep; |
| 128 |
print "aqbooksellers.address3" . $sep; |
| 129 |
print "aqbooksellers.address4" . $sep; |
| 130 |
print "aqbooksellers.phone" . $sep; |
| 131 |
print "aqorders.ordernumber" . $sep; |
| 132 |
print "biblioitems.isbn" . $sep; |
| 133 |
print "biblio.title" . $sep; |
| 134 |
print "biblio.author" . $sep; |
| 135 |
print "aqorders.quantity" . $sep; |
| 136 |
print "aqorders.listprice" . $sep; |
| 137 |
print "\n"; |
| 138 |
|
| 139 |
foreach my $data (@$dataorders) { |
| 140 |
print _surround($data->{name}) . $sep; |
| 141 |
print _surround($data->{address1}) . $sep; |
| 142 |
print _surround($data->{address2}) . $sep; |
| 143 |
print _surround($data->{address3}) . $sep; |
| 144 |
print _surround($data->{address4}) . $sep; |
| 145 |
print _surround($data->{phone}) . $sep; |
| 146 |
print _surround($data->{ordernumber}) . $sep; |
| 147 |
print _surround($data->{isbn}) . $sep; |
| 148 |
print _surround($data->{title}) . $sep; |
| 149 |
print _surround($data->{author}) . $sep; |
| 150 |
print _surround($data->{quantity}) . $sep; |
| 151 |
print _surround($data->{listprice}) . $sep; |
| 152 |
print "\n"; |
| 153 |
} |
| 154 |
|
| 155 |
exit(1); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
sub _surround { |
| 160 |
my $string = shift || ""; |
| 161 |
$string =~ s/"/""/g; |
| 162 |
return "\"$string\""; |
| 163 |
} |
| 164 |
|
| 165 |
my $CGIextChoice = CGI::scrolling_list( |
| 166 |
-name => 'MIME', |
| 167 |
-id => 'MIME', |
| 168 |
-values => ['CSV'], # FIXME translation |
| 169 |
-size => 1, |
| 170 |
-multiple => 0 |
| 171 |
); |
| 172 |
|
| 173 |
my $CGIsepChoice = C4::Reports::GetDelimiterChoices; |
| 174 |
|
| 175 |
$template->param(ERROR_LOOP => \@errors) if (@errors); |
| 176 |
$template->param( |
| 177 |
CGIextChoice => $CGIextChoice, |
| 178 |
CGIsepChoice => $CGIsepChoice, |
| 179 |
delay => $delay, |
| 180 |
intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"), |
| 181 |
); |
| 182 |
output_html_with_http_headers $input, $cookie, $template->output; |