From 8df63f256ddd55a6e8878241e1288bef39416749 Mon Sep 17 00:00:00 2001 From: Damien Metzger Date: Fri, 6 Sep 2013 16:25:37 +0200 Subject: [PATCH] // Added chart data --- modules/dashtrends/dashtrends.php | 176 +++++++++++++++++----- modules/dashtrends/views/js/dashtrends.js | 1 - 2 files changed, 140 insertions(+), 37 deletions(-) diff --git a/modules/dashtrends/dashtrends.php b/modules/dashtrends/dashtrends.php index 6f01cc4da..fbe16313e 100644 --- a/modules/dashtrends/dashtrends.php +++ b/modules/dashtrends/dashtrends.php @@ -58,6 +58,23 @@ class Dashtrends extends Module return $this->display(__FILE__, 'dashboard_zone_two.tpl'); } + protected $dashboard_data = array( + 'sales' => array(), + 'orders' => array(), + 'average_cart_value' => array(), + 'visits' => array(), + 'conversion_rate' => array(), + 'net_profits' => array() + ); + protected $dashboard_data_sum = array( + 'sales' => 0, + 'orders' => 0, + 'average_cart_value' => 0, + 'visits' => 0, + 'conversion_rate' => 0, + 'net_profits' => 0 + ); + public function hookDashboardData($params) { $gapi = Module::isInstalled('gapi') ? Module::getInstanceByName('gapi') : false; @@ -75,40 +92,104 @@ class Dashtrends extends Module WHERE `date_add` BETWEEN "'.pSQL($params['date_from']).'" AND "'.pSQL($params['date_to']).'" '.Shop::addSqlRestriction(false)); } - $row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' + + $data = array( + 'orders_score' => array(), + 'total_paid_tax_excl' => array(), + 'total_discounts_tax_excl' => array(), + 'total_credit_tax_excl' => array(), + 'total_credit_shipping_tax_excl' => array(), + 'total_product_price_tax_excl' => array(), + 'total_purchase_price' => array() + ); + + $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' SELECT + LEFT(`invoice_date`, 10) as date, COUNT(`id_order`) as orders_score, SUM(`total_paid_tax_excl` / `conversion_rate`) as total_paid_tax_excl, SUM(`total_discounts_tax_excl` / `conversion_rate`) as total_discounts_tax_excl FROM `'._DB_PREFIX_.'orders` WHERE `invoice_date` BETWEEN "'.pSQL($params['date_from']).'" AND "'.pSQL($params['date_to']).'" - '.Shop::addSqlRestriction(Shop::SHARE_ORDER)); - extract($row); - $row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' - SELECT SUM(os.`amount` / o.`conversion_rate`) as total_credit_tax_excl, SUM(os.`shipping_cost_amount` / o.`conversion_rate`) as total_credit_shipping_tax_excl + '.Shop::addSqlRestriction(Shop::SHARE_ORDER).' + GROUP BY LEFT(`invoice_date`, 10)'); + foreach ($result as $row) + foreach (array('orders_score', 'total_paid_tax_excl', 'total_discounts_tax_excl') as $var) + $data[$var][strtotime($row['date'])] = $row[$var]; + + $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' + SELECT + LEFT(os.`date_add`, 10) as date, + SUM(os.`amount` / o.`conversion_rate`) as total_credit_tax_excl, + SUM(os.`shipping_cost_amount` / o.`conversion_rate`) as total_credit_shipping_tax_excl FROM `'._DB_PREFIX_.'orders` o LEFT JOIN `'._DB_PREFIX_.'order_slip` os ON o.id_order = os.id_order WHERE os.`date_add` BETWEEN "'.pSQL($params['date_from']).'" AND "'.pSQL($params['date_to']).'" - '.Shop::addSqlRestriction(Shop::SHARE_ORDER, 'o')); - extract($row); - $row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' + '.Shop::addSqlRestriction(Shop::SHARE_ORDER, 'o').' + GROUP BY LEFT(os.`date_add`, 10)'); + foreach ($result as $row) + foreach (array('total_credit_tax_excl', 'total_credit_shipping_tax_excl') as $var) + $data[$var][strtotime($row['date'])] = $row[$var]; + + $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' SELECT + LEFT(`invoice_date`, 10) as date, SUM(od.`total_price_tax_excl` / `conversion_rate`) as total_product_price_tax_excl, SUM(od.`product_quantity` * od.`purchase_supplier_price` / `conversion_rate`) as total_purchase_price FROM `'._DB_PREFIX_.'orders` o LEFT JOIN `'._DB_PREFIX_.'order_detail` od ON o.id_order = od.id_order WHERE `invoice_date` BETWEEN "'.pSQL($params['date_from']).'" AND "'.pSQL($params['date_to']).'" - '.Shop::addSqlRestriction(Shop::SHARE_ORDER, 'o')); - extract($row); + '.Shop::addSqlRestriction(Shop::SHARE_ORDER, 'o').' + GROUP BY LEFT(`invoice_date`, 10)'); + foreach ($result as $row) + foreach (array('total_product_price_tax_excl', 'total_purchase_price') as $var) + $data[$var][strtotime($row['date'])] = $row[$var]; + + $from = max(strtotime(_PS_CREATION_DATE_.' 00:00:00'), strtotime($params['date_from'].' 00:00:00')); + $to = min(time(), strtotime($params['date_to'].' 23:59:59')); + for ($date = $from; $date <= $to; $date = strtotime('+1 day', $date)) + { + $this->dashboard_data['sales'][$date] = 0; + if (isset($data['total_paid_tax_excl'][$date])) + $this->dashboard_data['sales'][$date] += $data['total_paid_tax_excl'][$date]; + if (isset($data['total_credit_tax_excl'][$date])) + $this->dashboard_data['sales'][$date] -= $data['total_credit_tax_excl'][$date]; + if (isset($data['total_credit_shipping_tax_excl'][$date])) + $this->dashboard_data['sales'][$date] -= $data['total_credit_shipping_tax_excl'][$date]; + + $this->dashboard_data['orders'][$date] = isset($data['orders_score'][$date]) ? $data['orders_score'][$date] : 0; + + $this->dashboard_data['average_cart_value'][$date] = $this->dashboard_data['orders'][$date] ? $this->dashboard_data['sales'][$date] / $this->dashboard_data['orders'][$date] : 0; + + $this->dashboard_data['visits'][$date] = 0; + + $this->dashboard_data['conversion_rate'][$date] = $this->dashboard_data['visits'][$date] ? $this->dashboard_data['orders'][$date] / $this->dashboard_data['visits'][$date] : 0; + + $this->dashboard_data['net_profits'][$date] = 0; + if (isset($data['total_product_price_tax_excl'][$date])) + $this->dashboard_data['net_profits'][$date] += $data['total_product_price_tax_excl'][$date]; + if (isset($data['total_discounts_tax_excl'][$date])) + $this->dashboard_data['net_profits'][$date] -= $data['total_discounts_tax_excl'][$date]; + if (isset($data['total_purchase_price'][$date])) + $this->dashboard_data['net_profits'][$date] -= $data['total_purchase_price'][$date]; + if (isset($data['total_credit_tax_excl'][$date])) + $this->dashboard_data['net_profits'][$date] -= $data['total_credit_tax_excl'][$date]; + } + $this->dashboard_data_sum['sales'] = array_sum($this->dashboard_data['sales']); + $this->dashboard_data_sum['orders'] = array_sum($this->dashboard_data['orders']); + $this->dashboard_data_sum['average_cart_value'] = $this->dashboard_data_sum['sales'] ? $this->dashboard_data_sum['sales'] / $this->dashboard_data_sum['orders'] : 0; + $this->dashboard_data_sum['visits'] = 0; + $this->dashboard_data_sum['conversion_rate'] = $this->dashboard_data_sum['visits'] ? $this->dashboard_data_sum['orders'] / $this->dashboard_data_sum['visits'] : 0; + $this->dashboard_data_sum['net_profits'] = array_sum($this->dashboard_data['net_profits']); return array( 'data_value' => array( - 'sales_score' => Tools::displayPrice($total_paid_tax_excl - $total_credit_tax_excl - $total_credit_shipping_tax_excl), - 'orders_score' => $orders_score, - 'cart_value_score' => Tools::displayPrice($orders_score ? $total_paid_tax_excl / $orders_score : 0), - 'visits_score' => $visits_score, - 'convertion_rate_score' => $visits_score ? round(100 * $orders_score / $visits_score, 2) : 0, - 'net_profits_score' => Tools::displayPrice($total_product_price_tax_excl - $total_discounts_tax_excl - $total_purchase_price - $total_credit_tax_excl), + 'sales_score' => Tools::displayPrice($this->dashboard_data_sum['sales']), + 'orders_score' => $this->dashboard_data_sum['orders'], + 'cart_value_score' => Tools::displayPrice($this->dashboard_data_sum['average_cart_value']), + 'visits_score' => $this->dashboard_data_sum['visits'], + 'convertion_rate_score' => round(100 * $this->dashboard_data_sum['conversion_rate'], 2), + 'net_profits_score' => Tools::displayPrice($this->dashboard_data_sum['net_profits']), ), 'data_trends' => array( 'sales_score_trends' => array('way' => 'up', 'value' => 0.42), @@ -119,34 +200,57 @@ class Dashtrends extends Module 'net_profits_score_trends' => array('way' => 'up', 'value' => 0.42) ), 'data_chart' => array( - 'dash_trends_chart1' => $this->getChartTrends($params['date_from'], $params['date_to']), + 'dash_trends_chart1' => $this->getChartTrends(), ), ); } - public function getChartTrends($date_from, $date_to) + public function getChartTrends() { + $chart_data = array(); + foreach (array_keys($this->dashboard_data) as $chart_key) + { + $calibration = 1; + foreach ($this->dashboard_data[$chart_key] as $value) + if ($value) + { + $calibration = $value; + break; + } + $chart_values = array(); + foreach ($this->dashboard_data[$chart_key] as $key => $value) + $chart_values[] = array(1000 * $key, 100 * $value / $calibration); + $chart_data[$chart_key] = $chart_values; + } + return array( 'chart_type' => 'line_chart_trends', - 'data' => $this->getChartTrendsData($date_from, $date_to) - ); - } - - public function getChartTrendsData($date_from, $date_to) - { - return array( - array( - 'key' => 'Sales', - 'values' => array( - array(1083297600000, -2.974623048543), array(1085976000000, -1.7740300785979), array(1088568000000, 4.4681318138177), array(1091246400000, 7.0242541001353), array(1093924800000, 7.5709603667586), array(1096516800000, 20.612245065736), array(1099195200000, 21.698065237316), array(1101790800000, 40.501189458018), array(1104469200000, 50.464679413194), array(1107147600000, 48.917421973355), array(1109566800000, 63.750936549160), array(1112245200000, 59.072499126460), array(1114833600000, 43.373158880492), array(1117512000000, 54.490918947556), array(1120104000000, 56.661178852079), array(1122782400000, 73.450103545496), array(1125460800000, 71.714526354907), array(1128052800000, 85.221664349607), array(1130734800000, 77.769261392481), array(1133326800000, 95.966528716500), array(1136005200000, 107.59132116397), array(1138683600000, 127.25740096723), array(1141102800000, 122.13917498830), array(1143781200000, 126.53657279774), array(1146369600000, 132.39300992970), array(1149048000000, 120.11238242904), array(1151640000000, 118.41408917750), array(1154318400000, 107.92918924621), array(1156996800000, 110.28057249569), array(1159588800000, 117.20485334692), array(1162270800000, 141.33556756948), array(1164862800000, 159.59452727893), array(1167541200000, 167.09801853304), array(1170219600000, 185.46849659215), array(1172638800000, 184.82474099990), array(1175313600000, 195.63155213887), array(1177905600000, 207.40597044171), array(1180584000000, 230.55966698196), array(1183176000000, 239.55649035292), array(1185854400000, 241.35915085208), array(1188532800000, 239.89428956243), array(1191124800000, 260.47781917715), array(1193803200000, 276.39457482225), array(1196398800000, 258.66530682672), array(1199077200000, 250.98846121893), array(1201755600000, 226.89902618127), array(1204261200000, 227.29009273807), array(1206936000000, 218.66476654350), array(1209528000000, 232.46605902918), array(1212206400000, 253.25667081117), array(1214798400000, 235.82505363925), array(1217476800000, 229.70112774254), array(1220155200000, 225.18472705952), array(1222747200000, 189.13661746552), array(1225425600000, 149.46533007301), array(1228021200000, 131.00340772114), array(1230699600000, 135.18341728866), array(1233378000000, 109.15296887173), array(1235797200000, 84.614772549760), array(1238472000000, 100.60810015326), array(1241064000000, 141.50134895610), array(1243742400000, 142.50405083675), array(1246334400000, 139.81192372672), array(1249012800000, 177.78205544583), array(1251691200000, 194.73691933074), array(1254283200000, 209.00838460225), array(1256961600000, 198.19855877420), array(1259557200000, 222.37102417812), array(1262235600000, 234.24581081250), array(1264914000000, 228.26087689346), array(1267333200000, 248.81895126250), array(1270008000000, 270.57301075186), array(1272600000000, 292.64604322550), array(1275278400000, 265.94088520518), array(1277870400000, 237.82887467569), array(1280548800000, 265.55973314204), array(1283227200000, 248.30877330928), array(1285819200000, 278.14870066912), array(1288497600000, 292.69260960288), array(1291093200000, 300.84263809599), array(1293771600000, 326.17253914628), array(1296450000000, 337.69335966505), array(1298869200000, 339.73260965121), array(1301544000000, 346.87865120765), array(1304136000000, 347.92991526628), array(1306814400000, 342.04627502669), array(1309406400000, 333.45386231233), array(1312084800000, 323.15034181243), array(1314763200000, 295.66126882331), array(1317355200000, 251.48014579253), array(1320033600000, 295.15424257905), array(1322629200000, 294.54766764397), array(1325307600000, 295.72906119051), array(1327986000000, 325.73351347613), array(1330491600000, 340.16106061186), array(1333166400000, 345.15514071490), array(1335758400000, 337.10259395679), array(1338436800000, 318.68216333837), array(1341028800000, 317.03683945246), array(1343707200000, 318.53549659997), array(1346385600000, 332.85381464104), array(1348977600000, 337.36534373477), array(1351656000000, 350.27872156161), array(1354251600000, 349.45128876100) - ) + 'data' => array( + array( + 'key' => $this->l('Sales'), + 'values' => $chart_data['sales'] ), - array( - 'key' => 'Orders', - 'values' => array( - array(1083297600000 , -1.7798428181819) , array( 1085976000000 , -0.36883324836999) , array( 1088568000000 , 1.7312581046040) , array( 1091246400000 , -1.8356125950460) , array( 1093924800000 , -1.5396564170877) , array( 1096516800000 , -0.16867791409247) , array( 1099195200000 , 1.3754263993413) , array( 1101790800000 , 5.8171640898041) , array( 1104469200000 , 9.4350145241608) , array( 1107147600000 , 6.7649081510160) , array( 1109566800000 , 9.1568499314776) , array( 1112245200000 , 7.2485090994419) , array( 1114833600000 , 4.8762222306595) , array( 1117512000000 , 8.5992339354652) , array( 1120104000000 , 9.0896517982086) , array( 1122782400000 , 13.394644048577) , array( 1125460800000 , 12.311842010760) , array( 1128052800000 , 13.221003650717) , array( 1130734800000 , 11.218481009206) , array( 1133326800000 , 15.565352598445) , array( 1136005200000 , 15.623703865926) , array( 1138683600000 , 19.275255326383) , array( 1141102800000 , 19.432433717836) , array( 1143781200000 , 21.232881244655) , array( 1146369600000 , 22.798299192958) , array( 1149048000000 , 19.006125095476) , array( 1151640000000 , 19.151889158536) , array( 1154318400000 , 19.340022855452) , array( 1156996800000 , 22.027934841859) , array( 1159588800000 , 24.903300681329) , array( 1162270800000 , 29.146492833877) , array( 1164862800000 , 31.781626082589) , array( 1167541200000 , 33.358770738428) , array( 1170219600000 , 35.622684613497) , array( 1172638800000 , 33.332821711366) , array( 1175313600000 , 34.878748635832) , array( 1177905600000 , 40.582332613844) , array( 1180584000000 , 45.719535502920) , array( 1183176000000 , 43.239344722386) , array( 1185854400000 , 38.550955100342) , array( 1188532800000 , 40.585368816283) , array( 1191124800000 , 45.601374057981) , array( 1193803200000 , 48.051404337892) , array( 1196398800000 , 41.582581696032) , array( 1199077200000 , 40.650580792748) , array( 1201755600000 , 32.252222066493) , array( 1204261200000 , 28.106390258553) , array( 1206936000000 , 27.532698196687) , array( 1209528000000 , 33.986390463852) , array( 1212206400000 , 36.302660526438) , array( 1214798400000 , 25.015574480172) , array( 1217476800000 , 23.989494069029) , array( 1220155200000 , 25.934351445531) , array( 1222747200000 , 14.627592011699) , array( 1225425600000 , -5.2249403809749) , array( 1228021200000 , -12.330933408050) , array( 1230699600000 , -11.000291508188) , array( 1233378000000 , -18.563864948088) , array( 1235797200000 , -27.213097001687) , array( 1238472000000 , -20.834133840523) , array( 1241064000000 , -12.717886701719) , array( 1243742400000 , -8.1644613083526) , array( 1246334400000 , -7.9108408918201) , array( 1249012800000 , -0.77002391591209) , array( 1251691200000 , 2.8243816569672) , array( 1254283200000 , 6.8761411421070) , array( 1256961600000 , 4.5060912230294) , array( 1259557200000 , 10.487179794349) , array( 1262235600000 , 13.251375597594) , array( 1264914000000 , 9.2207594803415) , array( 1267333200000 , 12.836276936538) , array( 1270008000000 , 19.816793904978) , array( 1272600000000 , 22.156787167211) , array( 1275278400000 , 12.518039090576) , array( 1277870400000 , 6.4253587440854) , array( 1280548800000 , 13.847372028409) , array( 1283227200000 , 8.5454736090364) , array( 1285819200000 , 18.542801953304) , array( 1288497600000 , 23.037064683183) , array( 1291093200000 , 23.517422401888) , array( 1293771600000 , 31.804723416068) , array( 1296450000000 , 34.778247386072) , array( 1298869200000 , 39.584883855230) , array( 1301544000000 , 40.080647664875) , array( 1304136000000 , 44.180050667889) , array( 1306814400000 , 42.533535927221) , array( 1309406400000 , 40.105374449011) , array( 1312084800000 , 37.014659267156) , array( 1314763200000 , 29.263745084262) , array( 1317355200000 , 19.637463417584) , array( 1320033600000 , 33.157645345770) , array( 1322629200000 , 32.895053150988) , array( 1325307600000 , 34.111544824647) , array( 1327986000000 , 40.453985817473) , array( 1330491600000 , 46.435700783313) , array( 1333166400000 , 51.062385488671) , array( 1335758400000 , 50.130448220658) , array( 1338436800000 , 41.035476682018) , array( 1341028800000 , 46.591932296457) , array( 1343707200000 , 48.349391180634) , array( 1346385600000 , 51.913011286919) , array( 1348977600000 , 55.747238313752) , array( 1351656000000 , 52.991824077209) , array( 1354251600000 , 49.556311883284) - ) + array( + 'key' => $this->l('Orders'), + 'values' => $chart_data['orders'] ), - ); + array( + 'key' => $this->l('Average Cart Value'), + 'values' => $chart_data['average_cart_value'] + ), + array( + 'key' => $this->l('Visits'), + 'values' => $chart_data['visits'] + ), + array( + 'key' => $this->l('Conversion Rate'), + 'values' => $chart_data['conversion_rate'] + ), + array( + 'key' => $this->l('Net Profits'), + 'values' => $chart_data['net_profits'] + ) + ) + ); } } \ No newline at end of file diff --git a/modules/dashtrends/views/js/dashtrends.js b/modules/dashtrends/views/js/dashtrends.js index 8b5380496..cd10f3b65 100644 --- a/modules/dashtrends/views/js/dashtrends.js +++ b/modules/dashtrends/views/js/dashtrends.js @@ -1,6 +1,5 @@ function line_chart_trends(widget_name, chart_details) { - console.log(chart_details.data); nv.addGraph(function() { var chart = nv.models.lineChart() .useInteractiveGuideline(true)