<?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("realestateunitsreportsview/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 = '';
    $esearchQuery = '';
    $rsearchQuery = ''; 
    if($start_date != '' && $end_date != ''){
        $searchQuery .=' and  realestateunitspaids.addtoday  >= "' . $start_date . ' 00-00-00" and realestateunitspaids.addtoday <= "' . $end_date . ' 23-59-55" ';
       $esearchQuery .=' and  expenses.expensesdate  >= "' . $start_date . '" and expenses.expensesdate <= "' . $end_date . '" ';
       $rsearchQuery .=' and  restaurantrawdestruction.sysdate  >= "' . $start_date . '" and restaurantrawdestruction.sysdate <= "' . $end_date . ' 23-59-55" ';
    }else{
         $searchQuery .=' and  realestateunitspaids.addtoday  >= "' . $date . ' 00-00-00" and realestateunitspaids.addtoday <= "' . $date . ' 23-59-55" ';
         $esearchQuery .=' and  expenses.expensesdate >= "' . $date . '" and expenses.expensesdate <= "' . $date . '" ';
         $rsearchQuery .=' and  restaurantrawdestruction.sysdate  >= "' . $date . '" and restaurantrawdestruction.sysdate <= "' . $date . ' 23-59-55" ';
    }

    if($realestateid != ''){
       $searchQuery .=' and  realestateunitspaids.realestateid  = ' . $realestateid . ' ';
       $esearchQuery .=' and  expenses.realestateid  = ' . $realestateid . ' ';
       $rsearchQuery .=' and  restaurantrawdestruction.realestateid  = ' . $realestateid . ' ';
    }
    
    if($realestateunitid != ''){
       $searchQuery .=' and  realestateunitspaids.realestateunitid  = ' . $realestateunitid . ' ';
       $esearchQuery .=' and  expenses.realestateunitid  = ' . $realestateunitid . ' ';
       $rsearchQuery .=' and  restaurantrawdestruction.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 > 0  $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 > 0',[$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 > 0 $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 > 0',[$crealestateunitspaid->id]);
        $crealestateunitspaid->realestate = $realestate;
        $crealestateunitspaid->realestatesunit = $realestatesunit;
        $crealestateunitspaid->client = $client;
        $crealestateunitspaid->realestateunitpaidinstallments = $realestateunitpaidinstallments;

    }


    $expenses = R::getAll("SELECT * FROM expenses 
        left join realestates on expenses.realestateid = realestates.id
        left join realestatesunits on expenses.realestateunitid = realestatesunits.id
        where expenses.conditions = 0 $esearchQuery ");

     $programsettings = R::getRow('select * from programsettings WHERE programsettingsid = 1 ');
     switch ($programsettings['Inventoryevaluation']) {
          case "first":
              $buyprice = 'restaurantrawdestruction.productBuyPrice';
              break;
          case "last":
              $buyprice = 'restaurantrawdestruction.lastbuyprice';
              break;
          case "mean":
          
          case "generalPrice":
              $buyprice = 'restaurantrawdestruction.meanbuyprice';
              break;
          case "last_discount":
              $buyprice = 'restaurantrawdestruction.lastbuyprice_withDiscount';
              break;
          case "mean_discount":
              $buyprice = 'restaurantrawdestruction.meanbuyprice_withDiscount';
              break;
      }

    $restaurantrawdestruction = R::getAll("SELECT *,  $buyprice as buypricenew,  restaurantrawdestruction.quantity as quantitynew FROM restaurantrawdestruction 
        left join product on restaurantrawdestruction.productid = product.productId
        left join realestates on restaurantrawdestruction.realestateid = realestates.id
        left join realestatesunits on restaurantrawdestruction.realestateunitid = realestatesunits.id
        where restaurantrawdestruction.conditions = 0 $rsearchQuery ");
    $smarty->assign('restaurantrawdestruction', $restaurantrawdestruction);

    $months = (int)abs((strtotime($end_date) - strtotime($start_date))/(60*60*24*30));
    $smarty->assign('months', $months);
    $smarty->assign('expenses', $expenses);
    $smarty->assign('srealestateunitspaids', $srealestateunitspaids);
    $smarty->assign('crealestateunitspaids', $crealestateunitspaids);
    $smarty->display("realestateunitsreportsview/getdata.html");  
}
?>

