<?php
    
    /*
     * Author       : Jai K
     * Purpose      : Script for set payment created_on by main table created_on
     * Created On   : 2017-01-28 15:02
     */

class Script extends CI_Controller {
    
    function __construct() {
        parent::__construct();
    }
    
    function index(){
        
        
        switch( $_GET['act'] ) :
            case 'slug' :
                $this->fill_slug();
            break;
                
            
            case 'set_daily' :
                $this->set_daily_report();
            break;    
        
            default :
                die('Error: "act" command is empty! <br />Usage: <span style="color: #f33;">/script/index/?act=slug&id=90</span>');
            break;
        
        endswitch;
            
        //echo '<pre>Pay: '; print_r( $pays ); die;
        
        
    }
    
    function fill_slug(){
        $tbl_name = !empty( $_GET['table'] ) ? $_GET['table'] : TBL_CATEGORY;
        $clm_name = !empty( $_GET['table'] ) ? $_GET['table'] : 'category_name';  
        
        $q = $this->db->get_where( TBL_CATEGORY );
        $cats = $q->result_array();

        foreach( $cats as $k => $cat ) :
            $sql = 'UPDATE `'. TBL_CATEGORY .'` SET `cat_slug` = "'. get_var_name( $cat['category_name'] ) .'" WHERE `id` = '. $cat['id'].';';
            echo '<br />'. $sql;

        endforeach;

        echo '<br /><br />-- ----------------------------------------------------------- <br /><br />';

        $q = $this->db->get_where( TBL_ITEM );
        $res = $q->result_array();
        foreach( $res as $k => $r ) :
            $sql = 'UPDATE `'. TBL_ITEM .'` SET `item_slug` = "'. get_var_name( $r['name'] ) .'" WHERE `id` = '. $r['id'].';';
            echo '<br />'. $sql;

        endforeach;
       
        
    }
    
    function set_daily_report(){
        
        $whr = array(
            'status' => 1,
            'pay_ref_type !=' => 2,
        );
        
        $this->db->order_by('pay_ref_type');
        $this->db->select('id, pay_ref_type, pay_ref_id, created_on');
        $q = $this->db->get_where( TBL_PAY, $whr );
        $payments = $q->result_array();
        
        $pays = array();
        foreach( $payments as $k => $pay ) :
            $pays[$pay['pay_ref_type']][$pay['pay_ref_id']] = $pay;
        endforeach;
        
        $q = $this->db->get_where( TBL_SERVICE_MENU, array('status' => 1) );
        $services = $q->result_array();
        
        foreach( $services as $k => $r ) :
            $tbls[$r['id']] = $r['table_name'];
        endforeach;
        
        
        
        foreach( $pays as $pay_ref_type => $pay ) :
            
            $this->db->select('id, created_on');
            $this->db->where_in('id', array_keys($pay));
            $q = $this->db->get_where(TBL_PREFIX . $tbls[$pay_ref_type]);
            echo '<br>Q:'. $this->db->last_query();
            
            
            $res = $q->result_array();
            
            $a = array();
            foreach( $res as $k => $r ) :
                $a[date("Y-m-d", strtotime($r['created_on']))][$r['id']] = $r;
            endforeach;
            
            /*if( $pay_ref_type == '14' ) :
                echo "<pre>"; print_r( $a );
            endif;*/
            
            foreach( $a as $k => $r ) :
                $date = array_values($r);
            
                $this->db->where('pay_ref_type', $pay_ref_type);
                $this->db->where_in('pay_ref_id', array_keys($r));
                $q = $this->db->update(TBL_PAY, array('created_on' => $date[0]['created_on']));
                echo '<br>Pay:'. $this->db->last_query();
                
                
            endforeach;
            
            
        endforeach;
        
    }
    

    

}






