<?php
session_start();
require_once('config/config.php');
require_once('class/functions.php');
dbconnect();

if (!is_user()) {
    redirect(LOGIN);
}

$page = "Transfer Completion";
$geo = ip2location($_SERVER['REMOTE_ADDR']);
$country = $geo->country;
$city = $geo->city;
visit_log($_SERVER['REMOTE_ADDR'], $country, $city, $_SERVER['HTTP_USER_AGENT'], getBrowser($_SERVER['HTTP_USER_AGENT']), getOS($_SERVER['HTTP_USER_AGENT']), $page, $_SESSION['user_login']);


$sql = "SELECT * FROM `".TRANF_TABLE_NAME."` WHERE user_number='{$_SESSION['user_number']}' AND trans_number='{$_SESSION['trans_code']}' ORDER BY tranf_id DESC LIMIT 1";
$stmt = $pdo->query($sql);
if ($row = $stmt->fetch(PDO::FETCH_ASSOC)){

$benacc = $row['rcv_acc'];
$swift = $row['rcv_code'];
$memo = $row['purpose'];
$benname = $row['rcv_name'];
$benbnk = $row['rcv_bk'];
$bencntry = $row['rcv_country'];
$amount = $row['atmpt_amt'];
$accno = $row['acc_no'];
$date = $row['atmpt_date'];
$details = 'Outbound transfer from '.$accno.' to '.$benname.' ('.$benacc.' in '.$benbnk.'- '.$bencntry.')';
$transsms = trans_sms();
$transemail = trans_email();

if (!sufficient_balance($accno, $amount)){ store_msg("Insufficient Balance to complete transfer", "danger"); redirect(OTHERTRANSFER);}

if (SUCCESS_FULL) {
// Send EMAIL TO USER
$message = "
<html>
<head>
<title>".WEB_NAME."</title>
</head>
<body>
<img src=".LOGO_URL." alt=".WEB_NAME."/><br><br>
<p>".strtoupper($transemail).":<br><br>
DATE: ".date('l jS F Y', strtotime($date))." <br>
SOURCE ACCOUNT: ".$accno." <br>
BENEFICIARY ACCOUNT NUMBER: ".$benacc." <br>
BENEFICIARY ACCOUNT NAME: ".$benname." <br>
BENEFICIARY BANK: ".$benbnk." <br>
BENEFICIARY BANK COUNTRY: ".$bencntry." <br>
".strtoupper(SWFT_CODE).": ".$swift." <br>
TRANSFER MEMO(PURPOSE): ".$memo." <br>
AMOUNT ".get_currency($accno)."".number_format($amount,2)."
</p><br><br><br>
===========
<p>".WEB_NAME."</p>
</body>
</html>
";
$email = get_email($_SESSION['user_login']);
$subject = "TRANSFER ALERT";
//send_email($fromname, $fromemail, $toemail, $subject, $message)
send_email(WEB_NAME, WEB_EMAIL, $email, $subject, $message);



// Send EMAIL TO ADMIN
$message = "
<html>
<head>
<title>".WEB_NAME."</title>
</head>
<body>
<img src=".LOGO_URL." alt=".WEB_NAME."/><br><br>
<p>".strtoupper($transemail).":<br><br>
DATE: ".date('l jS F Y', strtotime($date))." <br>
SOURCE ACCOUNT: ".$accno." <br>
BENEFICIARY ACCOUNT NUMBER: ".$benacc." <br>
BENEFICIARY ACCOUNT NAME: ".$benname." <br>
BENEFICIARY BANK: ".$benbnk." <br>
BENEFICIARY BANK COUNTRY: ".$bencntry." <br>
".strtoupper(SWFT_CODE).": ".$swift." <br>
TRANSFER MEMO(PURPOSE): ".$memo." <br>
AMOUNT ".get_currency($accno)."".number_format($amount,2)."
</p><br><br><br>
===========
<p>".WEB_NAME."</p>
</body>
</html>
";
$subject = "TRANSFER ALERT";
//send_email($fromname, $fromemail, $toemail, $subject, $message)
send_email(WEB_NAME, WEB_EMAIL, WEB_EMAIL, $subject, $message);



if (USE_SMS){
//send SMS to User
$phone = clean_phone(user_phone($_SESSION['user_login']));
$sms_cnt = ''.strtoupper($transsms).' FROM '.$accno.' TO '.$benacc.' '.$benname.', AMOUNT '.get_currency($accno).''.number_format($amount,2).'';
sms_scryba($phone, $sms_cnt);
}

//trans_debit($transnumber, $usernumber, $account, $usernames, $currency, $details, $status, $amount)
trans_debit(trans_number(), $_SESSION['user_number'], $accno, user_names($_SESSION['user_login']), get_currency($accno), $details, '1', $amount);
log_info($_SERVER['REMOTE_ADDR'], $_SESSION['user_login'], $accno, 'Transfer Succefully Completed');
//complete_transfer($transnumber, $account, $status, $result)
complete_transfer($_SESSION['trans_code'], $accno, '1', '1');

}else{

// Send EMAIL TO USER
$message = "
<html>
<head>
<title>".WEB_NAME."</title>
</head>
<body>
<img src=".LOGO_URL." alt=".WEB_NAME."/><br><br>
<p>FUNDS TRANSFER ERROR:<br><br>
DATE: ".date('l jS F Y', strtotime($date))." <br>
SOURCE ACCOUNT: ".$accno." <br>
BENEFICIARY ACCOUNT NUMBER: ".$benacc." <br>
BENEFICIARY ACCOUNT NAME: ".$benname." <br>
BENEFICIARY BANK: ".$benbnk." <br>
BENEFICIARY BANK COUNTRY: ".$bencntry." <br>
".strtoupper(SWFT_CODE).": ".$swift." <br>
TRANSFER MEMO(PURPOSE): ".$memo." <br>
AMOUNT ".get_currency($accno)."".number_format($amount,2)."
</p><br><br><br>
===========
<p>".WEB_NAME."</p>
</body>
</html>
";
$email = get_email($_SESSION['user_login']);
$subject = "TRANSFER ALERT";
//send_email($fromname, $fromemail, $toemail, $subject, $message)
send_email(WEB_NAME, WEB_EMAIL, $email, $subject, $message);



// Send EMAIL TO ADMIN
$message = "
<html>
<head>
<title>".WEB_NAME."</title>
</head>
<body>
<img src=".LOGO_URL." alt=".WEB_NAME."/><br><br>
<p>FUNDS TRANSFER ERROR:<br><br>
DATE: ".date('l jS F Y', strtotime($date))." <br>
SOURCE ACCOUNT: ".$accno." <br>
BENEFICIARY ACCOUNT NUMBER: ".$benacc." <br>
BENEFICIARY ACCOUNT NAME: ".$benname." <br>
BENEFICIARY BANK: ".$benbnk." <br>
BENEFICIARY BANK COUNTRY: ".$bencntry." <br>
".strtoupper(SWFT_CODE).": ".$swift." <br>
TRANSFER MEMO(PURPOSE): ".$memo." <br>
AMOUNT ".get_currency($accno)."".number_format($amount,2)."
</p><br><br><br>
===========
<p>".WEB_NAME."</p>
</body>
</html>
";
$subject = "TRANSFER ALERT";
//send_email($fromname, $fromemail, $toemail, $subject, $message)
send_email(WEB_NAME, WEB_EMAIL, WEB_EMAIL, $subject, $message);

if (USE_SMS){
//send SMS User
$phone = clean_phone(user_phone($_SESSION['user_login']));
$sms_cnt = 'TRANSFER ERROR FROM '.$accno.' TO '.$benacc.' '.$benname.', AMOUNT '.get_currency($accno).''.number_format($amount,2).'';
sms_scryba($phone, $sms_cnt);
}

log_info($_SERVER['REMOTE_ADDR'], $_SESSION['user_login'], $accno, 'Transfer Succefully Completed');
//complete_transfer($transnumber, $account, $status, $result)
complete_transfer($_SESSION['trans_code'], $accno, '1', '0');
}

}else{
    store_msg("Your Transfer cannot be completed at the moment, please try again", "danger"); redirect(OTHERTRANSFER);
}

