]> git.imager.perl.org - bse.git/blob - site/cgi-bin/modules/BSE/TB/OrderItem.pm
the metadata fetcher
[bse.git] / site / cgi-bin / modules / BSE / TB / OrderItem.pm
1 package BSE::TB::OrderItem;
2 use strict;
3 # represents an order line item from the database
4 use Squirrel::Row;
5 use vars qw/@ISA/;
6 @ISA = qw/Squirrel::Row/;
7
8 our $VERSION = "1.006";
9
10 sub table { "order_item" }
11
12 sub columns {
13   return qw/id productId orderId units price wholesalePrice gst options
14             customInt1 customInt2 customInt3 customStr1 customStr2 customStr3
15             title description subscription_id subscription_period max_lapsed
16             session_id product_code tier_id product_discount product_discount_units/;
17 }
18
19 sub db_columns {
20   return map { $_ eq "description" ? "summary" : $_ } $_[0]->columns;
21 }
22
23 sub 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
37 sub 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
45 sub product {
46   my ($self) = @_;
47
48   $self->productId == -1
49     and return;
50   require BSE::TB::Products;
51   return BSE::TB::Products->getByPkey($self->productId);
52 }
53
54 sub 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
80 sub 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
89 sub 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
99 # cart item compatibility
100 sub retailPrice {
101   $_[0]->price;
102 }
103
104 sub extended {
105   my ($self, $name) = @_;
106
107   return $self->units * $self->$name();
108 }
109
110 sub 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
123 1;