class-batch-export-donors.php
7 years ago
class-batch-export-forms.php
8 years ago
class-batch-export.php
8 years ago
class-core-settings-export.php
8 years ago
class-export-earnings.php
8 years ago
class-export.php
8 years ago
class-give-export-donations.php
8 years ago
export-actions.php
8 years ago
export-functions.php
8 years ago
give-export-donations-exporter.php
7 years ago
give-export-donations-functions.php
7 years ago
pdf-reports.php
7 years ago
pdf-reports.php
347 lines
| 1 | <?php |
| 2 | /** |
| 3 | * PDF Report Generation Functions. |
| 4 | * |
| 5 | * @package Give |
| 6 | * @subpackage Admin/Reports |
| 7 | * @copyright Copyright (c) 2016, WordImpress |
| 8 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
| 9 | * @since 1.0 |
| 10 | */ |
| 11 | |
| 12 | // Exit if accessed directly.. |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Generate PDF Reports. |
| 19 | * |
| 20 | * Generates PDF report on donations and income for all forms for the current year. |
| 21 | * |
| 22 | * @since 1.0 |
| 23 | * |
| 24 | * @param string $data Data. |
| 25 | * |
| 26 | * @uses give_pdf |
| 27 | */ |
| 28 | function give_generate_pdf( $data ) { |
| 29 | |
| 30 | if ( ! current_user_can( 'view_give_reports' ) ) { |
| 31 | wp_die( __( 'You do not have permission to generate PDF sales reports.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) ); |
| 32 | } |
| 33 | |
| 34 | if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'give_generate_pdf' ) ) { |
| 35 | wp_die( __( 'Nonce verification failed.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) ); |
| 36 | } |
| 37 | |
| 38 | if ( ! file_exists( GIVE_PLUGIN_DIR . '/includes/libraries/give-pdf.php' ) ) { |
| 39 | wp_die( __( 'Dependency missing.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) ); |
| 40 | } |
| 41 | |
| 42 | require_once GIVE_PLUGIN_DIR . '/includes/libraries/give-pdf.php'; |
| 43 | |
| 44 | $daterange = utf8_decode( |
| 45 | sprintf( |
| 46 | /* translators: 1: start date 2: end date */ |
| 47 | __( '%1$s to %2$s', 'give' ), |
| 48 | date_i18n( give_date_format(), mktime( 0, 0, 0, 1, 1, date( 'Y' ) ) ), |
| 49 | date_i18n( give_date_format() ) |
| 50 | ) |
| 51 | ); |
| 52 | |
| 53 | $categories_enabled = give_is_setting_enabled( give_get_option( 'categories', 'disabled' ) ); |
| 54 | $tags_enabled = give_is_setting_enabled( give_get_option( 'tags', 'disabled' ) ); |
| 55 | |
| 56 | $pdf = new Give_PDF( 'L', 'mm', 'A', true, 'UTF-8', false ); |
| 57 | $default_font = apply_filters( 'give_pdf_default_font', 'Helvetica' ); |
| 58 | $custom_font = 'dejavusans'; |
| 59 | $font_style = ''; |
| 60 | |
| 61 | if ( |
| 62 | file_exists( GIVE_PLUGIN_DIR . '/includes/libraries/tcpdf/fonts/CODE2000.TTF' ) && |
| 63 | |
| 64 | // RIAL exist for backward compatibility. |
| 65 | in_array( give_get_currency(), array( 'RIAL', 'RUB', 'IRR' ) ) |
| 66 | ) { |
| 67 | TCPDF_FONTS::addTTFfont( GIVE_PLUGIN_DIR . '/includes/libraries/tcpdf/fonts/CODE2000.TTF', '' ); |
| 68 | $custom_font = 'CODE2000'; |
| 69 | $font_style = 'B'; |
| 70 | } |
| 71 | |
| 72 | $pdf->AddPage( 'L', 'A4' ); |
| 73 | $pdf->setImageScale( 1.5 ); |
| 74 | $pdf->SetTitle( utf8_decode( __( 'Donation report for the current year for all forms', 'give' ) ) ); |
| 75 | $pdf->SetAuthor( utf8_decode( __( 'Give - Democratizing Generosity', 'give' ) ) ); |
| 76 | $pdf->SetCreator( utf8_decode( __( 'Give - Democratizing Generosity', 'give' ) ) ); |
| 77 | |
| 78 | // Image URL should have absolute path. @see https://tcpdf.org/examples/example_009/. |
| 79 | $pdf->Image( apply_filters( 'give_pdf_export_logo', GIVE_PLUGIN_DIR . 'assets/dist/images/give-logo-small.png' ), 247, 8 ); |
| 80 | |
| 81 | $pdf->SetMargins( 8, 8, 8 ); |
| 82 | $pdf->SetX( 8 ); |
| 83 | |
| 84 | $pdf->SetFont( $default_font, '', 16 ); |
| 85 | $pdf->SetTextColor( 50, 50, 50 ); |
| 86 | $pdf->Cell( 0, 3, utf8_decode( __( 'Donation report for the current year for all forms', 'give' ) ), 0, 2, 'L', false ); |
| 87 | |
| 88 | $pdf->SetFont( $default_font, '', 13 ); |
| 89 | $pdf->SetTextColor( 150, 150, 150 ); |
| 90 | $pdf->Ln( 1 ); |
| 91 | $pdf->Cell( 0, 6, utf8_decode( __( 'Date Range: ', 'give' ) ) . $daterange, 0, 2, 'L', false ); |
| 92 | $pdf->Ln(); |
| 93 | $pdf->SetTextColor( 50, 50, 50 ); |
| 94 | $pdf->SetFont( $default_font, '', 14 ); |
| 95 | $pdf->Cell( 0, 10, utf8_decode( __( 'Table View', 'give' ) ), 0, 2, 'L', false ); |
| 96 | $pdf->SetFont( $default_font, '', 12 ); |
| 97 | |
| 98 | $pdf->SetFillColor( 238, 238, 238 ); |
| 99 | $pdf->SetTextColor( 0, 0, 0, 100 ); // Set Black color. |
| 100 | $pdf->Cell( 50, 6, utf8_decode( __( 'Form Name', 'give' ) ), 1, 0, 'L', true ); |
| 101 | $pdf->Cell( 50, 6, utf8_decode( __( 'Price', 'give' ) ), 1, 0, 'L', true ); |
| 102 | |
| 103 | // Display Categories Heading only, if user has opted for it. |
| 104 | if ( $categories_enabled ) { |
| 105 | $pdf->Cell( 45, 6, utf8_decode( __( 'Categories', 'give' ) ), 1, 0, 'L', true ); |
| 106 | } |
| 107 | |
| 108 | // Display Tags Heading only, if user has opted for it. |
| 109 | if ( $tags_enabled ) { |
| 110 | $pdf->Cell( 45, 6, utf8_decode( __( 'Tags', 'give' ) ), 1, 0, 'L', true ); |
| 111 | } |
| 112 | |
| 113 | $pdf->Cell( 45, 6, utf8_decode( __( 'Number of Donations', 'give' ) ), 1, 0, 'L', true ); |
| 114 | $pdf->Cell( 45, 6, utf8_decode( __( 'Income to Date', 'give' ) ), 1, 1, 'L', true ); |
| 115 | |
| 116 | // Set Custom Font to support various currencies. |
| 117 | $pdf->SetFont( apply_filters( 'give_pdf_custom_font', $custom_font ), $font_style, 12 ); |
| 118 | |
| 119 | // Object for getting stats. |
| 120 | $donation_stats = new Give_Payment_Stats(); |
| 121 | |
| 122 | $give_forms = get_posts( array( |
| 123 | 'post_type' => 'give_forms', |
| 124 | 'posts_per_page' => - 1, |
| 125 | 'suppress_filters' => false, |
| 126 | ) ); |
| 127 | |
| 128 | if ( $give_forms ) { |
| 129 | $pdf->SetWidths( array( 50, 50, 45, 45, 45, 45 ) ); |
| 130 | |
| 131 | foreach ( $give_forms as $form ): |
| 132 | $pdf->SetFillColor( 255, 255, 255 ); |
| 133 | |
| 134 | $title = $form->post_title; |
| 135 | |
| 136 | if ( give_has_variable_prices( $form->ID ) ) { |
| 137 | $price = html_entity_decode( give_price_range( $form->ID, false ), ENT_COMPAT, 'UTF-8' ); |
| 138 | } else { |
| 139 | $price = give_currency_filter( give_get_form_price( $form->ID ), array( 'decode_currency' => true ) ); |
| 140 | } |
| 141 | |
| 142 | // Display Categories Data only, if user has opted for it. |
| 143 | $categories = array(); |
| 144 | if ( $categories_enabled ) { |
| 145 | $categories = get_the_term_list( $form->ID, 'give_forms_category', '', ', ', '' ); |
| 146 | $categories = ! is_wp_error( $categories ) ? strip_tags( $categories ) : ''; |
| 147 | } |
| 148 | |
| 149 | // Display Tags Data only, if user has opted for it. |
| 150 | $tags = array(); |
| 151 | if ( $tags_enabled ) { |
| 152 | $tags = get_the_term_list( $form->ID, 'give_forms_tag', '', ', ', '' ); |
| 153 | $tags = ! is_wp_error( $tags ) ? strip_tags( $tags ) : ''; |
| 154 | } |
| 155 | |
| 156 | $sales = $donation_stats->get_sales( $form->ID, 'this_year' ); |
| 157 | $earnings = give_currency_filter( give_format_amount( $donation_stats->get_earnings( $form->ID, 'this_year' ), array( 'sanitize' => false, ) ), array( 'decode_currency' => true ) ); |
| 158 | |
| 159 | // This will help filter data before appending it to PDF Receipt. |
| 160 | $prepare_pdf_data = array(); |
| 161 | $prepare_pdf_data[] = $title; |
| 162 | $prepare_pdf_data[] = $price; |
| 163 | |
| 164 | // Append Categories Data only, if user has opted for it. |
| 165 | if ( $categories_enabled ) { |
| 166 | $prepare_pdf_data[] = $categories; |
| 167 | } |
| 168 | |
| 169 | // Append Tags Data only, if user has opted for it. |
| 170 | if ( $tags_enabled ) { |
| 171 | $prepare_pdf_data[] = $tags; |
| 172 | } |
| 173 | |
| 174 | $prepare_pdf_data[] = $sales; |
| 175 | $prepare_pdf_data[] = $earnings; |
| 176 | |
| 177 | $pdf->Row( $prepare_pdf_data ); |
| 178 | |
| 179 | endforeach; |
| 180 | } else { |
| 181 | |
| 182 | // Fix: Minor Styling Alignment Issue for PDF. |
| 183 | if ( $categories_enabled && $tags_enabled ) { |
| 184 | $no_found_width = 280; |
| 185 | } elseif ( $categories_enabled || $tags_enabled ) { |
| 186 | $no_found_width = 235; |
| 187 | } else { |
| 188 | $no_found_width = 190; |
| 189 | } |
| 190 | $title = utf8_decode( __( 'No forms found.', 'give' ) ); |
| 191 | $pdf->MultiCell( $no_found_width, 5, $title, 1, 'C', false, 1, '', '', true, 0, false, true, 0, 'T', false ); |
| 192 | }// End if(). |
| 193 | $pdf->Ln(); |
| 194 | $pdf->SetTextColor( 50, 50, 50 ); |
| 195 | $pdf->SetFont( $default_font, '', 14 ); |
| 196 | |
| 197 | // Output Graph on a new page. |
| 198 | $pdf->AddPage( 'L', 'A4' ); |
| 199 | $pdf->Cell( 0, 10, utf8_decode( __( 'Graph View', 'give' ) ), 0, 2, 'L', false ); |
| 200 | $pdf->SetFont( $default_font, '', 12 ); |
| 201 | |
| 202 | $image = html_entity_decode( urldecode( give_draw_chart_image() ) ); |
| 203 | $image = str_replace( ' ', '%20', $image ); |
| 204 | |
| 205 | $pdf->SetX( 25 ); |
| 206 | $pdf->Image( $image . '&file=.png' ); |
| 207 | $pdf->Ln( 7 ); |
| 208 | $pdf->Output( apply_filters( 'give_sales_earnings_pdf_export_filename', 'give-report-' . date_i18n( 'Y-m-d' ) ) . '.pdf', 'D' ); |
| 209 | exit(); |
| 210 | } |
| 211 | |
| 212 | add_action( 'give_generate_pdf', 'give_generate_pdf' ); |
| 213 | |
| 214 | /** |
| 215 | * Draws Chart for PDF Report. |
| 216 | * |
| 217 | * Draws the sales and earnings chart for the PDF report and then retrieves the |
| 218 | * URL of that chart to display on the PDF Report. |
| 219 | * |
| 220 | * @since 1.1.4.0 |
| 221 | * @uses GoogleChart |
| 222 | * @uses GoogleChartData |
| 223 | * @uses GoogleChartShapeMarker |
| 224 | * @uses GoogleChartTextMarker |
| 225 | * @uses GoogleChartAxis |
| 226 | * @return string $chart->getUrl() URL for the Google Chart |
| 227 | */ |
| 228 | function give_draw_chart_image() { |
| 229 | require_once GIVE_PLUGIN_DIR . '/includes/libraries/googlechartlib/GoogleChart.php'; |
| 230 | require_once GIVE_PLUGIN_DIR . '/includes/libraries/googlechartlib/markers/GoogleChartShapeMarker.php'; |
| 231 | require_once GIVE_PLUGIN_DIR . '/includes/libraries/googlechartlib/markers/GoogleChartTextMarker.php'; |
| 232 | |
| 233 | $chart = new GoogleChart( 'lc', 900, 330 ); |
| 234 | |
| 235 | $i = 1; |
| 236 | $earnings = ""; |
| 237 | $sales = ""; |
| 238 | |
| 239 | while ( $i <= 12 ) : |
| 240 | $earnings .= give_get_earnings_by_date( null, $i, date( 'Y' ) ) . ","; |
| 241 | $sales .= give_get_sales_by_date( null, $i, date( 'Y' ) ) . ","; |
| 242 | $i ++; |
| 243 | endwhile; |
| 244 | |
| 245 | $earnings_array = explode( ",", $earnings ); |
| 246 | $sales_array = explode( ",", $sales ); |
| 247 | |
| 248 | $i = 0; |
| 249 | while ( $i <= 11 ) { |
| 250 | if ( empty( $sales_array[ $i ] ) ) { |
| 251 | $sales_array[ $i ] = 0; |
| 252 | } |
| 253 | $i ++; |
| 254 | } |
| 255 | |
| 256 | $min_earnings = 0; |
| 257 | $max_earnings = max( $earnings_array ); |
| 258 | $earnings_scale = round( $max_earnings, - 1 ); |
| 259 | |
| 260 | $data = new GoogleChartData( array( |
| 261 | $earnings_array[0], |
| 262 | $earnings_array[1], |
| 263 | $earnings_array[2], |
| 264 | $earnings_array[3], |
| 265 | $earnings_array[4], |
| 266 | $earnings_array[5], |
| 267 | $earnings_array[6], |
| 268 | $earnings_array[7], |
| 269 | $earnings_array[8], |
| 270 | $earnings_array[9], |
| 271 | $earnings_array[10], |
| 272 | $earnings_array[11], |
| 273 | ) ); |
| 274 | |
| 275 | $data->setLegend( __( 'Income', 'give' ) ); |
| 276 | $data->setColor( '1b58a3' ); |
| 277 | $chart->addData( $data ); |
| 278 | |
| 279 | $shape_marker = new GoogleChartShapeMarker( GoogleChartShapeMarker::CIRCLE ); |
| 280 | $shape_marker->setColor( '000000' ); |
| 281 | $shape_marker->setSize( 7 ); |
| 282 | $shape_marker->setBorder( 2 ); |
| 283 | $shape_marker->setData( $data ); |
| 284 | $chart->addMarker( $shape_marker ); |
| 285 | |
| 286 | $value_marker = new GoogleChartTextMarker( GoogleChartTextMarker::VALUE ); |
| 287 | $value_marker->setColor( '000000' ); |
| 288 | $value_marker->setData( $data ); |
| 289 | $chart->addMarker( $value_marker ); |
| 290 | |
| 291 | $data = new GoogleChartData( array( |
| 292 | $sales_array[0], |
| 293 | $sales_array[1], |
| 294 | $sales_array[2], |
| 295 | $sales_array[3], |
| 296 | $sales_array[4], |
| 297 | $sales_array[5], |
| 298 | $sales_array[6], |
| 299 | $sales_array[7], |
| 300 | $sales_array[8], |
| 301 | $sales_array[9], |
| 302 | $sales_array[10], |
| 303 | $sales_array[11], |
| 304 | ) ); |
| 305 | $data->setLegend( __( 'Donations', 'give' ) ); |
| 306 | $data->setColor( 'ff6c1c' ); |
| 307 | $chart->addData( $data ); |
| 308 | |
| 309 | $chart->setTitle( __( 'Donations by Month for all Give Forms', 'give' ), '336699', 18 ); |
| 310 | |
| 311 | $chart->setScale( 0, $max_earnings ); |
| 312 | |
| 313 | $y_axis = new GoogleChartAxis( 'y' ); |
| 314 | $y_axis->setDrawTickMarks( true )->setLabels( array( 0, $max_earnings ) ); |
| 315 | $chart->addAxis( $y_axis ); |
| 316 | |
| 317 | $x_axis = new GoogleChartAxis( 'x' ); |
| 318 | $x_axis->setTickMarks( 5 ); |
| 319 | $x_axis->setLabels( array( |
| 320 | __( 'Jan', 'give' ), |
| 321 | __( 'Feb', 'give' ), |
| 322 | __( 'Mar', 'give' ), |
| 323 | __( 'Apr', 'give' ), |
| 324 | __( 'May', 'give' ), |
| 325 | __( 'June', 'give' ), |
| 326 | __( 'July', 'give' ), |
| 327 | __( 'Aug', 'give' ), |
| 328 | __( 'Sept', 'give' ), |
| 329 | __( 'Oct', 'give' ), |
| 330 | __( 'Nov', 'give' ), |
| 331 | __( 'Dec', 'give' ), |
| 332 | ) ); |
| 333 | $chart->addAxis( $x_axis ); |
| 334 | |
| 335 | $shape_marker = new GoogleChartShapeMarker( GoogleChartShapeMarker::CIRCLE ); |
| 336 | $shape_marker->setSize( 6 ); |
| 337 | $shape_marker->setBorder( 2 ); |
| 338 | $shape_marker->setData( $data ); |
| 339 | $chart->addMarker( $shape_marker ); |
| 340 | |
| 341 | $value_marker = new GoogleChartTextMarker( GoogleChartTextMarker::VALUE ); |
| 342 | $value_marker->setData( $data ); |
| 343 | $chart->addMarker( $value_marker ); |
| 344 | |
| 345 | return $chart->getUrl(); |
| 346 | } |
| 347 |