<?php

namespace RxConnexionBundle\Repository;

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

        $errors = array();

        /*$action = $patientChartPayment_form['action'];

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

            $patientChartPaymentExist = $this->getEntityManager()->getRepository('RxConnexionBundle:PatientChartPaymentEntity')
                ->createQueryBuilder('pcp')
                ->where('pcp.id != :id')
                ->andWhere('pcp.patientChart = :patientChart_id')
                ->andWhere('pcp.chartProcedure = :chartProcedure_id')
                ->setParameters(array(
                    'id' => $patientChartPayment_form['id'],
                    'patientChart_id' => $patientChartPayment_form['patient_chart'],
                    'chartProcedure_id' => base64_decode($patientChartPayment_form['chart_procedure'])
                ))
                ->getQuery()->getResult();

            if($patientChartPaymentExist)
                $errors[] = 'Procedure already exist.';
        }*/

        return $errors;
    }

    public function ajax_list(array $get){

        $columns = array(
            array('pcp.`payment_type`', 'pcp.`payment_type`'),
            array('pcp.`payment_date`', "DATE_FORMAT(pcp.`payment_date`, '%m/%d/%Y')", 'payment_date'),
            array('pcp.`or_no`', 'pcp.`or_no`'),
            array('pcp.`dr_no`', 'pcp.`dr_no`'),
            array('pcp.`barcode`', 'pcp.`barcode`'),
            array('pcp.`check_no`', 'pcp.`check_no`'),
            array('pcp.`check_date`', "DATE_FORMAT(pcp.`check_date`, '%m/%d/%Y')", 'check_date'),
            array('pcp.`amount`', 'pcp.`amount`'),
            array('pcp.`note`', 'pcp.`note`'),
            array('pcp.`id`', "pcp.`id`")
        );
        $asColumns = array();

        $select = "SELECT";
        $from = "FROM `patient_chart_payment` pcp";
        $joins = "";
        $sqlWhere = "WHERE pcp.`patient_chart_id` = :patient_chart_id";
        $sqlWhere .= " AND  pcp.`is_deleted` = 0";
        $groupBy = "";
        $orderBy = "";
        $limit = "";
        $stmtParams = array(
            'patient_chart_id' => base64_decode($get['patient_chart_id'])
        );

        foreach($columns as $key => $column) {
            $select .= ($key > 0 ? ', ' : ' ') . $column[1] . (isset($column[2]) ? ' AS ' . $column[2] : '');
        }

        /*
         * Ordering
         */
        foreach($get['columns'] as $key => $column) {
            if($column['orderable']=='true') {
                if(isSet($get['order'])) {
                    foreach($get['order'] as $order) {
                        if($order['column']==$key) {
                            $orderBy .= (!empty($orderBy) ? ', ' : 'ORDER BY ') . $columns[$key][0] . (!empty($order['dir']) ? ' ' . $order['dir'] : '');
                        }
                    }
                }
            }
        }

        /*
         * Filtering
         */
        if(isset($get['search']) && $get['search']['value'] != ''){
            $aLikes = array();
            foreach($get['columns'] as $key => $column) {
                if($column['searchable']=='true') {
                    $aLikes[] = $columns[$key][0] . ' LIKE :searchValue';
                }
            }
            foreach($asColumns as $asColumn) {
                $aLikes[] = $asColumn . ' LIKE :searchValue';
            }
            if(count($aLikes)) {
                $sqlWhere .= (!empty($sqlWhere) ? ' AND ' : 'WHERE ') . '(' . implode(' OR ', $aLikes) . ')';
                $stmtParams['searchValue'] = "%" . $get['search']['value'] . "%";
            }
        }

        /* Set Limit and Length */
        if(isset( $get['start'] ) && $get['length'] != '-1'){
            $limit = 'LIMIT ' . (int)$get['start'] . ',' . (int)$get['length'];
        }

        $sql = "$select $from $joins $sqlWhere $groupBy $orderBy";
        $query = $this->getEntityManager()->getConnection()->prepare($sql);
        $query->execute($stmtParams);
        $result_count = $query->fetchAll();

        $sql = "$select $from $joins $sqlWhere $groupBy $orderBy $limit";
        $query = $this->getEntityManager()->getConnection()->prepare($sql);
        $query->execute($stmtParams);
        $result = $query->fetchAll();

        return array(
            'results' => $result,
            'total' => count($result_count)
        );
    }

    //PENDING PAYMENT REPORT
    public function reportPendingPayment() {
        $sqlWhere = "";
        $stmtParams = array();

//        if (!is_null($params['dateFrom']) && $params['dateFrom'] !== 'null' && $params['dateFrom'] !== 'all' && !empty($params['dateFrom']) && !is_null($params['dateTo']) && $params['dateTo'] !== 'null' && $params['dateTo'] !== 'all' && !empty($params['dateTo'])) {
//            $sqlWhere .= (empty($sqlWhere) ? "WHERE " : " AND ") . "s.`schedule_date` BETWEEN :dateFrom AND :dateTo";
//            $stmtParams['dateFrom'] = date('Y-m-d 00:00:00', strtotime(str_replace('-',  '/', $params['dateFrom'])));
//            $stmtParams['dateTo'] = date('Y-m-d 23:59:59', strtotime(str_replace('-', '/', $params['dateTo'])));
//        }
//
//        if(isset($params['patient_id']) && !empty($params['patient_id']) && 'NULL' !== strtoupper($params['patient_id'])){
//            $sqlWhere .= (empty($sqlWhere) ? "WHERE " : " AND ") . "s.`patient_id` = :patient_id";
//            $stmtParams['patient_id'] = $params['patient_id'];
//        }
//
//        if(isset($params['provider_id']) && !empty($params['provider_id']) && 'NULL' !== strtoupper($params['provider_id'])){
//            $sqlWhere .= (empty($sqlWhere) ? "WHERE " : " AND ") . "s.`provider_id` = :provider_id";
//            $stmtParams['provider_id'] = $params['provider_id'];
//        }
//
//        if(isset($params['healthFacility_id']) && !empty($params['healthFacility_id']) && 'NULL' !== strtoupper($params['healthFacility_id'])){
//            $sqlWhere .= (empty($sqlWhere) ? "WHERE " : " AND ") . "s.`health_facility_id` = :healthFacility_id";
//            $stmtParams['healthFacility_id'] = $params['healthFacility_id'];
//        }
//
//        if(isset($params['status_id']) && 'ALL' !== strtoupper($params['status_id'])) {
//            $sqlWhere .= (empty($sqlWhere) ? "WHERE " : " AND ") . "s.`schedule_status_id` = :status_id";
//            $stmtParams['status_id'] = $params['status_id'];
//        }


        $sql = "
            SELECT 
                hf.`description` AS health_facility_desc,
                ct.`description` AS chart_type_desc,
                DATE_FORMAT(pc.`chart_date`, '%m/%d/%Y') AS chart_date,
                pr.`full_name` AS provider_fullName,
                cs.`description` AS chart_status_desc
            FROM `patient_chart` pc
            LEFT JOIN `health_facility` hf 
            ON pc.`health_facility_id` = hf.`id`
            LEFT JOIN `provider` pr 
            ON pc.`provider_id` = pr.`id`
            LEFT JOIN `chart_status` cs 
            ON pc.`chart_status_id` = cs.`id`
            LEFT JOIN `chart_type` ct 
            ON pc.`chart_type_id` = ct.`id`
            WHERE pc.`is_deleted` = 0
        ";

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

        $query->execute($stmtParams);

        $result = $query->fetchAll();

        return $result;
    }

    //SALES REPORT
    public function reportSales() {
        $sqlWhere = "";
        $stmtParams = array();

//        if (!is_null($params['dateFrom']) && $params['dateFrom'] !== 'null' && $params['dateFrom'] !== 'all' && !empty($params['dateFrom']) && !is_null($params['dateTo']) && $params['dateTo'] !== 'null' && $params['dateTo'] !== 'all' && !empty($params['dateTo'])) {
//            $sqlWhere .= (empty($sqlWhere) ? "WHERE " : " AND ") . "s.`schedule_date` BETWEEN :dateFrom AND :dateTo";
//            $stmtParams['dateFrom'] = date('Y-m-d 00:00:00', strtotime(str_replace('-',  '/', $params['dateFrom'])));
//            $stmtParams['dateTo'] = date('Y-m-d 23:59:59', strtotime(str_replace('-', '/', $params['dateTo'])));
//        }
//
//        if(isset($params['patient_id']) && !empty($params['patient_id']) && 'NULL' !== strtoupper($params['patient_id'])){
//            $sqlWhere .= (empty($sqlWhere) ? "WHERE " : " AND ") . "s.`patient_id` = :patient_id";
//            $stmtParams['patient_id'] = $params['patient_id'];
//        }
//
//        if(isset($params['provider_id']) && !empty($params['provider_id']) && 'NULL' !== strtoupper($params['provider_id'])){
//            $sqlWhere .= (empty($sqlWhere) ? "WHERE " : " AND ") . "s.`provider_id` = :provider_id";
//            $stmtParams['provider_id'] = $params['provider_id'];
//        }
//
//        if(isset($params['healthFacility_id']) && !empty($params['healthFacility_id']) && 'NULL' !== strtoupper($params['healthFacility_id'])){
//            $sqlWhere .= (empty($sqlWhere) ? "WHERE " : " AND ") . "s.`health_facility_id` = :healthFacility_id";
//            $stmtParams['healthFacility_id'] = $params['healthFacility_id'];
//        }
//
//        if(isset($params['status_id']) && 'ALL' !== strtoupper($params['status_id'])) {
//            $sqlWhere .= (empty($sqlWhere) ? "WHERE " : " AND ") . "s.`schedule_status_id` = :status_id";
//            $stmtParams['status_id'] = $params['status_id'];
//        }


        $sql = "
            SELECT 
                hf.`description` AS health_facility_desc,
                ct.`description` AS chart_type_desc,
                DATE_FORMAT(pc.`chart_date`, '%m/%d/%Y') AS chart_date,
                pr.`full_name` AS provider_fullName,
                cs.`description` AS chart_status_desc
            FROM `patient_chart` pc
            LEFT JOIN `health_facility` hf 
            ON pc.`health_facility_id` = hf.`id`
            LEFT JOIN `provider` pr 
            ON pc.`provider_id` = pr.`id`
            LEFT JOIN `chart_status` cs 
            ON pc.`chart_status_id` = cs.`id`
            LEFT JOIN `chart_type` ct 
            ON pc.`chart_type_id` = ct.`id`
            WHERE pc.`is_deleted` = 0
        ";

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

        $query->execute($stmtParams);

        $result = $query->fetchAll();

        return $result;
    }

    /*public function autocomplete_suggestions($q) {

        $sql = "
            SELECT
                p.`id`,
                CONCAT_WS(' - ', p.`description`, a.`description`) AS data,
                CONCAT_WS(' - ', p.`description`, a.`description`) AS value
            FROM `chart_procedure` p
            LEFT JOIN `account` a ON p.`account_id` = a.`id`
            WHERE CONCAT_WS(' - ', p.`description`, a.`description`) LIKE :description
            ORDER BY p.`description`
            LIMIT 0,20
        ";

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

        $query->execute(array(
            'description' => "%$q%"
        ));

        $result = $query->fetchAll();

        return $result;
    }*/
}
