<?php

namespace app\models;

use Yii;
use yii\helpers\Html;
use app\base\Converter;
use rmrevin\yii\fontawesome\FA;
use yii\behaviors\TimestampBehavior;
use app\models\Screening;
use app\models\Registration;

/**
 * This is the model class for table "v_visitstatus".
 *
 * @property string $pid
 * @property integer $demographics_pid
 * @property integer $quality_of_life_pid
 * @property integer $hiv_continuum_pid
 * @property integer $pre_art_beliefs_pid
 * @property integer $post_art_beliefs_pid
 * @property integer $substance_use_pid
 * @property integer $risk_behavior_pid
 * @property integer $depression_pid
 * @property integer $health_care_utilization_pid
 * @property integer $hiv_visit_pid
 * @property integer $cd4_test_pid
 */
class Allincvoucherreport extends \yii\db\ActiveRecord
{
	/**
	 * @inheritdoc
	 */
    public static function tableName()
	{
		return 'v_allinc_report';
	}

	/**
	 * @inheritdoc
	 */
	public function behaviors()
	{
		return [
			[
				'class' => TimestampBehavior::className(),
				'createdAtAttribute' => 'create_time',
				'updatedAtAttribute' => 'update_time',
				'value' => date('U'),
			]
		];
	}

	/**
	 * @inheritdoc
	 */
	public function rules()
	{
		return [
			[['pid'], 'required'],           
			[['pid','survey'], 'string', 'max' => 11],		
			[['visitdate', 'paymentstatus', 'amount','totalEarned','totalpaidtillyesterday','totalpaidtoday','balance', 'paiddate', 'create_time', 'create_user', 'update_time', 'update_user', 'record_date'], 'integer'],		
			[['voucherno'], 'string', 'max' => 50],
			[['inctype'], 'string', 'max' => 100],
			[['status'], 'string', 'max' => 1],
		];
	}

	/**
	 * @inheritdoc
	 */
	public function attributeLabels()
	{
		return [
			'id' => Yii::t('app', 'ID'),
			'survey' => Yii::t('app', 'Survey'),
			'pid' => Yii::t('app', 'ICC Client ID'),
			'visitdate' => Yii::t('app', 'Visit Date'),
			'paymentstatus' => Yii::t('app', 'Payment Status'),
			'amount' => Yii::t('app', 'Amount'),
			'totalEarned' => Yii::t('app', 'Total Earned'),
			'paiddate' => Yii::t('app', 'Paid Date'),
			'voucherno' => Yii::t('app', 'Voucher Number'),
			'inctype' => Yii::t('app', 'Incentive Type'),			
			'status' => Yii::t('app', 'Status'),
			'create_time' => Yii::t('app', 'Create Time'),
			'create_user' => Yii::t('app', 'Create User'),
			'update_time' => Yii::t('app', 'Update Time'),
			'update_user' => Yii::t('app', 'Update User'),
			'record_date' => Yii::t('app', 'Record Date'),
		];
	}
	
	public function getScrdate($pid)
	{		
	  $model = Screening::find()->where(["mem_scrn_part_id" => $pid])->orderBy(['record_date' => SORT_DESC])->one();
	   if ($model !== null && $model->record_date !== null) {
	   if(!is_integer($model->record_date))
		$date = Converter::toStore($model->record_date);
       else
		$date = $model->record_date;
	
	   return $date;	
		
	 }
	}
	
	public function getRegdate($pid)
	{		
	  $model = Registration::find()->where(["corfrm_pid" => $pid])->orderBy(['record_date' => SORT_DESC])->one();
	   if ($model !== null && $model->record_date !== null) {
	   if(!is_integer($model->record_date))
		$date = Converter::toStore($model->record_date);
       else
		$date = $model->record_date;
	
	   return $date;	
		
	 }
	}
	
/*
	public function getInc($pid, $dt, $type = "")
	{
		if($type == "prtart")
			$Gmodel = Allincvoucherreport::find()->where(["pid" => $pid])->andWhere(["=","inctype",'Pre-ART Incentive'])->andWhere(["<=","record_date",$dt])->sum("amount");
		elseif($type == "artinit")
			$Gmodel = Allincvoucherreport::find()->where(["pid" => $pid])->andWhere(["=","inctype",'ART Initiation Incentive'])->andWhere(["<=","record_date",$dt])->sum("amount");
		elseif($type == "artref")
			$Gmodel = Allincvoucherreport::find()->where(["pid" => $pid])->andWhere(["=","inctype",'ART Refill Incentive'])->andWhere(["<=","record_date",$dt])->sum("amount");
		elseif($type == "mot")
			$Gmodel = Allincvoucherreport::find()->where(["pid" => $pid])->andWhere(["=","inctype",'Motivational Counseling'])->andWhere(["<=","record_date",$dt])->sum("amount");
		if($Gmodel !== null) {
			return $Gmodel;
		}
		else
			return "-";
	}

	*/
	
	public function getInc($pid, $type = "")
	{
		if($type == "prtart")
			$Gmodel = Allincvoucherreport::find()->where(["pid" => $pid])->andWhere(["=","inctype",'Pre-ART Incentive'])->sum("amount");
		elseif($type == "artinit")
			$Gmodel = Allincvoucherreport::find()->where(["pid" => $pid])->andWhere(["=","inctype",'ART Initiation Incentive'])->sum("amount");
		elseif($type == "artref")
			$Gmodel = Allincvoucherreport::find()->where(["pid" => $pid])->andWhere(["=","inctype",'ART Refill Incentive'])->sum("amount");
		elseif($type == "mot")
			$Gmodel = Allincvoucherreport::find()->where(["pid" => $pid])->andWhere(["=","inctype",'Motivational Counseling Incentive'])->sum("amount");
		if($Gmodel !== null) {
			return $Gmodel;
		}
		else
			return "-";
	}
    
    
}
