6894dc1c by Chris Boden

Cleaned up reimbursment display, mapped calls to new API in CBV Invoice

1 parent 72db23b4
...@@ -205,35 +205,38 @@ jQuery(function($) { ...@@ -205,35 +205,38 @@ jQuery(function($) {
205 var iid = $(this).attr('rel'); 205 var iid = $(this).attr('rel');
206 var current_amount = $(this).attr('amount'); 206 var current_amount = $(this).attr('amount');
207 207
208 jConfirm('<strong>Are you sure</strong> you want to <em>refund</em> this invoice?', 'Refund Invoice', function(c) {
209 if (c) {
210
211 // now we are going to pop up with invoice items. 208 // now we are going to pop up with invoice items.
212 $.colorbox({ 209 $.colorbox({
213 href: '/wp-admin/admin-ajax.php?ajax=yes&action=admin_refund_invoice_form&invoice_id='+iid 210 href: '/wp-admin/admin-ajax.php?ajax=yes&action=admin_refund_invoice_form&invoice_id='+iid+'&uid='+user_id
214 }); 211 , onComplete: function() {
215 /* 212 jQuery('#invoice_refund_form').ajaxForm({
216 jPrompt('How much would you like to re-imburse?', current_amount, 'Re-imburse...', function(r) {
217 if( r ) {
218 jQuery.ajax({
219 url: '/wp-admin/admin-ajax.php' 213 url: '/wp-admin/admin-ajax.php'
220 , data: ({ajax:"yes", action: 'admin_refund_invoice',invoice_id:iid, amount:r}) 214 , data: ({ajax:"yes", action: 'issue_invoice_refund', invoice_id: iid})
221 , type: 'POST'
222 , dataType: 'json' 215 , dataType: 'json'
216 , type: 'POST'
223 , success: function(data) { 217 , success: function(data) {
224 document.location.reload(); 218 if (data.success == 'true') {
219 document.location.href = document.location.href;
220 } else {
221 jAlert(data.msg, 'Server Error Encountered');
225 } 222 }
226 }); 223 }
224 , error: function(XMLHttpRequest, textStatus, errorThrown) {
225 jAlert(textStatus, 'Server Error Encountered');
227 } 226 }
228 }); 227 });
229 */ 228
229 $('#refund_cancel_btn').click(function(e) {
230 e.preventDefault();
231 $.colorbox.close();
232 });
230 } 233 }
231 }); 234 });
235
232 e.preventDefault(); 236 e.preventDefault();
233 return false; 237 return false;
234 }); 238 });
235 239
236
237 jQuery('.invoice-cancel-btn').live('click', function(e) { 240 jQuery('.invoice-cancel-btn').live('click', function(e) {
238 var iid = $(this).attr('rel'); 241 var iid = $(this).attr('rel');
239 242
......
...@@ -18,7 +18,8 @@ use Tz\WordPress\CBV; ...@@ -18,7 +18,8 @@ use Tz\WordPress\CBV;
18 use Tz\WordPress\CBV\User; 18 use Tz\WordPress\CBV\User;
19 use Tz\WordPress\CBV\Events; 19 use Tz\WordPress\CBV\Events;
20 use Tz\WordPress\CBV\CEHours; 20 use Tz\WordPress\CBV\CEHours;
21 use Tz\WordPress\CBV\Invoice; 21 use Tz\WordPress\CBV\Invoice, Tz\WordPress\CBV\Invoice\Order\Item as OrderItem;
22 use Tz\WordPress\CBV\Beanstream\Card as CreditCard;
22 use Tz\WordPress\CBV\Courses; 23 use Tz\WordPress\CBV\Courses;
23 use Tz\WordPress\CBV\CBVOptions; 24 use Tz\WordPress\CBV\CBVOptions;
24 use Exception, StdClass, ArrayAccess, Iterator, Countable; 25 use Exception, StdClass, ArrayAccess, Iterator, Countable;
...@@ -804,137 +805,38 @@ class Actions { ...@@ -804,137 +805,38 @@ class Actions {
804 $user = new User\Account($_POST['user_id']); 805 $user = new User\Account($_POST['user_id']);
805 $course = new Courses\Course($_POST['course_id']); 806 $course = new Courses\Course($_POST['course_id']);
806 $enrolltype = $_POST['enrolltype']; 807 $enrolltype = $_POST['enrolltype'];
808 $tax_rate = $user->getLocTaxPct();
807 809
808 // Apply user to course, sets group, set enrollment, etc. 810 // Apply user to course, sets group, set enrollment, etc.
809 $user->registerForCourse($course->getID(), (boolean)isset($_POST['exam']), $enrolltype); 811 $user->registerForCourse($course->getID(), (boolean)isset($_POST['exam']), $enrolltype);
810 $user->approveToCourse($course->getID()); 812 $user->approveToCourse($course->getID());
811 813
812 // Do all the invoicey stuff 814 $order = new Invoice\Order();
813 $items_subtotal = $items_taxes = $items_total = 0; // Running totals...should be a class 815 $order->addItem(new OrderItem($course->$enrolltype, $tax_rate, $course->post_title, $course->getID()));
814 $tax_rate = $user->getLocTaxPct();
815 $calc_tax = function($cost, $tax_rate) {
816 return number_format(($cost * $tax_rate * 0.01), 2, '.', '');
817 };
818
819 $course_cost = $course->$enrolltype;
820 $course_taxes = $calc_tax($course_cost, $tax_rate);
821
822 // Add course cost to running totals
823 $items_subtotal += $course_cost;
824 $items_taxes += $course_taxes;
825 $items_total += ($course_cost + $course_taxes);
826
827 // Add the course its self to the receipt
828 $items = Array(Array(
829 'post_id' => $course->getID()
830 , 'cost' => $course_cost
831 , 'discount_label' => ""
832 , 'discount_amount' => 0
833 , 'tax_label' => "Taxes"
834 , 'tax_rate' => $tax_rate
835 , 'tax_amount' => $course_taxes
836 , 'subtotal' => $course_cost
837 , 'total' => number_format(($course_cost + $course_taxes), 2, '.', '')
838 , 'description' => $course->post_title
839 , 'extras' => array()
840 ));
841 816
842 if (isset($_POST['studentfee']) && $_POST['studentfee'] == 1) { 817 if (isset($_POST['studentfee']) && $_POST['studentfee'] == 1) {
843 $fee = number_format(CBVOptions\GetOption('student-fees', 'fees'), 2, '.', ''); 818 $fee = number_format(CBVOptions\GetOption('student-fees', 'fees'), 2, '.', '');
844 $taxes = $calc_tax($fee, $tax_rate);
845 819
846 $items_subtotal += $fee; 820 $order->addItem(new OrderItem($fee, $tax_rate, 'Annual Registration Fee'));
847 $items_taxes += $taxes;
848 $items_total += $fee + $taxes;
849
850 $items[] = Array(
851 'post_id' => 0
852 , 'cost' => $fee
853 , 'discount_label' => ''
854 , 'discount_amount' => ''
855 , 'tax_label' => 'Taxes'
856 , 'tax_rate' => $tax_rate
857 , 'tax_amount' => $taxes
858 , 'subtotal' => $fee
859 , 'total' => ($fee + $taxes)
860 , 'description' => 'Annual Registration Fee'
861 , 'extras' => Array()
862 );
863 821
864 $user->setMeta('fee_semester_paid', $course->semester_id); 822 $user->setMeta('fee_semester_paid', $course->semester_id);
865 } 823 }
866 824
867 // If the user paid for the casebook add to invoice and Casebook group
868 if (isset($_POST['casebook']) && $_POST['casebook'] == 1) { 825 if (isset($_POST['casebook']) && $_POST['casebook'] == 1) {
869 $book = UAM\getGroup('Casebook'); 826 $book = UAM\getGroup('Casebook');
870 $book->addUser($user->ID); 827 $book->addUser($user->ID);
871 828
872 $cost = substr($book->getDescription(), strpos($book->getDescription(), ':') + 1); 829 $cost = substr($book->getDescription(), strpos($book->getDescription(), ':') + 1);
873 $taxes = $calc_tax($cost, CBV\get_tax_pct()); 830 $order->addItem(new OrderItem($cost, CBV\get_tax_pct(), 'Casebook', CBV\CASEBOOK_POSTID));
874
875 $items_subtotal += $cost;
876 $items_taxes += $taxes;
877 $items_total += $cost + $taxes;
878
879 $items[] = Array(
880 'post_id' => CBV\CASEBOOK_POSTID
881 , 'cost' => $cost
882 , 'discount_label' => ''
883 , 'discount_amount' => ''
884 , 'tax_label' => 'Taxes'
885 , 'tax_rate' => CBV\get_tax_pct()
886 , 'tax_amount' => $taxes
887 , 'subtotal' => $cost
888 , 'total' => ($cost + $taxes)
889 , 'description' => 'Casebook'
890 , 'extras' => Array()
891 );
892 } 831 }
893 832
894 // If the user is overseas and CBV charged them for it, add item to receipt
895 if (isset($_POST['overseas']) && $_POST['overseas'] == 1) { 833 if (isset($_POST['overseas']) && $_POST['overseas'] == 1) {
896 $os_cost = number_format(CBVOptions\GetOption('overseas-surcharge', 'fees'), 2, '.', ''); 834 $os_cost = number_format(CBVOptions\GetOption('overseas-surcharge', 'fees'), 2, '.', '');
897 $os_taxes = $calc_tax($os_cost, $tax_rate); 835 $order->addItem(new OrderItem($os_cost, $tax_rate, 'Overseas Surcharge'));
898
899 $items[] = Array(
900 'post_id' => 0
901 , 'cost' => $os_cost
902 , 'discount_label' => ''
903 , 'discount_amount' => ''
904 , 'tax_label' => 'Taxes'
905 , 'tax_rate' => $tax_rate
906 , 'tax_amount' => $os_taxes
907 , 'subtotal' => $os_cost
908 , 'total' => ($os_cost + $os_taxes)
909 , 'description' => 'Overseas Surcharge'
910 , 'extras' => Array()
911 );
912
913 $items_subtotal += $os_cost;
914 $items_taxes += $os_taxes;
915 $items_total += ($os_cost + $os_taxes);
916 } 836 }
917 837
918 $invoice_data = Array(); 838 $cc = new CreditCard(null, $_POST['paid_by']);
919 $invoice_data['items'] = $items; 839 $order->generateInvoice($user, 'course', 'Course Invoice', $cc);
920 $invoice_data['payment'] = Array(
921 'total_cost' => $items_subtotal
922 , 'total_discounts' => ""
923 , 'total_taxes' => $items_taxes
924 , 'subtotal' => $items_subtotal
925 , 'total' => $items_total
926 , 'bt_address' => ""
927 , 'bt_address2' => ""
928 , 'bt_city' => ""
929 , 'bt_province' => ""
930 , 'bt_country' => ""
931 , 'bt_postal' => ""
932 , 'bt_card_holder' => ""
933 , 'bt_card_number' => ""
934 , 'bt_card_type' => $_POST['paid_by']
935 );
936
937 Invoice\create($invoice_data, 'course', 'Course Invoice', $user->ID, 'paid', 'publish', $items_total);
938 840
939 die(json_encode(Array('success' => 'true', 'refresh' => 'true'))); 841 die(json_encode(Array('success' => 'true', 'refresh' => 'true')));
940 } 842 }
...@@ -1437,8 +1339,7 @@ class Actions { ...@@ -1437,8 +1339,7 @@ class Actions {
1437 $user = new User\Account($_GET['uid']); 1339 $user = new User\Account($_GET['uid']);
1438 ob_start(); 1340 ob_start();
1439 1341
1440 $invoice = get_post($_GET['invoice_id']); 1342 $invoice = new Invoice\Receipt($_GET['invoice_id']);
1441
1442 1343
1443 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'partials' . DIRECTORY_SEPARATOR . 'invoice-refund-view.php'); 1344 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'partials' . DIRECTORY_SEPARATOR . 'invoice-refund-view.php');
1444 1345
...@@ -1448,13 +1349,16 @@ class Actions { ...@@ -1448,13 +1349,16 @@ class Actions {
1448 } 1349 }
1449 1350
1450 public static function wp_ajax_issue_invoice_refund() { 1351 public static function wp_ajax_issue_invoice_refund() {
1451 1352 $invoice = new Invoice\Receipt($_POST['invoice_id']);
1452 $invoice = get_post($_POST['invoice_id']); 1353 $user = new User\Account($invoice->getOwner());
1453 $user = new User\Account($invoice->post_author);
1454 $items = $_POST['keys']; 1354 $items = $_POST['keys'];
1455 1355
1456 // Chris, do what you need to do with this.... 1356 foreach ($items as $index => $yes) {
1357 $invoice->seek($index)->cancel();
1358 }
1457 1359
1360 $invoice->runRefund();
1361 // Chris, do what you need to do with this....
1458 1362
1459 // if there is an error, turn success to 'false' and set a msg. 1363 // if there is an error, turn success to 'false' and set a msg.
1460 $return = array( 1364 $return = array(
...@@ -1462,8 +1366,6 @@ class Actions { ...@@ -1462,8 +1366,6 @@ class Actions {
1462 , 'msg' => '' 1366 , 'msg' => ''
1463 ); 1367 );
1464 die(json_encode($return)); 1368 die(json_encode($return));
1465
1466
1467 } 1369 }
1468 1370
1469 public static function wp_ajax_admin_search_merge_users() { 1371 public static function wp_ajax_admin_search_merge_users() {
......
...@@ -15,6 +15,7 @@ use Exception, StdClass; ...@@ -15,6 +15,7 @@ use Exception, StdClass;
15 use WP_User; 15 use WP_User;
16 ?> 16 ?>
17 <div style="padding:10px 10px 0px 10px; min-width:760px;position:relative;"> 17 <div style="padding:10px 10px 0px 10px; min-width:760px;position:relative;">
18
18 <h2 style="margin-bottom:10px;padding-bottom:0px;">Users' Invoice/Receipts... 19 <h2 style="margin-bottom:10px;padding-bottom:0px;">Users' Invoice/Receipts...
19 <?php if (!isset($_GET['view'])):?> 20 <?php if (!isset($_GET['view'])):?>
20 <a href="#" class="button add-new-h2" id="creditNotBtn">Create new Invoice/Credit Note</a> 21 <a href="#" class="button add-new-h2" id="creditNotBtn">Create new Invoice/Credit Note</a>
......
...@@ -74,7 +74,7 @@ ...@@ -74,7 +74,7 @@
74 <div class="dashboard-section" style="margin-left:20px;margin-top:10px;"> 74 <div class="dashboard-section" style="margin-left:20px;margin-top:10px;">
75 <div class="dashboard-section-title"> 75 <div class="dashboard-section-title">
76 <input type="checkbox" name="exam" id="exam" value="1" checked /> 76 <input type="checkbox" name="exam" id="exam" value="1" checked />
77 <label for="casebook">WRITING EXAM?</label> 77 <label for="exam">WRITING EXAM?</label>
78 </div> 78 </div>
79 </div> 79 </div>
80 80
...@@ -100,7 +100,7 @@ ...@@ -100,7 +100,7 @@
100 <div class="dashboard-section" style="margin-left:20px;margin-top:10px;"> 100 <div class="dashboard-section" style="margin-left:20px;margin-top:10px;">
101 <div class="dashboard-section-title"> 101 <div class="dashboard-section-title">
102 <input type="checkbox" name="overseas" id="overseas" value="1" checked /> 102 <input type="checkbox" name="overseas" id="overseas" value="1" checked />
103 <label for="casebook">PAID OVERSEAS SURCHARGE?</label> 103 <label for="overseas">PAID OVERSEAS SURCHARGE?</label>
104 </div> 104 </div>
105 </div> 105 </div>
106 <?php endif; ?> 106 <?php endif; ?>
......
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>
3
4 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
7 4
8 <title>Edit CE Hours</title> 5 <title>Refund: <?php echo $invoice->getTitle(); ?></title>
9 <style type="text/css"> 6 <style type="text/css">
10
11 html, body { margin:0px; padding:0;} 7 html, body { margin:0px; padding:0;}
12 .title-link { 8 .title-link {
13 color:#f7bd55; 9 color: #F7BD55;
14 font-size: 12px; 10 font-size: 12px;
15 font-weight: bold; 11 font-weight: bold;
16 text-align: left; 12 text-align: left;
17 line-height: 1.75em; 13 line-height: 1.75em;
18 background: #3b0d32; 14 background: #3B0D32;
19 border: solid 1px #FFF; 15 border: 1px solid #FFF;
20 border-bottom: solid 1px #999; 16 border-bottom: 1px solid #999;
21 cursor: default; 17 cursor: default;
22 padding: 0em; padding:3px 10px 3px 10px; 18 padding: 3px 10px 3px 10px;
23 margin: 0em; 19 margin: 0em;
24
25 } 20 }
26 21
27
28 form { 22 form {
29 display: block; 23 display: block;
30 margin-right:20px; 24 margin-right: 20px;
31 } 25 }
32 26
33 table.item-listings tr th { text-align:left; border-bottom:1px solid #ccc; } 27 table.item-listings tr th { text-align: left; border-bottom: 1px solid #CCC; }
34 table.item-listings tr td { text-aling:left; border-bottom:1px solid #e8e8e8; } 28 table.item-listings tr td { text-aling: left; border-bottom: 1px solid #CCC; }
29
30 table.item-listings tbody tr.odd td { background-color: #F5F5F5; }
35 31
36 table.item-listings tr th, 32 table.item-listings tr th,
37 table.item-listings tr td { padding: 4px 5px; } 33 table.item-listings tr td { padding: 4px 5px; }
34 table.item-listings thead tr th { padding-bottom: 10px; }
35
36 table { border-collapse:collapse; }
38 37
39 .dashboard-section-title { font-weight:bold; color:#3b0d32; } 38 .dashboard-section-title { font-weight:bold; color:#3b0d32; }
39
40 label.amount {
41 color: #999;
42 font-size: 11px;
43 }
40 </style> 44 </style>
41 </head> 45 </head>
42 46
43 <body> 47 <body class="invoice-refund">
44
45 <div style="display:block; width:750px; margin-bottom:0px;"> 48 <div style="display:block; width:750px; margin-bottom:0px;">
46 <div class="title-link" style="display:block;color:#f7bd55; font-size: 12px;font-weight: bold;text-align: left;line-height: 1.75em; background-color: #3b0d32; border: solid 1px #FFF; border-bottom: solid 1px #999; cursor: default; padding: 0em; padding:3px 10px 3px 10px; margin: 0em;">Refund: <?php echo $invoice->post_title; ?></div> 49 <div class="title-link">Refund: <?php echo $invoice->getTitle(); ?></div>
47 <div style="padding:10px;"> 50 <div style="padding:10px;">
48 51
49 <form id="invoice_refund_form"> 52 <form id="invoice_refund_form">
50 53
51 <table cellpadding="0" cellspacing="0" width="100%" border="0" style="border-collapse:collapse;" class="item-listings"> 54 <table cellpadding="0" cellspacing="0" width="100%" border="0" class="item-listings">
52 <tr> 55 <thead><tr>
53 <th>Item Description</th> 56 <th>Item Description</th>
54 <th width="140">Refund?</th> 57 <th width="140">Refund?</th>
55 </tr> 58 </tr></thead>
56 59
60 <tbody>
57 <?php 61 <?php
58 $items = get_post_meta($invoice->ID, 'items', true); 62 $i = 0;
59 foreach($items as $key=>$item): 63 foreach ($invoice as $key => $item):
64 $unq = "keys[{$key}]";
60 ?> 65 ?>
61 <tr> 66 <tr class="<?php echo (++$i & 1 ? 'odd' : 'even'); ?>">
67 <td><label for="<?php echo $unq; ?>"><?php echo $item->getDescription(); ?></label></td>
62 <td> 68 <td>
63 <?php 69 <input type="checkbox" checked="checked" value="1" id="<?php echo $unq; ?>" name="<?php echo $unq; ?>" />
64 if ( isset($item['post_id']) && $item['post_id'] > 0) { 70 <label for="<?php echo $unq; ?>" class="amount">(-$<?php echo $item->getTotal();?>)</label>
65 $p = get_post($item['post_id']);
66 echo $p->post_title;
67 } else {
68 echo $item['description'];
69 }
70 ?>
71 </td> 71 </td>
72 <td><input type="checkbox" checked="checked" name="keys[<?php echo $key?>]" /> <span style="color:#999;font-size:11px;">(-$<?php echo number_format($item['total'], 2, ".", "");?>)</span></td>
73 </tr> 72 </tr>
74 <?php endforeach; ?> 73 <?php endforeach; ?>
75 </table> 74 </tbody></table>
76 75
77 76 <div style="padding-top:8px;margin-top:25px;">
78 <div style="border-top:1px solid #ccc;padding-top:8px;margin-top:25px;"> 77 <table cellpadding="0" cellspacing="0" width="100%" border="0">
79
80 <table cellpadding="0" cellspacing="0" width="100%" border="0" style="border-collapse:collapse;">
81 <tr> 78 <tr>
82 <td> 79 <td>
83 <input name="force_cancellation_fee" value="on" type="checkbox" checked="checked" />&nbsp;<label>Apply cancellation fee?</label> 80 <input name="force_cancellation_fee" id="force_cancellation_fee" value="1" type="checkbox" checked="checked" />
81 <label for="force_cancellation_fee">Apply cancellation fee?</label>
82 <label for="force_cancellation_fee" class="amount">(<?php echo '$150.00'; ?>)</label>
84 </td> 83 </td>
85 <td width="400" style="text-align:right;"> 84 <td width="400" style="text-align:right;">
86 <input type="submit" value="Apply Changes" /> <input type="button" value="Cancel Changes" id="refund_cancel_btn" /> 85 <input type="submit" value="Apply Changes" /> <input type="button" value="Cancel Changes" id="refund_cancel_btn" />
87 </td> 86 </td>
88 </tr> 87 </tr>
89 </table> 88 </table>
90
91
92 </div> 89 </div>
93
94 </form> 90 </form>
95
96 </div> 91 </div>
97
98 </div> 92 </div>
99
100 <script type="text/javascript">
101
102 jQuery(document).ready(function($) {
103 $('#invoice_refund_form').ajaxForm({
104 url: '/wp-admin/admin-ajax.php'
105 , data: ({ajax:"yes", action: 'issue_invoice_refund', invoice_id: <?php echo $invoice->ID?>})
106 , dataType: 'json'
107 , type: 'POST'
108 , success: function(data) {
109 if (data.success == 'true') {
110 document.location.href = document.location.href;
111 } else {
112 jAlert(data.msg, 'Server Error Encountered');
113 }
114 }
115 });
116
117
118 $('#refund_cancel_btn').click(function(e) {
119 e.preventDefault();
120 $.colorbox.close();
121 });
122 });
123
124 </script>
125
126 </body> 93 </body>
127 </html> 94 </html>
...\ No newline at end of file ...\ No newline at end of file
......