#### begin [value mv_order_number] #####
[calc]
	if (! $Values->{separate_addresses} ) {
		foreach (@$Items) {
			$_->{mv_ad} = '';
		}
	}
	return;
[/calc]
[tmpn auto_create][/tmpn]
[loop list="transactions orderline inventory userdb"]
[flag type=write table="[loop-code]"]
[/loop]
[tmp transaction_record]
Total order: [total-cost]
Payment processing...payment_method=[value mv_order_profile]
[set do_payment][/set]
[set do_invoice][/set]

[try]

[if value mv_order_profile eq postal]

	[comment]Sales order only[/comment]
	Payment: [value name=mv_payment set="Check or Money Order (will call)"]

[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]
	[set do_invoice]1[/set]
[/elsif]
[elsif value mv_order_profile eq cod]
	[comment] do nothing [/comment]
[/elsif]
[elsif variable MV_PAYMENT_MODE]
	[tmp name="charge_succeed"][charge route="[var MV_PAYMENT_MODE]"][/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]
	[/then]
	[else]
	Real-time charge FAILED. Reason: [data session cybercash_error]
	[calc]
		die errmsg(
				"Real-time charge failed. Reason: %s\n",
				errmsg($Session->{cybercash_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-set="Payment process" error-scratch="mv_route_failed"]
	There was an error accepting payment: $ERROR$
[/catch]

[goto if="[scratch mv_route_failed]"]

[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]
	[if type=explicit compare=|
		[userdb
			function=new_account
				assign_username=1
					password='[value zip]'
						verify='[value zip]'
		]
		|]
		[seti mv_autocreate]
			mv_username=[data session username]
			mv_password=[value zip]
		[/seti]
		Auto-created user [seti auto_username][data session username][/seti][scratch auto_username].
	[else]
	Auto-create of user failed.
	[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=username key="[value customer_id]"][/tmp]
	[if !scratch tmp_username]
		[tmp hide_status][userdb
							function=new_account
							no-login=1
							assign-username=1
							password='[value zip]'
							verify='[value zip]'
		][/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=customer_id set="[scratch tmp_username]" hide=1]
	[if !value customer_id]
	Auto-create of user failed.
	[perl] die errmsg("Auto-create of user failed."); [/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 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][total-cost noformat=1][/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 uc"]
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: [value filter=strip name=b_company]
b_fname: [value filter=strip name=b_fname]
b_lname: [value filter=strip name=b_lname]
b_address1: [value filter="strip mac" name=b_address1]
b_address2: [value filter="strip mac" name=b_address2]
b_city: [value filter=strip name=b_city]
b_state: [value filter=strip name=b_state]
b_zip: [value filter=strip name=b_zip]
b_country: [value filter=strip name=b_country]
b_phone: [value filter=strip name=b_phone]
payment_method: [value mv_payment]
payment_mode: [data session payment_mode]
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] 
[/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]

[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-price noformat]
subtotal: [item-subtotal noformat]
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-item-modifier mv_ad]
	[data
	base=orderline
	field=separate_address
	key="[value mv_order_number]-[item-increment]"
	value=|[item-modifier mv_ad]|
	]
[/if]

[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 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]
[/if]
[/try]

[catch
	error-set="log_userdb_table"
	error-scratch="mv_route_failed"
	]
There was an error updating the user information: $ERROR$
[/catch]

[comment]Past add data entry.[/comment]

[comment]Past all transaction log.[/comment]
[/tmp][perl]
	my $out = $Scratch->{transaction_record};
	$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 [value mv_order_number] #####
