$error = '';
$success = '';
if (is_null($template)) {
    $error = 'Cannot find the selected template.';
    $template = new Model_Message_Template(array('id' => 0));
} elseif (isset($_POST['submit'])) {
    $template->template = trim($_POST['template']);
    if (trim($template->template) == '') {
        $error = 'Please provide some text for the contect of this template.';
    } elseif ($template->save()) {
        $success = 'Successfully updated your template.';
    } else {
        $error = 'There was an error in updating this template.';
    }
}
Tracetec_Debt_Manager::setupMessageTemplates();
?>
<!DOCTYPE html>
<html lang="en">
  <head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <META HTTP-EQUIV="refresh" CONTENT="60">

    <title>Tracetec Debt Recovery - Reports</title>

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
require_once dirname(__FILE__) . '/_autoload.php';
$user_id = isset($_SESSION['adminId']) && intval($_SESSION['adminId']) > 0 ? intval($_SESSION['adminId']) : 0;
if ($user_id < 1) {
    header('Location: login.php');
    exit;
}
$subMenus = Tracetec_Debt_Manager::getSubMenus();
include "include/head.php";
$year = isset($_REQUEST['year']) ? $_REQUEST['year'] : date('Y');
$month = isset($_REQUEST['month']) ? $_REQUEST['month'] : date('m');
$date_picker = new Date_Picker($year, $month);
if (!$date_picker->date_valid()) {
    header('Location: admin-debt-recovery-dashboard.php?year=' . date('Y') . '&month=' . date('m'));
    exit;
}
Tracetec_Debt_Manager::init($user_id, $year, $month);
$debt_manager = new Tracetec_Debt_Manager($user_id, $date_picker->get_date());
?>
<!DOCTYPE html>
<html lang="en">
  <head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <META HTTP-EQUIV="refresh" CONTENT="60">

    <title>Tracetec Debt Recovery - Reports</title>

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
ob_start();
require_once dirname(__FILE__) . '/_autoload.php';
$user_id = isset($_SESSION['adminId']) && intval($_SESSION['adminId']) > 0 ? intval($_SESSION['adminId']) : 0;
if ($user_id < 1) {
    header('Location: login.php');
    exit;
}
// http://dev.tracetec.co.za/debt-recovery-runs.php?debt_recovery_id=2
$recovery_id = isset($_REQUEST['debt_recovery_id']) ? intval($_REQUEST['debt_recovery_id']) : 0;
$debt_recovery = Model_Debt_Recovery::find_by_id($recovery_id);
if (is_null($debt_recovery)) {
    header('Location: debt-recovery.php');
    exit;
}
$date_picker = new Date_Picker($debt_recovery->year, $debt_recovery->month);
$subMenus = Tracetec_Debt_Manager::getSubMenus();
include "include/head.php";
$option = isset($_REQUEST['option']) ? trim(strtolower($_REQUEST['option'])) : '';
switch ($option) {
    case 'send-sms-to-all':
        // take as much time as you want :)
        set_time_limit(0);
        require_once dirname(__FILE__) . '/debt-recovery/lib/higate.php';
        $errmsg = '';
        $debug = true;
        $return = array();
        //     ( [status_code] => 0 [data] => msg_generic_rsp [msg_no] => 5 [seq_no] => 3075729397 ) fokArray ( [status_code] => 0 [data] => msg_generic_rsp [msg_no] => 5 [seq_no] => 3075729397 [errmsg] => )
        if ($debt_recovery->sms_sent == 0) {
            $profiles = Model_Debt_Recovery_Run::all(array('conditions' => array('debt_recovery_id = ?', $debt_recovery->id)));
            $sms_sent = 0;
            foreach ($profiles as $client) {
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

  </head>
  <body>

    <div class="container">

      <b>Tracetec Debt Recovery - Message Templates</b>

      <p><br>Below are the templates used when sending out notifications to clients.</p>

      <p>&nbsp;</p>

      <?php 
$templates = Tracetec_Debt_Manager::getMessageTemplates();
if (count($templates) > 0) {
    ?>
      <table width="95%" border="1">
          <thead>
              <tr>
                  <th>Type</th>
                  <th>Name</th>
                  <th>Template</th>
                  <!--<th>Supported Keys</th>-->
                  <th>Actions</th>
              </tr>
          </thead>
            <?php 
    foreach ($templates as $template) {
        ?>
}
require_once dirname(__FILE__) . '/_autoload.php';
$user_id = isset($_SESSION['adminId']) && intval($_SESSION['adminId']) > 0 ? intval($_SESSION['adminId']) : 0;
if ($user_id < 1) {
    header('Location: login.php');
    exit;
}
// http://dev.tracetec.co.za/debt-recovery-import-debtors-list.php?debt_recovery_id=5
$recovery_id = isset($_REQUEST['debt_recovery_id']) ? intval($_REQUEST['debt_recovery_id']) : -1;
$debt_recovery = Model_Debt_Recovery::find_by_id($recovery_id);
if (is_null($debt_recovery)) {
    header('Location: admin-debt-recovery-dashboard.php');
    exit;
}
$date_picker = new Date_Picker($debt_recovery->year, $debt_recovery->month);
$debt_manager = new Tracetec_Debt_Manager($user_id, $date_picker->get_date(), $debt_recovery->id);
$allowed_extensions = array('csv');
$success = '';
$error = '';
if (isset($_POST['do-import'])) {
    // ignore user abort
    ignore_user_abort(false);
    // do not timeout
    set_time_limit(0);
    // get file extension
    $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
    if (in_array(strtolower($ext), $allowed_extensions)) {
        $target_dir = "debt-recovery/files/";
        $target_file = $target_dir . 'import-list-' . date('Y-m-d-h-i-s') . "." . $ext;
        if (move_uploaded_file($_FILES['file']['tmp_name'], $target_file)) {
            // start import