<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('memory_limit', '2048M');

include_once "../config.php";
include_once "../database1.php";
//include_once "../../validation.php";
//include_once "../../Helper.php";

$csvAsArray = array_map(function ($line) {
    return str_getcsv($line, "\t");
}, file('csv/wave_products.tsv'));


class Insert extends database
{
    public function checkBrand($name)
    {
            return $this->fetchRow('tb_brands', ['cl_name' => $name]);
    }
    
    public function insertBrand($brand)
    {
      return $this->insert('tb_brands', ['cl_name' => $brand]);
    }
  
    public function checkProduct($productID)
    {
        return $this->fetchRow('tb_product', ['cl_product_id' => $productID, 'cl_supplier' => 'wave']);
    }
    
    public function updateBulk($products)
    {
        $count  = 0;
        $currentDateTime = date('Y-m-d H:i:s');
        
        $insertHTML = '';
        $where = [];
        
        $bulkCount = 1;
          

        foreach($products AS $product)
        {

            if($count == 500)
            {

                  $sql = 'INSERT INTO tb_product (cl_cat_id, cl_sub_cat_id, cl_brand_id, cl_name, cl_slug, cl_regular_price, cl_sale_price,cl_supplier_price,cl_supplier,cl_product_id,cl_stock,cl_ean, cl_x, cl_y, cl_z, cl_weight,cl_last_price_updated, cl_manufacturer,cl_description,cl_meta_description,cl_keywords, cl_created_at, cl_updated_at)
VALUES ' . $insertHTML;
            
  
             //   $sql = ' UPDATE tb_product SET ' . $stockHtml . ' ELSE cl_stock END, ' . $supplierPriceHTML . ' ELSE cl_supplier_price END, ' . $profitPercentageHTML . ' ELSE cl_profit_percentage END, ' . $monthlyPriceHTML . ' ELSE cl_monthly_price END ';



                //$whereMultiple = array_merge($whereMultipleStock,$whereMultipleSupplier, $whereMultipleProfitPercentage, $whereMultipleMonthlyPrice);

                $this->callQueryInsert($sql, $where);
                
                 $fp = fopen('wave_insert_product_text.txt', 'a');
                //opens file in append mode  
                fwrite($fp, 'Inserted ' . $bulkCount * 500 . ' Products<br/>');  
                fclose($fp); 
                
                
                $bulkCount++;


                $count = 0;
                $insertHTML = '';
                $where = [];

                
            }
            
            $productArray = $product;
            
            if($count > 0)
            {
                $insertHTML.= ',';
            }
            $insertHTML.= '(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
            
            foreach($product AS $each)
            {
                $where[] = $each;
            }
            

            $count++;
        }

           $sql = 'INSERT INTO tb_product (cl_cat_id, cl_sub_cat_id, cl_brand_id, cl_name, cl_slug, cl_regular_price, cl_sale_price,cl_supplier_price,cl_supplier,cl_product_id,cl_stock,cl_ean, cl_x, cl_y, cl_z, cl_weight,cl_last_price_updated, cl_manufacturer,cl_description,cl_meta_description,cl_keywords, cl_created_at, cl_updated_at)
VALUES ' . $insertHTML;
        //  $whereMultiple = array_merge($whereMultipleStock,$whereMultipleSupplier, $whereMultipleProfitPercentage, $whereMultipleMonthlyPrice);
     
         $this->callQueryInsert($sql, $where);

         $fp = fopen('wave_insert_product_text.txt', 'a');
                //opens file in append mode  
                fwrite($fp, 'Inserted All<br/>');  
                fclose($fp); 
            
        return 1;
    }
}
//print_r($response->data[0]);
$obj = new Insert();
$insertData = array();
$count = 0;
foreach($csvAsArray AS $data)
{
   
    if(!$obj->checkProduct($data[23]))
    {
        



        $insertData[$count]['cl_cat_id'] = 50;
        $insertData[$count]['cl_sub_cat_id'] = 3925;
        
        if(isset($data[2]) && trim($data[2]) != '') {
          $brand = trim($data[2]);
          $res = $obj->checkBrand($brand);
          
          if(!$res)
          {
            $brandID = $obj->insertBrand($brand);
            $insertData[$count]['cl_brand_id'] = $brandID;
          	//$fileError.= 'Brand not found in database for ' . ($count + 1) . ' row<br/>';
          }
          else
          {
          	$insertData[$count]['cl_brand_id'] = $res['cl_id'];
          }
      }
      else
      {
      	$fileError.= 'Brand is missing for ' . ($count + 1) . ' row<br/>';
      }
        
        
        $insertData[$count]['cl_brand_id'] = 1291;
        $insertData[$count]['cl_name'] = $data[3];
        $insertData[$count]['cl_slug'] = $data[4];
        $insertData[$count]['cl_regular_price'] = $data[27];
        $insertData[$count]['cl_sale_price'] = $data[27];
        $insertData[$count]['cl_supplier_price'] = $data[27];
        $insertData[$count]['cl_supplier'] = 'wave';
        $insertData[$count]['cl_product_id'] = $data[23];
        if($data[24] > 0)
        {
            $insertData[$count]['cl_stock'] = 1;
        }
        else {
            $insertData[$count]['cl_stock'] = 0;
        }

        $insertData[$count]['cl_ean'] = $data[29];
        $insertData[$count]['cl_x'] = 0;
        $insertData[$count]['cl_y'] = 0;
        $insertData[$count]['cl_z'] = 0;
        $insertData[$count]['cl_weight'] = 0;
        $insertData[$count]['cl_last_price_updated'] = date('Y-m-d H:i:s');
        $insertData[$count]['cl_manufacturer'] = $data[2];
        $insertData[$count]['cl_description'] = $data[7];
        $insertData[$count]['cl_meta_description'] = $data[19];
        $insertData[$count]['cl_keywords'] = $data[20];
        $insertData[$count]['cl_created_at'] = date('Y-m-d H:i:s');
        $insertData[$count]['cl_updated_at'] = date('Y-m-d H:i:s');
    
    
        $count++;
        
    }
    
}


$obj->updateBulk($insertData);
