<?php
// include "system/library/logo.php";
ini_set('max_execution_time', 0);

class ControllerToolLogobarcodev2 extends Controller {
    public function index() {
        //****************
        exit;
        //***************** */
        /*
        $products = $this->db->query("SELECT DISTINCT
            p.product_id, 
            ps_product_to_category.category_id, 
            ps_category_description.`name`
        FROM
            ps_product p
            LEFT JOIN
            ps_product_to_category
            ON 
                p.product_id = ps_product_to_category.product_id
            LEFT JOIN
            ps_category
            ON 
                ps_category.category_id = ps_product_to_category.category_id
            LEFT JOIN
            ps_category_description
            ON 
                ps_category.category_id = ps_category_description.category_id
        WHERE
            ps_category.is_logo_material = 1
        GROUP BY p.product_id
        LIMIT 10000000000 OFFSET 1000000
        ")->rows;

        */

        $products = $this->db->query("SELECT DISTINCT
            p.product_id, 
            ps_product_to_category.category_id, 
            ps_category_description.name
        FROM
            ps_product p
            LEFT JOIN
            ps_product_to_category
            ON 
                p.product_id = ps_product_to_category.product_id
            LEFT JOIN
            ps_category
            ON 
                ps_category.category_id = ps_product_to_category.category_id
            LEFT JOIN
            ps_category_description
            ON 
                ps_category.category_id = ps_category_description.category_id
        WHERE
            ps_category.is_logo_material = 1 AND ps_category.category_id IN (4762, 4894) 
        GROUP BY p.product_id
            ")->rows;
        
        foreach($products as $product) {
            $category = $this->db->query("SELECT
                c.category_id,
                cd.`name`, 
	            c.infinityeditid
            FROM
                ps_product_to_category AS ptc
                INNER JOIN ps_category AS c ON ptc.category_id = c.category_id
                INNER JOIN ps_category_description AS cd ON c.category_id = cd.category_id 
            WHERE
                ptc.product_id = {$product['product_id']}   
                AND c.is_logo_material = 1
            ")->rows;

            if(count($category)!==1) {
                echo 'Logo için kategori sayısı uygun değil.:' . $product['product_id'] . '<br/>';
                continue;
            }

            $category = $category[0];

            
            $color = $this->db->query("SELECT ad.`name`, a.infinityeditid, a.attribute_id 
            FROM ps_product AS p 
            INNER JOIN ps_product_attribute AS pa ON p.product_id = pa.product_id
            INNER JOIN ps_attribute AS a ON pa.attribute_id = a.attribute_id
            INNER JOIN ps_attribute_description AS ad ON a.attribute_id = ad.attribute_id
            INNER JOIN ps_attribute_group AS ag ON a.attribute_group_id = ag.attribute_group_id 
            WHERE a.attribute_group_id = 13
            AND p.product_id = {$product['product_id']} 
            ")->row;

            if(empty($color)) {
                echo 'Renk bilgisi yok.:' . $product['product_id'] . '<br/>';
                continue;
            }

            $collection = $this->db->query("SELECT ad.`name`, a.infinityeditid, a.attribute_id 
                FROM ps_product AS p 
                INNER JOIN ps_product_attribute AS pa ON p.product_id = pa.product_id
                INNER JOIN ps_attribute AS a ON pa.attribute_id = a.attribute_id
                INNER JOIN ps_attribute_description AS ad ON a.attribute_id = ad.attribute_id
                INNER JOIN ps_attribute_group AS ag ON a.attribute_group_id = ag.attribute_group_id 
                WHERE a.attribute_group_id = 11                                                                                                                                                                                                                                                                      
                AND p.product_id = {$product['product_id']} 
            ")->row;

            if(empty($collection)) {
                echo 'Koleksiyon bilgisi yok.:' . $product['product_id'] . '<br/>';
                continue;
            }

            # Saat kordonlarında barkoda bedeni de ekler
            if($category['category_id'] == 4762 || $category['category_id'] == 4894) {
                
                //normalde satılan ürünün tek opsiyonu olur. şimdi materyal üretileceği için productun hepsini çektim
                $options = $this->db->query("SELECT
                    ovd.* 
                FROM
                    ps_product_option AS po
                    INNER JOIN ps_product_option_value AS pov ON po.product_option_id = pov.product_option_id
                    INNER JOIN ps_product AS p ON po.product_id = p.product_id
                    INNER JOIN ps_option_value AS ov ON pov.option_value_id = ov.option_value_id
                    INNER JOIN ps_option_value_description AS ovd ON ov.option_value_id = ovd.option_value_id 
                WHERE
                    p.product_id = {$product['product_id']}
                ")->rows;

                foreach($options as $option) {
                    $barcode_category = !empty($category['infinityeditid']) ? $category['infinityeditid'] : $category['category_id'];
                    $barcode_collection = !empty($collection['infinityeditid']) ? $collection['infinityeditid'] : $collection['attribute_id'];
                    $barcode_color = !empty($color['infinityeditid']) ? $color['infinityeditid'] : $color['attribute_id'];

                    $barcode = $barcode_category . $barcode_collection . $barcode_color . $option['option_value_id'];
        
                    $category_name = str_replace("Telefon Kılıfı", "", $category['name']);
                    $short_color_name = preg_replace('/('. $collection['name'] .'(?!.*'.$collection['name'].'))/', "", $color['name']); #renk adı koleksiyon adını içeriyorsa kısaltır.
        
                    // Kategori adı ve Koleksiyon adı aynıysa tekrara gerek yok. örn: kategori Kablo Koruyucular, koleksiyon Kablo Koruyucular olan ürünler var.
                    if($category_name == $collection['name']) {
                        $material_name = $category_name . ' ' . $short_color_name . ' ' . $option['name'];
                    } else {
                        $material_name = $category_name . ' ' . $collection['name'] . ' ' . $short_color_name . ' ' . $option['name'];
                    }
        
                    $material_name = preg_replace('/\s+/', ' ', $material_name); // birden çok boşluk yanyanaysa tek boşluk yapıp materyal adı oluşturur.
                    echo "{$barcode}  {$material_name} <br/>" ;
                }

            }

            /*
            $barcode_category = !empty($category['infinityeditid']) ? $category['infinityeditid'] : $category['category_id'];
            $barcode_collection = !empty($collection['infinityeditid']) ? $collection['infinityeditid'] : $collection['attribute_id'];
            $barcode_color = !empty($color['infinityeditid']) ? $color['infinityeditid'] : $color['attribute_id'];
            $barcode = $barcode_category . $barcode_collection . $barcode_color;

            $category_name = str_replace("Telefon Kılıfı", "", $category['name']);
            $short_color_name = preg_replace('/('. $collection['name'] .'(?!.*'.$collection['name'].'))/', "", $color['name']); #renk adı koleksiyon adını içeriyorsa kısaltır.

            // Kategori adı ve Koleksiyon adı aynıysa tekrara gerek yok. örn: kategori Kablo Koruyucular, koleksiyon Kablo Koruyucular olan ürünler var.
            if($category_name == $collection['name']) {
                $material_name = $category_name . ' ' . $short_color_name;
            } else {
                $material_name = $category_name . ' ' . $collection['name'] . ' ' . $short_color_name;
            }

            $material_name = preg_replace('/\s+/', ' ', $material_name); // birden çok boşluk yanyanaysa tek boşluk yapıp materyal adı oluşturur.
            //echo "{$barcode}  {$material_name} <br/>" ;
            
            $this->db->query("INSERT IGNORE INTO " . DB_PREFIX . "product_stock2 
                SET product_stock_id = '" . (int)$barcode . "', 
                    barcode = '" . (int)$barcode . "', 
                    name = '" . $material_name . "', 
                    category_id = '" . $category['category_id'] . "', 
                    category_name = '" . $category_name . "', 
                    collection_id = '" . $collection['attribute_id'] . "', 
                    collection_name = '" . $collection['name'] . "',
                    color_id = '" . $color['attribute_id'] . "', 
                    color_name = '" . $color['name'] . "',    
                    quantity = 0,
                    kquantity = 0,
                    price = 0,
                    price_cost = 0
                ");
            */
        }
    }
}