2014-12-26 06:26:26 +00:00
< ? php
2015-01-07 07:59:33 +00:00
require_once ( '../Connections/database_functions.php' );
2014-12-26 06:26:26 +00:00
require_once ( '../Connections/YBDB.php' );
mysql_select_db ( $database_YBDB , $YBDB );
2015-01-10 07:54:12 +00:00
$change_fund = CHANGE_FUND ;
2015-04-03 06:54:16 +00:00
$csv_directory = CSV_DIRECTORY ;
2014-12-26 06:26:26 +00:00
2015-01-07 07:59:33 +00:00
// Is there a current shop?
if ( isset ( $_POST [ 'shop_exist' ])) {
2015-01-15 07:33:09 +00:00
if ( current_shop_by_ip () >= 1 ) {
echo " current_shop " ;
} else {
echo " no_shop " ;
}
2017-07-17 06:02:32 +00:00
}
2014-12-29 07:53:41 +00:00
// update whether paid or not
2014-12-26 06:26:26 +00:00
if ( isset ( $_POST [ 'paid' ])) {
if ( $_POST [ 'paid' ] == 1 ) {
$query = " UPDATE transaction_log SET paid=1 WHERE transaction_id= " . $_POST [ 'transaction_id' ] . " ; " ;
$result = mysql_query ( $query , $YBDB ) or die ( mysql_error ());
} elseif ( $_POST [ 'paid' ] == 0 ) {
$query = " UPDATE transaction_log SET paid=0 WHERE transaction_id= " . $_POST [ 'transaction_id' ] . " ; " ;
$result = mysql_query ( $query , $YBDB ) or die ( mysql_error ());
}
}
2014-12-29 07:53:41 +00:00
// update payment type
if ( isset ( $_POST [ 'payment_type' ])) {
$query = 'UPDATE transaction_log SET payment_type="' .
$_POST [ 'payment_type' ] . '" WHERE transaction_id=' .
$_POST [ 'transaction_id' ] . " ; " ;
$result = mysql_query ( $query , $YBDB ) or die ( mysql_error ());
}
2015-01-06 08:25:57 +00:00
// If payment_type check is selected - return check number if exists
if ( isset ( $_POST [ 'check_number' ])) {
$query = 'SELECT check_number FROM transaction_log WHERE transaction_id="' . $_POST [ 'transaction_id' ] . '";' ;
$sql = mysql_query ( $query , $YBDB ) or die ( mysql_error ());
$result = mysql_fetch_assoc ( $sql );
echo json_encode ( $result );
}
2015-01-10 07:54:12 +00:00
// Editable Change Fund
if ( isset ( $_POST [ 'editable_change' ])) {
$transaction_id = split ( '_' , $_POST [ 'id' ], 1 );
$query = 'UPDATE transaction_log set change_fund="' . $_POST [ 'editable_change' ] . '" WHERE transaction_id="' . $transaction_id [ 0 ] . '";' ;
$result = mysql_query ( $query , $YBDB ) or die ( mysql_error ());
$send_back = array (
" changed_change " => $_POST [ 'editable_change' ],
" change " => $change_fund ,
);
echo json_encode ( $send_back );
}
2015-01-26 18:52:16 +00:00
// Patron who made a transaction not logged in.
if ( isset ( $_POST [ 'not_logged_in' ])) {
2015-01-26 19:15:36 +00:00
$query = " SELECT CONCAT(contacts.last_name, ', ', contacts.first_name, ' ',contacts.middle_initial) AS full_name,
transaction_log . sold_to
2015-01-26 18:52:16 +00:00
FROM transaction_log , contacts
WHERE transaction_id = " . $_POST['transaction_id'] .
" AND contacts.contact_id = transaction_log.sold_to; " ;
$sql = mysql_query ( $query , $YBDB ) or die ( mysql_error ());
$result = mysql_fetch_assoc ( $sql );
2015-01-26 19:15:36 +00:00
echo json_encode ( $result );
2015-01-26 18:52:16 +00:00
}
2015-01-10 11:06:21 +00:00
2017-07-17 20:38:23 +00:00
// Membership Benefits
if ( isset ( $_POST [ 'membership_benefits' ])) {
2017-07-17 06:02:32 +00:00
$query = " SELECT contacts.contact_id, CONCAT(last_name, ', ', first_name, ' ',middle_initial) AS full_name,
CONCAT ( first_name , ' ' , last_name ) AS normal_full_name , contacts . email AS email , contacts . phone AS phone ,
transaction_log . date AS membership_start , SUBSTRING_INDEX ( DATE_ADD ( date , INTERVAL 365 DAY ), ' ' , 1 ) AS expiration_date
FROM transaction_log LEFT JOIN contacts ON transaction_log . sold_to = contacts . contact_id
WHERE SUBSTRING_INDEX ( date , ' ' , 1 ) <= DATE_ADD ( date , INTERVAL 365 DAY )
AND ( transaction_type = 'Memberships' AND paid = 1 ) AND contact_id = " .
2017-07-17 07:11:21 +00:00
$_POST [ 'contact_id' ] .
" ORDER by membership_start DESC; " ;
2017-07-17 06:02:32 +00:00
$sql = mysql_query ( $query , $YBDB ) or die ( mysql_error ());
$result = mysql_fetch_assoc ( $sql );
echo json_encode ( $result );
2017-07-17 20:38:23 +00:00
// SELECT contact_id, full_name, normal_full_name, email, phone, visits, volunteer_hours FROM (SELECT contacts.contact_id, CONCAT(last_name, ', ', first_name, ' ',middle_initial) AS full_name, CONCAT(first_name, ' ', last_name) AS normal_full_name, contacts.email AS email, contacts.phone AS phone, COUNT(shop_hours.contact_id) as visits, ROUND(SUM(HOUR(SUBTIME( TIME(time_out), TIME(time_in))) + MINUTE(SUBTIME( TIME(time_out), TIME(time_in)))/60)) AS volunteer_hours FROM shop_hours LEFT JOIN contacts ON shop_hours.contact_id = contacts.contact_id LEFT JOIN shop_user_roles ON shop_hours.shop_user_role = shop_user_roles.shop_user_role_id WHERE (SUBSTRING_INDEX(time_in, ' ', 1) >= DATE_SUB(CURDATE(),INTERVAL 365 DAY) AND SUBSTRING_INDEX(time_in, ' ', 1) <= DATE_SUB(CURDATE(), INTERVAL 0 DAY)) AND shop_user_roles.volunteer = 1 GROUP BY contact_id) AS members;
} // Membership Benefits
// Volunteer Benefits
if ( isset ( $_POST [ 'volunteer_benefits' ])) {
$query = " SELECT contact_id, full_name, normal_full_name, email, phone, visits, volunteer_hours
FROM ( SELECT contacts . contact_id ,
CONCAT ( last_name , ', ' , first_name , ' ' , middle_initial ) AS full_name ,
CONCAT ( first_name , ' ' , last_name ) AS normal_full_name ,
contacts . email AS email ,
contacts . phone AS phone ,
COUNT ( shop_hours . contact_id ) AS visits ,
ROUND ( SUM ( HOUR ( SUBTIME ( TIME ( time_out ), TIME ( time_in ))) + MINUTE ( SUBTIME ( TIME ( time_out ), TIME ( time_in ))) / 60 )) AS volunteer_hours
FROM shop_hours
LEFT JOIN contacts ON shop_hours . contact_id = contacts . contact_id
LEFT JOIN shop_user_roles ON shop_hours . shop_user_role = shop_user_roles . shop_user_role_id
WHERE ( SUBSTRING_INDEX ( time_in , ' ' , 1 ) >= DATE_SUB ( CURDATE (), INTERVAL 365 DAY )
AND SUBSTRING_INDEX ( time_in , ' ' , 1 ) <= DATE_SUB ( CURDATE (), INTERVAL 0 DAY ))
AND shop_user_roles . volunteer = 1 AND contacts . contact_id = " .
$_POST [ 'contact_id' ] .
" GROUP BY contact_id) AS members; " ;
2017-07-18 04:42:34 +00:00
$sql = mysql_query ( $query , $YBDB ) or die ( mysql_error ());
$result = mysql_fetch_assoc ( $sql );
// current year volunteer hours and visits
$query = " SELECT current_year_visits, current_year_volunteer_hours
FROM ( SELECT contacts . contact_id , CONCAT ( last_name , ', ' , first_name , ' ' , middle_initial ) AS full_name ,
CONCAT ( first_name , ' ' , last_name ) AS normal_full_name , contacts . email AS email ,
contacts . phone AS phone , COUNT ( shop_hours . contact_id ) AS current_year_visits ,
ROUND ( SUM ( HOUR ( SUBTIME ( TIME ( time_out ), TIME ( time_in ))) + MINUTE ( SUBTIME ( TIME ( time_out ), TIME ( time_in ))) / 60 )) AS current_year_volunteer_hours
FROM shop_hours
LEFT JOIN contacts ON shop_hours . contact_id = contacts . contact_id
LEFT JOIN shop_user_roles ON shop_hours . shop_user_role = shop_user_roles . shop_user_role_id
WHERE ( SUBSTRING_INDEX ( time_in , ' ' , 1 ) >= CONCAT ( YEAR ( CURDATE ()), '-01-01' )
AND SUBSTRING_INDEX ( time_in , ' ' , 1 ) <= DATE_ADD ( CONCAT ( YEAR ( CURDATE ()), '-01-01' ), INTERVAL DAYOFYEAR ( CONCAT ( YEAR ( CURDATE ()), '-12-31' )) - 1 DAY ))
AND shop_user_roles . volunteer = 1
AND contacts . contact_id = " .
$_POST [ 'contact_id' ] .
" ) AS members; " ;
2017-07-18 05:02:44 +00:00
$sql = mysql_query ( $query , $YBDB ) or die ( mysql_error ());
$result2 = mysql_fetch_assoc ( $sql );
$result = ( object ) array_merge (( array ) $result , ( array ) $result2 );
2017-07-18 04:42:34 +00:00
2017-07-18 05:02:44 +00:00
2017-07-18 04:42:34 +00:00
echo json_encode ( $result );
2017-07-17 20:38:23 +00:00
} // Volunteer Benefits
2017-07-17 06:02:32 +00:00
2015-01-12 05:19:09 +00:00
// Anonymous transaction - save and communicate back settings
if ( isset ( $_POST [ 'anonymous' ])) {
if ( $_POST [ 'anonymous' ] == 1 ) {
$query = 'UPDATE transaction_log SET anonymous=1, sold_to=NULL WHERE transaction_id="' .
$_POST [ 'transaction_id' ] . '";' ;
$result = mysql_query ( $query , $YBDB ) or die ( mysql_error ());
} else {
$query = 'UPDATE transaction_log SET anonymous=0 WHERE transaction_id="' .
$_POST [ 'transaction_id' ] . '";' ;
$result = mysql_query ( $query , $YBDB ) or die ( mysql_error ());
}
}
2015-02-08 09:00:06 +00:00
// Transaction history - fetch history
2015-02-06 09:50:57 +00:00
if ( isset ( $_POST [ 'history_select' ])) {
$query = 'SELECT history FROM transaction_log WHERE transaction_id="' . $_POST [ 'transaction_id' ] . '";' ;
$sql = mysql_query ( $query , $YBDB ) or die ( mysql_error ());
$result = mysql_fetch_assoc ( $sql );
if ( $result [ 'history' ] == " " ) {
echo " First Transaction " ;
} else {
2015-02-11 01:55:38 +00:00
// Description may have newlines
$history_result = str_replace ( " \n " , " \\ n " , $result [ 'history' ]);
echo $history_result ;
2015-02-06 09:50:57 +00:00
}
}
2017-07-17 06:02:32 +00:00
2015-02-08 09:00:06 +00:00
// Transaction history - update transaction history
2015-02-11 01:55:38 +00:00
// Note: This could easily be turned into its own table with a foreign key
// referencing transaction_log.transaction_id, but most transactions
// will probably only occur 1 time, and there probably isn't that much
// need to do many things with this data other than rollback a transaction, or
// research what happened on a particular shop day.
2015-02-06 09:50:57 +00:00
if ( isset ( $_POST [ 'history_update' ])) {
$json = json_encode ( $_POST [ 'history' ]);
$query = " UPDATE transaction_log SET history=' $json ' " .
' WHERE transaction_id="' . $_POST [ 'transaction_id' ] . '";' ;
$result = mysql_query ( $query , $YBDB ) or die ( mysql_error ());
2015-02-11 01:55:38 +00:00
// show history
if ( isset ( $_POST [ 'more_than_one' ])) {
list_history ( $_POST [ 'history' ]);
}
2015-02-06 09:50:57 +00:00
}
2015-02-08 09:00:06 +00:00
// Check for most recent transaction_id if transaction_id has changed
if ( isset ( $_POST [ 'most_recent_transaction_id' ])) {
$query = 'SELECT MAX(transaction_id) as transaction_id FROM transaction_log;' ;
$sql = mysql_query ( $query , $YBDB ) or die ( mysql_error ());
$result = mysql_fetch_assoc ( $sql );
echo $result [ 'transaction_id' ];
}
2015-01-29 07:32:02 +00:00
// check if start storage date has been changed since original shop date
if ( isset ( $_POST [ 'date_startstorage' ])) {
$query = 'SELECT shops.date FROM transaction_log, shops WHERE transaction_id=' . $_POST [ 'transaction_id' ] .
' AND transaction_log.shop_id = shops.shop_id;' ;
$sql = mysql_query ( $query , $YBDB ) or die ( mysql_error ());
$result = mysql_fetch_assoc ( $sql );
if ( $result [ 'date' ] != $_POST [ 'date_startstorage' ]) {
echo $result [ 'date' ];
}
}
2015-01-12 05:19:09 +00:00
2015-01-10 11:06:21 +00:00
// reset payment_type && amount for storage transaction
if ( isset ( $_POST [ 'storage_payment_reset' ])) {
$query = 'UPDATE transaction_log SET payment_type=NULL, amount=NULL WHERE transaction_id="' .
$_POST [ 'transaction_id' ] . '";' ;
$result = mysql_query ( $query , $YBDB ) or die ( mysql_error ());
}
2015-01-24 03:33:17 +00:00
// populate transaction slider for accounting programs
2015-04-02 03:57:10 +00:00
2015-04-02 20:15:59 +00:00
// Originally, deposits of $0 (amount > 0) would not be considered real deposits, however,
2015-04-02 03:57:10 +00:00
// there may be shops where only non-monetary transactions occurred (amount >= 0)
// which would be useful to record in an accounting program.
// One caveat, if a monetary transaction is recorded, but the depositor only
// enters $0, the deposit will show "Difference: n/a", however this should be a cue
2015-04-02 20:15:59 +00:00
// since it should be obvious that a real world deposit of $0
// would not be made at a bank.
2015-01-24 03:33:17 +00:00
if ( isset ( $_POST [ 'transaction_slider' ])) {
2015-04-02 03:57:10 +00:00
$query = ' SELECT transaction_id , IF ( amount >= 0 , " yes " , " no " ) AS " deposited " , date
2015-01-24 03:33:17 +00:00
FROM transaction_log WHERE transaction_type = " Deposit " ; ' ;
$sql = mysql_query ( $query , $YBDB ) or die ( mysql_error ());
while ( $result = mysql_fetch_assoc ( $sql ) ) {
$slider_range [] = $result ;
2015-01-25 23:53:08 +00:00
}
// this is the first real deposit
2015-01-26 02:09:04 +00:00
if ( ( $slider_range && ! $slider_range [ 1 ][ " transaction_id " ]) || ( $slider_range && $slider_range [ 1 ][ " deposited " ] == " no " ) ) {
2015-01-26 02:43:47 +00:00
$fake_trans_id = 0 ;
2015-01-25 23:53:08 +00:00
$real_trans = $slider_range [ 0 ];
$year = date ( " Y " );
$slider_range [ 0 ] = array ( " transaction_id " => " $fake_trans_id " , " deposited " => " yes " , " date " => " $year -01-01 22:22:22 " );
$slider_range [ 1 ] = $real_trans ;
echo json_encode ( $slider_range );
// no real deposits exist
} elseif ( ! $slider_range ) {
// send fake data
$year = date ( " Y " );
$slider_range = array
2015-01-26 02:43:47 +00:00
( array ( " transaction_id " => " 0 " , " deposited " => " yes " , " date " => " $year -01-01 22:22:22 " ),
array ( " transaction_id " => " 1 " , " deposited " => " yes " , " date " => " $year -01-02 22:22:22 " ),
2015-01-25 23:53:08 +00:00
);
echo json_encode ( $slider_range );
// more than 1 deposit exists
} else {
echo json_encode ( $slider_range );
}
2015-01-24 03:33:17 +00:00
}
2015-04-03 06:54:16 +00:00
2015-04-06 07:31:42 +00:00
// Create csv file(s) for GnuCash
2015-04-03 06:54:16 +00:00
if ( isset ( $_POST [ 'gnucash_account_type' ])) {
2015-04-06 07:31:42 +00:00
2015-04-11 02:35:00 +00:00
/* require_once ( '../php-console/src/PhpConsole/__autoload.php' );
2015-04-06 07:31:42 +00:00
$handler = PhpConsole\Handler :: getInstance ();
$handler -> start (); */
2015-04-03 06:54:16 +00:00
$transaction_range = $_POST [ 'transaction_range' ];
$account_type = $_POST [ 'gnucash_account_type' ];
2015-04-06 07:31:42 +00:00
$accounts_gnucash = array_flip ( $gnucash_accounts );
2015-04-03 06:54:16 +00:00
// Date (yyyy-mm-dd), Num, Description, Deposit, Account
// checking (check or cash) || credit
// transaction has been 1) paid and is 2) cash & check [checking] or credit and 3) deposited
if ( $account_type === 'checking' ) {
2015-04-11 04:13:22 +00:00
// first statement to find coordinator for associated transactions
2015-04-11 02:35:00 +00:00
$query = " SELECT transaction_id, " .
" CONCAT(contacts.first_name, ' ', contacts.last_name) AS 'coordinator' " .
" FROM transaction_log, contacts WHERE paid=1 AND date!='NULL' " .
" AND (payment_type='cash' OR payment_type='check') " .
" AND contacts.contact_id = transaction_log.sold_by " .
" AND (transaction_id> " . $transaction_range [ 0 ] . " AND transaction_id< " . $transaction_range [ 1 ] . " ); " ;
$sql2 = mysql_query ( $query , $YBDB ) or die ( mysql_error ());
$coordinator = [];
while ( $result = mysql_fetch_assoc ( $sql2 ) ) {
$coordinator [ $result [ 'transaction_id' ]] = $result [ 'coordinator' ];
2015-04-11 04:13:22 +00:00
}
// second statement to find normal transactions
2015-04-11 05:15:47 +00:00
$query = " SELECT SUBSTRING_INDEX(date, ' ', 1) AS 'date', transaction_id, transaction_type, check_number, description, amount, " .
2015-04-11 04:13:22 +00:00
" CONCAT(contacts.first_name, ' ', contacts.last_name) AS 'patron' " .
" FROM transaction_log, contacts WHERE paid=1 AND date!='NULL' " .
" AND (payment_type='cash' OR payment_type='check') " .
" AND contacts.contact_id = transaction_log.sold_to " .
" AND (transaction_id> " . $transaction_range [ 0 ] . " AND transaction_id< " . $transaction_range [ 1 ] . " ); " ;
$sql2 = mysql_query ( $query , $YBDB ) or die ( mysql_error ());
// third statement to find anonymous transactions
2015-04-11 05:15:47 +00:00
$query = " SELECT SUBSTRING_INDEX(date, ' ', 1) AS 'date', transaction_id, transaction_type, check_number, description, amount " .
2015-04-11 04:13:22 +00:00
" FROM transaction_log WHERE paid=1 AND date!='NULL' " .
" AND (payment_type='cash' OR payment_type='check') " .
" AND anonymous=1 " .
" AND (transaction_id> " . $transaction_range [ 0 ] . " AND transaction_id< " . $transaction_range [ 1 ] . " ); " ;
$sql3 = mysql_query ( $query , $YBDB ) or die ( mysql_error ());
2015-04-11 02:35:00 +00:00
2015-04-06 07:31:42 +00:00
$gnucash_csv_file = " " ;
2015-04-11 04:13:22 +00:00
// normal transaction
while ( $result = mysql_fetch_assoc ( $sql2 ) ) {
2015-04-06 07:31:42 +00:00
$description = preg_replace ( '/\n/' , ' \r ' , $result [ 'description' ]);
$description = preg_replace ( '/\r/' , '\r' , $description );
$description = preg_replace ( '/,/' , ';' , $description );
2015-04-11 05:15:47 +00:00
if ( $result [ 'check_number' ] != " NULL " ) {
$check_number = $result [ 'check_number' ];
}
$gnucash_csv_file .= $result [ 'date' ] . ', ' . $result [ 'transaction_id' ] . ' ' . $check_number .
2015-04-11 04:13:22 +00:00
',' . ' [' . $coordinator [ $result [ 'transaction_id' ]] . ' => ' . $result [ 'patron' ] . '] ' .
$description . ' (Income:' . $result [ 'transaction_type' ] . ') ' .
2015-04-07 05:55:32 +00:00
', ' . $result [ 'amount' ] . ', ' .
2015-04-06 07:31:42 +00:00
$accounts_gnucash [ 'checking' ] . " \n " ;
}
2015-04-11 04:13:22 +00:00
// anonymous transaction
while ( $result = mysql_fetch_assoc ( $sql3 ) ) {
$description = preg_replace ( '/\n/' , ' \r ' , $result [ 'description' ]);
$description = preg_replace ( '/\r/' , '\r' , $description );
$description = preg_replace ( '/,/' , ';' , $description );
2015-04-11 05:15:47 +00:00
if ( $result [ 'check_number' ] != " NULL " ) {
$check_number = $result [ 'check_number' ];
}
$gnucash_csv_file .= $result [ 'date' ] . ', ' . $result [ 'transaction_id' ] . ' ' . $check_number .
2015-04-11 04:13:22 +00:00
',' . ' [' . $coordinator [ $result [ 'transaction_id' ]] . ' => Anonymous] ' .
$description . ' (Income:' . $result [ 'transaction_type' ] . ') ' .
', ' . $result [ 'amount' ] . ', ' .
$accounts_gnucash [ 'checking' ] . " \n " ;
}
2015-04-03 06:54:16 +00:00
2015-04-06 07:31:42 +00:00
$file_name = preg_replace ( '/ /' , '_' , $accounts_gnucash [ 'checking' ]);
$file_name = preg_replace ( '/:/' , '-' , $file_name );
$file_name = $file_name . '-' . $transaction_range [ 0 ] . '-' . $transaction_range [ 1 ] . '.csv' ;
$file = '../' . $csv_directory . '/' . $file_name ;
$csv_file = fopen ( $file , " w " ) or die ( " Unable to open file for writing. " );
fwrite ( $csv_file , $gnucash_csv_file );
fclose ( $csv_file );
2015-04-06 08:39:31 +00:00
echo $file ;
2015-04-03 06:54:16 +00:00
}
if ( $account_type === 'credit' ) {
2015-04-11 04:13:22 +00:00
// first statement to find coordinator for associated transactions
2015-04-11 02:35:00 +00:00
$query = " SELECT transaction_id, " .
" CONCAT(contacts.first_name, ' ', contacts.last_name) AS 'coordinator' " .
" FROM transaction_log, contacts WHERE paid=1 AND date!='NULL' " .
" AND payment_type='credit' " .
" AND contacts.contact_id = transaction_log.sold_by " .
" AND (transaction_id> " . $transaction_range [ 0 ] . " AND transaction_id< " . $transaction_range [ 1 ] . " ); " ;
2015-04-11 04:13:22 +00:00
$sql = mysql_query ( $query , $YBDB ) or die ( mysql_error ());
2015-04-11 02:35:00 +00:00
$coordinator = [];
2015-04-11 04:13:22 +00:00
while ( $result = mysql_fetch_assoc ( $sql ) ) {
2015-04-11 02:35:00 +00:00
$coordinator [ $result [ 'transaction_id' ]] = $result [ 'coordinator' ];
2015-04-11 04:13:22 +00:00
}
// second statement to find normal transactions
$query = " SELECT SUBSTRING_INDEX(date, ' ', 1) AS 'date', transaction_id, transaction_type, description, amount, " .
" CONCAT(contacts.first_name, ' ', contacts.last_name) AS 'patron' " .
" FROM transaction_log, contacts WHERE paid=1 AND date!='NULL' " .
" AND payment_type='credit' " .
" AND contacts.contact_id = transaction_log.sold_to " .
" AND (transaction_id> " . $transaction_range [ 0 ] . " AND transaction_id< " . $transaction_range [ 1 ] . " ); " ;
$sql2 = mysql_query ( $query , $YBDB ) or die ( mysql_error ());
// third statement to find anonymous transactions
$query = " SELECT SUBSTRING_INDEX(date, ' ', 1) AS 'date', transaction_id, transaction_type, description, amount " .
" FROM transaction_log WHERE paid=1 AND date!='NULL' " .
" AND payment_type='credit' " .
" AND anonymous=1 " .
" AND (transaction_id> " . $transaction_range [ 0 ] . " AND transaction_id< " . $transaction_range [ 1 ] . " ); " ;
$sql3 = mysql_query ( $query , $YBDB ) or die ( mysql_error ());
2015-04-11 02:35:00 +00:00
2015-04-11 04:13:22 +00:00
$gnucash_csv_file = " " ;
// normal transaction
while ( $result = mysql_fetch_assoc ( $sql2 ) ) {
$description = preg_replace ( '/\n/' , ' \r ' , $result [ 'description' ]);
$description = preg_replace ( '/\r/' , '\r' , $description );
$description = preg_replace ( '/,/' , ';' , $description );
$gnucash_csv_file .= $result [ 'date' ] . ', ' . $result [ 'transaction_id' ] .
',' . ' [' . $coordinator [ $result [ 'transaction_id' ]] . ' => ' . $result [ 'patron' ] . '] ' .
$description . ' (Income:' . $result [ 'transaction_type' ] . ') ' .
', ' . $result [ 'amount' ] . ', ' .
$accounts_gnucash [ 'credit' ] . " \n " ;
}
// anonymous transaction
while ( $result = mysql_fetch_assoc ( $sql3 ) ) {
2015-04-06 07:31:42 +00:00
$description = preg_replace ( '/\n/' , ' \r ' , $result [ 'description' ]);
$description = preg_replace ( '/\r/' , '\r' , $description );
$description = preg_replace ( '/,/' , ';' , $description );
$gnucash_csv_file .= $result [ 'date' ] . ', ' . $result [ 'transaction_id' ] .
2015-04-11 04:13:22 +00:00
',' . ' [' . $coordinator [ $result [ 'transaction_id' ]] . ' => Anonymous] ' .
$description . ' (Income:' . $result [ 'transaction_type' ] . ') ' .
2015-04-07 05:55:32 +00:00
', ' . $result [ 'amount' ] . ', ' .
2015-04-11 04:13:22 +00:00
$accounts_gnucash [ 'credit' ] . " \n " ;
2015-04-06 07:31:42 +00:00
}
$file_name = preg_replace ( '/ /' , '_' , $accounts_gnucash [ 'credit' ]);
$file_name = preg_replace ( '/:/' , '-' , $file_name );
$file_name = $file_name . '-' . $transaction_range [ 0 ] . '-' . $transaction_range [ 1 ] . '.csv' ;
$file = '../' . $csv_directory . '/' . $file_name ;
$csv_file = fopen ( $file , " w " ) or die ( " Unable to open file for writing. " );
fwrite ( $csv_file , $gnucash_csv_file );
fclose ( $csv_file );
2015-04-06 08:39:31 +00:00
echo $file ;
2015-04-03 06:54:16 +00:00
}
2015-04-06 07:31:42 +00:00
} // Create csv file(s) for GnuCash
2015-01-24 03:33:17 +00:00
2015-01-02 22:44:27 +00:00
// Deposit Calculator
if ( isset ( $_POST [ 'deposit' ])) {
2015-01-02 10:12:37 +00:00
2015-01-03 07:50:48 +00:00
$visible_count = count ( $_POST [ 'deposit' ]);
$c = $visible_count - 1 ;
$deposit = $_POST [ 'deposit' ];
2015-01-02 10:12:37 +00:00
2015-01-03 07:50:48 +00:00
$query = 'SELECT COUNT(transaction_type) AS "count" FROM transaction_log WHERE transaction_type="Deposit";' ;
$sql = mysql_query ( $query , $YBDB ) or die ( mysql_error ());
$result = mysql_fetch_assoc ( $sql );
2015-01-02 10:12:37 +00:00
2015-01-04 01:35:19 +00:00
if ( $visible_count == $result [ " count " ] ) { // 1 or more deposits, and all deposits are visible
2015-01-16 00:18:58 +00:00
2015-01-03 07:50:48 +00:00
foreach ( $deposit as $key => $value ) {
2015-01-16 00:18:58 +00:00
2015-01-03 07:50:48 +00:00
if ( $c > $key ) {
$query = ' SELECT SUM ( IF ( payment_type = " check " , amount , 0 )) AS " check " ,
SUM ( IF ( payment_type = " credit " , amount , 0 )) AS " credit " ,
SUM ( IF ( payment_type = " cash " , amount , 0 )) AS " cash "
FROM transaction_log WHERE paid = 1 AND transaction_id < ' . $deposit[$key] . ' AND transaction_id > '
. $deposit [ $key + 1 ] . ';' ;
$sql = mysql_query ( $query , $YBDB ) or die ( mysql_error ());
$result = mysql_fetch_assoc ( $sql );
2015-01-03 11:00:33 +00:00
$result_obj [ $deposit [ $key ]] = $result ;
2015-01-16 00:18:58 +00:00
} else {
2015-01-03 07:50:48 +00:00
$query = ' SELECT SUM ( IF ( payment_type = " check " , amount , 0 )) AS " check " ,
SUM ( IF ( payment_type = " credit " , amount , 0 )) AS " credit " ,
SUM ( IF ( payment_type = " cash " , amount , 0 )) AS " cash "
FROM transaction_log WHERE paid = 1 AND transaction_id < ' . $deposit[$key] . ' ; ' ;
$sql = mysql_query ( $query , $YBDB ) or die ( mysql_error ());
$result = mysql_fetch_assoc ( $sql );
2015-01-03 11:00:33 +00:00
$result_obj [ $deposit [ $key ]] = $result ;
2015-01-03 07:50:48 +00:00
}
}
2015-01-03 11:00:33 +00:00
echo json_encode ( $result_obj );
2015-01-03 07:50:48 +00:00
} else { // more deposits than visible
$limit = $visible_count + 1 ;
$query = ' SELECT transaction_id FROM transaction_log
2015-01-16 00:18:58 +00:00
WHERE transaction_type = " Deposit " AND transaction_id <= ' . $deposit [ 0 ] .
' ORDER BY transaction_id DESC LIMIT ' . $limit . ';' ;
2015-01-03 07:50:48 +00:00
$sql = mysql_query ( $query , $YBDB ) or die ( mysql_error ());
while ( $result = mysql_fetch_assoc ( $sql ) ) {
$transaction_id [] = $result [ 'transaction_id' ];
}
2015-01-16 00:18:58 +00:00
foreach ( $transaction_id as $key => $value ) {
if ( $key <= $c && $transaction_id [ $key + 1 ]) {
2015-01-03 07:50:48 +00:00
$query = ' SELECT SUM ( IF ( payment_type = " check " , amount , 0 )) AS " check " ,
SUM ( IF ( payment_type = " credit " , amount , 0 )) AS " credit " ,
SUM ( IF ( payment_type = " cash " , amount , 0 )) AS " cash "
FROM transaction_log WHERE paid = 1 AND transaction_id < ' . $transaction_id[$key] . ' AND transaction_id > '
. $transaction_id [ $key + 1 ] . ';' ;
$sql = mysql_query ( $query , $YBDB ) or die ( mysql_error ());
$result = mysql_fetch_assoc ( $sql );
2015-01-03 11:00:33 +00:00
$result_obj [ $transaction_id [ $key ]] = $result ;
2015-01-16 00:18:58 +00:00
} elseif ( $key <= $c && ! $transaction_id [ $key + 1 ] ) {
$query = ' SELECT SUM ( IF ( payment_type = " check " , amount , 0 )) AS " check " ,
SUM ( IF ( payment_type = " credit " , amount , 0 )) AS " credit " ,
SUM ( IF ( payment_type = " cash " , amount , 0 )) AS " cash "
FROM transaction_log WHERE paid = 1 AND transaction_id < ' . $transaction_id[$key] . ' ; ' ;
$sql = mysql_query ( $query , $YBDB ) or die ( mysql_error ());
$result = mysql_fetch_assoc ( $sql );
$result_obj [ $transaction_id [ $key ]] = $result ;
2015-01-03 11:00:33 +00:00
}
2015-01-03 07:50:48 +00:00
2015-01-03 11:00:33 +00:00
} // foreach
echo json_encode ( $result_obj );
2015-01-03 07:50:48 +00:00
} // end else for invisibles
2015-01-06 08:25:57 +00:00
} // End Deposit Calculator
2015-01-02 10:12:37 +00:00
2014-12-26 06:26:26 +00:00
?>