<?php

/*
 * awframework from aw.inc present aw controller
 * it will help you develop easy and fast with secure tools
 * you can edit and add what you feel right
 * don't change in strucure of file
 */

require_once("imp/_imp.php");

class Controllers {

    //public vars
    public $smarty;
    public $hosturl;
    public $controllname;
    public $shcontrollname;
    public $active;

    // it will run in everytime with
    // default requried
    public function __construct() {
        require_once("../public/aw_config.php");
        require_once("imp/_autho.php"); //general functions
        $this->smarty = $smarty;
        $this->hosturl = $hosturl;

        //here name of controll to known
        $this->controllname = 'Completesreport';
        $this->shcontrollname = 'Home';
        $this->smarty->assign('controllname', $this->controllname);
        $this->smarty->assign('shcontrollname', $this->shcontrollname);

        //breadcrumbs
        require_once("../../_library/plugins/Breadcrumbs/Breadcrumbs.php");
        $acty = filter_input(INPUT_GET, 'ac');
        $bc = new breadcrumbs($hosturl, '', '', $acty);
        $bc->crumbs();
        $breadcrumbs = $bc->breadcrumbs;
        $smarty->assign('breadcrumbs', $breadcrumbs);

        # here show the active page in menu
        // array called active[] with level of active element
        $active[0] = 'Completesreport'; //controller name or tab name
        $this->active = $active;


        //here will call all php pages
        require_once("_header.php");
        require_once("_sidebar.php");
        require_once("_leftsidebar.php");
        require_once("_footer.php");
    }

    //here the frist page and
    //the frist action in this class
    public function index() {
        //check login
        autho::checkautho($this->hosturl);
        //check authoize
        autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);
        //here will load the menu show
        $patients = R::findAll('patients');
        $this->smarty->assign('patients', $patients);


