add support for storing custom metadata to cart/order items and options
[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;
33d04a1b 5use BSE::CustomData;
0ec4ac8a 6use vars qw/@ISA/;
33d04a1b 7@ISA = qw/Squirrel::Row BSE::CustomData/;
0ec4ac8a 8
33d04a1b 9our $VERSION = "1.008";
4ad0e50a
TC
10
11sub table { "order_item" }
cb7fd78d 12
0ec4ac8a
TC
13sub columns {
14 return qw/id productId orderId units price wholesalePrice gst options
15 customInt1 customInt2 customInt3 customStr1 customStr2 customStr3
7b4490e1 16 title description subscription_id subscription_period max_lapsed
33d04a1b
TC
17 session_id product_code tier_id product_discount product_discount_units
18 custom_data/;
4ad0e50a
TC
19}
20
21sub db_columns {
22 return map { $_ eq "description" ? "summary" : $_ } $_[0]->columns;
0ec4ac8a
TC
23}
24
14604ada
TC
25sub defaults {
26 return
27 (
28 units => 1,
29 options => '',
30 customInt1 => undef,
31 customInt2 => undef,
32 customInt3 => undef,
33 customStr1 => undef,
34 customStr2 => undef,
35 customStr3 => undef,
5014aef9
TC
36 tier_id => undef,
37 product_discount => 0,
38 product_discount_units => 0,
33d04a1b 39 custom_data => undef,
14604ada
TC
40 );
41}
42
58baa27b
TC
43sub option_list {
44 my ($self) = @_;
45
46 require BSE::TB::OrderItemOptions;
47 return sort { $a->{display_order} <=> $b->{display_order} }
48 BSE::TB::OrderItemOptions->getBy(order_item_id => $self->{id});
49}
50
13a986ee
TC
51sub product {
52 my ($self) = @_;
53
54 $self->productId == -1
55 and return;
10dd37f9
AO
56 require BSE::TB::Products;
57 return BSE::TB::Products->getByPkey($self->productId);
13a986ee
TC
58}
59
8d8895b4
TC
60sub option_hashes {
61 my ($self) = @_;
62
63 my $product = $self->product;
64 if (length $self->{options}) {
65 my @values = split /,/, $self->options;
66 return map
67 +{
68 id => $_->{id},
69 value => $_->{value},
70 desc => $_->{desc},
71 label => $_->{display},
72 }, $product->option_descs(BSE::Cfg->single, \@values);
73 }
74 else {
75 my @options = $self->option_list;
76 return map
77 +{
78 id => $_->original_id,
79 value => $_->value,
80 desc => $_->name,
81 label => $_->display
82 }, @options;
83 }
84}
85
86sub nice_options {
87 my ($self) = @_;
88
89 my @options = $self->option_hashes
90 or return '';
91
92 return '('.join(", ", map("$_->{desc} $_->{label}", @options)).')';
93}
94
f4f29389
TC
95sub session {
96 my ($self) = @_;
97
98 $self->session_id
99 or return;
100
101 require BSE::TB::SeminarSessions;
102 return BSE::TB::SeminarSessions->getByPkey($self->session_id);
103}
104
676f5398
TC
105# cart item compatibility
106sub retailPrice {
107 $_[0]->price;
108}
109
110sub extended {
111 my ($self, $name) = @_;
112
113 return $self->units * $self->$name();
114}
115
4ad0e50a
TC
116sub tier {
117 my ($self) = @_;
118
119 my $tier_id = $self->tier_id
120 or return;
121
122 require BSE::TB::PriceTiers;
123 my $tier = BSE::TB::PriceTiers->getByPkey($tier_id)
124 or return;
125
126 return $tier;
127}
128
0ec4ac8a 1291;