<?php

namespace app\base;

use Yii;
use yii\helpers\Url;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\grid\GridView;
use yii\data\ArrayDataProvider;

use kartik\widgets\DepDrop;

use app\base\Converter;
use rmrevin\yii\fontawesome\FA;

use app\models\Departments;
use app\models\Attandance;
use app\models\Contact;
use app\models\Counselor;
use app\models\Linkidlinkage;
use app\models\Ostmaster;
use app\models\Ostvisit;
use app\models\Hivlinkage;
use app\models\Hivtesting;
use app\models\Hivcounseling;
use app\models\Hcvlinkage;
use app\models\Hcvtesting;
use app\models\Registration;
use app\models\Surveymaster;
use app\models\Locationmaster;
use app\models\Labmaster;
use app\models\Labtestmapping;
use app\models\Labresultsmaster;
use app\models\Labresultmapping;
use app\models\Labsamplerequest;
use app\models\Mainmenu;
use app\models\Submenu;
use app\models\Menuprivileges;
use app\models\Trackingform;
use app\models\Mortalityform;
use app\models\Users;
use app\models\Arvmaster;
use app\models\Screening;
use app\models\Nursevisit;
use app\models\Cd4;
use app\models\Hivvisit;
use app\models\Sti;
use app\models\Tb;
use app\models\Demographics;
use app\models\Qualityoflife;
use app\models\Hivcontinuum;
use app\models\Preartbeliefs;
use app\models\Postartbeliefs;
use app\models\Depression;
use app\models\Substanceuse;
use app\models\Riskbehavior;
use app\models\Healthcareutilization;
use app\models\Reimbursementmaster;
use app\models\Clientidref;
use app\models\Locationmapping;
use app\models\Applicationsettings;
use app\models\Settings;
use app\models\Fieldmaster;
use app\models\Preartincenmaster;
use app\models\Artinitiationincentmaster;
use app\models\Motincenmaster;
use app\models\Artincenmaster;
use app\models\Visitmaster;
use app\models\Visitschedule;
use app\models\Artbook;
use app\models\Allincvoucherreport;
use app\models\Ipe;

$Settings = Settings::find()->orderBy(["stngs_id" => SORT_ASC])->one();
Yii::$app->params['SURVEY'] = $Settings->stngs_survey_code;
Yii::$app->params['SURVEY_FIXED'] = $Settings->stngs_survey_fixed;
if(isset($Settings->surveys)) {
	Yii::$app->params['SURVEY_TITLE'] = $Settings->surveys->sur_title;
} else {
	Yii::$app->params['SURVEY_TITLE'] = '';
}

if($Settings->stngs_survey_code != "") {
	$AppSettings = Applicationsettings::find()->where(["app_survey_id" => Yii::$app->params['SURVEY']])->orderBy(["app_stngs_id" => SORT_ASC])->one();
	Yii::$app->params['LOCATION'] = $AppSettings->app_location;
	Yii::$app->params['LOCATION_FIXED'] = $AppSettings->app_location_fixed;
	if(isset($AppSettings->locations)) {
	    Yii::$app->params['LOCATION_TITLE'] = $AppSettings->locations->loc_name;
	} else {
	    Yii::$app->params['LOCATION_TITLE'] = '';
	}
}
class Common
{
    // public $today = strtotime(date("Y/m/d"));

    public static function getSurvey()
    {
    	$Surveys = ArrayHelper::map(Surveymaster::find()->orderBy(["sur_id" => SORT_ASC])->all(), 'sur_code', 'sur_title');
    	return $Surveys;
    }

    public static function getAttendancePIDs()
    {
        $PIDs = ArrayHelper::map(Attandance::find()->where(["visit_date" => strtotime(date("Y/m/d"))])->andWhere(["IS", "out_interviewer", NULL])->orderBy(["pid" => SORT_ASC])->all(), 'pid', 'pid');
        return $PIDs;
    }
/*
    public static function getReimbursePIDs()
    {
        $SModel = Screening::find()->where(["mem_scrn_q25" => 1])->all();
        $RModel = Reimbursementmaster::find()->all();
    	$PIDs = ArrayHelper::map(Registration::find()->where(["<>", "corfrm_q13", 3])->andWhere(["IN", "corfrm_pid", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->andWhere(["NOT IN", "corfrm_pid", ArrayHelper::getColumn($RModel, "reimburse_master_part_id")])->orderBy(["corfrm_pid" => SORT_ASC])->all(), 'corfrm_pid', 'corfrm_pid');
    	return $PIDs;
    }
*/
    public static function getAttendanceInPIDs($sid = "", $loc = "")
    {
    	$attndncetable = Attandance::tableName();
        $AModel = Attandance::find()->where(["IS", "out_interviewer", NULL])->andWhere(["IS", "visit_out", NULL])->all();
        if($sid == "" && $loc == "")
           $model = Registration::find()->where(["NOT IN", "corfrm_pid", ArrayHelper::getColumn($AModel, "pid")])->andWhere(["<>", "corfrm_q13", 3])->orderBy(["corfrm_pid" => SORT_ASC])->all();
       else
    	   $model = Registration::find()->where(["corfrm_sid" => $sid, "corfrm_loc" => $loc])->andWhere(["NOT IN", "corfrm_pid", ArrayHelper::getColumn($AModel, "pid")])->andWhere(["<>", "corfrm_q13", 3])->orderBy(["corfrm_pid" => SORT_ASC])->all();
    	$PIDs = ArrayHelper::map($model, 'corfrm_pid', 'corfrm_pid');
    	return $PIDs;
    }

    public static function getAttendanceOutPIDs($sid = "", $loc = "")
    {
        if($sid == "" && $loc == "")
	        $PIDs = ArrayHelper::map(Attandance::find()->where(["IS", "out_interviewer", NULL])->andWhere(["IS", "visit_out", NULL])->orderBy(["pid" => SORT_ASC])->all(), 'pid', 'pid');
	    else
	        $PIDs = ArrayHelper::map(Attandance::find()->where(["sid" => $sid, "location" => $loc])->andWhere(["IS", "out_interviewer", NULL])->andWhere(["IS", "visit_out", NULL])->orderBy(["pid" => SORT_ASC])->all(), 'pid', 'pid');
        return $PIDs;
    }

    public static function getAttendanceOutChecks($pid)
    {
        $services = [];
        $date = strtotime(date("Y/m/d"));
        $AModel = Attandance::find()->where(["pid" => $pid])->andWhere(["IS", "out_interviewer", NULL])->andWhere(["IS", "visit_out", NULL])->orderBy(["pid" => SORT_ASC])->one();
        $date = $AModel->visit_date;
        $disp_date = Converter::toDisplay($date);
        $model = Counselor::find()->where(["counfrm_pid" => $pid, "record_date" => $date])->one();
        $index = 0;

        if($model !== null) {
            $CQ1 = explode(",", $model->counfrm_q1);
            $CQ2 = explode(",", $model->counfrm_q2);

            if(in_array("1", $CQ1)) {
                $HIVTModel = Hivtesting::find()->where(["hiv_test_part_id" => $pid, "record_date" => $date])->one();
                $services[$index]["service"] = "HIV Counseling and testing";
                $services[$index]["status"] = FA::icon('close text-danger');
                if($HIVTModel !== null) {
                    $services[$index]["status"] = FA::icon('check text-success');
                }

                $index++;
                $services[$index]["service"] = "HIV Post Test Counseling";
                $services[$index]["status"] = FA::icon('close text-danger');
                if($model->counfrm_q4 == 1) {
                    $services[$index]["status"] = FA::icon('check text-success');
                }
            }
            if(in_array("2", $CQ1)) {
                $index++;
                $HIVCModel = Hivcounseling::find()->where(["hiv_counseling_part_id" => $pid, "record_date" => $date])->one();
                $services[$index]["service"] = "HIV Motivational Counseling";
                $services[$index]["status"] = FA::icon('close text-danger');
                if($HIVCModel !== null) {
                    $services[$index]["status"] = FA::icon('check text-success');
                }
            }
            if(in_array("1", $CQ2)) {
                $index++;
                $HCVTModel = Hcvtesting::find()->where(["hcv_test_part_id" => $pid, "record_date" => $date])->one();
                $services[$index]["service"] = "HCV counseling and testing";
                $services[$index]["status"] = FA::icon('close text-danger');
                if($HCVTModel !== null) {
                    $services[$index]["status"] = FA::icon('check text-success');
                }

                $index++;
                $services[$index]["service"] = "HCV Post Test Counseling";
                $services[$index]["status"] = FA::icon('close text-danger');
                if($model->counfrm_q5 == 1) {
                    $services[$index]["status"] = FA::icon('check text-success');
                }
            }
        }

        $model = Nursevisit::find()->where(["nursefrm_pid" => $pid, "record_date" => $date])->one();
        if($model !== null) {
            $NQ1 = explode(",", $model->nursefrm_q1);
            $NQ2 = explode(",", $model->nursefrm_q2);

            if(in_array("1", $NQ1)) {
                $index++;
                $CD4Model = Cd4::find()->where(["cd4_test_part_id" => $pid, "record_date" => $date])->one();
                $services[$index]["service"] = "CD4";
                $services[$index]["status"] = FA::icon('close text-danger');
                if($CD4Model !== null) {
                    $services[$index]["status"] = FA::icon('check text-success');
                }

                $index++;
                $HIVVModel = Hivvisit::find()->where(["hiv_visit_part_id" => $pid, "record_date" => $date])->one();
                $services[$index]["service"] = "HIV Visit";
                $services[$index]["status"] = FA::icon('close text-danger');
                if($HIVVModel !== null) {
                    $services[$index]["status"] = FA::icon('check text-success');
                }
            }
            if(in_array("1", $NQ2)) {
                $index++;
                $OSTTModel = Ostvisitnew::find()->where(["ost_visit_part_id" => $pid, "record_date" => $date])->one();
                $services[$index]["service"] = "OST";
                $services[$index]["status"] = FA::icon('close text-danger');
                if($OSTTModel !== null) {
                    $services[$index]["status"] = FA::icon('check text-success');
                }
            }
            if(in_array("4", $NQ2)) {
                $index++;
                $STIModel = Sti::find()->where(["sti_test_part_id" => $pid, "record_date" => $date])->one();
                $services[$index]["service"] = "STI Testing";
                $services[$index]["status"] = FA::icon('close text-danger');
                if($STIModel !== null) {
                    $services[$index]["status"] = FA::icon('check text-success');
                }
            }
            if(in_array("5", $NQ2)) {
                $index++;
                $TBModel = Tb::find()->where(["tb_test_part_id" => $pid, "record_date" => $date])->one();
                $services[$index]["service"] = "TB Testing";
                $services[$index]["status"] = FA::icon('close text-danger');
                if($TBModel !== null) {
                    $services[$index]["status"] = FA::icon('check text-success');
                }
            }
        }

        $provider = new ArrayDataProvider([
            'allModels' => $services,
            // 'sort' => [
            //     'attributes' => ['service', 'status'],
            // ],
        ]);
        return GridView::widget([
            'dataProvider' => $provider,
            'layout' => '<div class="box"><div class="box-header with-border"><b>'.$disp_date.' Service Entry Status</b>{summary}</div><div class="box-body">{items}</div><div class="box-footer clearfix">{pager}</div></div>',
            'columns' => [
                ['class' => 'yii\grid\SerialColumn'],
                'service',
                [
                    'attribute' => 'status',
                    'format' => 'raw',
                ],
            ],
        ]);
    }

    public static function getScreeningPIDs($sid = "", $loc = "")
    {
        $PIDs = Registration::find()->where("corfrm_q12 = 1 OR corfrm_q12 = 9")->asArray()->all();
        $SModels = Screening::find()->where("mem_scrn_q25 = 1 OR mem_scrn_q26 = 2")->asArray()->all();
        $scrtable = Screening::tableName();
        // $model = Attandance::find()->where(["=", "record_date", strtotime(date("Y/m/d"))])->andWhere(["IS", "visit_out", NULL])->andWhere("pid NOT IN (SELECT mem_scrn_part_id FROM $scrtable WHERE (mem_scrn_q25 = 1) OR (mem_scrn_q25 = 2 AND mem_scrn_q26 = 2))")->andWhere(["IN", "pid", ArrayHelper::getColumn($PIDs, "corfrm_pid")])->orderBy(["pid" => SORT_ASC])->all();
        // $model = Attandance::find()->where(["=", "record_date", strtotime(date("Y/m/d"))])->andWhere(["IS", "visit_out", NULL])->andWhere("pid NOT IN (SELECT mem_scrn_part_id FROM $scrtable WHERE mem_scrn_q25 = 1 OR mem_scrn_q26 = 2)")->andWhere(["IN", "pid", ArrayHelper::getColumn($PIDs, "corfrm_pid")])->orderBy(["pid" => SORT_ASC])->all();
        // $model = Attandance::find()->where(["=", "record_date", strtotime(date("Y/m/d"))])->andWhere(["IS", "visit_out", NULL])->andWhere("pid NOT IN (SELECT mem_scrn_part_id FROM $scrtable WHERE (mem_scrn_q26 = 2 OR mem_scrn_q26 = 1))")->andWhere(["IN", "pid", ArrayHelper::getColumn($PIDs, "corfrm_pid")])->orderBy(["pid" => SORT_ASC])->all();
		
      
		$model = Attandance::find()->where(["=", "record_date", strtotime(date("Y/m/d"))])->andWhere(["<=", "visit", "1"])->andWhere(["IS", "visit_out", NULL])->andWhere(["NOT IN", "pid", ArrayHelper::getColumn($SModels, "mem_scrn_part_id")])->andWhere(["IN", "pid", ArrayHelper::getColumn($PIDs, "corfrm_pid")])->orderBy(["pid" => SORT_ASC])->all();
	
	/*  
		$model = Attandance::find()->where(["=", "record_date", strtotime(date("Y/m/d"))])->andWhere(["IS", "visit_out", NULL])->andWhere(["NOT IN", "pid", ArrayHelper::getColumn($SModels, "mem_scrn_part_id")])->andWhere(["IN", "pid", ArrayHelper::getColumn($PIDs, "corfrm_pid")])->orderBy(["pid" => SORT_ASC])->all();
	*/
        $PIDs = ArrayHelper::map($model, 'pid', 'pid');
        return $PIDs;
    }

