CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
distributed amounts

 



lightspd
Novice

Mar 15, 2013, 10:09 AM

Post #1 of 6 (936 views)
distributed amounts Can't Post

Hi All,

This may belong more in the beginners section. I'm hoping someone with a bit more data manipulation experience might be able to help me or have an idea. Anyway, here is the issue.

I have a array of hashrefs, each hashref is a ticket item, with several fields. I have no control over the received data, it will always be an array of hashrefs. The ones I'm working with are the amt and discounted amount. The discounts amount gets subtracted from every time a discount is applied to that item. Issue is that there are both distributed discounts, that apply to every item and individual item discounts.

When a discount is distributed I need to make sure it's not subtracting more than is currently in the discounted amount field. If it is, then distribute the remainder to the rest of the items, if they have a high enough amt.

I haven't started to code it, so don't have anything to show you, but suggestions are greatly appreciated. I'm sure the answer involves a simple sort on discounted amount, but I'm drawing a blank.

Cheers.


(This post was edited by lightspd on Mar 15, 2013, 10:49 AM)


Kenosis
User

Mar 15, 2013, 11:22 AM

Post #2 of 6 (930 views)
Re: [lightspd] distributed amounts [In reply to] Can't Post

Your description is unclear--at least to me.

You say that the fields your working with are the amt and discounted amount. So, you've mentioned two fields:

Code
amt 
discounted amount

But then you say, The discounts amount gets subtracted... Did you mean discounted amount? Is discounts amount a new field?

To continue...


The discounts amount gets subtracted from every time a discount is applied to that item.

What does The discounts amount gets subtracted from every time... mean? The value of a field is subtracted from the value of another field--not a time. Then you mention that item. What item?

To continue...

Issue is that there are both distributed discounts, that apply to every item and individual item discounts.

Bu you said earlier that your working with the fields amt and discounted amount. Are you working with item fields, too? Are the distributed discounts and individual item discounts both included in the discounted amount?

To continue...

When a discount is distributed I need to make sure it's not subtracting more than is currently in the discounted amount field. If it is, then distribute the remainder to the rest of the items, if they have a high enough amt.

Did you mean to use the word "subtracting" in the above? Again, it sounds like you're using more fields than what you initially mentioned, viz., distributed discount, discounted amount, (implicitly) items, perhaps a grand total...

Mentioning the array, hashes and hash references isn't relevant to the issue you've raised. It seems you're trying to get a handle on the relations of the fields within each (purchase) dataset that's represented by a hash.

Perhaps it would help if you provided some sample data to illustrate the issue you're bumping against.

My apologies in advance if your issue is actually quite clear, but I'm just not understanding it...


lightspd
Novice

Mar 15, 2013, 12:10 PM

Post #3 of 6 (928 views)
Re: [Kenosis] distributed amounts [In reply to] Can't Post

EDIT: I'll also add, that I'm limited to perl version 5.8.8, I really really wish I wasn't but that's what I got.

No my apologies, I wasn't very clear.

I'm writing a new discount function for an already existing code base. I get passed the amt or percentage to be discounted, then distribute them over all items that meet X conditions. Initially the spec said evenly distributed over all items, then I got the complaint that the item becomes neg and accounting people didn't like that. ( I probably should have thought of it myself.)

To clarify, I just insert the discount below the discounted item and the math is done else where. The discAmt is used to keep track the item price minus any discounts that have been applied to it, the field is also used on percentage discounts. The amt field is the amt of the item itself, never gets changed.

Here is an example of the basic structure.


Code
 
my @AOH = (
{ plu => "coke",
amt => "299",
discAmt => "299",
void => "f",
},
{ plu => "pepsi",
amt => "199",
discAmt => "199",
void => "f",
},
);

foreach my $hash ( @AOH ){
#process $1.00 off discount for example.
$hash->{'discAmt'} -= 50;
splice in discount hashref below item.
}


Now my issue comes in because they can enter as many discounts as they like and do, as long as it doesn't take the total below zero.

Using the example above, they apply a $4.00 off discount, so $2.00 off each item. The pepsi is $1.99 so I should take $1.99 off that and $2.01 off the coke.

I hope this clarifies the issue I'm having.

Cheers.


(This post was edited by lightspd on Mar 15, 2013, 12:38 PM)


Kenosis
User

Mar 15, 2013, 4:11 PM

Post #4 of 6 (920 views)
Re: [lightspd] distributed amounts [In reply to] Can't Post

Perhaps the following will be helpful:

Code
use strict; 
use warnings;

my @AOH = (
{
plu => "coke",
amt => "299",
discAmt => "299",
void => "f",
},
{
plu => "pepsi",
amt => "199",
discAmt => "199",
void => "f",
},

);

@AOH = sort { $a->{'amt'} <=> $b->{'amt'} } @AOH;
my $discount = 400;
my $numItems = @AOH;

for my $item (@AOH) {
my $finalDiscount =
$discount / $numItems <= $item->{amt}
? $discount / $numItems
: $item->{amt};

$discount -= $finalDiscount;
$numItems--;

print 'Item: '
. $item->{plu}
. "\t Amt: "
. $item->{amt}
. "\tDiscount: "
. $finalDiscount . "\n"

}


Output:

Code
Item: pepsi	 Amt: 199	Discount: 199 
Item: coke Amt: 299 Discount: 201


It first sorts the hash refs from lowest to highest amt. In the for loop, the ternary operator checks whether dividing the total discount amount evenly may exceed the item cost. If it does, the item cost is the discount amount, else the total discount left divided by the number of items left is used.

Tested OK using three items...

Edit: Changed

Code
{ $a->{'discAmt'} <=> $b->{'discAmt'} }


to

Code
{ $a->{'amt'} <=> $b->{'amt'} }



(This post was edited by Kenosis on Mar 15, 2013, 5:04 PM)


lightspd
Novice

Mar 15, 2013, 4:53 PM

Post #5 of 6 (915 views)
Re: [Kenosis] distributed amounts [In reply to] Can't Post

Cheers Kenosis, I'll definitely be able to adapt this.


Kenosis
User

Mar 15, 2013, 4:58 PM

Post #6 of 6 (914 views)
Re: [lightspd] distributed amounts [In reply to] Can't Post

Excellent! Am glad you can use it...

Edit: I carelessly used the wrong hash key

Code
{ $a->{'discAmt'} <=> $b->{'discAmt'} }


When I meant:

Code
{ $a->{'amt'} <=> $b->{'amt'} }


I've updated the original posting.

I need more coffee...


(This post was edited by Kenosis on Mar 15, 2013, 5:02 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives