1 <?php
2 3 4 5 6 7 8 9 10 11
12
13
14 if ( ! defined( 'ABSPATH' ) ) exit;
15
16 17 18 19 20 21 22 23 24 25
26 function edd_generate_pdf( $data ) {
27 $edd_pdf_reports_nonce = $_GET['_wpnonce'];
28
29 if ( wp_verify_nonce( $edd_pdf_reports_nonce, 'edd_generate_pdf' ) ) {
30 require_once EDD_PLUGIN_DIR . '/includes/libraries/fpdf/fpdf.php';
31 require_once EDD_PLUGIN_DIR . '/includes/libraries/fpdf/edd_pdf.php';
32
33 $daterange = date_i18n( get_option( 'date_format' ), mktime( 0, 0, 0, 1, 1, date( 'Y' ) ) ) . ' ' . utf8_decode( __( 'to', 'edd' ) ) . ' ' . date_i18n( get_option( 'date_format' ) );
34
35 $pdf = new edd_pdf();
36 $pdf->AddPage( 'L', 'A4' );
37
38 $pdf->SetTitle( utf8_decode( __( 'Sales and earnings reports for the current year for all products', 'edd') ) );
39 $pdf->SetAuthor( utf8_decode( __( 'Easy Digital Downloads', 'edd' ) ) );
40 $pdf->SetCreator( utf8_decode( __( 'Easy Digital Downloads', 'edd' ) ) );
41
42 $pdf->Image( EDD_PLUGIN_URL . 'assets/images/edd-logo.png', 205, 10 );
43
44 $pdf->SetMargins( 8, 8, 8 );
45 $pdf->SetX( 8 );
46
47 $pdf->SetFont( 'Helvetica', '', 16 );
48 $pdf->SetTextColor( 50, 50, 50 );
49 $pdf->Cell( 0, 3, utf8_decode( __( 'Sales and earnings reports for the current year for all products', 'edd' ) ), 0, 2, 'L', false );
50
51 $pdf->SetFont( 'Helvetica', '', 13 );
52 $pdf->Ln();
53 $pdf->SetTextColor( 150, 150, 150 );
54 $pdf->Cell( 0, 6, utf8_decode( __( 'Date Range: ', 'edd' ) ) . $daterange, 0, 2, 'L', false );
55 $pdf->Ln();
56 $pdf->SetTextColor( 50, 50, 50 );
57 $pdf->SetFont( 'Helvetica', '', 14 );
58 $pdf->Cell( 0, 10, utf8_decode( __( 'Table View', 'edd' ) ), 0, 2, 'L', false );
59 $pdf->SetFont( 'Helvetica', '', 12 );
60
61 $pdf->SetFillColor( 238, 238, 238 );
62 $pdf->Cell( 70, 6, utf8_decode( __( 'Product Name', 'edd' ) ), 1, 0, 'L', true );
63 $pdf->Cell( 30, 6, utf8_decode( __( 'Price', 'edd' ) ), 1, 0, 'L', true );
64 $pdf->Cell( 50, 6, utf8_decode( __( 'Categories', 'edd' ) ), 1, 0, 'L', true );
65 $pdf->Cell( 50, 6, utf8_decode( __( 'Tags', 'edd' ) ), 1, 0, 'L', true );
66 $pdf->Cell( 45, 6, utf8_decode( __( 'Number of Sales', 'edd' ) ), 1, 0, 'L', true );
67 $pdf->Cell( 35, 6, utf8_decode( __( 'Earnings to Date', 'edd' ) ), 1, 1, 'L', true );
68
69 $year = date('Y');
70 $downloads = get_posts( array( 'post_type' => 'download', 'year' => $year, 'posts_per_page' => -1 ) );
71
72 if ( $downloads ):
73 $pdf->SetWidths( array( 70, 30, 50, 50, 45, 35 ) );
74
75 foreach ( $downloads as $download ):
76 $pdf->SetFillColor( 255, 255, 255 );
77
78 $title = utf8_decode( get_the_title( $download->ID ) );
79
80 if ( edd_has_variable_prices( $download->ID ) ) {
81
82 $prices = edd_get_variable_prices( $download->ID );
83
84 $first = $prices[0]['amount'];
85 $last = array_pop( $prices );
86 $last = $last['amount'];
87
88 if ( $first < $last ) {
89 $min = $first;
90 $max = $last;
91 } else {
92 $min = $last;
93 $max = $first;
94 }
95
96 $price = html_entity_decode( edd_currency_filter( edd_format_amount( $min ) ) . ' - ' . edd_currency_filter( edd_format_amount( $max ) ) );
97 } else {
98 $price = html_entity_decode( edd_currency_filter( edd_get_download_price( $download->ID ) ) );
99 }
100
101 $categories = get_the_term_list( $download->ID, 'download_category', '', ', ', '' );
102 $categories = $categories ? strip_tags( $categories ) : '';
103
104 $tags = get_the_term_list( $download->ID, 'download_tag', '', ', ', '' );
105 $tags = $tags ? strip_tags( $tags ) : '';
106
107 $sales = edd_get_download_sales_stats( $download->ID );
108 $link = get_permalink( $download->ID );
109 $earnings = html_entity_decode ( edd_currency_filter( edd_get_download_earnings_stats( $download->ID ) ) );
110
111 $pdf->Row( array( $title, $price, $categories, $tags, $sales, $earnings ) );
112 endforeach;
113 else:
114 $pdf->SetWidths( array( 280 ) );
115 $title = utf8_decode( sprintf( __( 'No %s found.', 'edd' ), edd_get_label_plural() ) );
116 $pdf->Row( array( $title ) );
117 endif;
118
119 $pdf->Ln();
120 $pdf->SetTextColor( 50, 50, 50 );
121 $pdf->SetFont( 'Helvetica', '', 14 );
122 $pdf->Cell( 0, 10, utf8_decode( __('Graph View', 'edd') ), 0, 2, 'L', false );
123 $pdf->SetFont( 'Helvetica', '', 12 );
124
125 $image = html_entity_decode( urldecode( edd_draw_chart_image() ) );
126 $image = str_replace( ' ', '%20', $image );
127
128 $pdf->SetX( 25 );
129 $pdf->Image( $image .'&file=.png' );
130 $pdf->Ln( 7 );
131 $pdf->Output( 'edd-report-' . date_i18n('Y-m-d') . '.pdf', 'D' );
132 }
133 }
134 add_action( 'edd_generate_pdf', 'edd_generate_pdf' );
135
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
151 function edd_draw_chart_image() {
152 require_once EDD_PLUGIN_DIR . '/includes/libraries/googlechartlib/GoogleChart.php';
153 require_once EDD_PLUGIN_DIR . '/includes/libraries/googlechartlib/markers/GoogleChartShapeMarker.php';
154 require_once EDD_PLUGIN_DIR . '/includes/libraries/googlechartlib/markers/GoogleChartTextMarker.php';
155
156 $chart = new GoogleChart( 'lc', 900, 330 );
157
158 $i = 1;
159 $earnings = "";
160 $sales = "";
161
162 while ( $i <= 12 ) :
163 $earnings .= edd_get_earnings_by_date( null, $i, date('Y') ) . ",";
164 $sales .= edd_get_sales_by_date( null, $i, date('Y') ) . ",";
165 $i++;
166 endwhile;
167
168 $earnings_array = explode( ",", $earnings );
169 $sales_array = explode( ",", $sales );
170
171 $i = 0;
172 while ( $i <= 11 ) {
173 if ( empty( $sales_array[ $i ] ) )
174 $sales_array[$i] = 0;
175 $i++;
176 }
177
178 $min_earnings = 0;
179 $max_earnings = max( $earnings_array );
180 $earnings_scale = round( $max_earnings, -1 );
181
182 $data = new GoogleChartData( array(
183 $earnings_array[0],
184 $earnings_array[1],
185 $earnings_array[2],
186 $earnings_array[3],
187 $earnings_array[4],
188 $earnings_array[5],
189 $earnings_array[6],
190 $earnings_array[7],
191 $earnings_array[8],
192 $earnings_array[9],
193 $earnings_array[10],
194 $earnings_array[11]
195 ) );
196
197 $data->setLegend( __( 'Earnings', 'edd' ) );
198 $data->setColor( '1b58a3' );
199 $chart->addData( $data );
200
201 $shape_marker = new GoogleChartShapeMarker( GoogleChartShapeMarker::CIRCLE );
202 $shape_marker->setColor( '000000' );
203 $shape_marker->setSize( 7 );
204 $shape_marker->setBorder( 2 );
205 $shape_marker->setData( $data );
206 $chart->addMarker( $shape_marker );
207
208 $value_marker = new GoogleChartTextMarker( GoogleChartTextMarker::VALUE );
209 $value_marker->setColor( '000000' );
210 $value_marker->setData( $data );
211 $chart->addMarker( $value_marker );
212
213 $data = new GoogleChartData( array( $sales_array[0], $sales_array[1], $sales_array[2], $sales_array[3], $sales_array[4], $sales_array[5], $sales_array[6], $sales_array[7], $sales_array[8], $sales_array[9], $sales_array[10], $sales_array[11] ) );
214 $data->setLegend( __( 'Sales', 'edd' ) );
215 $data->setColor( 'ff6c1c' );
216 $chart->addData( $data );
217
218 $chart->setTitle( __( 'Sales and Earnings by Month for all Products', 'edd' ), '336699', 18 );
219
220 $chart->setScale( 0, $max_earnings );
221
222 $y_axis = new GoogleChartAxis( 'y' );
223 $y_axis->setDrawTickMarks( true )->setLabels( array( 0, $max_earnings ) );
224 $chart->addAxis( $y_axis );
225
226 $x_axis = new GoogleChartAxis( 'x' );
227 $x_axis->setTickMarks( 5 );
228 $x_axis->setLabels( array(
229 __('Jan', 'edd'),
230 __('Feb', 'edd'),
231 __('Mar', 'edd'),
232 __('Apr', 'edd'),
233 __('May', 'edd'),
234 __('June', 'edd'),
235 __('July', 'edd'),
236 __('Aug', 'edd'),
237 __('Sept', 'edd'),
238 __('Oct', 'edd'),
239 __('Nov', 'edd'),
240 __('Dec', 'edd')
241 ) );
242 $chart->addAxis( $x_axis );
243
244 $shape_marker = new GoogleChartShapeMarker( GoogleChartShapeMarker::CIRCLE );
245 $shape_marker->setSize( 6 );
246 $shape_marker->setBorder( 2 );
247 $shape_marker->setData( $data );
248 $chart->addMarker( $shape_marker );
249
250 $value_marker = new GoogleChartTextMarker( GoogleChartTextMarker::VALUE );
251 $value_marker->setData( $data );
252 $chart->addMarker( $value_marker );
253
254 return $chart->getUrl();
255 }