anonymous order display
[bse.git] / site / cgi-bin / modules / BSE / TB / Order.pm
CommitLineData
0ec4ac8a
TC
1package BSE::TB::Order;
2use strict;
3# represents an order from the database
4use Squirrel::Row;
5use vars qw/@ISA/;
6@ISA = qw/Squirrel::Row/;
5d88571c 7use Carp 'confess';
0ec4ac8a 8
080fc207 9our $VERSION = "1.002";
cb7fd78d 10
0ec4ac8a
TC
11sub columns {
12 return qw/id
13 delivFirstName delivLastName delivStreet delivSuburb delivState
14 delivPostCode delivCountry
15 billFirstName billLastName billStreet billSuburb billState
16 billPostCode billCountry
17 telephone facsimile emailAddress
18 total wholesaleTotal gst orderDate
19 ccNumberHash ccName ccExpiryHash ccType
20 filled whenFilled whoFilled paidFor paymentReceipt
21 randomId cancelled userId paymentType
22 customInt1 customInt2 customInt3 customInt4 customInt5
23 customStr1 customStr2 customStr3 customStr4 customStr5
24 instructions billTelephone billFacsimile billEmail
e3d242f7 25 siteuser_id affiliate_code shipping_cost
41e7c841
TC
26 delivMobile billMobile
27 ccOnline ccSuccess ccReceipt ccStatus ccStatusText
37dd20ad 28 ccStatus2 ccTranId complete delivOrganization billOrganization
d9803c26 29 delivStreet2 billStreet2 purchase_order shipping_method
13a986ee 30 shipping_name shipping_trace
080fc207 31 paypal_token paypal_tran_id freight_tracking/;
0ec4ac8a
TC
32}
33
14604ada
TC
34sub defaults {
35 require BSE::Util::SQL;
36 require Digest::MD5;
37 return
38 (
39 total => 0,
40 wholesaleTotal => 0,
41 gst => 0,
42 orderDate => BSE::Util::SQL::now_datetime(),
43 filled => 0,
44 whenFilled => undef,
45 whoFilled => '',
46 paidFor => 0,
47 paymentReceipt => '',
48 randomId => Digest::MD5::md5_hex(time().rand().{}.$$),
49 ccNumberHash => '',
50 ccName => '',
51 ccExpiryHash => '',
52 ccType => '',
53 randomId => '',
54 cancelled => 0,
55 userId => '',
56 paymentType => 0,
57 customInt1 => undef,
58 customInt2 => undef,
59 customInt3 => undef,
60 customInt4 => undef,
61 customInt5 => undef,
62 customStr1 => undef,
63 customStr2 => undef,
64 customStr3 => undef,
65 customStr4 => undef,
66 customStr5 => undef,
67 instructions => '',
68 siteuser_id => undef,
69 affiliate_code => '',
70 shipping_cost => 0,
71 ccOnline => 0,
72 ccSuccess => 0,
73 ccReceipt => '',
74 ccStatus => 0,
75 ccStatusText => '',
76 ccStatus2 => '',
77 ccTranId => '',
78 complete => 0,
79 purchase_order => '',
80 shipping_method => '',
81 shipping_name => '',
82 shipping_trace => undef,
080fc207 83 freight_tracking => "",
14604ada
TC
84 );
85}
86
13a986ee
TC
87sub table { "orders" }
88
14604ada
TC
89sub address_columns {
90 return qw/
91 delivFirstName delivLastName delivStreet delivSuburb delivState
92 delivPostCode delivCountry
93 billFirstName billLastName billStreet billSuburb billState
94 billPostCode billCountry
95 telephone facsimile emailAddress
96 instructions billTelephone billFacsimile billEmail
97 delivMobile billMobile
98 delivOrganization billOrganization
99 delivStreet2 billStreet2/;
100}
101
102sub user_columns {
103 return qw/userId siteuser_id/;
104}
105
106sub payment_columns {
107 return qw/ccNumberHash ccName ccExpiryHash ccType
108 paidFor paymentReceipt paymentType
109 ccOnline ccSuccess ccReceipt ccStatus ccStatusText
110 ccStatus2 ccTranId/;
111}
112
0ec4ac8a
TC
113=item siteuser
114
115returns the SiteUser object of the user who made this order.
116
117=cut
118
119sub siteuser {
120 my ($self) = @_;
121
122 $self->{userId} or return;
123
124 require SiteUsers;
125
126 return ( SiteUsers->getBy(userId=>$self->{userId}) )[0];
127}
128
129sub items {
130 my ($self) = @_;
131
132 require BSE::TB::OrderItems;
133 return BSE::TB::OrderItems->getBy(orderId => $self->{id});
134}
135
ab2cd916
TC
136sub files {
137 my ($self) = @_;
138
139 BSE::DB->query(orderFiles=>$self->{id});
140}
141
eb9d306d
TC
142sub paid_files {
143 my ($self) = @_;
144
145 $self->paidFor
146 or return;
147
148 require BSE::TB::ArticleFiles;
149 return BSE::TB::ArticleFiles->getSpecial(orderPaidFor => $self->id);
150}
151
ab2cd916
TC
152sub products {
153 my ($self) = @_;
154
155 require Products;
156 Products->getSpecial(orderProducts=>$self->{id});
157}
158
41e7c841
TC
159sub valid_fields {
160 my ($class, $cfg) = @_;
161
162 my %fields =
163 (
37dd20ad
TC
164 delivFirstName => { description=>'Delivery First Name',
165 rules=>'dh_one_line' },
166 delivLastName => { description => 'Delivery Last Name',
167 rules=>'dh_one_line' },
168 delivOrganization => { description => 'Delivery Organization',
169 rules=>'dh_one_line' },
170 delivStreet => { description => 'Delivery Street',
171 rules=>'dh_one_line' },
172 delivStreet2 => { description => 'Delivery Street 2',
173 rules=>'dh_one_line' },
174 delivState => { description => 'Delivery State',
175 rules=>'dh_one_line' },
176 delivSuburb => { description => 'Delivery Suburb',
177 rules=>'dh_one_line' },
178 delivPostCode => { description => 'Delivery Post Code',
9074efa2 179 rules=>'dh_one_line;dh_int_postcode' },
37dd20ad
TC
180 delivCountry => { description => 'Delivery Country',
181 rules=>'dh_one_line' },
182 billFirstName => { description => 'Billing First Name',
183 rules=>'dh_one_line' },
184 billLastName => { description => 'Billing Last Name',
185 rules=>'dh_one_line' },
186 billOrganization => { description => 'Billing Organization',
187 rules=>'dh_one_line' },
188 billStreet => { description => 'Billing Street',
189 rules=>'dh_one_line' },
190 billStreet2 => { description => 'Billing Street 2',
191 rules=>'dh_one_line' },
192 billSuburb => { description => 'Billing Suburb',
193 rules=>'dh_one_line' },
194 billState => { description => 'Billing State',
195 rules=>'dh_one_line' },
196 billPostCode => { description => 'Billing Post Code',
9074efa2 197 rules=>'dh_one_line;dh_int_postcode' },
37dd20ad
TC
198 billCountry => { description => 'Billing First Name',
199 rules=>'dh_one_line' },
41e7c841
TC
200 telephone => { description => 'Telephone Number',
201 rules => "phone" },
202 facsimile => { description => 'Facsimile Number',
203 rules => 'phone' },
204 emailAddress => { description => 'Email Address',
205 rules=>'email;required' },
206 instructions => { description => 'Instructions' },
207 billTelephone => { description => 'Billing Telephone Number',
208 rules=>'phone' },
209 billFacsimile => { description => 'Billing Facsimile Number',
210 rules=>'phone' },
211 billEmail => { description => 'Billing Email Address',
212 rules => 'email' },
213 delivMobile => { description => 'Delivery Mobile Number',
214 rules => 'phone' },
215 billMobile => { description => 'Billing Mobile Number',
216 rules=>'phone' },
217 instructions => { description => 'Instructions' },
74b21f6d 218 purchase_order => { description => 'Purchase Order No' },
d8674b8b
AMS
219 shipping_cost => { description => 'Shipping charges' },
220 shipping_method => { description => 'Shipping method' },
41e7c841
TC
221 );
222
223 for my $field (keys %fields) {
224 my $display = $cfg->entry('shop', "display_$field");
225 $display and $fields{$field}{description} = $display;
226 }
227
228 return %fields;
229}
230
231sub valid_rules {
232 my ($class, $cfg) = @_;
233
234 return;
235}
236
237sub valid_payment_fields {
238 my ($class, $cfg) = @_;
239
240 my %fields =
241 (
242 cardNumber =>
243 {
244 description => "Credit Card Number",
245 rules=>"creditcardnumber",
246 },
247 cardExpiry =>
248 {
249 description => "Credit Card Expiry Date",
250 rules => 'creditcardexpirysingle',
251 },
252 cardHolder => { description => "Credit Card Holder" },
253 cardType => { description => "Credit Card Type" },
254 cardVerify =>
255 {
256 description => 'Card Verification Value',
257 rules => 'creditcardcvv',
258 },
259 );
260
261 for my $field (keys %fields) {
262 my $display = $cfg->entry('shop', "display_$field");
263 $display and $fields{$field}{description} = $display;
264 }
265
266 return %fields;
267}
268
269sub valid_payment_rules {
270 return;
271}
272
5d88571c
TC
273sub clear_items {
274 my ($self) = @_;
275
276 confess "Attempt to clear items on completed order $self->{id}"
277 if $self->{complete};
278
279 BSE::DB->run(deleteOrdersItems => $self->{id});
280}
281
14604ada
TC
282sub add_item {
283 my ($self, %opts) = @_;
284
285 my $prod = delete $opts{product}
286 or confess "Missing product option";
287 my $units = delete $opts{units} || 1;
288
289 my $options = '';
290 my @dboptions;
291 if ($opts{options}) {
292 if (ref $opts{options}) {
293 @dboptions = @{delete $opts{options}};
294 }
295 else {
296 $options = delete $opts{options};
297 }
298 }
299
300 require BSE::TB::OrderItems;
301 my %item =
302 (
303 productId => $prod->id,
304 orderId => $self->id,
305 units => $units,
306 price => $prod->retailPrice,
307 options => $options,
308 max_lapsed => 0,
309 session_id => 0,
310 ( map { $_ => $prod->{$_} }
311 qw/wholesalePrice gst customInt1 customInt2 customInt3 customStr1 customStr2 customStr3 title description subscription_id subscription_period product_code/
312 ),
313 );
314
315 $self->set_total($self->total + $prod->retailPrice * $units);
316
317 return BSE::TB::OrderItems->make(%item);
318}
319
13a986ee
TC
320sub deliv_country_code {
321 my ($self) = @_;
322
323 my $use_codes = BSE::Cfg->single->entry("shop", "country_code", 0);
324 if ($use_codes) {
325 return $self->delivCountry;
326 }
327 else {
328 require BSE::Countries;
329 return BSE::Countries::bse_country_code($self->delivCountry);
330 }
331}
332
0ec4ac8a 3331;