Exemple #1
0
 /**
  * Processes customer statistics.
  *
  * @return void
  */
 public static function customer()
 {
     if (!($task = \Service_Statistic_Task::begin('customer'))) {
         return false;
     }
     $data = array();
     $date_ranges = \Service_Statistic_Task::date_ranges('customer');
     foreach ($date_ranges as $range) {
         $begin = \Date::create_from_string($range['begin'], 'mysql');
         $end = \Date::create_from_string($range['end'], 'mysql');
         $date = $begin->format('mysql_date');
         $data[$date] = array();
         $created = \Service_Customer_Statistic::created($begin, $end);
         foreach ($created as $result) {
             $data[$date][] = array('seller_id' => $result['seller_id'], 'name' => 'created', 'value' => $result['total']);
         }
         $deleted = \Service_Customer_Statistic::deleted($begin, $end);
         foreach ($deleted as $result) {
             $data[$date][] = array('seller_id' => $result['seller_id'], 'name' => 'deleted', 'value' => $result['total']);
         }
         $subscribed = \Service_Customer_Statistic::subscribed($begin, $end);
         foreach ($subscribed as $result) {
             $data[$date][] = array('seller_id' => $result['seller_id'], 'name' => 'subscribed', 'value' => $result['total']);
         }
         $unsubscribed = \Service_Customer_Statistic::unsubscribed($begin, $end);
         foreach ($unsubscribed as $result) {
             $data[$date][] = array('seller_id' => $result['seller_id'], 'name' => 'unsubscribed', 'value' => $result['total']);
         }
         $total = \Service_Customer_Statistic::total($end);
         foreach ($total as $result) {
             $data[$date][] = array('seller_id' => $result['seller_id'], 'name' => 'total', 'value' => $result['total']);
         }
         $total_active = \Service_Customer_Statistic::total_active($end);
         foreach ($total_active as $result) {
             $data[$date][] = array('seller_id' => $result['seller_id'], 'name' => 'total_active', 'value' => $result['total']);
         }
         $total_subscribed = \Service_Customer_Statistic::total_subscribed($end);
         foreach ($total_subscribed as $result) {
             $data[$date][] = array('seller_id' => $result['seller_id'], 'name' => 'total_subscribed', 'value' => $result['total']);
         }
     }
     // Save the queried results as statistics.
     foreach ($data as $date => $results) {
         $date = \Date::create_from_string($date, 'mysql_date');
         foreach ($results as $result) {
             $seller = \Service_Seller::find_one($result['seller_id']);
             if (!\Service_Statistic::create($seller, 'customer', $date, $result['name'], $result['value'])) {
                 \Service_Statistic_Task::end($task, 'failed', "Error creating customer.{$result['name']} statistic for seller {$seller->name}.");
                 return;
             }
         }
     }
     \Service_Statistic_Task::end($task);
     \Cli::write('Customer Statistical Calculations Complete', 'green');
 }
Exemple #2
0
 /**
  * Switches the active seller.
  * 
  * @param int $id Seller ID.
  *
  * @return void
  */
 public function action_switch($id = null)
 {
     if (!$id) {
         throw new HttpNotFoundException();
     }
     $seller = Service_Seller::find_one($id);
     if (!$seller) {
         throw new HttpNotFoundException();
     }
     Seller::set($seller);
     Session::set_alert('success', "You are now viewing as seller \"{$seller->name}\".");
     Response::redirect('/');
 }
Exemple #3
0
 /**
  * Loads a seller based on session.
  *
  * @return void
  */
 public static function load()
 {
     Config::load('api', true);
     $seller_id = Session::get(self::$namespace . '.id');
     if ($seller_id) {
         $seller = Service_Seller::find_one($seller_id);
     } elseif ($api_key = Input::param('api_key', Config::get('api.key'))) {
         $api_key = Service_Api_Key::find_one(array('key' => $api_key));
         $seller = $api_key->seller;
     } else {
         $seller = Service_Seller::find_one();
     }
     if (!$seller || !$seller->active()) {
         return false;
     }
     self::set($seller);
     return true;
 }
Exemple #4
0
 /**
  * Updates a seller.
  *
  * @param int $id Seller ID.
  *
  * @return void
  */
 public function put_index($id = null)
 {
     if (!$id) {
         throw new HttpNotFoundException();
     }
     $seller = \Service_Seller::find_one($id);
     if (!$seller || $seller != \Seller::active()) {
         throw new HttpNotFoundException();
     }
     $validator = \Validation_Seller::update();
     if (!$validator->run(\Input::put())) {
         throw new HttpBadRequestException($validator->errors());
     }
     $data = $validator->validated();
     $seller = \Service_Seller::update($seller, $data);
     if (!$seller) {
         throw new HttpServerErrorException();
     }
     $this->response($seller);
 }
Exemple #5
0
 /**
  * Calculates date ranges for a particular statistic ($name) based on the last time the task was run.
  *
  * @param string $name Statistic name.
  *
  * @return array
  */
 public static function date_ranges($name)
 {
     $current_time = Date::time()->get_timestamp();
     $last_task = self::last($name);
     if (!$last_task) {
         // Statistic $name has never been processed - use first seller as range's beginning.
         $last_task = Service_Seller::find_one(array('status' => 'all'));
     }
     $begin_time = strtotime($last_task->created_at);
     $end_time = strtotime('tomorrow', $begin_time) - 1;
     // Initial range is last time until end of its day.
     $ranges = array(array('begin' => Date::forge($begin_time)->format('mysql'), 'end' => Date::forge($end_time)->format('mysql')));
     // Add additional 1 day ranges up to the current time.
     while ($end_time < $current_time) {
         $begin_time = strtotime('midnight', strtotime('+1 Day', $end_time));
         $end_time = strtotime('tomorrow', $begin_time) - 1;
         $ranges[] = array('begin' => Date::forge($begin_time)->format('mysql'), 'end' => Date::forge($end_time)->format('mysql'));
     }
     return $ranges;
 }