Hi All
I am trying to create a report that is date filtered of bookings. That part I have completed. However I need to amend GridField so that the last row is a summary of the total combined booking value of the filtered results.
I.e
Row1 / £10
Row2 / £20
---- GridField Footer --
Total / £30
So far this is my code... but I am getting nowhere! Help :)
<?php
class BookingReport_BookingSummary extends SS_Report {
public function title() {
return _t('BookingSummaryReport.Title',"Booking Summary Report");
}
public function description(){
return "A date ranged report of bookings";
}
public function parameterFields() {
$dateFrom = new DateField('dateFrom', 'Date From');
$dateTo = new DateField('dateTo', 'Date To');
return new FieldList(
new DropdownField('BookingStatusID', "Booking Status", array(
'4' => 'Paid',
'1' => 'Not Paid',
'2' => 'Cancelled'
)),
$dateFrom->setConfig('showcalendar', true),
$dateTo->setConfig('showcalendar', true)
);
}
public function sourceRecords($params, $sort, $limit) {
$ret = Booking::get();
if($params["BookingStatusID"]){
$ret = $ret->filter(array("BookingStatusID"=>$params["BookingStatusID"]));
}
if($params["dateFrom"]){
$dateFrom = new DateTime($params["dateFrom"]);
$ret = $ret->where("Created>='".$dateFrom->format("Y-m-d")." 00:00:00'");
}
if($params["dateTo"]){
$dateTo = new DateTime($params["dateTo"]);
$ret = $ret->where("Created<='".$dateTo->format("Y-m-d")." 23:59:59'");
}
$returnSet = new ArrayList();
if ($ret) foreach($ret as $record) {
$returnSet->push($record);
}
return $returnSet;
}
public function columns() {
$fields = array(
"Reference" => array(
"title" => 'Reference',
'formatting' => sprintf(
'<a href=\"/admin/Bookingmanagement/Booking/EditForm/field/Booking/item/$ID/edit\" title=\"%s\">$value</a>',
'View Booking'
)
),
"Created.Long" => array('title' => "Date"),
"LeadPassenger.FirstName" => array('title' => "First Name"),
"LeadPassenger.LastName" => array('title' => "Last Name"),
"LeadPassenger.EmailAddress" => array('title' => "Email"),
"LeadPassenger.MobileNumber" => array('title' => "Telephone"),
"Total" => array('title' => "Total")
);
return $fields;
}
public function getTotal(){
$ret = Booking::get();
if($_GET["BookingStatusID"]){
$ret = $ret->filter(array("BookingStatusID"=>$params["BookingStatusID"]));
}
if($_GET["dateFrom"]){
$dateFrom = new DateTime($_GET["dateFrom"]);
$ret = $ret->where("Created>='".$dateFrom->format("Y-m-d")." 00:00:00'");
}
if($_GET["dateTo"]){
$dateTo = new DateTime($_GET["dateTo"]);
$ret = $ret->where("Created<='".$dateTo->format("Y-m-d")." 23:59:59'");
}
$returnSet = new ArrayList();
$Total = 0;
if ($ret) foreach($ret as $record) {
$Total = $Total + $record->Total;
}
return $Total;
}
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->push(new LiteralField('ReportDescription', "<h3 style=\"float:right\">Total Value: £".$this->getTotal()."</h3>"));
$this->extend('updateCMSFields', $fields);
return $fields;
}
}
class BookingTotalSummary implements GridField_HTMLProvider {
public function getHTMLFragments( $gridField) {
return array(
'footer' => '<tr class="ss-gridfield-item last even" data-id="500032" data-class="Booking" style="cursor: default;">
<td class="col-Reference" colspan="5"></td>
<td class="col-Total">'.$this->getTotal().'</td></tr>'
);
}
}