require_once('head.php');

?>

<body>
  <div class="app" id="app">

<!-- ############ LAYOUT START-->
  <!-- aside -->
  <?php require_once('aside.php'); ?>
  <!-- / aside -->



  <!-- content -->
  <div id="content" class="app-content box-shadow-z3" role="main">

    <?php require_once('header.php'); ?>

    <?php require_once('footer.php'); ?>

    <div ui-view class="app-body" id="view">
    <!-- ############ PAGE START-->

    <?php require_once('topbar.php'); ?>


    <?php
    $notify = get_alerts($_SESSION['user_login'], 'all');
    foreach($notify as $ntfy){
    echo "<div class=\"alert alert-warning\" role=\"alert\">
    <strong>".date('l jS F Y', strtotime($ntfy['alrt_date']))."</strong><br>".$ntfy['alrt_cnt']."
    </div>";
    }
    ?>

    <?php if (!empty($_SESSION['msg_text'])) {
    $msg = create_alert($_SESSION['msg_text'], $_SESSION['msg_type']);
    echo $msg;
    clear_msg(); }
    ?>


    <div class="padding">
      <div class="p-x-lg">
        <div class="row">
        <div class="col-lg-8 offset-lg-2">
          <div class="box">
            <div class="box-header">
              <h2><?php if(SUCCESS_FULL){ echo'<span class="text-success">Transfer Successfuly Completed</span>';}else{ echo'<span class="text-danger">Transfer encountered an error</span>';} ?></h2>
              <small>Transfer Details.</small>
            </div>
            <div class="box-divider m-a-0"></div>
            <div class="box-body p-v-md">
              <div class="p-a">
                <div class="streamline b-l m-b">
                    <div class="sl-item <?php if(SUCCESS_FULL){ echo'b-success';}else{ echo'b-danger';} ?>">
                        <div class="sl-icon">
                          <i class="material-icons text-info"><?php if(SUCCESS_FULL){ echo'&#xe86c;';}else{ echo'&#xe5c9;';} ?></i>
                        </div>
                        <div class="sl-content">
                            <div class="sl-date text-muted">Transfer Status</div>
                            <p class="<?php if(SUCCESS_FULL){ echo'text-success';}else{ echo'text-danger';} ?>"><?php if(SUCCESS_FULL){ echo'Transfer Successfuly Completed';}else{ echo'Transfer encountered an error';} ?></p>
                        </div>
                    </div>
                    <div class="sl-item b-info">
                        <div class="sl-icon">
                          <i class="material-icons text-info">&#xe0b2;</i>
                        </div>
                        <div class="sl-content">
                            <div class="sl-date text-muted">SOURCE ACCOUNT NUMBER:</div>
                            <p><?php echo $accno; ?></p>
                        </div>
                    </div>
                    <div class="sl-item b-success">
                        <div class="sl-icon">
                          <i class="material-icons text-info">&#xe0b5;</i>
                        </div>
                        <div class="sl-content">
                            <div class="sl-date text-muted">BENEFICIARY ACCOUNT NUMBER:</div>
                            <p><?php echo $benacc; ?></p>
                        </div>
                    </div>
                    <div class="sl-item b-success">
                        <div class="sl-content">
                            <div class="sl-date text-muted">BENEFICIARY ACCOUNT NAME:</div>
                            <p><?php echo $benname; ?></p>
                        </div>
                    </div>
                    <div class="sl-item">
                        <div class="sl-content">
                            <div class="sl-date text-muted">BENEFICIARY BANK:</div>
                            <p><?php echo $benbnk; ?></p>
                        </div>
                    </div>
                    <div class="sl-item">
                        <div class="sl-content">
                            <div class="sl-date text-muted">BENEFICIARY BANK COUNTRY:</div>
                            <p><?php echo $bencntry; ?></p>
                        </div>
                    </div>
                    <div class="sl-item b-info">
                        <div class="sl-content">
                            <div class="sl-date text-muted"><?php echo strtoupper(SWFT_CODE); ?>:</div>
                            <p><?php echo $swift; ?></p>
                        </div>
                    </div>
                    <div class="sl-item">
                        <div class="sl-content">
                            <div class="sl-date text-muted">TRANSFER MEMO (PURPOSE):</div>
                            <p><?php echo $memo; ?></p>
                        </div>
                    </div>
                    <div class="sl-item <?php if(SUCCESS_FULL){ echo'b-success';}else{ echo'b-danger';} ?>">
                        <div class="sl-icon">
                          <i class="material-icons text-info"><?php if(SUCCESS_FULL){ echo'&#xe877;';}else{ echo'&#xe001;';} ?></i>
                        </div>
                        <div class="sl-content">
                            <div class="sl-date text-muted">AMOUNT:</div>
                            <p class="<?php if(SUCCESS_FULL){ echo'text-success';}else{ echo'text-danger';} ?>"><?php echo get_currency($accno); ?><strong><?php echo number_format($amount,2); ?></p>
                        </div>
                    </div>
                </div>
              </div>
              <div class="row row-sm">
                  <div class="col-sm-12">
                      <div class="md-form-group">
                        <a href="<?php echo OTHERTRANSFER ?>" class="btn rounded btn-outline <?php if(SUCCESS_FULL){ echo'b-success text-success';}else{ echo'b-danger text-danger';} ?> p-x-md">Finish</a>
                      </div>
                  </div>
              </div>
            </div>
          </div>
        </div>
        </div>
      </div>
    </div>


    <!-- ############ PAGE END-->
    </div>



    </div>
