+#!/usr/bin/perl\r
+\r
+#things to do\r
+\r
+# First sort by branch\r
+#Then sort by surname\r
+\r
+#_Branch_: Could we have Levin displaying as L, please, not C__\r
+\r
+#_Totals_ :\r
+#*Total Paid *\r
+#*Total written off*\r
+#*Total credits (which will include manual credits and credits for lost books returned*\r
+\r
+#use strict;\r
+use CGI;\r
+use C4::Output;\r
+use HTML::Template;\r
+use C4::Auth;\r
+use C4::Interface::CGI::Output;\r
+use C4::Context;\r
+use Date::Manip;\r
+use C4::Stats;\r
+use Data::Dumper;\r
+\r
+use Text::CSV_XS;\r
+\r
+my $csv = Text::CSV_XS->new(\r
+ {\r
+ 'quote_char' => '"',\r
+ 'escape_char' => '"',\r
+ 'sep_char' => ',',\r
+ 'binary' => 1,\r
+ 'always_quote' => 1,\r
+ }\r
+);\r
+\r
+my $input=new CGI;\r
+\r
+#my $time=$input->param('time');\r
+#my $time="month";\r
+#my $time="today";\r
+\r
+my $date;\r
+my $date2;\r
+if ($time eq 'yesterday'){\r
+ $date=ParseDate('yesterday');\r
+ $date2=ParseDate('today');\r
+}\r
+if ($time eq 'today'){\r
+ $date=ParseDate('today');\r
+ $date2=ParseDate('tomorrow');\r
+}\r
+if ($time eq 'daybefore'){\r
+ $date=ParseDate('2 days ago');\r
+ $date2=ParseDate('yesterday');\r
+}\r
+if ($time eq 'month') {\r
+ $date = ParseDate('1 month ago');\r
+ $date2 = ParseDate('today');\r
+\r
+}\r
+if ($time=~ /\//){\r
+ $date=ParseDate($time);\r
+ $date2=ParseDateDelta('+ 1 day');\r
+ $date2=DateCalc($date,$date2);\r
+}\r
+\r
+#my $date=UnixDate($date,'%Y-%m-%d');\r
+#my $date2=UnixDate($date2,'%Y-%m-%d');\r
+\r
+my $date="2005-08-19";\r
+my $date2="2005-08-20";\r
+\r
+#my $date="2005-01-05";\r
+#my $date2="2005-01-06";\r
+\r
+#get a list of every payment\r
+my @payments=TotalPaid($date,$date2);\r
+\r
+my $count=@payments;\r
+# print "MASON: number of payments=$count\n";\r
+\r
+my $i=0;\r
+my $totalcharges=0;\r
+my $totalcredits=0;\r
+my $totalpaid=0;\r
+my $totalwritten=0;\r
+\r
+# lets get a a list of all individual item charges paid for by that payment\r
+while ($i<$count ){\r
+\r
+ my $count;\r
+ my @charges;\r
+\r
+ if ($payments[$i]{'type'} ne 'writeoff'){ # lets ignore writeoff payments!.\r
+ @charges=getcharges($payments[$i]{'borrowernumber'}, $payments[$i]{'timestamp'}, $payments[$i]{'proccode'});\r
+ $totalcharges++;\r
+ $count=@charges;\r
+ # getting each of the charges and putting them into a array to be printed out\r
+ #this loops per charge per person\r
+ for (my $i2=0;$i2<$count;$i2++){\r
+ my $hour=substr($payments[$i]{'timestamp'},8,2);\r
+ my $min=substr($payments[$i]{'timestamp'},10,2);\r
+ my $sec=substr($payments[$i]{'timestamp'},12,2);\r
+ my $time="$hour:$min:$sec";\r
+ my $time2="$payments[$i]{'date'}";\r
+ my $branch=Getpaidbranch($time2,$payments[$i]{'borrowernumber'});\r
+ my @rows1 = ($branch, # lets build up a row\r
+ $payments[$i]->{'datetime'},\r
+ $payments[$i]->{'surname'},\r
+ $payments[$i]->{'firstname'},\r
+ $charges[$i2]->{'description'},\r
+ $charges[$i2]->{'accounttype'},\r
+ sprintf("%.2f", $charges[$i2]->{'amount'}), # rounding amounts to 2dp\r
+ $payments[$i]->{'type'},\r
+ $payments[$i]->{'value'});\r
+ push (@loop1, \@rows1);\r
+ }\r
+ } else {\r
+ ++$totalwritten;\r
+ }\r
+ $i++; #increment the while loop\r
+ $totalpaid = $totalpaid + $payments[$i]->{'value'};\r
+}\r
+\r
+\r
+\r
+#get credits and append to the bottom of payments\r
+my @credits=getcredits($date,$date2);\r
+\r
+#print Dumper(@credits);\r
+\r
+my $count=@credits;\r
+my $i=0;\r
+\r
+while ($i<$count ){\r
+\r
+ my @rows2 = ($credits[$i]->{'branchcode'},\r
+ $credits[$i]->{'date'},\r
+ $credits[$i]->{'surname'},\r
+ $credits[$i]->{'firstname'},\r
+ $credits[$i]->{'description'},\r
+ $credits[$i]->{'accounttype'},\r
+ $credits[$i]->{'amount'});\r
+\r
+ push (@loop2, \@rows2);\r
+ $i++; #increment the while loop\r
+ $totalcredits = $totalcredits + $credits[$i]->{'amount'};\r
+ ;\r
+\r
+}\r
+#takes off first char minus sign "-100.00"\r
+\r
+\r
+$totalcredits = substr($totalcredits, 1);\r
+\r
+\r
+print $input->header(\r
+ -type => 'application/vnd.ms-excel',\r
+ -attachment => "moo.csv",\r
+);\r
+print "Branch, Datetime, Surame, Firstnames, Description, Type, Invoice amount, Payment type, Payment Amount\n";\r
+\r
+\r
+for my $row ( @loop1 ) {\r
+\r
+ $csv->combine(@$row);\r
+ my $string = $csv->string;\r
+ print $string, "\n";\r
+}\r
+\r
+print ",,,,,,,\n";\r
+\r
+for my $row ( @loop2 ) {\r
+\r
+ $csv->combine(@$row);\r
+ my $string = $csv->string;\r
+ print $string, "\n";\r
+}\r
+\r
+print ",,,,,,,\n";\r
+print ",,,,,,,\n";\r
+print ",,Total Amount Paid, $totalpaid\n";\r
+print ",,Total Number Written, $totalwritten\n";\r
+print ",,Total Amount Credits, $totalcredits\n";\r
+\r