id] = $sale; } public static function convertPrice($price) { if(PRIVATESALES_SHOW_DECIMAL_IN_ARRAY === true) { $price = round($price); $price = Tools::displayPrice($price); $price = str_replace(',00', '', $price); } else { $price = Tools::displayPrice($price); } return $price; } public static function getSales() { return self::$sales; } public static function getStateNull() { return $state_null = array(6, 7, 8); } public static function getAllSales(DateTime $date_from = null, DateTime $date_to = null, $orderby = null, $ordered = null) { $collection = new Collection('SaleCoreExtend', Context::getContext()->language->id); if($date_from !== null && $date_to !== null) { $format = 'Y-m-d H:i:s'; $collection->sqlWhere('(date_start BETWEEN "'.$date_from->format($format).'" AND "'.$date_to->format($format).'") OR (date_end BETWEEN "'.$date_from->format($format).'" AND "'.$date_to->format($format).'") '); } if($orderby === null || $ordered === null) $collection->orderBy('id_privatesales', 'DESC'); else { $definition = ObjectModel::getDefinition('SaleCoreExtend', $orderby); $collection->orderBy($orderby, $ordered); } $array = array(); foreach($collection as $sale) $array[$sale->id] = $sale; return $array; } /*******************************MISE A JOUR*********************************/ public static function run(DateTime $date_from = null, DateTime $date_to = null) { /*$date_from = new DateTime("2016-02-02 00:00:00"); $date_to = new DateTime("2016-02-02 23:59:59");*/ $sales = self::getSales(); $id_products_ori = array(); foreach($sales as $sale) { /*$ids_products = $sale->getProducts();*/ $ids_products = $sale->getDefaultProducts(); foreach($ids_products as $k => $product) { if(in_array($product, $id_products_ori)) { unset($ids_products[$k]); } else { $id_products_ori[] = $product; } } try { self::setProducts($sale, $ids_products, $date_from, $date_to); self::setOrders($sale, $ids_products, $date_from, $date_to); self::setTotal($sale); } catch(Exception $e) { self::$errors[] = sprintf(Tools::displayError('An error occured during stats update, private sale : %s (%s)'), $sale->title, $e->getMessage() ); } } self::runGeneral($date_from, $date_to); } public static function runGeneral(DateTime $date_from = null, DateTime $date_to = null) { self::setTotalFields($date_from, $date_to); } private static function setProducts($sale, $ids_products, DateTime $date_from = null, DateTime $date_to = null) { Db::getInstance()->delete('privatesales_stats_products', ($date_from !== null && $date_to !== null ? '(date BETWEEN "'.pSQL($date_from->format('Y-m-d')).'" AND "'.pSQL($date_to->format('Y-m-d')).'") AND ' : '') . ' id_sale = '.(int)$sale->id); if(count($ids_products) > 0) { $total = Db::getInstance()->executeS(' SELECT a.*, (total_products_price_tax_excl - total_products_wholesale_price) as rate_amount, ROUND( (total_products_price_tax_excl - total_products_wholesale_price) / total_products_wholesale_price, 3) as rate, ROUND( (total_products_price_tax_excl - total_products_wholesale_price) / total_products_price_tax_excl, 3) as rate_brand FROM ( SELECT '.$sale->id.' as id_sale, d.product_id, d.product_attribute_id, d.product_reference AS reference, d.product_supplier_reference AS supplier_reference, SUM( ROUND( d.product_price*d.product_quantity, 4)) as total_products_price_without_reduction, DATE_FORMAT(o.`date_add`, "%Y-%m-%d") as date, d.product_name as name, SUM(d.`product_quantity`) AS `total_products`, SUM( ROUND( d.unit_price_tax_incl * d.`product_quantity`, 6 ) ) AS `total_products_price`, SUM( ROUND( d.unit_price_tax_excl * d.`product_quantity`, 6 ) ) AS `total_products_price_tax_excl`, CASE WHEN pas.wholesale_price IS NULL AND d.product_attribute_id != 0 AND pas.wholesale_price != 0 THEN SUM(pas.wholesale_price * d.`product_quantity`) ELSE SUM(ps.wholesale_price * d.`product_quantity`) END AS total_products_wholesale_price FROM `'._DB_PREFIX_.'order_detail` d INNER JOIN `'._DB_PREFIX_.'orders` o ON (d.`id_order` = o.`id_order` AND o.current_state NOT IN ("'.implode('","', self::getStateNull()).'") ) LEFT OUTER JOIN `'._DB_PREFIX_.'product_shop` ps ON ps.id_shop = o.id_shop AND ps.id_product = d.product_id LEFT OUTER JOIN `'._DB_PREFIX_.'product_attribute_shop` pas ON d.product_attribute_id != 0 AND pas.id_product_attribute = d.product_attribute_id WHERE d.`product_id` IN ("'.implode('","', $ids_products).'") '.($date_from !== null && $date_to !== null ? 'AND DATE_FORMAT(o.`date_add`, "%Y-%m-%d") BETWEEN "'.pSQL($date_from->format('Y-m-d')).'" AND "'.pSQL($date_to->format('Y-m-d')).'"' : '').' GROUP BY date, d.product_id, d.product_attribute_id ) a '); foreach($total as $k => $tot) $total[$k]['name'] = pSQL($tot['name']); //if (count($total)>0) { //echo "
";var_dump($total);echo "";die(); //} Db::getInstance()->insert('privatesales_stats_products', $total); } } public static function setOrders($sale, $ids_products, DateTime $date_from = null, DateTime $date_to = null) { Db::getInstance()->delete('privatesales_stats_order', ($date_from !== null && $date_to !== null ? '(date BETWEEN "'.pSQL($date_from->format('Y-m-d')).'" AND "'.pSQL($date_to->format('Y-m-d')).'") AND' : '') . ' id_sale = '.(int)$sale->id); if(count($ids_products) > 0) { $total = Db::getInstance()->executeS(' SELECT a.*, ROUND(total_products_price_tax_excl - total_wholesale_price, 2) as rate_amount, ROUND( (total_products_price_tax_excl - total_wholesale_price) / total_wholesale_price, 3) as rate, ROUND( (total_products_price_tax_excl - total_wholesale_price) / total_products_price_tax_excl, 3) as rate_brand FROM ( SELECT '.$sale->id.' as id_sale, DATE_FORMAT(o.`date_add`, "%Y-%m-%d") as date, SUM(d.`product_quantity`) AS `total_products`, SUM(d.`total_shipping_price_tax_incl`) as `total_shipping`, SUM( ROUND( d.product_price*d.product_quantity, 4)) as total_products_price_without_reduction, COUNT(d.id_order) AS total_order, SUM( ROUND( d.unit_price_tax_incl * d.`product_quantity`, 6 ) ) AS `total_products_price`, SUM( ROUND( d.unit_price_tax_excl * d.`product_quantity`, 6 ) ) AS `total_products_price_tax_excl`, CASE WHEN pas.wholesale_price IS NULL AND d.product_attribute_id != 0 THEN SUM(pas.wholesale_price * d.`product_quantity`) ELSE SUM(ps.wholesale_price * d.`product_quantity`) END AS total_wholesale_price FROM `'._DB_PREFIX_.'order_detail` d INNER JOIN `'._DB_PREFIX_.'orders` o ON d.`id_order` = o.`id_order` AND o.current_state NOT IN ("'.implode('","', self::getStateNull()).'") LEFT OUTER JOIN `'._DB_PREFIX_.'product_shop` ps ON ps.id_shop = o.id_shop AND ps.id_product = d.product_id LEFT OUTER JOIN `'._DB_PREFIX_.'product_attribute_shop` pas ON d.product_attribute_id != 0 AND pas.id_product_attribute = d.product_attribute_id WHERE d.`product_id` IN ("'.implode('","', $ids_products).'") '.($date_from !== null && $date_to !== null ? 'AND DATE_FORMAT(o.`date_add`, "%Y-%m-%d") BETWEEN "'.pSQL($date_from->format('Y-m-d')).'" AND "'.pSQL($date_to->format('Y-m-d')).'"' : '').' GROUP BY date ) a '); Db::getInstance()->insert('privatesales_stats_order', $total); } } public static function setTotal($sale) { $total = Db::getInstance()->executeS('SELECT id_sale, SUM(`total_products`) AS `total_products`, SUM(`total_products_price`) AS `total_products_price`, SUM(`total_products_price_tax_excl`) AS `total_products_price_tax_excl`, SUM(`total_shipping`) AS `total_shipping`, SUM(`total_wholesale_price`) AS `total_wholesale_price`, SUM(`rate_amount`) AS `rate_amount`, AVG(`rate_brand`) AS rate_brand, AVG(`rate`) AS `rate` FROM '._DB_PREFIX_.'privatesales_stats_order WHERE id_sale = '.(int)$sale->id.' GROUP BY id_sale'); Db::getInstance()->delete('privatesales_stats_order_global', 'id_sale = '.(int)$sale->id); Db::getInstance()->insert('privatesales_stats_order_global', $total); } public static function setTotalFields(DateTime $date_from = null, DateTime $date_to = null) { Db::getInstance()->delete('privatesales_stats_other', ($date_from !== null && $date_to !== null ? 'date BETWEEN "'.pSQL($date_from->format('Y-m-d')).'" AND "'.pSQL($date_to->format('Y-m-d')).'"' : '')); self::setTotalOrder($date_from, $date_to); self::setTotalOrderWithError($date_from, $date_to); self::setTotalPayment($date_from, $date_to); self::setTotalValueAlreadyCalculed($date_from, $date_to); } public static function setTotalValueAlreadyCalculed(DateTime $date_from = null, DateTime $date_to = null) { $sales = self::getSales(); $sql = 'SELECT date, SUM(`total_wholesale_price`) AS total_wholesale_price, SUM(`total_products_price_without_reduction`) AS total_products_price_without_reduction FROM '._DB_PREFIX_.'privatesales_stats_order WHERE 1 '.($date_from !== null && $date_to !== null ? 'AND date BETWEEN "'.pSQL($date_from->format('Y-m-d')).'" AND "'.pSQL($date_to->format('Y-m-d')).'"' : '').' GROUP BY date'; $total_wholesale = Db::getInstance()->executeS($sql); foreach($total_wholesale as $value) { $array_insert[] = array( 'name' => 'total_wholesale_price', 'date' => $value['date'], 'value' => $value['total_wholesale_price'] ); $array_insert[] = array( 'name' => 'total_products_price_without_reduction', 'date' => $value['date'], 'value' => $value['total_products_price_without_reduction'] ); } Db::getInstance()->insert('privatesales_stats_other', $array_insert); } public static function setTotalPayment(DateTime $date_from = null, DateTime $date_to = null) { $sql = 'SELECT DATE_FORMAT(o.`date_add`, "%Y-%m-%d") as `date`, SUM(`total_paid`) AS `total_total_paid`, `payment` as `payment_method` FROM '._DB_PREFIX_.'orders o WHERE o.`current_state` NOT IN ("'.implode('","', self::getStateNull()).'") '.($date_from !== null && $date_to !== null ? 'AND DATE_FORMAT(o.`date_add`, "%Y-%m-%d") BETWEEN "'.pSQL($date_from->format('Y-m-d')).'" AND "'.pSQL($date_to->format('Y-m-d')).'"' : '').' GROUP BY `payment_method`, `date`'; // $sql = 'SELECT // o.date, // SUM(amount) AS total_paid, // payment_method // FROM '._DB_PREFIX_.'order_payment op // RIGHT JOIN ( // SELECT reference, DATE_FORMAT(o.`date_add`, "%Y-%m-%d") as date // FROM '._DB_PREFIX_.'orders o // WHERE o.current_state NOT IN ("'.implode('","', self::getStateNull()).'") // '.($date_from !== null && $date_to !== null ? // 'AND DATE_FORMAT(o.`date_add`, "%Y-%m-%d") BETWEEN "'.pSQL($date_from->format('Y-m-d')).'" AND "'.pSQL($date_to->format('Y-m-d')).'"' : '').' // ) o ON o.reference = op.order_reference // GROUP BY payment_method, date'; $total_payment_type = Db::getInstance()->executeS($sql); $array_insert = array(); $tot = 0; foreach($total_payment_type as $value) { $array_tmp = array( 'name' => 'total_payment', 'date' => $value['date'], 'value' => $value['total_total_paid'], 'value_txt' => $value['payment_method'] ); $array_insert[] = $array_tmp; } return Db::getInstance()->insert('privatesales_stats_other', $array_insert, false, true, Db::INSERT_IGNORE); } public static function setTotalOrder(DateTime $date_from = null, DateTime $date_to = null) { //ALGO POUR TROUVER LES REDUCTIONS CORRESPONDANTES AU TRANSPORTEUR ET CEUX CORRESPONDANTES AUX PRODUITS //LORSQUE FREE SHIPPING = 1 // LA REDUCTION SHIPPING = LE MONTANT DES FRAIS DE PORTS. // LA REDUCTION DES PRODUITS = TOTAL_REDUCTION - LE MONTANT DES FRAIS DE PORTS //LORSQUE FREE SHIPPING = 0 // LA REDUCTION SHIPPING = 0 // LA REDUCTION DES PRODUITS = TOTAL_REDUCTION $sql = 'SELECT DATE_FORMAT(o.`date_add`, "%Y-%m-%d") AS date, SUM(`total_products`) AS `total_products_tax_excl`, SUM(`total_products_wt`) AS `total_products_tax_incl`, SUM(`total_shipping_tax_incl`) AS `total_shipping_tax_incl`, SUM(`total_shipping_tax_excl`) AS `total_shipping_tax_excl`, SUM(`total_discounts_tax_incl`) AS `total_discounts_tax_incl`, SUM(`total_discounts_tax_excl`) AS `total_discounts_tax_excl`, SUM(`total_paid_tax_excl`) AS `total_paid_tax_excl`, SUM(`total_paid_tax_incl`) AS `total_paid_tax_incl`, SUM(`total_discount_product_incl`) AS `total_discount_product_incl`, SUM(`total_discount_product_excl`) AS `total_discount_product_excl`, SUM(`total_discount_shipping_tax_incl`) AS `total_discount_shipping_tax_incl`, SUM(`total_discount_shipping_tax_excl`) AS `total_discount_shipping_tax_excl`, SUM(`total_products_wt`) - SUM(`total_discount_product_incl`) as `total_product_real_paid_tax_incl`, SUM(`total_products`) - SUM(`total_discount_product_excl`) as `total_product_real_paid_tax_excl`, SUM(`total_shipping_tax_excl`) - SUM(`total_discount_shipping_tax_excl`) as `total_shipping_real_paid_tax_excl`, SUM(`total_shipping_tax_incl`) - SUM(`total_discount_shipping_tax_incl`) as `total_shipping_real_paid_tax_incl` FROM ( SELECT o.*, (CASE WHEN `ocr`.`total_free_shipping` = 1 THEN ( `total_discounts_tax_incl` - `total_shipping_tax_incl` ) ELSE ( `total_discounts_tax_incl` ) END) AS `total_discount_product_incl`, (CASE WHEN `ocr`.`total_free_shipping` = 1 THEN ( `total_discounts_tax_excl` - `total_shipping_tax_excl` ) ELSE ( `total_discounts_tax_excl` ) END) AS `total_discount_product_excl`, (CASE WHEN `ocr`.`total_free_shipping` = 1 THEN `total_shipping_tax_incl` ELSE 0 END) AS `total_discount_shipping_tax_incl`, (CASE WHEN `ocr`.`total_free_shipping` = 1 THEN `total_shipping_tax_excl` ELSE 0 END) AS `total_discount_shipping_tax_excl`, `total_free_shipping` FROM '._DB_PREFIX_.'orders o LEFT OUTER JOIN ( SELECT `id_order`, MAX(`free_shipping`) as `total_free_shipping` FROM `'._DB_PREFIX_.'order_cart_rule` GROUP BY `id_order`) as ocr ON ocr.id_order = o.id_order WHERE `o`.`current_state` NOT IN ("'.implode('","', self::getStateNull()).'") '.($date_from !== null && $date_to !== null ? ' AND DATE_FORMAT(o.`date_add`, "%Y-%m-%d") BETWEEN "'.pSQL($date_from->format('Y-m-d')).'" AND "'.pSQL($date_to->format('Y-m-d')).'"' : '').' ) o GROUP BY date '; //AND (o.total_paid_tax_incl - o.total_products_wt - o.total_shipping_tax_incl) = 0 ???? // $sql = 'SELECT // DATE_FORMAT(o.`date_add`, "%Y-%m-%d") AS date, // SUM(`total_products`) AS `total_products_tax_excl`, // SUM(`total_products_wt`) AS `total_products_tax_incl`, // SUM(`total_shipping_tax_incl`) AS `total_shipping_tax_incl`, // SUM(`total_shipping_tax_excl`) AS `total_shipping_tax_excl`, // SUM(`total_discounts_tax_incl`) AS `total_discounts_tax_incl`, // SUM(`total_discounts_tax_excl`) AS `total_discounts_tax_excl`, // SUM(`total_paid_tax_excl`) AS `total_paid_tax_excl`, // SUM(`total_paid_tax_incl`) AS `total_paid_tax_incl`, // SUM(`total_discount_product_incl`) AS `total_discount_product_incl`, // SUM(`total_discount_product_excl`) AS `total_discount_product_excl`, // SUM(`total_discount_shipping_tax_incl`) AS `total_discount_shipping_tax_incl`, // SUM(`total_discount_shipping_tax_excl`) AS `total_discount_shipping_tax_excl`, // SUM(`total_products_wt`) - SUM(`total_discount_product_incl`) as `total_product_real_paid_tax_incl`, // SUM(`total_products`) - SUM(`total_discount_product_excl`) as `total_product_real_paid_tax_excl`, // SUM(`total_shipping_tax_excl`) - SUM(`total_discount_shipping_tax_excl`) as `total_shipping_real_paid_tax_excl`, // SUM(`total_shipping_tax_incl`) - SUM(`total_discount_shipping_tax_incl`) as `total_shipping_real_paid_tax_incl` // FROM ( // SELECT o.*, // (CASE // WHEN `ocr`.`total_free_shipping` = 1 THEN ( `total_value_cart_rule_incl` - `total_shipping_tax_incl` + `total_reduction_amount_tax_incl` ) // ELSE ( `total_value_cart_rule_incl` + `total_reduction_amount_tax_incl` ) // END) AS `total_discount_product_incl`, // (CASE // WHEN `ocr`.`total_free_shipping` = 1 THEN ( `total_value_cart_rule_excl` - `total_shipping_tax_excl` + `total_reduction_amount_tax_excl` ) // ELSE ( `total_value_cart_rule_excl` + `total_reduction_amount_tax_excl` ) // END) AS `total_discount_product_excl`, // (CASE // WHEN `ocr`.`total_free_shipping` = 1 THEN `total_shipping_tax_incl` // ELSE 0 // END) AS `total_discount_shipping_tax_incl`, // (CASE // WHEN `ocr`.`total_free_shipping` = 1 THEN `total_shipping_tax_excl` // ELSE 0 // END) AS `total_discount_shipping_tax_excl`, // `total_free_shipping` // FROM '._DB_PREFIX_.'orders o // LEFT OUTER JOIN ( // SELECT // `id_order`, // SUM(`value`) as `total_value_cart_rule_incl`, // SUM(`value_tax_excl`) as `total_value_cart_rule_excl`, // MAX(`free_shipping`) as `total_free_shipping` // FROM `'._DB_PREFIX_.'order_cart_rule` // GROUP BY `id_order`) as ocr // ON ocr.id_order = o.id_order // LEFT JOIN ( // SELECT // `id_order`, // SUM(`reduction_amount_tax_incl`) as `total_reduction_amount_tax_incl`, // SUM(`reduction_amount_tax_excl`) as `total_reduction_amount_tax_excl`, // SUM(``) // FROM `'._DB_PREFIX_.'order_detail` od // GROUP BY `id_order` // ) as od // ON od.`id_order` = o.`id_order` // WHERE `o`.`current_state` NOT IN ("'.implode('","', self::getStateNull()).'") // '.($date_from !== null && $date_to !== null ? ' // AND DATE_FORMAT(o.`date_add`, "%Y-%m-%d") BETWEEN "'.pSQL($date_from->format('Y-m-d')).'" AND "'.pSQL($date_to->format('Y-m-d')).'"' : '').' // ) o // GROUP BY date // '; $total_order = Db::getInstance()->executeS($sql); $array_insert = array(); foreach($total_order as $values) { foreach($values as $key => $value) { if($key == 'date') continue; $array_insert[] = array( 'name' => $key, 'date' => $values['date'], 'value' => $value ); } } return Db::getInstance()->insert('privatesales_stats_other', $array_insert, false, true, Db::INSERT_IGNORE);; } public static function setTotalOrderWithError(DateTime $date_from = null, DateTime $date_to = null) { $sql = ' SELECT GROUP_CONCAT(o.id_order) as ids_orders, o.date as date, SUM(value) as value FROM ( SELECT o.id_order, DATE_FORMAT(o.`date_add`, "%Y-%m-%d") as `date`, 1 as value FROM '._DB_PREFIX_.'orders o WHERE `o`.`current_state` NOT IN ("'.implode('","', self::getStateNull()).'") '.($date_from !== null && $date_to !== null ? ' AND DATE_FORMAT(o.`date_add`, "%Y-%m-%d") BETWEEN "'.pSQL($date_from->format('Y-m-d')).'" AND "'.pSQL($date_to->format('Y-m-d')).'"' : '').' AND (o.total_paid_tax_incl - o.total_products_wt - o.total_shipping_tax_incl) > 0 ) o GROUP BY o.date '; $total_order_with_error = Db::getInstance()->executeS($sql); $array_insert = array(); foreach($total_order_with_error as $values) { $array_insert[] = array( 'name' => 'total_order_with_error', 'date' => $values['date'], 'value' => $values['value'], 'value_txt' => $values['ids_orders'], ); } return Db::getInstance()->insert('privatesales_stats_other', $array_insert, false, true, Db::INSERT_IGNORE); } /*******************************FIN MISE A JOUR*********************************/ /*******************************RECUPERATION*********************************/ public static function getStatsGlobal(DateTime $date_from = null, DateTime $date_to = null) { $array = array( 'total_products_tax_excl', 'total_products_tax_incl', 'total_shipping_tax_incl', 'total_shipping_tax_excl', 'total_discounts_tax_incl', 'total_discounts_tax_excl', 'total_paid_tax_excl', 'total_paid_tax_incl', 'total_discount_product_incl', 'total_discount_product_excl', 'total_discount_shipping_tax_incl', 'total_discount_shipping_tax_excl', 'total_shipping_real_paid_tax_excl', 'total_shipping_real_paid_tax_incl', 'total_product_real_paid_tax_excl', 'total_product_real_paid_tax_incl', 'total_wholesale_price', 'total_products_price_without_reduction' ); $returns = array(); foreach($array as $field) { $sql = 'SELECT SUM(value) FROM '._DB_PREFIX_.'privatesales_stats_other WHERE name = \''.$field.'\' '.($date_from !== null && $date_to !== null ? ' AND date BETWEEN "'.pSQL($date_from->format('Y-m-d')).'" AND "'.pSQL($date_to->format('Y-m-d')).'"' : '').''; $returns[$field] = Db::getInstance()->getValue($sql); } //PAYMENT ERROR $sql = 'SELECT SUM(value) as value, GROUP_CONCAT(value_txt) as ids_orders FROM '._DB_PREFIX_.'privatesales_stats_other WHERE name = \'total_order_with_error\' '.($date_from !== null && $date_to !== null ? ' AND date BETWEEN "'.pSQL($date_from->format('Y-m-d')).'" AND "'.pSQL($date_to->format('Y-m-d')).'"' : '').''; $returns['total_order_with_error'] = Db::getInstance()->getRow($sql); $returns['total_discount_global_product_excl'] = ($returns['total_products_price_without_reduction'])+$returns['total_discount_product_excl']; $returns['total_product_and_shipping'] = $returns['total_product_real_paid_tax_excl']+$returns['total_shipping_real_paid_tax_excl']; $returns['total_product_with_discount_excl'] = $returns['total_product_real_paid_tax_excl']-$returns['total_discount_product_excl']; $returns['total_shipping_with_discount_excl'] = $returns['total_shipping_real_paid_tax_excl']-$returns['total_discount_shipping_tax_excl']; $returns['rate_amount'] = $returns['total_products_tax_excl']-$returns['total_wholesale_price']; $returns['rate'] = round( $returns['rate_amount']/$returns['total_wholesale_price'], 3); $returns['rate_brand'] = round( $returns['rate_amount']/$returns['total_products_tax_excl'], 3); $returns['rate_amount_without_discount'] = $returns['total_product_real_paid_tax_excl']-$returns['total_wholesale_price']; $returns['rate_without_discount'] = round( $returns['rate_amount_without_discount']/$returns['total_wholesale_price'], 3); $returns['rate_brand_without_discount'] = round( $returns['rate_amount_without_discount']/$returns['total_product_real_paid_tax_excl'], 3); // $returns['total_products_price_without_reduction_global'] = $returns['total_products_tax_excl']+$returns['total_products_price_without_reduction']; // $returns['total_product_and_shipping_without_reduction'] = $returns['total_products_price_without_reduction_global'] + $returns['total_shipping_tax_excl']; // $returns['rate_amount_without_reduction'] = $returns['total_product_and_shipping_without_reduction']-$returns['total_wholesale_price']; // $returns['rate_brand_without_reduction'] = round( $returns['rate_amount_without_reduction']/$returns['total_product_and_shipping_without_reduction'], 2); return $returns; } public static function getStatsPaymentType(DateTime $date_from = null, DateTime $date_to = null) { $sql = 'SELECT SUM(value) as total_paid, value_txt as payment_method FROM '._DB_PREFIX_.'privatesales_stats_other WHERE name = \'total_payment\' '.($date_from !== null && $date_to !== null ? ' AND date BETWEEN "'.pSQL($date_from->format('Y-m-d')).'" AND "'.pSQL($date_to->format('Y-m-d')).'"' : '').' GROUP BY value_txt'; $total_payment_type = Db::getInstance()->executeS($sql); return $total_payment_type; } public static function getSaleDateToDate(DateTime $date_from = null, DateTime $date_to = null) { $format = 'Y-m-d'; $sql = ' SELECT p.id_privatesales FROM `'. _DB_PREFIX_ . self::$definition['table'].' p WHERE CAST(`date_start` AS DATE) BETWEEN "'.pSQL($format).'" AND "'.pSQL($date_to).'" OR CAST(`date_end` AS DATE) BETWEEN "'.pSQL($date_from).'" AND "'.pSQL($date_to).'" OR (CAST(p.`date_start` AS DATE) <= "'.pSQL($date_from).'" AND CAST(p.`date_end` AS DATE) >= "'.pSQL($date_to).'") GROUP BY p.`id_privatesales` ORDER BY p.`date_start` DESC'; $res = Db::getInstance()->executeS($sql); $sales = array(); foreach($res as $sale) $sales[(int) $sale['id_sale']] = new SaleCoreExtend($sale['id_sale']); return $sales; } public static function getStatsStatic($sales = array(), $type = 'global', DateTime $date_from = null, DateTime $date_to = null, $order_by = null, $ordered = null) { if(is_object($sales) && get_class($sales) == 'Collection') $sales = $sales->getResults(); if(!is_array($sales) || count($sales) == 0) return false; $sales_ids = array(); foreach($sales as $sale) { if(!is_object($sale)) $sales_ids[] = $sale['id']; else $sales_ids[] = $sale->id; } if($order_by != null && $ordered != null) { $order_by_req = $order_by.' '.$ordered; } else { $order_by_req = 'id_sale DESC'; } switch($type) { case 'global': if($date_from !== null && $date_to !== null) { $req = 'SELECT id_sale, SUM(total_products_price) AS total_products_price, SUM(total_products_price_tax_excl) as total_products_price_tax_excl, SUM(total_products) AS total_products, SUM(total_shipping) AS total_shipping, SUM(total_wholesale_price) AS total_wholesale_price, SUM(rate_amount) AS rate_amount, AVG(`rate`) AS `rate`, AVG(`rate_brand`) AS `rate_brand` FROM '._DB_PREFIX_.'privatesales_stats_order WHERE id_sale IN ('.implode(',',$sales_ids).') AND `date` BETWEEN "'.$date_from->format('Y-m-d').'" AND "'.$date_to->format('Y-m-d').'" GROUP BY id_sale ORDER BY '.$order_by_req; $totals = Db::getInstance()->executeS($req); } else { $totals = Db::getInstance()->executeS('SELECT * FROM '._DB_PREFIX_.'privatesales_stats_order_global WHERE id_sale IN ('.implode(',',$sales_ids).') GROUP BY id_sale ORDER BY '.$order_by_req); } if(count($totals) == 0) return array(); $array_stats = array(); foreach($totals as $k => $total) { $array_stats[$total['id_sale']] = $total['id_sale']; } foreach($sales_ids as $sale) if(!in_array($sale, $array_stats)) { $array = array(); foreach(array_keys(reset($totals)) as $field) $array[$field] = 0; $array['id_sale'] = $sale; if($order_by != null && $ordered != null) { if($ordered == 'ASC') array_unshift($totals, $array); else $totals[] = $array; } else $totals[] = $array; } $array_stats = $totals; if($date_from !== null && $date_to !== null) { $array_stats['total'] = Db::getInstance()->getRow(' SELECT b.*, ROUND(b.total_total_products_price_tax_excl - b.total_total_wholesale_price, 2) as rate_amount, ROUND( (b.total_total_products_price_tax_excl - b.total_total_wholesale_price) / b.total_total_wholesale_price, 3) as rate, ROUND( (b.total_total_products_price_tax_excl - b.total_total_wholesale_price) / b.total_total_products_price_tax_excl, 3) as rate_brand FROM ( SELECT SUM(total_products_price) as total_total_products_price, SUM(total_products_price_tax_excl) as total_total_products_price_tax_excl, SUM(total_shipping) as total_total_shipping, SUM(total_products) as total_total_products, SUM(total_wholesale_price) as total_total_wholesale_price FROM ( '.$req.' ) a ) b '); } else { $array_stats['total'] = Db::getInstance()->getRow(' SELECT a.*, ROUND(a.total_total_products_price_tax_excl - a.total_total_wholesale_price, 3) as rate_amount, ROUND( (a.total_total_products_price_tax_excl - a.total_total_wholesale_price) / a.total_total_wholesale_price, 3) as rate, ROUND( (a.total_total_products_price_tax_excl - a.total_total_wholesale_price) / a.total_total_products_price_tax_excl, 3) as rate_brand FROM ( SELECT SUM(total_products_price) as total_total_products_price, SUM(total_products_price_tax_excl) as total_total_products_price_tax_excl, SUM(total_shipping) as total_total_shipping, SUM(total_products) as total_total_products, SUM(total_wholesale_price) as total_total_wholesale_price FROM '._DB_PREFIX_.'privatesales_stats_order_global WHERE id_sale IN ('.implode(',',$sales_ids).') ) a '); } //BUG DE LA CLE 0 $array_final = array(); foreach($array_stats as $k => $v) $array_final[(Validate::isInt($k) ? ($k+1) : $k)] = $v; return $array_final; break; } } public function getStats($sale, $type = 'global', DateTime $date_from = null, DateTime $date_to = null, $order_by = null, $ordered = null) { $date_req = ''; if($date_to !== null && $date_from !== null) $date_req = ' AND date BETWEEN \''.$date_from->format('Y-m-d').'\' AND \''.$date_to->format('Y-m-d').'\''; if($order_by != null && $ordered != null) $order_by_req = $order_by.' '.$ordered; else switch($type) { case 'product': $order_by_req = 'product_name ASC'; break; case 'order': $order_by_req = 'date DESC'; break; case 'global': $order_by_req = 'id_sale ASC'; break; } switch($type) { case 'global': case 'summary': if($date_req != '') { $req = 'SELECT id_sale, SUM(total_products_price) AS total_products_price, SUM(total_products_price_tax_excl) as total_products_price_tax_excl, SUM(total_products) AS total_products, SUM(total_wholesale_price) AS total_wholesale_price, SUM(rate_amount) AS rate_amount, AVG(`rate`) AS `rate`, AVG(`rate_brand`) AS `rate_brand` FROM '._DB_PREFIX_.'privatesales_stats_order WHERE id_sale = '.(int)$sale->id.' '.$date_req.' GROUP BY id_sale'; return Db::getInstance()->getRow($req); } else { return Db::getInstance()->getRow('SELECT * FROM '._DB_PREFIX_.'privatesales_stats_order_global WHERE id_sale = '.$sale->id); } break; case 'product': return Db::getInstance()->executeS('SELECT id_sale, product_id, reference, supplier_reference, SUM(total_products_price) AS total_total_products_price, SUM(total_products_price_tax_excl) AS total_total_products_price_tax_excl, SUM(total_products) AS total_total_products, SUM(rate_amount) AS rate_amount, AVG(`rate`) AS `rate`, AVG(`rate_brand`) AS `rate_brand`, SUM(total_products_wholesale_price) AS total_total_products_wholesale_price, name as product_name FROM '._DB_PREFIX_.'privatesales_stats_products a WHERE id_sale = '.(int)$sale->id.' '.$date_req.' GROUP BY id_sale, product_id, product_attribute_id ORDER BY '.$order_by_req.' '); break; case 'order': return Db::getInstance()->executeS('SELECT id_sale, `date`, SUM(total_products_price) AS total_total_products_price, SUM(total_products_price_tax_excl) AS total_products_price_tax_excl, SUM(total_products) AS total_total_products, SUM(total_wholesale_price) AS total_total_wholesale_price, SUM(rate_amount) AS rate_amount, AVG(`rate_brand`) AS `rate_brand`, AVG(`rate`) AS `rate` FROM '._DB_PREFIX_.'privatesales_stats_order WHERE id_sale = '.$sale->id.' '.$date_req.' GROUP BY id_sale, date ORDER BY '.$order_by_req.' '); break; } } /*******************************FIN RECUPERATION*********************************/ /*******************************EXPORT*********************************/ public static function getSaleExport(SaleCoreExtend $sale, $order_states = array(), $type = 1, DateTime $date_from, DateTime $date_to) { //TYPE 1 = BDC ; TYPE 2 = MARGE if(count($order_states) == 0) throw new Exception("Aucun statut de commande sélectionné"); $ids_products = $sale->getProducts(); if(count($ids_products) == 0) throw new Exception("Aucun produit pour cette vente"); $orders = self::getProductsOrder($ids_products, $order_states, $date_from, $date_to); self::$array_product = array(); foreach($ids_products as $id_product) self::$array_product[$id_product] = new Product($id_product); $lines = self::getSaleExportLine($orders, $type); if(count($lines) == 0) throw new Exception("Aucune vente pour cette vente privée"); return $lines; } public static function getProductsOrder($ids_products = array(), $order_states = array(), DateTime $date_from = null, DateTime $date_to = null) { if($date_from !== null && $date_to !== null) { $format = 'Y-m-d H:i:s'; $date_from = $date_from->format($format); $date_to = $date_to->format($format); if(!Validate::isDate($date_from) || !Validate::isDate($date_to)) throw new PrestaShopException(Tools::displayError('Invalid date')); } $req = 'SELECT od.* FROM '._DB_PREFIX_.'order_detail od RIGHT JOIN '._DB_PREFIX_.'orders o ON ( od.id_order = o.id_order '.($date_from !== null && $date_to !== null && Validate::isDate($date_from) && Validate::isDate($date_to) ? 'AND o.date_add >= "'.$date_from.'" AND o.date_add <= "'.$date_to.'"' : '' ).' ) WHERE (SELECT id_order_state FROM '._DB_PREFIX_.'order_history oh WHERE o.id_order = oh.id_order ORDER BY id_order_history DESC LIMIT 1) IN ("'.implode('","', $order_states).'") AND product_id IN ("'.implode('","', $ids_products).'")'; $res = Db::getInstance()->executeS($req); return $res; } public static function getProductPriceWithReduction($values) { return (float)( $values['product_price']*( 1-($values['reduction_percent']/100) ) ); } public static function getSaleExportLine($lines, $type, $by_product_price = false) { $arrayTmp = array(); $g = 0; foreach($lines as $line) { if( ($line['product_quantity'] - $line['product_quantity_reinjected']) == 0) continue; if(!isset($arrayTmp[$line['product_id']])) $arrayTmp[$line['product_id']] = array(); $product_price = self::getProductPriceWithReduction($line); if($by_product_price) { if(!isset($arrayTmp[$line['product_id']][$line['product_attribute_id']])) $arrayTmp[$line['product_id']][$line['product_attribute_id']] = array(); if(!isset($arrayTmp[$line['product_id']][$line['product_attribute_id']][$product_price])) $arrayTmp[$line['product_id']][$line['product_attribute_id']][$product_price] = array('line' => $line, 'total' => 0); } else { if(!isset($arrayTmp[$line['product_id']][$line['product_attribute_id']])) $arrayTmp[$line['product_id']][$line['product_attribute_id']] = array('line' => $line, 'total' => 0); } $quantity = $line['product_quantity'] - $line['product_quantity_reinjected']; if($by_product_price) $arrayTmp[$line['product_id']][$line['product_attribute_id']][$product_price]['total'] += $quantity; else $arrayTmp[$line['product_id']][$line['product_attribute_id']]['total'] += $quantity; } return $lines = $arrayTmp; } /*******************************FIN EXPORT*********************************/ }