<?php

namespace RxConnexionBundle\Repository;

/**
 * PatientLetterNoteRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class PatientLetterNoteRepository extends \Doctrine\ORM\EntityRepository
{
    public function validate($patientLetterNote_form) {

        $errors = array();

        $action = $patientLetterNote_form['action'];

        // d = delete
        if($action !== 'd') {

//            $accountUserTypeExist = $this->getEntityManager()->getRepository('RxConnexionBundle:AccountUserTypeEntity')
//                ->createQueryBuilder('ep')
//                ->where('ep.id != :id')
//                ->andWhere('ep.isDeleted = 0')
//                ->andWhere('ep.description = :description')
//                ->andWhere('ep.account = :account')
//                ->setParameters(array(
//                    'id' => $accountUserType_form['id'],
//                    'description' => $accountUserType_form['description'],
//                    'account' => $accountUserType_form['account']
//                ))
//                ->getQuery()->getResult();
//
//            if($accountUserTypeExist)
//                $errors[] = 'Description already exist.';
//
            if(empty($patientLetterNote_form['note']))
                $errors[] = 'Note should not be blank.';
        }

        return $errors;
    }

    public function patientLetterNoteView($params) {
        $sqlWhere = "WHERE (pln.`is_deleted` = 0 OR pln.`is_deleted` IS NOT NULL)";

        //STMTPARAMS
        $stmtParams = array();
        $stmtParams['patientLetterId'] = $params['patientLetterId'];
        $stmtParams['created_by_userData'] = $params['username'];
        $stmtParams['userData'] = $params['username'];

        $sql = "SELECT
                  au_to.`full_name` AS auTo,
                  CONCAT('[ ' , DATE_FORMAT(pln.`created_at`, '%m/%d/%Y %h:%i %p'), ' ] : ', pln.`note`) AS note
                FROM `patient_letter_note` pln
                LEFT JOIN `account_user` au_to ON pln.`account_user_id` = au_to.`id`
                $sqlWhere
                AND (pln.`created_by` = :created_by_userData OR au_to.`username` = :userData)
                AND pln.`patient_letter_id` = :patientLetterId
";

        $query = $this->getEntityManager()->getConnection()->prepare($sql);
        $query->execute($stmtParams);

        $result = $query->fetchAll();

        return $result;
    }
}
