-
Notifications
You must be signed in to change notification settings - Fork 0
/
donor-press.php
333 lines (293 loc) · 12 KB
/
donor-press.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
define(__NAMESPACE__ . '\GOODBYE', 'Goodbye cruel world!');
/*
* Plugin Name: Donor Press - A Donation Tracking System
* Plugin URI: https://donorpress.com/
* Description: A plugin for non-profits used to track donations and send donation acknowledgements and year end receipts. You can manually enter donations, upload a .csv file, or optionally integrates with Paypal and Quickbooks.
* Version: 0.1.1
* Requires at least: 0.1.1
* Requires PHP: 7.0
* Author: Denver Steiner
* Author URI: https://donorpress.com/author/
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Update URI: https://github.com/DonorPress/donor-press
* Text Domain: donor-press
*/
global $donor_press_db_version;
$donor_press_db_version='0.1.1';
### recommended to run "composer install" on the plugin directory to add PDF and other functionality, but not required
if (file_exists(__DIR__ . '/vendor/autoload.php')){
require_once __DIR__ . '/vendor/autoload.php';
}
use DonorPress\Donation;
use DonorPress\QuickBooks;
use DonorPress\Donor;
use DonorPress\DonorTemplate;
use DonorPress\CustomVariables;
use DonorPress\Paypal;
use DonorPress\DonationCategory;
// it inserts the entry in the admin menu
add_action('admin_menu', 'donorpress_plugin_create_menu_entry');
register_activation_hook( __FILE__, 'donorpress_plugin_create_tables' );
function donorpress_header_check() {
global $donor_press_db_version;
if (!session_id()) session_start();
require_once "donorpress-upgrade.php";
## When Code base is updated, make sure database upgrade is run
if (get_option( "donor_press_db_version")!=$donor_press_db_version){
donorpress_upgrade();
}
if (Donor::input('redirect','get')){
$qb=new QuickBooks();
$qb->check_redirects(Donor::input('redirect','get'));
}
## download functions before page is loaded
if (Donor::input('Function')){
switch(Donor::input('Function')){
case "pdfTemplatePreview":
$template=new DonorTemplate(DonorTemplate::input_model('post'));
$template->post_excerpt=$template->post_to_settings('post');
$template->settings_decode();
$template->pdf_preview();
break;
case "DonationReceiptPdf":
$donation=Donation::find(Donor::input('DonationId','request'));
$donation->pdf_receipt(stripslashes_deep(Donor::input('customMessage','post')));
break;
case 'BackupDonorPress':
CustomVariables::backup(true);
break;
break;
case "YearEndReceiptPdf":
$donor=Donor::find(Donor::input('DonorId','request'));
$donor->year_receipt_pdf(Donor::input('Year'),stripslashes_deep(Donor::input('customMessage')));
break;
case 'SendYearEndPdf':
Donor::YearEndReceiptMultiple(Donor::input('Year'),Donor::input('pdf','post'),Donor::input('limit'),Donor::input('blankBack','request'),Donor::input('preview','request')?false:true);
break;
case 'PdfLabelDonationReceipts':
Donation::label_by_id(Donor::input('EmailDonationId','post'),Donor::input('col','post'),Donor::input('row','post'),Donor::input('limit'));
break;
case 'PrintYearEndLabels':
Donor::YearEndLabels(Donor::input('Year'),Donor::input('pdf','post'),Donor::input('col','post'),Donor::input('row','post'),Donor::input('limit'));
break;
case 'ExportAllDonors':
Donor::get_mail_list();
break;
case 'ExportDonorList':
Donor::get_mail_list(["D.DonorId IN (".implode(",",Donor::input('pdf','post')).")"]);
break;
case 'DonationListCsv':
Donation::report();
break;
case 'QuickbookSessionKill';
$qb=new QuickBooks();
$qb->clearSession();
return header("Location: ?page=donorpress-quickbooks");
break;
}
}
if (Donor::input('donorAutocomplete','get')){
Donor::autocomplete(Donor::input('query','get'));
exit();
}
## add style
wp_enqueue_style('donorPressPluginStylesheet', plugins_url( '/css/style.css', __FILE__ ), false);
//wp_enqueue_style('donorPressPluginAutoComplete', plugins_url( '/css/autocomplete.min.css', __FILE__ ), false);
wp_enqueue_script('donorPressPluginDefault', plugins_url( '/js/donorpress.js', __FILE__ ), false);
}
//wp_register_style( 'donorPressPluginStylesheet', plugins_url( '/css/style.css', __FILE__ ) );
add_action( 'admin_init', 'donorpress_header_check',1);
// creating the menu entries
function donorpress_plugin_create_menu_entry() {
// icon image path that will appear in the menu
$icon = plugins_url('/images/DonorPressWPIcon2.svg', __FILE__);
//add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );
// adding the main manu entry
add_menu_page('Donors', 'Donors', 'edit_posts', 'donorpress-index', 'donorpress_show_index', $icon);
// adding the sub menu entry
add_submenu_page( 'donorpress-index', 'Reports', 'Reports', 'edit_posts', 'donorpress-reports', 'donorpress_show_reports',2 );
if (Quickbooks::is_setup()) add_submenu_page( 'donorpress-index', 'QuickBooks', 'QuickBooks', 'edit_posts', 'donorpress-quickbooks', 'donorpress_show_quickbooks',4);
if (Paypal::is_setup()) add_submenu_page( 'donorpress-index', 'Paypal', 'Paypal', 'edit_posts', 'donorpress-paypal', 'donorpress_show_paypal',4);
add_submenu_page( 'donorpress-index', 'Settings', 'Settings', 'edit_posts', 'donorpress-settings', 'donorpress_show_settings',5);
}
// function triggered in add_menu_page
function donorpress_show_index() {
include('donorpress-index.php');
}
// function triggered in add_submenu_page
function donorpress_show_reports() {
include('donorpress-reports.php');
}
function donorpress_show_settings() {
include('donorpress-settings.php');
}
function donorpress_show_paypal() {
include('donorpress-paypal.php');
}
function donorpress_show_quickbooks() {
include('donorpress-quickbooks.php');
}
function donorpress_plugin_base_dir(){
return str_replace("\\","/",dirname(__FILE__));
}
function donorpress_tables(){
return ["Donor","Donation","DonationReceipt","DonationCategory","DonorType"];
}
function donorpress_nuke(){
CustomVariables::nuke_it(['droptable'=>"t",'dropfields'=>"t",'rebuild'=>"t"]);
}
function donorpress_plugin_create_tables() {
global $donor_press_db_version;
$tableNames=donorpress_tables();
foreach($tableNames as $table){
$class="DonorPress\\".$table;
$class::create_table();
}
add_option( "donor_press_db_version", $donor_press_db_version );
}
function donorpress_upload_dir( $dirs ) {
//keep uploads in their own donorpress directory, outside normal uploads
$dirs['subdir'] = '/donorpress';
$dirs['path'] = $dirs['basedir'] . $dirs['subdir'];
$dirs['url'] = $dirs['baseurl'] . $dirs['subdir'];
return $dirs;
}
function donorpress_load_test_data($count=20){
if (!class_exists("Faker\Factory")){
Donor::display_error("Faker Class is not installed. You must run 'composer install' on the donor-press plugin directory to get this to function.");
wp_die();
return false;
}
$donationCategory = new DonationCategory();
$donationCategory->Category="Where Needed Most";
$donationCategory->save();
for($i=0;$i<8;$i++){
$donationCategory = new DonationCategory();
$faker = Faker\Factory::create();
$donationCategory->Category=ucfirst($faker->word());
if ($faker->numberBetween(0, 1)==1){
$donationCategory->Category.=" ".ucfirst($faker->word());
}
$donationCategory->save();
}
$donationCategories=DonationCategory::get();
for($i=0;$i<$count;$i++){
$faker = Faker\Factory::create();
$donor=new Donor();
$primarySex=$faker->numberBetween(0, 1);
$single=$faker->numberBetween(0, 1);
$lastName=$faker->lastName;
$donor->Name=($primarySex==0?$faker->firstNameMale:$faker->firstNameFemale) . ' ' .$lastName ;
if ($single==1){
$donor->Name2= ($primarySex==1?$faker->firstNameMale:$faker->firstNameFemale) . ' ' .$lastName;
}
$donor->Email=$faker->email;
$donor->Address1=$faker->streetAddress;
if($faker->numberBetween(0, 10)==1){
$donor->Address2=$faker->secondaryAddress;
}
$donor->City=$faker->city;
$donor->Region=$faker->stateAbbr;
$donor->PostalCode=$faker->postcode;
$donor->Country='US';
$donor->Phone=$faker->phoneNumber;
$donor->Email=$faker->email;
$donor->AddressStatus=1;
$donor->EmailStatus=1;
if($faker->numberBetween(0, 20)==1){
$donor->AddressStatus=-1;
}
if($faker->numberBetween(0, 20)==1){
$donor->EmailStatus=-1;
}
$donor->save();
### add one time donations
$donationTotal=$faker->numberBetween(1, 7);
for($d=0;$d< $donationTotal;$d++){
$donation=new Donation();
$donation->DonorId=$donor->DonorId;
$donation->Date=$faker->dateTimeBetween('-5 years', 'now')->format('Y-m-d');
$donation->DateDeposited=date("Y-m-d",strtotime($donation->Date." +".$faker->numberBetween(0, 10)." days"));
$donation->CreatedAt=date("Y-m-d H:i:s",strtotime($donation->DateDeposited." +".$faker->numberBetween(0, 60*24)." minutes"));
$donation->UpdatedAt=$donation->CreatedAt;
$donation->Name=$donor->Name;
$donation->Gross=$faker->numberBetween(2, 300)*10;
$donation->Fee=0;
$donation->Net=$donation->Gross;
$donation->Currency='USD';
$donation->PaymentSource=1; //set to check
$donation->TransactionType=0;//set to tax deductible
$donation->TransactionID=$faker->numberBetween(100, 2000);
$donation->Type=1;
if ($faker->numberBetween(0, 3)==1){
$donation->Note="Note from Donor";//$faker->sentence();
}
if ($faker->numberBetween(0, 3)==1){
$donation->TransactionType=1; //not tax deductible
$donation->TransactionID=$faker->numberBetween(100000, 300000);
}
if ($faker->numberBetween(0, 5)>1){
$donation->CategoryId=$donationCategories[$faker->numberBetween(0, count($donationCategories)-1)]->CategoryId;
}
$donation->save();
}
$monthlyGift=$faker->numberBetween(0, 1);
if ($monthlyGift==1){
$startDate=$faker->dateTimeBetween('-5 years', 'now')->format('Y-m-d');
$amount=$faker->numberBetween(2, 40)*5;
/* Add monthly giving from that start date till now */
$months=round((strtotime(date("Y-m-d"))-strtotime($startDate))/(60*60*24*30));
$categoryId=0;
//if ($faker->numberBetween(0, 5)>1){
$categoryId=$donationCategories[$faker->numberBetween(0, count($donationCategories)-1)]->CategoryId;
//}
for($m=0;$m<$months;$m++){
$donation=new Donation();
$donation->DonorId=$donor->DonorId;
$donation->Date=date("Y-m-d",strtotime($startDate." +".$m." months"));
$donation->DateDeposited=date("Y-m-d",strtotime($donation->Date." +".$faker->numberBetween(0, 10)." days"));
$donation->CreatedAt=date("Y-m-d H:i:s",strtotime($donation->DateDeposited." +".$faker->numberBetween(0, 60*24)." minutes"));
$donation->UpdatedAt=$donation->CreatedAt;
$donation->Name=$donor->Name;
$donation->Gross=$amount;
$donation->Fee=round($amount*.03,2)*-1;
$donation->Net=$donation->Gross+$donation->Fee;
$donation->Currency='USD';
$donation->FromEmailAddress=$donor->Email;
$donation->PaymentSource=10; //set to Paypal
$donation->TransactionType=0;//set to tax deductible
$donation->TransactionID= bin2hex(random_bytes(5));
$donation->CategoryId=$categoryId;
$donation->Type=5; //subscription
$donation->save();
}
}
}
return true;
}
//adapted from: https://www.php-fig.org/psr/psr-4/examples/ -> used to autolad the DonorPress namespace
spl_autoload_register(function ($class) {
// project-specific namespace prefix
$prefix = 'DonorPress\\';
// base directory for the namespace prefix
$base_dir = __DIR__ . '/classes/';
// does the class use the namespace prefix?
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
// no, move to the next registered autoloader
return;
}
// get the relative class name
$relative_class = substr($class, $len);
// replace the namespace prefix with the base directory, replace namespace
// separators with directory separators in the relative class name, append
// with .php
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
// if the file exists, require it
if (file_exists($file)) {
require $file;
}
});