<?php

///*****************************أعمار الديون********************///
//the global file operation
include("../public/impOpreation.php");
include_once("dailyentryfun.php");

// get the config file
include_once("../public/config.php");

//get the do the action
$do = $_GET['do'];


$langs = $_SESSION['erp_lang'];

/* ======================

  Controller Name :- dailyReportController

  OPERTATION in Controller

  1-display daily reports

======================== */

//here the global templates
$smarty->display("header.html");

//check and use the condetion that suite this action
if (empty($do) || $do == "show") {
  include_once("../public/authentication.php");
  $Programsettingdata = $ProgramsettingDAO->load(1);

  $today = filter_input(INPUT_POST, 'date');
  if (!$today)
    $today = date('Y-m-d');
  $smarty->assign('today', $today);
  $yesterday = date("Y-m-d", strtotime($today . ' -1 days'));
  if (isset($Programsettingdata->reportsPlusHours) && !empty($Programsettingdata->reportsPlusHours)) {
    $reportsPlusHours = $Programsettingdata->reportsPlusHours + 24; //24 to get the end of the day and add search hours to it
    $endToday = date('Y-m-d H:i:s', strtotime('+' . $reportsPlusHours . ' hour +0 minutes', strtotime($today)));
    $startToday = date('Y-m-d H:i:s', strtotime('+' . $Programsettingdata->reportsPlusHours . ' hour +0 minutes', strtotime($today)));
    //
    $endYesterday = date('Y-m-d H:i:s', strtotime('+' . $reportsPlusHours . ' hour +0 minutes', strtotime($yesterday)));
    $startYesterday = date('Y-m-d H:i:s', strtotime('+' . $Programsettingdata->reportsPlusHours . ' hour +0 minutes', strtotime($yesterday)));
  } else {
    $endToday = $today . ' 23:59:59';
    $startToday = $today . " 00:00:00";
    //
    $endYesterday = $today . ' 23:59:59';
    $startYesterday = $today . " 00:00:00";
  }
  if (date('H') < $Programsettingdata->reportsPlusHours) {
    $startDate = $startYesterday;
    $endDate = $endYesterday;
  } else {
    $startDate = $startToday;
    $endDate = $endToday;
  }


  $sellBillPayed = R::getCell('SELECT SUM(sellbilltotalpayed) FROM sellbill WHERE conditions = 0 AND sellbilldate >= "' . $startDate . '" AND sellbilldate <= "' . $endDate . '"');
  $smarty->assign('sellBillPayed', $sellBillPayed);

  $sellBillDebt = R::getCell('SELECT SUM(sellbillfinalbill) FROM sellbill WHERE conditions = 0 AND sellbilldate >= "' . $startDate . '" AND sellbilldate <= "' . $endDate . '"');
  $smarty->assign('sellBillDebt', $sellBillDebt);

  $buybillsToday = R::getAll('SELECT suppliername, SUM(buybillaftertotalbill) AS total from buybill join supplier on buybill.buybillsupplierid = supplier.supplierid
                            Where buybill.conditions = 0 AND buybilldate >= "' . $startDate . '" AND buybilldate <= "' . $endDate . '" group by buybill.buybillsupplierid');
  $smarty->assign('buybillsToday', $buybillsToday);

  $supplierPayed = R::getAll('SELECT suppliername, SUM(supplierdebtchangeamount) total from supplierdebtchange join supplier ON supplierdebtchange.supplierid = supplier.supplierid
                  WHERE del = 0 AND supplierdebtchangedate >= "' . $startDate . '" AND supplierdebtchangedate <= "' . $endDate . '" AND tablename = "supplierPayedDeptController.php" GROUP BY supplierdebtchange.supplierid');
  $smarty->assign('supplierPayed', $supplierPayed);

  $clientPayed = R::getAll('SELECT clientname, SUM(clientdebtchangeamount) total from clientdebtchange join client ON clientdebtchange.clientid = client.clientid
                  WHERE del = 0 AND clientdebtchangedate >= "' . $startDate . '" AND clientdebtchangedate <= "' . $endDate . '" AND tablename = "clientPayedDeptController.php" GROUP BY clientdebtchange.clientid');
  $smarty->assign('clientPayed', $clientPayed);

  $clientsDebts = R::getCell('SELECT SUM(clientdebt) from client where conditions = 0');
  $smarty->assign('clientsDebts', $clientsDebts);

  $supplierDebts = R::getCell('SELECT SUM(suppliercurrentDebt) from supplier where conditions = 0');
  $smarty->assign('supplierDebts', $supplierDebts);

  $pro_price = ' ';
  switch ($Programsettingdata->Inventoryevaluation) {
    case "first":
      $pro_price .= 'productBuyPrice';
      break;
    case "last":
      $pro_price .= 'lastbuyprice';
      break;
    case "mean":
      $pro_price .= 'meanbuyprice';
      break;
    case "last_discount":
      $pro_price .= 'lastbuyprice_withDiscount';
      break;
    case "mean_discount":
      $pro_price .= 'meanbuyprice_withDiscount';
      break;
    case "tax":
      $pro_price .= 'lastbuyprice_withTax';
      break;
    case "mean_tax":
      $pro_price .= 'meanbuyprice_withTax';
      break;
    default:
      $pro_price .= 'overAllAveragePrice';
      break;
  }

  $storeEvaluation = R::getCell('SELECT ROUND(SUM(productquantity * ' . $pro_price . '),2) AS total FROM storedetail JOIN product ON product.productId = storedetail.productid 
                          WHERE product.conditions = 0');
  $smarty->assign('storeEvaluation', $storeEvaluation);

  $expenseTotal = R::getCell('SELECT SUM(expensesValue) FROM expenses WHERE expenses.conditions = 0 AND expensesdate = "' . $today . '"');
  $smarty->assign('expenseTotal', $expenseTotal);

  $expenseData = R::getAll('SELECT expensestypename, SUM(expensesValue) total FROM expenses JOIN expensestype ON expenses.expensestypeid = expensestype.expensestypeid
                            WHERE expenses.conditions = 0 AND expensesdate = "' . $today . '" group by expenses.expensestypeid');
  $smarty->assign('expenseData', $expenseData);

  $saveTotal = R::getCell('SELECT SUM(savecurrentvalue) FROM save WHERE conditions = 0');
  $smarty->assign('saveTotal', $saveTotal);


  $smarty->display("dailyReportView/show.html");
}

$smarty->display("footer.html");
