-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);
-
-output_html_with_http_headers $input, $cookie, $template->output;
-
+my $totalcash = $totalpaid - $totalrefunds;
+
+if ( $op eq 'To Excel' ) {
+
+ my $csv = Text::CSV_XS->new(
+ {
+ 'quote_char' => '"',
+ 'escape_char' => '"',
+ 'sep_char' => ',',
+ 'binary' => 1
+ }
+ );
+
+ 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;
+}