bebeboutik-scripts/cron_weekly_stats.php
2017-12-18 17:14:31 +01:00

203 lines
7.1 KiB
PHP

<?php
if(isset($_SERVER['REMOTE_ADDR'])) exit;
$_SERVER['SERVER_PORT'] = 80;
$_SERVER['HTTP_HOST'] = 'www.bebeboutik.com';
include dirname(__FILE__).'/www/config/config.inc.php';
include dirname(__FILE__).'/www/init.php';
include dirname(__FILE__).'/www/modules/privatesales/Sale.php';
include dirname(__FILE__).'/www/modules/privatesales_logistique/classes/OrderForm.php';
include dirname(__FILE__).'/www/modules/privatesales_logistique/classes/MakeStats.php';
include dirname(__FILE__).'/www/modules/privatesales_logistique/classes/SaleStats.php';
$now = $argv[1].' 00:00:00';
$f = fopen('extracts/sales_stats/'.date('Y-m-d', strtotime($now)).'.csv', 'w');
$headers = array(
'sale_id',
'name',
'date_start',
'date_end',
'duration',
'nb_ref',
'nb_quantity_start',
'nb_order_simple',
'nb_order_multi',
'nb_order_total',
'taux_multi',
'quantity_sale',
'percent_in_stock',
'percent_out_of_stock',
'ca_ttc_product',
'bdc_ht',
'marge',
'bestsale1',
'quantity',
'ht',
'bestsale2',
'quantity',
'ht',
'bestsale3',
'quantity',
'ht',
'category_title',
'nb_id',
'quantity_sold',
'total_buy_ht',
'category_title',
'nb_id',
'quantity_sold',
'total_buy_ht',
'category_title',
'nb_id',
'quantity_sold',
'total_buy_ht',
'category_title',
'nb_id',
'quantity_sold',
'total_buy_ht',
'category_title',
'nb_id',
'quantity_sold',
'total_buy_ht',
'category_title',
'nb_id',
'quantity_sold',
'total_buy_ht',
'category_title',
'nb_id',
'quantity_sold',
'total_buy_ht',
'category_title',
'nb_id',
'quantity_sold',
'total_buy_ht',
'category_title',
'nb_id',
'quantity_sold',
'total_buy_ht',
);
fwrite($f, implode(';', $headers)."\n");
$id_lang = 2;
$sales = Db::getInstance()->ExecuteS('
SELECT `id_sale`, `id_category`
FROM `'._DB_PREFIX_.'privatesale`
WHERE `date_end` >= DATE_FORMAT("'.$now.'" - INTERVAL 7 DAY, \'%Y-%m-%d 00:00:00\')
AND `date_end` < DATE_FORMAT("'.$now.'", \'%Y-%m-%d 00:00:00\')
ORDER BY `date_end`
');
//echo "<pre>";var_dump($sales);echo "</pre>";die();
foreach($sales as $row) {
//echo $row['id_sale']." - ".$row['id_category']."\n";
$id_category = (int) $row['id_category'];
$category = new Category($id_category, $id_lang);
$sale = new Sale((int)$row['id_sale']);
$products = $category->getProductsStats($id_lang , "1", "1000000000000000000", "reference",NULL , FALSE, FALSE , FALSE );
$arrayProduct = array();
foreach($products as $product)
$array_product[$product['id_product']] = $arrayProduct[$product['id_product']] = new Product($product['id_product']);
$order_states = explode(',', Configuration::get('PS_IT_OF_ORDER_STATES'));
$lines = MakeStats::getOrdersByIdsProduct(array_keys($arrayProduct), $order_states);
// for stats
$lines_tmp = $lines;
$lines = MakeStats::getArrayLines($lines);
$stats = new SaleStats($sale, $category);
$stats->loadData();
$total_sale_product = array();
$total_sale_ht = 0;
foreach ($lines_tmp as $key => $order_line) {
$id_product = $order_line['product_id'];
$id_product_attribute = $order_line['product_attribute_id'];
$current_product = $array_product[$id_product];
if($id_product_attribute != 0) {
$combination = new Combination($id_product_attribute);
if($combination->wholesale_price == 0) {
$wholesale_price = $current_product->wholesale_price;
} else {
$wholesale_price = $combination->wholesale_price;
}
} else {
$wholesale_price = $current_product->wholesale_price;
}
$quantity_to_cmd = $order_line['product_quantity'] - $order_line['product_quantity_reinjected'];
$total_sale_ht += $quantity_to_cmd * $wholesale_price;
if (!is_array($total_sale_product[(int)$id_product]) ){
$total_sale_product[(int)$id_product] = array();
$total_sale_product[(int)$id_product]['total_ht'] = 0;
$total_sale_product[(int)$id_product]['quantiy'] = 0;
$total_sale_product[(int)$id_product]['name'] = $current_product->name[2];
}
$total_sale_product[(int)$id_product]['total_ht'] += ($wholesale_price * $quantity_to_cmd);
$total_sale_product[(int)$id_product]['quantiy'] += $quantity_to_cmd;
}
$stats->addToStats('total_bdc_ht', $total_sale_ht);
$bestsales = MakeStats::orderMultiArray($total_sale_product, 'total_ht');
$bestsales_extract = array_slice($bestsales, 0, 3);
$i = 1;
foreach ($bestsales_extract as $key => $bestsalesProduct) {
$stats->addToStats('bestsale_'.$i.'_product', $bestsalesProduct['name']);
$stats->addToStats('bestsale_'.$i.'_quantity', $bestsalesProduct['quantiy']);
$stats->addToStats('bestsale_'.$i.'_ca', $bestsalesProduct['total_ht']);
$i++;
}
$stats->loadDataForSubCategory();
$csv_row = array(
$row['id_sale'],
$stats->data_stats['name'],
$stats->data_stats['date_start'],
$stats->data_stats['date_end'],
$stats->data_stats['duration'],
$stats->data_stats['nb_ref'],
$stats->data_stats['nb_quantity_start'],
$stats->data_stats['nb_order_simple'],
$stats->data_stats['nb_order_multi'],
$stats->data_stats['nb_order_total'],
$stats->data_stats['taux_multi'],
$stats->data_stats['quantity_sale'],
$stats->data_stats['percent_sale'],
$stats->data_stats['percent_out_of_stock'],
Tools::displayPrice($stats->data_stats['ca_ttc']),
Tools::displayPrice($stats->data_stats['total_bdc_ht']),
$stats->data_stats['marge'],
(isset($stats->data_stats['bestsale_1_product'])?$stats->data_stats['bestsale_1_product']:''),
(isset($stats->data_stats['bestsale_1_quantity'])?$stats->data_stats['bestsale_1_quantity']:''),
(isset($stats->data_stats['bestsale_1_ca'])?Tools::displayPrice($stats->data_stats['bestsale_1_ca']):0),
(isset($stats->data_stats['bestsale_2_product'])?$stats->data_stats['bestsale_2_product']:''),
(isset($stats->data_stats['bestsale_2_quantity'])?$stats->data_stats['bestsale_2_quantity']:''),
(isset($stats->data_stats['bestsale_2_ca'])?Tools::displayPrice($stats->data_stats['bestsale_2_ca']):0),
(isset($stats->data_stats['bestsale_3_product'])?$stats->data_stats['bestsale_3_product']:''),
(isset($stats->data_stats['bestsale_3_quantity'])?$stats->data_stats['bestsale_3_quantity']:''),
(isset($stats->data_stats['bestsale_3_ca'])?Tools::displayPrice($stats->data_stats['bestsale_3_ca']):0),
);
foreach ($stats->data_subcategories as $key => $subcategories) {
foreach ($subcategories as $key_2 => $subcategory) {
if ($key_2 == 'total_amount_wholesale_price') {
$csv_row[] = Tools::displayPrice($subcategory);
} else {
$csv_row[]= $subcategory;
}
$i++;
}
}
fwrite($f, implode(';', $csv_row)."\n");
}
fclose($f);
exit;