1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
package Vending::ReturnedItem;
use strict;
use warnings;
use Vending;
class Vending::ReturnedItem {
has => [
name => { is => 'String' },
value => { is => 'Float' },
source_machine_location => { is => 'Vending::MachineLocation', id_by => 'source_machine_location_id' },
price => { via => 'source_machine_location', to => 'price' },
cost_cents => { via => 'source_machine_location', to => 'cost_cents' },
],
doc => 'Represents a thing being returned to the user, not stored in the database',
};
# Create Vending::ReturnedItem objects from a product or coin
# To enforce vending machine rules, the passed-in item is deleted
sub create_from_vend_items {
my($class,@items) = @_;
my $transaction = UR::Context::Transaction->begin();
my @returned_items = eval {
my @returned_items;
foreach my $item ( @items ) {
my %create_params = ( name => $item->name, source_machine_location_id => $item->machine_location_id );
if ($item->isa('Vending::Coin')) {
$create_params{'value'} = $item->value_cents;
} elsif ($item->isa('Vending::Merchandise')) {
$create_params{'value'} = $item->cost_cents;
} else {
die "Can't create a Vending::ReturnedItem from an object of type ".$item->class;
}
$item->delete();
my $returned_item = $class->create(%create_params);
push @returned_items, $returned_item;
}
return @returned_items;
};
if ($@) {
$class->error_message($@);
$transaction->rollback();
} else {
$transaction->commit();
return @returned_items;
}
}
1;
|