<?php
namespace admin\factory;
class ShopPromotion
{
  static public function promotion_details( int $promotion_id )
  {
    global $mdb;
    return $mdb -> get( 'pp_shop_promotion', '*', [ 'id' => $promotion_id ] );
  }

  static public function save( $promotion_id, $name, $status, $condition_type, $discount_type, $amount, $date_to, $categories, $condition_categories, $include_coupon, $include_product_promo, $min_product_count, $price_cheapest_product )
  {
    global $mdb, $user;

    if ( !$promotion_id )
    {
      $mdb -> insert( 'pp_shop_promotion', [
        'name' => $name,
        'status' => $status,
        'condition_type' => $condition_type,
        'discount_type' => $discount_type,
        'amount' => $amount,
        'date_to' => $date_to != '' ? $date_to : null,
        'categories' => $categories != null ? ( is_array( $categories ) ? json_encode( $categories ) : json_encode( [ $categories ] ) ) : null,
        'condition_categories' => $condition_categories != null ? ( is_array( $condition_categories ) ? json_encode( $condition_categories ) : json_encode( [ $condition_categories ] ) ) : null,
        'include_coupon' => $include_coupon,
        'include_product_promo' => $include_product_promo,
        'min_product_count' => $min_product_count ? $min_product_count : null,
        'price_cheapest_product' => $price_cheapest_product ? $price_cheapest_product : null
      ] );

      return $mdb -> id();
    }
    else
    {
      $mdb -> update( 'pp_shop_promotion', [
        'name' => $name,
        'status' => $status,
        'condition_type' => $condition_type,
        'discount_type' => $discount_type,
        'amount' => $amount,
        'date_to' => $date_to != '' ? $date_to : null,
        'categories' => $categories != null ? ( is_array( $categories ) ? json_encode( $categories ) : json_encode( [ $categories ] ) ) : null,
        'condition_categories' => $condition_categories != null ? ( is_array( $condition_categories ) ? json_encode( $condition_categories ) : json_encode( [ $condition_categories ] ) ) : null,
        'include_coupon' => $include_coupon,
        'include_product_promo' => $include_product_promo,
        'min_product_count' => $min_product_count ? $min_product_count : null,
        'price_cheapest_product' => $price_cheapest_product ? $price_cheapest_product : null
      ], [
        'id' => $promotion_id
      ] );
      return $promotion_id;
    }
  }
}