The Daily WTF: Curious Perversions in Information Technology
Welcome to TDWTF Forums Sign in | Join | Help
in Search

Discount calculation

Last post 03-04-2008 5:16 AM by Zecc. 9 replies.
Page 1 of 1 (10 items)
Sort Posts: Previous Next
  • 03-03-2008 6:48 AM

    • Zecc
    • Top 100 Contributor
    • Joined on 06-12-2007
    • Posts 288

    Discount calculation

    Just bumped against this code on my current project:
     
    if($_REQUEST['discount_price'][$key] != 0) //avoid division by 0
    {   
        $value = floatval($_REQUEST['list_price'][$key]);
        $discount = - 100 + ($value * 100) / $value;       
        $discount = round($discount, 2);
    }
    else
        $discount = 100;
    
    With discounts like these, I think I'll stick to the list price, thank you.
    If mixed metaphors were illegal, I'd be having an indigestion.
  • 03-03-2008 8:10 AM In reply to

    Re: Discount calculation

    Wow.  That's wrong in so many different ways.  It's beautiful, if you love looking at train wrecks. 

  • 03-03-2008 8:25 AM In reply to

    Re: Discount calculation

    eh... your discount is either 0 or 100.

    why doesnt it just say $discount = $_REQUEST['discount_price'][$key] == 0?100:0;

    edit: not to mention, the user can get themselves the discount by setting the cookie themselves. Not that they could do too much harm since they couldnt get anything but a 0 or 100.

  • 03-03-2008 8:32 AM In reply to

    • Zecc
    • Top 100 Contributor
    • Joined on 06-12-2007
    • Posts 288

    Re: Discount calculation

    darkmattar:
    why doesnt it just say $discount = $_REQUEST['discount_price'][$key] == 0?100:0;

    Don't forget that you could still have a division by 0 if $_REQUEST['list_price'][$key] == 0    :)

    Anyway, I've corrected the code to, you know, something that actually makes sense.

     

    Edit: also, it's not a cookie, but the value of an input field, named something like "discount_price[2]". And it is set by the user anyway (we're talking back-office here).

    If mixed metaphors were illegal, I'd be having an indigestion.
    Filed under:
  • 03-03-2008 9:28 AM In reply to

    Re: Discount calculation

    Zecc:
    Just bumped against this code on my current project:

    Ah, I think I see the problem... 

     
    Zecc:
    if($_REQUEST['discount_price'][$key] != 0) //avoid division by 0
    {
    $value = floatval($_REQUEST['list_price'][$key]);
    $discount = - 100 + ($value * 100) / $value;
    $discount = round($discount, 2);
    }
    else
    $discount = 100;

     

    Clealy, should be named "%discount" rather than "$discount" since it's calculating the discount in percent, not dollars!

  • 03-03-2008 11:42 AM In reply to

    • Zecc
    • Top 100 Contributor
    • Joined on 06-12-2007
    • Posts 288

    Re: Discount calculation

    And lower on the same method:

    foreach($response_items as $item)

    {

        if( ! is_array($response_items) )

            $item = $response_items;

     

        // In the middle of a bunch of code where $item is used, but not $response_items :

        // (notice the missing, or perhaps misplaced, parenthesis) 

        $_ POST['discount_price'][$item->item_no] = $item->price * 1 - ($item->final_discount / 100);

     

         if( ! is_array($response_items) )

            break;

    }

     

    I guess I can get the intent behind the weird code, but were they asleep to actually think this would work?

    If mixed metaphors were illegal, I'd be having an indigestion.
  • 03-03-2008 1:50 PM In reply to

    • mfah
    • Top 500 Contributor
    • Joined on 12-01-2007
    • Posts 112

    Re: Discount calculation

    Please! No more! Still trying to get over the horror of the first one! I can't feel my legs!
  • 03-03-2008 6:45 PM In reply to

    Re: Discount calculation

     I trust this code isn't in production anywhere. If it is maybe you can add some logic to give any user %iali% a special discount. Whatever it is I'd be buying ... does it have a good resale value?

     

    There are three kinds of people: those who make things happen, those who watch things happen and those who wonder what happened.
    Filed under: ,
  • 03-03-2008 7:16 PM In reply to

    Re: Discount calculation

     

    Zecc:

    Don't forget that you could still have a division by 0 if $_REQUEST['list_price'][$key] == 0    :)

    Anyway, I've corrected the code to, you know, something that actually makes sense.

     

    Edit: also, it's not a cookie, but the value of an input field, named something like "discount_price[2]". And it is set by the user anyway (we're talking back-office here).

    $_REQUEST includes Cookies, GETs, POSTS it depends on the ordering/setup in your php.ini as to what gets through in what order. No matter how you slice it, if you use $_REQUEST and don't validate where the data is coming from, the user can just submit whatever they please if they know the name of the key to name it (ie, if they can view source and just pull your JS submit code/form names).

    And my code only results in division by zero because the original code has division by zero if there is a real float value for $value. 

    $discount = - 100 + ($value * 100) / $value; 
    This -100 + ($value/$value)*100

    This is the same as -100 + ($value/$value)*100, which is the same as -100+100. That's 0.

    To be honest, I can't figure out what it was even attempting to truly put in the $discount variable... the actual $ amount of the discount, the % of discount, or a 0 or 100 based on what the user inputs for [list_price]['key']?

    It makes me cry. I'll go back to looking at code I get paid for, it hurts me less. 

  • 03-04-2008 5:16 AM In reply to

    • Zecc
    • Top 100 Contributor
    • Joined on 06-12-2007
    • Posts 288

    Re: Discount calculation

    darkmattar:
    $_REQUEST includes Cookies, GETs, POSTS it depends on the ordering/setup in your php.ini as to what gets through in what order. No matter how you slice it, if you use $_REQUEST and don't validate where the data is coming from, the user can just submit whatever they please if they know the name of the key to name it (ie, if they can view source and just pull your JS submit code/form names).
    If only the js code were that clear to read..  :)

    But yes, I guess you're right. But that really doesn't matter anyway, because they can give whatever discounts they want to their clients. It's a clear text input field.

    darkmattar:
    This is the same as -100 + ($value/$value)*100, which is the same as -100+100. That's 0.
    Actually, this is zero if $value != 0 and division by zero if $value == 0. But enough of that already.

    If mixed metaphors were illegal, I'd be having an indigestion.
Page 1 of 1 (10 items)
Powered by Community Server (Non-Commercial Edition), by Telligent Systems