<?php
namespace shop;

class Dashboard implements \ArrayAccess
{
  static public function summary_orders()
  {
    global $mdb;

    try
    {
      $redis = \RedisConnection::getInstance() -> getConnection();
      $objectData = $redis -> get( "summary_ordersd" );

      if ( !$objectData )
      {
        $summary = $mdb -> count( 'pp_shop_orders', [ 'status' => 6 ] );
        $redis -> setex( "summary_ordersd", 60 * 5, serialize( $summary ) );
      }
      else
        $summary = unserialize( $objectData );
    }
    catch ( \RedisException $e )
    {
      $summary = $mdb -> count( 'pp_shop_orders', [ 'status' => 6 ] );
    }

    return $summary;
  }

  static public function summary_sales()
  {
    global $mdb;

    try
    {
      $redis = \RedisConnection::getInstance() -> getConnection();
      $objectData = $redis -> get( "summary_salesd" );

      if ( !$objectData )
      {
        $summary = $mdb -> sum( 'pp_shop_orders', 'summary', [ 'status' => 6 ] ) - $mdb -> sum( 'pp_shop_orders', 'transport_cost', [ 'status' => 6 ] );
        $redis -> setex( "summary_salesd", 60 * 5, serialize( $summary ) );
      }
      else
        $summary = unserialize( $objectData );
    }
    catch ( \RedisException $e )
    {
      $summary = $mdb -> sum( 'pp_shop_orders', 'summary', [ 'status' => 6 ] ) - $mdb -> sum( 'pp_shop_orders', 'transport_cost', [ 'status' => 6 ] );
    }

    return $summary;
  }

  static public function sales_grid()
  {
    global $mdb;

    $rows = $mdb -> select( 'pp_shop_orders', [ 'id', 'date_order' ], [ 'status' => 6 ] );
    if ( \S::is_array_fix( $rows ) ) foreach ( $rows as $row )
    {
      if ( date( 'N', strtotime( $row['date_order'] ) ) )
        $grid[ date( 'N', strtotime( $row['date_order'] ) ) ][ date( 'G', strtotime($row['date_order'] ) ) ] += 1;
    }

    return $grid;
  }

  static public function most_view_products()
  {
    global $mdb;
    return $mdb -> query( 'SELECT '
                            . 'id, SUM(visits) AS visits '
                          . 'FROM '
                            . 'pp_shop_products AS psop '
                          . 'GROUP BY '
                            . 'id '
                          . 'ORDER BY '
                            . 'visits DESC '
                          . 'LIMIT 10' ) -> fetchAll( \PDO::FETCH_ASSOC );
  }

  static public function best_sales_products()
  {
    global $mdb;

    return $mdb -> query( 'SELECT parent_product_id, SUM(quantity) AS quantity_summary, SUM(price_brutto_promo * quantity) AS sales FROM pp_shop_order_products AS psop INNER JOIN pp_shop_orders AS pso ON pso.id = psop.order_id WHERE pso.status = 6  GROUP BY parent_product_id ORDER BY sales DESC LIMIT 10' ) -> fetchAll( \PDO::FETCH_ASSOC );
  }

  static public function last_24_months_sales()
  {
    global $mdb;

    $monthsBack = 24;

    $sales = [ [ 'date' => date( 'Y-m' ) ] ];
    $previousMonthDate = new \DateTime();
    for ( $monthInterval = 0; $monthInterval < $monthsBack; $monthInterval++)
    {
      $previousMonthDate -> sub( new \DateInterval( "P1M" ) );
      array_push( $sales, [ 'date' => $previousMonthDate -> format( 'Y-m' ) ] );
    }

    for ( $i = 0; $i < 24; $i++ )
    {
      $date_start = date( 'Y-m-1', strtotime( $sales[$i]['date'] ) );
      $date_end = date( 'Y-m-t', strtotime( $sales[$i]['date'] ) );
      $sales[$i]['sales'] = $mdb -> sum( 'pp_shop_orders', 'summary', [ 'AND' => [ 'status' => 6, 'date_order[>=]' => $date_start, 'date_order[<=]' => $date_end ] ] ) - $mdb -> sum( 'pp_shop_orders', 'transport_cost', [ 'AND' => [ 'status' => 6, 'date_order[>=]' => $date_start, 'date_order[<=]' => $date_end ] ] );
    }

    return $sales;
  }

  static public function last_orders()
  {
    global $mdb;

    return $mdb -> query( 'SELECT '
                            . 'id, number, date_order, CONCAT( client_name, \' \', client_surname ) AS client, client_email, CONCAT( client_street, \', \', client_postal_code, \' \', client_city ) AS address, status, client_phone, summary '
                          . 'FROM '
                            . 'pp_shop_orders AS pso '
                          . 'ORDER BY '
                            . 'date_order DESC '
                          . 'LIMIT '
                            . '10' ) -> fetchAll( \PDO::FETCH_ASSOC );
  }

  public function offsetExists( $offset )
  {
    return isset( $this -> $offset );
  }

  public function offsetGet( $offset )
  {
    return $this -> $offset;
  }

  public function offsetSet( $offset, $value )
  {
    $this -> $offset = $value;
  }

  public function offsetUnset( $offset )
  {
    unset( $this -> $offset );
  }
}