<?php

function get_customer_group_discount($cart_content_total)
{
    global $customers_status, $price;

    $customer_group_discount = 0;

    if ($customers_status->customers_status_discount_flag=='1' && !empty($customers_status->customers_discount)
    ) {

        $cart_discount_already = 0;
        if(is_array(sessionCart()->discount))
        {
            $cart_discount_already = sessionCart()->discount['plain'];
        }
        $total_cart = $cart_content_total + $cart_discount_already;

        if (strstr($customers_status->customers_discount,'#')) {
            $discounts = explode(';',$customers_status->customers_discount);

            $_SESSION['customer_group_discount'] = 0;
            $discounts = array_reverse(array_filter($discounts));
            for ($i=0;$i<sizeof($discounts);$i++)
            {
                $dsc = explode('#', $discounts[$i]);
                if (round($total_cart, 2) >= round($price->_calcCurrency($dsc[0]), 2))
                {
                    $customer_group_discount = $dsc[1];
                    break;
                }
            }
        } else { // only single discount
            $customer_group_discount = floatval($customers_status->customers_discount);
        }
    }

    return $customer_group_discount;
}

function XtCustomersDiscount_orderEditActive()
{
    $oePages = array('addOrderItem','removeOrderItem','applyExistingAddress','apply','updateOrderItem', 'editCoupon', 'applyNewAddress', 'applyEditedAddress', 'applyExistingAddress');

    if(
        ( isset($_GET['plugin']) && isset($_GET['pg']) && $_GET['plugin']=='order_edit' && in_array($_GET['pg'], $oePages))
        ||
        ( isset($_POST['plugin']) && isset($_POST['pg']) && $_POST['plugin']=='order_edit' && in_array($_POST['pg'], $oePages))
    )
    {
        return true;
    }
    return false;
}