X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=reports%2Fstats.screen.pl;h=1d3eaef332b0b43f11b368deda34e1c43677d3ef;hb=d8aa557fc92a7030eb9628dc5fcce3fef99046dc;hp=a7161994d9803e3e1766470bc312e56419a22f17;hpb=100e6a9808ead4ee8d951da59ead1550e75bb4c3;p=koha_gimpoz diff --git a/reports/stats.screen.pl b/reports/stats.screen.pl index a7161994d9..1d3eaef332 100755 --- a/reports/stats.screen.pl +++ b/reports/stats.screen.pl @@ -16,86 +16,48 @@ # Suite 330, Boston, MA 02111-1307 USA use strict; +#use warnings; FIXME - Bug 2505 use CGI; use C4::Output; use C4::Auth; use C4::Context; -use Date::Manip; use C4::Stats; - -&Date_Init("DateFormat=non-US"); # set non-USA date, eg:19/08/2005 +use C4::Accounts; +use C4::Debug; +use Date::Manip; my $input = new CGI; my $time = $input->param('time'); my $time2 = $input->param('time2'); - -if ( $input->param('submit') eq "To Excel" - || $input->param('submit_x') eq "To Excel" ) -{ - print $input->redirect( - "/cgi-bin/koha/stats.print.pl?time=$time&time2=$time2"); -} +my $op = $input->param('submit'); my ( $template, $loggedinuser, $cookie ) = get_template_and_user( { - template_name => "stats_screen.tmpl", + template_name => "reports/stats_screen.tmpl", query => $input, type => "intranet", authnotrequired => 1, - flagsrequired => { reports => 1 }, + flagsrequired => { reports => '*' }, debug => 1, } ); -my $date; -my $date2; -if ( $time eq 'yesterday' ) { - $date = ParseDate('yesterday'); - $date2 = ParseDate('today'); -} -if ( $time eq 'today' ) { - $date = ParseDate('today'); - $date2 = ParseDate('tomorrow'); -} -if ( $time eq 'daybefore' ) { - $date = ParseDate('2 days ago'); - $date2 = ParseDate('yesterday'); -} -if ( $time eq 'month' ) { - $date = ParseDate('1 month ago'); - $date2 = ParseDate('today'); +( $time = "today" ) if !$time; +( $time2 = "tomorrow" ) if !$time2; -} -if ( $time =~ /\// ) { - $date = ParseDate($time); - $date2 = ParseDateDelta('+ 1 day'); - $date2 = DateCalc( $date, $date2 ); -} - -# if time is blank -if ( $time eq '' ) { - $date = ParseDate('today'); - $date2 = ParseDate('tomorrow'); -} - -# if script is called with a start and finsh date range... -if ( $time ne '' && $time2 ne '' ) { - $date = ParseDate($time); - $date2 = ParseDate($time2); -} - -my $date = UnixDate( $date, '%Y-%m-%d' ); -my $date2 = UnixDate( $date2, '%Y-%m-%d' ); - -# warn "MASON: TIME: $time, $time2"; -# warn "MASON: DATE: $date, $date2"; +my $date = ParseDate($time); +my $date2 = ParseDate($time2); +$date = UnixDate( $date, '%Y-%m-%d' ); +$date2 = UnixDate( $date2, '%Y-%m-%d' ); +$debug and warn "MASON: TIME: $time, $time2"; +$debug and warn "MASON: DATE: $date, $date2"; # get a list of every payment -my @payments = TotalPaid( $date, $date2, 0 ); +my @payments = TotalPaid( $date, $date2 ); my $count = @payments; -# print "MASON: number of payments=$count\n"; +$debug and warn "MASON: number of payments=$count\n"; my $i = 0; my $totalcharges = 0; @@ -106,59 +68,56 @@ my @loop1; my @loop2; # lets get a a list of all individual item charges paid for by that payment -while ( $i < $count ) { - my $count; - my @charges; +foreach my $payment (@payments) { - if ( $payments[$i]{'type'} ne 'writeoff' ) { + my @charges; + if ( $payment->{'type'} ne 'writeoff' ) { @charges = getcharges( - $payments[$i]{'borrowernumber'}, - $payments[$i]{'timestamp'}, - $payments[$i]{'proccode'} + $payment->{'borrowernumber'}, + $payment->{'timestamp'}, + $payment->{'proccode'} ); $totalcharges++; - $count = @charges; + my $count = @charges; # getting each of the charges and putting them into a array to be printed out #this loops per charge per person for ( my $i2 = 0 ; $i2 < $count ; $i2++ ) { - my $hour = substr( $payments[$i]{'timestamp'}, 8, 2 ); - my $min = substr( $payments[$i]{'timestamp'}, 10, 2 ); - my $sec = substr( $payments[$i]{'timestamp'}, 12, 2 ); + my $hour = substr( $payment->{'timestamp'}, 8, 2 ); + my $min = substr( $payment->{'timestamp'}, 10, 2 ); + my $sec = substr( $payment->{'timestamp'}, 12, 2 ); my $time = "$hour:$min:$sec"; - my $time2 = "$payments[$i]{'date'}"; + my $time2 = "$payment->{'date'}"; -# my $branch=Getpaidbranch($time2,$payments[$i]{'borrowernumber'}); - my $branch = $payments[$i]{'branch'}; + # my $branch=Getpaidbranch($time2,$payment->{'borrowernumber'}); + my $branch = $payment->{'branch'}; -# if ($payments[$i]{'borrowernumber'} == 18265){ -# warn "$payments[$i]{'branch'} $branch $payments[$i]{'borrowernumber'}";# -# } -# lets build up a row + # lets build up a row my %rows1 = ( branch => $branch, - datetime => $payments[$i]->{'datetime'}, - surname => $payments[$i]->{'surname'}, - firstname => $payments[$i]->{'firstname'}, + datetime => $payment->{'datetime'}, + surname => $payment->{'surname'}, + firstname => $payment->{'firstname'}, description => $charges[$i2]->{'description'}, accounttype => $charges[$i2]->{'accounttype'}, amount => sprintf( "%.2f", $charges[$i2]->{'amount'} ) , # rounding amounts to 2dp - type => $payments[$i]->{'type'}, - value => sprintf( "%.2f", $payments[$i]->{'value'} ) + type => $payment->{'type'}, + value => sprintf( "%.2f", $payment->{'value'} ) ); # rounding amounts to 2dp push( @loop1, \%rows1 ); - $totalpaid = $totalpaid + $payments[$i]->{'value'}; + } + $totalpaid = $totalpaid + $payment->{'value'}; + $debug and warn "totalpaid = $totalpaid"; } else { ++$totalwritten; } - $i++; #increment the while loop } #get credits and append to the bottom of payments @@ -195,9 +154,9 @@ $i = 0; while ( $i < $count ) { - my %rows2 = ( + my %rows3 = ( refundbranch => $refunds[$i]->{'branchcode'}, - refunddate => $refunds[$i]->{'date'}, + refunddate => $refunds[$i]->{'datetime'}, refundsurname => $refunds[$i]->{'surname'}, refundfirstname => $refunds[$i]->{'firstname'}, refunddescription => $refunds[$i]->{'description'}, @@ -205,25 +164,102 @@ while ( $i < $count ) { refundamount => sprintf( "%.2f", $refunds[$i]->{'amount'} ) ); - push( @loop3, \%rows2 ); + push( @loop3, \%rows3 ); $totalrefunds = $totalrefunds + $refunds[$i]->{'amount'}; $i++; #increment the while loop } my $totalcash = $totalpaid - $totalrefunds; -$template->param( - date => $time, - date2 => $time2, - loop1 => \@loop1, - loop2 => \@loop2, - loop3 => \@loop3, - totalpaid => $totalpaid, - totalcredits => $totalcredits, - totalwritten => $totalwritten, - totalrefund => $totalrefunds, - totalcash => $totalcash -); +if ( $op eq 'To Excel' ) { + + my $csv = Text::CSV_XS->new( + { + 'quote_char' => '"', + 'escape_char' => '"', + 'sep_char' => ',', + 'binary' => 1 + } + ); -output_html_with_http_headers $input, $cookie, $template->output; + print $input->header( + -type => 'application/vnd.ms-excel', + -attachment => "stats.csv", + ); + print +"Branch, Datetime, Surname, Firstnames, Description, Type, Invoice amount, Payment type, Payment Amount\n"; + + $DB::single = 1; + + for my $row (@loop1) { + my @array = ( + $row->{'branch'}, $row->{'datetime'}, + $row->{'surname'}, $row->{'firstname'}, + $row->{'description'}, $row->{'accounttype'}, + $row->{'amount'}, $row->{'type'}, + $row->{'value'} + ); + + $csv->combine(@array); + my $string = $csv->string(@array); + print $string, "\n"; + } + print ",,,,,,,\n"; + print +"Branch, Date/time, Surname, Firstname, Description, Charge Type, Invoice Amount\n"; + + for my $row (@loop2) { + + my @array = ( + $row->{'creditbranch'}, $row->{'creditdate'}, + $row->{'creditsurname'}, $row->{'creditfirstname'}, + $row->{'creditdescription'}, $row->{'creditaccounttype'}, + $row->{'creditamount'} + ); + + $csv->combine(@array); + my $string = $csv->string(@array); + print $string, "\n"; + } + print ",,,,,,,\n"; + print +"Branch, Date/time, Surname, Firstname, Description, Charge Type, Invoice Amount\n"; + + for my $row (@loop3) { + my @array = ( + $row->{'refundbranch'}, $row->{'refunddate'}, + $row->{'refundsurname'}, $row->{'refundfirstname'}, + $row->{'refunddescription'}, $row->{'refundaccounttype'}, + $row->{'refundamount'} + ); + + $csv->combine(@array); + my $string = $csv->string(@array); + print $string, "\n"; + + } + + print ",,,,,,,\n"; + print ",,,,,,,\n"; + print ",,Total Amount Paid, $totalpaid\n"; + print ",,Total Number Written, $totalwritten\n"; + print ",,Total Amount Credits, $totalcredits\n"; + print ",,Total Amount Refunds, $totalrefunds\n"; +} +else { + $template->param( + date => $time, + date2 => $time2, + loop1 => \@loop1, + loop2 => \@loop2, + loop3 => \@loop3, + totalpaid => $totalpaid, + totalcredits => $totalcredits, + totalwritten => $totalwritten, + totalrefund => $totalrefunds, + totalcash => $totalcash, + DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(), + ); + output_html_with_http_headers $input, $cookie, $template->output; +}