<?php

/*
 * Author       : Jai K
 * Purpose      : Model for Billing Prasadam
 * Created On   : 2016-08-12 12:13
 */

class Prasadam_Model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function get($tbl_name, $where = array(), $args = array()) {
        if (!is_array($where)) {
            $where = array();
        }
        $where['status'] = 1;
        if ($tbl_name == 'thiru_payment_types') :
            $where['menu_id'] = 0;
        endif;

        if (!empty($args['order_by'])) :
            $this->db->order_by($args['order_by']);
        endif;

        $q = $this->db->get_where($tbl_name, $where);
        return $q->result_array();
    }

    function get_all($id = '', $where = array()) {
        if (!is_array($where)) {
            $where = array();
        }
        if (!empty($id)) :
            $where['self.id'] = $id;
        endif;

        #$where['self.status'] = 1;
        $where['self.bp_book_status'] = 1;

        $this->db->select('self.*, self.status AS status, serv.name AS service_name, serv.amount as ser_amount, serv.is_deity, star1.name AS star_name_1, star2.name AS star_name_2, star1.tamil_name AS star_tamil_name1, star2.tamil_name AS star_tamil_name2, serv.prefix');

        $this->db->join(TBL_SERVICE . ' AS serv', 'serv.id = self.service_id', 'left');
        $this->db->join(TBL_STAR . ' AS star1', 'star1.id = self.star_id_1', 'left');
        $this->db->join(TBL_STAR . ' AS star2', 'star2.id = self.star_id_2', 'left');

        $this->db->order_by('self.bp_rec_no', 'desc');

        $q = $this->db->get_where(TBL_BP . ' AS self', $where);
        return $q->result_array();
    }

    function get_all_booked_details($id = '', $where = array()) {
        if (!is_array($where)) {
            $where = array();
        }
        if (!empty($id)) :
            $where['self.id'] = $id;
        endif;

        #$where['self.status'] = 1;

        $this->db->select('self.*, self.status AS status, serv.name AS service_name, serv.amount as ser_amount, serv.is_deity, star1.name AS star_name_1, star2.name AS star_name_2, star1.tamil_name AS star_tamil_name1, star2.tamil_name AS star_tamil_name2, serv.prefix');

        $this->db->join(TBL_SERVICE . ' AS serv', 'serv.id = self.service_id', 'left');
        $this->db->join(TBL_STAR . ' AS star1', 'star1.id = self.star_id_1', 'left');
        $this->db->join(TBL_STAR . ' AS star2', 'star2.id = self.star_id_2', 'left');

        $this->db->order_by('self.id', 'desc');

        $q = $this->db->get_where(TBL_BP . ' AS self', $where);
        return $q->result_array();
    }

    function get_items($bp_id) {
        $where['self.bp_id'] = $bp_id;
        $where['self.status'] = 1;

        $this->db->select('self.*, self.id AS id, self.status AS status, god.name AS god_name, pras.name AS prasadam_name');

        $this->db->join(TBL_GOD_NAME . ' AS god', 'god.id = self.god_id', 'left');
        $this->db->join(TBL_PRASADAM . ' AS pras', 'pras.id = self.prasadam_id', 'left');
        $q = $this->db->get_where(TBL_BPI . ' AS self', $where);
        return $q->result_array();
    }

    function get_payments($bp_id) {
        $where['self.pay_ref_type'] = 15; // Billing Prasadam
        $where['self.pay_ref_id'] = $bp_id;
        $where['self.status'] = 1;

        $this->db->join(TBL_PAY_TYPE . ' AS ptype', 'ptype.id = self.pay_type_id');
        $q = $this->db->get_where(TBL_PAY . ' AS self', $where);
        return $q->result_array();
    }

    public function get_by_id($id) {
        $this->db->select('bp.*, serv.is_deity', FALSE);
        $this->db->where('bp.id', $id);
        $this->db->join(TBL_SERVICE . ' AS serv', 'serv.id = bp.service_id', 'left');
        return $this->db->get(TBL_BP . ' as bp')->row();
    }

    function manipulate() {
        $gods = array();
        $prasadams = array();
        $print_body = array();
        $print_head = array();
        $post = $this->input->post(); #echo '<pre>';print_r($post);exit;
        $date_now = date("Y-m-d H:i:s");
        $session_data = $this->session->userdata(TBL_PREFIX . 'user_logged_in');
        $segments = $this->uri->segments;
        $page = $segments[2];
        $is_add = $page == 'add' || $page == 'insert' ? TRUE : FALSE;
        $db_id = $is_add ? '' : $segments[3];
        $service_id = $is_add ? $segments[3] : '';

        /* if(!empty($post['hidden_id'])) {
          $prasadam_data = $this->get_by_id($post['hidden_id']);
          } */

        //echo "<pre>Data: "; print_r( $post ); die;
        $gst_details = $this->get_amount_by_service_id($post['fn_type']);

        if ($gst_details->gst == 1) {
            $gst = get_general_setting()->gst;
            $gst_amount = ($gst / 100) * ($post['amount']);
        } else {
            $gst = '0.00';
            $gst_amount = '0.00';
        }

        $q = $this->db->get_where(TBL_SERVICE, array('id' => $post['fn_type']));
        $service = $q->row_array();

        $q = $this->db->get_where(TBL_SERVICE_MENU, array('id' => $service['menu_name']));
        $main_service = $q->row_array();

        //$total_amt = 0;
        $e = explode(( strpos($post['fn_date'], '/') > 0 ? '/' : '-'), $post['fn_date']); // dd-mm-yyyy
        $fn_date = $e[2] . '-' . $e[1] . '-' . $e[0];

        //print_r($post['pr_deity']);    echo "<br>";
        $gods_list = array_unique($post['pr_deity']); // print_r($gods_list); die();
        $paydets = $post['pay_type'];
        foreach ($gods_list as $god) { //--------------
            $args = array(
                'service_id' => $post['fn_type'],
                'star_id_1' => !empty($post['star_1']) ? $post['star_1'] : '0',
                'star_id_2' => !empty($post['star_2']) ? $post['star_2'] : '0',
                'fn_date' => date('Y-m-d H:i:s', strtotime($fn_date)),
                'bp_fn_date' => $fn_date,
                'bp_period' => $post['fn_period'],
                'bp_dev_name_1' => $post['dev_name_1'],
                'bp_dev_name_2' => $post['dev_name_2'],
                'bp_tamil_name_1' => htmlspecialchars($post['tamil_name_1']),
                'bp_tamil_name_2' => htmlspecialchars($post['tamil_name_2']),
                'bp_contact_no' => $post['contact_no'],
                'bp_email' => $post['email'],
                /* 'bp_address_1'      => $post['address_1'],
                  'bp_address_2'      => $post['address_2'], */
                'bp_desc' => $post['desc'],
                'gst' => $gst,
                'gst_value' => $gst_amount,
                'bp_book_status' => !empty($post['book-now']) ? 2 : 1,
            );

            $bk_status 	= $args['bp_book_status'];
            $receipt_no = $post['rec_no'];
            if (empty($receipt_no) || (!$is_add && $bk_status == "1" && $post['book_status'] == "2" )) :

                /*
                 * Get last Receipt ID
                 */
                $wh = array(
                    'service_id' => $service['id'],
                    'bp_book_status' => $bk_status
                );

                $receipt_no = generate_rec_no(TBL_BP, $wh);
                $args['bp_rec_no'] = $receipt_no;
            endif;


            if ($is_add) :
                // Insert

                $args['created_on'] = $date_now;
                $args['created_by'] = $session_data['user_id'];

                if ($bk_status == "2") :
                    // Booked Now
                    $args['updated_on'] = $date_now;
                    $args['updated_by'] = $session_data['user_id'];
                endif;

                $this->db->insert(TBL_BP, $args);
                $db_id = $this->db->insert_id();

            elseif (!empty($db_id)):
                // Update
                $args['updated_on'] = $date_now;
                $args['updated_by'] = $session_data['user_id'];

                $this->db->update(TBL_BP, $args, array('id' => $db_id));

            endif;


            $prsaDetail = $this->get_by_id($db_id);
            /*
             * Add Service Amount
             */
            //$total_amt += $service['amount'];

            /*
             * Items
             */
            // Set status for removed Items
            if (!empty($post['removed-items'])) :

                $e = explode("-", trim($post['removed-items'], '-'));
                foreach ($e as $k => $rm_id) :
                    $this->db->update(TBL_BPI, array('status' => 2), array('id' => $rm_id));
                endforeach;

            endif;
            $it_tot = 0.00;
            foreach ($post['pr_deity'] as $k => $god_id) :
                if ($god_id == $god) {
                    $sub_total = $post['pr_qty'][$k] * $post['pr_price'][$k];
                    //$total_amt += $sub_total;

                    $args = array(
                        'god_id' => $post['pr_deity'][$k],
                        'prasadam_id' => $post['pr_prasadam'][$k],
                        'bpi_qty' => $post['pr_qty'][$k],
                        'bpi_price' => $post['pr_price'][$k],
                        'bpi_sub_total' => $sub_total,
                    );
                    $it_tot += $sub_total;

                    if (!array_key_exists($args['god_id'], $gods)) :
                        $q = $this->db->get_where(TBL_GOD_NAME, array('id' => $args['god_id']));
                        $gods[$args['god_id']] = $q->row_array();
                    endif;

                    if (!array_key_exists($args['prasadam_id'], $prasadams)) :
                        $q = $this->db->get_where(TBL_PRASADAM, array('id' => $args['prasadam_id']));
                        $prasadams[$args['prasadam_id']] = $q->row_array();
                    endif;

                    if (empty($post['pr_id'][$k])) :
                        // Insert
                        $args['bp_id'] = $db_id;
                        $args['created_on'] = $date_now;
                        $args['created_by'] = $session_data['user_id'];

                        $this->db->insert(TBL_BPI, $args);

                    else :
                        // Update

                        $args['updated_on'] = $date_now;
                        $args['updated_by'] = $session_data['user_id'];

                        $this->db->update(TBL_BPI, $args, array('id' => $post['pr_id'][$k], 'bp_id' => $db_id));

                    endif;
                }
            endforeach;

            if (empty(($post['book-now'])) && ($is_add || (!empty($db_id) && ($post['hidden_amount'] != $post['amount'] || $post['book_status'] == 2)))) {				

                $pprasadam = $this->prasadam_model->get_by_id($db_id);
                $pprasadam_details = $this->prasadam_model->get_items_id($db_id);

                $gods = $prasadams = $print_body = $print_head = array();

                if ($prsaDetail->is_deity == 1) : // is Deity
                    foreach ($pprasadam_details as $k => $details) :
                        $args = array();
                        if (!array_key_exists($details['god_id'], $gods)) :
                            $q = $this->db->get_where(TBL_GOD_NAME, array('id' => $details['god_id']));
                            $gods[$details['god_id']] = $q->row_array();
                        endif;

                        if (!array_key_exists($details['prasadam_id'], $prasadams)) :
                            $q = $this->db->get_where(TBL_PRASADAM, array('id' => $details['prasadam_id']));
                            $prasadams[$details['prasadam_id']] = $q->row_array();
                        endif;

                        $print_body[$details['god_id']][] = array(
                            ((!empty($gods[$details['god_id']]['name'])) ? $gods[$details['god_id']]['name'] : ' - ') . ((!empty($gods[$details['god_id']]['tamil_name'])) ? ' / <span class="linear-lbl lang-tamil">' . $gods[$details['god_id']]['tamil_name'] . '</span>' : ''),
                            $prasadams[$details['prasadam_id']]['name'] . ((!empty($prasadams[$details['prasadam_id']]['tamil_name'])) ? ' / <span class="linear-lbl lang-tamil">' . $prasadams[$details['prasadam_id']]['tamil_name'] . '</span>' : ''),
                            '<div style="text-align: right;">' . $details['bpi_qty'] . ' x ' . $details['bpi_price'] . '</div>',
                            '<div style="text-align: right;">' . number_format($details['bpi_qty'] * $details['bpi_price'], 2) . '</div>',
                        );

                        $print_head = array('God Name<div class="sub-lbl">கடவுள் பெயர்<div>', 'Prasadam Name<div class="sub-lbl">பிரசாதம்<div>', 'Qty x Unit Price($)<div class="sub-lbl">எண்ணிக்கை x ஒன்றின் விலை<div>', 'Amount($)<div class="sub-lbl">தொகை<div>');


                    endforeach;


                else :  // No Deity
                    $args = array();
                    foreach ($pprasadam_details as $k => $details) :
                        if (!array_key_exists($details['god_id'], $gods)) :
                            $q = $this->db->get_where(TBL_GOD_NAME, array('id' => $details['god_id']));
                            $gods[$details['god_id']] = $q->row_array();
                        endif;

                        if (!array_key_exists($details['prasadam_id'], $prasadams)) :
                            $q = $this->db->get_where(TBL_PRASADAM, array('id' => $details['prasadam_id']));
                            $prasadams[$details['prasadam_id']] = $q->row_array();
                        endif;


                        $print_body['single'][$k] = array(
                            $prasadams[$details['prasadam_id']]['name'] . ((!empty($prasadams[$details['prasadam_id']]['tamil_name'])) ? ' / <span class="linear-lbl lang-tamil">' . $prasadams[$details['prasadam_id']]['tamil_name'] . '</span>' : ''),
                            '<div style="text-align: right;">' . $details['bpi_qty'] . ' x ' . $details['bpi_price'] . '</div>',
                            '<div style="text-align: right;">' . number_format($details['bpi_qty'] * $details['bpi_price'], 2) . '</div>',
                        );

                        $print_head = array('Prasadam Name<div class="sub-lbl">பிரசாதம்<div>', 'Qty x Unit Price($)<div class="sub-lbl">எண்ணிக்கை x ஒன்றின் விலை<div>', 'Amount($)<div class="sub-lbl">தொகை<div>');
                    endforeach;
                endif;
            }

            /*
             * Get last Receipt ID
             */
            /* $wh = array(
              'service_id'    => $service['id'],
              'ba_book_status'=> $bk_status
              );

              $this->db->select('COUNT(id) AS last_count');
              $q = $this->db->get_where( TBL_BP, $wh );
              $rec_id = $q->row_array()['last_count'];
              $receipt_no = (($last_no + 1)  > 9999) ? ($last_no + 1)  : sprintf('0%03d', ($last_no + 1) ); */

            /* $services = $this->service_type_exists($post['fn_type']);
              if($services->num_rows() > 0) {
              $last_receipt_no = $services->row()->bp_rec_no;
              $last_no = (int)$last_receipt_no;
              $receipt_no = (($last_no + 1)  > 9999) ? ($last_no + 1)  : sprintf('0%03d', ($last_no + 1) );
              } else
              $receipt_no = '0001'; */


            $args = array(
                'bp_amount' => $it_tot,
                'bp_paid_amount' => $it_tot,
                'bp_rec_no' => $receipt_no,
                'bp_pay_status' => 3, //$post['amount'] >= $total_amt ? '3' : ( $paid_amt > 0 ? '2' : '1' ), // Always FULL payment
            );
            $print_amt = $it_tot;
            $this->db->update(TBL_BP, $args, array('id' => $db_id));

            $Prasadam = $this->get_by_id($db_id);
            /*
             * Payments
             */

            // Drop all previous payments
            #$this->db->delete( TBL_PAY, array( 'pay_ref_type'  => 15, 'pay_ref_id'    => $db_id ) );

            if (empty(( $post['book-now'])) && ($is_add || (!empty($db_id)))) {
                $paid_hidden_id = $this->input->post('paid_hidden_id');
				//echo "HERE <pre>"; print_r($paid_hidden_id ); echo $paid_hidden_id; exit;
                $this->db->select('created_on');
                $this->db->where_in("id", $paid_hidden_id);
                $results = $this->db->get("thiru_payments")->row();
                if (!empty($results)) {
                    $date_now = $results->created_on;
                } else {
                    $date_now = $date_now;
                }
                if (!empty($paid_hidden_id)) :

                    $paidType = $this->delete_payment_by_paid_id($paid_hidden_id);
                endif;
                // print_r($post['pay_type']); die();
                $pri = 0;
                $prs[$db_id] = -1;
                foreach ($paydets as $pay_type_id => $pay_amt) :
                    //print_r($paydets); echo "###";
                    if (empty($pay_amt)) :
                        // Skip empty amounts ..
                        continue;
                    endif;

                    $this->db->select('slug');
                    $q = $this->db->get_where(TBL_PAY_TYPE, array('id' => $pay_type_id));
                    $prow = $q->row_array();

                    $pay_details = array();
                    if ($prow['slug'] == 'cheque' && !empty($pay_amt)) :
                        // Cheque
                        $e = explode('-', $post['chq_date']);
                        $pay_details = array(
                            'name' => $post['chq_bnk_name'],
                            'no' => $post['chq_no'],
                            'date' => $e[2] . '-' . $e[1] . '-' . $e[0],
                        );
                    endif;
                    //echo $pay_amt; echo "--"; echo $it_tot; echo "--";
                    $pay_details = json_encode($pay_details);
                    //echo $it_tot."-".$pay_amt; echo "-";
                    if ($pay_amt >= $it_tot) {
                        $paymode_amt = $it_tot;
                        $prs[$db_id] = 0;
                        $paydets[$pay_type_id] = $pay_amt - $it_tot;
                        $it_tot = 0.00;
                    } else { // echo "else"; echo "--";
                        if ($it_tot > 0) {
                            $paymode_amt = $pay_amt;
                            $paydets[$pay_type_id] = $paydets[$pay_type_id] - $paymode_amt;
                            if ($prs[$db_id] == 0) {
                                $prs[$db_id] = 0;
                            } else {
                                $prs[$db_id] = 1;
                            }
                            $it_tot = $it_tot - $pay_amt;
                        }
                    }

                    // if(($prs[$db_id] == 0) and ($db_id == $pri)){ $paymode_amt = 0.00; }
                    //if($it_tot == 0){  $paymode_amt = 0.00; }
                    //echo "<pre>";echo  $paymode_amt; echo "--";
                    // print_r($paydets); echo "=="; echo "P$".$it_tot; echo"__"; echo $paymode_amt;
                    if ($paymode_amt > 0) { //echo "TT";
                        $args = array(
                            'pay_ref_type' => 15, // For Billing Prasadam
                            'pay_ref_id' => $db_id, // Main ID
                            'pay_type_id' => $pay_type_id,
                            'pay_amount' => $paymode_amt, //$it_tot, //$pay_amt
                            'pay_details' => $pay_details,
                            'created_on' => $date_now, // (( empty( $receipt_no ) || ( !$is_add && $bk_status == "1"  && $post['book_status'] == "2" ) ) ? date('Y-m-d H:i:s') : $Prasadam->created_on),
                            'created_by' => $session_data['user_id'],
                        );
                        $this->db->insert(TBL_PAY, $args);
                    }
                    $pri = $db_id;

                    //print_r($paydets); echo "<br>-----<br>";
                    // Insert
                    //echo "/////". $this->db->last_query()."/////"; echo "<br>";


                endforeach;
            }

            if (count($print_body) > 0) {
                if (empty(( $post['book-now'])) && ($is_add || (!empty($db_id) && ($post['hidden_amount'] != $post['amount'] || $post['book_status'] == 2)))) {
                    /*                     * * To Print Receipt - Start ** */
                    $prefix = get_general_setting()->prefix;
					
				

					 $this->db->where('id',$post['star_1']);
                     $str1 = $this->db->get(TBL_STAR)->row();
					// $star1 = $str1->name.' / '.$str1->tamil_name;
					 $star1 = $str1->tamil_name;
					
                    $this->db->where('id',$post['star_2']);
                    $str2 = $this->db->get(TBL_STAR)->row();
					//$star2 = $str2->name.' / '.$str2->tamil_name;
					$star2 = $str2->tamil_name;
					

                    foreach ($print_body as $z => $body) :
					
					if(!empty($post['dev_name_1'])){ $sls= ' / '; }else{ $sls=''; }
							  if(!empty($post['dev_name_2'])){ $sls2= ' / '; }else{ $sls2=''; }

                        $args = array(
                            'type' => '10', // Default = 1 // Normal (Double print), '2' => Archanai,   '3' = Hall Booking | Wedding Hall
                            'rec_no' => (($prefix == 1) ? $gst_details->prefix : '') . $receipt_no,
                            'pay_ref_id' => $db_id,
                            'title' => 'Prasadam - ' . $gst_details->name,
                            'amount' => array($print_amt),
                            'gst' => $gst_details->gst,
                            'copy' => FALSE,
                            'status' => $prsaDetail->status,
                            'created_on' => $prsaDetail->created_on,
							'pay_type'	=> $post['pay_type'],
							'pay_ref_type' => 15,
                            /* 'data'      => array(
                              // Type = 1 && 3
                              'name'      => array($post['dev_name_1'], $post['tamil_name_1']),array($post['dev_name_1'], $post['tamil_name_1'], $post['dev_name_2'], $post['tamil_name_2']),
                              'contact_no'=> $post['contact_no'],
                              'email'     => $post['email'],
                              'address'   => $post['address_1'],
                              'desc'      => $post['desc'],
                              ), */
							  
                            'data' => array(
                                // Type = 6
                                'fields' => array(
                                    'Name<div class="sub-lbl">பெயர்<div>' => $post['dev_name_1'] . ((!empty($post['tamil_name_1'])) ? $sls.' <span class="linear-lbl lang-tamil">' . htmlspecialchars($post['tamil_name_1']) . '</span>' : '') . ' ' . $star1, // . tamil( $post['tamil_name_1'], 'border: none; font-weight: bold; font-size: 24px;' ),
                                    /* 'Tamil Name<div class="sub-lbl">தமிழ் பெயர்<div>'    => tamil( $post['tamil_name_1'], 'border: none; font-weight: bold; font-size: 24px;' ), */
                                    '' => $post['dev_name_2'] . ((!empty($post['tamil_name_2'])) ? $sls2.' <span class="linear-lbl lang-tamil">' . htmlspecialchars($post['tamil_name_2']) . '</span>' : ''). ' ' . $star2,
                                    'Date<div class="sub-lbl">தேதி<div>' => str_replace('-', '/', $post['fn_date']),
                                    'Period<div class="sub-lbl">காலம்<div>' => $post['fn_period'] == '1' ? 'Morning  / காலை' : 'Evening / மாலை',
                                    'Contact No<div class="sub-lbl">தொடர்பு எண்<div>' => $post['contact_no'],
                                    'Email ID<div class="sub-lbl">மின்னஞ்சல்<div>' => $post['email'],
                                ),
                                'tables' => array(
                                    'head' => $print_head,
                                    'body' => $body,
                                ),
                            ),
                        );

                        //echo "<pre>";
                        //print_r($args);
                        //die();

                        //print_rec_A4($args, ($z + 1));
                        $ppg = is_numeric($z) ? ($z + 1) : $z;
                        print_rec_A4($args, $ppg);
                        /*                         * * End ** */
                    endforeach;
                }
            }
        }
        //die();
    }

    function delete($id, $hidden_del_reason) {

        $args = array(
            'pay_amount' => 0, // Deleted
        );

        $this->db->update(TBL_PAY, $args, array('pay_ref_type' => 15, 'pay_ref_id' => $id));
        $this->db->update(TBL_REFUND, array('status' => 2), array('menu_id' => 15, 'auto_id' => $id));

        $update = array(
            'bpi_sub_total' => 0,
            'status' => 2
        );
        $this->db->update(TBL_BPI, $update, array('bp_id' => $id));

        $update = array(
            'bp_amount' => 0,
            'bp_paid_amount' => 0,
            'del_reason' => $hidden_del_reason,
            'status' => 2
        );
        $this->db->update(TBL_BP, $update, array('id' => $id));
    }

    public function insert_refund_amount($data) {
        $this->db->insert(TBL_REFUND, $data);
        return $this->db->insert_id();
    }

    function ajax() {
        $post = $this->input->post();

        switch ($post['act']) :
            case 'booked' :

                /*
                 * Get all booked details
                 */
                $where['self.bp_book_status'] = 2;
                $where['self.status'] = 1;

                if (!empty($post['fromDate'])) :
                    $e = explode("-", $post['fromDate']);
                    $where['DATE(self.bp_fn_date) >= '] = $e[2] . '-' . $e[1] . '-' . $e[0];
                endif;

                if (!empty($post['toDate'])) :
                    $e = explode("-", $post['toDate']);
                    $where['DATE(self.bp_fn_date) <= '] = $e[2] . '-' . $e[1] . '-' . $e[0];
                endif;

                if (!empty($post['period'])) :
                    $where['self.bp_period'] = $post['period'];
                endif;

                $results = $this->get_all_booked_details('', $where);

                break;

        endswitch;

        return $results;
    }

    public function service_type_exists($type) {
        $this->db->where('service_id', $type);
        $this->db->where('status !=', 2);
        $this->db->order_by('bp_rec_no', 'desc');
        return $this->db->get(TBL_BP);
    }

    public function get_amount_by_service_id($id) {
        $this->db->where('id', $id);
        return $this->db->get(TBL_SERVICE)->row();
    }

    public function get_refund_details_by_ids($menu_id, $service_id, $auto_id) {
        $this->db->select('SUM(amount) as refund_amount');
        $this->db->where('menu_id', $menu_id);
        $this->db->where('service_id', $service_id);
        $this->db->where('auto_id', $auto_id);
        $this->db->where('status !=', 2);
        return $this->db->get(TBL_REFUND)->row();
    }

    public function total_paid_amount($menu, $type) {
        $this->db->select('SUM(pay_amount) as paidAmount');
        $this->db->where('pay_ref_type', $menu);
        $this->db->where('pay_ref_id', $type);
        $this->db->where('status', 1);
        return $this->db->get(TBL_PAY)->row();
    }

    public function get_prasadam_price($id) {
        $this->db->where('id', $id);
        return $this->db->get(TBL_PRASADAM)->row();
    }

    function get_items_id($id) {
        $this->db->where('bp_id', $id);
        $this->db->where('status !=', 2);
        return $this->db->get(TBL_BPI)->result_array();
    }

    public function payment_mode_lists() {
		$this->db->select('id, name, img_url, slug');
        $this->db->where('status', 1);
        $this->db->where('menu_id', 0);
        return $this->db->get(TBL_PAY_TYPE)->result();
    }

    public function get_paid_type($menu, $type, $id) {
        $this->db->select('*,SUM(pay_amount) as pay_amount');
        $this->db->where('pay_ref_type', $menu);
        $this->db->where('pay_ref_id', $type);
        $this->db->where('pay_type_id', $id);
        $this->db->where('status', 1);
        $this->db->group_by('pay_type_id');
        return $this->db->get(TBL_PAY)->row();
    }

    public function get_paidID($menu, $type, $id) {
        $this->db->where('pay_ref_type', $menu);
        $this->db->where('pay_ref_id', $type);
        $this->db->where('pay_type_id', $id);
        $this->db->where('status', 1);
        return $this->db->get(TBL_PAY)->result();
		//echo $this->db->last_query();
    }

    public function delete_payment_by_paid_id($id) {
        $this->db->where_in('id', $id);
        $this->db->delete(TBL_PAY);
        return $this->db->affected_rows();
    }

    public function get_basic_details($contact_no) {
		$this->db->where('bp_contact_no <>', '');
        $this->db->where('bp_contact_no', $contact_no);
        $this->db->where('status', 1);
        $this->db->order_by('id', 'desc');
        $this->db->limit(1);
        return $this->db->get(TBL_BP)->row();
    }
	
	function get_star($stid){
	 $this->db->where('id',$stid);
	 return $this->db->get(TBL_STAR)->row();
	}
	
	function get_booked_details_by_id( $id= '', $where = array() ) {
        if ( ! is_array( $where ) ) {
            $where = array();
        }
        if( !empty( $id ) ) :
            $where['self.id'] = $id;
        endif;
        $this->db->select( 'self.*, self.status AS status' );
        $this->db->order_by('self.id','desc');        
        $q = $this->db->get_where( TBL_BP . ' AS self', $where );
        return $q->result_array();
    }
	
	function get_items_by_id( $bp_id ) {
        $where['self.bp_id'] 	= $bp_id;
        $where['self.status'] 	= 1;
        $this->db->select( 'self.*, self.id AS id, self.status AS status');
        $q = $this->db->get_where( TBL_BPI . ' AS self', $where );
        return $q->result_array();
    }
	
	public function get_service_by_id($id) {
        $this->db->where('id',$id);
        return $this->db->get(TBL_SERVICE)->row();
    }
}
