class-batch-export-donors.php
5 years ago
class-batch-export.php
4 years ago
class-core-settings-export.php
7 years ago
class-export-earnings.php
3 years ago
class-export.php
3 years ago
class-give-export-donations.php
5 years ago
export-actions.php
3 years ago
export-functions.php
4 years ago
give-export-donations-exporter.php
6 years ago
give-export-donations-functions.php
5 years ago
pdf-reports.php
5 years ago
class-export-earnings.php
186 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Earnings Export Class |
| 4 | * |
| 5 | * This class handles earnings export |
| 6 | * |
| 7 | * @package Give |
| 8 | * @since 1.0 |
| 9 | * @copyright Copyright (c) 2016, GiveWP |
| 10 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
| 11 | * @subpackage Admin/Reports |
| 12 | */ |
| 13 | |
| 14 | // Exit if accessed directly. |
| 15 | |
| 16 | if (!defined('ABSPATH')) { |
| 17 | exit; |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Give_Earnings_Export Class |
| 22 | * |
| 23 | * @since 1.0 |
| 24 | */ |
| 25 | class Give_Earnings_Export extends Give_Export |
| 26 | { |
| 27 | |
| 28 | /** |
| 29 | * Our export type. Used for export-type specific filters/actions |
| 30 | * |
| 31 | * @since 1.0 |
| 32 | * @var string |
| 33 | */ |
| 34 | public $export_type = 'earnings'; |
| 35 | |
| 36 | /** |
| 37 | * Set the export headers |
| 38 | * |
| 39 | * @access public |
| 40 | * @since 1.6 |
| 41 | * @return void |
| 42 | */ |
| 43 | public function headers() |
| 44 | { |
| 45 | give_ignore_user_abort(); |
| 46 | |
| 47 | nocache_headers(); |
| 48 | header('Content-Type: text/csv; charset=utf-8'); |
| 49 | header( |
| 50 | 'Content-Disposition: attachment; filename=' . apply_filters( |
| 51 | 'give_earnings_export_filename', |
| 52 | 'give-export-' . $this->export_type . '-' . date('n') . '-' . date('Y') |
| 53 | ) . '.csv' |
| 54 | ); |
| 55 | header('Expires: 0'); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Set the CSV columns |
| 60 | * |
| 61 | * @access public |
| 62 | * @since 1.0 |
| 63 | * @return array $cols All the columns |
| 64 | */ |
| 65 | public function csv_cols() |
| 66 | { |
| 67 | $cols = [ |
| 68 | 'date' => __('Date', 'give'), |
| 69 | 'donations' => __('Donations', 'give'), |
| 70 | /* translators: %s: currency */ |
| 71 | 'earnings' => sprintf(__('Revenue (%s)', 'give'), give_currency_symbol('', true)), |
| 72 | ]; |
| 73 | |
| 74 | return $cols; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Include a nonce check when authenticating |
| 79 | * |
| 80 | * @since 2.21.4 |
| 81 | * |
| 82 | * @return bool |
| 83 | */ |
| 84 | public function can_export() { |
| 85 | return (bool) apply_filters( 'give_export_capability', current_user_can( 'export_give_reports' ) && wp_verify_nonce($_REQUEST['give-nonce'], 'give_earnings_export')); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get the Export Data |
| 90 | * |
| 91 | * @access public |
| 92 | * @since 1.0 |
| 93 | * @return array $data The data for the CSV file |
| 94 | */ |
| 95 | public function get_data() |
| 96 | { |
| 97 | $dates = $this->getDatesFromRequest(); |
| 98 | |
| 99 | $data = []; |
| 100 | $year = $dates->startYear; |
| 101 | $stats = new Give_Payment_Stats(); |
| 102 | |
| 103 | while ($year <= $dates->endYear) { |
| 104 | if ($year === $dates->startYear && $year === $dates->endYear) { |
| 105 | $m1 = $dates->startMonth; |
| 106 | $m2 = $dates->endMonth; |
| 107 | } elseif ($year === $dates->startYear) { |
| 108 | $m1 = $dates->startMonth; |
| 109 | $m2 = 12; |
| 110 | } elseif ($year === $dates->endYear) { |
| 111 | $m1 = 1; |
| 112 | $m2 = $dates->endMonth; |
| 113 | } else { |
| 114 | $m1 = 1; |
| 115 | $m2 = 12; |
| 116 | } |
| 117 | |
| 118 | while ($m1 <= $m2) { |
| 119 | $date1 = mktime(0, 0, 0, $m1, 1, $year); |
| 120 | $date2 = mktime(0, 0, 0, $m1, cal_days_in_month(CAL_GREGORIAN, $m1, $year), $year); |
| 121 | |
| 122 | $data[] = [ |
| 123 | 'date' => date_i18n('F Y', $date1), |
| 124 | 'donations' => $stats->get_sales(0, $date1, $date2), |
| 125 | 'earnings' => give_format_amount($stats->get_earnings(0, $date1, $date2), ['sanitize' => false]), |
| 126 | ]; |
| 127 | |
| 128 | $m1++; |
| 129 | } |
| 130 | |
| 131 | $year++; |
| 132 | } |
| 133 | |
| 134 | $data = apply_filters('give_export_get_data', $data); |
| 135 | $data = apply_filters("give_export_get_data_{$this->export_type}", $data); |
| 136 | |
| 137 | return $data; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * @since 2.21.2 |
| 142 | * |
| 143 | * @return object|null |
| 144 | */ |
| 145 | private function getDatesFromRequest() |
| 146 | { |
| 147 | $dates = new stdClass(); |
| 148 | $firstDonation = give()->donations->getFirstDonation(); |
| 149 | $lastDonation = give()->donations->getLatestDonation(); |
| 150 | |
| 151 | if ($firstDonation === null ) { |
| 152 | return (object)[ |
| 153 | 'startYear' => date('Y'), |
| 154 | 'endYear' => date('Y' ), |
| 155 | 'startMonth' => date('m'), |
| 156 | 'endMonth' => date('m') |
| 157 | ]; |
| 158 | } |
| 159 | |
| 160 | if (!isset($_POST['start_year'], $_POST['end_year'], $_POST['start_month'], $_POST['end_month'])) { |
| 161 | throw new \Give\Framework\Exceptions\Primitives\InvalidArgumentException( |
| 162 | 'Start year & month, End year & month can not be empty. Please enter validate dates to export revenue and donation stats.' |
| 163 | ); |
| 164 | } |
| 165 | |
| 166 | $dates->startYear = (string)absint($_POST['start_year']); |
| 167 | $dates->endYear = (string)absint($_POST['end_year']); |
| 168 | $dates->startMonth = (string)absint($_POST['start_month']); |
| 169 | $dates->endMonth = (string)absint($_POST['end_month']); |
| 170 | |
| 171 | if ($firstDonation && $firstDonation->createdAt->format('Y') > $dates->startYear) { |
| 172 | throw new \Give\Framework\Exceptions\Primitives\InvalidArgumentException( |
| 173 | 'Start year cannot be less than first donation year' |
| 174 | ); |
| 175 | } |
| 176 | |
| 177 | if ($lastDonation && $lastDonation->createdAt->format('Y') < $dates->endYear) { |
| 178 | throw new \Give\Framework\Exceptions\Primitives\InvalidArgumentException( |
| 179 | 'End year cannot be greater than last donation year' |
| 180 | ); |
| 181 | } |
| 182 | |
| 183 | return $dates; |
| 184 | } |
| 185 | } |
| 186 |