コード例 #1
0
function apply_discount_amount($sourceid, $amount)
{
    /*
    	Error codes:
    	5. Amount < 0
    	6. Amount > total
    */
    $_SESSION['discount']['type'] = "amount";
    $amount = str_replace(",", ".", $amount);
    $amount = (double) $amount;
    if ($amount < 0) {
        return 5;
    }
    if ($amount > table_total_without_discount($sourceid)) {
        return 6;
    }
    $amount = -1 * $amount;
    if ($amount) {
        $_SESSION['discount'] = array('type', 'percent', 'amount');
        $_SESSION['discount']['type'] = "amount";
        $_SESSION['discount']['percent'] = 0;
        $_SESSION['discount']['amount'] = $amount;
    } else {
        unset($_SESSION['discount']);
    }
    return 0;
}
コード例 #2
0
/**
* Calculates total with discounts
*
* This function reads all the orders associated to a given table
* and sums their price field values.
* After this the value is formatted with 2 decimal places and returned.
*
* The returned value includes discounts.
*
* Note: this function will return 0 if a MySQL error occurs.
*
* @param integer $sourceid
* @return string Total value formatted
*/
function table_total($sourceid)
{
    $total = table_total_without_discount($sourceid);
    $query = "SELECT * FROM `sources` WHERE `id`='" . $sourceid . "'";
    $res = common_query($query, __FILE__, __LINE__);
    if (!$res) {
        return 0;
    }
    $arr = mysql_fetch_array($res);
    $discount = $arr["discount"];
    $total = $total + $discount;
    $total = sprintf("%01.2f", $total);
    return $total;
}