Пример #1
0
 /**
  * calculates AU history after connection
  * 
  * @param current day's timestamp
  * @param user
  * @param date of first event
  * @param starting point
  *
  * @return array of int
  */
 public static function calculateHistory($timestamp, $user, $firstDate, $baseAU)
 {
     // return array
     $historyAU = array();
     $currentDate = date('Y-m-d', $timestamp);
     // save the first one
     $historyAU[date('Y-m-d', $timestamp)] = $baseAU;
     while ($timestamp >= strtotime($firstDate)) {
         $events = Event::where('user', $user->id)->where('date', $currentDate)->get();
         // set the new current time
         $timestamp -= 86400;
         $currentDate = date('Y-m-d', $timestamp);
         $historyAU[$currentDate] = self::calculate($baseAU, $events, -1);
         // set the new base value
         $baseAU = $historyAU[$currentDate];
     }
     return $historyAU;
 }
Пример #2
0
 /**
  * Prepare data for full statistics (for single_stat)
  *
  * @return array
  */
 public static function showFullStat($metrics)
 {
     // helpers
     $currentDay = time();
     $lastMonthTime = $currentDay - 30 * 24 * 60 * 60;
     $twoMonthTime = $currentDay - 2 * 30 * 24 * 60 * 60;
     $threeMonthTime = $currentDay - 3 * 30 * 24 * 60 * 60;
     $sixMonthTime = $currentDay - 6 * 30 * 24 * 60 * 60;
     $nineMonthTime = $currentDay - 9 * 30 * 24 * 60 * 60;
     $lastYearTime = $currentDay - 365 * 24 * 60 * 60;
     // return array
     $data = array();
     $data = self::showSimpleStat($metrics);
     $firstDay = Event::where('user', Auth::user()->id)->orderBy('date', 'asc')->first();
     if ($firstDay) {
         $firstDay = strtotime($firstDay->date);
     } else {
         $firstDay = time();
     }
     $data['firstDay'] = date('d-m-Y', $firstDay);
     $fullMetricHistory = Metric::where('user', Auth::user()->id)->orderBy('date', 'asc')->get();
     foreach ($fullMetricHistory as $metric) {
         $data['fullHistory'][$metric->date] = $metric->cancellations;
     }
     if (!isset($data['fullHistory'])) {
         $data['fullHistory'] = array();
     }
     // past values (null if not available)
     $lastMonthValue = static::getIndicatorStatOnDay($lastMonthTime);
     $twoMonthValue = static::getIndicatorStatOnDay($twoMonthTime);
     $threeMonthValue = static::getIndicatorStatOnDay($threeMonthTime);
     $sixMonthValue = static::getIndicatorStatOnDay($sixMonthTime);
     $nineMonthValue = static::getIndicatorStatOnDay($nineMonthTime);
     $oneYearValue = static::getIndicatorStatOnDay($lastYearTime);
     // 30 days ago
     $data['oneMonth'] = $lastMonthValue;
     // 6 months ago
     $data['sixMonth'] = $sixMonthValue;
     // 1 year ago
     $data['oneYear'] = $oneYearValue;
     // check if data is available, so we don't divide by null
     // we have 30 days change
     if ($twoMonthValue) {
         $changeInPercent = $data['currentValue'] / $twoMonthValue * 100 - 100;
         $data['twoMonthChange'] = round($changeInPercent) . '%';
     } else {
         $data['twoMonthChange'] = null;
     }
     if ($threeMonthValue) {
         $changeInPercent = $data['currentValue'] / $threeMonthValue * 100 - 100;
         $data['threeMonthChange'] = round($changeInPercent) . '%';
     } else {
         $data['threeMonthChange'] = null;
     }
     if ($sixMonthValue) {
         $changeInPercent = $data['currentValue'] / $sixMonthValue * 100 - 100;
         $data['sixMonthChange'] = round($changeInPercent) . '%';
     } else {
         $data['sixMonthChange'] = null;
     }
     if ($nineMonthValue) {
         $changeInPercent = $data['currentValue'] / $nineMonthValue * 100 - 100;
         $data['nineMonthChange'] = round($changeInPercent) . '%';
     } else {
         $data['nineMonthChange'] = null;
     }
     if ($oneYearValue) {
         $changeInPercent = $data['currentValue'] / $oneYearValue * 100 - 100;
         $data['oneYearChange'] = round($changeInPercent) . '%';
     } else {
         $data['oneYearChange'] = null;
     }
     // time interval for shown statistics
     // right now, only last 30 days
     $startDate = date('d-m-Y', $lastMonthTime);
     $stopDate = date('d-m-Y', $currentDay);
     $data['dateInterval'] = array('startDate' => $startDate, 'stopDate' => $stopDate);
     // positiveIsGood, for front end colors
     $data['positiveIsGood'] = false;
     return $data;
 }