        // $infertility = R::findOne('infertilitysheet', 'patientid = 1');
        // $infertility->husbndiagnosis1t = R::findAll('husbndiagnosis1',"id in (0$infertility->husbndiagnosis1)");
        // $infertility->sheetlocationt = R::load('sheetlocation', $infertility->sheetlocation);
        // $infertility->medicalhistorydmt = R::findAll('medicalhistorydm',"id in (0$infertility->medicalhistorydm)");
        // $infertility->surgicalhistorydmt = R::findAll('surgicalhistorydm',"id in (0$infertility->surgicalhistorydm)");
        // $infertility->examinationlocalset = R::findAll('examinationlocalse',"id in (0$infertility->examinationlocalse)");
        // $semen = R::findAll('semeninfertility', 'infertilitysheetid = ?', [$infertility->id]);
        // foreach($semen as $data){
        //     $data->semenplacet = R::load('semenplace', $data->semenplace);
        //     $data->sementypet = R::load('sementype', $data->sementype);
        // }
        // $this->smarty->assign('semen', $semen); 
        // $semen2 = R::findAll('semen2', 'infertilitysheetid = ?', [$infertility->id]);
        // foreach($semen2 as $data){
        //     $data->semen2placesemenfrt = R::load('semen2placesemenfr', $data->semen2placesemenfr);
        //     $data->semen2placeteset = R::load('semen2placetese', $data->semen2placetese);
        //     $data->semen2resultscrotalt = R::findAll('semen2resultscrotal', "id in (0$data->semen2resultscrotal)");
        //     $data->semen2resulttruet = R::load('sementype', $data->semen2resulttrue);
        //     $data->semen2resultkaryot = R::load('semen2resultkaryo', $data->semen2resultkaryo);
        //     $data->semen2resultyqmicrot = R::load('semen2resultyqmicro', $data->semen2resultyqmicro);
        //     $data->semen2resultsdft = R::load('semen2resultsdf', $data->semen2resultsdf);
        // }
        // $this->smarty->assign('semen2', $semen2); 
        // $hormonalprofile = R::findAll('hormonalprofile', 'infertilitysheetid = ?', [$infertility->id]);
        // $this->smarty->assign('hormonalprofile', $hormonalprofile);  
        // $infertility->diagnosist = R::findAll('diagnosis',"id in (0$infertility->diagnosis)"); 
        // $infertility->menstrualregt = R::load('menstrualreg', $infertility->menstrualreg);
        // $infertility->menstrualamountt = R::load('menstrualamount', $infertility->menstrualamount);
        // $infertility->menstrualdysmt = R::load('menstrualdysm', $infertility->menstrualdysm);
        // $infertility->medicalhistoryt = R::findAll('medicalhistory',"id in (0$infertility->medicalhistory)");
        // $infertility->surgicalhistoryt = R::findAll('surgicalhistory',"id in (0$infertility->surgicalhistory)");
        // $infertility->familyhistoryt = R::findAll('familyhistory',"id in (0$infertility->familyhistory)");
        // $awifep = R::findAll('awifep', 'infertilitysheetid = ?', [$infertility->id]);
        // foreach($awifep as $data){
        //     $data->wifesext = R::load('wifesex', $data->wifesex);
        //     $data->wifemodeofdt = R::load('wifemodeofd', $data->wifemodeofd);
        //     $data->wifetypeofdt = R::load('wifetypeofd', $data->wifetypeofd);
        //     $data->wifeobstt = R::findAll('wifeobst', "id in (0$data->wifeobst)");
        //     $data->awifepmethodt = R::findAll('awifepmethod', "id in (0$data->awifepmethod)");
        // }
        // $this->smarty->assign('awifep', $awifep); 
        // $wifeepc = R::findAll('wifeepc', 'infertilitysheetid = ?', [$infertility->id]);
        // foreach($wifeepc as $data){
        //     $data->wifeepctypet = R::load('wifeepctype', $data->wifeepctype);
        //     $data->wifeepctttt = R::load('wifeepcttt', $data->wifeepcttt);
        //     $data->wifeepcobstt = R::load('wifeepcobst', $data->wifeepcobst);
        // }
        // $this->smarty->assign('wifeepc', $wifeepc); 
        // $infertility->localvulvat = R::findAll('localvulva',"id in (0$infertility->localvulva)");
        // $infertility->localvaginat = R::findAll('localvagina',"id in (0$infertility->localvagina)");
        // $infertility->generalbreastt = R::findAll('generalbreast',"id in (0$infertility->generalbreast)");
        // $infertility->localcxt = R::findAll('localcx',"id in (0$infertility->localcx)");
        // $infertility->generalhirsutismt = R::load('generalhirsutism', $infertility->generalhirsutism);
        // $infertility->generalthyroidt = R::load('generalthyroid', $infertility->generalthyroid);
        // $hormonalprofile2 = R::findAll('hormonalprofile2', 'infertilitysheetid = ?', [$infertility->id]);
        // $this->smarty->assign('hormonalprofile2', $hormonalprofile2); 
        // $tvs = R::findAll('tvs', 'infertilitysheetid = ?', [$infertility->id]);
        // foreach($tvs as $data){
        //     $data->tvsutt = R::findAll('tvsut', "id in (0$data->tvsut)");
        //     $data->tvscxt = R::findAll('tvscx', "id in (0$data->tvscx)");
        //     $data->tvsrot = R::findAll('tvsro', "id in (0$data->tvsro)");
        //     $data->tvslot = R::findAll('tvslo', "id in (0$data->tvslo)");
        //     $data->tvspelvist = R::findAll('tvspelvis', "id in (0$data->tvspelvis)");
        // }
        // $this->smarty->assign('tvs', $tvs); 
        // $dtvs = R::findAll('dtvs', 'infertilitysheetid = ?', [$infertility->id]);
        // foreach($dtvs as $data){
        //     $data->dtvsplacet = R::findAll('dtvsplace', "id in (0$data->dtvsplace)");
        //     $data->dtvsresultt = R::findAll('dtvsresult', "id in (0$data->dtvsresult)");
        // }
        // $this->smarty->assign('dtvs', $dtvs); 
        // $sis = R::findAll('sis', 'infertilitysheetid = ?', [$infertility->id]);
        // foreach($sis as $data){
        //     $data->sisresultt = R::findAll('sisresult', "id in (0$data->sisresult)");
        // }
        // $this->smarty->assign('sis', $sis); 
        // $hsg = R::findAll('hsg', 'infertilitysheetid = ?', [$infertility->id]);
        // foreach($hsg as $data){
        //     $data->hsgplacet = R::load('hsgplace', $data->hsgplace);
        //     $data->hsgutt = R::load('hsgut', $data->hsgut);
        //     $data->hsgrtt = R::findAll('hsgrt', "id in (0$data->hsgrt)");
        //     $data->hsgltt = R::findAll('hsglt', "id in (0$data->hsglt)");
        //     $data->hsgpelvist = R::load('hsgpelvis', $data->hsgpelvis);
        // }
        // $this->smarty->assign('hsg', $hsg); 
        // $hysteroscopy = R::findAll('hysteroscopy', 'infertilitysheetid = ?', [$infertility->id]);
        // foreach($hysteroscopy as $data){
        //     $data->copyplacet = R::load('copyplace', $data->copyplace);
        //     $data->copydilt = R::load('copydil', $data->copydil);
        //     $data->copyintrodt = R::load('copyintrod', $data->copyintrod);
        //     $data->copycxt = R::findAll('copycx', "id in (0$data->copycx)");
        //     $data->copycavityt = R::findAll('copycavity', "id in (0$data->copycavity)");
        //     $data->copyrostiumt = R::load('copyrostium', $data->copyrostium);          
        //     $data->copylostiumt = R::load('copylostium', $data->copylostium);            
        //     $data->copymcxt = R::findAll('copymcx', "id in (0$data->copymcx)");
        //     $data->copymcavityt = R::findAll('copymcavity', "id in (0$data->copymcavity)");
        //     $data->copymrostiumt = R::findAll('copymrostium', "id in (0$data->copymrostium)");
        //     $data->copymlostiumt = R::findAll('copymlostium', "id in (0$data->copymlostium)");
        // }
        // $this->smarty->assign('hysteroscopy', $hysteroscopy); 
        // $laparoscopy = R::findAll('laparoscopy', 'infertilitysheetid = ?', [$infertility->id]);
        // foreach($laparoscopy as $data){
        //     $data->laparplacet = R::load('laparplace', $data->laparplace);
        //     $data->laparult = R::findAll('laparul', "id in (0$data->laparul)");
        //     $data->laparrtt = R::findAll('laparrt', "id in (0$data->laparrt)");      
        //     $data->laparltt = R::findAll('laparlt', "id in (0$data->laparlt)");
        //     $data->laparrot = R::findAll('laparro', "id in (0$data->laparro)");
        //     $data->laparlot = R::findAll('laparlo', "id in (0$data->laparlo)");
        //     $data->laparpelvist = R::findAll('laparpelvis', "id in (0$data->laparpelvis)");
        //     $data->laparmutt = R::findAll('laparmut', "id in (0$data->laparmut)");
        //     $data->laparmrtt = R::findAll('laparmrt', "id in (0$data->laparmrt)");      
        //     $data->laparmltt = R::findAll('laparmlt', "id in (0$data->laparmlt)");
        //     $data->laparmrot = R::findAll('laparmro', "id in (0$data->laparmro)");
        //     $data->laparmlot = R::findAll('laparmlo', "id in (0$data->laparmlo)");
        //     $data->laparmpelvist = R::findAll('laparmpelvis', "id in (0$data->laparmpelvis)");
        // }
        // $this->smarty->assign('laparoscopy', $laparoscopy);
        // $infertility->tttdat = R::load('tttda', $infertility->tttda);
        // $infertility->tttmetformint = R::load('tttmetformin', $infertility->tttmetformin);
        // $infertility->ttttypet = R::findAll('ttttype',"id in (0$infertility->ttttype)");
        // $infertility->tttresponset = R::load('tttresponse', $infertility->tttresponse);
        // $icsi = R::findAll('icsi', 'infertilitysheetid = ?', [$infertility->id]);
        // foreach($icsi as $data){
        //     $data->icsiplacet = R::load('icsiplace', $data->icsiplace);
        // }
        // $this->smarty->assign('icsi', $icsi); 
        // $folliculom = R::findAll('folliculom', 'infertilitysheetid = ?', [$infertility->id]);
        // $this->smarty->assign('folliculom', $folliculom);
        // $operations = R::findAll('operations', 'infertilitysheetid = ?', [$infertility->id]);
        // foreach($operations as $data){
        //     $data->operationt = R::findAll('operation', "id in (0$data->operation)");
        //     $data->placet = R::load('place', $data->place);
        //     $data->histopatht = R::findAll('histopath', "id in (0$data->histopath)");
        // }
        // $this->smarty->assign('operations', $operations);
        // $infertility->sheethusbandt = R::findAll('sheethusband',"id in (0$infertility->sheethusband)");
        // $infertility->sheetwifet = R::findAll('sheetwife',"id in (0$infertility->sheetwife)");
        // $newvisit = R::findAll('newvisit', 'infertilitysheetid = ?', [$infertility->id]);
        // foreach($newvisit as $data){
        //     $data->newvisitcyclest = R::load('newvisitcycles', $data->newvisitcycles);
        // }
        // $this->smarty->assign('newvisit', $newvisit);
        // $this->smarty->assign('infertility',$infertility); 
        // $ivfsheet = R::findOne('ivfsheet', 'patientid = 1 and endpreg = 0');
        // $ivfsheet->antypet = R::load('antype', $ivfsheet->antype);
        // $ivfsheet->antypest = R::load('antypes', $ivfsheet->antypes);
        // $ivfsheet->icsiprotocolt = R::load('icsiprotocol', $ivfsheet->anprotocol);
        // $ivfsheet->ssement = R::load('ssemen', $ivfsheet->ssemen);
        // $ivfsheet->sseement = R::load('sseemen', $ivfsheet->sseemen);
        // $ivfsheet->icsisement = R::load('icsisemen', $ivfsheet->sseemmen);
        // $ivfsheet->sseemmeent = R::load('sseemmeen', $ivfsheet->sseemmeen);
        // $this->smarty->assign('ivfsheet', $ivfsheet); 
        // $investigations = R::findAll('investigations', 'ancsheetid = ?', [$ivfsheet->id]);
        // $this->smarty->assign('investigations', $investigations); 
        // $ovst = R::findAll('ovst', 'ivfsheetid = ?', [$ivfsheet->id]);
        // $this->smarty->assign('ovst', $ovst); 
        // $eprep = R::findAll('eprep', 'ivfsheetid = ?', [$ivfsheet->id]);
        // foreach($eprep as $data){
        //     $data->epreplpst = R::load('epreplps', $data->epreplps);
        // }
        // $this->smarty->assign('eprep', $eprep);
        // $ancsheet = R::findOne('ancsheet', 'patientid = 1 and endpreg = 0');
        // $ancsheet->placentat = R::load('placenta', $ancsheet->placenta);
        // $this->smarty->assign('ancsheet', $ancsheet); 
        // $investigations = R::findAll('investigations', 'ancsheetid = ?', [$ancsheet->id]);
        // $this->smarty->assign('ancinvestigations', $investigations); 
        // // $ovst = R::findAll('ovst', 'ancsheetid = ?', [$ancsheet->id]);
        // // $this->smarty->assign('ovst', $ovst); 
        // $ancnewvisit = R::findAll('ancnewvisit', 'ancsheetid = ?', [$ancsheet->id]);
        //  foreach($ancnewvisit as $data){
        //     $data->usnt = R::load('usn', $data->usn);
        //     $data->usplacet = R::load('usplace', $data->usplace);
        //     $data->usaft = R::load('usaf', $data->usaf);
        // }
        // $this->smarty->assign('ancnewvisit', $ancnewvisit); 
        // $registeration = R::findAll('registeration', 'ancsheetid = ?', [$ancsheet->id]);
        //  foreach($registeration as $data){
        //     $data->awifept = R::load('awifep', $data->awifep);
        //     $data->place2t = R::load('place2', $data->place2);
        //     $data->origint = R::load('origin', $data->origin);
        //     $data->typet = R::load('type', $data->type);
        // }
        // $this->smarty->assign('registeration', $registeration); 
        // $gynasheet = R::findOne('gynasheet', 'patientid = 1');
        // $this->smarty->assign('gynasheet', $gynasheet);
        // $newvisitg = R::findAll('newvisitg', 'gynasheetid = ?', [$gynasheet->id]);
        // foreach($newvisitg as $data){
        //     $data->newvisitcyclest = R::load('newvisitcycles', $data->newvisitcycles);
        //     $data->newvisitgcot = R::findAll('newvisitgco', "id in (0$data->newvisitgco)");
        //     $data->newvisitgdiagt = R::findAll('newvisitgdiag', "id in (0$data->newvisitgdiag)");
        // }
        // $this->smarty->assign('newvisitg', $newvisitg);


