| 1 | # ------------------------------------------------------------------------------------- | ||
| 2 | # MKDoc::ECommerce::Order | ||
| 3 | # ------------------------------------------------------------------------------------- | ||
| 4 | # Author : Jean-Michel Hiver <jhiver@mkdoc.com>. | ||
| 5 | # Copyright : (c) MKDoc Holdings Ltd, 2003 | ||
| 6 | # | ||
| 7 | # This class represents an order. An order is connected to a basket object (which | ||
| 8 | # represents a list of products ordered), a delivery address (an Address object), | ||
| 9 | # a country of delivery, and a tactic for zone delivery and shipping rules. | ||
| 10 | # ------------------------------------------------------------------------------------- | ||
| 11 | package MKDoc::ECommerce::Order; | ||
| 12 | use strict; | ||
| 13 | use warnings; | ||
| 14 | use MKDoc::ECommerce::Rules; | ||
| 15 | use MKDoc::ECommerce::Basket; | ||
| 16 | use MKDoc::ECommerce::Address; | ||
| 17 | |||
| 18 | use Petal::Mail; | ||
| 19 | use Petal; | ||
| 20 | |||
| 21 | |||
| 22 | ## | ||
| 23 | # $class->new (%args); | ||
| 24 | # -------------------- | ||
| 25 | # Constructs a new Address object, comprising: | ||
| 26 | # | ||
| 27 | # session_id => $session_id | ||
| 28 | # address => $address_object | ||
| 29 | # basket => $shopping_basket | ||
| 30 | ## | ||
| 31 | sub new | ||
| 32 | { | ||
| 33 | my $class = shift; | ||
| 34 | my $self = bless { @_ }, $class; | ||
| 35 | $self->{timestamp} = time(); | ||
| 36 | |||
| 37 | my $rules = new MKDoc::ECommerce::Rules ( | ||
| 38 | session => $self->{session}, | ||
| 39 | basket => $self->{basket}, | ||
| 40 | address => $self->{address}, | ||
| 41 | |||
| 42 | subtotal => $self->{basket}->total(), | ||
| 43 | country => $self->{address}->country(), | ||
| 44 | ); | ||
| 45 | |||
| 46 | $self->{deal_price} = $rules->deal_price(); | ||
| 47 | |||
| 48 | $self->save(); | ||
| 49 | return $self; | ||
| 50 | } | ||
| 51 | |||
| 52 | |||
| 53 | sub is_pending | ||
| 54 | { | ||
| 55 | my $self = shift; | ||
| 56 | $self->is_accepted() && return; | ||
| 57 | $self->is_rejected() && return; | ||
| 58 | return 1; | ||
| 59 | } | ||
| 60 | |||
| 61 | |||
| 62 | sub is_accepted | ||
| 63 | { | ||
| 64 | my $self = shift; | ||
| 65 | return $self->{is_accepted}; | ||
| 66 | } | ||
| 67 | |||
| 68 | |||
| 69 | sub is_rejected | ||
| 70 | { | ||
| 71 | my $self = shift; | ||
| 72 | return $self->{is_rejected}; | ||
| 73 | } | ||
| 74 | |||
| 75 | |||
| 76 | sub accept | ||
| 77 | { | ||
| 78 | my $self = shift; | ||
| 79 | $self->is_pending() || return; | ||
| 80 | $self->_accept_send_mail_merchant(); | ||
| 81 | $self->_accept_send_mail_customer(); | ||
| 82 | $self->_accept_clear_session(); | ||
| 83 | $self->{is_accepted} = 1; | ||
| 84 | $self->save(); | ||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | sub reject | ||
| 89 | { | ||
| 90 | my $self = shift; | ||
| 91 | $self->is_pending() || return; | ||
| 92 | $self->_reject_send_mail_merchant(); | ||
| 93 | $self->_reject_send_mail_customer(); | ||
| 94 | $self->{is_rejected} = 1; | ||
| 95 | $self->save(); | ||
| 96 | } | ||
| 97 | |||
| 98 | |||
| 99 | sub _reject_send_mail_customer | ||
| 100 | { | ||
| 101 | my $self = shift; | ||
| 102 | my $mail = new Petal::Mail ( | ||
| 103 | file => 'shop/email/reject_customer', | ||
| 104 | lang => flo::Standard::current_document()->language(), | ||
| 105 | ); | ||
| 106 | |||
| 107 | $mail->send (self => $self); | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | sub _reject_send_mail_merchant | ||
| 112 | { | ||
| 113 | my $self = shift; | ||
| 114 | my $mail = new Petal::Mail ( | ||
| 115 | file => 'shop/email/reject_merchant', | ||
| 116 | lang => flo::Standard::current_document()->language(), | ||
| 117 | ); | ||
| 118 | |||
| 119 | $mail->send (self => $self); | ||
| 120 | } | ||
| 121 | |||
| 122 | |||
| 123 | sub _accept_send_mail_customer | ||
| 124 | { | ||
| 125 | my $self = shift; | ||
| 126 | my $mail = new Petal::Mail ( | ||
| 127 | file => 'shop/email/accept_customer', | ||
| 128 | lang => flo::Standard::current_document()->language(), | ||
| 129 | ); | ||
| 130 | |||
| 131 | $mail->send (self => $self); | ||
| 132 | } | ||
| 133 | |||
| 134 | |||
| 135 | sub _accept_send_mail_merchant | ||
| 136 | { | ||
| 137 | my $self = shift; | ||
| 138 | my $mail = new Petal::Mail ( | ||
| 139 | file => 'shop/email/accept_merchant', | ||
| 140 | lang => flo::Standard::current_document()->language(), | ||
| 141 | ); | ||
| 142 | |||
| 143 | $mail->send (self => $self); | ||
| 144 | } | ||
| 145 | |||
| 146 | |||
| 147 | sub _accept_clear_session | ||
| 148 | { | ||
| 149 | my $self = shift; | ||
| 150 | my $session = $self->session() || return; | ||
| 151 | $session->{basket}->clear(); | ||
| 152 | $session->save(); | ||
| 153 | } | ||
| 154 | |||
| 155 | |||
| 156 | sub session | ||
| 157 | { | ||
| 158 | my $self = shift; | ||
| 159 | return MKDoc::Session->load ( $self->{session_id} ) || return; | ||
| 160 | } | ||
| 161 | |||
| 162 | |||
| 163 | sub date_placed | ||
| 164 | { | ||
| 165 | my $self = shift; | ||
| 166 | my $time = $self->{timestamp}; | ||
| 167 | |||
| 168 | # 0 1 2 3 4 5 6 7 8 | ||
| 169 | # ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) | ||
| 170 | my @time = localtime ($time); | ||
| 171 | |||
| 172 | $time[4] += 1; | ||
| 173 | $time[4] = "0$time[4]" unless (length ($time[4]) == 2); | ||
| 174 | |||
| 175 | $time[5] += 1900; | ||
| 176 | $time[2] = "0$time[2]" unless (length ($time[2]) == 2); | ||
| 177 | $time[1] = "0$time[1]" unless (length ($time[1]) == 2); | ||
| 178 | $time[0] = "0$time[0]" unless (length ($time[0]) == 2); | ||
| 179 | |||
| 180 | return "$time[5]-$time[4]-$time[3] $time[2]:$time[1]:$time[0]"; | ||
| 181 | } | ||
| 182 | |||
| 183 | |||
| 184 | sub admin_user { return flo::Standard::table ('Editor')->get ( Login => 'admin') } | ||
| 185 | |||
| 186 | |||
| 187 | sub root { return flo::Standard::table ('Document')->get ( Full_Path => '/' ) } | ||
| 188 | |||
| 189 | sub _gen_id | ||
| 190 | { | ||
| 191 | my $class = shift; | ||
| 192 | my $time = time(); | ||
| 193 | my ($_1, $_2, $_3, $_4) = $time =~ /(.)(...)(...)(...)/; | ||
| 194 | my $r1 = join '', map { uc (chr (ord ('a') + int (rand 26))) } 1..3; | ||
| 195 | |||
| 196 | return "$_1-$_2-$_3-$_4-$r1"; | ||
| 197 | } | ||
| 198 | |||
| 199 | |||
| 200 | 1; | ||
| 201 | |||
| 202 | |||
| 203 | __END__ |