Пример #3
0
 /**
  * Format events to be displayed on dashboard
  * @param user object
  *
  * @return array of formatted events
  */
 public static function formatEvents($user)
 {
     $eventArray = array();
     // return array
     $tempArray = array();
     // helper
     $prevTempArray = array();
     // previous values
     // last X events from database
     // select only those event types, which we show on dashboard
     $events = Event::where('user', $user->id)->whereIn('type', ['charge.succeeded', 'charge.failed', 'charge.captured', 'charge.refunded', 'customer.created', 'customer.deleted', 'customer.subscription.created', 'customer.subscription.updated', 'customer.subscription.deleted', 'customer.discount.created', 'customer.discount.updated', 'customer.discount.deleted'])->orderBy('created', 'desc')->take(20)->get();
     $i = 0;
     foreach ($events as $id => $event) {
         if ($event->provider != 'connect') {
             try {
                 // decoding object
                 $tempArray = json_decode(strstr($event->object, '{'), true);
                 $prevTempArray = !is_null($event->previousAttributes) ? json_decode(strstr($event->previousAttributes, '{'), true) : null;
                 // formatting and creating data for return array
                 // type eg. 'charge.succeeded'
                 $eventArray[$i]['type'] = $event->type;
                 // provider eg. 'stripe'
                 $eventArray[$i]['provider'] = $event->provider;
                 // date eg. '02-11 20:44'
                 $eventArray[$i]['date'] = date('m-d H:i', strtotime($event->created));
                 // name eg. 'chris'
                 if (array_key_exists('card', $tempArray)) {
                     if (array_key_exists('name', $tempArray['card'])) {
                         if ($tempArray['card']['name']) {
                             $eventArray[$i]['name'] = $tempArray['card']['name'];
                         } else {
                             $eventArray[$i]['name'] = 'Someone';
                         }
                     } else {
                         $eventArray[$i]['name'] = 'Another guy';
                     }
                 } else {
                     $eventArray[$i]['name'] = 'Some company';
                 }
                 // currency
                 if (array_key_exists('currency', $tempArray)) {
                     $eventArray[$i]['currency'] = $tempArray['currency'];
                 } elseif (array_key_exists('plan', $tempArray)) {
                     if (array_key_exists('currency', $tempArray['plan'])) {
                         $eventArray[$i]['currency'] = $tempArray['plan']['currency'];
                     }
                 } else {
                     $eventArray[$i]['currency'] = null;
                 }
                 // amount paid
                 if (array_key_exists('amount', $tempArray)) {
                     $eventArray[$i]['amount'] = $tempArray['amount'];
                 } elseif (array_key_exists('amount_due', $tempArray)) {
                     $eventArray[$i]['amount'] = $tempArray['amount_due'];
                 } elseif (array_key_exists('plan', $tempArray)) {
                     if (array_key_exists('amount', $tempArray['plan'])) {
                         $eventArray[$i]['amount'] = $tempArray['plan']['amount'];
                     }
                 } elseif (array_key_exists('amount_refunded', $tempArray)) {
                     $eventArray[$i]['amount'] = $tempArray['amount_refunded'];
                 } else {
                     $eventArray[$i]['amount'] = null;
                 }
                 // plan name
                 if (array_key_exists('plan', $tempArray)) {
                     if (array_key_exists('name', $tempArray['plan'])) {
                         $eventArray[$i]['plan_name'] = $tempArray['plan']['name'];
                     }
                 }
                 // plan interval
                 if (array_key_exists('plan', $tempArray)) {
                     switch ($tempArray['plan']['interval']) {
                         case 'day':
                             $eventArray[$i]['plan_interval'] = 'daily';
                             break;
                         case 'week':
                             $eventArray[$i]['plan_interval'] = 'weekly';
                             break;
                         case 'month':
                             $eventArray[$i]['plan_interval'] = 'monthly';
                             break;
                         case 'year':
                             $eventArray[$i]['plan_interval'] = 'yearly';
                             break;
                         default:
                             // don't do anything
                     }
                 }
                 // previous plan ID, name, interval and amount
                 if (!is_null($prevTempArray)) {
                     if (array_key_exists('plan', $prevTempArray)) {
                         $eventArray[$i]['prevPlanID'] = $prevTempArray['plan']['id'];
                         $eventArray[$i]['prevPlanName'] = $prevTempArray['plan']['name'];
                         $eventArray[$i]['prevPlanAmount'] = $prevTempArray['plan']['amount'];
                         switch ($prevTempArray['plan']['interval']) {
                             case 'day':
                                 $eventArray[$i]['prevPlanInterval'] = 'daily';
                                 break;
                             case 'week':
                                 $eventArray[$i]['prevPlanInterval'] = 'weekly';
                                 break;
                             case 'month':
                                 $eventArray[$i]['prevPlanInterval'] = 'daily';
                                 break;
                             case 'year':
                                 $eventArray[$i]['prevPlanInterval'] = 'yearly';
                                 break;
                             default:
                                 // don't do anything
                         }
                     }
                 }
                 // if the event is a coupon event
                 if (isset($tempArray['coupon'])) {
                     $eventArray[$i]['newCoupon'] = $tempArray['coupon']['id'];
                     if (!is_null($prevTempArray)) {
                         $eventArray[$i]['prevCoupon'] = $prevTempArray['coupon']['id'];
                     }
                 }
                 $i++;
             } catch (Exception $e) {
             }
         }
         // /if - provider
     }
     // /foreach - events
     return $eventArray;
 }
 private static function getAU($user, $customers, $previousValue = 0)
 {
     $au = $previousValue;
     foreach ($customers as $customer) {
         foreach ($customer->paymentMethods() as $paymentMethod) {
             if (isset($paymentMethod->_attributes['subscriptions'])) {
                 foreach ($paymentMethod->_attributes['subscriptions'] as $subscription) {
                     if ($subscription->status == Braintree_Subscription::ACTIVE) {
                         $au++;
                         // needs a new custom events so the calculator runs normal
                         $newEvent = new Event();
                         $newEvent->date = Carbon::now()->format('Y-m-d');
                         $newEvent->eventID = str_random(32);
                         $newEvent->user = $user->id;
                         $newEvent->created = Carbon::now();
                         $newEvent->provider = 'connect';
                         $newEvent->type = 'customer.created';
                         $newEvent->object = '{}';
                         $newEvent->previousAttributes = null;
                         $newEvent->save();
                     }
                     // /if active
                 }
                 // /foreach subscription
             }
             // /if isset
         }
         // /foreach paymentMethod
     }
     // /foreach customer
     return $au;
 }