    public static function getLabrequestPIDs($sid = "", $loc = "")
    {
    	$labreqtable = Labsamplerequest::tableName();
        // $model = Attandance::find()->where("out_interviewer IS NULL AND visit_out IS NULL")->andWhere("pid NOT IN (SELECT lab_smple_part_id FROM $labreqtable WHERE lab_smple_date = ".strtotime(date('Y/m/d')).")")->orderBy(["pid" => SORT_ASC])->all();
        if($sid == "" && $loc == "")
            $model = Attandance::find()->where(["visit_date" => strtotime(date("Y/m/d"))])->andWhere(["IS", "out_interviewer", NULL])->orderBy(["pid" => SORT_ASC])->all();
        else
            $model = Attandance::find()->where(["sid" => $sid, "location" => $loc])->andWhere(["visit_date" => strtotime(date("Y/m/d"))])->andWhere(["IS", "out_interviewer", NULL])->orderBy(["pid" => SORT_ASC])->all();
    	$PIDs = ArrayHelper::map($model, 'pid', 'pid');
    	return $PIDs;
    }

    public static function getHivLinkagePIDs($sid = "", $loc = "")
    {
        $hivlinktable = Hivlinkage::tableName();
        if($sid == "" && $loc == "")
            $model = Registration::find()->where("corfrm_q12 = 1 OR corfrm_q12 = 9")->andWhere("corfrm_pid NOT IN (SELECT hiv_linkage_part_id FROM $hivlinktable WHERE hiv_linkage_q8 IS NULL)")->orderBy(["corfrm_pid" => SORT_ASC])->all();
        else
            $model = Registration::find()->where(["corfrm_sid" => $sid, "corfrm_loc" => $loc])->andWhere("corfrm_q12 = 1 OR corfrm_q12 = 9")->andWhere("corfrm_pid NOT IN (SELECT hiv_linkage_part_id FROM $hivlinktable WHERE hiv_linkage_q8 IS NULL)")->orderBy(["corfrm_pid" => SORT_ASC])->all();
        $PIDs = ArrayHelper::map($model, 'corfrm_pid', 'corfrm_pid');
        return $PIDs;
    }

    public static function getHcvLinkagePIDs($sid = "", $loc = "")
    {
    	$hcvlinktable = Hcvlinkage::tableName();
        if($sid == "" && $loc == "")
            $model = Registration::find()->where("corfrm_q14 = 1 OR corfrm_q14 = 9")->andWhere("corfrm_pid NOT IN (SELECT hcv_linkage_part_id FROM $hcvlinktable)")->orderBy(["corfrm_pid" => SORT_ASC])->all();
        else
        	$model = Registration::find()->where(["corfrm_sid" => $sid, "corfrm_loc" => $loc])->andWhere("corfrm_q14 = 1 OR corfrm_q14 = 9")->andWhere("corfrm_pid NOT IN (SELECT hcv_linkage_part_id FROM $hcvlinktable)")->orderBy(["corfrm_pid" => SORT_ASC])->all();
    	$PIDs = ArrayHelper::map($model, 'corfrm_pid', 'corfrm_pid');
    	return $PIDs;
    }

    public static function getOstMasterPIDs()
    {
        $osttable = Ostmaster::tableName();
        $model = Registration::find()->where("corfrm_pid NOT IN (SELECT ost_master_part_id FROM $osttable WHERE ost_master_q3 IS NULL)")->orderBy(["corfrm_pid" => SORT_ASC])->all();
        $PIDs = ArrayHelper::map($model, 'corfrm_pid', 'corfrm_pid');
        return $PIDs;
    }

    public static function getlinkMasterPIDs($sid = "", $loc = "")
    {
    	$linktable = Linkidlinkage::tableName();
        if($sid == "" && $loc == "")
            $model = Registration::find()->where("corfrm_pid NOT IN (SELECT link_idlinkage_part_id FROM $linktable)")->orderBy(["corfrm_pid" => SORT_ASC])->all();
        else
        	$model = Registration::find()->where(["corfrm_sid" => $sid, "corfrm_loc" => $loc])->andWhere("corfrm_pid NOT IN (SELECT link_idlinkage_part_id FROM $linktable)")->orderBy(["corfrm_pid" => SORT_ASC])->all();
    	$PIDs = ArrayHelper::map($model, 'corfrm_pid', 'corfrm_pid');
    	return $PIDs;
    }

    public static function getOstVisitPIDs($sid = "", $loc = "")
    {
    	$ostmtable = Ostmaster::tableName();
    	$attntable = Attandance::tableName();
		$ostvtable= Ostvisit::tableName();
    	$date = strtotime(date("Y/m/d"));
		/*
		$model = Ostvisitnew::find()->where("ost_visit_part_id IN (SELECT pid FROM $attntable WHERE out_interviewer IS NULL and visit_date between $date AND ".date('U').")")->andWhere("ost_visit_q7 = $date")->andWhere("ost_visit_part_id  IN (SELECT ost_master_part_id FROM $ostmtable WHERE ost_master_q3 IS NULL)")
		*/
    	$model = Attandance::find()->where(["between", "visit_date", strtotime(date("Y/m/d")), date('U')])->andWhere("out_interviewer IS NULL AND pid NOT IN (SELECT ost_visit_part_id FROM $ostvtable WHERE ost_visit_q7 = $date) AND pid IN (SELECT ost_master_part_id FROM $ostmtable WHERE ost_master_q3 IS NULL)")->orderBy(["pid" => SORT_ASC])->all();
		
    	$PIDs = ArrayHelper::map($model, 'pid', 'pid');
    	return $PIDs;
    }

    public static function getCounselorPIDs($sid = "", $loc = "")
    {
        $today = strtotime(date("Y/m/d"));
        $CModel = Counselor::find()->where(["record_date" => $today])->all();
        if($sid == "" && $loc == "")
           $model = Attandance::find()->where(["visit_date" => $today])->andWhere(["IS", "out_interviewer", NULL])->andWhere(["IS", "visit_out", NULL])->andWhere(["NOT IN", "pid", ArrayHelper::getColumn($CModel, "counfrm_pid")])->orderBy(["pid" => SORT_ASC])->all();
       else
           $model = Attandance::find()->where(["visit_date" => $today])->andWhere(["IS", "out_interviewer", NULL])->andWhere(["IS", "visit_out", NULL])->andWhere(["sid" => $sid, "location" => $loc])->andWhere(["NOT IN", "pid", ArrayHelper::getColumn($CModel, "counfrm_pid")])->orderBy(["pid" => SORT_ASC])->all();
        $PIDs = ArrayHelper::map($model, 'pid', 'pid');
        return $PIDs;
    }

    public static function getCounselorServicePIDs($type, $sid = "", $loc = "")
    {
        $date = strtotime(date("Y/m/d"));
        $query = Counselor::find()->where(["between", "create_time", $date, date('U')]);
        if($type == "hivtest") {
            $hivtesttable = Hivtesting::tableName();
            $query->andWhere(["like", "counfrm_q1", "1"]);
            $query->andWhere("counfrm_pid NOT IN (SELECT hiv_test_part_id FROM $hivtesttable WHERE hiv_test_q1 between $date AND ".date('U').")");
        } elseif($type == "hivcaretest") {
            $hivcounstable = Hivcounseling::tableName();
            $query->andWhere(["like", "counfrm_q1", "2"]);
            $query->andWhere("counfrm_pid NOT IN (SELECT hiv_counseling_part_id FROM $hivcounstable WHERE create_time between $date AND ".date('U').")");
        } elseif($type == "hcvtest") {
            $hcvtesttable = Hcvtesting::tableName();
            $query->andWhere(["like", "counfrm_q2", "1"]);
            $query->andWhere("counfrm_pid NOT IN (SELECT hcv_test_part_id FROM $hcvtesttable WHERE hcv_test_q1 between $date AND ".date('U').")");
        }
        if($sid != "" && $loc != "")
            $query->andWhere(["counfrm_sid" => $sid, "counfrm_loc" => $loc]);

        $query->orderBy(["counfrm_pid" => SORT_ASC]);
        $model = $query->all();
        $PIDs = ArrayHelper::map($model, 'counfrm_pid', 'counfrm_pid');
        return $PIDs;
    }

    public static function getNursePIDs($sid = "", $loc = "")
    {
        $date = strtotime(date("Y/m/d"));
        $nursetable = Nursevisit::tableName();
    	$nurse = Nursevisit::find()->where(["record_date" => $date])->all();
        // $model = Attandance::find()->where(["record_date" => $date])->andWhere("out_interviewer IS NULL AND pid NOT IN (SELECT nursefrm_pid FROM $nursetable WHERE record_date = $date)")->orderBy(["pid" => SORT_ASC])->all();
        if($sid == "" && $loc == "")
            $model = Attandance::find()->where(["record_date" => $date])->andWhere(["IS", "out_interviewer", NULL])->andWhere(["NOT IN", "pid", ArrayHelper::getColumn($nurse, "nursefrm_pid")])->orderBy(["pid" => SORT_ASC])->all();
        else
        	$model = Attandance::find()->where(["record_date" => $date])->andWhere(["sid" => $sid, "location" => $loc])->andWhere(["IS", "out_interviewer", NULL])->andWhere(["NOT IN", "pid", ArrayHelper::getColumn($nurse, "nursefrm_pid")])->orderBy(["pid" => SORT_ASC])->all();
    	$PIDs = ArrayHelper::map($model, 'pid', 'pid');
    	return $PIDs;
    }

    public static function getNurseServicePIDs($type, $sid = "", $loc = "")
    {
    	$date = strtotime(date("Y/m/d"));
    	$query = Nursevisit::find()->where(["between", "create_time", $date, date('U')]);
    	if($type == "cd4test") {
            $query->andWhere(["like", "nursefrm_q1", "1"]);
            $query->andWhere(["IN", "nursefrm_pid", ArrayHelper::getColumn(Registration::find()->where("corfrm_q12 = 1 OR corfrm_q12 = 9")->asArray()->all(), "corfrm_pid")]);
	    	$query->andWhere(["NOT IN", "nursefrm_pid", ArrayHelper::getColumn(Cd4::find()->where(["record_date" => $date])->all(), "cd4_test_part_id")]);
	    } elseif($type == "hivvisit") {
            $query->andWhere(["like", "nursefrm_q1", "1"]);
            $query->andWhere(["IN", "nursefrm_pid", ArrayHelper::getColumn(Registration::find()->where("corfrm_q12 = 1 OR corfrm_q12 = 9")->asArray()->all(), "corfrm_pid")]);
            $query->andWhere(["NOT IN", "nursefrm_pid", ArrayHelper::getColumn(Hivvisit::find()->where(["record_date" => $date])->all(), "hiv_visit_part_id")]);
        } elseif($type == "osttest") {
            $query->andWhere(["like", "nursefrm_q2", "1"]);
            $query->andWhere(["NOT IN", "nursefrm_pid", ArrayHelper::getColumn(Ostvisit::find()->where(["record_date" => $date])->all(), "ost_visit_part_id")]);
        } elseif($type == "stitest") {
            $query->andWhere(["like", "nursefrm_q2", "4"]);
            $query->andWhere(["NOT IN", "nursefrm_pid", ArrayHelper::getColumn(Sti::find()->where(["record_date" => $date])->all(), "sti_test_part_id")]);
        } elseif($type == "tbtest") {
            $query->andWhere(["like", "nursefrm_q2", "5"]);
            $query->andWhere(["NOT IN", "nursefrm_pid", ArrayHelper::getColumn(Tb::find()->where(["record_date" => $date])->all(), "tb_test_part_id")]);
	    }
        if($sid != "" && $loc != "")
            $query->andWhere(["nursefrm_sid" => $sid, "nursefrm_loc" => $loc]);

    	$query->orderBy(["nursefrm_pid" => SORT_ASC]);
    	$model = $query->all();
    	$PIDs = ArrayHelper::map($model, 'nursefrm_pid', 'nursefrm_pid');
    	return $PIDs;
    }

    public static function getRegistrationPIDs()
    {
		$PIDs = ArrayHelper::map(Registration::find()->orderBy(["corfrm_pid" => SORT_ASC])->all(), 'corfrm_pid', 'corfrm_pid');
		return $PIDs;
    }

