Beispiel #1
0
/**
 * Return of Product URL (image and link)
 *
 * @param $post_data : sent parameters
 * @return array with info data
 */
function getUrlProducts(&$post_data)
{
    $reponse = array();
    $array_url = array();
    $post_message = Tools::jsonDecode(NetReviewsModel::acDecodeBase64($post_data['message']), true);
    $ids_product = $post_message['list_produits'];
    foreach ($ids_product as $id_product) {
        $urls = NetReviewsModel::getUrlsProduct($id_product);
        if ($urls) {
            //return urls only if product exist
            $array_url[$id_product] = $urls;
        }
    }
    $reponse['return'] = 1;
    $reponse['query'] = 1;
    $reponse['list_produits'] = $array_url;
    return $array_url;
}
    public function export($id_shop = null, $header_colums)
    {
        $o_netreviews = new NetReviews();
        $duree = Tools::getValue('duree');
        $order_statut_list = array_map('intval', Tools::getValue('orderstates'));
        $order_statut_list = !empty($order_statut_list) ? implode(',', $order_statut_list) : null;
        if (!empty($id_shop)) {
            $file_name = Configuration::get('AV_CSVFILENAME', null, null, $id_shop);
            $delay = Configuration::get('AV_DELAY', null, null, $id_shop) ? Configuration::get('AV_DELAY', null, null, $id_shop) : 0;
        } else {
            $file_name = Configuration::get('AV_CSVFILENAME');
            $delay = Configuration::get('AV_DELAY') ? Configuration::get('AV_DELAY') : 0;
        }
        $avis_produit = Tools::getValue('productreviews');
        if (!empty($file_name)) {
            $file_path = _PS_MODULE_DIR_ . 'netreviews/Export_NetReviews_' . str_replace('/', '', Tools::stripslashes($file_name));
            if (file_exists($file_path)) {
                if (is_writable($file_path)) {
                    unlink($file_path);
                } else {
                    throw new Exception($o_netreviews->l('Writing on our server is not allowed. Please assign write permissions to the folder netreviews'));
                }
            } else {
                foreach (glob(_PS_MODULE_DIR_ . 'netreviews/Export_NetReviews_*') as $filename_to_delete) {
                    if (is_writable($filename_to_delete)) {
                        unlink($filename_to_delete);
                    }
                }
            }
        }
        $file_name = date('d-m-Y') . '-' . Tools::substr(md5(rand(0, 10000)), 1, 10) . '.csv';
        $file_path = _PS_MODULE_DIR_ . 'netreviews/Export_NetReviews_' . $file_name;
        $duree_sql = '';
        switch ($duree) {
            case '1w':
                $duree_sql = 'INTERVAL 1 WEEK';
                break;
            case '2w':
                $duree_sql = 'INTERVAL 2 WEEK';
                break;
            case '1m':
                $duree_sql = 'INTERVAL 1 MONTH';
                break;
            case '2m':
                $duree_sql = 'INTERVAL 2 MONTH';
                break;
            case '3m':
                $duree_sql = 'INTERVAL 3 MONTH';
                break;
            case '4m':
                $duree_sql = 'INTERVAL 4 MONTH';
                break;
            case '5m':
                $duree_sql = 'INTERVAL 5 MONTH';
                break;
            case '6m':
                $duree_sql = 'INTERVAL 6 MONTH';
                break;
            case '7m':
                $duree_sql = 'INTERVAL 7 MONTH';
                break;
            case '8m':
                $duree_sql = 'INTERVAL 8 MONTH';
                break;
            case '9m':
                $duree_sql = 'INTERVAL 9 MONTH';
                break;
            case '10m':
                $duree_sql = 'INTERVAL 10 MONTH';
                break;
            case '11m':
                $duree_sql = 'INTERVAL 11 MONTH';
                break;
            case '12m':
                $duree_sql = 'INTERVAL 12 MONTH';
                break;
            default:
                $duree_sql = 'INTERVAL 1 WEEK';
                break;
        }
        $all_orders = array();
        // Get orders with choosen date interval
        $where_id_shop = !empty($id_shop) ? 'AND o.id_shop = ' . (int) $id_shop : '';
        $select_id_shop = !empty($id_shop) ? ', o.id_shop' : '';
        $where_id_state = !empty($order_statut_list) ? ' AND o.current_state IN (' . $order_statut_list . ')' : '';
        $select_id_state = !empty($order_statut_list) ? ', o.current_state' : '';
        $qry_sql = '	SELECT lg.iso_code, o.id_order, o.total_paid, o.id_customer, o.date_add, c.firstname, c.lastname, c.email ' . $select_id_shop . $select_id_state . '
						FROM ' . _DB_PREFIX_ . 'orders o
						LEFT JOIN ' . _DB_PREFIX_ . 'customer c ON o.id_customer = c.id_customer
						LEFT JOIN ' . _DB_PREFIX_ . 'lang lg ON o.id_lang = lg.id_lang
						WHERE (TO_DAYS(DATE_ADD(o.date_add,' . $duree_sql . ')) - TO_DAYS(NOW())) >= 0
						' . $where_id_shop . $where_id_state;
        $item_list = Db::getInstance()->ExecuteS($qry_sql);
        foreach ($item_list as $item) {
            $all_orders[$item['id_order']] = array('ID_ORDER' => $item['id_order'], 'MONTANT_COMMANDE' => $item['total_paid'], 'DATE_ORDER' => date('d/m/Y', strtotime($item['date_add'])), 'ID_CUSTOMER' => array('ID_CUSTOMER' => $item['id_customer'], 'FIRST_NAME' => $item['firstname'], 'LAST_NAME' => $item['lastname'], 'EMAIL' => $item['email']), 'EMAIL_CLIENT' => '', 'NOM_CLIENT' => '', 'ORDER_STATE' => $item['current_state'], 'ISO_LANG' => $item['iso_code'], 'PRODUCTS' => array());
            $qry_sql = 'SELECT id_order, product_id, product_name FROM ' . _DB_PREFIX_ . 'order_detail WHERE id_order = ' . (int) $item['id_order'];
            $product_list = Db::getInstance()->ExecuteS($qry_sql);
            foreach ($product_list as $product) {
                $array_url = NetReviewsModel::getUrlsProduct($product['product_id']);
                $all_orders[$product['id_order']]['PRODUCTS'][] = array('ID_PRODUCT' => $product['product_id'], 'NOM_PRODUCT' => $product['product_name'], 'URL_PRODUCT' => $array_url['url_product'], 'URL_IMAGE_PRODUCT' => $array_url['url_image_product']);
            }
        }
        if (count($all_orders) > 0) {
            if ($csv = @fopen($file_path, 'w')) {
                fwrite($csv, $header_colums);
                foreach ($all_orders as $order) {
                    $count_products = count($order['PRODUCTS']);
                    if ($avis_produit == 1 && $count_products > 0) {
                        for ($i = 0; $i < $count_products; $i++) {
                            $line = '';
                            //reset the line
                            $line[] = $order['ID_ORDER'];
                            $line[] = $order['MONTANT_COMMANDE'];
                            $line[] = $order['ID_CUSTOMER']['EMAIL'];
                            $line[] = utf8_decode($order['ID_CUSTOMER']['LAST_NAME']);
                            $line[] = utf8_decode($order['ID_CUSTOMER']['FIRST_NAME']);
                            $line[] = $order['DATE_ORDER'];
                            $line[] = $delay;
                            $line[] = $order['PRODUCTS'][$i]['ID_PRODUCT'];
                            $line[] = '';
                            // Categorie du produit
                            $line[] = utf8_decode($order['PRODUCTS'][$i]['NOM_PRODUCT']);
                            $line[] = utf8_decode($order['PRODUCTS'][$i]['URL_PRODUCT']);
                            //Url fiche product
                            $line[] = utf8_decode($order['PRODUCTS'][$i]['URL_IMAGE_PRODUCT']);
                            //Url image fiche product
                            $line[] = $order['ORDER_STATE'];
                            //Etat de la commande
                            $line[] = $order['ISO_LANG'];
                            //Order lang
                            if (!empty($id_shop)) {
                                $line[] = $id_shop;
                            }
                            fwrite($csv, self::generateCsvLine($line));
                        }
                    } else {
                        $line = '';
                        //reset the line
                        $line[] = $order['ID_ORDER'];
                        $line[] = $order['ID_CUSTOMER']['EMAIL'];
                        $line[] = utf8_decode($order['ID_CUSTOMER']['LAST_NAME']);
                        $line[] = utf8_decode($order['ID_CUSTOMER']['FIRST_NAME']);
                        $line[] = $order['DATE_ORDER'];
                        $line[] = $delay;
                        $line[] = '';
                        $line[] = '';
                        // Product category
                        $line[] = '';
                        $line[] = '';
                        // URL
                        $line[] = '';
                        //Url image fiche product
                        $line[] = $order['ORDER_STATE'];
                        //Order state
                        $line[] = $order['ISO_LANG'];
                        //Order lang
                        if (!empty($id_shop)) {
                            $line[] = $id_shop;
                        }
                        fwrite($csv, self::generateCsvLine($line));
                    }
                }
                fclose($csv);
                if (file_exists($file_path)) {
                    Configuration::updateValue('AV_CSVFILENAME', $file_name);
                    return array($file_name, count($all_orders), $file_path);
                } else {
                    throw new Exception($o_netreviews->l('Unable to read/write export file'));
                }
            } else {
                throw new Exception($o_netreviews->l('Unable to read/write export file'));
            }
        } else {
            throw new Exception($o_netreviews->l('No order to export'));
        }
    }
