<?php

// include "system/library/logo.php";



class ControllerToolLogoRebarcoding extends Controller {

    public function index() {

die();
        // $products = $this->db->query("SELECT * FROM ps_product GROUP BY product_stock_id")->rows;  

        $products = $this->db->query("SELECT

        *

    FROM

        ps_product

    WHERE

        ps_product.barcode NOT LIKE '%A%' AND

        ps_product.barcode NOT LIKE '%R%' AND

        ps_product.barcode <> 4106293335 AND

        ps_product.product_stock_id <> '' AND

        ps_product.barcode <> ''

    GROUP BY

        ps_product.product_stock_id

        ")->rows;  

        

        $i=0;

        $processed = 0;

        foreach($products as $product) {

            if(strlen($product['barcode'])<9) {

                continue;

            }

            // if($i>5000) {

            //     return;

            // }

            $categories = $this->db->query("SELECT c.category_id, c.infinityeditid, cd.name FROM ps_product_to_category p2c



                            LEFT JOIN ps_category c ON (p2c.category_id = c.category_id)



                            LEFT JOIN ps_category_description cd ON (c.category_id = cd.category_id)



                            WHERE p2c.product_id = '" . $product['product_id'] . "' GROUP BY c.category_id 



                        ")->rows;



                        if(count($categories)==3) { # kategori varsa barkod üret. 



                            $category = $categories[1];



                            $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;



                            $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;



                            $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.



                            if($barcode!=$product['barcode']) {

                                

                                echo $product['product_id'];

                                echo '<br/>';

                                echo 'Eski Barkod:'. $product['barcode'];

                                echo '<br/>';

                                echo $category_name . ' ' . $collection['name'] . ' ' . $short_color_name;

                                echo '<br/>';



                                echo $barcode_category; 

                                echo '<br/>';

                                echo $barcode_collection; 

                                echo '<br/>';

                                echo $barcode_color; 

                                echo '<br/>';

                                echo $barcode;



                                

                                echo '<br/>';

                                echo '----';

                                echo '<br/>';

                                $processed++;



                            };

            }

            $i++;

        }

        echo '***********işlenen:' . $processed;



    }

}