<?php



//the global file operation

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


require_once('../models/dao/SellbillDAO.class.php');
require_once('../models/dto/Sellbill.class.php');
require_once('../models/mysql/SellbillMySqlDAO.class.php');
require_once('../models/mysql/ext/SellbillMySqlExtDAO.class.php');

require_once('../models/dao/SellbilldetailDAO.class.php');
require_once('../models/dto/Sellbilldetail.class.php');
require_once('../models/mysql/SellbilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/SellbilldetailMySqlExtDAO.class.php');
//Returnsellbill
require_once('../models/dao/ReturnsellbillDAO.class.php');
require_once('../models/dto/Returnsellbill.class.php');
require_once('../models/mysql/ReturnsellbillMySqlDAO.class.php');
require_once('../models/mysql/ext/ReturnsellbillMySqlExtDAO.class.php');
//Returnsellbilldetail
require_once('../models/dao/ReturnsellbilldetailDAO.class.php');
require_once('../models/dto/Returnsellbilldetail.class.php');
require_once('../models/mysql/ReturnsellbilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/ReturnsellbilldetailMySqlExtDAO.class.php');
//Sellbillandrutern
require_once('../models/dao/SellbillandruternDAO.class.php');
require_once('../models/dto/Sellbillandrutern.class.php');
require_once('../models/mysql/SellbillandruternMySqlDAO.class.php');
require_once('../models/mysql/ext/SellbillandruternMySqlExtDAO.class.php');
//Sellandruternbilldetail
require_once('../models/dao/SellandruternbilldetailDAO.class.php');
require_once('../models/dto/Sellandruternbilldetail.class.php');
require_once('../models/mysql/SellandruternbilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/SellandruternbilldetailMySqlExtDAO.class.php');
require_once('../models/mysql/ext/SellbillandruternMySqlExtDAO.class.php');




$do = $_GET['do'];

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

  Controller Name :- storedetailCTRL تقرير مخزون اول مدة

  OPERTATION in Controller

  1-load save data
  2-display show form

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

//here the global templates

//here goes the instances and general variables


//Sellbill
$sellBill = new Sellbill();
$sellBillDAO = new SellbillMySqlDAO();
$sellBillEX = new SellbillMySqlExtDAO();
//Sellbilldetail
$sellbilldetail = new Sellbilldetail();
$sellbilldetailDAO = new SellbilldetailMySqlDAO();
$sellbilldetailEX = new SellbilldetailMySqlExtDAO();
//Returnsellbill
$returnSellBill = new Returnsellbill();
$returnSellBillDAO = new ReturnsellbillMySqlDAO();
$returnSellBillEX = new ReturnsellbillMySqlExtDAO();
//Returnsellbilldetail
$returnSellBillDetail = new Returnsellbilldetail();
$returnSellBillDetailDAO = new ReturnsellbilldetailMySqlDAO();
$returnSellBillDetailEX = new ReturnsellbilldetailMySqlExtDAO();
//Sellandruternbill
$sellAndRuternBill = new Sellbillandrutern();
$sellAndRuternBillDAO = new SellbillandruternMySqlDAO();
$sellAndRuternBillEX = new SellbillandruternMySqlExtDAO();
//Sellandruternbilldetail
$sellAndRuternBillDetail = new Sellandruternbilldetail();
$sellAndRuternBillDetailDAO = new SellandruternbilldetailMySqlDAO();
$sellAndRuternBillDetailEX = new SellandruternbilldetailMySqlExtDAO();


