Ejemplo n.º 1
0
function fn_create_periods($params)
{
    $period_name = empty($params['period']) ? null : $params['period'];
    $available_periods = array(DateTimeHelper::PERIOD_TODAY, DateTimeHelper::PERIOD_YESTERDAY, DateTimeHelper::PERIOD_THIS_WEEK, DateTimeHelper::PERIOD_LAST_WEEK, DateTimeHelper::PERIOD_THIS_MONTH, DateTimeHelper::PERIOD_LAST_MONTH, DateTimeHelper::PERIOD_THIS_YEAR, DateTimeHelper::PERIOD_LAST_YEAR, DateTimeHelper::PERIOD_DAY_AGO_TILL_NOW, DateTimeHelper::PERIOD_WEEK_AGO_TILL_NOW, DateTimeHelper::PERIOD_MONTH_AGO_TILL_NOW);
    if (in_array($period_name, $available_periods)) {
        $period = DateTimeHelper::getPeriod($period_name);
        $time_from = $period['from']->getTimestamp();
        $time_to = $period['to']->getTimestamp();
    } elseif ($period_name == 'HC' && isset($params['last_days'])) {
        $period = DateTimeHelper::createCustomPeriod("-{$params['last_days']} day", 'now');
        $time_from = $period['from']->getTimestamp();
        $time_to = $period['to']->getTimestamp();
    } else {
        $time_from = empty($params['time_from']) ? 0 : fn_parse_date($params['time_from']);
        $time_to = empty($params['time_to']) ? TIME : fn_parse_date($params['time_to'], true);
    }
    Tygh::$app['view']->assign('time_from', $time_from);
    Tygh::$app['view']->assign('time_to', $time_to);
    return array($time_from, $time_to);
}
Ejemplo n.º 2
0
 }
 $time_periods = array(DateTimeHelper::PERIOD_TODAY, DateTimeHelper::PERIOD_YESTERDAY, DateTimeHelper::PERIOD_THIS_MONTH, DateTimeHelper::PERIOD_LAST_MONTH, DateTimeHelper::PERIOD_THIS_YEAR, DateTimeHelper::PERIOD_LAST_YEAR);
 $time_period = DateTimeHelper::getPeriod(DateTimeHelper::PERIOD_MONTH_AGO_TILL_NOW);
 // Predefined period selected
 if (isset($_REQUEST['time_period']) && in_array($_REQUEST['time_period'], $time_periods)) {
     $time_period = DateTimeHelper::getPeriod($_REQUEST['time_period']);
     fn_set_session_data('dashboard_selected_period', serialize(array('period' => $_REQUEST['time_period'])));
 } elseif (isset($_REQUEST['time_from'], $_REQUEST['time_to'])) {
     $time_period = DateTimeHelper::createCustomPeriod('@' . $_REQUEST['time_from'], '@' . $_REQUEST['time_to']);
     fn_set_session_data('dashboard_selected_period', serialize(array('from' => $time_period['from']->format(DateTime::ISO8601), 'to' => $time_period['to']->format(DateTime::ISO8601))));
 } elseif ($timeframe = fn_get_session_data('dashboard_selected_period')) {
     $timeframe = unserialize($timeframe);
     if (isset($timeframe['period']) && in_array($timeframe['period'], $time_periods)) {
         $time_period = DateTimeHelper::getPeriod($timeframe['period']);
     } elseif (isset($timeframe['from'], $timeframe['to'])) {
         $time_period = DateTimeHelper::createCustomPeriod($timeframe['from'], $timeframe['to']);
     }
 }
 $timestamp_from = $time_period['from']->getTimestamp();
 $timestamp_to = $time_period['to']->getTimestamp();
 $time_difference = $timestamp_to - $timestamp_from;
 $is_day = $timestamp_to - $timestamp_from <= SECONDS_IN_DAY ? true : false;
 $stats = '';
 if (!defined('HTTPS')) {
     $stats .= base64_decode('PGltZyBzcmM9Imh0dHA6Ly93d3cuY3MtY2FydC5jb20vaW1hZ2VzL2JhY2tncm91bmQuZ2lmIiBoZWlnaHQ9IjEiIHdpZHRoPSIxIiBhbHQ9IiIgLz4=');
 }
 $general_stats = array();
 /* Products */
 if (fn_check_view_permissions('products.manage', 'GET')) {
     $general_stats['products'] = array();
     $params = array('only_short_fields' => true, 'extend' => array('companies', 'sharing'), 'status' => 'A', 'get_conditions' => true);
Ejemplo n.º 3
0
/**
 * Generates date-time intervals of a given period for sales reports
 *
 * @param int $interval_id    Sales reports interval ID
 * @param int $timestamp_from Timestamp of report period beginning date
 * @param int $timestamp_to   Timestamp of report period end date
 * @param int $limit
 *
 * @return array
 */
function fn_check_intervals($interval_id, $timestamp_from, $timestamp_to, $limit = 0)
{
    $interval_definition = db_get_row('SELECT * FROM ?:sales_reports_intervals WHERE `interval_id` = ?i', $interval_id);
    $intervals = array();
    // Passthru given timeframe
    if (empty($interval_definition['value'])) {
        $interval = $interval_definition;
        $interval['time_from'] = $timestamp_from;
        $interval['time_to'] = $timestamp_to;
        $intervals[] = $interval;
    } else {
        $interval_type_map = array('day' => 'D', 'week' => 'W', 'month' => 'M', 'year' => 'Y');
        $mapped_interval_type = $interval_type_map[$interval_definition['interval_code']];
        $timezone = new \DateTimeZone(date_default_timezone_get());
        $date_interval = new \DateInterval("P1{$mapped_interval_type}");
        $datetime_from = new \DateTime("@{$timestamp_from}");
        $datetime_from->setTimezone($timezone);
        $datetime_to = new \DateTime("@{$timestamp_to}");
        $datetime_to->setTimezone($timezone);
        $period_iterator_datetime_from = clone $datetime_from;
        $period_definitions = DateTimeHelper::getPeriodDefinitions();
        switch ($interval_definition['interval_code']) {
            case 'week':
                $period_iterator_start_datetime_modifier = $period_definitions[DateTimeHelper::PERIOD_THIS_WEEK]['from'];
                break;
            case 'month':
                $period_iterator_start_datetime_modifier = $period_definitions[DateTimeHelper::PERIOD_THIS_MONTH]['from'];
                break;
            case 'year':
                $period_iterator_start_datetime_modifier = $period_definitions[DateTimeHelper::PERIOD_THIS_YEAR]['from'];
                break;
            default:
                $period_iterator_start_datetime_modifier = null;
                break;
        }
        if (null !== $period_iterator_start_datetime_modifier) {
            $period_iterator_datetime_from->modify($period_iterator_start_datetime_modifier);
        }
        $period_list = new \DatePeriod($period_iterator_datetime_from, $date_interval, $datetime_to);
        foreach ($period_list as $i => $period_start) {
            /** @var \DateTime $period_start */
            $period_end = clone $period_start;
            $period_end->add($date_interval)->modify('-1 second');
            if ($period_start < $datetime_from) {
                $period_start = clone $datetime_from;
            }
            if ($period_end > $datetime_to) {
                $period_end = clone $datetime_to;
            }
            $interval = array('interval_id' => "{$interval_definition['interval_id']}{$i}", 'value' => $interval_definition['value'], 'interval_code' => $interval_definition['interval_code'], 'time_from' => $period_start->getTimestamp(), 'time_to' => $period_end->getTimestamp(), 'iso8601_from' => $period_start->format(\DateTime::ISO8601), 'iso8601_to' => $period_end->format(\DateTime::ISO8601));
            $interval_date_format = Registry::get("settings.Reports.{$interval_definition['interval_code']}");
            // String representation of interval's beginning date
            $interval['description'] = fn_date_format($interval['time_from'], $interval_date_format);
            // Week, month and year intervals may belong to two real weeks/months/years,
            // so we have to display both of them.
            if (in_array($interval_definition['interval_code'], array('week', 'month', 'year'))) {
                $datetime_to_description = fn_date_format($interval['time_to'], $interval_date_format);
                // Interval belongs to two real weeks/months/years
                if ($interval['description'] !== $datetime_to_description) {
                    $interval['description'] .= " - {$datetime_to_description}";
                }
            }
            $intervals[$i + 1] = $interval;
            // $i+1 is also preserved for BC, no algorhytmical meaning
        }
    }
    // This magical part of code must be working, but is left non-tested yet
    if (!empty($limit)) {
        $i = 1;
        $j = 0;
        $temp = array();
        foreach ($intervals as $k => $v) {
            $temp[$i][$k] = $v;
            $j++;
            if ($j == $limit) {
                $j = 0;
                $i++;
            }
        }
        unset($intervals);
        $intervals = $temp;
    }
    return $intervals;
}