Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Creating a summary row of combined total bookings in SS_reports


Go to End


4 Posts   1878 Views

Avatar
lozhowlett

Community Member, 151 Posts

18 June 2014 at 1:00am

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: &pound;".$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>'
        );
    }
}

Avatar
martimiz

Forum Moderator, 1391 Posts

18 June 2014 at 1:55am

Thinking about this... How would you make that summary row stand out and maybe always be visible, even if the report consisted of many pages?

Wouldn't just creating a second report called total bookings be a whole lot easier? Or is that out of the question?

Martine

Avatar
lozhowlett

Community Member, 151 Posts

18 June 2014 at 1:58am

Thats out of the questions, needs to be a summary on this report, infact I would like to remove paging probably too.

Avatar
martimiz

Forum Moderator, 1391 Posts

19 June 2014 at 4:44am

Edited: 19/06/2014 4:45am

This might work as a starting point:

class BookingTotalSummary implements GridField_HTMLProvider {
	
	private $report;
	
	public function __construct($report) {
		$this->report = $report;
	}
	
	public function getHTMLFragments( $gridField) {
		
		$total = $this->report->getTotal();
		
		return array(
			'footer' => '
			<tr class="ss-gridfield-item last even" data-class="Booking" style="cursor: default;">
			<td class="col-Reference" colspan="6"></td>
			<td class="col-Total">'. $total .'</td></tr>'
		);
	}
}

and in your BookingReport_BookingSummary class:

	public function getReportField() {
		$gridField = parent::getReportField();
		
		$gridField->getConfig()->addComponent(new BookingTotalSummary($this), 'GridFieldPaginator');

		return $gridField;
	}

You'd have to style it, and I think it might always be there, even when paginating, which might look weird, since the paginator is underneath... In that case just do:

$gridField->getConfig()->addComponent(new BookingTotalSummary($this));

to get it underneath the footer. Hope this helps,