mirror of
				https://github.com/fspc/Yellow-Bike-Database.git
				synced 2025-10-31 00:45:35 -04:00 
			
		
		
		
	Creates GnuCash csv transaction files!
1) Does not automatically download them the browser yet, but can be found in the designated directory. Works great with GnuCash! Victory!
This commit is contained in:
		
							parent
							
								
									7997087a85
								
							
						
					
					
						commit
						f89c86384a
					
				| @ -154,6 +154,7 @@ define("CSV_DIRECTORY","csv"); | |||||||
| // chown www-data:www-data csv 
 | // chown www-data:www-data csv 
 | ||||||
| // chmod 0700 csv 
 | // chmod 0700 csv 
 | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
| // Define array mapping for Accounts.  Usually Asset Accounts since Income is generally the main type of transaction.
 | // Define array mapping for Accounts.  Usually Asset Accounts since Income is generally the main type of transaction.
 | ||||||
| // Currently four types of accounts are supported:  checking, credit, account_receivable, donation 
 | // Currently four types of accounts are supported:  checking, credit, account_receivable, donation 
 | ||||||
| // 	checking/credit = transaction has been 1) paid and is 2) cash & check [checking] or a credit card [credit] and 3) deposited
 | // 	checking/credit = transaction has been 1) paid and is 2) cash & check [checking] or a credit card [credit] and 3) deposited
 | ||||||
|  | |||||||
| @ -403,7 +403,7 @@ $(function() { | |||||||
| 					$.post("json/transaction.php",{ gnucash_account_type: v, transaction_range: transaction_range});		 | 					$.post("json/transaction.php",{ gnucash_account_type: v, transaction_range: transaction_range});		 | ||||||
| 				}); | 				}); | ||||||
| 			} | 			} | ||||||
| 		});	 | 		});			 | ||||||
| 	 | 	 | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
|  | |||||||
| @ -192,42 +192,88 @@ $csv_directory = CSV_DIRECTORY; | |||||||
| 		 | 		 | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	// Create csv file(s) for GnuCash with this format:
 | 	// Create csv file(s) for GnuCash
 | ||||||
| 	if(isset($_POST['gnucash_account_type'])) { | 	if(isset($_POST['gnucash_account_type'])) { | ||||||
| 		 | 	 | ||||||
|  | 
 | ||||||
|  | /*		require_once('../php-console/src/PhpConsole/__autoload.php'); | ||||||
|  | 		$handler = PhpConsole\Handler::getInstance(); | ||||||
|  | 		$handler->start();*/ | ||||||
|  | 	 | ||||||
| 		$transaction_range = $_POST['transaction_range'];	 | 		$transaction_range = $_POST['transaction_range'];	 | ||||||
| 		$account_type = $_POST['gnucash_account_type'];	 | 		$account_type = $_POST['gnucash_account_type'];	 | ||||||
| 		 | 		$accounts_gnucash = array_flip($gnucash_accounts); | ||||||
|  | 
 | ||||||
|  | 						 | ||||||
| 		// Date (yyyy-mm-dd), Num, Description, Deposit, Account		
 | 		// Date (yyyy-mm-dd), Num, Description, Deposit, Account		
 | ||||||
| 		 | 		 | ||||||
| 		// checking (check or cash) || credit
 | 		// checking (check or cash) || credit
 | ||||||
| 		// transaction has been 1) paid and is 2) cash & check [checking] or credit and 3) deposited
 | 		// transaction has been 1) paid and is 2) cash & check [checking] or credit and 3) deposited
 | ||||||
| 		if( $account_type === 'checking' ) { | 		if( $account_type === 'checking' ) { | ||||||
|  | 			$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='cash' OR payment_type='check') " .  | ||||||
|  | 						"AND (transaction_id>" . $transaction_range[0] . " AND transaction_id<" . $transaction_range[1]  . ");";		 | ||||||
|  | 			$sql = mysql_query($query, $YBDB) or die(mysql_error());	 | ||||||
|  | 			$gnucash_csv_file = ""; | ||||||
|  | 			while ( $result = mysql_fetch_assoc($sql) ) { | ||||||
|  | 				$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'] .  | ||||||
|  | 											', (Income:' . $result['transaction_type'] . ') '  . $description . ', ' . $result['amount'] . ', ' .  | ||||||
|  | 											$accounts_gnucash['checking'] . "\n"; | ||||||
|  | 			}	 | ||||||
| 			 | 			 | ||||||
|  | 			$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); | ||||||
| 			 | 			 | ||||||
|  | 			// download file to browser
 | ||||||
|  | 			/* | ||||||
|  | 	    	header('Content-Description: File Transfer'); | ||||||
|  | 	    	header('Content-Type: application/octet-stream'); | ||||||
|  | 	    	header('Content-Disposition: attachment; filename='.basename($file)); | ||||||
|  | 	    	header('Expires: 0'); | ||||||
|  | 	    	header('Cache-Control: must-revalidate'); | ||||||
|  | 	    	header('Pragma: public'); | ||||||
|  | 	    	header('Content-Length: ' . filesize($file)); | ||||||
|  | 	    	readfile($file);			 | ||||||
|  | 			*/ | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		if ( $account_type === 'credit' ) { | 		if ( $account_type === 'credit' ) { | ||||||
| 		 | 			$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 (transaction_id>" . $transaction_range[0] . " AND transaction_id<" . $transaction_range[1]  . ");";		 | ||||||
|  | 			$sql = mysql_query($query, $YBDB) or die(mysql_error());	 | ||||||
|  | 			$result = mysql_fetch_assoc($sql); | ||||||
|  | 			$gnucash_csv_file = "";	 | ||||||
|  | 			while ( $result = mysql_fetch_assoc($sql) ) { | ||||||
|  | 				$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'] .  | ||||||
|  | 											', (Income:' . $result['transaction_type'] . ') '  . $description . ', ' . $result['amount'] . ', ' .  | ||||||
|  | 											$accounts_gnucash['credit'] . "\n";							 | ||||||
|  | 			} | ||||||
|  | 				 | ||||||
|  | 			$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); | ||||||
| 		 | 		 | ||||||
| 		}		 | 		}		 | ||||||
| 		 | 		 | ||||||
| 		/* | 	} // Create csv file(s) for GnuCash
 | ||||||
| 		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="cash" OR payment_type="check")  |  | ||||||
| 		AND (transaction_id>256 AND transaction_id<259); |  | ||||||
| 		*/ |  | ||||||
| 	 |  | ||||||
| 	 |  | ||||||
| 		/* |  | ||||||
| 		require_once('../php-console/src/PhpConsole/__autoload.php'); |  | ||||||
| 		$handler = PhpConsole\Handler::getInstance(); |  | ||||||
| 		$handler->start(); |  | ||||||
| 		$handler->debug($transaction_range[0]); |  | ||||||
| 		*/ |  | ||||||
| 		 |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	// Deposit Calculator
 | 	// Deposit Calculator
 | ||||||
| 	if (isset($_POST['deposit'])) { | 	if (isset($_POST['deposit'])) { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user