<?php

//the global file operation
include("../public/impOpreation.php");

//global varable
global $showoutside;

//to check if the page from .htacess
//$showoutside = $_GET['sn'];
// get the config file
include_once("../public/config.php");

//here the db files that include in the file
include("../public/include_dao.php");
//Bill
require_once('../models/dao/BillsDAO.class.php');
require_once('../models/dto/Bill.class.php');
require_once('../models/mysql/BillsMySqlDAO.class.php');
require_once('../models/mysql/ext/BillsMySqlExtDAO.class.php');

//Client
require_once('../models/dao/ClientDAO.class.php');
require_once('../models/dto/Client.class.php');
require_once('../models/mysql/ClientMySqlDAO.class.php');
require_once('../models/mysql/ext/ClientMySqlExtDAO.class.php');

//Clientdebtchange
require_once('../models/dao/ClientdebtchangeDAO.class.php');
require_once('../models/dto/Clientdebtchange.class.php');
require_once('../models/mysql/ClientdebtchangeMySqlDAO.class.php');
require_once('../models/mysql/ext/ClientdebtchangeMySqlExtDAO.class.php');

require_once('../models/dao/YoutubeLinkDAO.class.php');
require_once('../models/dto/YoutubeLink.class.php');
require_once('../models/mysql/YoutubeLinkMySqlDAO.class.php');
require_once('../models/mysql/ext/YoutubeLinkMySqlExtDAO.class.php');

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

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

  Controller Name :- clientPayedDeptReportController

  OPERTATION in Controller

  1- تقرير سداد ديون عميل بصاريات

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

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

//here goes the instances and general variables
//bill
$bills = new Bill();
$billsDAO = new BillsMySqlDAO();
$billsEX = new BillsMySqlExtDAO();

//Client
$client = new Client();
$clientDAO = new ClientMySqlDAO();
$clientExt = new ClientMySqlExtDAO();

//Clientdebtchange
$clientDeptChange = new Clientdebtchange();
$clientDeptChangeDAO = new ClientdebtchangeMySqlDAO();
$clientDeptChangeExt = new ClientdebtchangeMySqlExtDAO();

$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();

$today = date("Y-m-d");

//check and use the condition that suite this action
if (empty($do) || $do == "show") {// تقرير سداد ديون عميل بصاريات
    //here the permssion check
    include_once("../public/authentication.php");

    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);

    $start_date = $_REQUEST['from'];
    $end_date = $_REQUEST['to'];
    $client = $_POST['client'];
    $bill_no = $_POST['bill_no'];

    $where = '';
    if ((isset($client) && $client != "-1")) {
        $where = ' clientid = ' . $client . ' ';
    }
    if ((isset($bill_no) && $bill_no != "")) {
        if (empty($where)) {
            $where .= ' id = ' . $bill_no . ' ';
        } else {
            $where .= ' and id = ' . $bill_no . ' ';
        }
    }

    if (isset($start_date) && $start_date != "" && isset($end_date) && $end_date != "") {

        if (empty($where)) {
            $where .= ' date(billdate) >= "' . $start_date . '" and date(billdate) <= "' . $end_date . '" ';
        } else {
            $where .= ' and  date(billdate) >= "' . $start_date . '" and date(billdate) <= "' . $end_date . '" ';
        }
    }



    if ((isset($client) && $client != "-1")) {


        $messageData = $clientDAO->load($client);
        $smarty->assign("client_id", $messageData->clientid);

        $message = " اسم العميل : " . $messageData->clientname . " التاريخ: من   :  " . $start_date . " إلى: " . $end_date;
        $smarty->assign("message", $message);

        $clientBills = $billsEX->queryAllforcondemnedbills($where);

        $client_bills = array();
        foreach ($clientBills as $cli_bill_val) {

            $bill_id = $cli_bill_val->billno;
            $client_dept_change = $clientDeptChangeExt->sumByBillId($bill_id);

            $waitvalue = $cli_bill_val->waitvalue - $client_dept_change->clientdebtchangeamount; //# قيمة الدين

            if ($waitvalue > 0) {
                $client_bills[] = array(
                    'id' => $cli_bill_val->id,
                    'billno' => $bill_id,
                    'finalnetbillvalue' => $cli_bill_val->finalnetbillvalue,
                    'waitvalue' => $cli_bill_val->waitvalue - $client_dept_change->clientdebtchangeamount,
                    'total_payed' => $cli_bill_val->cashvalue + $cli_bill_val->cardvalue + $cli_bill_val->companycarry + $client_dept_change->clientdebtchangeamount
                );
            }
        }
//        print_r('<pre>');
//                print_r($client_bills);
//        print_r('<pre>');
        // $smarty->assign("clientBills", $clientBills);
        $smarty->assign("client_bills", $client_bills);
    }

    $smarty->assign("customClientDeptChangeReport", 1);
    $smarty->display("clientPayedDeptReportView/show.html");
}

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