'Waybill
@param r: the S3Request instance
@param attr: controller attributes'

def waybill(self, r, **attr):

    T = current.T
    s3db = current.s3db
    s3db.add_components('inv_send', inv_warehouse=({'name': 'origin', 'joinby': 'site_id', 'pkey': 'site_id', 'filterby': False, 'multiple': False}, {'name': 'destination', 'joinby': 'site_id', 'pkey': 'to_site_id', 'filterby': False, 'multiple': False}))
    resource = s3db.resource(r.tablename, id=r.id, components=['origin', 'destination', 'track_item'])
    header_fields = ['send_ref', 'req_ref', 'date', 'delivery_date', (T('Origin'), 'site_id'), (T('Destination'), 'to_site_id'), 'sender_id', 'origin.contact', 'recipient_id', 'destination.contact', 'transported_by', 'transport_ref', (T('Delivery    Address'), 'destination.location_id'), 'comments']
    header_data = resource.select(header_fields, start=0, limit=1, represent=True, show_links=False, raw_data=True)
    if (not header_data):
        r.error(404, current.ERROR.BAD_RECORD)
    pdf_header = self.waybill_header(header_data)
    header_row = header_data.rows[0]
    pdf_filename = header_row['_row']['inv_send.send_ref']
    component = resource.components['track_item']
    body_fields = ['bin', 'item_id', 'item_pack_id', 'quantity', (T('Total    Volume    (m3)'), 'total_volume'), (T('Total    Weight    (kg)'), 'total_weight'), 'supply_org_id', 'inv_item_status']
    component.configure(extra_fields=['item_id$weight', 'item_id$volume'])
    aggregate = [('sum', 'inv_track_item.quantity'), ('sum', 'inv_track_item.total_volume'), ('sum', 'inv_track_item.total_weight')]
    json_data = self._json_data(component, body_fields, aggregate=aggregate)
    from s3 import S3GroupedItemsTable
    output = S3GroupedItemsTable(component, data=json_data, totals_label=T('Total'), title=T('Waybill'), pdf_header=pdf_header, pdf_footer=self.waybill_footer)
    return output.pdf(r, filename=pdf_filename)
