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