<?php

$do = isset($_GET['do']) ? $_GET['do'] : "";

if ($do != "excel") {
include("../public/impOpreation.php");
}

include("../library/uploadImages.php");


$today = date("Y-m-d H:i:s");
$date = date("Y-m-d");

if (empty($do)) {
  $smarty->display("header.html");
  $smarty->display("claimview/add.html");
  $smarty->assign("claim", 1);
  $smarty->display("footer.html");
} elseif ($do == "show") {
  include_once("../public/authentication.php");
  $show = filter_input(INPUT_GET, 'show');
  if ($show == 2) {
    $smarty->assign('show', 2);
  }

  $smarty->assign('date', $date);
  $smarty->display("header.html");
  $smarty->display("claimview/show.html");
  $smarty->assign("claim", 1);
  $smarty->display("footer.html");

} elseif ($do == "edit") {
  include_once("../public/authentication.php");

  $id = filter_input(INPUT_GET, 'id');
  $claim = R::load('claims', $id);

  $typeclients = R::getAll('SELECT  *  FROM `typeclient` WHERE typeclient.typeId in (0,' .  $claim->typeclientids  . ' )');
  $smarty->assign('typeclients', $typeclients);

  $sellbills = R::getAll('SELECT claimsclientbills.*,clientname,typeName  FROM `claimsclientbills`
    join client on client.clientid = claimsclientbills.clientid
    join typeclient on typeclient.typeId = claimsclientbills.typeclientid
    WHERE claimid = ? and claimsclientbills.del < 2 ',[$claim->id]);
  $smarty->assign('sellbills', $sellbills);

  $smarty->assign('claim', $claim);

  $smarty->display("header.html");
  $smarty->display("claimview/edit.html");
  $smarty->assign("claim", 1);
  $smarty->display("footer.html");
} elseif ($do == "detail") {

  $id = filter_input(INPUT_GET, 'id');
  $claim = R::load('claims', $id);

  $typeclients = R::getAll('SELECT  *  FROM `typeclient` WHERE typeclient.typeId in (0,' .  $claim->typeclientids  . ' )');
  $smarty->assign('typeclients', $typeclients);

  $sellbills = R::getAll('SELECT claimsclientbills.*,clientname,typeName  FROM `claimsclientbills`
    join client on client.clientid = claimsclientbills.clientid
    join typeclient on typeclient.typeId = claimsclientbills.typeclientid
    WHERE claimid = ? and claimsclientbills.del < 2 ',[$claim->id]);
  $smarty->assign('sellbills', $sellbills);

  $smarty->assign('claim', $claim);

  $smarty->display("header.html");
  $smarty->display("claimview/detail.html");
  $smarty->assign("claim", 1);
  $smarty->display("footer.html");  


} elseif ($do == "excel") {

  include '../library/Classes/PHPExcel/IOFactory.php';
  include_once("../public/config.php");

  $id = filter_input(INPUT_GET, 'id');
  $claim = R::load('claims', $id);

  // $typeclients = R::getAll('SELECT  *  FROM `typeclient` WHERE typeclient.typeId in (0,' .  $claim->typeclientids  . ' )');

  $send_data = R::getAll('SELECT claimsclientbills.*,clientname,typeName  FROM `claimsclientbills`
    join client on client.clientid = claimsclientbills.clientid
    join typeclient on typeclient.typeId = claimsclientbills.typeclientid
    WHERE claimid = ? and claimsclientbills.del < 2 ',[$claim->id]);



    $objPHPExcel = new PHPExcel();
    $objPHPExcel->setActiveSheetIndex(0);
    $objPHPExcel->getActiveSheet()->setTitle("المطالبه  رقم  '.$id.'.xls");
    PHPExcel_Shared_Font::setAutoSizeMethod(PHPExcel_Shared_Font::AUTOSIZE_METHOD_EXACT);
    $objPHPExcel->getActiveSheet()->setCellValue('A1', "  ser(pk) ")
        ->setCellValue('B1', "   العميل ")
        ->setCellValue('C1', "جهة التعاقد ")
        ->setCellValue('D1', "  المتبقى");
        $RowStartProduct = 2;
  

    if(!empty($send_data)) {

        $i = 1; //row number
        foreach ($send_data as $item) {


            $col = 0;
            $objPHPExcel->getActiveSheet()->getCell('A' . $RowStartProduct)->setValueExplicit($i++, PHPExcel_Cell_DataType::TYPE_STRING);
            $col++;
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $RowStartProduct,  $item['clientname']);
            $col++;
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $RowStartProduct,  $item['typeName']);
            $col++;
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $RowStartProduct,  $item['valueclaimed']);
            $RowStartProduct++;
            
        }
    }


    ##
    foreach (range('A', 'F') as $columnID) {
        $objPHPExcel->getActiveSheet()->getColumnDimension($columnID)->setWidth(25); //->setAutoSize(true);
    }


    //https://github.com/PHPOffice/PHPExcel/tree/develop/Documentation/markdown/Overview
    // Redirect output to a client’s web browser (Excel5)
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="المطالبه  رقم  '.$id.'.xls"');
    header('Cache-Control: max-age=0');
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save('php://output');
    //Clearing a Workbook from memory
    $objPHPExcel->disconnectWorksheets();
    unset($objPHPExcel);




  


  // header("location:?do=show");

  

} elseif ($do == "stop") {
  $id = filter_input(INPUT_GET, 'id');
  $stoptrue = filter_input(INPUT_GET, 'stoptrue');
  $tables = R::load('claims', $id);
  $tables->stoptrue = $stoptrue;
  $tables->stoptoday = $today;
  $tables->stopuserid = $_SESSION['userid'];
  R::store($tables);


  $claimstop = R::dispense('claimstop');
  $claimstop->claimid = $id;
  $claimstop->stops = $stoptrue;
  $claimstop->addtoday = $today;
  $claimstop->adduserid = $_SESSION['userid'];
  R::store($claimstop);

  header("location:?do=show");
} else if ($do == 'savedata') {
  savedata();
} else if ($do == 'showajax') {
  showajax();
} else if ($do == 'removecontroller') {
  removecontroller();
} elseif ($do == "changetypeclient") {

  $from_date = filter_input(INPUT_POST, 'from_date');
  $to_date = filter_input(INPUT_POST, 'to_date');
  $typeclientids =  filter_input(INPUT_POST, 'typeclientids');
  $editforn =  filter_input(INPUT_POST, 'editforn');
  $sellbillQuery =  '';
  $returnsellbillQuery =  '';

  if($from_date != ''){
      $sellbillQuery .=' and  sellbill.sellbilldate  >= "' . $from_date . ' 00-00-00" ';
      $returnsellbillQuery .=' and  returnsellbill.returnsellbilldate  >= "' . $from_date . ' 00-00-00" ';
  }

  if($sellbillQuery != ''){
      $sellbillQuery .=' and sellbill.sellbilldate <= "' . $to_date . ' 23-59-55" ';
      $returnsellbillQuery .=' and returnsellbill.returnsellbilldate <= "' . $to_date . ' 23-59-55" ';
  }


  if($typeclientids != ''){
      $sellbillQuery .=' and sellbill.sellbilltypeclientid IN ('. $typeclientids .') ';
      $returnsellbillQuery .=' and returnsellbill.returnsellbilltypeclientid IN ('. $typeclientids .') '; 
  }



  
  $sellbills = R::getAll('SELECT sellbillclientid as clientid , clientname, sellbilltypeclientid , typeName, sum(sellbillfinalbill)  as sellbillfinalbill ,
    GROUP_CONCAT(CONCAT(sellbill.sellbillid,"/",sellbill.sellbillfinalbill)  SEPARATOR ",") AS sellbillids  FROM `sellbill`
    join client on client.clientid = sellbill.sellbillclientid
    join typeclient on typeclient.typeId = sellbill.sellbilltypeclientid
    LEFT join claimsellbills ON claimsellbills.claimsellbillid = sellbill.sellbillid and claimsellbills.del = 0
   WHERE sellbill.conditions = 0 and claimsellbills.claimsellbillid is null ' . $sellbillQuery . ' GROUP BY sellbillclientid, sellbilltypeclientid ');
  $sellbills = customArrayManyIndexOne($sellbills, $colName = ['clientid','sellbilltypeclientid']);


  $returnsellbills = R::getAll('SELECT returnsellbillclientid as clientid, returnsellbilltypeclientid, sum(returnsellbillfinalbill) as returnsellbillfinalbill ,
    GROUP_CONCAT(CONCAT(returnsellbill.returnsellbillid,"/",returnsellbill.returnsellbillfinalbill) SEPARATOR ",") AS returnsellbillids FROM `returnsellbill`
    LEFT join claimreturnsellbills ON claimreturnsellbills.claimreturnsellbillid = returnsellbill.returnsellbillid  and claimreturnsellbills.del = 0
     WHERE returnsellbill.conditions = 0 and claimreturnsellbills.claimreturnsellbillid is null  ' . $returnsellbillQuery . ' GROUP BY returnsellbillclientid, returnsellbilltypeclientid'); 
  $allvalueclaimed = 0;
  $returnsellbills = customArrayManyIndexOne($returnsellbills, $colName = ['clientid','returnsellbilltypeclientid']);
  $typeclientids = '0';
  $editallvalueclaimed = 0;
  foreach($sellbills as $key => $sellbill){
         $sellbills[$key]['returnsellbillfinalbill'] = $returnsellbills[$key]['returnsellbillfinalbill'];
         $sellbills[$key]['returnsellbillids'] = $returnsellbills[$key]['returnsellbillids'];
         $valueclaimed = (int)$sellbill['sellbillfinalbill'] + $returnsellbills[$key]['returnsellbillfinalbill'];
         $sellbills[$key]['valueclaimed'] = $valueclaimed;
         $allvalueclaimed += $valueclaimed;
         if($editforn){
            $claimsclientbill = R::getRow('SELECT * from claimsclientbills WHERE claimid = ? and typeclientid = ?',[$editforn, $sellbill['sellbilltypeclientid']]);
              if($claimsclientbill){
                $editallvalueclaimed += $claimsclientbill['valueclaimed'];
                $allvalueclaimed += $claimsclientbill['valueclaimed'];
                $typeclientids .= ','.$claimsclientbill['typeclientid'];
              }

         }

  }

  if($editforn){
      $editdatasellbills = R::getAll('SELECT claimsclientbills.*,clientname,typeName  FROM `claimsclientbills`
    join client on client.clientid = claimsclientbills.clientid
    join typeclient on typeclient.typeId = claimsclientbills.typeclientid
    WHERE claimid = ? and claimsclientbills.del < 2 and claimsclientbills.typeclientid not in ('.$typeclientids.')',[$editforn]);
      $allvalueclaimed += R::getcell('SELECT claim_value  from claims where id = ?',[$editforn]) - $editallvalueclaimed;
  }else{
    $editdatasellbills = [];
  }
  $smarty->assign('editforn', $editforn);
  $smarty->assign('editdatasellbills', $editdatasellbills);

  $smarty->assign('sellbills', $sellbills);
  $smarty->assign('allvalueclaimed', $allvalueclaimed);
  $smarty->display("claimview/changetypeclient.html");
}elseif ($do == "removeclaimsclientbills") {

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


  $id = filter_input(INPUT_POST, 'id');
  $tables = R::load('claimsclientbills', $id);
  $tables->del = 2;
  $tables->deltoday = $today;
  $tables->deluserid = $userid;
  try {
    R::store($tables);
    $claimsclientbills = R::getAll('SELECT  * from claimsclientbills where del < 2 and claimid = ?',[$tables->claimid]);
    $allvalueclaimed = 0;
    $typeclientids = '0';
    foreach ($claimsclientbills as $claimsclientbill) {
      $allvalueclaimed += $claimsclientbill['valueclaimed'];
      $typeclientids .= ','. $claimsclientbill['typeclientid'];
    }

    $typeclientids = implode(',',array_unique(explode(',', $typeclientids)));

    R::exec("UPDATE `claims` SET `claim_value`= $allvalueclaimed, `typeclientids`= '$typeclientids', `updateuserid`= $userid, `updatetoday`= '$today' WHERE id = ? ",[$tables->claimid]);
    R::exec("UPDATE `claimstypeclients` SET `del`= 2, `deluserid`= $userid, `deltoday`= '$today' WHERE claimid = ? and typeclientid not in (".$typeclientids.") ",[$tables->claimid]);
    R::exec("UPDATE `claimsellbills` SET `del`= 2 WHERE claimsclientbillid = $id");
    R::exec("UPDATE `claimreturnsellbills` SET `del`= 2 WHERE claimsclientbillid = $id");

    echo 1;
  } catch (Exception $e) {
    echo 0;
  }
}elseif ($do == "gettypeclientids") {
  $ids = $_POST['fornvalids'];
  $datas = R::getAll('SELECT typeId as id, typeName as text FROM typeclient WHERE typeId in (0,' . $ids . ') ');
  echo json_encode($datas);
}



function savedata()
{
  $today = date("Y-m-d H:i:s");
  $date = date('Y-m-d');
  $userid = $_SESSION['userid'];
  $from_date = filter_input(INPUT_POST, 'from_date');
  $to_date = filter_input(INPUT_POST, 'to_date');
  $typeclientids = $_POST['typeclientids'];
  $explodetypeclientids = explode(',', $typeclientids);
  $sellbills = filter_input(INPUT_POST, 'sellbills');
  $claim_number = filter_input(INPUT_POST, 'claim_number');
  $claim_value = filter_input(INPUT_POST, 'allvalueclaimed');
  $claim_comment = filter_input(INPUT_POST, 'claim_comment');
  $claimid = filter_input(INPUT_POST, 'claimid');


  if (!$claimid) {
    $claims = R::dispense('claims');
    $claims->del = 0;
    $claims->addtoday = $today;
    $claims->adduserid = $userid;
    $claims->deltoday = '';
    $claims->deluserid = '';
    $claims->debttrue = 0;
    $claims->stoptrue = 1;
    // $handle = new upload($_FILES['charityimage']);
    // $charityimage = uploadImages($handle, "../views/default/images/charities", 300, 300);
  } else {
    $claims = R::load('claims', $claimid);

    $claims->del = 1;
    $claims->updatetoday = $today;
    $claims->updateuserid = $userid;
    // $handle = new upload($_FILES['charityimage']);
    // $charityimage = updateImages($handle, "oldcharityimage", "../views/default/images/charities", 300, 300);
    // unlink("../views/default/images/charities" . $charities->charityimage);
  }
  

  $claims->from_date  = $from_date;
  $claims->to_date = $to_date;
  $claims->typeclientids = $typeclientids;
  $claims->claim_number = $claim_number;
  $claims->claim_value = $claim_value;
  $claims->claim_comment = $claim_comment;

  try {
    $claimid = R::store($claims);


    for ($i = 0; $i < count($explodetypeclientids); $i++) { 
          $count = R::count('claimstypeclients', 'claimid = ? and typeclientid = ? and del < 2', [$claimid, $explodetypeclientids[$i]]);
          if ($count == 0) {
              $claimstypeclients = R::dispense('claimstypeclients');
              $claimstypeclients->del = 0;
              $claimstypeclients->deltoday = '';      
              $claimstypeclients->deluserid = '';
              $claimstypeclients->typeclientid  = $explodetypeclientids[$i];
              $claimstypeclients->claimid  = $claimid;
              R::store($claimstypeclients);
          }
      } 


      for ($i = 1; $i <= $sellbills; $i++) {
          $claimsclientbillid = filter_input(INPUT_POST, 'claimsclientbillid_' . $i);
          $clientid = filter_input(INPUT_POST, 'clientid_' . $i);
          $typeclientid = filter_input(INPUT_POST, 'typeclientid_' . $i);
          $sellbillfinalbill = filter_input(INPUT_POST, 'sellbillfinalbill_' . $i);
          $sellbillids = filter_input(INPUT_POST, 'sellbillids_' . $i);
          $returnsellbillfinalbill = filter_input(INPUT_POST, 'returnsellbillfinalbill_' . $i);
          $returnsellbillids = filter_input(INPUT_POST, 'returnsellbillids_' . $i);
          $valueclaimed = filter_input(INPUT_POST, 'valueclaimed_' . $i);

          if (!$clientid) {
              continue;
          }


          if($claimsclientbillid) {
            $claimsclientbills = R::load('claimsclientbills', $claimsclientbillid);
          }else{
            $claimsclientbills = R::dispense('claimsclientbills');
            $claimsclientbills->del = 0;
            $claimsclientbills->deltoday = '';      
            $claimsclientbills->deluserid = '';
          }


          
          $claimsclientbills->clientid  = $clientid;
          $claimsclientbills->typeclientid  = $typeclientid;
          $claimsclientbills->sellbillidsprices  = $sellbillids;
          $claimsclientbills->sellbillfinalbill  = $sellbillfinalbill;
          $claimsclientbills->returnsellbillidsprices  = $returnsellbillids;
          $claimsclientbills->returnsellbillfinalbill  = $returnsellbillfinalbill;
          $claimsclientbills->valueclaimed  = $valueclaimed;
          $claimsclientbills->clientid  = $clientid;
          $claimsclientbills->claimid  = $claimid;
          $claimsclientbillid = R::store($claimsclientbills);

          $onlysellbillids = '0';
          $onlyreturnsellbillids = '0';
          
          $sellbillids = explode(',',$sellbillids);
          for ($i2 = 0; $i2 < count($sellbillids); $i2++) {

           if (!$sellbillids[$i2]) {continue;} 
              $exsellbillids = explode('/',$sellbillids[$i2]);
              $count = R::count('claimsellbills', 'claimsclientbillid = ? and claimsellbillid = ? and del < 2', [$claimsclientbillid, $exsellbillids[0]]);
              if ($count == 0) {
                  $onlysellbillids .= ','.$exsellbillids[0];
                  $claimsellbills = R::dispense('claimsellbills');
                  $claimsellbills->del = 0;
                  $claimsellbills->deltoday = '';      
                  $claimsellbills->deluserid = '';
                  $claimsellbills->claimsellbillid  = $exsellbillids[0];
                  $claimsellbills->claimsellbillfinalbill = $exsellbillids[1];
                  $claimsellbills->claimsclientbillid  = $claimsclientbillid;
                  $claimsellbills->claimid  = $claimid;
                  R::store($claimsellbills);
              }
          } 


          $returnsellbillids = explode(',',$returnsellbillids);
          for ($i3 = 0; $i3 < count($returnsellbillids); $i3++) { 
              if (!$returnsellbillids[$i3]) {continue;}
              $exreturnsellbillids = explode('/',$returnsellbillids[$i3]);
              $count = R::count('claimreturnsellbills', 'claimsclientbillid = ? and claimreturnsellbillid = ? and del < 2', [$claimsclientbillid, $exreturnsellbillids[0]]);
              if ($count == 0) {
                  $onlyreturnsellbillids .= ','.$exreturnsellbillids[0];
                  $claimreturnsellbills = R::dispense('claimreturnsellbills');
                  $claimreturnsellbills->del = 0;
                  $claimreturnsellbills->deltoday = '';      
                  $claimreturnsellbills->deluserid = '';
                  $claimreturnsellbills->claimreturnsellbillid  = $exreturnsellbillids[0];
                  $claimreturnsellbills->claimreturnsellbillfinalbill  = $exreturnsellbillids[1];
                  $claimreturnsellbills->claimsclientbillid  = $claimsclientbillid;
                  $claimreturnsellbills->claimid  = $claimid;
                  R::store($claimreturnsellbills);
              }
          } 

          $uclaimsclientbills = R::load('claimsclientbills', $claimsclientbillid);
          $uclaimsclientbills->sellbillids  = $onlysellbillids;
          $uclaimsclientbills->returnsellbillids  = $onlyreturnsellbillids;
          R::store($uclaimsclientbills);

      }





        echo json_encode(array('status' => 1,'id' => $claimid));
  } catch (Exception $e) {
        echo json_encode(array('status' => 0,'id' => 0));
  }
}
function showajax()
{
  $columns = array('claims.id', 'claims.id', 'from_date', 'to_date', 'typeclientids', 'claim_number', 'claim_value', 'addtoday', 'adduserid', 'claims.id');
  $start_date = $_POST['start_date'];
  $end_date   = $_POST['end_date'];
  $del        = $_POST['del'];
  $data1      = $_POST['data1'];
  $data2      = $_POST['data2'];
  $data3      = $_POST['data3'];

  $searchQuery = " ";
  $joinQuery = " ";
  if ($data1 != '') {
    $searchQuery .=  " and claimstypeclients.typeclientid = " . $data1 . " ";
    $joinQuery .=  "  LEFT join claimstypeclients ON claimstypeclients.claimid = claims.id  ";
  }
  if ($del == '') {
    $searchQuery .= " and claims.del < 2 ";
  }

  if ($data3 != '') {
    $searchQuery .= " and claims.branchid = " . $data3 . " ";
  }

  if ($start_date != '') {
    $searchQuery .= 'and  DATE(claims.addtoday)  >= "' . $start_date . '"';
  }
  if ($end_date != '') {
    $searchQuery .= 'and DATE(claims.addtoday) <= "' . $end_date . '" ';
  }

  // if ($_SESSION['charityids'] && !$data1)
  //   $searchQuery .= ' and claims.id in(' . $_SESSION['charityids'] . ')';


  if (isset($_POST['search']['value']) && $_POST['search']['value'] != "") {
    $searchQuery .= 'and ( claims.id LIKE " % ' . $_POST["search"]["value"] . ' %" 
        OR claims.from_date LIKE "%' . $_POST["search"]["value"] . '%"
        OR claims.to_date LIKE "%' . $_POST["search"]["value"] . '%"
        OR claims.claim_number LIKE "%' . $_POST["search"]["value"] . ' % "
        OR claims.claim_value LIKE "%' . $_POST["search"]["value"] . '%"
        )';
  }

  if (isset($_POST["order"])) {
    $searchQuery .= 'ORDER BY ' . $columns[$_POST['order']['0']['column']] . ' ' . $_POST['order']['0']['dir'] . '  ';
  } else {
    $searchQuery .= "ORDER BY  claims.id DESC ";
  }

  $totals = R::count('claims', 'LEFT JOIN user
         ON claims.adduserid = user.userid '.  $joinQuery .' WHERE 1 ' . $searchQuery . ' ');

  if (isset($_POST['start']) && $_POST['length'] != '-1') {
    $searchQuery .= "LIMIT " . intval($_POST['start']) . ", " .
      intval($_POST['length']);
  }
 

  $rResult = R::getAll(' SELECT claims.*, employeename FROM `claims` LEFT JOIN user
         ON claims.adduserid = user.userid '.  $joinQuery .' WHERE 1 ' . $searchQuery . ' ');
  $apps = count($rResult);

  $output = array(
    "draw" => intval($_POST["draw"]),
    "recordsTotal" => intval($apps),
    "recordsFiltered" => $totals,
    "data" => array()
  );

  foreach ($rResult as $row) {

     $typeclients = R::getAll('SELECT  typeName  FROM `typeclient` WHERE typeclient.typeId in (' .  $row["typeclientids"]  . ' )');
     $alltypeName = '';
     foreach($typeclients  as $typeclient){
          $alltypeName .= ' / ' .$typeclient['typeName'];
     }


    $date = date('Y-m-d');


    $sub_array = array();
    $sub_array[] = $row["id"];
    // if ($row["charityimage"] != "." && $row["charityimage"] != "") {
    //   $sub_array[] = '<img src="../views/default/images/charities/' . $row["charityimage"] . '"  style="width: 40px;height: 40px;border: 1px solid;border-radius: 6px;">';
    // } else {
    //   $sub_array[] = ' <img src="../views/default/assets/img/blank.png"  style="width: 40px;height: 40px;border: 1px solid;border-radius: 6px;" alt="" class="card-img">';
    // }
    $sub_array[] = $row["from_date"];
    $sub_array[] = $row["to_date"];
    $sub_array[] = $alltypeName;
    $sub_array[] = $row["claim_number"];
    $sub_array[] = $row["claim_value"];
    $sub_array[] = $row["addtoday"];
    $sub_array[] = $row["employeename"];
    if($row["debttrue"] == 1){
      $sub_array[] = '<a href="claimController.php?do=excel&id=' . $row["id"] . '" type="button" class="btn btn-default btn-lm ">اكسل</a>';
      $claimdebt = R::getRow('SELECT * from claimdebts WHERE claimid = ? and del < 2',[$row['id']]);
      $sub_array[] = '<a href="claimdebtController.php?do=detail&id=' . $claimdebt["id"] . '" type="button" class="btn btn-default btn-lm ">تفاصيل التحصيل</a>';
      $sub_array[] = '';
      $sub_array[] = '<a href="claimController.php?do=detail&id=' . $row["id"] . '" type="button" class="btn btn-default btn-lm ">تفاصيل</a>';
      $sub_array[] = 'تم التحصيل ';

    }else if ($row["del"] > 1) {
      $sub_array[] = '';
      $sub_array[] = '';
      $sub_array[] = '';
      $sub_array[] = '<a href="claimController.php?do=detail&id='. $row["id"] .'" type="button" class="btn btn-default btn-lm ">تفاصيل</a>';
      $sub_array[] = 'محذوف ';
    }else if ($row["stoptrue"] == 1) {
      $sub_array[] = '<a href="claimController.php?do=excel&id=' . $row["id"] . '" type="button" class="btn btn-default btn-lm ">اكسل</a>';
      $sub_array[] = '<a href="claimdebtController.php?do=&id=' . $row["id"] . '" type="button" class="btn btn-default btn-lm ">تحصيل</a>';
      $sub_array[] = '<a href="claimController.php?do=stop&id=' . $row["id"] . '&stoptrue=0" type="button" class="btn btn-default btn-lm "> ايقاف</a>';
      $sub_array[] = '<a href="claimController.php?do=edit&id=' . $row["id"] . '" type="button" class="btn btn-default btn-lg editicon"></a>';
      $sub_array[] = '<a href="javascript:;" data-id="' . $row["id"] . '" data-controll="claimController" type="button" class="btn btn-default btn-lg deleteicon removecontroller"  ></a>';
    } else {
      $sub_array[] = '<a href="claimController.php?do=excel&id=' . $row["id"] . '" type="button" class="btn btn-default btn-lm ">اكسل</a>';
      $sub_array[] = '';
      $sub_array[] = '<a href="claimController.php?do=stop&id=' . $row["id"] . '&stoptrue=1" type="button" class="btn btn-default btn-lm ">تفعيل</a>';
      $sub_array[] = '<a href="claimController.php?do=edit&id=' . $row["id"] . '" type="button" class="btn btn-default btn-lg editicon"></a>';
      $sub_array[] = '<a href="javascript:;" data-id="' . $row["id"] . '" data-controll="claimController" type="button" class="btn btn-default btn-lg deleteicon removecontroller"  ></a>';
    } 

    $output['data'][] = $sub_array;
  }

  echo json_encode($output, JSON_UNESCAPED_SLASHES);
}


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('claims', $id);
  $tables->del = 2;
  $tables->deltoday = $today;
  $tables->deluserid = $userid;
  try {
    R::store($tables);
    R::exec("UPDATE `claimstypeclients` SET `del`= 2 , `deluserid`= $userid , `deltoday`= '$today' WHERE claimid = $id");
    R::exec("UPDATE `claimsclientbills` SET `del`= 2 , `deluserid`= $userid , `deltoday`= '$today' WHERE claimid = $id");
    R::exec("UPDATE `claimsellbills` SET `del`= 2 WHERE claimid = $id");
    R::exec("UPDATE `claimreturnsellbills` SET `del`= 2 WHERE claimid = $id");

    echo 1;
  } catch (Exception $e) {
    echo 0;
  }
}



