<?php
include("../public/impOpreation.php");
$do = $_GET['do'];
$today = date("Y-m-d H:i:s");
$date = date("Y-m-d");
if (empty($do)) {  
    $smarty->display("header.html");
    $smarty->display("realestatepaymentsview/show.html");
    $smarty->display("footer.html");
} else if ($do == "getdata") { 
    $start_date = filter_input(INPUT_POST, 'start_date');  
    $end_date = filter_input(INPUT_POST, 'end_date');  
    $supplierid = filter_input(INPUT_POST, 'supplierid');  
    $clientid = filter_input(INPUT_POST, 'clientid'); 
    $realestateid = filter_input(INPUT_POST, 'realestateid');  
    $realestateunitid = filter_input(INPUT_POST, 'realestateunitid');
    $searchQuery = '';
    $ssearchQuery = '';
    $csearchQuery = '';
    if($start_date != '' && $end_date != ''){
       $searchQuery .=' and  realestateunitspaids.addtoday  >= "' . $start_date . ' 00-00-00" and realestateunitspaids.addtoday <= "' . $end_date . ' 23-59-55" ';
    }else{
         // $searchQuery .=' and  realestateunitspaids.addtoday  >= "' . $date . ' 00-00-00" and realestateunitspaids.addtoday <= "' . $date . ' 23-59-55" ';
    }

    if($realestateid != ''){
       $searchQuery .=' and  realestateunitspaids.realestateid  = ' . $realestateid . ' ';
    }
    
    if($realestateunitid != ''){
       $searchQuery .=' and  realestateunitspaids.realestateunitid  = ' . $realestateunitid . ' ';
    }


    if($supplierid != ''){
       $ssearchQuery .=' and  realestateunitspaids.supplierid  = ' . $supplierid . ' ';
    }

    if($clientid != ''){
       $csearchQuery .=' and  realestateunitspaids.clientid  = ' . $clientid . ' ';
    }
 
    $srealestateunitspaids = R::findAll('realestateunitspaids', " where realestateunitspaids.del < 2  and realestateunitspaids.clientid = 0 and realestateunitspaids.payed < 2  $searchQuery  $ssearchQuery ");
    foreach($srealestateunitspaids as $srealestateunitspaid){
        $realestate = R::load('realestates', $srealestateunitspaid->realestateid);
        $supplier = R::getRow('select * from supplier WHERE supplierid = ? ', [$srealestateunitspaid->supplierid]);
        $realestateunitpaidinstallments = R::findAll('realestateunitpaidinstallments','realestateunitpaidid = ? and del < 2 and payed < 2',[$srealestateunitspaid->id]);
        $srealestateunitspaid->realestate = $realestate;
        $srealestateunitspaid->supplier = $supplier;
        $srealestateunitspaid->realestateunitpaidinstallments = $realestateunitpaidinstallments;

    }

    $crealestateunitspaids = R::findAll('realestateunitspaids', " where realestateunitspaids.del < 2  and realestateunitspaids.supplierid = 0 and realestateunitspaids.payed < 2 $searchQuery  $csearchQuery ");
    foreach($crealestateunitspaids as $crealestateunitspaid){
        $realestate = R::load('realestates', $crealestateunitspaid->realestateid);
        $realestatesunit = R::load('realestatesunits', $crealestateunitspaid->realestateunitid);
        $client = R::getRow('select * from client WHERE clientid = ? ', [$crealestateunitspaid->clientid]);
        $realestateunitpaidinstallments = R::findAll('realestateunitpaidinstallments','realestateunitpaidid = ? and del < 2 and payed < 2',[$crealestateunitspaid->id]);
        $crealestateunitspaid->realestate = $realestate;
        $crealestateunitspaid->realestatesunit = $realestatesunit;
        $crealestateunitspaid->client = $client;
        $crealestateunitspaid->realestateunitpaidinstallments = $realestateunitpaidinstallments;

    }
    $smarty->assign('srealestateunitspaids', $srealestateunitspaids);
    $smarty->assign('crealestateunitspaids', $crealestateunitspaids);
    $smarty->display("realestatepaymentsview/getdata.html");  
}
?>