Beispiel #3
0
 public function exportApi($duree, $statut)
 {
     $o_netreviews = new NetReviews();
     $order_statut_list = array_map('intval', $statut);
     $order_statut_list = !empty($order_statut_list) ? implode(',', $order_statut_list) : null;
     $duree_sql = '';
     switch ($duree) {
         case '1w':
             $duree_sql = 'INTERVAL 1 WEEK';
             break;
         case '2w':
             $duree_sql = 'INTERVAL 2 WEEK';
             break;
         case '1m':
             $duree_sql = 'INTERVAL 1 MONTH';
             break;
         case '2m':
             $duree_sql = 'INTERVAL 2 MONTH';
             break;
         case '3m':
             $duree_sql = 'INTERVAL 3 MONTH';
             break;
         case '4m':
             $duree_sql = 'INTERVAL 4 MONTH';
             break;
         case '5m':
             $duree_sql = 'INTERVAL 5 MONTH';
             break;
         case '6m':
             $duree_sql = 'INTERVAL 6 MONTH';
             break;
         case '7m':
             $duree_sql = 'INTERVAL 7 MONTH';
             break;
         case '8m':
             $duree_sql = 'INTERVAL 8 MONTH';
             break;
         case '9m':
             $duree_sql = 'INTERVAL 9 MONTH';
             break;
         case '10m':
             $duree_sql = 'INTERVAL 10 MONTH';
             break;
         case '11m':
             $duree_sql = 'INTERVAL 11 MONTH';
             break;
         case '12m':
             $duree_sql = 'INTERVAL 12 MONTH';
             break;
         default:
             $duree_sql = 'INTERVAL 1 WEEK';
             break;
     }
     $all_orders = array();
     // Get orders with choosen date interval
     $where_id_state = !empty($order_statut_list) ? ' AND o.current_state IN (' . $order_statut_list . ')' : '';
     $select_id_state = !empty($order_statut_list) ? ', o.current_state' : '';
     $qry_sql = '    SELECT lg.iso_code, o.id_order, o.total_paid, o.id_customer, o.date_add, c.firstname, c.lastname, c.email ' . $select_id_state . '
                     FROM ' . _DB_PREFIX_ . 'orders o
                     LEFT JOIN ' . _DB_PREFIX_ . 'customer c ON o.id_customer = c.id_customer
                     LEFT JOIN ' . _DB_PREFIX_ . 'lang lg ON o.id_lang = lg.id_lang
                     WHERE (TO_DAYS(DATE_ADD(o.date_add,' . $duree_sql . ')) - TO_DAYS(NOW())) >= 0 ' . $where_id_state;
     $item_list = Db::getInstance()->ExecuteS($qry_sql);
     foreach ($item_list as $item) {
         $all_orders[$item['id_order']] = array('ID_ORDER' => $item['id_order'], 'MONTANT_COMMANDE' => $item['total_paid'], 'DATE_ORDER' => date('d/m/Y', strtotime($item['date_add'])), 'ID_CUSTOMER' => array('ID_CUSTOMER' => $item['id_customer'], 'FIRST_NAME' => $item['firstname'], 'LAST_NAME' => $item['lastname'], 'EMAIL' => $item['email']), 'EMAIL_CLIENT' => '', 'NOM_CLIENT' => '', 'ORDER_STATE' => $item['current_state'], 'ISO_LANG' => $item['iso_code'], 'PRODUCTS' => array());
         $qry_sql = 'SELECT id_order, product_id, product_name FROM ' . _DB_PREFIX_ . 'order_detail WHERE id_order = ' . (int) $item['id_order'];
         $product_list = Db::getInstance()->ExecuteS($qry_sql);
         foreach ($product_list as $product) {
             $array_url = NetReviewsModel::getUrlsProduct($product['product_id']);
             $all_orders[$product['id_order']]['PRODUCTS'][] = array('ID_PRODUCT' => $product['product_id'], 'NOM_PRODUCT' => $product['product_name'], 'URL_PRODUCT' => $array_url['url_product'], 'URL_IMAGE_PRODUCT' => $array_url['url_image_product']);
         }
     }
     if (count($all_orders) > 0) {
         return $all_orders;
     }
 }