diff --git a/include/controllers/default/cockpit/home/charts.php b/include/controllers/default/cockpit/home/charts.php index c6012fb4f..acc746ab5 100644 --- a/include/controllers/default/cockpit/home/charts.php +++ b/include/controllers/default/cockpit/home/charts.php @@ -450,6 +450,62 @@ class Controller_home_charts extends Crunchbutton_Controller_Account { ]]); break; + case 'reclaimed-users': + $query = "SELECT yearweek AS Week, + COUNT(*) AS Total + FROM + (SELECT last.total AS total, + lastbutone.id_order AS id_order_last_but_one, + lastbutone.date AS date_last_but_one, + last.id_order AS id_order_last, + last.date AS date_last, + lastbutone.phone AS phone, + YEARWEEK(last.date) AS yearweek, + DATEDIFF(last.date, lastbutone.date) AS days + FROM + (SELECT * + FROM + (SELECT count(*) AS total, + max(orders.id_order) AS id_order, + max(orders.date) AS date, + orders.phone + FROM + (SELECT o.id_order, + o.date, o.phone + FROM `order` o) orders + GROUP BY phone HAVING total > 1) orders) last + INNER JOIN + (SELECT o.id_order, + o.phone, + o.date + FROM `order` o + INNER JOIN + (SELECT MAX(o.id_order) AS id_order , + o.phone, + o.date + FROM `order` o + INNER JOIN + (SELECT id_order, + phone + FROM + (SELECT count(*) AS total, + max(id_order) AS id_order, + phone + FROM `order` + GROUP BY phone HAVING total > 1) orders) last ON last.phone = o.phone + AND last.id_order > o.id_order + GROUP BY phone) lastbutone ON lastbutone.id_order = o.id_order) lastbutone ON last.phone = lastbutone.phone) orders + WHERE days >= {$this->activeUsersInterval} + AND yearweek >= {$this->weekFrom} + AND yearweek <= {$this->weekTo} + GROUP BY yearweek"; + + $data = $this->parseDataWeeksSimple( $query, 'Users' ); + + $this->render( array( 'data' => $data, 'unit' => 'users' ) ); + + break; + case 'gross-revenue': $query = "SELECT YEARWEEK(date) AS `Week`, diff --git a/include/controllers/default/cockpit/home/index.php b/include/controllers/default/cockpit/home/index.php index a8fe8293f..58fc60c45 100644 --- a/include/controllers/default/cockpit/home/index.php +++ b/include/controllers/default/cockpit/home/index.php @@ -35,7 +35,6 @@ class Controller_home extends Crunchbutton_Controller_Account { $graphs = array( 'Stuff for Investors' => array( - 'active-users-per-week' => 'Active Users per Week', 'active-users-per-week-by-community' => 'Active Users per Week by Community', 'churn-rate' => 'Churn Rate - Active Users Lost', @@ -51,6 +50,7 @@ class Controller_home extends Crunchbutton_Controller_Account { 'repeat-orders-per-active-user' => 'Repeat Orders per Active User', 'gross-revenue' => 'Gross Revenue', 'active-users-by-community' => 'Active Users by Community', + 'reclaimed-users' => 'Reclaimed Users', ), 'Tracking Marketing Efforts' => array( 'active-users-per-week' => 'Active Users per Week', diff --git a/include/library/Crunchbutton/Newusers.php b/include/library/Crunchbutton/Newusers.php index 0f38d7f74..73736ead3 100644 --- a/include/library/Crunchbutton/Newusers.php +++ b/include/library/Crunchbutton/Newusers.php @@ -42,6 +42,29 @@ class Crunchbutton_Newusers extends Cana_Table { echo 'Sent ' . $orders->count() . ' emails!'; } + public function isFirstOrderOfPhone( $phone ){ + $orders = Crunchbutton_Order::q( "SELECT * FROM `order` o WHERE phone = '{$phone}'" );; + return ( $orders->count() == 1 ); + } + + public static function newUserInfo( $order ){ + + $config = static::getConfig(); + + $user = $order->user(); + $email = $config->email_to; + $subject = $user->name . ' placed their first CB order'; + + $mail = new Crunchbutton_Email_Newusers([ + 'subject' => $subject, + 'email' => $email, + 'order' => $order, + 'user' => $user + ]); + + $mail->send(); + } + public static function queSendEmail(){ $config = static::getConfig(); diff --git a/include/views/default/cockpit/home/index.phtml b/include/views/default/cockpit/home/index.phtml index 7c61ca5c0..5182bbfb8 100644 --- a/include/views/default/cockpit/home/index.phtml +++ b/include/views/default/cockpit/home/index.phtml @@ -217,7 +217,7 @@ if( check.is( ':checked' ) ){ chart.show(); if( !chart.attr( 'opened' ) ){ - loadChart( chartId ); + loadChart( chartId, true ); } } else { chart.attr( 'opened', false ); @@ -233,7 +233,7 @@ $( '.chart' ).each( function(){ var chart = $( this ); if( chart.attr( 'opened' ) ){ - loadChart( chart.attr( 'id' ) ); + loadChart( chart.attr( 'id' ), true ); $( '#button-reload' ).find( 'i' ).addClass( 'icon-spin ' ); setTimeout( function(){ $( '#button-reload' ).find( 'i' ).removeClass( 'icon-spin ' ); @@ -246,7 +246,6 @@ var url = '/home/charts/weeks'; $.ajax( { dataType: 'json', url: url, } ).done( function( weeks ) { - console.log( weeks ); var total = weeks.length; $( '#slider-label' ).html( 'Show from ' + weeks[ 0 ] + ' to ' + weeks[ total - 1 ] + '' ); $( '#slider-master' ).slider( { @@ -263,7 +262,7 @@ } ); } ); - function loadChart( chartId ){ + function loadChart( chartId, force ){ var chart = $( '#' + chartId ); chart.show(); var title = chart.attr( 'data-title' ); @@ -277,14 +276,16 @@ slider.css( 'opacity', 0.4 ); sliderValues = '&to=' + slider.attr( 'data-to' ) + '&from=' + slider.attr( 'data-from' ); } - chart.attr( 'opened', true ); var weeksStr = ''; - if( weekFrom ){ - weeksStr += '&from=' + weekFrom; - } - if( weekTo ){ - weeksStr += '&to=' + weekTo; + if( force ){ + if( weekFrom && weekFrom != '' ){ + weeksStr += '&from=' + weekFrom; + } + if( weekTo && weekTo != '' ){ + weeksStr += '&to=' + weekTo; + } } + chart.attr( 'opened', true ); var activeUserDays = $( '#active-user-days' ).val(); var url = '/home/charts/' + id + '/' + title + '/' + count + '?activeUserDays=' + activeUserDays + sliderValues + weeksStr; $.ajax( { url: url, }).done( function( data ) { chart.html( data ); } );