<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
namespace front\factory;

/**
 * Description of class
 *
 * @author Dom - Jacek
 */
class ShopPromotion
{
  //! promocja na wszystkie produkty z kategori 1 lub 2
  static public function promotion_type_03( $basket, $promotion )
  {
    $categories = json_decode( $promotion -> categories );
    $condition_categories = json_decode( $promotion -> condition_categories );

    foreach ( $basket as $key => $val )
    {
      $product_promotion = \shop\Product::is_product_on_promotion( $val['product-id'] );

      if ( !$product_promotion or $product_promotion and $promotion -> include_product_promo )
      {
        $product_categories = \front\factory\ShopProduct::product_categories( $val[ 'product-id' ] );
        foreach ( $product_categories as $category_tmp )
        {
          if ( in_array( $category_tmp[ 'category_id' ], $condition_categories ) or in_array( $category_tmp[ 'category_id' ], $categories ) )
          {
            $basket[$key]['discount_type'] = $promotion -> discount_type;
            $basket[$key]['discount_amount'] = $promotion -> amount;
            $basket[$key]['discount_include_coupon'] = $promotion -> include_coupon;
            $basket[$key]['include_product_promo'] = $promotion -> include_product_promo;
          }
        }
      }
    }
    return $basket;
  }

  //! promocja na produkty z kategorii 1 i 2
  static public function promotion_type_02( $basket, $promotion )
  {
    $condition_1 = false; $condition_2 = false;

    $categories = json_decode( $promotion -> categories );
    $condition_categories = json_decode( $promotion -> condition_categories );

    // sprawdzanie czy warunki są spełnione
    if ( is_array( $condition_categories ) and is_array( $categories ) )
    {
      foreach ( $basket as $key => $val )
      {
        $product_categories = \front\factory\ShopProduct::product_categories( $val[ 'product-id' ] );
        foreach ( $product_categories as $category_tmp )
        {
          // sprawdzam produkt pod kątem I kategorii
          if ( !$condition_1 and in_array( $category_tmp[ 'category_id' ], $condition_categories ) )
          {
            $condition_1 = true;
            unset( $basket_tmp[ $key ] );
          }
        }
      }

      foreach ( $basket as $key => $val )
      {
        $product_categories = \front\factory\ShopProduct::product_categories( $val[ 'product-id' ] );
        foreach ( $product_categories as $category_tmp )
        {
          // sprawdzam produkt pod kątem II kategorii
          if ( !$condition_2 and in_array( $category_tmp[ 'category_id' ], $categories ) )
            $condition_2 = true;
        }
      }
    }

    // jeżeli warunki są spełnione to szukam produktów, którym można obniżyć cenę
    if ( $condition_1 and $condition_2 )
    {
      foreach ( $basket as $key => $val )
      {
        $product_categories = \front\factory\ShopProduct::product_categories( $val[ 'product-id' ] );
        foreach ( $product_categories as $category_tmp )
        {
          if ( in_array( $category_tmp[ 'category_id' ], $categories ) or in_array( $category_tmp['category_id'], $condition_categories ) )
          {
            $basket[$key]['discount_type'] = $promotion -> discount_type;
            $basket[$key]['discount_amount'] = $promotion -> amount;
            $basket[$key]['discount_include_coupon'] = $promotion -> include_coupon;
            $basket[$key]['include_product_promo'] = $promotion -> include_product_promo;
          }
        }
      }
    }
    return $basket;
  }

  //! promocja na najtańszy produkt z kategorii 1 lub 2
  static public function promotion_type_04( $basket, $promotion )
  {
    $condition_1 = false;
    $categories = json_decode( $promotion -> categories );

    //! sprawdzanie czy warunki są spełnione
    if ( is_array( $categories ) and is_array( $categories ) )
    {
      foreach ( $basket as $key => $val )
      {
        $product_promotion = \shop\Product::is_product_on_promotion( $val['product-id'] );

        if ( !$product_promotion or $product_promotion and $promotion -> include_product_promo )
        {
          $product_categories = \front\factory\ShopProduct::product_categories( $val[ 'product-id' ] );
          foreach ( $product_categories as $category_tmp )
          {
            //! sprawdzam produkt pod kątem I kategorii
            if ( !$condition_1[$key] and in_array( $category_tmp[ 'category_id' ], $categories ) )
              $condition_1[$key] = true;
          }
        }
      }
    }

    if ( count( $condition_1 ) >= $promotion -> min_product_count )
    {
      foreach ( $basket as $key => $val )
      {
        $price = \shop\Product::get_product_price( $val['product-id'] );
        if ( !$cheapest_position or $cheapest_position['price'] > $price )
        {
          $cheapest_position['price'] = $price;
          $cheapest_position['key'] = $key;
        }
      }

      $basket[$cheapest_position['key']]['quantity'] = 1;
      $basket[$cheapest_position['key']]['discount_type'] = 3;
      $basket[$cheapest_position['key']]['discount_amount'] = $promotion -> price_cheapest_product;
      $basket[$cheapest_position['key']]['discount_include_coupon'] = $promotion -> include_coupon;
      $basket[$cheapest_position['key']]['include_product_promo'] = $promotion -> include_product_promo;
    }

    return $basket;
  }

  //! promocja na cały koszyk
  static public function promotion_type_05( $basket, $promotion )
  {
    foreach ( $basket as $key => $val )
    {
      $product_promotion = \shop\Product::is_product_on_promotion( $val['product-id'] );

      if ( !$product_promotion or $product_promotion and $promotion -> include_product_promo )
      {
        $product_categories = \front\factory\ShopProduct::product_categories( $val[ 'product-id' ] );
        foreach ( $product_categories as $category_tmp )
        {
          $basket[$key]['discount_type'] = $promotion -> discount_type;
          $basket[$key]['discount_amount'] = $promotion -> amount;
          $basket[$key]['discount_include_coupon'] = $promotion -> include_coupon;
          $basket[$key]['include_product_promo'] = $promotion -> include_product_promo;
        }
      }
    }
    return $basket;
  }

  //! Rabat procentowy na produkty z kategorii I jeżeli w koszyku jest produkt z kategorii II
  static public function promotion_type_01( $basket, $promotion )
  {
    $condition = false;
    $categories = json_decode( $promotion -> categories );
    $condition_categories = json_decode( $promotion -> condition_categories );

    // sprawdzanie czy warunki są spełnione
    if ( is_array( $condition_categories ) and is_array( $categories ) )
    {
      foreach ( $basket as $key => $val )
      {
        $product_categories = \front\factory\ShopProduct::product_categories( $val[ 'product-id' ] );
        foreach ( $product_categories as $category_tmp )
        {
          if ( in_array( $category_tmp[ 'category_id' ], $condition_categories ) )
          {
            $condition = true;
          }
        }
      }
    }

    // jeżeli warunki są spełnione to szukam produktów, którym można obniżyć cenę
    if ( $condition )
    {
      foreach ( $basket as $key => $val )
      {
        $product_categories = \front\factory\ShopProduct::product_categories( $val[ 'product-id' ] );
        foreach ( $product_categories as $category_tmp )
        {
          if ( in_array( $category_tmp[ 'category_id' ], $categories ) )
          {
            $basket[$key]['discount_type'] = $promotion -> discount_type;
            $basket[$key]['discount_amount'] = $promotion -> amount;
            $basket[$key]['discount_include_coupon'] = $promotion -> include_coupon;
            $basket[$key]['include_product_promo'] = $promotion -> include_product_promo;
          }
        }
      }
    }
    return $basket;
  }
}