<!-- ############ LAYOUT END-->

  </div>
<!-- build:js scripts/app.html.js -->
<!-- jQuery -->
  <script src="libs/jquery/jquery/dist/jquery.js"></script>

<!-- Bootstrap -->
  <script src="libs/jquery/tether/dist/js/tether.min.js"></script>
  <script src="libs/jquery/bootstrap/dist/js/bootstrap.js"></script>
  <script src="scripts/jquery.maskMoney.js"></script>
  <script>
    $(function() {
        $("#tfAmt").maskMoney();
    });
  </script>
<!-- core -->
  <script src="libs/jquery/underscore/underscore-min.js"></script>
  <script src="libs/jquery/jQuery-Storage-API/jquery.storageapi.min.js"></script>
  <script src="libs/jquery/PACE/pace.min.js"></script>

  <script src="scripts/config.lazyload.js"></script>

  <script src="scripts/palette.js"></script>
  <script src="scripts/ui-load.js"></script>
  <script src="scripts/ui-jp.js"></script>
  <script src="scripts/ui-include.js"></script>
  <script src="scripts/ui-device.js"></script>
  <script src="scripts/ui-form.js"></script>
  <script src="scripts/ui-nav.js"></script>
  <script src="scripts/ui-screenfull.js"></script>
  <script src="scripts/ui-scroll-to.js"></script>
  <script src="scripts/ui-toggle-class.js"></script>

  <script src="scripts/app.js"></script>

  <!-- ajax -->
  <script src="libs/jquery/jquery-pjax/jquery.pjax.js"></script>
  <script src="scripts/ajax.js"></script>
<!-- endbuild -->
</body>
</html>
