示例#1
0
 /**
  * Возвращает периоды в которых пользователь был PRO
  * @param   integer  $user_id     uid пользователя
  * @param   string   $from_date   начало периода в формате postgresql
  * @param   boolean  $check_freeze   учитывать периоды заморозки
  * @return  array                 массив с данными или NULL если данных нет или произошла ошибка
  */
 function GetUserProPeriods($user_id, $from_date, $check_freeze = false)
 {
     global $DB;
     $sql = "SELECT DISTINCT\n            CASE WHEN from_date < ? THEN ? ELSE from_date END  as from_time,\n            from_date + to_date + COALESCE(freeze_to, '0')::interval as to_time,\n            CASE WHEN tarif IN(164) THEN 1 ELSE 0 END as is_profi\n       FROM orders\n      WHERE from_id = ?i \n        -- AND payed = true -- deprecated #0021704\n        AND from_date + to_date + COALESCE(freeze_to, '0')::interval >= ? \n      ORDER BY from_time, to_time DESC";
     $res = $DB->rows($sql, $from_date, $from_date, $user_id, $from_date);
     if ($DB->error || !count($res)) {
         return null;
     }
     if ($check_freeze) {
         $freeze = promotion::getFreezePeriods($user_id);
         if (!$freeze) {
             $freeze = array();
         }
         $f = array();
         $pro = array();
         foreach ($freeze as $i => $p) {
             $from = strtotime(date('Ymd', strtotime($p['from_time'])));
             $to = strtotime(date('Ymd', strtotime($p['to_time'])));
             $d = $from;
             while ($d < $to) {
                 $f[] = $d;
                 $d = mktime(0, 0, 0, date('m', $d), date('d', $d) + 1, date('Y', $d));
             }
         }
         $st = 1000;
         foreach ($res as $i => $p) {
             $from = strtotime(date('Ymd', strtotime($p['from_time'])));
             $to = strtotime(date('Ymd', strtotime($p['to_time'])));
             $d = $from;
             $freezed = false;
             $pro[$st]['is_profi'] = $p['is_profi'];
             while ($d <= $to) {
                 if (!in_array($d, $f)) {
                     $pro[$st][] = $d;
                     $freezed = false;
                 } else {
                     if (!$freezed) {
                         $st++;
                     }
                     $freezed = true;
                 }
                 $d = mktime(0, 0, 0, date('m', $d), date('d', $d) + 1, date('Y', $d));
             }
             $st++;
         }
         $new = array();
         foreach ($pro as $k => $v) {
             $new[$k]['from_time'] = date('Y-m-d', $v[0]);
             $new[$k]['to_time'] = date('Y-m-d', end($v));
             $new[$k]['is_profi'] = $v['is_profi'];
         }
         //        echo '<pre>'; var_dump($new); die();
         if (count($new)) {
             $res = $new;
         }
     }
     return count($res) ? $res : null;
 }