<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('memory_limit', '15G');
ini_set("max_execution_time", 18000);
error_reporting(E_ALL);

use Magento\Framework\App\Bootstrap;

require '../app/bootstrap.php';
 include_once './config.php';
    include_once './employees.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$storeManager = $objectManager->create('Magento\Store\Model\StoreManagerInterface');

// $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
        $orderm = $objectManager->create('\Magento\Sales\Model\OrderRepository')->get('4823');    
        $idOrder= 4784;
        $objectManager =   \Magento\Framework\App\ObjectManager::getInstance();
    $connection = $objectManager->get('Magento\Framework\App\ResourceConnection')->getConnection('\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION'); 
    $result1 = $connection->fetchAll("SELECT * FROM `ahqa_swissup_deliverydate` WHERE `order_id` = '".$idOrder."' ORDER BY `date` DESC");
    if($result1){
        foreach($result1 as $values){
         echo   $dateTime=  date("F d,Y", strtotime($values['date'])).' '.$values['timerange'];
        }
    echo count($result1);
        // $extAttributes = $orderm->getExtensionAttributes();
        // if ($extAttributes) {
        //   echo  $time = $extAttributes->getDeliveryTime();
            
        //     echo $date = $extAttributes->getDeliveryDate();
            
        // } 
        print_r($result1);
    }
     $result1 = $connection->fetchAll("SELECT * FROM ahqa_swissup_checkoutfields_values  WHERE `order_id` = '".$idOrder."' ORDER BY `value` DESC");
    if($result1){
        foreach($result1 as $values){
         echo   $dateTime= $values['value'];
        }
    echo count($result1);
        // $extAttributes = $orderm->getExtensionAttributes();
        // if ($extAttributes) {
        //   echo  $time = $extAttributes->getDeliveryTime();
            
        //     echo $date = $extAttributes->getDeliveryDate();
            
        // } 
        print_r($result1);
    }
