fix a cart object crash
[bse.git] / site / cgi-bin / modules / BSE / TB / OrderItem.pm
CommitLineData
0ec4ac8a
TC
1package BSE::TB::OrderItem;
2use strict;
3# represents an order line item from the database
4use Squirrel::Row;
5use vars qw/@ISA/;
6@ISA = qw/Squirrel::Row/;
7
8d8895b4 8our $VERSION = "1.001";
cb7fd78d 9
0ec4ac8a
TC
10sub columns {
11 return qw/id productId orderId units price wholesalePrice gst options
12 customInt1 customInt2 customInt3 customStr1 customStr2 customStr3
7b4490e1 13 title description subscription_id subscription_period max_lapsed
74b21f6d 14 session_id product_code/;
0ec4ac8a
TC
15}
16
14604ada
TC
17sub defaults {
18 return
19 (
20 units => 1,
21 options => '',
22 customInt1 => undef,
23 customInt2 => undef,
24 customInt3 => undef,
25 customStr1 => undef,
26 customStr2 => undef,
27 customStr3 => undef,
28 );
29}
30
58baa27b
TC
31sub option_list {
32 my ($self) = @_;
33
34 require BSE::TB::OrderItemOptions;
35 return sort { $a->{display_order} <=> $b->{display_order} }
36 BSE::TB::OrderItemOptions->getBy(order_item_id => $self->{id});
37}
38
13a986ee
TC
39sub product {
40 my ($self) = @_;
41
42 $self->productId == -1
43 and return;
44 require Products;
45 return Products->getByPkey($self->productId);
46}
47
8d8895b4
TC
48sub option_hashes {
49 my ($self) = @_;
50
51 my $product = $self->product;
52 if (length $self->{options}) {
53 my @values = split /,/, $self->options;
54 return map
55 +{
56 id => $_->{id},
57 value => $_->{value},
58 desc => $_->{desc},
59 label => $_->{display},
60 }, $product->option_descs(BSE::Cfg->single, \@values);
61 }
62 else {
63 my @options = $self->option_list;
64 return map
65 +{
66 id => $_->original_id,
67 value => $_->value,
68 desc => $_->name,
69 label => $_->display
70 }, @options;
71 }
72}
73
74sub nice_options {
75 my ($self) = @_;
76
77 my @options = $self->option_hashes
78 or return '';
79
80 return '('.join(", ", map("$_->{desc} $_->{label}", @options)).')';
81}
82
0ec4ac8a 831;