Donations.php
382 lines
| 1 | <?php |
| 2 | namespace Give\DonorDashboards\Repositories; |
| 3 | |
| 4 | use Give\ValueObjects\Money; |
| 5 | use Give\Framework\Database\DB; |
| 6 | use Give\Receipt\DonationReceipt; |
| 7 | use InvalidArgumentException; |
| 8 | |
| 9 | /** |
| 10 | * @since 2.10.0 |
| 11 | */ |
| 12 | class Donations { |
| 13 | /** |
| 14 | * Get donations count for donor |
| 15 | * |
| 16 | * @param int $donorId |
| 17 | * @since 2.10.0 |
| 18 | * |
| 19 | * @return int |
| 20 | */ |
| 21 | public function getDonationCount( $donorId ) { |
| 22 | $aggregate = $this->getDonationAggregate( 'count(revenue.id)', $donorId ); |
| 23 | return $aggregate ? $aggregate->result : null; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Get donor revenue |
| 28 | * |
| 29 | * @param int $donorId |
| 30 | * @since 2.10.0 |
| 31 | * |
| 32 | * @return string |
| 33 | */ |
| 34 | public function getRevenue( $donorId ) { |
| 35 | $aggregate = $this->getDonationAggregate( 'sum(revenue.amount)', $donorId ); |
| 36 | return $aggregate ? $this->getAmountWithSeparators( Money::ofMinor( $aggregate->result, give_get_option( 'currency' ) )->getAmount() ) : null; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Get average donor revenue |
| 41 | * |
| 42 | * @param int $donorId |
| 43 | * @since 2.10.0 |
| 44 | * |
| 45 | * @return string |
| 46 | */ |
| 47 | public function getAverageRevenue( $donorId ) { |
| 48 | $aggregate = $this->getDonationAggregate( 'avg(revenue.amount)', $donorId ); |
| 49 | return $aggregate ? $this->getAmountWithSeparators( Money::ofMinor( $aggregate->result, give_get_option( 'currency' ) )->getAmount() ) : null; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Generates a donation aggregate for a given donor |
| 54 | * |
| 55 | * @param string $rawAggregate raw SELECT to determine what to aggregate over |
| 56 | * @param int $donorId |
| 57 | * |
| 58 | * @return object |
| 59 | */ |
| 60 | private function getDonationAggregate( $rawAggregate, $donorId ) { |
| 61 | global $wpdb; |
| 62 | |
| 63 | return DB::get_row( |
| 64 | DB::prepare( |
| 65 | " |
| 66 | SELECT {$rawAggregate} as result |
| 67 | FROM {$wpdb->give_revenue} as revenue |
| 68 | INNER JOIN {$wpdb->posts} as posts ON revenue.donation_id = posts.ID |
| 69 | INNER JOIN {$wpdb->prefix}give_donationmeta as donationmeta ON revenue.donation_id = donationmeta.donation_id |
| 70 | WHERE donationmeta.meta_key = '_give_payment_donor_id' |
| 71 | AND donationmeta.meta_value = %d |
| 72 | AND posts.post_status IN ( 'publish', 'give_subscription' ) |
| 73 | ", |
| 74 | $donorId |
| 75 | ) |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Get all donation ids by donor ID |
| 81 | * |
| 82 | * @param int $donorId |
| 83 | * @since 2.10.0 |
| 84 | * @return int[] Donation IDs |
| 85 | */ |
| 86 | protected function getDonationIDs( $donorId ) { |
| 87 | |
| 88 | $statusKeys = give_get_payment_status_keys(); |
| 89 | $statusQuery = "'" . implode( "','", $statusKeys ) . "'"; |
| 90 | |
| 91 | global $wpdb; |
| 92 | return DB::get_col( |
| 93 | DB::prepare( |
| 94 | " |
| 95 | SELECT revenue.donation_id as id |
| 96 | FROM {$wpdb->give_revenue} as revenue |
| 97 | INNER JOIN {$wpdb->posts} as posts ON revenue.donation_id = posts.ID |
| 98 | INNER JOIN {$wpdb->prefix}give_donationmeta as donationmeta ON revenue.donation_id = donationmeta.donation_id |
| 99 | WHERE donationmeta.meta_key = '_give_payment_donor_id' |
| 100 | AND donationmeta.meta_value = %d |
| 101 | AND posts.post_status IN ( {$statusQuery} ) |
| 102 | ", |
| 103 | $donorId |
| 104 | ) |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | /** |
| 110 | * Get all donations by donor ID |
| 111 | * |
| 112 | * @param int $donorId |
| 113 | * @since 2.10.0 |
| 114 | * @return array Donations |
| 115 | */ |
| 116 | public function getDonations( $donorId ) { |
| 117 | |
| 118 | $ids = $this->getDonationIds( $donorId ); |
| 119 | |
| 120 | if ( $ids === null ) { |
| 121 | return null; |
| 122 | } |
| 123 | |
| 124 | $args = [ |
| 125 | 'number' => -1, |
| 126 | 'post__in' => $ids, |
| 127 | ]; |
| 128 | |
| 129 | $query = new \Give_Payments_Query( $args ); |
| 130 | $payments = $query->get_payments(); |
| 131 | |
| 132 | $donations = []; |
| 133 | foreach ( $payments as $payment ) { |
| 134 | $donations[] = [ |
| 135 | 'id' => $payment->ID, |
| 136 | 'form' => $this->getFormInfo( $payment ), |
| 137 | 'payment' => $this->getPaymentInfo( $payment ), |
| 138 | 'donor' => $this->getDonorInfo( $payment ), |
| 139 | 'receipt' => $this->getReceiptInfo( $payment ), |
| 140 | ]; |
| 141 | } |
| 142 | return $donations; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Get form info |
| 147 | * |
| 148 | * @param Give_Payment $payment |
| 149 | * @since 2.10.0 |
| 150 | * @return array Payment form info |
| 151 | */ |
| 152 | protected function getFormInfo( $payment ) { |
| 153 | return [ |
| 154 | 'title' => wp_trim_words( $payment->form_title, 6, ' [...]' ), |
| 155 | 'id' => $payment->form_id, |
| 156 | ]; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Get payment info |
| 161 | * |
| 162 | * @param Give_Payment $payment |
| 163 | * @since 2.10.0 |
| 164 | * @return array Payment info |
| 165 | */ |
| 166 | protected function getPaymentInfo( $payment ) { |
| 167 | |
| 168 | $pdfReceiptUrl = ''; |
| 169 | if ( class_exists( 'Give_PDF_Receipts' ) ) { |
| 170 | $pdfReceiptUrl = html_entity_decode( give_pdf_receipts()->engine->get_pdf_receipt_url( $payment->ID ) ); |
| 171 | } |
| 172 | |
| 173 | $gateways = give_get_payment_gateways(); |
| 174 | |
| 175 | return [ |
| 176 | 'amount' => $this->getFormattedAmount( $payment->subtotal, $payment ), |
| 177 | 'currency' => $payment->currency, |
| 178 | 'fee' => $this->getFormattedAmount( ( $payment->total - $payment->subtotal ), $payment ), |
| 179 | 'total' => $this->getFormattedAmount( $payment->total, $payment ), |
| 180 | 'method' => isset( $gateways[ $payment->gateway ]['checkout_label'] ) ? $gateways[ $payment->gateway ]['checkout_label'] : '', |
| 181 | 'status' => $this->getFormattedStatus( $payment->status ), |
| 182 | 'date' => date_i18n( give_date_format( 'checkout' ), strtotime( $payment->date ) ), |
| 183 | 'time' => date_i18n( 'g:i a', strtotime( $payment->date ) ), |
| 184 | 'mode' => $payment->get_meta( '_give_payment_mode' ), |
| 185 | 'pdfReceiptUrl' => $pdfReceiptUrl, |
| 186 | 'serialCode' => give_is_setting_enabled( give_get_option( 'sequential-ordering_status', 'disabled' ) ) ? Give()->seq_donation_number->get_serial_code( $payment ) : $payment->ID, |
| 187 | ]; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Get array containing dynamic receipt information |
| 192 | * |
| 193 | * @param Give_Payment $payment |
| 194 | * @return array |
| 195 | * @since 2.10.0 |
| 196 | */ |
| 197 | protected function getReceiptInfo( $payment ) { |
| 198 | |
| 199 | $receipt = new DonationReceipt( $payment->ID ); |
| 200 | |
| 201 | /** |
| 202 | * Fire the action for receipt object. |
| 203 | * |
| 204 | * @since 2.7.0 |
| 205 | */ |
| 206 | do_action( 'give_new_receipt', $receipt ); |
| 207 | |
| 208 | $receiptArr = []; |
| 209 | |
| 210 | $sectionIndex = 0; |
| 211 | foreach ( $receipt as $section ) { |
| 212 | // Continue if section does not have line items. |
| 213 | if ( ! $section->getLineItems() ) { |
| 214 | continue; |
| 215 | } |
| 216 | |
| 217 | if ( 'PDFReceipt' === $section->id ) { |
| 218 | continue; |
| 219 | } |
| 220 | |
| 221 | if ( 'Subscription' === $section->id ) { |
| 222 | continue; |
| 223 | } |
| 224 | |
| 225 | $receiptArr[ $sectionIndex ]['id'] = $section->id; |
| 226 | |
| 227 | if ( $section->label ) { |
| 228 | $receiptArr[ $sectionIndex ]['label'] = $section->label; |
| 229 | } |
| 230 | |
| 231 | /* @var LineItem $lineItem */ |
| 232 | foreach ( $section as $lineItem ) { |
| 233 | // Continue if line item does not have value. |
| 234 | if ( ! $lineItem->value ) { |
| 235 | continue; |
| 236 | } |
| 237 | |
| 238 | // This class is required to highlight total donation amount in receipt. |
| 239 | $detailRowClass = ''; |
| 240 | if ( DonationReceipt::DONATIONSECTIONID === $section->id ) { |
| 241 | $detailRowClass = 'totalAmount' === $lineItem->id ? ' total' : ''; |
| 242 | } |
| 243 | |
| 244 | $label = html_entity_decode( wp_strip_all_tags( $lineItem->label ) ); |
| 245 | $value = $lineItem->id === 'paymentStatus' ? $this->getFormattedStatus( $payment->status ) : html_entity_decode( wp_strip_all_tags( $lineItem->value ) ); |
| 246 | |
| 247 | $receiptArr[ $sectionIndex ]['lineItems'][] = [ |
| 248 | 'class' => $detailRowClass, |
| 249 | 'icon' => $this->getIcon( $lineItem->icon ), |
| 250 | 'label' => $label, |
| 251 | 'value' => $value, |
| 252 | ]; |
| 253 | |
| 254 | } |
| 255 | |
| 256 | $sectionIndex++; |
| 257 | } |
| 258 | |
| 259 | return $receiptArr; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Get icon based on icon HTML string |
| 264 | * |
| 265 | * @param string $iconHtml |
| 266 | * @return string |
| 267 | * @since 2.10.0 |
| 268 | */ |
| 269 | protected function getIcon( $iconHtml ) { |
| 270 | |
| 271 | if ( empty( $iconHtml ) ) { |
| 272 | return ''; |
| 273 | } |
| 274 | |
| 275 | $iconMap = [ |
| 276 | 'user', |
| 277 | 'envelope', |
| 278 | 'globe', |
| 279 | 'calendar', |
| 280 | 'building', |
| 281 | ]; |
| 282 | |
| 283 | foreach ( $iconMap as $icon ) { |
| 284 | if ( strpos( $iconHtml, $icon ) !== false ) { |
| 285 | return $icon; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Get formatted status object (used for rendering status correctly in Donor Profile) |
| 293 | * |
| 294 | * @param string $status |
| 295 | * @since 2.10.0 |
| 296 | * @return array Formatted status object (with color and label) |
| 297 | */ |
| 298 | protected function getFormattedStatus( $status ) { |
| 299 | |
| 300 | $statusMap = []; |
| 301 | |
| 302 | $colorMap = [ |
| 303 | 'publish' => '#7ad03a', |
| 304 | 'give_subscription' => '#5bc0de', |
| 305 | 'refunded' => '#777', |
| 306 | 'failed' => '#a00', |
| 307 | 'abandoned' => '#333', |
| 308 | 'revoked' => '#d9534f', |
| 309 | 'pending' => '#ffba00', |
| 310 | ]; |
| 311 | |
| 312 | foreach ( give_get_payment_statuses() as $key => $value ) { |
| 313 | |
| 314 | if ( $status !== $key ) { |
| 315 | continue; |
| 316 | } |
| 317 | |
| 318 | $color = '#888'; |
| 319 | if ( in_array( $key, array_keys( $colorMap ) ) ) { |
| 320 | $color = $colorMap[ $key ]; |
| 321 | } |
| 322 | |
| 323 | $statusMap[ $key ] = [ |
| 324 | 'color' => $color, |
| 325 | 'label' => $value, |
| 326 | ]; |
| 327 | |
| 328 | } |
| 329 | |
| 330 | return isset( $statusMap[ $status ] ) ? $statusMap[ $status ] : [ |
| 331 | 'color' => '#FFBA00', |
| 332 | 'label' => esc_html__( 'Unknown', 'give' ), |
| 333 | ]; |
| 334 | } |
| 335 | |
| 336 | protected function getAmountWithSeparators( $amount ) { |
| 337 | $formatted = give_format_amount( |
| 338 | $amount, |
| 339 | [ |
| 340 | 'decimal' => false, |
| 341 | ] |
| 342 | ); |
| 343 | |
| 344 | return $formatted ? $formatted : (string) $amount; |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Get formatted payment amount |
| 349 | * |
| 350 | * @param float $amount |
| 351 | * @param Give_Payment $payment |
| 352 | * @since 2.10.0 |
| 353 | * @return string Formatted payment amount (with correct decimals and currency symbol) |
| 354 | */ |
| 355 | protected function getformattedAmount( $amount, $payment ) { |
| 356 | return give_currency_filter( |
| 357 | give_format_amount( |
| 358 | $amount, |
| 359 | [ |
| 360 | 'donation_id' => $payment->ID, |
| 361 | ] |
| 362 | ), |
| 363 | [ |
| 364 | 'currency_code' => $payment->currency, |
| 365 | 'decode_currency' => true, |
| 366 | 'sanitize' => false, |
| 367 | ] |
| 368 | ); |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Get donor info |
| 373 | * |
| 374 | * @param Give_Payment $payment |
| 375 | * @since 2.10.0 |
| 376 | * @return array Donor info |
| 377 | */ |
| 378 | protected function getDonorInfo( $payment ) { |
| 379 | return $payment->user_info; |
| 380 | } |
| 381 | } |
| 382 |