<?php

require_once('../config/config.php');
require_once('../foo/functions.php');
dbconnect();
session_start();

if(isset($_POST['submit']))
{

if ($_POST['date'] == ""){ redirect('add.php?error=' . urlencode('Enter Transaction Date'));}
elseif ($_POST['details'] == ""){ redirect('add.php?error=' . urlencode('Enter Transaction Details'));}
elseif ($_POST['type'] == ""){ redirect('add.php?error=' . urlencode('Enter Transaction Type'));}
elseif ($_POST['amount'] == ""){ redirect('add.php?error=' . urlencode('Enter Amount'));}

else{
$date = $_POST['date'];
$details = $_POST['details'];
$type = $_POST['type'];
$amnt = $_POST['amount'];
$user = $_POST['user'];
$userid = $_POST['userid'];
$account = $_POST['account'];
$amount = str_replace( ',', '', $amnt );
$status = "1";


if ($type == 'credit'){

dbconnect();
$stmt = $pdo->prepare('INSERT INTO '.TRANS_TABLE_NAME.' (user_acc, user_name, trans_date, trans_details, trans_status, credit) VALUES (:account, :user, :date, :details, :status, :credit)');

$stmt->execute(array(':account' => $account, ':user' => $user, ':date' => $date, ':details' => $details, ':status' => $status, ':credit' => $amount));
if ($pdo->lastInsertId())
	

	if (isset($_POST['email'])) {
			
	// Send EMAIL
	$currency = get_currency($account);
	$email = get_email($account);
	$message = "
	<html>
	<head>
	<title>".WEB_NAME."</title>
	</head>
	<body>
	<img src=".LOGO_URL." alt=".WEB_NAME."/><br><br>
	<p>YOUR ACCOUNT HAS BEEN CREDITED:<br><br>
	TRANSACTION DATE: ".date('jS F Y', strtotime($date))." <br>	
	ACCOUNT NUMBER: ".$account." <br>
	AMOUNT: ".$currency."".$amount." <br>
	DETAILS: ".$details."<br>
	</p><br><br><br>
	===========
	<p>".WEB_NAME."</p>
	</body>
	</html>
	";
	
	// Send the email
	$headers = "MIME-Version: 1.0" . "\r\n";
	$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
	$headers .= "From: ".WEB_NAME." <".WEB_EMAIL.">" . "\r\n";
	$subject = "YOUR ACCOUNT HAS BEEN CREDITED";
	mail($email,$subject,$message,$headers);
		
	}
	

	if (isset($_POST['sms']) && USE_SMS) {
	$currency = get_currency($account);
	$phone = clean_phone(user_phone($user));
	$sms_cnt = "YOUR ACCOUNT ".$account." HAS BEEN CREDITED,DATE ".$date." AMOUNT ".$currency."".$amount." DETAILS ".$details."";
	
	// Send SMS
	$url="http://www.easy2text.com/index.php";

	$postfields = array(
		'app' => SMS_APP,
		'ta' => SMS_TA,
		'u' => SMS_USER,
		'p' => SMS_WORD,
		'to' => "$phone",
		'from' => SMS_SENDER,
		'msg' => "$sms_cnt"
	);

	if (!$curld = curl_init()) {
		exit;
	}

	curl_setopt($curld, CURLOPT_POST, true);
	curl_setopt($curld, CURLOPT_POSTFIELDS, $postfields);
	curl_setopt($curld, CURLOPT_URL,$url);
	curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);

	$output = curl_exec($curld);
	curl_close ($curld);
	}

log_info($_SERVER['REMOTE_ADDR'], $_SESSION['admin_login'], $_POST['user'], 'Transaction Succesfully Added');

redirect('add.php?user='.$userid.'&name='.$user.'&acc='.$account.'&msg=' . urlencode('Transaction Succesfully Added'));	

}else{

$stmt = $pdo->prepare('INSERT INTO '.TRANS_TABLE_NAME.' (user_acc, user_name, trans_date, trans_details, trans_status, debit) VALUES (:account, :user, :date, :details, :status, :debit)');

$stmt->execute(array(':account' => $account, ':user' => $user, ':date' => $date, ':details' => $details, ':status' => $status, ':debit' => $amount));
if ($pdo->lastInsertId())
	

	if (isset($_POST['email'])) {
			
	// Send EMAIL
	$currency = get_currency($account);
	$email = get_email($account);
	$message = "
	<html>
	<head>
	<title>".WEB_NAME."</title>
	</head>
	<body>
	<img src=".LOGO_URL." alt=".WEB_NAME."/><br><br>
	<p>YOUR ACCOUNT HAS BEEN DEBITED:<br><br>
	TRANSACTION DATE: ".date('jS F Y', strtotime($date))." <br>	
	ACCOUNT NUMBER: ".$account." <br>
	AMOUNT: ".$currency."".$amount." <br>
	DETAILS: ".$details."<br>
	</p><br><br><br>
	===========
	<p>".WEB_NAME."</p>
	</body>
	</html>
	";
	
	// Send the email
	$headers = "MIME-Version: 1.0" . "\r\n";
	$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
	$headers .= "From: ".WEB_NAME." <".WEB_EMAIL.">" . "\r\n";
	$subject = "YOUR ACCOUNT HAS BEEN DEBITED";
	mail($email,$subject,$message,$headers);
		
	}
	
	
	if (isset($_POST['sms']) && USE_SMS) {
	$currency = get_currency($account);
	$phone = clean_phone(user_phone($user));
	$sms_cnt = "YOUR ACCOUNT ".$account." HAS BEEN DEBITED,DATE ".$date." AMOUNT ".$currency."".$amount." DETAILS ".$details."";
	
	// Send SMS
	$url="http://www.easy2text.com/index.php";

	$postfields = array(
		'app' => SMS_APP,
		'ta' => SMS_TA,
		'u' => SMS_USER,
		'p' => SMS_WORD,
		'to' => "$phone",
		'from' => SMS_SENDER,
		'msg' => "$sms_cnt"
	);

	if (!$curld = curl_init()) {
		exit;
	}

	curl_setopt($curld, CURLOPT_POST, true);
	curl_setopt($curld, CURLOPT_POSTFIELDS, $postfields);
	curl_setopt($curld, CURLOPT_URL,$url);
	curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);

	$output = curl_exec($curld);
	curl_close ($curld);
	}

log_info($_SERVER['REMOTE_ADDR'], $_SESSION['admin_login'], $_POST['user'], 'Transaction Succesfully Added');

redirect('add.php?user='.$userid.'&name='.$user.'&acc='.$account.'&msg=' . urlencode('Transaction Succesfully Added'));	
	
}
}
}

?>