Skip to content

Commit

Permalink
Implemented improved fine system
Browse files Browse the repository at this point in the history
Fines are now calculated dynamically adding all active fines then
subtracting all fine payments
All fine payments are now logged with the date and librarian that
authorised the payment
Renamed fine meta 'wp_lib_fine' to 'wp_lib_owed' to avoid confusion with
loan meta 'wp_lib_fine', which holds the post ID of the fine connected
to the loan
Added activation hook to remove deprecated meta 'wp_lib_owed' from
member meta
Added activation took to rename meta change noted above
Removed deprecated helper get_member_owed()
  • Loading branch information
kittsville committed Feb 1, 2015
1 parent 28f3cdc commit 3991a18
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 59 deletions.
30 changes: 12 additions & 18 deletions helpers/ajax.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -563,25 +563,19 @@ private function doPayFine() {
// Validates Nonce
$this->checkNonce( 'Pay Member Fines ' . $member->ID );

// Fetches member's current amount owed in fines
$owed = wp_lib_fetch_member_owed( $member->ID );
// Attempts to pay fine
$success = $member->payMoneyOwed( $fine_payment );

// If fine payment is negative or failed to validate (resulting in 0), call error
if ( $fine_payment <= 0 )
$this->stopAjax( 320 );
// If proposed amount is greater than the amount that needs to be paid, call error
elseif ( $fine_payment > $owed )
$this->stopAjax( 321 );

// Subtracts proposed amount from amount owed by member
$owed = $owed - $fine_payment;

// Updates member's amount owed
update_post_meta( $member->ID, 'wp_lib_owed', $owed );
// If payment failed, send error to user
if ( wp_lib_is_error( $success ) )
$this->stopAjax( $success );

// Sets up notification for successful fine reduction
$notification = wp_lib_format_money( $fine_payment ) . ' in fines has been paid by ' . get_the_title( $member->ID ) . '.';

// Fetches new amount owed by member
$owed = $member->getMoneyOwed();

// If money is still owed by the member, inform librarian
if ( $owed != 0 )
$notification .= ' ' . wp_lib_format_money( $owed ) . ' is still owed.';
Expand Down Expand Up @@ -905,7 +899,7 @@ private function prepMemberMetaBox( $member ) {
array( 'Email', $meta['wp_lib_member_email'][0] ),
array( 'Phone', $meta['wp_lib_member_phone'][0] ),
array( 'Mobile', $meta['wp_lib_member_mobile'][0] ),
array( 'Owed', wp_lib_format_money( wp_lib_fetch_member_owed( $member->ID ) ) ),
array( 'Owed', wp_lib_format_money( $member->getMoneyOwed() ) ),
array( 'On Loan', wp_lib_prep_members_items_out( $member->ID ) )
);

Expand Down Expand Up @@ -972,7 +966,7 @@ private function prepFineMetaBox( $fine ) {
array( 'Item', wp_lib_manage_item_dash_hyperlink( $meta['wp_lib_item'][0] ) ),
array( 'Member', wp_lib_manage_member_dash_hyperlink( $meta['wp_lib_member'][0] ) ),
array( 'Creator', $this->getUserName( get_post_field( 'post_author', $fine->ID ) ) ),
array( 'Amount', wp_lib_format_money( $meta['wp_lib_fine'][0] ) ),
array( 'Amount', wp_lib_format_money( $meta['wp_lib_owed'][0] ) ),
array( 'Status', wp_lib_format_fine_status( $meta['wp_lib_status'][0] ) ),
array( 'Created', get_the_date( '', $fine->ID ) )
)
Expand Down Expand Up @@ -1373,7 +1367,7 @@ private function genManageMember() {
);

// Fetches amount owed by member to Library
$owed = wp_lib_fetch_member_owed( $member->ID );
$owed = $member->getMoneyOwed();

// If money is owed by the member
if ( $owed > 0 ) {
Expand Down Expand Up @@ -2029,7 +2023,7 @@ private function genPayMemberFines() {
$member = $this->getMember();

// Checks that there is actually money owed, stopping page load on failure
if ( wp_lib_fetch_member_owed( $member->ID ) == 0 )
if ( $member->getMoneyOwed() <= 0 )
$this->stopAjax( 206 );

$this->sendPage(
Expand Down
1 change: 0 additions & 1 deletion helpers/error.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ class WP_LIB_ERROR {
318 => 'Given barcode invalid',
319 => 'No item found with that barcode',
320 => 'Fine payment amount is invalid',
321 => 'Proposed fine payment is greater than amount owed by member',
322 => 'Loan must be scheduled and the start date must have passed to give item to member',
323 => 'Item renewal date must be after item\'s current due date',
324 => 'Proposed return date is before item was loaned',
Expand Down
11 changes: 9 additions & 2 deletions lib/admin-tables.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@ public function fillItemsTableColumns( $column, $item_id ) {
* @see http://codex.wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column
*/
public function fillMembersTableColumns( $column, $member_id ) {
if ( $this->row_buffer[0] !== $member_id ) {
$this->row_buffer = array(
$member_id,
WP_LIB_MEMBER::create( $this->wp_librarian, $member_id )
);
}

switch ( $column ) {
// Displays the current status of the item (On Loan/Available/Late)
case 'member_name':
Expand All @@ -178,7 +185,7 @@ public function fillMembersTableColumns( $column, $member_id ) {

// Displays total amount currently owed to the Library in late item fines
case 'member_fines':
echo wp_lib_format_money( wp_lib_fetch_member_owed( $member_id ) );
echo wp_lib_format_money( $this->row_buffer[1]->getMoneyOwed() );
break;

// Displays total number of items donated by the member to the Library
Expand Down Expand Up @@ -299,7 +306,7 @@ public function fillFinesTableColumns( $column, $fine_id ) {
// Displays total charge to member
case 'fine_amount':
// Fetches fine amount from fine's post meta
$fine = get_post_meta( $fine_id, 'wp_lib_fine', true );
$fine = get_post_meta( $fine_id, 'wp_lib_owed', true );

// Formats fine with local currency and displays it
echo wp_lib_format_money( $fine );
Expand Down
23 changes: 0 additions & 23 deletions lib/fine.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,6 @@ public function cancel() {
// If fine is not active (has been cancelled), call error
if ( $fine_status !== '1' )
return wp_lib_error( 313 );

// Fetches member from fine meta
$member = WP_LIB_MEMBER::create( $this->wp_librarian, get_post_meta( $this->ID, 'wp_lib_member', true ) );

if ( wp_lib_is_error( $member ) )
return $member;

// Fetches current amount owed by member
$owed = $member->getOwed();

// Fetches fine total
$fine_total = get_post_meta( $this->ID, 'wp_lib_fine', true );

// If cancelling fine would leave member with negative money owed, call error
if ( $owed - $fine_total < 0 ) {
return wp_lib_error( 207 );
}

// Removes fine from member's debt
$owed -= $fine_total;

// Updates member debt
update_post_meta( $member->ID, 'wp_lib_owed', $owed );

// Changes fine status to Cancelled
update_post_meta( $this->ID, 'wp_lib_status', 2 );
Expand Down
2 changes: 1 addition & 1 deletion lib/loan.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public function returnItem( $date = false, $charge_fine = null ) {
'wp_lib_loan' => $this->ID,
'wp_lib_member' => $member_id,
'wp_lib_status' => 1,
'wp_lib_fine' => $fine_amount
'wp_lib_owed' => $fine_amount
)
);

Expand Down
69 changes: 69 additions & 0 deletions lib/member.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,74 @@ class WP_LIB_MEMBER extends WP_LIB_OBJECT {
public static function create( WP_LIBRARIAN $wp_librarian, $member_id ) {
return parent::create( $wp_librarian, $member_id, __class__, 'wp_lib_members', 'Member' );
}

/**
* Calculates amount currently owed by member in late fines
* @return float|int Amount currently owed by member without currency symbol
*/
public function getMoneyOwed() {
// Queries WP for all loans to current member where a fine was incurred
$query = new WP_Query(array(
'post_type' => 'wp_lib_loans',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'wp_lib_member',
'value' => $this->ID,
'compare' => '='
),
array(
'key' => 'wp_lib_fine',
'compare' => 'EXISTS'
)
)
));

// Initialises amount owed by member in fines
$owed = 0;

if ( $query->have_posts() ){
while ( $query->have_posts() ) {
$query->the_post();

// Fetches fine ID from loan post meta
$fine_id = get_post_meta( get_the_ID(), 'wp_lib_fine', true );

// If fine isn't active (fine was cancelled), skip adding to amount owed
if ( get_post_meta( $fine_id, 'wp_lib_status', true ) !== '1' )
continue;

// Adds fine amount for current loan to total amount owed
$owed += get_post_meta( $fine_id, 'wp_lib_owed', true );
}
}

// Fetches all times member has paid off their late fines
foreach ( get_post_meta( $this->ID, 'wp_lib_payments' ) as $fine_payment ) {
// Subtracts fine payment from amount member owes
$owed -= $fine_payment[1];
}

return $owed;
}

/**
* Adds fine payment to member records
* Fine payments that result in a negative amount owed are allowed
* @param float|int $payment Amount to be paid
* @return bool|WP_LIB_ERROR True on success, error on failure
*/
public function payMoneyOwed( $payment ) {
// If fine payment is negative or failed to validate (resulting in 0), call error
if ( $payment <= 0 )
return wp_lib_error( 320 );

// Creates record of fine payment
add_post_meta( $this->ID, 'wp_lib_payments', array(
current_time('timestamp'), // Date of fine payment
$payment, // Amount paid of member fines
get_current_user_id() // Librarian to authorise fine payment
));
}
}
?>
43 changes: 41 additions & 2 deletions lib/wp-librarian.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,52 @@ public function loadAdminTemplate( $name ) {
*/
public function runOnActivation() {
$version = get_option( 'wp_lib_version', false );

// GGGG add thing to remove meta 'wp_lib_owed' from all members
// If plugin has been previously installed
if ( is_array( $version ) ) {
// If previous plugin version is version 0.1 or older, remove depreciated data
if ( version_compare( $version['version'], '0.1', '<=' ) )
if ( version_compare( $version['version'], '0.1', '<=' ) ) {
delete_option( 'wp_lib_default_media_types' );
delete_option( 'wp_lib_taxonomy_spacer' );

// Deletes member post meta 'wp_lib_owed'
$members_query = new WP_Query(array(
'post_type' => 'wp_lib_members',
'meta_query' => array(
array(
'key' => 'wp_lib_owed',
'compare' => 'EXISTS'
)
)
));

if ( $members_query->have_posts() ){
while ( $members_query->have_posts() ) {
$members_query->the_post();

delete_post_meta( get_the_ID(), 'wp_lib_owed' );
}
}

// Renames fine post meta 'wp_lib_fine' to 'wp_lib_owed'
$fines_query = new WP_Query(array(
'post_type' => 'wp_lib_fines',
));

if ( $fines_query->have_posts() ){
while ( $fines_query->have_posts() ) {
$fines_query->the_post();

$fine_id = get_the_ID();

$fine_amount = get_post_meta( $fine_id, 'wp_lib_fine', true );

delete_post_meta( $fine_id, 'wp_lib_fine' );

update_post_meta( $fine_id, 'wp_lib_owed', $fine_amount );
}
}
}
} else {
// Sets current user as a Library Admin
wp_lib_update_user_meta( get_current_user_id(), 10 );
Expand Down
12 changes: 0 additions & 12 deletions wp-librarian-helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -602,18 +602,6 @@ function wp_lib_prep_members_items_out( $member_id ) {
return $query->post_count;
}

// Fetches amount member owes Library in fines
function wp_lib_fetch_member_owed( $member_id ) {
// Fetches total money owed by member to Library
$owed = get_post_meta( $member_id, 'wp_lib_owed', true );

// If blank, assumes nothing is owed
if ( $owed == '' )
$owed = 0;

return $owed;
}

// Updates multiple meta values of a post
function wp_lib_update_meta( $post_id, $meta_array ) {
foreach ( $meta_array as $key => $value ) {
Expand Down

0 comments on commit 3991a18

Please sign in to comment.