HEX
Server: Apache/2.2.15 (CentOS)
System: Linux ip-10-0-2-146.eu-west-1.compute.internal 2.6.32-754.35.1.el6.centos.plus.x86_64 #1 SMP Sat Nov 7 11:33:42 UTC 2020 x86_64
User: root (0)
PHP: 5.6.40
Disabled: NONE
Upload Files
File: /www/exchange2/exchange/custom_actions/TT1923.php
<?php
  class TT1923
  {
      private $query;
        
      function __construct()
      {
          include_once('../Bin/constants.php');
          include_once('../Bin/init.php');
          require_once(ROOT_FOLDER.'Bin/Security.php');
          
          $this->query = new Bin_Query();
          
          require_once(ROOT_FOLDER.'admin/classes/Model/investor_actions.php');
      }
      
      function initHolidays($year)
      {
          $result=array($year.'-01-01',$year.'-03-17',$year.'-12-15',$year.'-12-26');
          
          foreach(array('05','06','08') as $month_id)
          {
              $date_not_found=TRUE;
              $date=strtotime($year.'-'.$month_id.'-01');
              while($date_not_found)
              {
                  if (date('l',$date)=='Monday')
                  {
                      $date_not_found=FALSE;
                      $result[]=date('Y-m-d',$date);
                  }
                  else
                  {
                      $date+=86400;
                  }
              }
          }
          
          $date=strtotime($year.'-10-31');
          
          $date_not_found=TRUE;
          while($date_not_found)
          {
              if (date('l',$date)=='Monday')
              {
                  $date_not_found=FALSE;
                  $result[]=date('Y-m-d',$date);
              }
              else
              {
                  $date-=86400;
              }
          }
          
          return $result;
      }
      
      function  fixDates()
      {
          $this->query->executeQuery('SELECT bank_holidays FROM site_settings WHERE site_setting_id=1');
          
          $bank_holidays=$this->initHolidays(date('Y'));
          foreach(explode(",",$this->query->records[0]['bank_holidays']) as $date)
          {
              $bank_holidays[]=$date;
          }
          
          $this->query->executeQuery('SELECT DATE(date) as date, DATE_FORMAT(date,"%W") as date_name, GROUP_CONCAT(id) as ids
                                       FROM daily_summary
                                       GROUP BY date
                                       ORDER BY date');
          $records=$this->query->records;
          
          $previous_item=array();
          $current_year='';
          foreach($records as $item)
          {
              switch($item['date_name'])
              {
                  case 'Sunday':{
                      if ($previous_item['date_name']=='Thursday' OR $previous_item['date_name']=='Wednesday')
                      {
                          $this->query->updateQuery('UPDATE daily_summary SET date=DATE_ADD(date,INTERVAL -2 DAY) WHERE id IN ('.$item['ids'].')');
                      }
                      
                      break;
                  }
                  case 'Saturday':{
                      if ($previous_item['date_name']=='Thursday' OR $previous_item['date_name']=='Wednesday')
                      {
                          $this->query->updateQuery('UPDATE daily_summary SET date=DATE_ADD(date,INTERVAL -1 DAY) WHERE id IN ('.$item['ids'].')');
                      }
                      break;
                  }
                  case 'Monday':{
                     
                      if (in_array($item['date'],$bank_holidays))
                      {
                          if ($previous_item['date_name']=='Thursday' OR $previous_item['date_name']=='Wednesday')
                          {
                              $this->query->updateQuery('UPDATE daily_summary SET date=DATE_ADD(date, INTERVAL -3 DAY) WHERE id IN ('.$item['ids'].')');
                          }
                      }
                      break;
                  }
              }
              
              //first day in new year
              if ($current_year<>'' AND $current_year!=date('Y',strtotime($item['date'])))
              {
                  $investor_actions = new Investor_actions();
                  
                  $this->query->executeQuery('SELECT MIN(DATE(date)) as original_date
                                              FROM daily_summary
                                              WHERE DATE_FORMAT(date,"%Y")='.date('Y',strtotime($item['date'])));
                  $original_date= $this->query->records[0]['original_date'];
                  
                  foreach(array($current_year."-12-31",date('Y',strtotime($item['date']))."-01-01") as $check_date)
                  {
                      $this->query->executeQuery('SELECT date
                                                  FROM daily_summary
                                                  WHERE DATE(date) =DATE("'.$check_date.'")
                                                  GROUP BY date');
                      if (count($this->query->records)==0)
                      {
                          $this->query->executeQuery('SELECT date, GROUP_CONCAT(id) as ids
                                                      FROM daily_summary
                                                      WHERE DATE(date) =DATE("'.$original_date.'")
                                                      GROUP BY date');
                          
                          $this->query->updateQuery('INSERT INTO daily_summary (id,account_id,daily_summary,date,type,created_at)
                                                     SELECT NULL,account_id,daily_summary, "'.$check_date.' 00:00:00",type,NOW()
                                                     FROM daily_summary
                                                     WHERE id IN ('.$this->query->records[0]['ids'].')');
                          $this->query->updateQuery('UPDATE daily_summary SET 
                                                    daily_summary = '.$investor_actions->getCurrentInvestorFunds(FALSE,$check_date).' 
                                                    WHERE type="Current Investor Funds" AND date="'.$check_date.' 00:00:00" AND account_id IS NULL');
                          
                      }
                  }
              }
              
              
              if ($current_year=='' OR $current_year!=date('Y',strtotime($item['date'])))
              {
                  $current_year=date('Y',strtotime($item['date']));
              }
              
              $previous_item=$item;
          }
      }
  }
  
  $TT1923 = new TT1923();
  $TT1923->fixDates();
?>