    public static function getContactPIDs($sid = "", $loc = "")
    {
        $Model = Contact::find()->all();
        // $model = Registration::find()->where(["NOT IN", "corfrm_pid", ArrayHelper::getColumn($Model, "contact_part_id")])->orderBy(["corfrm_pid" => SORT_ASC])->all();
        // $PIDs = ArrayHelper::map($model, 'corfrm_pid', 'corfrm_pid');

        $model = Attandance::find()->where(["visit_date" => strtotime(date("Y/m/d"))])->andWhere(["IS", "out_interviewer", NULL])->andWhere(["NOT IN", "pid", ArrayHelper::getColumn($Model, "contact_part_id")])->orderBy(["pid" => SORT_ASC])->all();
        $PIDs = ArrayHelper::map($model, 'pid', 'pid');
        return $PIDs;
    }

    public static function getTrackingPIDs($sid = "", $loc = "")
    {
        $date = strtotime(date("Y/m/d"));
        $Model = Trackingform::find()->where(["record_date" => $date])->all();
        if($sid == "" && $loc == "")
           $model = Registration::find()->where(["<>", "corfrm_q13", 3])->andWhere(["NOT IN", "corfrm_pid", ArrayHelper::getColumn($Model, "track_form_part_id")])->orderBy(["corfrm_pid" => SORT_ASC])->all();
        else
           $model = Registration::find()->where(["corfrm_sid" => $sid, "corfrm_loc" => $loc])->andWhere(["<>", "corfrm_q13", 3])->andWhere(["NOT IN", "corfrm_pid", ArrayHelper::getColumn($Model, "track_form_part_id")])->orderBy(["corfrm_pid" => SORT_ASC])->all();
        $PIDs = ArrayHelper::map($model, 'corfrm_pid', 'corfrm_pid');
        return $PIDs;
    }

    public static function getMortalityPIDs($sid = "", $loc = "")
    {
        $Model = Mortalityform::find()->all();
        // $model = Registration::find()->where(["<>", "corfrm_q13", 3])->andWhere(["NOT IN", "corfrm_pid", ArrayHelper::getColumn($Model, "mortality_form_part_id")])->orderBy(["corfrm_pid" => SORT_ASC])->all();
        if($sid == "" && $loc == "")
            $model = Registration::find()->where(["<>", "corfrm_q13", 3])->andWhere(["NOT IN", "corfrm_pid", ArrayHelper::getColumn($Model, "mortality_form_part_id")])->orderBy(["corfrm_pid" => SORT_ASC])->all();
        else
            $model = Registration::find()->where(["corfrm_sid" => $sid, "corfrm_loc" => $loc])->andWhere(["<>", "corfrm_q13", 3])->andWhere(["NOT IN", "corfrm_pid", ArrayHelper::getColumn($Model, "mortality_form_part_id")])->orderBy(["corfrm_pid" => SORT_ASC])->all();
        $PIDs = ArrayHelper::map($model, 'corfrm_pid', 'corfrm_pid');
		return $PIDs;
    }

    public static function getLocations()
    {
        $Locations = ArrayHelper::map(Locationmaster::find()->orderBy(["loc_name" => SORT_ASC])->all(), 'loc_code', 'loc_name');
        return $Locations;
    }

	public static function getEnrollPIDs($type, $sid = "", $loc = "")
	  
