<?php

add_action( 'doliconnect_cron_hook', 'doliconnect_cron_process' );

function doliconnect_cron_process($refresh = false) {

    $cronjob = !empty(get_site_option('doliconnect_cronjob_multisite'))?get_site_option('doliconnect_cronjob_multisite'):get_option('doliconnect_cronjob');

    if (!empty($cronjob)) {
        if ($cronjob == '2') $refresh = true;
        $products = array();
        $categories = array();

        if ( doliCheckModules('discountprice') ) {
            $date = new DateTime(); 
            $date->modify('NOW');
            $lastdate = $date->format('Y-m-d');
            $requestp = "/discountprice?sortfield=t.rowid&sortorder=DESC&sqlfilters=(t.date_begin:>=:'".$lastdate."')and(t.date_end:<=:'".$lastdate."')and(d.tosell:=:1)";
            $listproduct = callDoliApi("GET", $requestp, null, dolidelay('category', $refresh));
            if ( !isset($listproduct->error) && $listproduct != null ) {
                foreach ($listproduct as $product) {
                    $products[$product->fk_product]['id'] = $product->fk_product;
                    $products[$product->fk_product]['entity'] = $product->entity;
                }
            }
        }

        $shop = doliconst("DOLICONNECT_CATSHOP");
        if ( $shop != null && $shop > 0 ) {
            $request = "/categories?sortfield=t.label&sortorder=ASC&limit=100&type=product&sqlfilters=(t.fk_parent:=:".esc_attr($shop).")";
        } else {
            $request = "/categories?sortfield=t.label&sortorder=ASC&limit=100&type=product&sqlfilters=(t.fk_parent:=:0)";
        }
        $resultatsc = callDoliApi("GET", $request, null, dolidelay('category', $refresh));
        if ( !isset($resultatsc->error) && $resultatsc != null ) {
            foreach ($resultatsc as $category) {
                $categories[$category->id] = $category->id;
                doliconnect_image('category', $category->id, 1, $refresh, $category->entity);
            }
        }

        foreach ($categories as $id => $categorie) {
            $request = "/categories/".$id."?include_childs=true";
            $resultatsc = callDoliApi("GET", $request, null, dolidelay('category', $refresh));
            if ( !isset($resultatsc->error) && $resultatsc != null ) {
                foreach ($resultatsc->childs as $category) {
                    $categories[$category->id] = $category->id;
                    $subcategories[$category->id] = $category->id;
                    doliconnect_image('category', $category->id, 1, $refresh, $category->entity);
                    getDoliProductCategory($category);
                }
            }
        }

        if (isset($subcategories)) {
            foreach ($subcategories as $id => $categorie) {
                $request = "/categories/".$id."?include_childs=true";
                $resultatsc = callDoliApi("GET", $request, null, dolidelay('category', $refresh));
                if ( !isset($resultatsc->error) && $resultatsc != null ) {
                    foreach ($resultatsc->childs as $category) {
                        $categories[$category->id] = $category->id;
                        doliconnect_image('category', $category->id, 1, $refresh, $category->entity);
                        getDoliProductCategory($category);
                    }
                }
            }
        }

        foreach ($categories as $id => $categorie) {
            $requestp = "/products?sortfield=t.rowid&sortorder=DESC&category=".$id."&sqlfilters=(t.tosell:=:1)&limit=1000";
            
            $listproduct = callDoliApi("GET", $requestp, null, dolidelay('product', $refresh));
            if ( !isset($listproduct->error) && $listproduct != null ) {
                foreach ($listproduct as $product) {
                    $products[$product->id]['id'] = $product->id;
                    $products[$product->id]['entity'] = $product->entity;
                }
            }
            //getDoliProductCategory($id);
        }
 
        foreach ($products as $id => $product) {
            getDoliProductUrl($product['id'], true);
            $product1 = callDoliApi("GET", "/products/".$product['id']."?includesubproducts=true&includetrans=true", null, dolidelay('product', $refresh));
            doliconnect_image('product', $product['id'], array('limit'=>1, 'entity'=>$product['entity'], 'size'=>'200x200'), $refresh);
            if ( ! empty(doliconnectid('dolicart')) ) {
                if ( doliCheckModules('discountprice') ) {
                    $date = new DateTime(); 
                    $date->modify('NOW');
                    $lastdate = $date->format('Y-m-d');
                    $requestp = "/discountprice?productid=".$product->id."&sortfield=t.rowid&sortorder=ASC&sqlfilters=(t.date_begin:>=:'".$lastdate."')and(t.date_end:<=:'".$lastdate."')and(d.tosell:=:1)";
                    $product2 = callDoliApi("GET",$requestp, null, dolidelay('product', $refresh));
                }
                if ( !empty(doliconst("PRODUIT_CUSTOMER_PRICES", $refresh))) {
                    $product3 = callDoliApi("GET", "/products/".$product['id']."/selling_multiprices/per_customer", null, dolidelay('product', $refresh));
                }
            }
        }
    }
}
?>