<?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
// GOES HERE ....................
include("../public/include_dao.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');

//Breadcrumb
require_once("../library/breadcrumb.php");

include_once("dailyentryfun.php");

//get the do the action

$do = $_GET['do'];
$langs = $_SESSION['erp_lang'];
include_once("../views/languages/$langs/success.php");
include_once("../views/languages/$langs/error.php");
/* ======================

  Controller Name :-storeCTRL

  OPERTATION in Controller

  1-display add form
  2- add in storerawmaterails tble
  3-display sucess or error
  4-display show and delete
  5-display edit
  ======================== */

if ($do != "printpatreport" && $do != 'printcompanyreport' && $do != 'printinscompanyreport') {
    $smarty->display("header.html");
}

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

$clientArea = new Clientarea();
$clientAreaDAO = new ClientareaMySqlDAO();
$clientAreaEX = new ClientareaMySqlExtDAO();

// insurancecompanies
$insuranceCompanies = new Insurancecompanie();
$insuranceCompaniesDAO = new InsurancecompaniesMySqlDAO();

// branch
$branch = new Branch();
$branchDAO = new BranchMySqlDAO();

//bill
$bills = new Bill();
$billsDAO = new BillsMySqlDAO();
$billsEX = new BillsMySqlExtDAO();


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

//Breadcrumb
$breadcrumbObject = new Breadcrumb();

//create object from user
## dispaly  add form

if (!isset($_GET['do'])) {
    include_once("../public/authentication.php");
}

## add bill
elseif ($do == "patreport") {
    include_once("../public/authentication.php");

    $clientArea = $clientAreaDAO->queryAll();
    $smarty->assign("clientArea", $clientArea);

    ## insurance Companies
    $insuranceCompanies = $insuranceCompaniesDAO->queryByDeleted(0);
    $smarty->assign('insuranceCompanies', $insuranceCompanies);

    ## insurance Companies
    $branches = $branchDAO->queryAll();
    $smarty->assign('branches', $branches);

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

    ## client Data
    $clientData = getClientData();
    $smarty->assign('clientData', $clientData);

    $smarty->display("reportsview/patients.html"); //the template page
}


## add bill
elseif ($do == "printpatreport") {
    include_once("../public/authentication.php");
    $smarty->assign("headName", 'Our Patient Group Statement');
    
    $clientAreaId = filter_input(INPUT_POST, "clientArea");
    $insuranceCompanyId = filter_input(INPUT_POST, "insuranceCompany");
    $branchId = filter_input(INPUT_POST, "branchId");
    $clientId = filter_input(INPUT_POST, "clientId");
    $searchDateFrom = filter_input(INPUT_POST, "searchDateFrom");
    //$searchDateTo = filter_input(INPUT_POST, "searchDateTo");
    ## company name
    $clientArea = $clientAreaDAO->load($clientAreaId);
    $smarty->assign("clientArea", $clientArea);

    ## insurance Company name
    $insuranceCompanies = $insuranceCompaniesDAO->load($insuranceCompanyId);
    $smarty->assign("insuranceCompany", $insuranceCompanies);

    ## insurance Company name
    $branch = $branchDAO->load($branchId);
    $smarty->assign("branch", $branch);


    ## get  clients of this company  which make bills in this month and payed by specific insurance company
    getBillsPat($clientAreaId, $insuranceCompanyId, $branchId, $searchDateFrom, $clientId);


    ## search month
    if ($searchDateFrom) {
        $batchMonthFrom = getBatchMonthEn($searchDateFrom);
        $smarty->assign("batchMonthFrom", $batchMonthFrom);
    }

//    if ($searchDateTo) {
//        $batchMonthTo = getBatchMonthAr($searchDateTo);
//        $smarty->assign("batchMonthTo", $batchMonthTo);
//    }

    $smarty->assign("today", date('Y-m-d'));
    $smarty->display("reportsview/printpat.html"); //the template page
}

####
elseif ($do == "companyreport") {
    include_once("../public/authentication.php");

    $clientArea = $clientAreaDAO->queryAll();
    $smarty->assign("clientArea", $clientArea);

    ## insurance Companies
    $insuranceCompanies = $insuranceCompaniesDAO->queryByDeleted(0);
    $smarty->assign('insuranceCompanies', $insuranceCompanies);

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

    ## insurance Companies
    $branches = $branchDAO->queryAll();
    $smarty->assign('branches', $branches);

    $smarty->display("reportsview/company.html"); //the template page
}

##
elseif ($do == "printcompanyreport") {
    include_once("../public/authentication.php");

    $smarty->assign("headName", 'Our Patient Summary  Statement');

    $clientAreaId = filter_input(INPUT_POST, "clientArea");
    $insuranceCompanyId = filter_input(INPUT_POST, "insuranceCompany");
    $branchId = filter_input(INPUT_POST, "branchId");
    $searchDateFrom = filter_input(INPUT_POST, "searchDateFrom");

    ## get  clients of this company  which make bills in this month and payed by specific insurance company
    getBillsCompany($clientAreaId, $insuranceCompanyId, $branchId, $searchDateFrom);

    ## insurance Company name
    $branch = $branchDAO->load($branchId);
    $smarty->assign("branch", $branch);

    ## insurance Company name
    $insuranceCompanies = $insuranceCompaniesDAO->load($insuranceCompanyId);
    $smarty->assign("insuranceCompany", $insuranceCompanies);


    ## search month
    if ($searchDateFrom) {
        $batchMonthFrom = getBatchMonthEn($searchDateFrom);
        $smarty->assign("batchMonthFrom", $batchMonthFrom);
    }

    $smarty->assign("today", date('Y-m-d'));


    $smarty->display("reportsview/companyprint.html"); //the template page
}


##
elseif ($do == "inscompanyreport") {
    include_once("../public/authentication.php");
    ## insurance Companies
    $insuranceCompanies = $insuranceCompaniesDAO->queryByDeleted(0);
    $smarty->assign('insuranceCompanies', $insuranceCompanies);

    ## insurance Companies
    $branches = $branchDAO->queryAll();
    $smarty->assign('branches', $branches);

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

    $smarty->display("reportsview/inscompany.html"); //the template page
}

##
elseif ($do == "printinscompanyreport") {
    include_once("../public/authentication.php");

    $smarty->assign("headName", '');


    $insuranceCompanyId = filter_input(INPUT_POST, "insuranceCompany");
    $branchId = filter_input(INPUT_POST, "branchId");
    $searchDateFrom = filter_input(INPUT_POST, "searchDateFrom");


    ## get   company  which make bills in this month and payed by specific insurance company
    getBillsInsCompany($insuranceCompanyId, $branchId, $searchDateFrom);

    ## insurance Company name
    $branch = $branchDAO->load($branchId);
    $smarty->assign("branch", $branch);


    ## search month
    if ($searchDateFrom) {
        $batchMonthFrom = getBatchMonthAr($searchDateFrom);
        $smarty->assign("batchMonthFrom", $batchMonthFrom);
    }

    $smarty->assign("today", date('Y-m-d'));
    $smarty->display("reportsview/inscompanyprint.html"); //the template page
}




##
elseif ($do == "paymethod") {
    include_once("../public/authentication.php");
    ## insurance Companies
    $insuranceCompanies = $insuranceCompaniesDAO->queryByDeleted(0);
    $smarty->assign('insuranceCompanies', $insuranceCompanies);

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

    ## insurance Companies
    $branches = $branchDAO->queryAll();
    $smarty->assign('branches', $branches);

    $smarty->display("reportsview/paymethod.html"); //the template page
}


##
elseif ($do == "sucess") {
    $smarty->display("succes.html");
}

##
elseif ($do == "error") {
    $smarty->display("error.html");
}


$smarty->assign("reports", 1);


if ($do != "printpatreport" && $do != 'printcompanyreport' && $do != 'printinscompanyreport') {
    $smarty->display("footer.html");
}



############################################################################################################################################
function getClientData() {
    global $clientExt;

    $alldata = $clientExt->queryAllsup();
    return $alldata;
}

function loadStore() {
    global $myStoreRecord;

    // select all data from Storetbl
    $storeData = $myStoreRecord->queryByConditions(0);
    return $storeData;
}

function getBillsPat($clientAreaId, $insuranceCompanyId, $branchId, $searchDateFrom, $clientId) {
    global $billsEX;
    global $smarty;

    $totalAccept = 0;
    $totalDiscount = 0;
    $totalCarry = 0;
    $totalNet = 0;
    $branchQuery = '';
    $dateFromQuery = '';
    $clientIdQuery = '';

    if ($branchId != -1) {
        $branchQuery = 'and b.branchid  =' . $branchId . ' ';
    }
    if ($searchDateFrom != '') {
        $dateFromQuery = 'and b.billdate like "' . $searchDateFrom . '-%"';
    }
    if ($clientId != -1) {
        $clientIdQuery = 'and c.clientid  =' . $clientId . ' ';
    }

    $result = array();


    $companyClientsBills = $billsEX->getBillsByClientCompanyIdAndInsuranceCompanyId($clientAreaId, $insuranceCompanyId, $branchQuery, $dateFromQuery, $clientIdQuery, '', '');
    foreach ($companyClientsBills as $bill) {
        $bill->networkname = ($bill->companyaccept * ($bill->insdiscountpercent) / 100);  ##  insurance company discount value
        $bill->username = $bill->companyaccept - $bill->networkname - $bill->customercarry;    ## net required insurance from company

        $totalAccept += $bill->companyaccept;
        $totalDiscount += $bill->networkname;
        $totalCarry += $bill->customercarry;
        $totalNet += $bill->username;
    }

    $smarty->assign("totalAccept", $totalAccept);
    $smarty->assign("totalDiscount", $totalDiscount);
    $smarty->assign("totalCarry", $totalCarry);
    $smarty->assign("totalNet", $totalNet);
    $smarty->assign("companyClientsBills", $companyClientsBills);
    $smarty->assign("count", count($companyClientsBills));

    $result['totalAccept'] = $totalAccept;
    $result['totalDiscount'] = $totalDiscount;
    $result['totalCarry'] = $totalCarry;
    $result['totalNet'] = $totalNet;
    $result['companyClientsBills'] = companyClientsBills;
    return $result;
}

function getBillsCompany($clientAreaId, $insuranceCompanyId, $branchId, $searchDateFrom) {
    global $billsEX;
    global $smarty;


    $totalAccept = 0;
    $totalDiscount = 0;
    $totalCarry = 0;
    $totalNet = 0;

    $result = array();

    $companys = getDistinctCompanies($clientAreaId, $insuranceCompanyId, $branchId, $searchDateFrom);
    $branchQuery = '';
    $dateFromQuery = '';
    $joinQuery = 'join clientarea a        on a.id = c.clientareaid';
    $joinQuery1 = ', a.name as companyname';

    if ($branchId != -1) {
        $branchQuery = 'and b.branchid  =' . $branchId . ' ';
    }
    if ($searchDateFrom != '') {
        $dateFromQuery = 'and b.billdate like "' . $searchDateFrom . '-%"';
    }


    foreach ($companys as $company) {
        $i = 1;
        $companyClientsBills = $billsEX->getBillsByClientCompanyIdAndInsuranceCompanyId($company->clientareaid, $insuranceCompanyId, $branchQuery, $dateFromQuery, '', $joinQuery, $joinQuery1);
        foreach ($companyClientsBills as $bill) {
            if ($i == 1) {
                $company->totalAccept = 0;
                $company->totalDiscount = 0;
                $company->totalCarry = 0;
                $company->totalNet = 0;
                $company->companyname = $bill->companyname;
            }

            $bill->networkname = ($bill->companyaccept * ($bill->insdiscountpercent) / 100);  ##  insurance company discount value
            $bill->username = $bill->companyaccept - $bill->networkname - $bill->customercarry;    ## net required insurance from company

            $company->totalAccept = $company->totalAccept + $bill->companyaccept;
            $company->totalDiscount = $company->totalDiscount + $bill->networkname;
            $company->totalCarry = $company->totalCarry + $bill->customercarry;
            $company->totalNet = $company->totalNet + $bill->username;
            $i++;
        }

        $totalAccept = $totalAccept + $company->totalAccept;
        $totalDiscount = $totalDiscount + $company->totalDiscount;
        $totalCarry = $totalCarry + $company->totalCarry;
        $totalNet = $totalNet + $company->totalNet;

        array_push($result, $company);
    }

    $smarty->assign("totalAccept", $totalAccept);
    $smarty->assign("totalDiscount", $totalDiscount);
    $smarty->assign("totalCarry", $totalCarry);
    $smarty->assign("totalNet", $totalNet);
    $smarty->assign("companyClientsBills", $result);
    $smarty->assign("count", count($result));
}

function getDistinctCompanies($clientAreaId, $insuranceCompanyId, $branchId, $searchDateFrom) {
    global $billsEX;

    $branchQuery = '';
    $dateFromQuery = '';
    $clientAreaQuery = '';

    if ($branchId != -1) {
        $branchQuery = 'and b.branchid  =' . $branchId . ' ';
    }
    if ($searchDateFrom != '') {
        $dateFromQuery = 'and b.billdate like "' . $searchDateFrom . '-%"';
    }
    if ($clientAreaId != -1) {
        $clientAreaQuery = 'and c.clientareaid  =' . $clientAreaId . ' ';
    }


    $companys = $billsEX->getDistinctCompaniesWithBillsByInsuranceCompanyId($insuranceCompanyId, $branchQuery, $dateFromQuery, $clientAreaQuery);

    return $companys;
}

function getBatchMonthAr($searchDate) {
    $result = explode("-", $searchDate);
    $monthId = $result[1];
    $month = getmonthnamear($monthId);

    $array = str_split($result[0], 2);
    $year = $array[1];

    $batchMonth = $month . ' - ' . $year;
    return $batchMonth;
}

function getBatchMonthEn($searchDate) {
    $result = explode("-", $searchDate);
    $monthId = $result[1];
    $month = getmonthname($monthId);

    $array = str_split($result[0], 2);
    $year = $array[1];

    $batchMonth = $month . ' - ' . $year;
    return $batchMonth;
}

function getmonthname($monthId) {

    switch ($monthId) {
        case 1:
            $monthName = 'January';
            break;
        case 2:
            $monthName = 'February';
            break;
        case 3:
            $monthName = 'March';
            break;
        case 4:
            $monthName = 'April';
            break;
        case 5:
            $monthName = 'May';
            break;
        case 6:
            $monthName = 'June';
            break;
        case 7:
            $monthName = 'July';
            break;
        case 8:
            $monthName = 'August';
            break;
        case 9:
            $monthName = 'September';
            break;
        case 10:
            $monthName = 'October';
            break;
        case 11:
            $monthName = 'November';
            break;
        case 12:
            $monthName = 'December';
            break;
        default:
            $monthName = ' ';
            break;
    }
    return $monthName;
}

function getmonthnamear($monthId) {
    switch ($monthId) {
        case 1:
            $monthName = 'يناير';
            break;
        case 2:
            $monthName = 'فبراير';
            break;
        case 3:
            $monthName = 'مارس';
            break;
        case 4:
            $monthName = 'أبريل';
            break;
        case 5:
            $monthName = 'مايو';
            break;
        case 6:
            $monthName = 'يونيو';
            break;
        case 7:
            $monthName = 'يوليو';
            break;
        case 8:
            $monthName = 'أغسطس';
            break;
        case 9:
            $monthName = 'سبتمبر';
            break;
        case 10:
            $monthName = 'أكتوبر';
            break;
        case 11:
            $monthName = 'نوفمبر';
            break;
        case 12:
            $monthName = 'ديسمبر';
            break;
        default:
            $monthName = ' ';
            break;
    }
    return $monthName;
}

function getBillsInsCompany($insuranceCompanyId, $branchId, $searchDateFrom) {
    global $billsEX;
    global $insuranceCompaniesDAO;
    global $smarty;

    $result = array();

    $totalAccept = 0;
    $totalDiscount = 0;
    $totalCarry = 0;
    $totalNet = 0;


    $branchQuery = '';
    $dateFromQuery = '';
    $insCompanyQuery = '';

    if ($insuranceCompanyId != -1) {
        $insCompanyQuery = 'and b.insurancecompanyid  =' . $insuranceCompanyId . ' ';
    }

    if ($branchId != -1) {
        $branchQuery = 'and b.branchid  =' . $branchId . ' ';
    }


    if ($searchDateFrom != '') {
        $dateFromQuery = 'and b.billdate like "' . $searchDateFrom . '-%"';
    }



    $insuranceCompanys = getDistinctInsCompanies($insCompanyQuery, $branchQuery, $dateFromQuery);
    foreach ($insuranceCompanys as $company) {
        $company->totalAccept = 0;
        $company->totalDiscount = 0;
        $company->totalCarry = 0;
        $company->totalNet = 0;


        $insuranceCompany = $insuranceCompaniesDAO->load($company->insurancecompanyid);
        $company->companyname = $insuranceCompany->name;

        $companyClientsBills = $billsEX->getBillsByInsuranceCompanyId($company->insurancecompanyid, $branchQuery, $dateFromQuery, '');
        foreach ($companyClientsBills as $bill) {
            $bill->networkname = ($bill->companyaccept * ($bill->insdiscountpercent) / 100);  ##  insurance company discount value
            $bill->username = $bill->companyaccept - $bill->networkname - $bill->customercarry;    ## net required insurance from company

            $company->totalAccept = $company->totalAccept + $bill->companyaccept;
            $company->totalDiscount = $company->totalDiscount + $bill->networkname;
            $company->totalCarry = $company->totalCarry + $bill->customercarry;
            $company->totalNet = $company->totalNet + $bill->username;
            $i++;
        }

        $totalAccept = $totalAccept + $company->totalAccept;
        $totalDiscount = $totalDiscount + $company->totalDiscount;
        $totalCarry = $totalCarry + $company->totalCarry;
        $totalNet = $totalNet + $company->totalNet;

        array_push($result, $company);
    }

    $smarty->assign("totalAccept", $totalAccept);
    $smarty->assign("totalDiscount", $totalDiscount);
    $smarty->assign("totalCarry", $totalCarry);
    $smarty->assign("totalNet", $totalNet);
    $smarty->assign("companyClientsBills", $result);
    $smarty->assign("count", count($result));
}

function getDistinctInsCompanies($insCompanyQuery, $branchQuery, $dateFromQuery) {
    global $billsEX;

    $insCompanys = $billsEX->getDistinctInsCompaniesWithBills($insCompanyQuery, $branchQuery, $dateFromQuery);

    return $insCompanys;
}

?>