	{
	    $scrtable = Screening::tableName();	
    	$dgtable = Demographics::tableName();
		$qltable = Qualityoflife::tableName();
		$hivcontable = Hivcontinuum::tableName();
		$prabtable = Preartbeliefs::tableName();
		$artbtable = Postartbeliefs::tableName();
		$sutable = Substanceuse::tableName();
		$rbtable = Riskbehavior::tableName();
        $phqtable = Depression::tableName();
		$hcutable = Healthcareutilization::tableName();
		$hivlink=Hivlinkage::tableName();
		$attntable=Attandance::tableName();	
		
	 if($type == "dg"){		
		$model = Attandance::find()->where(["=", "record_date", strtotime(date("Y/m/d"))])->andWhere(["=", "visit", 1])->andWhere(["IS", "visit_out", NULL])->andWhere("pid IN (SELECT mem_scrn_part_id FROM $scrtable WHERE mem_scrn_q25 = 1)")->andWhere("pid Not IN (SELECT demographics_part_id FROM $dgtable where $attntable.pid=demographics_part_id and $attntable.visit=visit_code)")->orderBy(["pid" => SORT_ASC])->all();             
		$PIDs = ArrayHelper::map($model, 'pid', 'pid');
		return $PIDs;
     }
	 if($type == "dgfup"){		
		$model = Attandance::find()->where(["=", "record_date", strtotime(date("Y/m/d"))])->andWhere(["<", "visit", 1])->andWhere(["IS", "visit_out", NULL])->andWhere("pid IN (SELECT mem_scrn_part_id FROM $scrtable WHERE mem_scrn_q25 = 1)")->andWhere("pid Not IN (SELECT demographics_part_id FROM $dgtable where $attntable.pid=demographics_part_id and $attntable.visit=visit_code)")->orderBy(["pid" => SORT_ASC])->all();             
		$PIDs = ArrayHelper::map($model, 'pid', 'pid');
		return $PIDs;
     }
	 elseif($type == "ql"){        
        $model = Attandance::find()->where(["=", "record_date", strtotime(date("Y/m/d"))])->andWhere(["<=", "visit", 1])->andWhere(["IS", "visit_out", NULL])->andWhere("pid IN (SELECT mem_scrn_part_id FROM $scrtable WHERE mem_scrn_q25 = 1)")->andWhere("pid IN (SELECT demographics_part_id FROM $dgtable where $attntable.pid=demographics_part_id and $attntable.visit=visit_code)")->andWhere("pid Not IN (SELECT quality_of_life_part_id FROM $qltable where $attntable.pid=quality_of_life_part_id and $attntable.visit=visit_code)")->orderBy(["pid" => SORT_ASC])->all();
		$PIDs = ArrayHelper::map($model, 'pid', 'pid');  
        return $PIDs;		
     }
	 elseif($type == "hivcon"){        
        $model = Attandance::find()->where(["=", "record_date", strtotime(date("Y/m/d"))])->andWhere(["<=", "visit", 1])->andWhere(["IS", "visit_out", NULL])->andWhere("pid IN (SELECT mem_scrn_part_id FROM $scrtable WHERE mem_scrn_q25 = 1)")->andWhere("pid IN (SELECT hiv_linkage_part_id FROM $hivlink )")->andWhere("pid IN (SELECT demographics_part_id FROM $dgtable where $attntable.pid=demographics_part_id and $attntable.visit=visit_code)")->andWhere("pid IN (SELECT quality_of_life_part_id FROM $qltable where $attntable.pid=quality_of_life_part_id and $attntable.visit=visit_code)")->andWhere("pid Not IN (SELECT hiv_continuum_part_id FROM $hivcontable where $attntable.pid=hiv_continuum_part_id and $attntable.visit=visit_code)")->orderBy(["pid" => SORT_ASC])->all();
		$PIDs = ArrayHelper::map($model, 'pid', 'pid');  
        return $PIDs;		
	}	 
	 elseif($type == "prab"){	        
        $PIDs = Hivlinkage::find()->where("hiv_linkage_q5 ='' or hiv_linkage_q5 is NULL")->asArray()->all();
		$model = Attandance::find()->where(["=", "record_date", strtotime(date("Y/m/d"))])->andWhere(["<=", "visit", 1])->andWhere(["IS", "visit_out", NULL])->andWhere("pid IN (SELECT mem_scrn_part_id FROM $scrtable WHERE mem_scrn_q25 = 1)")->andWhere(["IN", "pid", ArrayHelper::getColumn($PIDs, "hiv_linkage_part_id")])->andWhere("pid IN (SELECT demographics_part_id FROM $dgtable where $attntable.pid=demographics_part_id and $attntable.visit=visit_code)")->andWhere("pid IN (SELECT quality_of_life_part_id FROM $qltable where $attntable.pid=quality_of_life_part_id and $attntable.visit=visit_code)")->andWhere("pid IN (SELECT hiv_continuum_part_id FROM $hivcontable where $attntable.pid=hiv_continuum_part_id and $attntable.visit=visit_code)")->andWhere("pid Not IN (SELECT pre_art_beliefs_part_id FROM $prabtable where $attntable.pid=pre_art_beliefs_part_id and $attntable.visit=visit_code)")->orderBy(["pid" => SORT_ASC])->all();
		$PIDs = ArrayHelper::map($model, 'pid', 'pid');    
        return $PIDs;			
     }
	elseif($type == "artb"){		
        $PIDs = Hivlinkage::find()->where("hiv_linkage_q5 !='' And hiv_linkage_q5 is not NULL")->asArray()->all();
        $model = Attandance::find()->where(["=", "record_date", strtotime(date("Y/m/d"))])->andWhere(["<=", "visit", 1])->andWhere(["IS", "visit_out", NULL])->andWhere("pid IN (SELECT mem_scrn_part_id FROM $scrtable WHERE mem_scrn_q25 = 1)")->andWhere(["IN", "pid", ArrayHelper::getColumn($PIDs, "hiv_linkage_part_id")])->andWhere("pid IN (SELECT demographics_part_id FROM $dgtable where $attntable.pid=demographics_part_id and $attntable.visit=visit_code)")->andWhere("pid IN (SELECT quality_of_life_part_id FROM $qltable where $attntable.pid=quality_of_life_part_id and $attntable.visit=visit_code)")->andWhere("pid IN (SELECT hiv_continuum_part_id FROM $hivcontable where $attntable.pid=hiv_continuum_part_id and $attntable.visit=visit_code)")->andWhere("pid Not IN (SELECT post_art_beliefs_part_id FROM $artbtable where $attntable.pid=post_art_beliefs_part_id and $attntable.visit=visit_code )")->orderBy(["pid" => SORT_ASC])->all();
		$PIDs = ArrayHelper::map($model, 'pid', 'pid');    
        return $PIDs;		
     }	  
	elseif($type == "su"){		      
        $model = Attandance::find()->where(["=", "record_date", strtotime(date("Y/m/d"))])->andWhere(["=", "visit", 1])->andWhere(["IS", "visit_out", NULL])->andWhere("pid IN (SELECT mem_scrn_part_id FROM $scrtable WHERE mem_scrn_q25 = 1)")->andWhere("pid IN (SELECT demographics_part_id FROM $dgtable where $attntable.pid=demographics_part_id and $attntable.visit=visit_code)")->andWhere("pid IN (SELECT quality_of_life_part_id FROM $qltable where $attntable.pid=quality_of_life_part_id and $attntable.visit=visit_code)")->andWhere("pid IN (SELECT hiv_continuum_part_id FROM $hivcontable where $attntable.pid=hiv_continuum_part_id and $attntable.visit=visit_code)")->andWhere("pid Not IN (SELECT substance_use_part_id FROM $sutable where $attntable.pid=substance_use_part_id and $attntable.visit=visit_code)")->orderBy(["pid" => SORT_ASC])->all();
		$PIDs = ArrayHelper::map($model, 'pid', 'pid');  
        return $PIDs;		
	}	
    elseif($type == "sufup"){		      
        $model = Attandance::find()->where(["=", "record_date", strtotime(date("Y/m/d"))])->andWhere(["<", "visit", 1])->andWhere(["IS", "visit_out", NULL])->andWhere("pid IN (SELECT mem_scrn_part_id FROM $scrtable WHERE mem_scrn_q25 = 1)")->andWhere("pid IN (SELECT demographics_part_id FROM $dgtable where $attntable.pid=demographics_part_id and $attntable.visit=visit_code)")->andWhere("pid IN (SELECT quality_of_life_part_id FROM $qltable where $attntable.pid=quality_of_life_part_id and $attntable.visit=visit_code)")->andWhere("pid IN (SELECT hiv_continuum_part_id FROM $hivcontable where $attntable.pid=hiv_continuum_part_id and $attntable.visit=visit_code)")->andWhere("pid Not IN (SELECT substance_use_part_id FROM $sutable where $attntable.pid=substance_use_part_id and $attntable.visit=visit_code)")->orderBy(["pid" => SORT_ASC])->all();
		$PIDs = ArrayHelper::map($model, 'pid', 'pid');  
        return $PIDs;		
	}	 	 	
	elseif($type == "rb"){
        $model = Attandance::find()->where(["=", "record_date", strtotime(date("Y/m/d"))])->andWhere(["=", "visit", 1])->andWhere(["IS", "visit_out", NULL])->andWhere("pid IN (SELECT mem_scrn_part_id FROM $scrtable WHERE mem_scrn_q25 = 1)")->andWhere("pid IN (SELECT demographics_part_id FROM $dgtable where $attntable.pid=demographics_part_id and $attntable.visit=visit_code)")->andWhere("pid IN (SELECT quality_of_life_part_id FROM $qltable where $attntable.pid=quality_of_life_part_id and $attntable.visit=visit_code)")->andWhere("pid IN (SELECT hiv_continuum_part_id FROM $hivcontable where $attntable.pid=hiv_continuum_part_id and $attntable.visit=visit_code)")->andWhere("pid IN (SELECT substance_use_part_id FROM $sutable where $attntable.pid=substance_use_part_id and $attntable.visit=visit_code )")->andWhere("pid Not IN (SELECT risk_behavior_part_id FROM $rbtable where $attntable.pid=risk_behavior_part_id and $attntable.visit=visit_code)")->orderBy(["pid" => SORT_ASC])->all();
		$PIDs = ArrayHelper::map($model, 'pid', 'pid');  
        return $PIDs;		
	}
  elseif($type == "rbfup"){
        $model = Attandance::find()->where(["=", "record_date", strtotime(date("Y/m/d"))])->andWhere(["<", "visit", 1])->andWhere(["IS", "visit_out", NULL])->andWhere("pid IN (SELECT mem_scrn_part_id FROM $scrtable WHERE mem_scrn_q25 = 1)")->andWhere("pid IN (SELECT demographics_part_id FROM $dgtable where $attntable.pid=demographics_part_id and $attntable.visit=visit_code)")->andWhere("pid IN (SELECT quality_of_life_part_id FROM $qltable where $attntable.pid=quality_of_life_part_id and $attntable.visit=visit_code)")->andWhere("pid IN (SELECT hiv_continuum_part_id FROM $hivcontable where $attntable.pid=hiv_continuum_part_id and $attntable.visit=visit_code)")->andWhere("pid IN (SELECT substance_use_part_id FROM $sutable where $attntable.pid=substance_use_part_id and $attntable.visit=visit_code )")->andWhere("pid Not IN (SELECT risk_behavior_part_id FROM $rbtable where $attntable.pid=risk_behavior_part_id and $attntable.visit=visit_code)")->orderBy(["pid" => SORT_ASC])->all();
		$PIDs = ArrayHelper::map($model, 'pid', 'pid');  
        return $PIDs;		
	}	 		
	 elseif($type == "phq"){		
        $model = Attandance::find()->where(["=", "record_date", strtotime(date("Y/m/d"))])->andWhere(["<=", "visit", 1])->andWhere(["IS", "visit_out", NULL])->andWhere("pid IN (SELECT mem_scrn_part_id FROM $scrtable WHERE mem_scrn_q25 = 1)")->andWhere("pid IN (SELECT demographics_part_id FROM $dgtable where $attntable.pid=demographics_part_id and $attntable.visit=visit_code)")->andWhere("pid IN (SELECT quality_of_life_part_id FROM $qltable where $attntable.pid=quality_of_life_part_id and $attntable.visit=visit_code)")->andWhere("pid IN (SELECT hiv_continuum_part_id FROM $hivcontable where $attntable.pid=hiv_continuum_part_id and $attntable.visit=visit_code)")->andWhere("pid IN (SELECT substance_use_part_id FROM $sutable where $attntable.pid=substance_use_part_id and $attntable.visit=visit_code)")->andWhere("pid IN (SELECT risk_behavior_part_id FROM $rbtable where $attntable.pid=risk_behavior_part_id and $attntable.visit=visit_code )")->andWhere("pid Not IN (SELECT depression_part_id FROM $phqtable where $attntable.pid=depression_part_id and $attntable.visit=visit_code)")->orderBy(["pid" => SORT_ASC])->all();
		$PIDs = ArrayHelper::map($model, 'pid', 'pid');  
        return $PIDs;		
	}	 	
	elseif($type == "hcu"){	
        $model = Attandance::find()->where(["=", "record_date", strtotime(date("Y/m/d"))])->andWhere(["<=", "visit", 1])->andWhere(["IS", "visit_out", NULL])->andWhere("pid IN (SELECT mem_scrn_part_id FROM $scrtable WHERE mem_scrn_q25 = 1)")->andWhere("pid IN (SELECT demographics_part_id FROM $dgtable where $attntable.pid=demographics_part_id and $attntable.visit=visit_code)")->andWhere("pid IN (SELECT quality_of_life_part_id FROM $qltable where $attntable.pid=quality_of_life_part_id and $attntable.visit=visit_code)")->andWhere("pid IN (SELECT hiv_continuum_part_id FROM $hivcontable where $attntable.pid=hiv_continuum_part_id and $attntable.visit=visit_code)")->andWhere("pid IN (SELECT substance_use_part_id FROM $sutable where $attntable.pid=substance_use_part_id and $attntable.visit=visit_code)")->andWhere("pid IN (SELECT risk_behavior_part_id FROM $rbtable where $attntable.pid=risk_behavior_part_id and $attntable.visit=visit_code )")->andWhere("pid IN (SELECT depression_part_id FROM $phqtable where $attntable.pid=depression_part_id and $attntable.visit=visit_code )")->andWhere("pid Not IN (SELECT health_care_utilization_part_id FROM $hcutable where $attntable.pid=health_care_utilization_part_id and $attntable.visit=visit_code)")->orderBy(["pid" => SORT_ASC])->all();
		$PIDs = ArrayHelper::map($model, 'pid', 'pid');  
        return $PIDs;		
	}	 	
	}
	
/*

	public static function getEnrollPIDs($type, $sid = "", $loc = "")
	  
	{
	    $scrtable = Screening::tableName();	
    	$dgtable = Demographics::tableName();
		$qltable = Qualityoflife::tableName();
		$hivcontable = Hivcontinuum::tableName();
		$prabtable = Preartbeliefs::tableName();
		$artbtable = Postartbeliefs::tableName();
		$sutable = Substanceuse::tableName();
		$rbtable = Riskbehavior::tableName();
        $phqtable = Depression::tableName();
		$hcutable = Healthcareutilization::tableName();
		$hivlink=Hivlinkage::tableName();
		
	 if($type == "dg"){		
		$model = Attandance::find()->where(["=", "record_date", strtotime(date("Y/m/d"))])->andWhere(["IS", "visit_out", NULL])->andWhere("pid IN (SELECT mem_scrn_part_id FROM $scrtable WHERE mem_scrn_q25 = 1)")->andWhere("pid Not IN (SELECT demographics_part_id FROM $dgtable)")->orderBy(["pid" => SORT_ASC])->all();             
		$PIDs = ArrayHelper::map($model, 'pid', 'pid');
		return $PIDs;
     }
	 elseif($type == "ql"){        
        $model = Attandance::find()->where(["=", "record_date", strtotime(date("Y/m/d"))])->andWhere(["IS", "visit_out", NULL])->andWhere("pid IN (SELECT mem_scrn_part_id FROM $scrtable WHERE mem_scrn_q25 = 1)")->andWhere("pid IN (SELECT demographics_part_id FROM $dgtable)")->andWhere("pid Not IN (SELECT quality_of_life_part_id FROM $qltable)")->orderBy(["pid" => SORT_ASC])->all();
		$PIDs = ArrayHelper::map($model, 'pid', 'pid');  
        return $PIDs;		
     }
	 elseif($type == "hivcon"){        
        $model = Attandance::find()->where(["=", "record_date", strtotime(date("Y/m/d"))])->andWhere(["IS", "visit_out", NULL])->andWhere("pid IN (SELECT mem_scrn_part_id FROM $scrtable WHERE mem_scrn_q25 = 1)")->andWhere("pid IN (SELECT hiv_linkage_part_id FROM $hivlink )")->andWhere("pid IN (SELECT demographics_part_id FROM $dgtable)")->andWhere("pid IN (SELECT quality_of_life_part_id FROM $qltable)")->andWhere("pid Not IN (SELECT hiv_continuum_part_id FROM $hivcontable)")->orderBy(["pid" => SORT_ASC])->all();
		$PIDs = ArrayHelper::map($model, 'pid', 'pid');  
        return $PIDs;		
	}	 
	 elseif($type == "prab"){	        
        $PIDs = Hivlinkage::find()->where("hiv_linkage_q5 ='' or hiv_linkage_q5 is NULL")->asArray()->all();
		$model = Attandance::find()->where(["=", "record_date", strtotime(date("Y/m/d"))])->andWhere(["IS", "visit_out", NULL])->andWhere("pid IN (SELECT mem_scrn_part_id FROM $scrtable WHERE mem_scrn_q25 = 1)")->andWhere(["IN", "pid", ArrayHelper::getColumn($PIDs, "hiv_linkage_part_id")])->andWhere("pid IN (SELECT demographics_part_id FROM $dgtable)")->andWhere("pid IN (SELECT quality_of_life_part_id FROM $qltable)")->andWhere("pid IN (SELECT hiv_continuum_part_id FROM $hivcontable)")->andWhere("pid Not IN (SELECT pre_art_beliefs_part_id FROM $prabtable)")->orderBy(["pid" => SORT_ASC])->all();
		$PIDs = ArrayHelper::map($model, 'pid', 'pid');    
        return $PIDs;			
     }
	elseif($type == "artb"){		
        $PIDs = Hivlinkage::find()->where("hiv_linkage_q5 !='' And hiv_linkage_q5 is not NULL")->asArray()->all();
        $model = Attandance::find()->where(["=", "record_date", strtotime(date("Y/m/d"))])->andWhere(["IS", "visit_out", NULL])->andWhere("pid IN (SELECT mem_scrn_part_id FROM $scrtable WHERE mem_scrn_q25 = 1)")->andWhere(["IN", "pid", ArrayHelper::getColumn($PIDs, "hiv_linkage_part_id")])->andWhere("pid IN (SELECT demographics_part_id FROM $dgtable)")->andWhere("pid IN (SELECT quality_of_life_part_id FROM $qltable)")->andWhere("pid IN (SELECT hiv_continuum_part_id FROM $hivcontable)")->andWhere("pid Not IN (SELECT post_art_beliefs_part_id FROM $artbtable)")->orderBy(["pid" => SORT_ASC])->all();
		$PIDs = ArrayHelper::map($model, 'pid', 'pid');    
        return $PIDs;		
     }	  
	elseif($type == "su"){		      
        $model = Attandance::find()->where(["=", "record_date", strtotime(date("Y/m/d"))])->andWhere(["IS", "visit_out", NULL])->andWhere("pid IN (SELECT mem_scrn_part_id FROM $scrtable WHERE mem_scrn_q25 = 1)")->andWhere("pid IN (SELECT demographics_part_id FROM $dgtable)")->andWhere("pid IN (SELECT quality_of_life_part_id FROM $qltable)")->andWhere("pid IN (SELECT hiv_continuum_part_id FROM $hivcontable)")->andWhere("pid Not IN (SELECT substance_use_part_id FROM $sutable)")->orderBy(["pid" => SORT_ASC])->all();
		$PIDs = ArrayHelper::map($model, 'pid', 'pid');  
        return $PIDs;		
	}	 	
	elseif($type == "rb"){
        $model = Attandance::find()->where(["=", "record_date", strtotime(date("Y/m/d"))])->andWhere(["IS", "visit_out", NULL])->andWhere("pid IN (SELECT mem_scrn_part_id FROM $scrtable WHERE mem_scrn_q25 = 1)")->andWhere("pid IN (SELECT demographics_part_id FROM $dgtable)")->andWhere("pid IN (SELECT quality_of_life_part_id FROM $qltable)")->andWhere("pid IN (SELECT hiv_continuum_part_id FROM $hivcontable)")->andWhere("pid IN (SELECT substance_use_part_id FROM $sutable)")->andWhere("pid Not IN (SELECT risk_behavior_part_id FROM $rbtable)")->orderBy(["pid" => SORT_ASC])->all();
		$PIDs = ArrayHelper::map($model, 'pid', 'pid');  
        return $PIDs;		
	}	 	
	 elseif($type == "phq"){		
        $model = Attandance::find()->where(["=", "record_date", strtotime(date("Y/m/d"))])->andWhere(["IS", "visit_out", NULL])->andWhere("pid IN (SELECT mem_scrn_part_id FROM $scrtable WHERE mem_scrn_q25 = 1)")->andWhere("pid IN (SELECT demographics_part_id FROM $dgtable)")->andWhere("pid IN (SELECT quality_of_life_part_id FROM $qltable)")->andWhere("pid IN (SELECT hiv_continuum_part_id FROM $hivcontable)")->andWhere("pid IN (SELECT substance_use_part_id FROM $sutable)")->andWhere("pid IN (SELECT risk_behavior_part_id FROM $rbtable)")->andWhere("pid Not IN (SELECT depression_part_id FROM $phqtable)")->orderBy(["pid" => SORT_ASC])->all();
		$PIDs = ArrayHelper::map($model, 'pid', 'pid');  
        return $PIDs;		
	}	 	
	elseif($type == "hcu"){	
        $model = Attandance::find()->where(["=", "record_date", strtotime(date("Y/m/d"))])->andWhere(["IS", "visit_out", NULL])->andWhere("pid IN (SELECT mem_scrn_part_id FROM $scrtable WHERE mem_scrn_q25 = 1)")->andWhere("pid IN (SELECT demographics_part_id FROM $dgtable)")->andWhere("pid IN (SELECT quality_of_life_part_id FROM $qltable)")->andWhere("pid IN (SELECT hiv_continuum_part_id FROM $hivcontable)")->andWhere("pid IN (SELECT substance_use_part_id FROM $sutable)")->andWhere("pid IN (SELECT risk_behavior_part_id FROM $rbtable)")->andWhere("pid IN (SELECT depression_part_id FROM $phqtable)")->andWhere("pid Not IN (SELECT health_care_utilization_part_id FROM $hcutable)")->orderBy(["pid" => SORT_ASC])->all();
		$PIDs = ArrayHelper::map($model, 'pid', 'pid');  
        return $PIDs;		
	}	 	
	}
*/
    public static function getMainmenus()
    {
        $Mainmenus = ArrayHelper::map(Mainmenu::find()->orderBy(["min_mnu_desc" => SORT_ASC])->all(), 'min_mnu_id', 'min_mnu_desc');
        return $Mainmenus;
    }

    public static function getSubmenus()
    {
        $Submenus = ArrayHelper::map(Submenu::find()->orderBy(["sub_mnu_desc" => SORT_ASC])->all(), 'sub_mnu_id', 'sub_mnu_desc');
        return $Submenus;
    }

    public static function getUsers()
    {
        $Users = ArrayHelper::map(Users::find()->orderBy(["users_name" => SORT_ASC])->all(), 'usr_id', 'users_name');
        return $Users;
    }

