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