        header::headershow($this->smarty, $this->hosturl);
        sidebar::sidebarshow($this->smarty, $this->hosturl, $this->active);
        $this->smarty->display('reports2/completesreport.html');
        leftsidebar::leftsidebarshow($this->smarty, $this->hosturl);
        footer::footershow($this->smarty, $this->hosturl);
    }

    public function Add() {
        $tableName = filter_input(INPUT_POST, 'table');
        $id = filter_input(INPUT_POST, 'id');
        $colName = filter_input(INPUT_POST, 'name');
        $value = filter_input(INPUT_POST, 'value');

        try {
            $obj = R::load($tableName, $id);
            $obj->$colName = $value;
            R::store($obj);
        } catch (Exception $e) {
            
        }
    }

    public function search() {
        //check login
        autho::checkautho($this->hosturl);
        //check authoize
        autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);

        //R::debug(TRUE);
        $patientid = filter_input(INPUT_POST, 'patientid');
        $patientdata = R::load('patients', $patientid);
        if ($patientdata->dateofbirth != '0000-00-00' && $patientdata->dateofbirth != '' && $patientdata->dateofbirth != Null) {
            $currentdate_x = time();
            $currentdateofbirth = strtotime($patientdata->dateofbirth);
            $newage = $currentdate_x - $currentdateofbirth;
            $y = ($newage / (60 * 60 * 24 * 365));
            $patientdata->new_dateofbirth_calc = floor($y);
        } else {
            $patientdata->new_dateofbirth_calc = " ";
        }

        if (!empty($patientdata->mirragefromdate) && $patientdata->mirragefromdate != '0000-00-00') {
            $today = new DateTime(date('Y-m-d'));
            $mirragedate = new DateTime($patientdata->mirragefromdate);
            $interval = $today->diff($mirragedate);
            $patientdata->mirrageyears = $interval->y;
            $patientdata->mirragemonths = $interval->m;
        } else {
            $patientdata->mirrageyears = "";
            $patientdata->mirragemonths = "";
        }

        if (!empty($patientdata->statuesid)) {
            $status = R::load('wifestatus', $patientdata->statuesid);
            $patientdata->wifeStatus = $status->title;
        } else {
            $patientdata->wifeStatus = "";
        }

        $this->smarty->assign('patientdata', $patientdata);
        $infertility = R::findOne('infertilitysheet', 'patientid = ? ', [$patientid]);
        $infertility->husbndiagnosis1t = R::findAll('husbndiagnosis1', "id in (0$infertility->husbndiagnosis1)");
        $infertility->sheetlocationt = R::load('sheetlocation', $infertility->sheetlocation);
        $infertility->medicalhistorydmt = R::findAll('medicalhistorydm', "id in (0$infertility->medicalhistorydm)");
        $infertility->surgicalhistorydmt = R::findAll('surgicalhistorydm', "id in (0$infertility->surgicalhistorydm)");
        $infertility->examinationlocalset = R::findAll('examinationlocalse', "id in (0$infertility->examinationlocalse)");
        $semen = R::findAll('semeninfertility', 'infertilitysheetid = ?', [$infertility->id]);
        foreach ($semen as $data) {
            $data->semenplacet = R::load('semenplace', $data->semenplace);
            $data->sementypet = R::load('sementype', $data->sementype);
        }
        $this->smarty->assign('semen', $semen);
        $semen2 = R::findAll('semen2', 'infertilitysheetid = ?', [$infertility->id]);
        foreach ($semen2 as $data) {
            $data->semen2placesemenfrt = R::load('semen2placesemenfr', $data->semen2placesemenfr);
            $data->semen2placeteset = R::load('semen2placetese', $data->semen2placetese);
            $data->semen2resultscrotalt = R::findAll('semen2resultscrotal', "id in (0$data->semen2resultscrotal)");
            $data->semen2resulttruet = R::load('sementype', $data->semen2resulttrue);
            $data->semen2resultkaryot = R::load('semen2resultkaryo', $data->semen2resultkaryo);
            $data->semen2resultyqmicrot = R::load('semen2resultyqmicro', $data->semen2resultyqmicro);
            $data->semen2resultsdft = R::load('semen2resultsdf', $data->semen2resultsdf);
        }
        $this->smarty->assign('semen2', $semen2);
        $hormonalprofile = R::findAll('hormonalprofile', 'infertilitysheetid = ?', [$infertility->id]);
        $this->smarty->assign('hormonalprofile', $hormonalprofile);
        $infertility->diagnosist = R::findAll('diagnosis', "id in (0$infertility->diagnosis)");
        $infertility->menstrualregt = R::load('menstrualreg', $infertility->menstrualreg);
        $infertility->menstrualamountt = R::load('menstrualamount', $infertility->menstrualamount);
        $infertility->menstrualdysmt = R::load('menstrualdysm', $infertility->menstrualdysm);
        $infertility->medicalhistoryt = R::findAll('medicalhistory', "id in (0$infertility->medicalhistory)");
        $infertility->surgicalhistoryt = R::findAll('surgicalhistory', "id in (0$infertility->surgicalhistory)");
        $infertility->familyhistoryt = R::findAll('familyhistory', "id in (0$infertility->familyhistory)");

        $awifep = R::findAll('awifep', 'infertilitysheetid = ?', [$infertility->id]);
        foreach ($awifep as $data) {
            $data->wifesext = R::load('wifesex', $data->wifesex);
            $data->wifemodeofdt = R::load('wifemodeofd', $data->wifemodeofd);
            $data->wifetypeofdt = R::load('wifetypeofd', $data->wifetypeofd);
            $data->wifeobstt = R::findAll('wifeobst', "id in (0$data->wifeobst)");
            $data->awifepmethodt = R::findAll('awifepmethod', "id in (0$data->awifepmethod)");
        }
        $this->smarty->assign('awifep', $awifep);

        $wifeepc = R::findAll('wifeepc', 'infertilitysheetid = ?', [$infertility->id]);
        foreach ($wifeepc as $data) {
            $data->wifeepctypet = R::load('wifeepctype', $data->wifeepctype);
            $data->wifeepctttt = R::load('wifeepcttt', $data->wifeepcttt);
            $data->wifeepcobstt = R::load('wifeepcobst', $data->wifeepcobst);
        }
        $this->smarty->assign('wifeepc', $wifeepc);
        $infertility->localvulvat = R::findAll('localvulva', "id in (0$infertility->localvulva)");
        $infertility->localvaginat = R::findAll('localvagina', "id in (0$infertility->localvagina)");
        $infertility->generalbreastt = R::findAll('generalbreast', "id in (0$infertility->generalbreast)");
        $infertility->localcxt = R::findAll('localcx', "id in (0$infertility->localcx)");
        $infertility->generalhirsutismt = R::load('generalhirsutism', $infertility->generalhirsutism);
        $infertility->generalthyroidt = R::load('generalthyroid', $infertility->generalthyroid);
        $hormonalprofile2 = R::findAll('hormonalprofile2', 'infertilitysheetid = ?', [$infertility->id]);
        $this->smarty->assign('hormonalprofile2', $hormonalprofile2);


        $tvs = R::findAll('tvs', 'infertilitysheetid = ?', [$infertility->id]);
        foreach ($tvs as $data) {
            $data->tvsutt = R::findAll('tvsut', "id in (0$data->tvsut)");
            $data->tvscxt = R::findAll('tvscx', "id in (0$data->tvscx)");
            $data->tvsrot = R::findAll('tvsro', "id in (0$data->tvsro)");
            $data->tvslot = R::findAll('tvslo', "id in (0$data->tvslo)");
            $data->tvspelvist = R::findAll('tvspelvis', "id in (0$data->tvspelvis)");
        }
        $this->smarty->assign('tvs', $tvs);
        $dtvs = R::findAll('dtvs', 'infertilitysheetid = ?', [$infertility->id]);
        foreach ($dtvs as $data) {
            $data->dtvsplacet = R::findAll('dtvsplace', "id in (0$data->dtvsplace)");
            $data->dtvsresultt = R::findAll('dtvsresult', "id in (0$data->dtvsresult)");
        }
        $this->smarty->assign('dtvs', $dtvs);
        $sis = R::findAll('sis', 'infertilitysheetid = ?', [$infertility->id]);
        foreach ($sis as $data) {
            $data->sisresultt = R::findAll('sisresult', "id in (0$data->sisresult)");
        }
        $this->smarty->assign('sis', $sis);
        $hsg = R::findAll('hsginfertility', 'infertilitysheetid = ?', [$infertility->id]);
        foreach ($hsg as $data) {
            $data->hsgplacet = R::load('hsgplace', $data->hsgplace);
            $data->hsgutt = R::load('hsgut', $data->hsgut);
            $data->hsgrtt = R::findAll('hsgrt', "id in (0$data->hsgrt)");
            $data->hsgltt = R::findAll('hsglt', "id in (0$data->hsglt)");
            $data->hsgpelvist = R::load('hsgpelvis', $data->hsgpelvis);
        }
        $this->smarty->assign('hsg', $hsg);
        $hysteroscopy = R::findAll('hysteroscopyinfertility', 'infertilitysheetid = ?', [$infertility->id]);
        foreach ($hysteroscopy as $data) {
            $data->copyplacet = R::load('copyplace', $data->copyplace);
            $data->copydilt = R::load('copydil', $data->copydil);
            $data->copyintrodt = R::load('copyintrod', $data->copyintrod);
            $data->copycxt = R::findAll('copycx', "id in (0$data->copycx)");
            $data->copycavityt = R::findAll('copycavity', "id in (0$data->copycavity)");
            $data->copyrostiumt = R::load('copyrostium', $data->copyrostium);
            $data->copylostiumt = R::load('copylostium', $data->copylostium);
            $data->copymcxt = R::findAll('copymcx', "id in (0$data->copymcx)");
            $data->copymcavityt = R::findAll('copymcavity', "id in (0$data->copymcavity)");
            $data->copymrostiumt = R::findAll('copymrostium', "id in (0$data->copymrostium)");
            $data->copymlostiumt = R::findAll('copymlostium', "id in (0$data->copymlostium)");
        }
        $this->smarty->assign('hysteroscopy', $hysteroscopy);
        $laparoscopy = R::findAll('laparoscopyinfertility', 'infertilitysheetid = ?', [$infertility->id]);
        foreach ($laparoscopy as $data) {
            $data->laparplacet = R::load('laparplace', $data->laparplace);
            $data->laparult = R::findAll('laparul', "id in (0$data->laparul)");
            $data->laparrtt = R::findAll('laparrt', "id in (0$data->laparrt)");
            $data->laparltt = R::findAll('laparlt', "id in (0$data->laparlt)");
            $data->laparrot = R::findAll('laparro', "id in (0$data->laparro)");
            $data->laparlot = R::findAll('laparlo', "id in (0$data->laparlo)");
            $data->laparpelvist = R::findAll('laparpelvis', "id in (0$data->laparpelvis)");
            $data->laparmutt = R::findAll('laparmut', "id in (0$data->laparmut)");
            $data->laparmrtt = R::findAll('laparmrt', "id in (0$data->laparmrt)");
            $data->laparmltt = R::findAll('laparmlt', "id in (0$data->laparmlt)");
            $data->laparmrot = R::findAll('laparmro', "id in (0$data->laparmro)");
            $data->laparmlot = R::findAll('laparmlo', "id in (0$data->laparmlo)");
            $data->laparmpelvist = R::findAll('laparmpelvis', "id in (0$data->laparmpelvis)");
        }
        $this->smarty->assign('laparoscopy', $laparoscopy);
        $infertility->tttdat = R::load('tttda', $infertility->tttda);
        $infertility->tttmetformint = R::load('tttmetformin', $infertility->tttmetformin);
        $infertility->ttttypet = R::findAll('ttttype', "id in (0$infertility->ttttype)");
        $infertility->tttresponset = R::load('tttresponse', $infertility->tttresponse);
        $icsi = R::findAll('icsi', 'infertilitysheetid = ?', [$infertility->id]);
        foreach ($icsi as $data) {
            $data->icsiplacet = R::load('icsiplace', $data->icsiplace);
        }
        $this->smarty->assign('icsi', $icsi);

        $folliculom = R::findAll('folliculom', 'infertilitysheetid = ?', [$infertility->id]);
        $this->smarty->assign('folliculom', $folliculom);
        $operations = R::findAll('operations', 'infertilitysheetid = ?', [$infertility->id]);
        foreach ($operations as $data) {
            $data->operationt = R::findAll('operation', "id in (0$data->operation)");
            $data->placet = R::load('place', $data->place);
            $data->histopatht = R::findAll('histopath', "id in (0$data->histopath)");
        }
        $this->smarty->assign('operations', $operations);
        $infertility->sheethusbandt = R::findAll('sheethusband', "id in (0$infertility->sheethusband)");
        $infertility->sheetwifet = R::findAll('sheetwife', "id in (0$infertility->sheetwife)");

        $newvisit = R::findAll('newvisit', 'infertilitysheetid = ?', [$infertility->id]);
        foreach ($newvisit as $data) {
            $data->newvisitcyclest = R::load('newvisitcycles', $data->newvisitcycles);
        }
        $this->smarty->assign('newvisit', $newvisit);

        $this->smarty->assign('infertility', $infertility);
        $ivfsheet = R::findOne('ivfsheet', 'patientid = ? and endpreg = 0', [$patientid]);
        $ivfsheet->antypet = R::load('antype', $ivfsheet->antype);
        $ivfsheet->antypest = R::load('antypes', $ivfsheet->antypes);
        $ivfsheet->icsiprotocolt = R::load('icsiprotocol', $ivfsheet->anprotocol);
        $ivfsheet->ssement = R::load('ssemen', $ivfsheet->ssemen);
        $ivfsheet->sseement = R::load('sseemen', $ivfsheet->sseemen);
        $ivfsheet->icsisement = R::load('icsisemen', $ivfsheet->sseemmen);
        $ivfsheet->sseemmeent = R::load('sseemmeen', $ivfsheet->sseemmeen);

        $this->smarty->assign('ivfsheet', $ivfsheet);
        $investigations = R::findAll('investigations', 'ancsheetid = ?', [$ivfsheet->id]);
        $this->smarty->assign('investigations', $investigations);
        $ovst = R::findAll('ovst', 'ivfsheetid = ?', [$ivfsheet->id]);
        $this->smarty->assign('ovst', $ovst);
        $eprep = R::findAll('eprep', 'ivfsheetid = ?', [$ivfsheet->id]);
        foreach ($eprep as $data) {
            $data->epreplpst = R::load('epreplps', $data->epreplps);
        }
        $this->smarty->assign('eprep', $eprep);



        $ancsheet = R::findOne('ancsheet', 'patientid = ? and endpreg = 0', [$patientid]);
        $ancsheet->placentat = R::load('placenta', $ancsheet->placenta);
        $this->smarty->assign('ancsheet', $ancsheet);
        $investigations = R::findAll('investigations', 'ancsheetid = ?', [$ancsheet->id]);
        $this->smarty->assign('ancinvestigations', $investigations);
        // $ovst = R::findAll('ovst', 'ancsheetid = ?', [$ancsheet->id]);
        // $this->smarty->assign('ovst', $ovst); 
        $ancnewvisit = R::findAll('ancnewvisit', 'ancsheetid = ?', [$ancsheet->id]);
        foreach ($ancnewvisit as $data) {
            $data->usnt = R::load('usn', $data->usn);
            $data->usplacet = R::load('usplace', $data->usplace);
            $data->usaft = R::load('usaf', $data->usaf);
        }
        $this->smarty->assign('ancnewvisit', $ancnewvisit);
        $registeration = R::findAll('registeration', 'ancsheetid = ?', [$ancsheet->id]);
        foreach ($registeration as $data) {
            $data->awifept = R::load('awifep', $data->awifep);
            $data->place2t = R::load('place2', $data->place2);
            $data->origint = R::load('origin', $data->origin);
            $data->typet = R::load('type', $data->type);
        }
        $this->smarty->assign('registeration', $registeration);


        $gynasheet = R::findOne('gynasheet', 'patientid = ? ', [$patientid]);
        $this->smarty->assign('gynasheet', $gynasheet);
        $newvisitg = R::findAll('newvisitg', 'gynasheetid = ?', [$gynasheet->id]);
        foreach ($newvisitg as $data) {
            $data->newvisitcyclest = R::load('newvisitcycles', $data->newvisitcycles);
            $data->newvisitgcot = R::findAll('newvisitgco', "id in (0$data->newvisitgco)");
            $data->newvisitgdiagt = R::findAll('newvisitgdiag', "id in (0$data->newvisitgdiag)");
        }
        $this->smarty->assign('newvisitg', $newvisitg);


        $this->smarty->display('reports2/completesreports.html');
    }

    public function showprint() {
        //check login
        autho::checkautho($this->hosturl);
        //check authoize
        autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);

        //R::debug(TRUE);
        $fromdate = filter_input(INPUT_POST, 'fromdate');
        $todate = filter_input(INPUT_POST, 'todate');

        $fromdate = date("Y/m/d", strtotime($fromdate));
        $todate = date("Y/m/d", strtotime($todate));
        $today = date("Y/m/d");
        $allData = R::findAll('ancsheet', 'sheetedd >= ? and sheetedd <= ?', [$fromdate, $todate]);
        $this->smarty->assign('allData', $allData);
        foreach ($allData as $data) {
            $data->info = R::load('patients', $data->patientid);
            $diff = strtotime($today, 0) - strtotime($data->sheetlmp, 0);
            $tablenameappend->newvisitdate = $today;
            $ceil = ceil($diff / 604800);
            $data->week = abs($ceil);
        }
        $this->smarty->display('reports2/eddprint.html');
    }

    public function onesetup() {

        //check login
        autho::checkautho($this->hosturl);

        try {
            //delete all but with only default
            #1 clean roles only managment
            #2 clean menu only default
            #3 clean controls only default
            #4 users clean users
            #delete all menus
            $menus = R::findAll('awmenu', 'id > 15');
            #delete from awrolemenu
            foreach ($menus as $one) {
                //get propes
                $rolemenu = R::findAll('awrolemenu', 'menu_id = ?', [$one->id]);
                R::trashAll($rolemenu);
            }
            R::trashAll($menus);
            #delete controles
            $controlls = R::findAll('awcontroll', 'id > 4 and id < 18');
            foreach ($controlls as $one) {
                //get propes
                $inprop = R::findAll('awcontrollprop', 'awcontroll_id = ?', [$one->id]);
                #delete from awrolecontroleprop
                foreach ($inprop as $one) {
                    //get propes
                    $roleprop = R::findAll('awrolecontrollprop', 'controllprop_id = ?', [$one->id]);
                    R::trashAll($roleprop);
                }
                R::trashAll($inprop);
            }
            R::trashAll($controlls);
            #delete othre roles
            $roles = R::findAll('awrole', 'id > 1');
            R::trashAll($roles);
            #delete other users
            //$users = R::findAll('awusers', 'user_id > 1');
            //R::exec('Delete from awusers where user_id > 1');
            //R::trashAll($users);
            ############################
            //new one
            $menusx = R::findAll('awmenu', ' id = 4 or id = 5 or id = 7 or id = 8 or id = 10 or id = 11 or id = 13 or id = 14 or id = 15 ');
            #delete from awrolemenu
            foreach ($menusx as $one) {
                //get propes
                $rolemenu = R::findAll('awrolemenu', 'menu_id = ?', [$one->id]);
                R::trashAll($rolemenu);
            }
            R::trashAll($menusx);

            print_r('trash all , install is good');
        } catch (Exception $ex) {
            print_r('something go wrong  , try again');
        }
    }

    // at the end of class call
    public function __destruct() {
        //close connection
        R::close();
    }

}