    public static function getARVMasters()
    {
        $Arvmasters = ArrayHelper::map(Arvmaster::find()->orderBy(["arv_mstr_code" => SORT_ASC])->all(), 'arv_mstr_code', 'arv_mstr_name');
        return $Arvmasters;
    }

    public static function getLabtests($isspot = 0)
    {
        $Labtests = ArrayHelper::map(Labmaster::find()->where(["lab_spot_test" => $isspot])->orderBy(["lab_test_name" => SORT_ASC])->all(), 'lab_test_code', 'lab_test_name');
        return $Labtests;
    }

    public static function getLabresults($testcode = "")
    {
        if($testcode == "")
            $Labresults = ArrayHelper::map(Labresultsmaster::find()->orderBy(["lab_result_name" => SORT_ASC])->all(), 'lab_result_code', 'lab_result_name');
        else {
            $rescode = ArrayHelper::getColumn(Labresultmapping::find()->where(["lab_result_mapng_test" => $testcode])->all(), "lab_result_mapng_result");
            $Labresults = ArrayHelper::map(Labresultsmaster::find()->where(["IN", "lab_result_code", $rescode])->orderBy(["lab_result_name" => SORT_ASC])->all(), 'lab_result_code', 'lab_result_name');
        }
        return $Labresults;
    }

    public static function getLabdepartments()
    {
        $DepCodes = ArrayHelper::map(Labtestmapping::find()->innerJoinWith('tests T')->orderBy(["T.lab_test_name" => SORT_ASC])->asArray()->all(), 'tests.lab_dept_code', 'tests.lab_dept_code');
        $id = "'" . implode($DepCodes, "','") . "'";
        $Departments = ArrayHelper::map(Departments::find()->where("lab_dept_code IN ($id)")->orderBy(["lab_dept_name" => SORT_ASC])->all(), 'lab_dept_code', 'lab_dept_name');
        return $Departments;
    }

    public static function getMenuname()
    {
        $Submenu = Submenu::find()->where(["sub_mnu_name" => Yii::$app->controller->id.'/'.Yii::$app->controller->action->id])->one();
        if($Submenu !== null)
            return $Submenu->sub_mnu_desc;
        else {
            // $Submenu = Submenu::find()->where(["LIKE", "sub_mnu_name", Yii::$app->controller->id])->one();
            $Submenu = Submenu::find()->where("sub_mnu_name like '".Yii::$app->controller->id."%'")->one();
            if($Submenu !== null)
                return $Submenu->sub_mnu_desc;
        }
    }

    public static function getPrivileges($option)
    {
        $Menuprivileges = Menuprivileges::find()->joinWith('submenus s')->where(["s.sub_mnu_name" => Yii::$app->controller->id])->andWhere(["mnu_acs_usr_id_fk" => Yii::$app->user->getId()])->one();
        $Menuprivileges = Menuprivileges::find()->joinWith('submenus s')->where(["LIKE", "s.sub_mnu_name", Yii::$app->controller->id])->andWhere(["mnu_acs_usr_id_fk" => Yii::$app->user->getId()])->one();
        if($Menuprivileges !== null) {
            if($option == 'Create')
                return $Menuprivileges->mnu_acs_add;
            elseif($option == 'Update')
                return $Menuprivileges->mnu_acs_edit;
            elseif($option == 'Delete')
                return $Menuprivileges->mnu_acs_delete;
        } else
            return true;
    }

    public static function counselorServiceCheck($PID)
    {
        // Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $res = [];
        $RModel = Registration::find()->where(["corfrm_pid" => $PID])->orderBy(["corfrm_pid" => SORT_ASC])->one();
        $res['registration'] = $RModel;
        $LabModel = Labsamplerequest::find()->where(["lab_smple_part_id" => $PID])->orderBy(["record_date" => SORT_DESC])->all();
        $res['labsamples'] = $LabModel;
        // print_r($LabModel[0]->attributes);
        return $res;
    }

    public static function labServiceCheck($PID)
    {
        $today = strtotime(date("Y/m/d"));
        $labsampletable = Labsamplerequest::tableName();
        $res = [];
        $RModel = Registration::find()->where("corfrm_pid = $PID")->orderBy(["corfrm_pid" => SORT_ASC])->one();
        $res['registration'] = $RModel;
        $LabModel = Labsamplerequest::find()->innerJoinWith('tests T')->where("lab_smple_part_id = $PID")->andWhere("$labsampletable.record_date = $today")->groupBy(["T.lab_dept_code"])->asArray()->all();
        $res['labsamples'] = $LabModel;
        //$SModel = Screening::find()->where("mem_scrn_part_id = $PID AND mem_scrn_q25 = 1")->one();
		$SModel = Screening::find()->where("mem_scrn_part_id = $PID AND mem_scrn_q25 = 1")->andWhere("mem_scrn_part_id  Not IN (SELECT lab_smple_part_id FROM $labsampletable where record_date = $today and lab_smple_tst_id=10)") ->one();
        $res['iscohort'] = $SModel;
		$Amodel = Applicationsettings::find()->orderBy("create_time DESC")->one();		  
		$res['isidu'] = $Amodel->app_idu;	
        return $res;
    }

   public static function saveLabResults($models)
    {
        foreach ($models as $modelAddress) {
            if($modelAddress->lab_smple_tst_id == 1 && $modelAddress->lab_smple_result !== Null ) {
                $model = Registration::find()->where(["corfrm_pid" => $modelAddress->lab_smple_part_id])->one();
                $model->corfrm_q12 = $modelAddress->lab_smple_result;
                $model->save(false);
            } elseif($modelAddress->lab_smple_tst_id == 3 && $modelAddress->lab_smple_result !== Null) {
                $model = Registration::find()->where(["corfrm_pid" => $modelAddress->lab_smple_part_id])->one();
                $model->corfrm_q14 = $modelAddress->lab_smple_result;
                $model->save(false);
            }
        }
        return true;
    }
    
   /* public static function getClientid($sid = "", $loc = "")
    {
        if($sid == "" && $loc == "") {
            $CModel = Clientidref::find()->orderBy(["record_date" => SORT_DESC])->one();        
            $Locations = Locationmaster::find()->orderBy(["loc_name" => SORT_ASC])->one();
            $Loccode = $Locations->loc_code;
        } else {
            $CModel = Clientidref::find()->where(["clientid_ref_sur" => $sid, "clientid_ref_loc" => $loc])->one();        
            $Loccode = $loc;
        }
    
        if($CModel  !== null) {
            if ($CModel->clientid_ref_sur == null){
			 $ref_code = 9999;
			}
		    else {
			 $ref_code=$CModel->clientid_ref_code;
			} 
			
			$refid = ($ref_code+1);
		}	
		else {
		 $refid = 10000;
		}
		
		$clientid = $Loccode.$refid;
		return $clientid;
	}
*/

    public static function getClientid($sid = "", $loc = "")
    {
     	
	  if($sid == "" && $loc == "") {
            $AppSettings = Applicationsettings::find()->orderBy(["app_stngs_id" => SORT_ASC])->one();
			$Surveycode=$AppSettings->app_survey_id;
	        $Loccode  = $AppSettings->app_location;     
            $CModel = Clientidref::find()->where(["clientid_ref_sur" => $Surveycode, "clientid_ref_loc" => $Loccode])->one();          
        } else {
            $CModel = Clientidref::find()->where(["clientid_ref_sur" => $sid, "clientid_ref_loc" => $loc])->one();        
            $Loccode = $loc;
        }		
	
        if($CModel  !== null) {
            if ($CModel->clientid_ref_sur == null){
			 $ref_code = 9999;
			}
		    else {
			 $ref_code=$CModel->clientid_ref_code;
			} 
			
			$refid = ($ref_code+1);
		}	
		else {
		 $refid = 10000;
		}
		
		$clientid = $Loccode.$refid;
		return $clientid;
	}
	
    public static function getSurlocations($id)
    {
        $Locations = ArrayHelper::map(Locationmapping::find()->innerJoinWith('locations L')->where(["loc_mapng_sur_id" => $id])->orderBy(["L.loc_name" => SORT_ASC])->all(), 'locations.loc_code', 'locations.loc_name');
        return $Locations;
    }

    public static function generateControl($form, $model, $template, $ctrlid, $ctrllist, $type, $commonfun = "", $depends = "")
    {
    	$storevalue = $dispvalue = "";
        if($type == "survey") {
            $fixed = Yii::$app->params['SURVEY_FIXED'];
            if($model->isNewRecord) {
                $storevalue = Yii::$app->params['SURVEY'];
                $dispvalue = Yii::$app->params['SURVEY_TITLE'];
            } else {
                $storevalue = $model->$ctrlid;
                $SModel = Surveymaster::find()->where(["sur_code" => $model->$ctrlid])->one();
                $dispvalue = $SModel->sur_title;
            }
            echo Html::hiddenInput('common-method', $commonfun, ['id'=>'common-method']);
            echo Html::hiddenInput('common-method-type', $depends, ['id'=>'common-method-type']);
        } elseif($type == "location") {
            $fixed = Yii::$app->params['LOCATION_FIXED'];
            if($model->isNewRecord) {
                $storevalue = Yii::$app->params['LOCATION'];
                $dispvalue = Yii::$app->params['LOCATION_TITLE'];
            } else {
                $storevalue = $model->$ctrlid;
                $LModel = Locationmaster::find()->where(["loc_code" => $model->$ctrlid])->one();
                $dispvalue = $LModel->loc_name;
            }
        } elseif($type == "pid") {
            $fixed = false;
            $storevalue = $model->$ctrlid;
            $dispvalue = $model->$ctrlid;
        }

        if($model->isNewRecord) {
            if(!$fixed) {
            	if($storevalue != "")
            		$model->$ctrlid = $storevalue;
                if($type == "survey") {
                    return $form->field($model, $ctrlid, $template)->dropDownList($ctrllist, ['prompt' => $model->getAttributeLabel($ctrlid)]);
                } elseif($type == "location") {
                    return $form->field($model, $ctrlid, $template)->widget(DepDrop::classname(), ['data'=>$ctrllist, 'options'=>['prompt'=>$model->getAttributeLabel($ctrlid)], 'pluginOptions'=>['depends'=>$depends, 'placeholder'=>$model->getAttributeLabel($ctrlid), 'url'=>Url::to(['/dashboard/getloc'])]]);
                } elseif($type == "pid") {
                    return $form->field($model, $ctrlid, $template)->widget(DepDrop::classname(), ['data'=>$ctrllist, 'options'=>['prompt'=>$model->getAttributeLabel($ctrlid)], 'type'=>DepDrop::TYPE_SELECT2, 'pluginOptions'=>['depends'=>$depends, 'placeholder'=>$model->getAttributeLabel($ctrlid), 'url'=>Url::to(['/dashboard/getids']), 'params'=>['common-method', 'common-method-type']]]);
                }
            } else {
                $template['template'] = preg_replace_callback("/{\\w+}/", function ($matches) use ($dispvalue) {
                    switch ($matches[0]) {
                        case '{error}':
                            return $dispvalue;
                        default:
                            return $matches[0];
                    }
                }, $template['template']);
                return $form->field($model, $ctrlid, $template)->hiddenInput(['value' => $storevalue]);
            }
        } else {
            $template['template'] = preg_replace_callback("/{\\w+}/", function ($matches) use ($dispvalue) {
                switch ($matches[0]) {
                    case '{error}':
                            return $dispvalue;
                    default:
                        return $matches[0];
                }
            }, $template['template']);
            return $form->field($model, $ctrlid, $template)->hiddenInput(['value' => $storevalue]);
        }
    }

