<?php
include("../public/impOpreation.php");
include("../library/uploadImages.php");

$do = $_GET['do'];
$today = date("Y-m-d H:i:s");
$date = date("Y-m-d");

if (empty($do)) {
  $smarty->display("header.html");

  if ($_SESSION['charityids'])
    $searchQuery .= ' and charities.id in(' . $_SESSION['charityids'] . ')';

  $allCharities = R::getAll('select * from charities where del < 2' . $searchQuery);
  $smarty->assign('allCharities', $allCharities);

  $smarty->display("subventionPayview/add.html");
  $smarty->assign("charity", 1);
  $smarty->display("footer.html");
} else if ($do == "show") {
  header('location:subventionPayController.php');
} else if ($do == 'savedata') {
  savedata();
} else if ($do == 'removecontroller') {
  removecontroller();
} elseif ($do == "getSubvention") {
  $charity_id = filter_input(INPUT_POST, 'id');
  $month = filter_input(INPUT_POST, 'month');
  $allBen = R::getAll('SELECT b.id as beneficiary_id,b.del as benDel,b.name, b.idnumber,subvention.* ,subventionpay.id as payId, subventionpay.month,
  (CASE WHEN COUNT(subventionpay.id) > 0 THEN 1 ELSE 0 END)  AS paid
   from beneficiaries b join subvention ON subvention.beneficier_id = b.id
   left join subventionpay ON b.id = subventionpay.benefeciary_id AND subventionpay.month = "' . $month . '"
   where b.charity_id = ' . $charity_id . ' and b.del < 2 
   GROUP BY b.id');

  if (count($allBen) > 0) {
    $smarty->assign('allBen', $allBen);
    $smarty->assign('month', $month);
    $smarty->display('subventionPayview/benef_tbl.html');
  } else {
    echo -1;
  }
} else if ($do == 'gettabledata') {
  gettabledata();
} elseif ($do == "printPayedSubvention") {
  $charity_id = filter_input(INPUT_POST, 'id');
  $charityName = R::getCell('select charityname from charities where id = ' . $charity_id);
  $month = filter_input(INPUT_POST, 'month');
  $allBen = R::getAll('SELECT b.id as beneficiary_id,b.del as benDel,b.name, b.idnumber,subvention.* ,subventionPay.id as payId, subventionPay.month
   from beneficiaries b join subvention ON subvention.beneficier_id = b.id
   left join subventionPay ON subventionPay.benefeciary_id = b.id 
   where b.charity_id = ' . $charity_id . ' and subventionPay.month = "' . $month . '" and b.del < 2');
  if (count($allBen) > 0) {
    $smarty->assign('allBen', $allBen);
    $smarty->assign('charityName', $charityName);
    $smarty->assign('month', $month);
    $smarty->display('subventionPayview/print.html');
  } else {
    echo -1;
  }
} elseif ($do == "printIDs") {
  $ids = filter_input(INPUT_POST, 'id');
  $ids = rtrim($ids,',');
  $allBen = R::getAll('SELECT * from beneficiaries where id in (' . $ids . ')');
  if (count($allBen) > 0) {
    $smarty->assign('allBen', $allBen);
    $smarty->display('subventionPayview/print_ids.html');
  } else {
    echo -1;
  }
}


function savedata()
{
  $today = date("Y-m-d H:i:s");
  $userid = $_SESSION['userid'];
  $charity_id = filter_input(INPUT_POST, 'charity_id');
  $benefeciary_id = filter_input(INPUT_POST, 'benefeciary_id');
  $financial_aid = filter_input(INPUT_POST, 'financial_aid');
  $inkind_aid = filter_input(INPUT_POST, 'inkind_aid');
  $notes = filter_input(INPUT_POST, 'notes');
  $month = filter_input(INPUT_POST, 'month');
  if ($benefeciary_id && $charity_id) {
    $subventionPay = R::dispense('subventionpay');
    $subventionPay->del = 0;
    $subventionPay->sysdate = $today;
    $subventionPay->user_id = $userid;
    $subventionPay->charity_id = $charity_id;
    $subventionPay->benefeciary_id = $benefeciary_id;
    $subventionPay->month = $month;
    $subventionPay->financial_aid = $financial_aid;
    $subventionPay->inkind_aid = $inkind_aid;
    $subventionPay->notes = $notes;
  }
  try {
    $subventionPayid = R::store($subventionPay);
    echo 1;
  } catch (Exception $e) {
    echo 0;
  }
}

function removecontroller()
{
  $today = date("Y-m-d H:i:s");
  $date = date('Y-m-d');
  $userid = $_SESSION['userid'];
  $id = filter_input(INPUT_POST, 'id');
  $tables = R::load('subventionPay', $id);
  $tables->del = 2;
  $tables->del_date = $today;
  $tables->deluserid = $userid;
  try {
    R::store($tables);
    echo 1;
  } catch (Exception $e) {
    echo 0;
  }
}

function gettabledata()
{
  $table = $_POST['table'];
  $tableid = $_POST['tableid'];
  $tableval = $_POST['tableval'];
  $join = $_POST['join'];
  $tableid = $table . "." . $tableid;
  $getdata = R::getRow("SELECT * FROM $table $join WHERE $tableid = ?", [$tableval]);
  echo json_encode($getdata);
}