exit();
die();
$fp = fopen('csv/Qatar_Products_WITHout_Image2.CSV', 'w+');
$csvHeader = array('SKU', 'without image','Name','Category');
fputcsv($fp, $csvHeader, ",");
    $database = new Database();
    $db = $database->getConnection();
  //  $items = new Employee($db);
    $sqls="SELECT sku FROM `ahqa_catalog_product_entity` WHERE `entity_id` not in (SELECT `entity_id` FROM `ahqa_catalog_product_entity_media_gallery_value` ) and `entity_id` not in (select  `entity_id` from `ahqa_catalog_product_entity_int` where value = 2 and attribute_id= (SELECT `attribute_id` FROM `ahqa_eav_attribute` WHERE `attribute_code` = 'visibility'))";
        $stmt = $db->prepare($sqls);    
            $stmt->execute();
    $itemCount = $stmt->rowCount();
    
     if($itemCount > 0){
    $p = 0;
   // while (1) {    
    
      
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
    $p++;
   
$mysq="SELECT e.entity_id AS 'id',
v1.value AS 'name',
e.sku AS 'sku',
d1.value AS 'price',
t1.value AS 'short_description',
t2.value AS 'description',
v2.value AS 'image',
v3.value AS 'thumbnail',
mg.value AS 'media_gallery',
cids.category_ids AS 'category_ids',
cids.category_names AS 'category_names'
FROM  ahqa_catalog_product_entity e
LEFT JOIN  ahqa_catalog_product_entity_varchar v1 ON e.entity_id = v1.entity_id
AND v1.store_id = 0 AND v1.attribute_id = (SELECT attribute_id FROM  ahqa_eav_attribute WHERE attribute_code = 'name'
AND entity_type_id = (SELECT entity_type_id FROM  ahqa_eav_entity_type WHERE entity_type_code = 'catalog_product'))
LEFT JOIN  ahqa_catalog_product_entity_text t1 ON e.entity_id = t1.entity_id
AND t1.store_id = 0 AND t1.attribute_id = (SELECT attribute_id FROM  ahqa_eav_attribute WHERE attribute_code = 'short_description'
AND entity_type_id = (SELECT entity_type_id FROM  ahqa_eav_entity_type WHERE entity_type_code = 'catalog_product'))
LEFT JOIN  ahqa_catalog_product_entity_text t2 ON e.entity_id = t2.entity_id
AND t2.store_id = 0 AND t2.attribute_id = (SELECT attribute_id FROM  ahqa_eav_attribute WHERE attribute_code = 'description'
AND entity_type_id = (SELECT entity_type_id FROM  ahqa_eav_entity_type WHERE entity_type_code = 'catalog_product'))
LEFT JOIN  ahqa_catalog_product_entity_varchar v2 ON e.entity_id = v2.entity_id
AND v2.store_id = 0 AND v2.attribute_id = (SELECT attribute_id FROM  ahqa_eav_attribute WHERE attribute_code = 'image'
AND entity_type_id = (SELECT entity_type_id FROM  ahqa_eav_entity_type WHERE entity_type_code = 'catalog_product'))
LEFT JOIN  ahqa_catalog_product_entity_varchar v3 ON e.entity_id = v3.entity_id
AND v3.store_id = 0 AND v3.attribute_id = (SELECT attribute_id FROM  ahqa_eav_attribute WHERE attribute_code = 'thumbnail'
AND entity_type_id = (SELECT entity_type_id FROM  ahqa_eav_entity_type WHERE entity_type_code = 'catalog_product'))
LEFT JOIN  ahqa_catalog_product_entity_decimal d1 ON e.entity_id = d1.entity_id
AND d1.store_id = 0 AND d1.attribute_id = (SELECT attribute_id FROM  ahqa_eav_attribute WHERE attribute_code = 'price'
AND entity_type_id = (SELECT entity_type_id FROM  ahqa_eav_entity_type WHERE entity_type_code = 'catalog_product'))
LEFT JOIN
(SELECT m1.entity_id, GROUP_CONCAT(m2.value) AS value FROM  ahqa_catalog_product_entity_media_gallery_value_to_entity m1
INNER JOIN   ahqa_catalog_product_entity_media_gallery m2 ON m2.value_id = m1.value_id
AND m2.attribute_id = (SELECT attribute_id FROM  ahqa_eav_attribute WHERE attribute_code = 'media_gallery'
AND entity_type_id = (SELECT entity_type_id FROM  ahqa_eav_entity_type WHERE entity_type_code = 'catalog_product'))
GROUP BY m1.entity_id) mg ON e.entity_id = mg.entity_id
LEFT JOIN
(SELECT product_id, GROUP_CONCAT(c.category_id SEPARATOR ',') AS category_ids, GROUP_CONCAT(cv.value SEPARATOR ',') AS category_names
FROM  ahqa_catalog_category_product c
INNER JOIN   ahqa_catalog_category_entity_varchar cv ON c.category_id = cv.entity_id
AND cv.store_id = 0
AND cv.attribute_id = (SELECT attribute_id FROM  ahqa_eav_attribute WHERE attribute_code = 'name'
AND entity_type_id = (SELECT entity_type_id FROM  ahqa_eav_entity_type WHERE entity_type_code = 'catalog_category'))
GROUP BY product_id) cids ON e.entity_id = cids.product_id WHERE  e.sku=:sku";

//      $productRepository = $objectManager->get('\Magento\Catalog\Model\ProductRepository');
// $productObj2 = $productRepository->get($row['sku']);
// $productObj2->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
// $productObj2->save();
// echo $sql2 ="UPDATE ahqa_catalog_product_entity_int cpei, ahqa_catalog_product_entity cpe
// SET value = '1' WHERE cpe.entity_id = cpei.entity_id  AND cpe.sku = ".$row['sku'].";";
//echo $mysq;
 $stmt2 = $db->prepare($mysq);
 $stmt2->bindParam(':sku', $row['sku']);
 $stmt2->execute();
    // $productObj = $objectManager->create('Magento\Catalog\Model\Product')
    //     ->getCollection()
    //     ->addAttributeToSelect('*')
    //     ->addAttributeToFilter('sku',$row['sku'])
    //     //->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
    //     //->setPageSize(50)
    //     //->setCurPage($p)
    //     ->setOrder('id', 'ASC');
        
//         $productRepository = $objectManager->get('\Magento\Catalog\Model\ProductRepository');
// $productObj = $productRepository->get($row['sku'],true,0,true);
$row2 = $stmt2->fetch(PDO::FETCH_ASSOC);
    //while (){
        
   // foreach ($productObj as $product) {
          $catName=$row2['category_names'];
      
        // if ($product->getThumbnail() != '' ) {
        //     $isImage = 'Y';
        //     //fputcsv($fp, array($product->getSku(), $isImage), ",");
        // } else {
        
          //  $cats = $product->getCategoryIds();
            //  $categoryCollection = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
            //  $categories = $categoryCollection->create();
            //  $categories->addAttributeToSelect('*');
            //  $categories->addAttributeToFilter('entity_id', $cats);
            // foreach ($categories as $category) {
            //     $catName = $category->getName();
            // }
            $isImage = 'N';
            fputcsv($fp, array($row['sku'], $isImage,$row2['name'],$catName), ",");
        //}

       
       // }
    }

}
fclose($fp);
header("Location: https://ahmarket.com/custom-api/csv/Qatar_Products_WITHout_Image2.CSV"); 
exit;