PHP File Manager
Editing File: Stats.php
<?php /* * Copyright (C) Wayne Purton-Smith - All Rights Reserved * Unauthorized copying of this file or removing this paragraph, via any medium is strictly prohibited * Proprietary and confidential * Written by Wayne Purton-Smith <waynepurtonsmith@hotmail.co.uk> February 2014 */ class Stats extends CI_Model { public function getNewOrdersToday() { return $this->orders->get(NULL, NULL, array('count' => true, 'exclude_pending' => true, 'order_date_from' => strtotime('00:00:00'), 'order_date_to' => strtotime('23:59:59'))); } public function getOrdersToAccept() { return $this->orders->get(NULL, NULL, array('count' => true, 'exclude_pending' => true, 'to_review' => true, 'exclude_review' => false)); } public function getOrdersToDispatch() { return $this->orders->get(NULL, NULL, array('count' => true, 'exclude_pending' => true, 'ready_to_dispatch' => true, 'has_dispatched' => false)); } public function getOrdersMarkedPacked() { return $this->orders->get(NULL, NULL, array('count' => true, 'exclude_pending' => true, 'dispatchment_status' => DISPATCHMENT_PACKED)); } public function getOrdersAwaitingPickup() { return $this->orders->get(NULL, NULL, array('count' => true, 'exclude_pending' => true, 'dispatchment_status' => DISPATCHMENT_PACKED, 'has_tracking_number' => true)); } public function getOrdersMarkedGone() { return $this->orders->get(NULL, NULL, array('count' => true, 'exclude_pending' => true, 'dispatched_date_from' => strtotime('-2 weeks 00:00:00'), 'has_dispatched' => true)); } public function getPaidOrders() { return $this->orders->get(NULL, NULL, array('count' => true, 'exclude_pending' => true, 'unpaid' => false)); } public function getUnpaidOrders() { return $this->orders->get(NULL, NULL, array('count' => true, 'exclude_pending' => true, 'unpaid' => true)); } public function getOrdersDueToDispatchToday() { return $this->orders->get(NULL, NULL, array('count' => true, 'exclude_pending' => true, 'delivery_date_from' => strtotime('00:00:00'), 'delivery_date_to' => strtotime('23:59:59'))); } public function getOrdersOverdueForDispatch() { return $this->orders->get(NULL, NULL, array('count' => true, 'exclude_pending' => true, 'delivery_date_to' => strtotime('today 00:00:00'))); } public function getNewCustomersChange() { $first_week_day = strtotime('monday this week 00:00:00'); $last_week_count = $this->customers->get(NULL, array ( 'count' => true, 'registered_date_from' => strtotime('monday last week', $first_week_day), 'registered_date_to' => ($first_week_day - 1) )); $current_week_count = $this->customers->get(NULL, array ( 'count' => true, 'registered_date_from' => $first_week_day )); $percent_change = $this->calculatePercentChange($current_week_count, $last_week_count); return (object) array ( 'last_week' => $last_week_count, 'current_week' => $current_week_count, 'change' => $percent_change, 'change_text' => number_format(abs($percent_change), 1) . '%' ); } public function getNewOrdersChange() { $first_week_day = strtotime('monday this week 00:00:00'); $last_week_count = $this->orders->get(NULL, NULL, array ( 'count' => true, 'order_date_from' => strtotime('monday last week', $first_week_day), 'order_date_to' => ($first_week_day - 1) )); $current_week_count = $this->orders->get(NULL, NULL, array ( 'count' => true, 'order_date_from' => $first_week_day )); $percent_change = $this->calculatePercentChange($current_week_count, $last_week_count); return (object) array ( 'last_week' => $last_week_count, 'current_week' => $current_week_count, 'change' => $percent_change, 'change_text' => number_format(abs($percent_change), 1) . '%' ); } public function getRevenueChange() { $first_week_day = strtotime('monday this week 00:00:00'); $last_week_count = $this->orders->get(NULL, NULL, array ( 'sum' => 'paid', 'order_date_from' => strtotime('monday last week', $first_week_day), 'order_date_to' => ($first_week_day - 1) )); $current_week_count = $this->orders->get(NULL, NULL, array ( 'sum' => 'paid', 'order_date_from' => $first_week_day )); $percent_change = $this->calculatePercentChange($current_week_count, $last_week_count); return (object) array ( 'last_week' => $last_week_count, 'current_week' => $current_week_count, 'change' => $percent_change, 'change_text' => number_format(abs($percent_change), 1) . '%' ); } public function getDispatchedOrdersChange() { $first_week_day = strtotime('monday this week 00:00:00'); $last_week_count = $this->orders->get(NULL, NULL, array ( 'count' => true, 'dispatched_date_from' => strtotime('monday last week', $first_week_day), 'dispatched_date_to' => ($first_week_day - 1) )); $current_week_count = $this->orders->get(NULL, NULL, array ( 'count' => true, 'dispatched_date_from' => $first_week_day )); $percent_change = $this->calculatePercentChange($current_week_count, $last_week_count); return (object) array ( 'last_week' => $last_week_count, 'current_week' => $current_week_count, 'change' => $percent_change, 'change_text' => number_format(abs($percent_change), 1) . '%' ); } public function getRevenueMonthData($number_of_months = 3) { $number_of_months = ($number_of_months == 1) ? 2 : ($number_of_months - 1); $month_first_day = strtotime('first day of this month 00:00:00'); $month_last_day = strtotime('last day of this month 23:59:59', $month_first_day); $last_month_first_day = strtotime('first day of -' . $number_of_months . ' months 00:00:00', $month_first_day); $last_month_last_day = strtotime('last day of -' . $number_of_months . ' months 23:59:59', $last_month_first_day); $month_data = array(); $data_groups = array(); $data = array(); for($i = $number_of_months; $i >= 0; $i--) { $group_key = 'month_' . ($i + 1) . '_revenue'; $date_range_from = strtotime('-' . $i . ' months 00:00:00', $month_first_day); $date_range_to = strtotime('last day of this month 23:59:59', $date_range_from); $month_data[$group_key] = $this->generateKeyedDateArray($date_range_from, $date_range_to); $data_groups[$group_key] = $this->orders->get(NULL, NULL, array ( 'sum' => 'paid', 'sum_group' => 'order-date', 'order_date_from' => $date_range_from, 'order_date_to' => $date_range_to, 'order_by' => array('order-date' => 'asc') )); } foreach($data_groups as $group_key => $group_data) { if(!isset($data[$group_key])) { $data[$group_key] = array(); } foreach($group_data as $row_data) { $data[$group_key][$row_data->ordered_datestamp] = (isset($month_data[$group_key][$row_data->ordered_datestamp])) ? $row_data->summed : 0; } $data[$group_key] = array_replace($month_data[$group_key], $data[$group_key]); } return (object) $data; } public function generateDateSuffixes() { for($d = 1; $d <= 31; $d++) { $days[] = date_suffix($d, true); } return $days; } private function generateKeyedDateArray($date_from = NULL, $date_to = NULL, $default_value = 0) { $date_from = strtotime('00:00:00', $date_from); $date_to = strtotime('00:00:00', $date_to); $days_diff = floor((abs($date_from - $date_to)) / (60 * 60 * 24)); $dates_data = array(); for($i = 0; $i <= $days_diff; $i++) { $dates_data[date('d-m-Y', strtotime('+' . $i . ' days', $date_from))] = $default_value; } return $dates_data; } private function calculatePercentChange($sum_one = 0, $sum_two = 0) { return ($sum_one > 0 && $sum_two > 0) ? ((($sum_one - $sum_two) / $sum_two) * 100) : 0; } }
Cancel