    public static function getFieldvalue($frmfield = "")
    {
        if($frmfield == "")
            return [];
        $fields = ArrayHelper::map(Fieldmaster::find()->where(["fld_mstr_frmfield" => $frmfield])->andWhere(["status" => 1])->orderBy(["fld_mstr_code" => SORT_ASC])->all(), 'fld_mstr_code', 'fld_mstr_desc');
        return $fields;
    }

/*	
    public static function getIncentives($pid = "")
    {
        $services = [];

        if($pid == "") {
            $SModel = Screening::find()->where(["mem_scrn_q25" => 1])->all();
            
            $index = 0;
            $services[$index]["type"] = "Pre-ART";
            
            $services[$index]["allearned"] = Preartincenmaster::find()->sum("preart_inc_q2");
            $services[$index]["allpaid"] = Preartincenmaster::find()->where(["preart_inc_q1" => 1])->sum("preart_inc_q2");
            if($services[$index]["allearned"] !== null)
                $services[$index]["allbalance"] = $services[$index]["allearned"] - $services[$index]["allpaid"];
            else
                $services[$index]["allbalance"] = null;

            $services[$index]["incohortearned"] = Preartincenmaster::find()->where(["IN", "preart_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("preart_inc_q2");
            $services[$index]["incohortpaid"] = Preartincenmaster::find()->where(["preart_inc_q1" => 1])->andWhere(["IN", "preart_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("preart_inc_q2");
            if($services[$index]["incohortearned"] !== null)
                $services[$index]["incohortbalance"] = $services[$index]["incohortearned"] - $services[$index]["incohortpaid"];
            else
                $services[$index]["incohortbalance"] = null;
            
            $services[$index]["excohortearned"] = Preartincenmaster::find()->where(["NOT IN", "preart_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("preart_inc_q2");
            $services[$index]["excohortpaid"] = Preartincenmaster::find()->where(["preart_inc_q1" => 1])->andWhere(["NOT IN", "preart_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("preart_inc_q2");
            if($services[$index]["excohortearned"] !== null)
                $services[$index]["excohortbalance"] = $services[$index]["excohortearned"] - $services[$index]["excohortpaid"];
            else
                $services[$index]["excohortbalance"] = null;

            $index++;
            $services[$index]["type"] = "ART Initiation";
            
            $services[$index]["allearned"] = Artinitiationincentmaster::find()->sum("art_init_inc_q2");
            $services[$index]["allpaid"] = Artinitiationincentmaster::find()->where(["art_init_inc_q1" => 1])->sum("art_init_inc_q2");
            if($services[$index]["allearned"] !== null)
                $services[$index]["allbalance"] = $services[$index]["allearned"] - $services[$index]["allpaid"];
            else
                $services[$index]["allbalance"] = null;

            $services[$index]["incohortearned"] = Artinitiationincentmaster::find()->where(["IN", "art_init_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("art_init_inc_q2");
            $services[$index]["incohortpaid"] = Artinitiationincentmaster::find()->where(["art_init_inc_q1" => 1])->andWhere(["IN", "art_init_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("art_init_inc_q2");
            if($services[$index]["incohortearned"] !== null)
                $services[$index]["incohortbalance"] = $services[$index]["incohortearned"] - $services[$index]["incohortpaid"];
            else
                $services[$index]["incohortbalance"] = null;
            
            $services[$index]["excohortearned"] = Artinitiationincentmaster::find()->where(["NOT IN", "art_init_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("art_init_inc_q2");
            $services[$index]["excohortpaid"] = Artinitiationincentmaster::find()->where(["art_init_inc_q1" => 1])->andWhere(["NOT IN", "art_init_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("art_init_inc_q2");
            if($services[$index]["excohortearned"] !== null)
                $services[$index]["excohortbalance"] = $services[$index]["excohortearned"] - $services[$index]["excohortpaid"];
            else
                $services[$index]["excohortbalance"] = null;

            $index++;
            $services[$index]["type"] = "Motivational";
            
            $services[$index]["allearned"] = Motincenmaster::find()->sum("mot_inc_q2");
            $services[$index]["allpaid"] = Motincenmaster::find()->where(["mot_inc_q1" => 1])->sum("mot_inc_q2");
            if($services[$index]["allearned"] !== null)
                $services[$index]["allbalance"] = $services[$index]["allearned"] - $services[$index]["allpaid"];
            else
                $services[$index]["allbalance"] = null;

            $services[$index]["incohortearned"] = Motincenmaster::find()->where(["IN", "mot_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("mot_inc_q2");
            $services[$index]["incohortpaid"] = Motincenmaster::find()->where(["mot_inc_q1" => 1])->andWhere(["IN", "mot_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("mot_inc_q2");
            if($services[$index]["incohortearned"] !== null)
                $services[$index]["incohortbalance"] = $services[$index]["incohortearned"] - $services[$index]["incohortpaid"];
            else
                $services[$index]["incohortbalance"] = null;
            
            $services[$index]["excohortearned"] = Motincenmaster::find()->where(["NOT IN", "mot_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("mot_inc_q2");
            $services[$index]["excohortpaid"] = Motincenmaster::find()->where(["mot_inc_q1" => 1])->andWhere(["NOT IN", "mot_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("mot_inc_q2");
            if($services[$index]["excohortearned"] !== null)
                $services[$index]["excohortbalance"] = $services[$index]["excohortearned"] - $services[$index]["excohortpaid"];
            else
                $services[$index]["excohortbalance"] = null;

            $index++;
            $services[$index]["type"] = "ART Refills";
            
            $services[$index]["allearned"] = Artincenmaster::find()->sum("art_inc_q2");
            $services[$index]["allpaid"] = Artincenmaster::find()->where(["art_inc_q1" => 1])->sum("art_inc_q2");
            if($services[$index]["allearned"] !== null)
                $services[$index]["allbalance"] = $services[$index]["allearned"] - $services[$index]["allpaid"];
            else
                $services[$index]["allbalance"] = null;

            $services[$index]["incohortearned"] = Artincenmaster::find()->where(["IN", "art_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("art_inc_q2");
            $services[$index]["incohortpaid"] = Artincenmaster::find()->where(["art_inc_q1" => 1])->andWhere(["IN", "art_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("art_inc_q2");
            if($services[$index]["incohortearned"] !== null)
                $services[$index]["incohortbalance"] = $services[$index]["incohortearned"] - $services[$index]["incohortpaid"];
            else
                $services[$index]["incohortbalance"] = null;
            
            $services[$index]["excohortearned"] = Artincenmaster::find()->where(["NOT IN", "art_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("art_inc_q2");
            $services[$index]["excohortpaid"] = Artincenmaster::find()->where(["art_inc_q1" => 1])->andWhere(["NOT IN", "art_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("art_inc_q2");
            if($services[$index]["excohortearned"] !== null)
                $services[$index]["excohortbalance"] = $services[$index]["excohortearned"] - $services[$index]["excohortpaid"];
            else
                $services[$index]["excohortbalance"] = null;
        } else {
            $today = strtotime(date("Y/m/d"));
            $SModel = Screening::find()->where(["mem_scrn_part_id" => $pid, "mem_scrn_q25" => 1])->all();
            
            $index = 0;
            $services[$index]["pid"] = $pid;
            $services[$index]["pidcol"] = "preart_inc_part_id";
            $services[$index]["model"] = "Preartincenmaster";
            $services[$index]["type"] = "Pre-ART";
            
            $services[$index]["allearned"] = Preartincenmaster::find()->where(["preart_inc_part_id" => $pid])->sum("preart_inc_q2");
            $services[$index]["allpaid"] = Preartincenmaster::find()->where(["preart_inc_part_id" => $pid, "preart_inc_q1" => 1])->sum("preart_inc_q2");
            if($services[$index]["allearned"] !== null)
                $services[$index]["allbalance"] = $services[$index]["allearned"] - $services[$index]["allpaid"];
            else
                $services[$index]["allbalance"] = null;

            $services[$index]["incohortearned"] = Preartincenmaster::find()->where(["preart_inc_part_id" => $pid, "record_date" => $today])->sum("preart_inc_q2");
            $services[$index]["incohortpaid"] = Preartincenmaster::find()->where(["preart_inc_part_id" => $pid, "record_date" => $today, "preart_inc_q1" => 1])->sum("preart_inc_q2");
            if($services[$index]["incohortearned"] !== null)
                $services[$index]["incohortbalance"] = $services[$index]["incohortearned"] - $services[$index]["incohortpaid"];
            else
                $services[$index]["incohortbalance"] = null;

            $index++;
            $services[$index]["pid"] = $pid;
            $services[$index]["pidcol"] = "art_init_inc_part_id";
            $services[$index]["model"] = "Artinitiationincentmaster";
            $services[$index]["type"] = "ART Initiation";
            
            $services[$index]["allearned"] = Artinitiationincentmaster::find()->where(["art_init_inc_part_id" => $pid])->sum("art_init_inc_q2");
            $services[$index]["allpaid"] = Artinitiationincentmaster::find()->where(["art_init_inc_part_id" => $pid, "art_init_inc_q1" => 1])->sum("art_init_inc_q2");
            if($services[$index]["allearned"] !== null)
                $services[$index]["allbalance"] = $services[$index]["allearned"] - $services[$index]["allpaid"];
            else
                $services[$index]["allbalance"] = null;

            $services[$index]["incohortearned"] = Artinitiationincentmaster::find()->where(["art_init_inc_part_id" => $pid, "record_date" => $today])->sum("art_init_inc_q2");
            $services[$index]["incohortpaid"] = Artinitiationincentmaster::find()->where(["art_init_inc_part_id" => $pid, "record_date" => $today, "art_init_inc_q1" => 1])->sum("art_init_inc_q2");
            if($services[$index]["incohortearned"] !== null)
                $services[$index]["incohortbalance"] = $services[$index]["incohortearned"] - $services[$index]["incohortpaid"];
            else
                $services[$index]["incohortbalance"] = null;

            $index++;
            $services[$index]["pid"] = $pid;
            $services[$index]["pidcol"] = "mot_inc_part_id";
            $services[$index]["model"] = "Motincenmaster";
            $services[$index]["type"] = "Motivational";
            
            $services[$index]["allearned"] = Motincenmaster::find()->where(["mot_inc_part_id" => $pid])->sum("mot_inc_q2");
            $services[$index]["allpaid"] = Motincenmaster::find()->where(["mot_inc_part_id" => $pid, "mot_inc_q1" => 1])->sum("mot_inc_q2");
            if($services[$index]["allearned"] !== null)
                $services[$index]["allbalance"] = $services[$index]["allearned"] - $services[$index]["allpaid"];
            else
                $services[$index]["allbalance"] = null;

            $services[$index]["incohortearned"] = Motincenmaster::find()->where(["mot_inc_part_id" => $pid, "record_date" => $today])->sum("mot_inc_q2");
            $services[$index]["incohortpaid"] = Motincenmaster::find()->where(["mot_inc_part_id" => $pid, "record_date" => $today, "mot_inc_q1" => 1])->sum("mot_inc_q2");
            if($services[$index]["incohortearned"] !== null)
                $services[$index]["incohortbalance"] = $services[$index]["incohortearned"] - $services[$index]["incohortpaid"];
            else
                $services[$index]["incohortbalance"] = null;

            $index++;
            $services[$index]["pid"] = $pid;
            $services[$index]["pidcol"] = "art_inc_part_id";
            $services[$index]["model"] = "Artincenmaster";
            $services[$index]["type"] = "ART Refills";
            
            $services[$index]["allearned"] = Artincenmaster::find()->where(["art_inc_part_id" => $pid])->sum("art_inc_q2");
            $services[$index]["allpaid"] = Artincenmaster::find()->where(["art_inc_part_id" => $pid, "art_inc_q1" => 1])->sum("art_inc_q2");
            if($services[$index]["allearned"] !== null)
                $services[$index]["allbalance"] = $services[$index]["allearned"] - $services[$index]["allpaid"];
            else
                $services[$index]["allbalance"] = null;

            $services[$index]["incohortearned"] = Artincenmaster::find()->where(["art_inc_part_id" => $pid, "record_date" => $today])->sum("art_inc_q2");
            $services[$index]["incohortpaid"] = Artincenmaster::find()->where(["art_inc_part_id" => $pid, "record_date" => $today, "art_inc_q1" => 1])->sum("art_inc_q2");
            if($services[$index]["incohortearned"] !== null)
                $services[$index]["incohortbalance"] = $services[$index]["incohortearned"] - $services[$index]["incohortpaid"];
            else
                $services[$index]["incohortbalance"] = null;
        }

        $provider = new ArrayDataProvider([
            'allModels' => $services,
        ]);
        return $provider;
    }

  public static function getArtbookPIDs($sid = "", $loc = "")
    {
        $today = strtotime(date("Y/m/d"));
        if($sid == "" && $loc == "")
            $PIDs = ArrayHelper::map(Attandance::find()->where(["visit_date" => $today])->andWhere(["IS", "out_interviewer", NULL])->andWhere(["IS", "visit_out", NULL])->orderBy(["pid" => SORT_ASC])->all(), 'pid', 'pid');
        else
            $PIDs = ArrayHelper::map(Attandance::find()->where(["visit_date" => $today])->andWhere(["sid" => $sid, "location" => $loc])->andWhere(["IS", "out_interviewer", NULL])->andWhere(["IS", "visit_out", NULL])->orderBy(["pid" => SORT_ASC])->all(), 'pid', 'pid');
        return $PIDs;
    }


  public static function getArtbookPIDs($sid = "", $loc = "")
    {
        $today = strtotime(date("Y/m/d"));        
		
        $query = Attandance::find()->innerJoinWith("registration")->where(["IS", "visit_out", NULL])->andWhere(["IS", "out_interviewer", NULL])->andWhere(["visit_date" => $today])->andWhere(["OR", ["corfrm_q12" => 1], ["corfrm_q12" => 9]]);
		        
        if($sid == "" && $loc == "") {           
        } 
		else {           
            $query->andWhere(["sid" => $sid, "location" => $loc]);        }
		
        $PIDs = ArrayHelper::map($query->orderBy(["pid" => SORT_ASC])->all(), 'pid', 'pid');
        return $PIDs;
    }
	
	 public static function getArtbookPIDs($sid = "", $loc = "")
    {
      
		$PIDs = Hivlinkage::find()->where("hiv_linkage_q2a=1")->asArray()->all();       
		
		$today = strtotime(date("Y/m/d"));	
        if($sid == "" && $loc == "")
            $PIDs = ArrayHelper::map(Attandance::find()->where(["visit_date" => $today])->andWhere(["IS", "out_interviewer", NULL])->andWhere(["IS", "visit_out", NULL])->andWhere(["IN", "pid", ArrayHelper::getColumn($PIDs, "hiv_linkage_part_id")])->orderBy(["pid" => SORT_ASC])->all(), 'pid', 'pid');
        else
            $PIDs = ArrayHelper::map(Attandance::find()->where(["visit_date" => $today])->andWhere(["sid" => $sid, "location" => $loc])->andWhere(["IS", "out_interviewer", NULL])->andWhere(["IS", "visit_out", NULL])->andWhere(["IN", "pid", ArrayHelper::getColumn($PIDs, "hiv_linkage_part_id")])->orderBy(["pid" => SORT_ASC])->all(), 'pid', 'pid');
        return $PIDs;
    }	
*/

