1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
|
#### begin order logging of transaction ID [value mv_transaction_id] #####
[tmp transaction_record]
[comment]
First we dump the order to a file to track closely if something goes wrong.
[/comment]
[write-relative-file file="orders/session/[value mv_transaction_id]/[data session id].dump" interpolate=1][dump][/write-relative-file]
[comment]
Now we initialize some stuff. We set the DB to write mode, and set
some variables to known values.
[/comment]
[tmpn auto_create][/tmpn]
[loop list="transactions orderline inventory userdb"]
[flag type=write table="[loop-code]"]
[/loop]
Total order: [total-cost]
Payment processing...payment_method=[value mv_order_profile]
[set do_payment][/set]
[set do_invoice][/set]
[try]
[comment]
Handling of payment certificates. We set the temporary total, and the
amount remaining to be paid by any other payment method.
At the top, we initialize some variables.
[/comment]
[tmp tmp_total][total-cost noformat=1][/tmp]
[set charge_total_message][/set]
[set pay_cert_total][/set]
[tmp tmp_remaining][total-cost noformat=1][/tmp]
[calc]
$Tag->tmp('certs_to_use') ;
if($Values->{use_pay_cert}) {
my @things = split /\0/, $Values->{use_pay_cert};
$Scratch->{certs_to_use} = join ",", @things;
}
elsif($Values->{mv_order_profile} eq 'pay_cert') {
$Scratch->{certs_to_use} = $Scratch->{pay_cert_code};
}
return;
[/calc]
[if scratch certs_to_use]
Redeeming payment certs: [scratch certs_to_use]
[seti tmp_remaining][pay-cert-redeem
certs="[scratch certs_to_use]"
die=1
][/seti]
[if scratch tmp_remaining eq '']
[calc]
die errmsg("Gift cert authorization failed.") . "\n";
[/calc]
[/if]
Amount remaining: [scratch tmp_remaining]
[/if]
[/try]
[catch error-set="Payment process" error-scratch="mv_route_failed"]
There was an error accepting payment: $ERROR$
[/catch]
[goto if="[scratch mv_route_failed]"]
[try]
[if value mv_order_profile eq pay_cert]
[if scratch tmp_remaining == 0]
Fully paid by payment cert.
[else]
Not enough value on payment cert.
[calc]
die("Not enough value on payment certificates.");
[/calc]
[/else]
[/if]
[elsif scratch tmp_remaining == 0]
Fully paid by payment cert.
[/elsif]
[elsif value mv_order_profile eq postal]
[comment]Sales order only[/comment]
Payment: [value name=mv_payment set="Check or Money Order (will call)"]
[/elsif]
[elsif value mv_order_profile eq purchase_order]
limit=[scratch tmp_climit]
total=[data session latest_total]
[perl userdb]
return unless length($Scratch->{tmp_climit});
my $limit = $Tag->data( 'userdb', 'credit_limit', $Session->{username});
my $minus = $Session->{latest_total};
$minus = -$minus;
my $expect = $limit + $minus;
Log("Access to userdb worked... limit=$limit minus=$minus expect=$expect");
$Tag->data(
'userdb',
'credit_limit',
$Session->{username},
{
value => $minus,
increment => 1,
}
);
my $remain = $Tag->data( 'userdb', 'credit_limit', $Session->{username});
Log("Write to userdb worked...expect='$expect' remain='$remain'");
if ($expect ne $remain) {
die errmsg(
"error setting credit limit! Limit was %s, subtract %s, got %s, expect %s.\n",
$limit,
$Session->{latest_total},
$remain,
$expect,
);
}
$Scratch->{credit_limit} = $remain;
return "Credit limit was=$limit\nCredit limit now=$remain\n";
[/perl]
[/elsif]
[elsif value mv_order_profile eq online_check]
[if variable MV_ONLINE_CHECK_MODE]
[calc]
return if $Scratch->{tmp_total} == $Scratch->{tmp_remaining};
my $msg = sprintf "Your checking account was charged %.2f", $Scratch->{tmp_remaining};
$Scratch->{pay_cert_total} = $Scratch->{tmp_total} - $Scratch->{tmp_remaining};
$Scratch->{charge_total_message} = $msg;
return "Check will be written for $Scratch->{tmp_remaining}";
[/calc]
Online check with payment mode=[var MV_ONLINE_CHECK_MODE]
[tmp name="charge_succeed"][charge route="[var MV_ONLINE_CHECK_MODE]" amount="[scratch tmp_remaining]" order_id="[value mv_transaction_id]" method="[var MV_ONLINE_CHECK_METHOD]"][/tmp]
[if scratch charge_succeed]
[then]
[set do_invoice]1[/set]
[set do_payment]1[/set]
Real-time online check succeeded. ID=[data session payment_id] amount=[scratch tmp_remaining]
[/then]
[else]
Real-time online check FAILED. Reason: [data session payment_error]
[calc]
for(qw/
charge_total_message
pay_cert_total
/)
{
delete $Scratch->{$_};
}
die errmsg(
"Real-time online check failed. Reason: %s\n",
errmsg($Session->{payment_error}),
);
[/calc]
[/else]
[/if]
[else]
[set do_invoice]1[/set]
[/else]
[/if]
[/elsif]
[elsif value mv_order_profile eq cod]
[comment] do nothing [/comment]
[/elsif]
[elsif value mv_order_profile eq bank_transfer]
[comment] do nothing [/comment]
[/elsif]
[elsif variable MV_PAYMENT_MODE]
[calc]
return if $Scratch->{tmp_total} == $Scratch->{tmp_remaining};
my $msg = sprintf "Your credit card was charged %.2f", $Scratch->{tmp_remaining};
$Scratch->{pay_cert_total} = $Scratch->{tmp_total} - $Scratch->{tmp_remaining};
$Scratch->{charge_total_message} = $msg;
return "Credit card will be charged $Scratch->{tmp_remaining}";
[/calc]
Charging with payment mode=[var MV_PAYMENT_MODE]
[tmp name="charge_succeed"][charge route="[var MV_PAYMENT_MODE]" amount="[scratch tmp_remaining]" order_id="[value mv_transaction_id]"][/tmp]
[if scratch charge_succeed]
[then]
[set do_invoice]1[/set]
[set do_payment]1[/set]
Real-time charge succeeded. ID=[data session payment_id] amount=[scratch tmp_remaining]
[/then]
[else]
Real-time charge FAILED. Reason: [data session payment_error]
[calc]
for(qw/
charge_total_message
pay_cert_total
/)
{
delete $Scratch->{$_};
}
die errmsg(
"Real-time charge failed. Reason: %s\n",
errmsg($Session->{payment_error}),
);
[/calc]
[/else]
[/if]
[/elsif]
[else]
Offline credit card [value mv_credit_card_type] [value mv_credit_card_reference].
Payment: [value name=mv_payment set="credit_card"]
[/else]
[/if]
[calc]
$Values->{mv_payment} =~ s/\%c/$Values->{mv_credit_card_type}/g;
return;
[/calc]
[/try]
[catch error-scratch="mv_route_failed"]
There was an error accepting payment: $ERROR$
[if scratch pay_certs_to_capture]
voiding pay_cert auth, void id:[pay-cert tid="[scratch pay_certs_to_capture]" void=1]
[/if]
[/catch]
[goto if="[scratch mv_route_failed]"]
[comment]
Now we have accepted payment, update the order number.
[/comment]
Set order number in values: [value
name=mv_order_number
set="[counter
name=`
$Session->{current_route}{counter_name}
|| $Session->{current_route}{counter}
|| $Config->{OrderCounter}
`
sql=`$Session->{current_route}{sql_counter}`
start=`$Session->{current_route}{first_order_number}`
date=`$Session->{current_route}{date_counter}`
]"
]
Set order number in session: [calc]
$Session->{mv_order_number} = $Values->{mv_order_number};
[/calc]
[if type=explicit compare=`
if($Session->{admin} and $Values->{order_desk_entry}) {
## This is ordered from admin, don't login
## We scrub that order_desk_entry value below if not admin
return 0;
}
delete $Values->{order_desk_entry};
return 1 if ! $Session->{logged_in} or $Session->{login_table} ne 'userdb';
return 0;
`]
[try]
[if session logged_in]
[userdb function=logout clear=0 clear_cart=0]
[/if]
[tmp auto_create]1[/tmp]
[calcn]
my $pass = '';
my @lets = ('A'..'Z', 'a'..'z', '0'..'9');
$pass .= $lets[ rand @lets ]
while length($pass) < 8;
$Tag->tmpn('tmp_pass', $pass);
return;
[/calcn]
[if type=explicit compare=|
[userdb
function=new_account
assign_username=1
password='[scratch tmp_pass]'
verify='[scratch tmp_pass]'
]
|]
[seti mv_autocreate]
mv_username=[data session username]
mv_password=[scratch tmp_pass]
[/seti]
Auto-created user [seti auto_username][data session username][/seti][scratch auto_username].
[else]
Auto-create of user failed. Reason: [data session failure]
[perl] die errmsg("Auto-create of user failed."); [/perl]
[/else]
[/if]
[/try]
[catch error-set="Customer record creation" error-scratch="mv_route_failed"]
There was an error adding you to the customer table.
[/catch]
[/if]
[if value order_desk_entry]
[try]
[tmpn tmp_compare][/tmpn]
[tmp tmp_username][data table=userdb col=usernick key="[value customer_id]"][/tmp]
[if !scratch tmp_username]
[calcn]
my $pass = '';
my @lets = ('A'..'Z', 'a'..'z', '0'..'9');
$pass .= $lets[ rand @lets ]
while length($pass) < 8;
$Tag->tmpn('tmp_pass', $pass);
return;
[/calcn]
[tmp hide_status][userdb
function=new_account
no-login=1
assign-username=1
password='[scratch tmp_pass]'
verify='[scratch tmp_pass]'
profile=autocreate
][/tmp]
[tmp tmp_username][data session auto_created_user][/tmp]
[if scratch hide_status]
Auto-created user [scratch tmp_username], no login.
[userdb function=save scratch=NONE username="[scratch tmp_username]" hide=1]
[/if]
[/if]
[value name=usernick set="[scratch tmp_username]" hide=1]
[value name=customer_id set="[scratch tmp_username]" hide=1]
[if !value customer_id]
Auto-create of user failed. Reason: [data session failure]
[perl] die errmsg("Auto-create of user failed. Reason: %s", $Session->{failure}) [/perl]
[/if]
[/try]
[catch error-set="Customer record creation" error-scratch="mv_route_failed"]
There was an error adding to the customer table.
[/catch]
[/if]
[comment][perl] Log("Starting report."); [/perl][/comment]
[try]
[if value mv_same_billing]
[loop list="lname fname company address1 address2 city state zip country"]
[tmp tmp_[loop-code]][/tmp]
[/loop]
[else]
[loop list="lname fname company address1 address2 city state zip country phone"]
[tmp tmp_[loop-code]][value name=b_[loop-code] filter="strip mac"][/tmp]
[/loop]
[/else]
[/if]
[if value order_desk_entry]
[tmp tmp_username][value customer_id][/tmp]
[tmp tmp_source][value name=salesperson default=ORDER_DESK][/tmp]
Order desk entry, set next page to: [cgi name=mv_nextpage set="admin/order_view"]
[else]
[tmp tmp_username][data session username][/tmp]
[tmp tmp_source][data session source][/tmp]
[/else]
[/if]
[seti total_cost][if scratch pay_cert_total][scratch tmp_remaining][else][total-cost noformat=1][/else][/if][/seti]
Add main order [value mv_order_number] to transactions:
[import table=transactions type=LINE continue=NOTES no-commit=1]
code: [value mv_order_number]
store_id: __STORE_ID__
order_number: [value mv_order_number]
session: [data session id]
username: [scratch tmp_username]
shipmode: [value mv_shipmode] ([shipping-desc])
shipping: [shipping noformat=1]
nitems: [nitems]
subtotal: [subtotal noformat=1]
handling: [handling noformat=1]
salestax: [salestax noformat=1]
total_cost: [scratch total_cost]
fname: [value filter=strip name=fname]
lname: [value filter=strip name=lname]
company: [value filter=strip name=company]
address1: [value filter=strip name=address1]
address2: [value filter=strip name=address2]
city: [value filter=strip name=city]
state: [value name=state filter=strip]
zip: [value name=zip filter=word]
country: [value country]
email: [value name=email filter=strip]
phone_day: [value filter=strip name=phone_day]
phone_night: [value filter=strip name=phone_night]
b_company: [scratch tmp_company]
b_fname: [scratch tmp_fname]
b_lname: [scratch tmp_lname]
b_address1: [scratch tmp_address1]
b_address2: [scratch tmp_address2]
b_city: [scratch tmp_city]
b_state: [scratch tmp_state]
b_zip: [scratch tmp_zip]
b_country: [scratch tmp_country]
b_phone: [scratch tmp_phone]
payment_method: [value mv_payment]
payment_mode: [data session payment_mode]
avs: [calc] ($Session->{payment_result} || {})->{'pop.avs_code'}; [/calc]
order_id: [data session payment_id]
auth_code: [calc] ($Session->{payment_result} || {})->{'pop.auth-code'}; [/calc]
tracking_number:
order_date: [value name=order_date set="[tag time]%Y%m%d %H:%M:%S[/tag]"]
order_ymd: [value name=order_date set="[tag time]%Y%m%d[/tag]"]
order_wday: [value name=order_wday set="[tag time]%u[/tag]"]
status: pending
deleted: 0
archived: 0
complete: 0
comments: [value filter=mac name=gift_note]
affiliate: [scratch tmp_source]
campaign: [value campaign]
parent: __PARENT__
po_number: [value filter=strip name=po_number]
[if scratch pay_cert_total]pay_cert_total: [scratch pay_cert_total]
pay_cert_ord_total: [total-cost noformat=1][/if]
[/import]
[/try]
[catch error-set="log_orderline_table" error-scratch="mv_route_failed"]
There was an error adding the order to the transaction table. It was: $ERROR$
[/catch]
[goto if="[scratch mv_route_failed]"]
[try]
[if value mv_payment_mode eq purchase_order]
set credit_limit: [seti credit_limit][data
table=userdb
col=credit_limit
key="[data session username]"
value="-[scratch total_cost]"
increment=1
][/seti]
[/if]
[comment]Past transactions entry.[/comment]
[set download_present][/set]
[item-list]Added [item-code] to orderline:
[import table=orderline type=LINE continue=NOTES no-commit=1]
code: [value mv_order_number]-[item-increment]
store_id: __STORE_ID__
order_number: [value mv_order_number]
session: [data session id]
username: [scratch tmp_username]
shipmode: [item-modifier mv_shipmode]
sku: [item-code]
options: [item-filter mac strip][item-options report=1 type=value][/item-filter]
quantity: [item-quantity]
price: [item-discount-price noformat]
subtotal: [item-discount-subtotal noformat]
taxable: [if-item-data !products nontaxable]1[/if-item-data]
mv_mi: [item-modifier mv_mi]
mv_si: [item-modifier mv_si]
mv_mp: [item-modifier mv_mp]
order_date: [value order_date]
affiliate: [scratch tmp_source]
campaign: [value campaign]
status: pending
description: [filter mac][item-description][/filter]
[/import]
[if variable DECREMENT_INVENTORY]Inventory of [item-code] now:
[data
table=inventory
col=quantity
key="[item-code]"
increment=1
value="-[item-quantity]"
]
[/if]
[comment] Handle purchase of payment certificates [/comment]
[if-item-field gift_cert]
Issue payment cert=[pay-cert
issue=1
no-cookie=1
amount="[item-subtotal noformat]"
expires="__GIFT_CERT_PERIOD__"
item-pointer="[item-param mv_ip]"
]
[/if-item-field]
[comment] Handle downloadables [/comment]
[if-item-field download]
[if value mv_payment =~ /credit.*card/i]
[set download_present]1[/set]
[userdb
function=set_file_acl
mode="expire 7 days"
location="[item-code]"
]
[/if]
[/if-item-field]
[/item-list]
[/try]
[catch
error-set="log_orderline_table"
error-scratch="mv_route_failed"
]
There was an error adding the items to the orderline table: $ERROR$
[/catch]
[goto if="[scratch mv_route_failed]"]
[if variable TRANSACTION_ACCOUNTING_SYSTEM]
[try]
Realtime: [scratch realtime]
Add to SQL-Ledger: [accounting function=create_order_entry do_invoice="[scratch do_invoice]" do_payment="[scratch do_payment]"]
[/try]
[catch
error-set="log_accounting_xfer"
error-scratch="mv_route_failed"
]
There was an error transferring accounting information: $ERROR$
[/catch]
[goto if="[scratch mv_route_failed]"]
[/if]
[try]
[tmpn tmp_user_save]NO[/tmpn]
[tmpn tmp_save][/tmpn]
[if value order_desk_entry]
[calc]
## Set this so that we can go to admin/order_view[/comment]
$CGI->{order} = $Values->{mv_order_number};
my @funcs;
#### Commented out until separate functions built
#for(qw/shipping billing/) {
# next if $Values->{"no_save_$_"};
# push @funcs, "set_$_";
# $Scratch->{tmp_user_save} = '';
#}
@funcs = 'save' unless $Values->{no_save};
$Scratch->{tmp_save} = join(" ", @funcs);
my $msg = errmsg("Order %s entered for customer %s",
$Values->{mv_order_number},
$Scratch->{tmp_username},
);
$Tag->warnings($msg);
return;
[/calc]
[seti tmp_user_save]
[loop list="[scratch tmp_save]"]
[loop-code]=[userdb
function="[loop-code]"
profile=default
username="[scratch tmp_username]"
scratch=NONE
] [/loop]
[/seti]
[calc]
$Scratch->{tmp_user_save} =~ s/^\s+//;
$Scratch->{tmp_user_save} =~ s/\s+$//;
$Scratch->{tmp_user_save} =~ s/\s+/ /g;
return;
[/calc]
[elsif type=explicit compare="[userdb save]"]
[tmpn tmp_user_save]SUCCESS[/tmpn]
[/elsif][/if]
Saved user information to user database: [scratch tmp_user_save]
[if scratch auto_create]
Logout auto-created user: [userdb function=logout clear=0 clear_cart=0]
Clear auto-created user's usernick: [calc] delete $Scratch->{usernick}; return 1; [/calc]
[/if]
[/try]
[catch
error-set="log_userdb_table"
error-scratch="mv_route_failed"
]
There was an error updating the user information: $ERROR$
[/catch]
[goto if="[scratch mv_route_failed]"]
[comment]Past add data entry. Capture payment cert if necessary.[/comment]
[if scratch pay_certs_to_capture]
[pay-cert-redeem capture=1]
[/if]
[comment]
Log order to directory suitable for capture by outside process.
[/comment]
[order-logger]
[comment]Past all transaction log.[/comment]
[/tmp][perl]
my $out = $Scratch->{transaction_record};
$out =~ s/^\s+//;
$out =~ s/^\s+//mg;
$out =~ s/\s+$//mg;
$out =~ s/[\r\n]+/\n/;
$out =~ s/:\n(\d+|yes|succe\w+|fail\w+)\n/: $1\n/ig;
return $out;
[/perl]
#### end order logging of tid=[value mv_transaction_id], order_number=[value mv_order_number] #####
|