<?php
class ControllerToolFixReward extends Controller {
    public function index() {
		$query = $this->db->query("SELECT o.customer_id as customer_id, o.order_id, ot.value as value, o.order_status_id, o.order_id as order_id 
            FROM " . DB_PREFIX . "order o
			LEFT JOIN " . DB_PREFIX . "customer_reward ON o.order_id = " . DB_PREFIX . "customer_reward.order_id
            INNER JOIN " . DB_PREFIX . "order_total ot ON o.order_id = ot.order_id 
			WHERE o.payment_code = 'iyzico_checkout_form'
            AND ot.code = 'reward'
            AND o.order_status_id NOT IN (0,4,5,9,10)
            GROUP BY o.order_id
            ORDER BY o.order_id desc LIMIT 1"
        );
            
            //" . DB_PREFIX . "customer_reward bu tabloya ilgili kayıt eklencek öncesinde iligli sipariş idsinden - veya + değer olup olmadı kotrol edilcek "+45"  >=0 (kullanılmaya)
            foreach($query->rows as $item) {               
                //kazandığı puanın kontrolü
                $totals = $this->db->query(" SELECT * FROM " . DB_PREFIX . "order_total 
                    WHERE order_id = '" . (int)$item['order_id'] . "' 
                    ORDER BY sort_order"
                )->rows;

                $getTotal = 0;
                $reward_setting_status = $this->config->get('reward_setting_status');
        
                foreach ($totals as $total) {                    
                    if($total["code"] == "tax" ){
                        $getTaxTotals = $total["value"]; 
                    }
                    if($total["code"] == "sub_total" ){
                        $getTotals = $total["value"]; 
                    }
                }
        
                $getTotal = $getTaxTotals + $getTotals;
        
                $reward_total_last = 0;
        
                if($reward_setting_status){
                    $reward_setting_percent      = $this->config->get('reward_setting_percent');
                    $reward_total_last           = ( $getTotal * $reward_setting_percent) / 100;
                    
                //odul puanı kullanım ayarları
                
                    $order_info = $this->db->query("SELECT *, (SELECT CONCAT(c.firstname, ' ', c.lastname) 
                        FROM " . DB_PREFIX . "customer c 
                        WHERE c.customer_id = o.customer_id) AS customer, 
                        (SELECT os.name FROM " . DB_PREFIX . "order_status os WHERE os.order_status_id = o.order_status_id 
                        AND os.language_id = '" . (int)$this->config->get('config_language_id') . "') AS order_status 
                        FROM " . DB_PREFIX . "order o 
                        WHERE o.order_id = '" . (int)$item['order_id'] . "' "
                    )->row;
                    $order_info['reward'] = 0;
                    $order_product_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$item['order_id'] . "'");
                    foreach ($order_product_query->rows as $product) {
                        $order_info['reward'] += $product['reward'];
                    }

                    if ($order_info && $order_info['customer_id'] ) {
                        $reward_total= $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "customer_reward WHERE order_id = '" . (int)$item['order_id'] . "' AND points > 0")->row['total'];
                        $reward     = $order_info["reward"];
                        if($reward_setting_status){
                            $reward = $reward_total_last;
                        }  
                        if (!$reward_total) {
                            $this->db->query("INSERT INTO " . DB_PREFIX . "customer_reward SET 
                            customer_id = '" . (int)$order_info['customer_id'] . "', 
                            order_id = '" . (int)$item['order_id'] . "', 
                            points = '" . (float)$reward . "', 
                            description = 'Sipariş No: #" . (int)$item['order_id'] . "', 
                            date_added = NOW()");
                        }
                    }
                }else{
                            //odul puanı kullanım ayarları
                            $order_info = $this->db->query("SELECT *, (SELECT CONCAT(c.firstname, ' ', c.lastname) FROM " . DB_PREFIX . "customer c WHERE c.customer_id = o.customer_id) AS customer, 
                            (SELECT os.name FROM " . DB_PREFIX . "order_status os WHERE os.order_status_id = o.order_status_id AND os.language_id = '" . (int)$this->config->get('config_language_id') . "') AS order_status FROM " . DB_PREFIX . "order o WHERE o.order_id = '" . (int)$item['order_id'] . "' ")->row;
                            $order_info['reward'] = 0;
                            $order_product_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$item['order_id'] . "'");
                            foreach ($order_product_query->rows as $product) {
                                $order_info['reward'] += $product['reward'];
                            }
                            //$order_info = $this->model_sale_order->getOrder($item['order_id']);        
                            if ($order_info && $order_info['customer_id'] && ($order_info['reward'] > 0)) {                
                                $reward_total = $this->db->query("SELECT COUNT(*) AS total 
                                    FROM " . DB_PREFIX . "customer_reward 
                                    WHERE order_id = '" . (int)$item['order_id'] . "' 
                                    AND points > 0"
                                )->row['total'];
                                $reward = $order_info["reward"];
                                if($reward_setting_status){
                                    $reward = $reward_total_last;
                                }
                                if (!$reward_total) {
                                    $this->db->query("INSERT INTO " . DB_PREFIX . "customer_reward SET 
                                        customer_id = '" . (int)$order_info['customer_id'] . "', 
                                        order_id = '" . (int)$item['order_id'] . "', 
                                        points = '" . (float)$reward . "', 
                                        description = 'Sipariş No: #" . (int)$item['order_id'] . "', 
                                        date_added = NOW()"
                                    );
                                }
                            }
                }

                //harcadığı puanın kontrolü
                $sub_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer_reward 
                    WHERE order_id = " . $item['order_id'] . " 
                    AND points = " . $item['value']
                );
                if($sub_query->num_rows==0) {
                    $this->db->query("INSERT INTO " . DB_PREFIX . "customer_reward 
                        SET customer_id = '" . (int)$item['customer_id'] . "', 
                        order_id = '" . (int)$item['order_id'] . "', 
                        description = 'Sipariş No: #" . (int)$item['order_id'] . "', 
                        points = '" . $item['value'] . "', 
                        date_added = NOW()"
                    );
                }

                //eğer bir sipariş 2kere + puan eklenmiş veya 2 kere - puan eklenmişse birini sil.
                $gained_rewards = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer_reward WHERE order_id = " . $item['order_id'] . " AND points > 0");
                if($gained_rewards->num_rows==2) {
                    $this->db->query("DELETE FROM " . DB_PREFIX . "customer_reward WHERE order_id = " . $item['order_id'] . " AND points > 0 LIMIT 1");
                };

                $lost_rewards = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer_reward WHERE order_id = " . $item['order_id'] . " AND points < 0"); 
                if($lost_rewards->num_rows==2) {
                    $this->db->query("DELETE FROM " . DB_PREFIX . "customer_reward WHERE order_id = " . $item['order_id'] . " AND points < 0 LIMIT 1");
                };  
                
            }     
            
            $query2 = $this->db->query("
                SELECT * FROM " . DB_PREFIX . "customer_reward 
                WHERE points = 0"
            )->rows;

            foreach($query2 as $item) {
                $order_points = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer_reward WHERE order_id = " . $item['order_id'] . " AND points = 0"); 
                if($order_points->num_rows>0) {
                    $this->db->query("DELETE FROM " . DB_PREFIX . "customer_reward WHERE order_id = " . $item['order_id'] . " AND points = 0");
                };  
            }
    }
}