    public static function getIncentives($pid = "")
    {
        $services = [];

        if($pid == "") {
            $SModel = Screening::find()->where(["mem_scrn_q25" => 1])->all();
            
            $index = 0;
            $services[$index]["type"] = "Pre-ART";
            
            $services[$index]["allearned"] = Preartincenmaster::find()->sum("preart_inc_q2");
            $services[$index]["allpaid"] = Preartincenmaster::find()->where(["preart_inc_q1" => 1])->sum("preart_inc_q2");
            if($services[$index]["allearned"] !== null)
                $services[$index]["allbalance"] = $services[$index]["allearned"] - $services[$index]["allpaid"];
            else
                $services[$index]["allbalance"] = null;

            $services[$index]["incohortearned"] = Preartincenmaster::find()->where(["IN", "preart_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("preart_inc_q2");
            $services[$index]["incohortpaid"] = Preartincenmaster::find()->where(["preart_inc_q1" => 1])->andWhere(["IN", "preart_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("preart_inc_q2");
            if($services[$index]["incohortearned"] !== null)
                $services[$index]["incohortbalance"] = $services[$index]["incohortearned"] - $services[$index]["incohortpaid"];
            else
                $services[$index]["incohortbalance"] = null;
            
            $services[$index]["excohortearned"] = Preartincenmaster::find()->where(["NOT IN", "preart_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("preart_inc_q2");
            $services[$index]["excohortpaid"] = Preartincenmaster::find()->where(["preart_inc_q1" => 1])->andWhere(["NOT IN", "preart_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("preart_inc_q2");
            if($services[$index]["excohortearned"] !== null)
                $services[$index]["excohortbalance"] = $services[$index]["excohortearned"] - $services[$index]["excohortpaid"];
            else
                $services[$index]["excohortbalance"] = null;

            $index++;
            $services[$index]["type"] = "ART Initiation";
            
            $services[$index]["allearned"] = Artinitiationincentmaster::find()->sum("art_init_inc_q2");
            $services[$index]["allpaid"] = Artinitiationincentmaster::find()->where(["art_init_inc_q1" => 1])->sum("art_init_inc_q2");
            if($services[$index]["allearned"] !== null)
                $services[$index]["allbalance"] = $services[$index]["allearned"] - $services[$index]["allpaid"];
            else
                $services[$index]["allbalance"] = null;

            $services[$index]["incohortearned"] = Artinitiationincentmaster::find()->where(["IN", "art_init_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("art_init_inc_q2");
            $services[$index]["incohortpaid"] = Artinitiationincentmaster::find()->where(["art_init_inc_q1" => 1])->andWhere(["IN", "art_init_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("art_init_inc_q2");
            if($services[$index]["incohortearned"] !== null)
                $services[$index]["incohortbalance"] = $services[$index]["incohortearned"] - $services[$index]["incohortpaid"];
            else
                $services[$index]["incohortbalance"] = null;
            
            $services[$index]["excohortearned"] = Artinitiationincentmaster::find()->where(["NOT IN", "art_init_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("art_init_inc_q2");
            $services[$index]["excohortpaid"] = Artinitiationincentmaster::find()->where(["art_init_inc_q1" => 1])->andWhere(["NOT IN", "art_init_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("art_init_inc_q2");
            if($services[$index]["excohortearned"] !== null)
                $services[$index]["excohortbalance"] = $services[$index]["excohortearned"] - $services[$index]["excohortpaid"];
            else
                $services[$index]["excohortbalance"] = null;

            $index++;
            $services[$index]["type"] = "Motivational";
            
            $services[$index]["allearned"] = Motincenmaster::find()->sum("mot_inc_q2");
            $services[$index]["allpaid"] = Motincenmaster::find()->where(["mot_inc_q1" => 1])->sum("mot_inc_q2");
            if($services[$index]["allearned"] !== null)
                $services[$index]["allbalance"] = $services[$index]["allearned"] - $services[$index]["allpaid"];
            else
                $services[$index]["allbalance"] = null;

            $services[$index]["incohortearned"] = Motincenmaster::find()->where(["IN", "mot_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("mot_inc_q2");
            $services[$index]["incohortpaid"] = Motincenmaster::find()->where(["mot_inc_q1" => 1])->andWhere(["IN", "mot_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("mot_inc_q2");
            if($services[$index]["incohortearned"] !== null)
                $services[$index]["incohortbalance"] = $services[$index]["incohortearned"] - $services[$index]["incohortpaid"];
            else
                $services[$index]["incohortbalance"] = null;
            
            $services[$index]["excohortearned"] = Motincenmaster::find()->where(["NOT IN", "mot_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("mot_inc_q2");
            $services[$index]["excohortpaid"] = Motincenmaster::find()->where(["mot_inc_q1" => 1])->andWhere(["NOT IN", "mot_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("mot_inc_q2");
            if($services[$index]["excohortearned"] !== null)
                $services[$index]["excohortbalance"] = $services[$index]["excohortearned"] - $services[$index]["excohortpaid"];
            else
                $services[$index]["excohortbalance"] = null;

            $index++;
            $services[$index]["type"] = "ART Refills";
            
            $services[$index]["allearned"] = Artincenmaster::find()->sum("art_inc_q2");
            $services[$index]["allpaid"] = Artincenmaster::find()->where(["art_inc_q1" => 1])->sum("art_inc_q2");
            if($services[$index]["allearned"] !== null)
                $services[$index]["allbalance"] = $services[$index]["allearned"] - $services[$index]["allpaid"];
            else
                $services[$index]["allbalance"] = null;

            $services[$index]["incohortearned"] = Artincenmaster::find()->where(["IN", "art_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("art_inc_q2");
            $services[$index]["incohortpaid"] = Artincenmaster::find()->where(["art_inc_q1" => 1])->andWhere(["IN", "art_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("art_inc_q2");
            if($services[$index]["incohortearned"] !== null)
                $services[$index]["incohortbalance"] = $services[$index]["incohortearned"] - $services[$index]["incohortpaid"];
            else
                $services[$index]["incohortbalance"] = null;
            
            $services[$index]["excohortearned"] = Artincenmaster::find()->where(["NOT IN", "art_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("art_inc_q2");
            $services[$index]["excohortpaid"] = Artincenmaster::find()->where(["art_inc_q1" => 1])->andWhere(["NOT IN", "art_inc_part_id", ArrayHelper::getColumn($SModel, "mem_scrn_part_id")])->sum("art_inc_q2");
            if($services[$index]["excohortearned"] !== null)
                $services[$index]["excohortbalance"] = $services[$index]["excohortearned"] - $services[$index]["excohortpaid"];
            else
                $services[$index]["excohortbalance"] = null;
        } else {
            $today = strtotime(date("Y/m/d"));
            $SModel = Screening::find()->where(["mem_scrn_part_id" => $pid, "mem_scrn_q25" => 1])->all();
            
            $index = 0;
			$allearnedsum = 0;
			$allpaidsum = 0;
			$allbalancesum = 0;
			
            $services[$index]["pid"] = $pid;
            $services[$index]["pidcol"] = "preart_inc_part_id";
            $services[$index]["model"] = "Preartincenmaster";
            $services[$index]["type"] = "Pre-ART";
			
            
            $services[$index]["allearned"] = Preartincenmaster::find()->where(["preart_inc_part_id" => $pid])->sum("preart_inc_q2");		
            $services[$index]["allpaid"] = Preartincenmaster::find()->where(["preart_inc_part_id" => $pid, "preart_inc_q1" => 1])->sum("preart_inc_q2");
            if($services[$index]["allearned"] !== null)
                $services[$index]["allbalance"] = $services[$index]["allearned"] - $services[$index]["allpaid"];
            else
                $services[$index]["allbalance"] = null;
			
			$allearnedsum +=$services[$index]["allearned"] ;
			$allpaidsum +=$services[$index]["allpaid"] ;
			$allbalancesum +=$services[$index]["allbalance"] ;

            $services[$index]["incohortearned"] = Preartincenmaster::find()->where(["preart_inc_part_id" => $pid, "record_date" => $today])->sum("preart_inc_q2");
            $services[$index]["incohortpaid"] = Preartincenmaster::find()->where(["preart_inc_part_id" => $pid, "record_date" => $today, "preart_inc_q1" => 1])->sum("preart_inc_q2");
            if($services[$index]["incohortearned"] !== null)
                $services[$index]["incohortbalance"] = $services[$index]["incohortearned"] - $services[$index]["incohortpaid"];
            else
                $services[$index]["incohortbalance"] = null;

            $index++;
            $services[$index]["pid"] = $pid;
            $services[$index]["pidcol"] = "art_init_inc_part_id";
            $services[$index]["model"] = "Artinitiationincentmaster";
            $services[$index]["type"] = "ART Initiation";
            
            $services[$index]["allearned"] = Artinitiationincentmaster::find()->where(["art_init_inc_part_id" => $pid])->sum("art_init_inc_q2");
            $services[$index]["allpaid"] = Artinitiationincentmaster::find()->where(["art_init_inc_part_id" => $pid, "art_init_inc_q1" => 1])->sum("art_init_inc_q2");
            if($services[$index]["allearned"] !== null)
                $services[$index]["allbalance"] = $services[$index]["allearned"] - $services[$index]["allpaid"];
            else
                $services[$index]["allbalance"] = null;
			
			$allearnedsum +=$services[$index]["allearned"] ;
			$allpaidsum +=$services[$index]["allpaid"] ;
			$allbalancesum +=$services[$index]["allbalance"] ;

            $services[$index]["incohortearned"] = Artinitiationincentmaster::find()->where(["art_init_inc_part_id" => $pid, "record_date" => $today])->sum("art_init_inc_q2");
            $services[$index]["incohortpaid"] = Artinitiationincentmaster::find()->where(["art_init_inc_part_id" => $pid, "record_date" => $today, "art_init_inc_q1" => 1])->sum("art_init_inc_q2");
            if($services[$index]["incohortearned"] !== null)
                $services[$index]["incohortbalance"] = $services[$index]["incohortearned"] - $services[$index]["incohortpaid"];
            else
                $services[$index]["incohortbalance"] = null;

            $index++;
            $services[$index]["pid"] = $pid;
            $services[$index]["pidcol"] = "mot_inc_part_id";
            $services[$index]["model"] = "Motincenmaster";
            $services[$index]["type"] = "Motivational";
            
            $services[$index]["allearned"] = Motincenmaster::find()->where(["mot_inc_part_id" => $pid])->sum("mot_inc_q2");
            $services[$index]["allpaid"] = Motincenmaster::find()->where(["mot_inc_part_id" => $pid, "mot_inc_q1" => 1])->sum("mot_inc_q2");
            if($services[$index]["allearned"] !== null)
                $services[$index]["allbalance"] = $services[$index]["allearned"] - $services[$index]["allpaid"];
            else
                $services[$index]["allbalance"] = null;
			
			$allearnedsum +=$services[$index]["allearned"] ;
			$allpaidsum +=$services[$index]["allpaid"] ;
			$allbalancesum +=$services[$index]["allbalance"] ;

            $services[$index]["incohortearned"] = Motincenmaster::find()->where(["mot_inc_part_id" => $pid, "record_date" => $today])->sum("mot_inc_q2");
            $services[$index]["incohortpaid"] = Motincenmaster::find()->where(["mot_inc_part_id" => $pid, "record_date" => $today, "mot_inc_q1" => 1])->sum("mot_inc_q2");
            if($services[$index]["incohortearned"] !== null)
                $services[$index]["incohortbalance"] = $services[$index]["incohortearned"] - $services[$index]["incohortpaid"];
            else
                $services[$index]["incohortbalance"] = null;

            $index++;
            $services[$index]["pid"] = $pid;
            $services[$index]["pidcol"] = "art_inc_part_id";
            $services[$index]["model"] = "Artincenmaster";
            $services[$index]["type"] = "ART Refills";
            
            $services[$index]["allearned"] = Artincenmaster::find()->where(["art_inc_part_id" => $pid])->sum("art_inc_q2");
            $services[$index]["allpaid"] = Artincenmaster::find()->where(["art_inc_part_id" => $pid, "art_inc_q1" => 1])->sum("art_inc_q2");
            if($services[$index]["allearned"] !== null)
                $services[$index]["allbalance"] = $services[$index]["allearned"] - $services[$index]["allpaid"];
            else
                $services[$index]["allbalance"] = null;
			
			$allearnedsum +=$services[$index]["allearned"] ;
			$allpaidsum +=$services[$index]["allpaid"] ;
			$allbalancesum +=$services[$index]["allbalance"] ;

            $services[$index]["incohortearned"] = Artincenmaster::find()->where(["art_inc_part_id" => $pid, "record_date" => $today])->sum("art_inc_q2");
            $services[$index]["incohortpaid"] = Artincenmaster::find()->where(["art_inc_part_id" => $pid, "record_date" => $today, "art_inc_q1" => 1])->sum("art_inc_q2");
            if($services[$index]["incohortearned"] !== null)
                $services[$index]["incohortbalance"] = $services[$index]["incohortearned"] - $services[$index]["incohortpaid"];
            else
                $services[$index]["incohortbalance"] = null;
					
			$index++;
            $services[$index]["pid"] = $pid;
            $services[$index]["pidcol"] = "";
            $services[$index]["model"] = "";
            $services[$index]["type"] = "Total";
			
			 if($allearnedsum !== 0)
			  $services[$index]["allearned"] = $allearnedsum;
             else		  
				$services[$index]["allearned"] = null;
			
			 if($allpaidsum !== 0)
			   $services[$index]["allpaid"] = $allpaidsum;
             else		  
				$services[$index]["allpaid"] = null;
			
			 if($allearnedsum !== 0)
			   $services[$index]["allbalance"] = $allbalancesum;
             else		  
				$services[$index]["allbalance"] = null;
			
			 //$services[$index]["allearned"] = $allearnedsum;
			 //$services[$index]["allpaid"] = $allpaidsum;
			// $services[$index]["allbalance"] = $allbalancesum;
        }

        $provider = new ArrayDataProvider([
            'allModels' => $services,
        ]);
        return $provider;
    }

    public static function getTodaysPIDs($sid = "", $loc = "")
    {
        $today = strtotime(date("Y/m/d"));
        // $query = Attandance::find()->innerJoinWith("registration")->where([">", "visit1", "0"])->andWhere(["IS", "visit_out", NULL])->andWhere(["IS", "out_interviewer", NULL])->andWhere(["visit_date" => $today, "corfrm_q12" => 1])->orWhere(["visit_date" => $today, "corfrm_q12" => 9]);
        $query = Attandance::find()->innerJoinWith("registration")->where([">", "visit", "1"])->andWhere(["IS", "visit_out", NULL])->andWhere(["IS", "out_interviewer", NULL])->andWhere(["visit_date" => $today])->andWhere(["OR", ["corfrm_q12" => 1], ["corfrm_q12" => 9]]);
        // $query = Attandance::find()->innerJoinWith("registration")->where("visit1 > 0 AND visit_out IS NULL AND out_interviewer IS NULL AND visit_date = $today")->andWhere("corfrm_q12 = 1 OR corfrm_q12 = 9");
        if($sid == "" && $loc == "") {
            // $PIDs = ArrayHelper::map(Attandance::find()->where(["visit_date" => $today])->andWhere(["IS", "out_interviewer", NULL])->andWhere(["IS", "visit_out", NULL])->orderBy(["pid" => SORT_ASC])->all(), 'pid', 'pid');
        } else {
            // $PIDs = ArrayHelper::map(Attandance::find()->where(["visit_date" => $today])->andWhere(["sid" => $sid, "location" => $loc])->andWhere(["IS", "out_interviewer", NULL])->andWhere(["IS", "visit_out", NULL])->orderBy(["pid" => SORT_ASC])->all(), 'pid', 'pid');
            $query->andWhere(["sid" => $sid, "location" => $loc]);
        }
        $PIDs = ArrayHelper::map($query->orderBy(["pid" => SORT_ASC])->all(), 'pid', 'pid');
        return $PIDs;
    }

    public static function getAllPIDs()
    {
        $PIDs = ArrayHelper::map(Registration::find()->where(["<>", "corfrm_q13", 3])->orderBy(["corfrm_pid" => SORT_ASC])->all(), 'corfrm_pid', 'corfrm_pid');
        return $PIDs;
    }

    public static function getPIDsVisit($sid = "", $loc = "", $pid = "")
    {
        // $AModels = Attandance::find()->where(["sid" => $sid, "location" => $loc, "pid" => $pid])->all();
        // $VModels = Visitmaster::find()->where(['NOT', ['visit_interval' => null]])->andWhere(["NOT IN", "visit_code", ArrayHelper::getColumn($AModels, "visit")])->andWhere(["status" => 1])->orderBy(["visit_name" => SORT_ASC])->all();
        $VModels = Visitmaster::find()->where(['NOT', ['visit_interval' => null]])->andWhere(["status" => 1])->orderBy(["visit_name" => SORT_ASC])->all();
        return ArrayHelper::map($VModels, 'visit_code', 'visit_name');

        $today = strtotime(date("Y/m/d"));
        if($sid == "" && $loc == "")
            $PIDs = ArrayHelper::map(Attandance::find()->where(["visit_date" => $today])->andWhere(["IS", "out_interviewer", NULL])->andWhere(["IS", "visit_out", NULL])->orderBy(["pid" => SORT_ASC])->all(), 'pid', 'pid');
        else
            $PIDs = ArrayHelper::map(Attandance::find()->where(["visit_date" => $today])->andWhere(["sid" => $sid, "location" => $loc])->andWhere(["IS", "out_interviewer", NULL])->andWhere(["IS", "visit_out", NULL])->orderBy(["pid" => SORT_ASC])->all(), 'pid', 'pid');
        return $PIDs;
    }

    public static function getReportsPIDs($sid = "", $loc = "")
    {
        // $today = strtotime(date("Y/m/d"));
        // $query = Attandance::find()->innerJoinWith("registration")->andWhere(["IS", "visit_out", NULL])->andWhere(["IS", "out_interviewer", NULL])->andWhere(["visit_date" => $today])->andWhere(["OR", ["corfrm_q12" => 1], ["corfrm_q12" => 9]]);
        // if($sid != "" && $loc != "") {
        //     $query->andWhere(["sid" => $sid, "location" => $loc]);
        // }
        // $PIDs = ArrayHelper::map($query->orderBy(["pid" => SORT_ASC])->all(), 'pid', 'pid');
        // return $PIDs;

        $model = Registration::find()->where(["<>", "corfrm_q13", 3]);
        if($sid != "" && $loc != "") {
            $model->where(["corfrm_sid" => $sid, "corfrm_loc" => $loc]);
        }
        $PIDs = ArrayHelper::map($model->orderBy(["corfrm_pid" => SORT_ASC])->all(), 'corfrm_pid', 'corfrm_pid');
        return $PIDs;
    }
	
	 public static function getScrEnrPIDs()
    {
		
        $PIDs = ArrayHelper::map(Screening::find()->where(["mem_scrn_q25" => 1])->orderBy(["mem_scrn_part_id" => SORT_ASC])->all(), 'mem_scrn_part_id', 'mem_scrn_part_id');
        return $PIDs;
    }

    public static function convertTemplate($template, $type, $value)
    {
        $matches = preg_split('/(<[^>]*[\/div]>)/i', $template['template'], -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
        if($type == "prepand") {
            $len = count($matches);
            $matches[] = $matches[$len-1];
            $matches[$len-1] = $matches[$len-2];
            $matches[$len-2] = '<div class="col-md-3 text-prepand">'.$value.'</div>';
        } else {
            $matches[] = '<div class="col-md-3 text-postpand">'.$value.'</div>';
        }
        $template['template'] = implode("", $matches);
        return $template;
    }
	 public static function getVSPIDs()
    {
          
        $PIDs = ArrayHelper::map(Visitschedule::find()->orderBy(["participant_id" => SORT_ASC])->all(), 'participant_id', 'participant_id');
        return $PIDs;
    }
	
	 public static function getCohortVisits($sid = "", $loc = "", $pid = "")
    {
       
        $VModels = Visitmaster::find()->where(['NOT', ['visit_interval' => null]])->andWhere(["status" => 1])->orderBy(["visit_name" => SORT_ASC])->all();
        return ArrayHelper::map($VModels, 'visit_code', 'visit_name');
        
    }
	
	public static function getArtbookPIDs($sid = "", $loc = "")
    {
      
		$PIDs = Artbook::find()->asArray()->all();       
		
        if($sid == "" && $loc == "")
            $PIDs = ArrayHelper::map(Hivlinkage::find()->where(["hiv_linkage_q2a" => 1])->andWhere(["NOT IN", "hiv_linkage_part_id", ArrayHelper::getColumn($PIDs, "artbook_pid")])->orderBy(["hiv_linkage_part_id" => SORT_ASC])->all(), 'hiv_linkage_part_id', 'hiv_linkage_part_id');
        else
            $PIDs = ArrayHelper::map(Hivlinkage::find()->where(["hiv_linkage_q2a" => 1])->andWhere(["hiv_linkage_survey" => $sid, "hiv_linkage_loc" => $loc])->andWhere(["NOT IN", "hiv_linkage_part_id", ArrayHelper::getColumn($PIDs, "artbook_pid")])->orderBy(["hiv_linkage_part_id" => SORT_ASC])->all(), 'hiv_linkage_part_id', 'hiv_linkage_part_id');
        return $PIDs;
    }
	
	public static function getIncPIDs($sid = "", $loc = "")  
	{                
        $query = Registration::find()->where(["OR", ["corfrm_q12" => 1], ["corfrm_q12" => 9]]);
       
        if($sid == "" && $loc == "") {
           
        } else {            
            $query->andWhere(["corfrm_sid" => $sid, "corfrm_loc" => $loc]);
        }
        $PIDs = ArrayHelper::map($query->orderBy(["corfrm_pid" => SORT_ASC])->all(), 'corfrm_pid', 'corfrm_pid');
        return $PIDs;
    }
	
	public static function getArtrefillPIDs()
    {
        $today = strtotime(date("Y/m/d"));
        $Model = Attandance::find()->where(["record_date" => $today])->all();     
        $model = Artincenmaster::find()->where(["art_inc_q1" => 0])->andWhere(["IN", "art_inc_part_id", ArrayHelper::getColumn($Model, "pid")])->orderBy(["art_inc_part_id" => SORT_ASC])->all();        
        $PIDs = ArrayHelper::map($model, 'art_inc_part_id', 'art_inc_part_id');		
        return $PIDs;
    }
	
	public static function getArtInitPIDs()
    {
        $today = strtotime(date("Y/m/d"));
        $Model = Attandance::find()->where(["record_date" => $today])->all();     
        $model = Artinitiationincentmaster::find()->where(["art_init_inc_q1" => 0])->andWhere(["IN", "art_init_inc_part_id", ArrayHelper::getColumn($Model, "pid")])->orderBy(["art_init_inc_part_id" => SORT_ASC])->all();        
        $PIDs = ArrayHelper::map($model, 'art_init_inc_part_id', 'art_init_inc_part_id');		
        return $PIDs;
    }
	
	public static function getPreARTPIDs()
    {
        $today = strtotime(date("Y/m/d"));
        $Model = Attandance::find()->where(["record_date" => $today])->all();     
        $model = Preartincenmaster::find()->where(["preart_inc_q1" => 0])->andWhere(["IN", "preart_inc_part_id", ArrayHelper::getColumn($Model, "pid")])->orderBy(["preart_inc_part_id" => SORT_ASC])->all();        
        $PIDs = ArrayHelper::map($model, 'preart_inc_part_id', 'preart_inc_part_id');		
        return $PIDs;
    }
	
	public static function getMotivationPIDs()
    {
        $today = strtotime(date("Y/m/d"));
        $Model = Attandance::find()->where(["record_date" => $today])->all();     
        $model = Motincenmaster::find()->where(["mot_inc_q1" => 0])->andWhere(["IN", "mot_inc_part_id", ArrayHelper::getColumn($Model, "pid")])->orderBy(["mot_inc_part_id" => SORT_ASC])->all();        
        $PIDs = ArrayHelper::map($model, 'mot_inc_part_id', 'mot_inc_part_id');		
        return $PIDs;
    }
	
	public static function getIncEarnedPIDs()
    {
       
        $Model = Allincvoucherreport::find()->orderBy(["pid" => SORT_ASC])->all();
		$PIDs =ArrayHelper::map($Model, 'pid', 'pid');
        return $PIDs;
        
    }
	public static function getIpePIDs($sid = "", $loc = "")
    {
        $Model = Ipe::find()->where(["ipe_q1" => 1])->all();
		$ScrModel = Screening::find()->where(["mem_scrn_q28" => 1])->all();		
      
        $model = Attandance::find()->where(["visit_date" => strtotime(date("Y/m/d"))])->andWhere(["IS", "out_interviewer", NULL])->andWhere(["IN", "pid", ArrayHelper::getColumn($ScrModel, "mem_scrn_part_id")])->andWhere(["NOT IN", "pid", ArrayHelper::getColumn($Model, "ipe_pid")])->orderBy(["pid" => SORT_ASC])->all();
        $PIDs = ArrayHelper::map($model, 'pid', 'pid');
        return $PIDs;
    }
	
	public static function getIncARTRefEarnedPIDs()
    {
       
        $Model = Allincvoucherreport::find()->where(["inctype" => "ART Refill Incentive"])->orderBy(["pid" => SORT_ASC])->all();
		$PIDs =ArrayHelper::map($Model, 'pid', 'pid');
        return $PIDs;
        
    }
	
	public static function getReimbursePIDs()
    {   	
       	$attntable=Attandance::tableName();	
		$scrtable = Screening::tableName();	
    	$reimbursetable = Reimbursementmaster::tableName();
		$regtable = Registration::tableName();				  
		
		$model = Attandance::find()->where(["=", "record_date", strtotime(date("Y/m/d"))])->andWhere(["<=", "visit", 1])->andWhere(["IS", "visit_out", NULL])->andWhere("pid IN (SELECT corfrm_pid FROM $regtable WHERE corfrm_q13 <> 3)")->andWhere("pid IN (SELECT mem_scrn_part_id FROM $scrtable WHERE mem_scrn_q25 = 1)")->andWhere("pid Not IN (SELECT reimburse_master_part_id FROM $reimbursetable where $attntable.pid=reimburse_master_part_id and $attntable.visit=reimburse_master_visitcode)")->orderBy(["pid" => SORT_ASC])->all();             
		$PIDs = ArrayHelper::map($model, 'pid', 'pid');
		return $PIDs;
    }
}