//check and use the condition that suite this action
if (empty($do)) {


  $columns = array('name', 'datetime', 'totalbill', 'monetary', 'totalbill', 'client', 'id');



  // $start_date = '2020-01-01';
  // $end_date =  '2020-01-23';
  // $day = 0;

  // $ORDER ='';
  // if(isset($_POST["order"]))
  // {
  //  $$ORDER .= 'ORDER BY '.$columns[$_POST['order']['0']['column']].' '.$_POST['order']['0']['dir'].' 
  //  ';
  // }else{
  //    $$ORDER .= 'ORDER BY sellbill.sellbilldate desc';
  // }

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


  $start_date = $_POST["start_date"];
  $end_date = $_POST["end_date"];
  $day = $_POST["day"];


  $sellbill = " where 1 "; //sellbill
  $returnsellbill = " where 1 "; //sellbillreturn
  $sellbillandrutern = " where 1 "; //sellbillandretu

  if ($start_date != '' &&  $end_date  != '') {
    $sellbill .= '  and  sellbill.sellbilldate  >= "' . $start_date . '00:00:00" and sellbill.sellbilldate <= "' . $end_date . ' 23:59:59"  ';
    $returnsellbill .= ' and  returnsellbill.returnsellbilldate >= "' . $start_date . ' 00:00:00" and returnsellbill.returnsellbilldate <= "' . $end_date . ' 23:59:59" ';
    $sellbillandrutern .= '  and  sellbillandrutern.sellbilldate  >= "' . $start_date . '00:00:00" and sellbillandrutern.sellbilldate <= "' . $end_date . ' 23:59:59"  ';
  } else {
    if ($start_date != '') {
      $sellbill .= 'and  sellbill.sellbilldate  >= "' . $start_date . '" ';
      $returnsellbill .= 'and  returnsellbill.returnsellbilldate  >= "' . $start_date . '"  ';
      $sellbillandrutern .= 'and  sellbillandrutern.sellbilldate  >= "' . $start_date . '"  ';
    }
    if ($end_date != '') {

      $sellbill .= 'and  sellbill.sellbilldate  <= "' . $end_date . '" ';
      $returnsellbill .= 'and  returnsellbill.returnsellbilldate  <= "' . $end_date . '" ';
      $sellbillandrutern .= 'and  sellbillandrutern.sellbilldate  <= "' . $end_date . '" ';
    }
  }

  $today = date('Y-m-d');
  ##if no search make it today
  if ($sellbill == " where 1 ") {
    $sellbill .= '  and  sellbill.sellbilldate  >= "' . $today . ' 00:00:00" and sellbill.sellbilldate <= "' . $today . ' 23:59:59" ';
  }
  if ($returnsellbill == " where 1 ") {
    $returnsellbill .= ' and  returnsellbill.returnsellbilldate >= "' . $today . ' 00:00:00" and returnsellbill.returnsellbilldate <= "' . $today . ' 23:59:59"  ';
  }
  if ($sellbillandrutern == " where 1 ") {
    $sellbillandrutern .= 'and  sellbillandrutern.sellbilldate  >= "' . $today . ' 00:00:00" and sellbillandrutern.sellbilldate <= "' . $today . '23:59:59"  ';
  }



  global $sellBillEX;
  global $returnSellBillEX;
  global $sellAndRuternBillEX;
  global $smarty;


  class billData
  {
    public $datetime;
  }

  // $sellBillData = $sellBillEX->queryAllGeneral_2($sellbill);
  $sellBillData = R::findAll('sellbill', $sellbill);
  // $sellBillDataReturn = $returnSellBillEX->queryAllGenerale($returnsellbill);
  $sellBillDataReturn = R::findAll('returnsellbill', $returnsellbill);
  // $sellBillDataSellAndReturn = $sellAndRuternBillEX->queryAllGenerales($sellbillandrutern);
  $sellBillDataSellAndReturn = R::findAll('sellbillandrutern', $sellbillandrutern);

  $allDataArr = array();

  foreach ($sellBillData as $value) {
    $myBill = new billData();
    $myBill->name = '<h3 style="color: green;" >فاتورة مبيعات    </h3>';
    $myBill->id = '<a style="color: green;"  target="_blank" href="sellbillController.php?do=showDetail&sellbillid=' . $value->sellbillid . ' ">تفاصيل</a>';
    $myBill->client = $value->sellbillclientname;

    $myBill->datetime = $value->sellbilldate;
    $myBill->monetary += $value->sellbilltotalpayed;
    $myBill->totalbill += $value->sellbillaftertotalbill;
    array_push($allDataArr, $myBill);
  }
  foreach ($sellBillDataReturn as $value) {
    $myBill = new billData();
    $myBill->name = '<h3 style="color: blue;" >فاتوره مردودات مبيعات  </h3>';
    $myBill->id = '<a style="color: blue;"  target="_blank" href="returnsellbillController.php?do=showDetail&returnsellbillid=' . $value->returnsellbillid . ' ">تفاصيل</a>';
    $myBill->client = $value->sellbillclientname;

    $myBill->datetime = $value->sellbilldate;
    $myBill->monetary += $value->sellbilltotalpayed;
    $myBill->totalbill += $value->sellbillaftertotalbill;
    array_push($allDataArr, $myBill);
  }
  foreach ($sellBillDataSellAndReturn as $value) {
    $myBill = new billData();
    $myBill->name = '<h3 style="color: red;" >فاتورة مبيعات و مردودات  </h3>';
    $myBill->id = '<a style="color: red;" target="_blank" href="sellbillandruternController.php?do=showDetail&sellbillid=' . $value->sellbillid . ' ">تفاصيل </a>';
    $myBill->client = $value->returnsellbillclientname;
    $myBill->datetime = $value->returnsellbilldate;
    $myBill->monetary -= (int)$value->returnsellbilltotalpayed;
    $myBill->totalbill -= (int)$value->returnsellbillaftertotalbill;

    array_push($allDataArr, $myBill);
  }



  $monetary = 0;
  $totalbill = 0;
  foreach ($allDataArr as $value) {

    $totalbill += $value->totalbill;
    $monetary += $value->monetary;
  }


  if ($day == 1) {
    $myArr = array();
    $existDate = array();
    foreach ($allDataArr as $value) {
      $dt = strtotime($value->datetime);
      if (in_array(date("Y-m-d", $dt), $existDate)) {
        $key = array_search(date("Y-m-d", $dt), $existDate);
        $myObj = $myArr[$key];

        $myObj->totalbill += $value->totalbill;
        $myObj->monetary += $value->monetary;
      } else {
        array_push($existDate, date("Y-m-d", $dt));

        array_push($myArr, $value);
      }
    }
    $allDataArr =  $myArr;
  };
  $all = count($allDataArr);
  $output = array(
    "draw" => intval($draw),
    "iTotalRecords" => $all,
    "iTotalDisplayRecords" => $all,
    "data" => array()
  );



  if ($day  == 0) {

    foreach ($allDataArr as $value) {
      $sub_array = array();
      $sub_array[] = $value->name;
      $sub_array[] = $value->datetime;
      $sub_array[] = $value->totalbill - $value->monetary;
      $sub_array[] = $value->monetary;
      $sub_array[] = $value->totalbill;
      $sub_array[] = $value->client;
      $sub_array[] = $value->id;
      $output['data'][] = $sub_array;
    };
    $sub_array = array();
    $sub_array[] =   '************';

    $sub_array[] =  '<h3 style="color:  tomato ;" > الاجمالى    </h3>';
    $sub_array[] =  '<h3 style="color:  violet;" > ' . ($totalbill - $monetary) . '    </h3>';
    $sub_array[] =  '<h3 style="color:  gray;" > ' . $monetary . '    </h3>';
    $sub_array[] =   '<h3 style="color: slateblue ;" >  ' . $totalbill . '   </h3>';
    $sub_array[] = '************';

    $sub_array[] = '************';

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


  if ($day  == 1) {


    foreach ($allDataArr as $value) {
      $sub_array = array();
      $sub_array[] = '************';
      $sub_array[] = $value->datetime;
      $sub_array[] = $value->totalbill - $value->monetary;
      $sub_array[] = $value->monetary;
      $sub_array[] = $value->totalbill;
      $sub_array[] = '************';
      $sub_array[] = '************';

      $output['data'][] = $sub_array;
    };
    $sub_array = array();
    $sub_array[] =   '************';
    $sub_array[] =  '<h3 style="color:  tomato ;" > الاجمالى    </h3>';
    $sub_array[] =  '<h3 style="color:  violet;" > ' . ($totalbill - $monetary) . '    </h3>';
    $sub_array[] =  '<h3 style="color:  gray;" > ' . $monetary . '    </h3>';
    $sub_array[] =   '<h3 style="color: slateblue ;" >  ' . $totalbill . '   </h3>';
    $sub_array[] =   '************';
    $sub_array[] = '************';
    $output['data'][] = $sub_array;
  };










  echo json_encode($output, JSON_UNESCAPED_SLASHES);
} elseif ($do == "add") {


  $start_date = $_POST["start_date"];
  $end_date = $_POST["end_date"];
  $day = $_POST["day"];
  $sell = $_POST["sell"];


  // $start_date = '2020-01-01';
  // $end_date =  '2020-01-23';
  // $day = 0;
  // $sell = 0;


  $sellbill = " where 1 "; //sellbill
  $returnsellbill = " where 1 "; //sellbillreturn
  $sellbillandrutern = " where 1 "; //sellbillandretu

  if ($start_date != '' &&  $end_date  != '') {

    if ($sell == 0 || $sell == 1) {
      $sellbill .= '  and  sellbill.sellbilldate  >= "' . $start_date . '00:00:00" and sellbill.sellbilldate <= "' . $end_date . ' 23:59:59"  ';
    }
    if ($sell == 0 ||  $sell == 2) {
      $returnsellbill .= ' and  returnsellbill.returnsellbilldate >= "' . $start_date . ' 00:00:00" and returnsellbill.returnsellbilldate <= "' . $end_date . ' 23:59:59" ';
    }

    if ($sell == 0 ||  $sell == 3) {
      $sellbillandrutern .= ' and  sellbillandrutern.sellbilldate >= "' . $start_date . ' 00:00:00" and sellbillandrutern.sellbilldate <= "' . $end_date . ' 23:59:59" ';
    }
  } else {
    if ($start_date != '') {
      if ($sell == 0 || $sell == 1) {
        $sellbill .= 'and  sellbill.sellbilldate  >= "' . $start_date . '" ';
      }
      if ($sell == 0 || $sell == 2) {
        $returnsellbill .= 'and  returnsellbill.returnsellbilldate  >= "' . $start_date . '"  ';
      }
      if ($sell == 0 || $sell == 3) {
        $sellbillandrutern .= 'and  sellbillandrutern.sellbilldate  >= "' . $start_date . '"  ';
      }
    }
    if ($end_date != '') {
      if ($sell == 0 || $sell == 1) {
        $sellbill .= 'and  sellbill.sellbilldate  <= "' . $end_date . '" ';
      }
      if ($sell == 0 || $sell == 2) {
        $returnsellbill .= 'and  returnsellbill.returnsellbilldate  <= "' . $end_date . '" ';
      }
      if ($sell == 0 || $sell == 3) {
        $sellbillandrutern .= 'and  sellbillandrutern.sellbilldate  <= "' . $end_date . '" ';
      }
    }
  }

  $today = date('Y-m-d');
  ##if no search make it today
  if ($sellbill == " where 1 ") {
    $sellbill .= '  and  sellbill.sellbilldate  >= "' . $today . ' 00:00:00" and sellbill.sellbilldate <= "' . $today . ' 23:59:59" ';
  }
  if ($returnsellbill == " where 1 ") {
    $returnsellbill .= ' and  returnsellbill.returnsellbilldate >= "' . $today . ' 00:00:00" and returnsellbill.returnsellbilldate <= "' . $today . ' 23:59:59"  ';
  }
  if ($sellbillandrutern == " where 1 ") {
    $sellbillandrutern .= ' and  sellbillandrutern.sellbilldate >= "' . $today . ' 00:00:00" and sellbillandrutern.sellbilldate <= "' . $today . ' 23:59:59" ';
  }



  global $sellBillEX;
  global $returnSellBillEX;
  global $sellAndRuternBillEX;
  global $smarty;


  class billData
  {
    public $datetime;
  }
  $sellBillData = $sellBillEX->queryAllGeneral_2($sellbill);

  $sellBillDataReturn = $returnSellBillEX->queryAllGenerale($returnsellbill);

  $sellBillDataSellAndReturn = $sellAndRuternBillEX->queryAllGenerales($sellbillandrutern);
  $allDataArr = array();

  foreach ($sellBillData as $value) {
    $myBill = new billData();
    $myBill->name = '<h3 style="color: green;" >فاتورة مبيعات    </h3>';
    $myBill->id = '<a style="color: green;"  target="_blank" href="sellbillController.php?do=showDetail&sellbillid=' . $value->sellbillid . ' ">تفاصيل</a>';
    $myBill->client = $value->sellbillclientname;

    $myBill->datetime = $value->sellbilldate;
    $myBill->monetary += $value->sellbilltotalpayed;
    $myBill->totalbill += $value->sellbillaftertotalbill;
    array_push($allDataArr, $myBill);
  }
  foreach ($sellBillDataSellAndReturn as $value) {
    $myBill = new billData();
    $myBill->name = '<h3 style="color: blue;" >فاتوره مردودات مبيعات  </h3>';
    $myBill->id = '<a style="color: blue;"  target="_blank" href="sellbillandruternController.php?do=showDetail&sellbillid=' . $value->sellbillid . ' ">تفاصيل</a>';
    $myBill->client = $value->sellbillclientname;

    $myBill->datetime = $value->sellbilldate;
    $myBill->monetary += $value->sellbilltotalpayed;
    $myBill->totalbill += $value->sellbillaftertotalbill;
    array_push($allDataArr, $myBill);
  }
  foreach ($sellBillDataReturn as $value) {
    $myBill = new billData();
    $myBill->name = '<h3 style="color: red;" >فاتورة مبيعات و مردودات  </h3>';
    $myBill->id = '<a style="color: red;" target="_blank" href="sellbillandruternController.php?do=showDetail&sellbillid' . $value->returnsellbillid . ' ">تفاصيل </a>';
    $myBill->client = $value->returnsellbillclientname;
    $myBill->datetime = $value->returnsellbilldate;
    if ($day == 1) {
      $myBill->monetary -= (int)$value->returnsellbilltotalpayed;
      $myBill->totalbill -= (int)$value->returnsellbillaftertotalbill;
    } else {
      $myBill->monetary -= (int)$value->returnsellbilltotalpayed;
      $myBill->totalbill -= (int)$value->returnsellbillaftertotalbill;
    }
    array_push($allDataArr, $myBill);
  }
  $monetary = 0;
  $totalbill = 0;
  foreach ($allDataArr as $value) {

    $totalbill += $value->totalbill;
    $monetary += $value->monetary;
  }


  if ($day == 1) {
    $myArr = array();
    $existDate = array();
    foreach ($allDataArr as $value) {
      $dt = strtotime($value->datetime);
      if (in_array(date("Y-m-d", $dt), $existDate)) {
        $key = array_search(date("Y-m-d", $dt), $existDate);
        $myObj = $myArr[$key];

        $myObj->totalbill += $value->totalbill;
        $myObj->monetary += $value->monetary;
      } else {
        array_push($existDate, date("Y-m-d", $dt));

        array_push($myArr, $value);
      }
    }
    $allDataArr =  $myArr;
  };
  $all = count($allDataArr);
  $output = array(
    "draw" => intval($draw),
    "iTotalRecords" => $all,
    "iTotalDisplayRecords" => $all,
    "data" => array()
  );



  if ($day  == 0) {

    foreach ($allDataArr as $value) {
      $sub_array = array();
      $sub_array[] = $value->name;
      $sub_array[] = $value->datetime;
      $sub_array[] = $value->totalbill - $value->monetary;
      $sub_array[] = $value->monetary;
      $sub_array[] = $value->totalbill;
      $sub_array[] = $value->client;
      $sub_array[] = $value->id;
      $output['data'][] = $sub_array;
    };
    $sub_array = array();
    $sub_array[] =   '************';

    $sub_array[] =  '<h3 style="color:  tomato ;" > الاجمالى    </h3>';
    $sub_array[] =  '<h3 style="color:  violet;" > ' . ($totalbill - $monetary) . '    </h3>';
    $sub_array[] =  '<h3 style="color:  gray;" > ' . $monetary . '    </h3>';
    $sub_array[] =   '<h3 style="color: slateblue ;" >  ' . $totalbill . '   </h3>';
    $sub_array[] = '************';

    $sub_array[] = '************';

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


  if ($day  == 1) {


    foreach ($allDataArr as $value) {
      $sub_array = array();
      $sub_array[] = '************';
      $sub_array[] = $value->datetime;
      $sub_array[] = $value->totalbill - $value->monetary;
      $sub_array[] = $value->monetary;
      $sub_array[] = $value->totalbill;
      $sub_array[] = '************';
      $sub_array[] = '************';

      $output['data'][] = $sub_array;
    };
    $sub_array = array();
    $sub_array[] =   '************';
    $sub_array[] =  '<h3 style="color:  tomato ;" > الاجمالى    </h3>';
    $sub_array[] =  '<h3 style="color:  violet;" > ' . ($totalbill - $monetary) . '    </h3>';
    $sub_array[] =  '<h3 style="color:  gray;" > ' . $monetary . '    </h3>';
    $sub_array[] =   '<h3 style="color: slateblue ;" >  ' . $totalbill . '   </h3>';
    $sub_array[] =   '************';
    $sub_array[] = '************';
    $output['data'][] = $sub_array;
  };










  echo json_encode($output, JSON_UNESCAPED_SLASHES);
}
