#!/usr/bin/perl

use DBI;

require './jcode.pl';
require './subfunc.cgi';
require './settings.cgi';

#==================================================================
#	設定
#==================================================================
$html_file1 = "kessai.html";
$html_file2 = "kakunin.html";
$mail_file = "../data/maildata.txt";
$mail_file2 = "../data/thanks.txt";
$cost_file = "../data/mailcost.txt";
$daibiki_file = "../data/daibiki.txt";
@kind = ( "", "銀行振込", "代引き", "カード" );

# 現日時の取得
($sec,$min,$hour,$day,$mon,$year,$wday) = localtime(time);
$nowdate = sprintf("%04d%02d%02d",$year+1900,$mon+1,$day);
$nowtime = sprintf("%02d%02d%02d",$hour,$min,$sec);

#==================================================================
#	引数を解析する
#==================================================================
if ($ENV{'REQUEST_METHOD'} eq "POST") {
	read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
} else {
	$buffer = $ENV{'QUERY_STRING'};
}
@pairs = split(/&/,$buffer);
foreach $pair (@pairs)
{
	($name, $value) = split(/=/, $pair);
	$value =~ tr/+/ /;
	$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	$value =~ s/\n//g;
	&jcode'h2z_sjis( *value );
	&jcode'convert(*value,'sjis');
	$PARA{$name} = $value;
}
 
#==================================================================
#	cookieの取得
#==================================================================
$cookie = $ENV{'HTTP_COOKIE'};
@cookie = split(/ /, $cookie);
foreach (@cookie) {
	local($name, $val) = split(/=/);
	$val =~ s/\;//g;
	$cc{$name} = $val;
}
$id = $cc{'CartID'};
if( $id == "" ) { $id = "guest"; }

#==================================================================
#	引数により処理を分岐する
#==================================================================

if( $cc{LoginID} eq "" && $PARA{mode} eq "" ) {
	print "Content-type: text/html\n";
	print "Location: login_now2.html\n\n";
	exit;
}
if( $PARA{mode} eq "send" ) {
	print "Content-type: text/html\n";
	$chumonno = sprintf( "%06d%06d%03d", $nowdate % 1000000, $nowtime, substr( $id, 16 ) );
#	if( $cc{LoginID} ne "" ) { &write_order( $chumonno ); }

	# カートデータの読み込み(HDR)
	open (DATA,"../cart/h_$id.txt");
	flock (DATA, 2);
	@HDR = <DATA>;
	flock (DATA, 8);
	close(DATA);
	
	# カートの中身チェック
	($date,$time,$g_tensu) = split(/,/,$HDR[0]);
	if( $g_tensu == 0 || $g_tensu eq "" ) {
		print "\n";
		&no_item( "t_kounyu_jyouhou.gif" );
		exit;
	}

	&write_order( $chumonno );
	&send_mail;
	print "Set-Cookie: CartID=;\n";
	print "Location: thanks.html\n\n";
	
	exit;
}
if( $PARA{mode} eq "view" ) {
	&SetCookie;
	if( &check_data() == 0 ) {
		&view_data;
		exit;
	}
}
&input_data;
exit;

#==================================================================
#	顧客情報等の入力画面
#==================================================================

sub input_data {

	# HTMLファイルの読み込み
	open(HTML,"$html_file1") || die "error!\n";
	flock(HTML, 2);
	@HTML = <HTML>;
	flock(HTML, 8);
	close(HTML);

	# カートデータの読み込み(HDR)
	open (DATA,"../cart/h_$id.txt");
	flock (DATA, 2);
	@HDR = <DATA>;
	flock (DATA, 8);
	close(DATA);

	# カートデータの読み込み(BDY)
	open (DATA,"../cart/b_$id.txt");
	flock (DATA, 2);
	@BDY = <DATA>;
	flock (DATA, 8);
	close(DATA);

	open(HTML2,"../head.html") || die "error!\n";
	flock (HTML2, 2);
	@HEAD_HTML = <HTML2>;
	flock (DATA2, 8);
	close(HTML2);

	print "Content-type: text/html\n\n";

	($date,$time,$g_tensu) = split(/,/,$HDR[0]);
	if( $g_tensu == 0 || $g_tensu eq "" ) {
		&no_item( "t_kounyu_jyouhou.gif" );
		return;
	}

	$dbh = DBI->connect($data_source,$user_name,$user_pass) || die print "CONNECT ERROR $DBI::errstr";
	
	$alltodoke = 0;
	$select = 0;
	$getkaiin = 1;
	foreach $line (@HTML) {
		if( index( $line, '#INCLUDE_HEAD#' ) >= 0 ) {
			foreach $line2 (@HEAD_HTML) {
				print $line2;
			}
			$line = "<!-- INCLUDE OK -->\n";
		}
		if( index( $line, "#ORDER#" ) >= 0 ) {
			&order_view( 0 );
			$getkaiin = 0;
			next;
		}
		if( index( $line, "#HENKO#" ) >= 0 ) {
			if( $cc{LoginID} ne "" ) {
				$line = "<TR><TD align=right colspan=8><b><font color=red>お届先の変更は,「カートを見る」→「カートの中の確認」→お届け先の変更にて行えます。</font></b></TD></TR>\n";
			} else {
				next;
			}
		}
		$line =~ s/#MESSAGE#/$message/g;
		if( $PARA{mode} eq "view" ) {
			$line =~ s/#YUBIN#/$PARA{yubin}/g;
			if( $select == 0 ) {
				if( $PARA{adrs0} ne "" ) {
					$line =~ s/\"$PARA{adrs0}\"/\"$PARA{adrs0}\" selected/g;
				}
			} 
			if( ( $select == 1 && $cc{LoginID} ne "" ) || ( $select == 2 && $cc{LoginID} eq "" ) ) {
				$line =~ s/\"$PARA{cardco}\"/\"$PARA{cardco}\" selected/g;
			}
			if( $select == 1 && $cc{LoginID} eq "" ) {
				$line =~ s/\"$PARA{d_adrs0}\"/\"$PARA{d_adrs0}\" selected/g;
			}
			if( $cc{LoginID} eq "" ) {
				$line =~ s/<!--#TODOKESAKI#//g;
				$line =~ s/#TODOKESAKI#-->//g;
			}
			$line =~ s/#ADRS1#/$PARA{adrs1}/g;
			$line =~ s/#ADRS2#/$PARA{adrs2}/g;
			$line =~ s/#NAME#/$PARA{name}/g;
			$line =~ s/#FURIGANA#/$PARA{furigana}/g;
#			$line =~ s/#NENREI#/$PARA{nenrei}/g;
			$line =~ s/#TEL#/$PARA{tel}/g;
			$line =~ s/#EMAIL#/$PARA{email}/g;
			$line =~ s/#EMAIL2#/$PARA{email2}/g;
		} else {
			if( $cc{LoginID} ne "" ) {
				if( $getkaiin == 0 ) {
					# LOGIN済みなら会員データを取得
					$sql = "SELECT * FROM customer WHERE Id=?;";
					$sth = $dbh->prepare($sql);
					$sth->execute( "$cc{LoginID}" ) || die print $sth->errstr;
					@value = $sth->fetchrow_array;
					$sth->finish;
					$getkaiin = 1;
#					$nenrei = substr( $nowdate, 0, 4 );
#					$nenrei = $nenrei - substr( $value[14], 0, 4 );
#					if( substr( $nowdate, 4, 4 ) < substr( $value[14], 4, 4 ) ) { $nenrei--; }
				}
				$line =~ s/#YUBIN#/$value[2]/g;
				if( $select == 0 ) {
					$line =~ s/\"$value[3]\"/\"$value[3]\" selected/g;
				}
				$line =~ s/#ADRS1#/$value[4]/g;
				$line =~ s/#ADRS2#/$value[5]/g;
				$line =~ s/#NAME#/$value[6]/g;
				$line =~ s/#FURIGANA#/$value[7]/g;
#				$line =~ s/#NENREI#/$nenrei/g;
				$line =~ s/#TEL#/$value[8]/g;
				$line =~ s/#EMAIL#/$value[9]/g;
				$line =~ s/#EMAIL2#/$value[9]/g;
			} else {
				$line =~ s/#YUBIN#/$cc{yubin}/g;
				if( $select == 0 && $cc{adrs0} ne "" ) {
					$line =~ s/\"$cc{adrs0}\"/\"$cc{adrs0}\" selected/g;
				}
				$line =~ s/#ADRS1#/$cc{adrs1}/g;
				$line =~ s/#ADRS2#/$cc{adrs2}/g;
				$line =~ s/#NAME#/$cc{name}/g;
				$line =~ s/#FURIGANA#/$cc{furigana}/g;
#				$line =~ s/#NENREI#/$cc{nenrei}/g;
				$line =~ s/#TEL#/$cc{tel}/g;
				$line =~ s/#EMAIL#/$cc{email}/g;
				$line =~ s/#EMAIL2#//g;
			}
		}
		if( index( $line, "/SELECT" ) >= 0 ) { $select++; }
		if( $cc{LoginID} eq "" || $alltodoke == 0 ) {
			$line =~ s/<!--#TODOKESAKI#//g;
			$line =~ s/#TODOKESAKI#-->//g;
		}
		$line =~ s/#BIKO#/$PARA{biko}/g;
		$line =~ s/#D_YUBIN#/$PARA{d_yubin}/g;
		$line =~ s/#D_ADRS1#/$PARA{d_adrs1}/g;
		$line =~ s/#D_ADRS2#/$PARA{d_adrs2}/g;
		$line =~ s/#D_NAME#/$PARA{d_name}/g;
#		$line =~ s/#D_NENREI#/$PARA{d_nenrei}/g;
		$line =~ s/#D_TEL#/$PARA{d_tel}/g;
		$line =~ s/#CARDCO#/$PARA{cardco}/g;
		$line =~ s/#CARDNO1#/$PARA{cardno1}/g;
		$line =~ s/#CARDNO2#/$PARA{cardno2}/g;
		$line =~ s/#CARDNO3#/$PARA{cardno3}/g;
		$line =~ s/#CARDNO4#/$PARA{cardno4}/g;
		$line =~ s/#KIGEN1#/$PARA{kigen1}/g;
		$line =~ s/#KIGEN2#/$PARA{kigen2}/g;
		$line =~ s/#MEIGI#/$PARA{meigi}/g;
		$line =~ s/#AEONID#/$PARA{aeonid}/g;
		if( $PARA{paykind} eq "" ) {
			$PARA{paykind} = 1;
			if( $cc{pay} eq "1" || $cc{pay} eq "2" || $cc{pay} eq "3" ) {
				$PARA{paykind} = $cc{pay};
			}
		}
		if( index( $line, "paykind" ) >= 0 ) {
			$line =~ s/value=$PARA{paykind}/value=$PARA{paykind} checked/g;
		}
		if( index( $line, "google.html" ) >= 0 ) { &google_analytics; next; }
		print $line;
	}
}

#==================================================================
#	入力データのチェック
#==================================================================

sub check_data {
	$message = "";
	if( $PARA{email} eq "" ) {
		$message = "メールアドレスが未入力です。";
		return 1;
	}
	if( $PARA{email2} eq "" ) {
		$message = "メールアドレス（確認）が未入力です。";
		return 1;
	}
	if( $PARA{email} ne $PARA{email2} ) {
		$message = "メールアドレスが一致しません。";
		return 1;
	}
	if( $PARA{adrs0} eq "" ) {
		$message = "都道府県を選択してください。";
		return 1;
	}
	if( $PARA{yubin} eq "" ) {
		$message = "必須項目が入力されていません。（郵便番号）";
		return 10;
	}
	if( length( $PARA{yubin} ) != 7 || $PARA{yubin} =~ /[^0-9]/ ) {
		$message = "郵便番号が正しくありません。（数字7桁）";
		return 10;
	}
	if( $PARA{adrs1} eq "" ) {
		$message = "必須項目が入力されていません。（住所）";
		return 11;
	}
	if( $PARA{name} eq "" ) {
		$message = "必須項目が入力されていません。（氏名）";
		return 12;
	}
	if( $PARA{furigana} eq "" ) {
		$message = "必須項目が入力されていません。（フリガナ）";
		return 12;
	}
#	if( $PARA{nenrei} eq "" ) {
#		$message = "必須項目が入力されていません。（年齢）";
#		return 12;
#	}
#	if( $PARA{d_name} ne "" ) {
#		if( $PARA{d_nenrei} < 20 ) {
#			$message = "未成年の方へのお届けはできません。";
#'			return 12;
#		}
#	} else {
#		if( $PARA{nenrei} < 20 ) {
#			$message = "未成年の方への酒類の販売は禁止されています。";
#			return 12;
#		}
#	}
	if( $PARA{tel} eq "" ) {
		$message = "必須項目が入力されていません。（電話番号）";
		return 13;
	}
	if( $PARA{tel} =~ /[^0-9|-]/ ) {
		$message = "半角で入力して下さい。（電話番号）";
		return 13;
	}
	if( $PARA{email} eq "" ) {
		$message = "必須項目が入力されていません。（メールアドレス）";
		return 14;
	}
	if ( $PARA{email} =~ /^[^@]+@[^.]+\..+/ ) {
	} else {
		$message = "メールアドレスが正しくありません。";
		return 14;
	}
	if( $PARA{paykind} ne 3 && $PARA{cardno1} ne "" ) {
		$message = "カードを選択されていませんがカード情報が入力されています。";
		return 15;
	}
	if( $PARA{paykind} == 3 && $PARA{cardco} eq "" ) {
		$message = "必須項目が入力されていません。（カード会社）";
		return 15;
	}
	if( $PARA{paykind} == 3 && ( length( $PARA{cardno1} ) != 4 || $PARA{cardno1} =~ /[^0-9]/ ) ) {
		$message = "入力項目に誤りがあります。（カード番号1）";
		return 15;
	}
	if( $PARA{cardco} eq "AMEX" ) {
		if( $PARA{paykind} == 3 && ( length( $PARA{cardno2} ) != 3 || $PARA{cardno2} =~ /[^0-9]/ ) ) {
			$message = "入力項目に誤りがあります。（カード番号2）";
			return 15;
		}
	} else {
		if( $PARA{paykind} == 3 && ( length( $PARA{cardno2} ) != 4 || $PARA{cardno2} =~ /[^0-9]/ ) ) {
			$message = "入力項目に誤りがあります。（カード番号2）";
			return 15;
		}
	}
	if( $PARA{cardco} eq "AMEX" ) {
		if( $PARA{paykind} == 3 && ( length( $PARA{cardno3} ) != 3 || $PARA{cardno3} =~ /[^0-9]/ ) ) {
			$message = "入力項目に誤りがあります。（カード番号3）";
			return 15;
		}
	} else {
		if( $PARA{paykind} == 3 && ( length( $PARA{cardno3} ) != 4 || $PARA{cardno3} =~ /[^0-9]/ ) ) {
			$message = "入力項目に誤りがあります。（カード番号3）";
			return 15;
		}
	}
	if( $PARA{cardco} eq "AMEX" ) {
		if( $PARA{paykind} == 3 && ( length( $PARA{cardno4} ) != 5 || $PARA{cardno4} =~ /[^0-9]/ ) ) {
			$message = "入力項目に誤りがあります。（カード番号4）";
			return 15;
		}
	} else {
		if( $PARA{paykind} == 3 && ( length( $PARA{cardno4} ) != 4 || $PARA{cardno4} =~ /[^0-9]/ ) ) {
			$message = "入力項目に誤りがあります。（カード番号4）";
			return 15;
		}
	}
	if( $PARA{paykind} == 3 && ( $PARA{kigen1} eq "" || $PARA{kigen2} eq "" ) ) {
		$message = "入力項目に誤りがあります。（カード有効期限）";
		return 16;
	}
	if( $PARA{paykind} == 3 && ( length( $PARA{kigen1} ) != 2 || length( $PARA{kigen2} ) != 2 ) ) {
		$message = "入力項目に誤りがあります。（カード有効期限）";
		return 16;
	}
	if( $PARA{paykind} == 3 && ( $PARA{kigen1} =~ /[^0-9]/ || $PARA{kigen2} =~ /[^0-9]/ ) ) {
		$message = "入力項目に誤りがあります。（カード有効期限）";
		return 16;
	}
	if( $PARA{paykind} == 3 && $PARA{meigi} eq "" ) {
		$message = "入力項目に誤りがあります。（カード名義）";
		return 17;
	}
	if( $PARA{paykind} == 3 && $PARA{cardco} eq "AEONREGI" && $PARA{aeonid} eq "" ) {
		$message = "必須項目が入力されていません。（AEONネットメンバーＩＤ）";
		return 17;
	}
	if( $PARA{d_yubin} ne "" || $PARA{d_adrs0} ne "" || $PARA{d_name} ne "" ) {
		if( $PARA{d_yubin} eq "" ) {
			$message = "未入力項目があります。（お届先郵便番号）";
			return 10;
		}
		if( length( $PARA{d_yubin} ) != 7 || $PARA{d_yubin} =~ /[^0-9]/ ) {
			$message = "お届先郵便番号が正しくありません。（数字7桁）";
			return 10;
		}
		if( $PARA{d_adrs0} eq "" || $PARA{d_adrs1} eq "" ) {
			$message = "未入力項目があります。（お届先住所）";
			return 11;
		}
		if( $PARA{d_name} eq "" ) {
			$message = "未入力項目があります。（お届先氏名）";
			return 12;
		}
		if( $PARA{d_tel} eq "" ) {
			$message = "未入力項目があります。（お届先電話番号）";
			return 13;	
		}
		if( $PARA{d_tel} !~ /^[0-9]([0-9]|-)+$/ ) {
			$message = "半角数字とハイフンで入力して下さい。（お届先電話番号）";
			return 13;	
		}
	}
	return 0;
}

#==================================================================
#	入力データの確認画面
#==================================================================

sub view_data {

	# HTMLファイルの読み込み
	open(HTML,"$html_file2") || die "error!\n";
	flock(HTML, 2);
	@HTML = <HTML>;
	flock(HTML, 8);
	close(HTML);

	# カートデータの読み込み(HDR)
	open (DATA,"../cart/h_$id.txt");
	flock (DATA, 2);
	@HDR = <DATA>;
	flock (DATA, 8);
	close(DATA);

	# カートデータの読み込み(BDY)
	open (DATA,"../cart/b_$id.txt");
	flock (DATA, 2);
	@BDY = <DATA>;
	flock (DATA, 8);
	close(DATA);
	
	open(HTML2,"../head.html") || die "error!\n";
	flock (HTML2, 2);
	@HEAD_HTML = <HTML2>;
	flock (DATA2, 8);
	close(HTML2);
	
	$dbh = DBI->connect($data_source,$user_name,$user_pass) || die print "CONNECT ERROR $DBI::ERRSTR";

	$cardno = $PARA{cardno1} . " " . $PARA{cardno2} . " " . $PARA{cardno3} . " " . $PARA{cardno4};

	print "Content-type: text/html\n\n";
	foreach $line (@HTML) {
		if( index( $line, '#INCLUDE_HEAD#' ) >= 0 ) {
			foreach $line2 (@HEAD_HTML) {
				print $line2;
			}
			$line = "<!-- INCLUDE OK -->\n";
		}
		if( index( $line, "#ORDER#" ) >= 0 ) {
			&order_view( 1 );
			if( $daibiki eq 'エラー' ) {
				print "<tr><th colspan='8'><font color='red'>代引き可能\な金額をオーバーしています！（最大$daibiki_max円）</font></th></tr></table></body></html>";
				$dbh->disconnect;
				exit;
			}
			next;
		}
		if( $cc{LoginID} eq "" || $PARA{d_name} ne "" ) {
			$line =~ s/<!--#TODOKESAKI#//g;
			$line =~ s/#TODOKESAKI#-->//g;
		}
		$line =~ s/#YUBIN#/$PARA{yubin}/g;
		$line =~ s/#ADRS0#/$PARA{adrs0}/g;
		$line =~ s/#ADRS1#/$PARA{adrs1}/g;
		$line =~ s/#ADRS2#/$PARA{adrs2}/g;
		$line =~ s/#NAME#/$PARA{name}/g;
		$line =~ s/#FURIGANA#/$PARA{furigana}/g;
#		$line =~ s/#NENREI#/$PARA{nenrei}/g;
		$line =~ s/#TEL#/$PARA{tel}/g;
		$line =~ s/#EMAIL#/$PARA{email}/g;
		$line =~ s/#BIKO#/$PARA{biko}/g;

		$line =~ s/#D_YUBIN#/$PARA{d_yubin}/g;
		$line =~ s/#D_ADRS0#/$PARA{d_adrs0}/g;
		$line =~ s/#D_ADRS1#/$PARA{d_adrs1}/g;
		$line =~ s/#D_ADRS2#/$PARA{d_adrs2}/g;
		$line =~ s/#D_NAME#/$PARA{d_name}/g;
#		$line =~ s/#D_NENREI#/$PARA{d_nenrei}/g;
		$line =~ s/#D_TEL#/$PARA{d_tel}/g;

		$line =~ s/#CARDCO#/$PARA{cardco}/g;
		$line =~ s/#CARDNO#/$cardno/g;
		$line =~ s/#KIGEN1#/$PARA{kigen1}/g;
		$line =~ s/#KIGEN2#/$PARA{kigen2}/g;
		$line =~ s/#MEIGI#/$PARA{meigi}/g;
		$line =~ s/#AEONID#/$PARA{aeonid}/g;
		$line =~ s/#PAYKIND#/$kind[$PARA{paykind}]/g;
		if( index( $line, "google.html" ) >= 0 ) { &google_analytics; next; }
		print $line;
	}
	$dbh->disconnect;
}

#==================================================================
#	入力内容をメールで送信
#==================================================================

sub send_mail {
	
	$ENV{'TZ'} = "JST-9"; 
	@DATE = localtime(time);
	$DATE[5] = $DATE[5] + 1900;
	$DATE[4] = sprintf("%02d",$DATE[4] + 1);
	$DATE[3] = sprintf("%02d",$DATE[3]);
	$DATE[2] = sprintf("%02d",$DATE[2]);
	$DATE[1] = sprintf("%02d",$DATE[1]);
	$DATE[6] = ('日','月','火','水','木','金','土') [$DATE[6]];
	$date_now = "$DATE[5]年$DATE[4]月$DATE[3]日";
	$time_now = "$DATE[2]時$DATE[1]分";

	# 送信メール定義ファイルの読み込み
	open(MDATA,"$mail_file") || die "error!\n";
	flock(MDATA, 2);
	@MDATA = <MDATA>;
	flock(MDATA, 8);
	close(MDATA);

	# カートデータの読み込み(HDR)
	open (DATA,"../cart/h_$id.txt");
	flock (DATA, 2);
	@HDR = <DATA>;
	flock (DATA, 8);
	close(DATA);

	# カートデータの読み込み(BDY)
	open (DATA,"../cart/b_$id.txt");
	flock (DATA, 2);
	@BDY = <DATA>;
	flock (DATA, 8);
	close(DATA);

	# ボディー部分の定型文を取得
	@MBODY = ();
	$flag = 0;
	foreach $line (@MDATA) {
		if( index( $line, "#BODY_END#" ) >= 0 ) { last; }
		if( $flag == 1 ) {
			push( @MBODY, $line );
		}
		if( index( $line, "#BODY_START#" ) >= 0 ) { $flag = 1; next; }
	}
	
	# 動的データを置き換える
	$dbh = DBI->connect($data_source,$user_name,$user_pass) || die print "CONNECT ERROR $DBI::ERRSTR";
	&calc_soryo( 1 );

	@END = ();
	$pos = 1;
	$haisosu = @haisoryo;
	foreach $body_line (@BDY) {		# 購入明細の数だけチェック
		@MESSAGE = ();
		$flag = 0;
		$shokei = 0;
		($su,$komitanka,$komikingaku,$nukitanka,$zei,$todokesaki,$noshi ) = split(/,/,$body_line);
		
		# すでに処理済みの届け先か？
		$endflg = 0;
		foreach $yet (@END) {
			if( $yet == $todokesaki ) { $endflg = 1; last; }
		}
		if( $endflg == 1 ) { next; }
		push( @END, $todokesaki );
		$konkai_haiso = $todokesaki;

		# 注文Noを組み立て
		$chumonno = sprintf( "%06d%06d%03d%02d%02d", $nowdate % 1000000, $nowtime, substr( $id, 16 ), $pos, $haisosu );
		open(CHUMON, ">../order/$chumonno.txt") || die print "ERROR ../data/$chumonno.txt";
		$pos++;

		# 届け先を読み込む
		if( $todokesaki != 0 && $todokesaki ne "" ) {
			$sql = "SELECT * FROM receiver WHERE SeqNo=?;";
			$sth = $dbh->prepare($sql);
			$sth->execute( $todokesaki ) || die print $sth->errstr;
			@value = $sth->fetchrow_array;
			$sth->finish;
		}
		
		# のし紙情報を作成
		$biko2 = "\nのし紙情報";
		foreach $data_line (@BDY) {
			($su,$komitanka,$komikingaku,$nukitanka,$zei,$todokesaki,$noshi,$floor,$corner,$scode ) = split(/,/,$data_line);
			if( $su == 0 ) { next; }
			if( $todokesaki != $konkai_haiso ) { next; }
			$biko2 = $biko2 . "\n$scode：$noshi";
		}
		
		$tokubetu_soryo = 0;
		$coolend = 0;
		foreach $line (@MDATA) {
			if( $flag == 1 ) {
				foreach $data_line (@BDY) {
					($su,$komitanka,$komikingaku,$nukitanka,$zei,$todokesaki,$noshi,
						$floor,$corner,$scode,$name,$imgs,$imgl,$maker,$cmnts,$cmntm,$cmntl,
						$yoryo,$sanchi,$genryo,$hyojun,$tujo,$baika,$soryo,
						$sonta1,$sonta2,$sonta3,$sonta4,$sonta5,$sonta6,$sonta7,$sonta8,$sonta9,$sonta10,$CR)
						= split(/,/,$data_line);
					if( $su == 0 ) { next; }
					if( $todokesaki != $konkai_haiso ) { next; }
					$shokei += $komikingaku;
					$baika = &put_comma( $komitanka );
					$end_tokubetu = 0;
					foreach $mail_line (@MBODY) {
						$temp = $mail_line;
						if( index( $temp, "#D_" ) >= 0 && ( $todokesaki == 0 || $todokesaki eq "" ) ) { next; }
						$temp =~ s/#CODE#/$scode/g;
						$temp =~ s/#SHOHIN#/$name/g;
						$temp =~ s/#SURYO#/$su/g;
						$temp =~ s/#BAIKA#/$baika/g;
						push( @MESSAGE, $temp );
						if( $soryo >= 100 && $soryo != 200 && $coolend == 0 && $end_tokubetu == 0 ) {
							if( $soryo == 200 ) {
								$tokubetu_soryo += $soryo;
								$coolend = 1;
							} else {
								$tokubetu_soryo += $soryo * $su;
							}
							$end_tokubetu = 1;
						}
					}
				}
				$flag = 2;
				$daibiki = 0;
				if( $PARA{paykind} eq "代引き" ) { $daibiki = &get_daibiki( $shokei + $mailcost{$konkai_haiso} + $tokubetu_soryo ); }
				$gokei = $shokei + $mailcost{$konkai_haiso} + $daibiki + $tokubetu_soryo;
				$shokei = &put_comma( $shokei );
				$soryo = &put_comma( $mailcost{$konkai_haiso} + $tokubetu_soryo );
				$gokei = &put_comma( $gokei );
				next;
			}
			if( index( $line, "#BODY_START#" ) >= 0 ) { $flag = 1; next; }
			if( index( $line, "#BODY_END#" ) >= 0 ) { $flag = 0; next; }
			if( $flag == 2 ) { next; }
	
			$line2 = $line;
			$yubin = sprintf( "%03d-%04d", $PARA{yubin} / 10000, $PARA{yubin} % 10000 );
			$d_yubin = sprintf( "%03d-%04d", $PARA{d_yubin} / 10000, $PARA{d_yubin} % 10000 );
			$line2 =~ s/#YUBIN#/$yubin/g;
			$line2 =~ s/#ADRS0#/$PARA{adrs0}/g;
			$line2 =~ s/#ADRS1#/$PARA{adrs1}/g;
			$line2 =~ s/#ADRS2#/$PARA{adrs2}/g;
			$line2 =~ s/#NAME#/$PARA{name}/g;
			$line2 =~ s/#FURIGANA#/$PARA{furigana}/g;
			$line2 =~ s/#TEL#/$PARA{tel}/g;
			$line2 =~ s/#EMAIL#/$PARA{email}/g;
			$line2 =~ s/#BIKO#/$PARA{biko}$biko2/g;
	
			if( $konkai_haiso == 0 ) {	# 会員用届け先指定ナシ
				if( $PARA{d_name} eq "" ) {	# 非会員用届け先入力ナシ
					$line2 =~ s/#D_YUBIN#/$yubin/g;
					$line2 =~ s/#D_ADRS0#/$PARA{adrs0}/g;
					$line2 =~ s/#D_ADRS1#/$PARA{adrs1}/g;
					$line2 =~ s/#D_ADRS2#/$PARA{adrs2}/g;
					$line2 =~ s/#D_NAME#/$PARA{name}/g;
					$line2 =~ s/#D_TEL#/$PARA{tel}/g;
				} else {					# 非会員用届け先アリ
					$line2 =~ s/#D_YUBIN#/$d_yubin/g;
					$line2 =~ s/#D_ADRS0#/$PARA{d_adrs0}/g;
					$line2 =~ s/#D_ADRS1#/$PARA{d_adrs1}/g;
					$line2 =~ s/#D_ADRS2#/$PARA{d_adrs2}/g;
					$line2 =~ s/#D_NAME#/$PARA{d_name}/g;
					$line2 =~ s/#D_TEL#/$PARA{d_tel}/g;
				}
			} else {
				$yubin = sprintf( "%03d-%04d", $PARA{yubin} / 10000, $PARA{yubin} % 10000 );
				$line2 =~ s/#D_YUBIN#/$yubin/g;
				$line2 =~ s/#D_ADRS0#/$value[3]/g;
				$line2 =~ s/#D_ADRS1#/$value[4]/g;
				$line2 =~ s/#D_ADRS2#/$value[5]/g;
				$line2 =~ s/#D_NAME#/$value[6]/g;
				$line2 =~ s/#D_TEL#/$value[7]/g;
			}

			$line2 =~ s/#CARDNO#/$PARA{cardno}/g;
			$line2 =~ s/#KIGEN1#/$PARA{kigen1}/g;
			$line2 =~ s/#KIGEN2#/$PARA{kigen2}/g;
			$line2 =~ s/#MEIGI#/$PARA{meigi}/g;
			$line2 =~ s/#PAYKIND#/$PARA{paykind}/g;
	
			$line2 =~ s/#SHOKEI#/$shokei/g;
			$line2 =~ s/#SORYO#/$soryo/g;
			$line2 =~ s/#GOKEI#/$gokei/g;
			if( $daibiki == 0 ) {
				if( index( $line2, "#DAIBIKI#" ) >= 0 ) { next; }
			} else {
				$line2 =~ s/#DAIBIKI#/$daibiki/g;
			}

			$line2 =~ s/#CHUMONNO#/$chumonno/g;
			$line2 =~ s/#USERID#/$cc{LoginID}/g;
			$line2 =~ s/#SYSDATE#/$date_now/g;
			$line2 =~ s/#SYSTIME#/$time_now/g;
			$line2 =~ s/#STORE_NO#/$cc{RefStore}/g;
			push( @MESSAGE, $line2 );
		}

        $ENV{'TZ'} = "JST-9";
        ($sec,$min,$hour,$mday,$mon,$year,$wday) = localtime(time);
        # 日時のフォーマット
        @week = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
        @mname = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
        $date = sprintf("%s, %d %s %04d %02d:%02d:%02d +0900",
                      $week[$wday],$mday,$mname[$mon],$year+1900,$hour,$min,$sec);

		# メールを送信する
		$title = $MESSAGE[0];
		&jcode'convert(*title,"jis");
		open(MAIL, "|$sendmail -t -i -f '$myaddress'");
		print MAIL "Date: $date\n";
		print MAIL "From: $mail_from <$myaddress>\n";
		print MAIL "To: $sendto\n";
		print MAIL "Reply-To: $myaddress\n";
		print MAIL "MIME-Version: 1.0\n";
		print MAIL "Content-type: text/plain; charset=ISO-2022-JP\n";
		print MAIL "Content-Transfer-Encoding: 7bit\n";
		print MAIL "Subject: $title\n\n";
		foreach $line(@MESSAGE){
			print CHUMON "$line";
			&jcode'convert(*line,"jis");
			print MAIL "$line";
		}
		close(MAIL);
#-------------------------- test -----------------------------
#$sendto2 = 'morimoto@fantec.co.jp';
#		open(MAIL, "|$sendmail -t -i -f '$myaddress'");
#		print(MAIL "From: $mail_from <$myaddress>\n");
#		print(MAIL "To: $sendto2\n");
#		print(MAIL "Reply-To: $myaddress\n");
#		print MAIL "MIME-Version: 1.0\n";
#		print MAIL "Content-type: text/plain; charset=ISO-2022-JP\n";
#		print MAIL "Content-Transfer-Encoding: 7bit\n";
#		print MAIL "Subject: $title\n\n";
#		foreach $line(@MESSAGE){
#			print MAIL "$line";
#		}
#		close(MAIL);
#-------------------------- test -----------------------------
		close(CHUMON);
	}	
	
	$dbh->disconnect;

	# カートファイルを削除する
	unlink( "../cart/h_$id.txt" );
	unlink( "../cart/b_$id.txt" );

	# ありがとうメール内容ファイルの読み込み
	open(MDATA,"$mail_file2") || die "error!\n";
	flock(MDATA, 2);
	@MDATA = <MDATA>;
	flock(MDATA, 8);
	close(MDATA);

	# 発注者あてに確認メールを送る
	$title = "FANTEC：ご注文受付";
	&jcode'convert(*title,"jis");
	open(MAIL, "|$sendmail -t -i -f '$myaddress'");
 	print MAIL "From: $mail_from <$myaddress>\n";
	print MAIL "To: $PARA{email}\n";
	print MAIL "Reply-To: $myaddress\n";
	print MAIL "MIME-Version: 1.0\n";
	print MAIL "Content-type: text/plain; charset=ISO-2022-JP\n";
	print MAIL "Content-Transfer-Encoding: 7bit\n";
	print MAIL "Subject: $title\n\n";
	$cno = "ご注文番号[$chumonno]\n\n";
	&jcode'convert(*cno,"jis");
	print MAIL $cno;
	foreach $line(@MDATA){
		&jcode'convert(*line,"jis");
		print MAIL "$line";
	}
	close(MAIL);
}

#==================================================================
#	オーダー情報の表示
#==================================================================

sub order_view {
	($mode) = @_;

	@cool = ();
	$mno = 1;
	foreach $data_line (@BDY) {
		($su,$komitanka,$komikingaku,$nukitanka,$zei,$todokesaki,$noshi,
			$floor,$corner,$scode,$name,$imgs,$imgl,$maker,$cmnts,$cmntm,$cmntl,
			$yoryo,$sanchi,$genryo,$hyojun,$tujo,$baika,$soryo,
			$sonta1,$sonta2,$sonta3,$sonta4,$sonta5,$sonta6,$sonta7,$sonta8,$sonta9,$sonta10,$CR)
			= split(/,/,$data_line);
		if( $su == 0 ) { next; }
		print "<TR bgcolor=\"FFF0FF\">";
		print "$nokm";
				
		$komitanka = &put_comma( $komitanka );
		$komikingaku = &put_comma( $komikingaku );
		if( $soryo < 100 ) { $soryo = ""; }
		if( $soryo ne "" ) {
			if( $soryo == $cool_daikin ) {
				if( $cool[$todokesaki] != 1 ) {
					$soryo = "\\" . &put_comma( $soryo );
				} else {
					$soryo = "\\" . &put_comma( 0 );
				}
				$cool[$todokesaki] = 1;
			} else {
				$soryo = "\\" . &put_comma( $soryo * $su );
			}
		}

		$meisai = sprintf( "<TD align=right>%s</TD><TD>%s</TD><TD>%s</TD><TD align=right>　\\%s</TD><TD align=right>　%s</TD><TD align=right>　\\%s</TD><TD align=right>　%s</TD>",
			$mno, $scode, $name, $komitanka, $su, $komikingaku, $soryo );
		print "$meisai";
				
		# お届先の取得
		if( $todokesaki != 0 ) {
			$alltodoke = 1;
			$sql = "SELECT * FROM receiver WHERE SeqNo=?;";
			$sth = $dbh->prepare($sql);
			$sth->execute( $todokesaki ) || die print $sth->errstr;
			@value = $sth->fetchrow_array;
			$sth->finish;
			print "<td>$value[6] 様</td>";
		} else {
			if( $cc{LoginID} ne "" ) {
				print "<td>ご本人 様</td>";
			} else {
				print "<td>　</td>";
			}
		}

		print "</TR>\n";
		$mno += 1;
	}
	# <<<<< 合計表示 >>>>>
	foreach $hdr_line (@HDR) {
		($date,$time,$g_tensu,$g_kingaku,$g_hontai,$g_zei,$g_soryo1,$g_soryo2,$CR)
			= split(/,/,$hdr_line);

		$daibiki = 0;
		$g_soryo2 = &calc_soryo( 0 );
		if( $PARA{paykind} == 2 ) { $daibiki = &get_daibiki( $g_kingaku + $g_soryo1 + $g_soryo2 ); }
		$g_total = &put_comma( $g_kingaku + $g_soryo1 + $g_soryo2 + $daibiki );
		$g_kingaku = &put_comma( $g_kingaku );
		$g_soryo2 = &put_comma( $g_soryo2 );
		$g_soryo1 = &put_comma( $g_soryo1 );
		if( $daibiki == -1 ) { $daibiki = "エラー"; } else { $daibiki = &put_comma( $daibiki ); }

		$gl_shokei = sprintf( "<TR bgcolor=#ffffff><TD>　</TD><TD>　</TD><TD>　</TD><TH bgcolor=\"#FFDDFF\">小　計</TH><TD align=right>　%s</TD><TD align=right>　\\%s</TD><TD align=right>　</TD><TD>　</TD></TR>\n",
			$g_tensu,$g_kingaku );
		print "$gl_shokei";
		if( $mode == 1 ) {
			$gl_soryo1 = sprintf( "<TR bgcolor=#ffffff><TD>　</TD><TD>　</TD><TD>　</TD><TH bgcolor=#FFDDFF>送　料</TH><TD>　</TD><TD align=right><font color=red>(注)</font>　\\%s</TD><TD align=right>　\\%s</TD><TD>　</TD></TR>\n", $g_soryo2, $g_soryo1 );
			print "$gl_soryo1";
			if( $PARA{paykind} == 2 ) {
				$gl_daibiki = sprintf( "<TR bgcolor=#ffffff><TD>　</TD><TD>　</TD><TD>　</TD><TH bgcolor=#FFDDFF>代引き手数料</TH><TD>　</TD><TD align=right><font color=red>(注)</font>　\\%s</TD><TD>　</TD><TD>　</TD></TR>\n", $daibiki );
				print "$gl_daibiki";
			}
#			$gl_soryo2 = sprintf( "<TR bgcolor=#ffffff><TD>　</TD><TD>　</TD><TD>　</TD><TH bgcolor=#ccccff>送料2</TH><TD>　</TD><TD align=right>　\\%s</TD><TD>　</TD><TD>*2</TD></TR>\n", $g_soryo2 );
#			print "$gl_soryo2";
			$gl_gokei = sprintf( "<TR bgcolor=#ffffff><TD>　</TD><TD>　</TD><TD>　</TD><TH bgcolor=#FFDDFF>合　計</TH><TD>　</TD><TD align=right>　<b>\\%s</b></TD><TD>　</TD><TD>(税込)</TD></TR>\n",	
			$g_total );
			print "$gl_gokei";
		}
	}
}

#==================================================================
#	送料の計算
#==================================================================

sub calc_soryo {
	($mode2) = @_;
	
	# 地区別送料の読み込み
	open(COST,"$cost_file") || die "error!\n";
	flock(COST, 2);
	@COST = <COST>;
	flock(COST, 8);
	close(COST);

	# 個別送料のかかる個数を配達先別にカウント
	foreach $data_line (@BDY) {
		($su,$komitanka,$komikingaku,$nukitanka,$zei,$todokesaki,$noshi,
			$floor,$corner,$scode,$name,$imgs,$imgl,$maker,$cmnts,$cmntm,$cmntl,
			$yoryo,$sanchi,$genryo,$hyojun,$tujo,$baika,$soryo,
			$sonta1,$sonta2,$sonta3,$sonta4,$sonta5,$sonta6,$sonta7,$sonta8,$sonta9,$sonta10,$CR)
			= split(/,/,$data_line);
		if( $su <= 0 ) { next; }
		if( $soryo >= 100 && $soryo != $cool_daikin ) {
			$mailcostsu{$todokesaki} += $su;
		} else {
			$mailcostsu2{$todokesaki} = 1;
		}
	}

	# 配達先別に送料を求める
	@haisoryo = ();
	foreach $data_line (@BDY) {
		($su,$komitanka,$komikingaku,$nukitanka,$zei,$todokesaki,$noshi,
			$floor,$corner,$scode,$name,$imgs,$imgl,$maker,$cmnts,$cmntm,$cmntl,
			$yoryo,$sanchi,$genryo,$hyojun,$tujo,$baika,$soryo,
			$sonta1,$sonta2,$sonta3,$sonta4,$sonta5,$sonta6,$sonta7,$sonta8,$sonta9,$sonta10,$CR)
			= split(/,/,$data_line);

		if( $mailcost{$todokesaki} eq "" ) {
			if( $todokesaki == 0 ) {
				if( $PARA{d_name} eq "" ) {
					$haiso = $PARA{adrs0};
				} else {
					$haiso = $PARA{d_adrs0};
				}
			} else {
				$sql = "SELECT * FROM receiver WHERE SeqNo=?;";
				$sth = $dbh->prepare($sql);
				$sth->execute( $todokesaki ) || die print $sth->errstr;
				@value = $sth->fetchrow_array;
				$sth->finish;
				$haiso = $value[3];
			}
			foreach $soryodata (@COST) {
				($ken,$ryokin) = split(/,/,$soryodata);
				chomp( $ryokin );
				if( $ken eq $haiso ) {
					$mailcost{$todokesaki} = $ryokin;
					if( $mailcostsu{$todokesaki} ne "" ) { $mailcost{$todokesaki} = $ryokin * ( $mailcostsu{$todokesaki} + $mailcostsu2{$todokesaki} ); }
					push( @haisoryo, "$todokesaki=$ryokin" );
#print "$ken=$ryokin<br>";
					last;
				}
			}
		}
	}	
	
	# 無料条件のチェック
	foreach $data_line (@BDY) {
		($su,$komitanka,$komikingaku,$nukitanka,$zei,$todokesaki,$noshi,
			$floor,$corner,$scode,$name,$imgs,$imgl,$maker,$cmnts,$cmntm,$cmntl,
			$yoryo,$sanchi,$genryo,$hyojun,$tujo,$baika,$soryo,
			$sonta1,$sonta2,$sonta3,$sonta4,$sonta5,$sonta6,$sonta7,$sonta8,$sonta9,$sonta10,$CR)
			= split(/,/,$data_line);
		if( $su == 0 ) { next; }			# 数量0の商品は不要
		if( $soryo >= 100 ) { next; }		# 送料100以上は固定費用
		if( $soryo >= 1 || $soryo < 0 ) {	# 送料マイナスか1-99は無料になる金額/本数の条件
			$honsu = 0;
			$sogaku = 0;
			# 同じコーナーで同じ送料（必要本数）の商品の数量を集計
			foreach $temp_line (@BDY) {
				($su2,$komitanka2,$komikingaku2,$nukitanka2,$zei2,$todokesaki2,$noshi2,
					$floor2,$corner2,$scode2,$name2,$imgs2,$imgl2,$maker2,$cmnts2,$cmntm2,$cmntl2,
					$yoryo2,$sanchi2,$genryo2,$hyojun2,$tujo2,$baika2,$soryo2,
					$filler)
					= split(/,/,$temp_line);
				if( $soryo > 0 && ( $corner != $corner2 || $soryo != $soryo2 ) ) { next; }
				if( $soryo < 0 && $soryo != $soryo2 ) { next; }
				$honsu += $su2;
				$sogaku += $komikingaku2;
			}
#print "条件=$soryo*-1  金額=$sogaku<br>";
			if( ( $soryo > 0 && $honsu >= $soryo ) || ( $soryo < 0 && $sogaku >= $soryo*-1 ) ) {
				if( $mailcostsu{$todokesaki} eq "" ) {
					$mailcost{$todokesaki} = 0;
				} else {
					if( $tmpcost{$todokesaki} eq "" ) {
						$mailcost{$todokesaki} = $mailcost{$todokesaki} / ( $mailcostsu{$todokesaki} + $mailcostsu2{$todokesaki} ) * $mailcostsu{$todokesaki};
					}
					$tmpcost{$todokesaki} = 1;
				}
#print "$todokesaki=0<br>";
			}
		}
	}
	
	if( $mode2 == 1 ) { return 0; }

	# 配達先別の送料を合計する
	$ret = 0;
	foreach $data_line (@BDY) {
		($su,$komitanka,$komikingaku,$nukitanka,$zei,$todokesaki,$noshi,$filler ) = split(/,/,$data_line);
		$ret += $mailcost{$todokesaki};
#print "COST=$mailcost{$todokesaki}<br>";
		$mailcost{$todokesaki} = 0;
	}
	
	return $ret;
}

#==================================================================
#	注文履歴の更新
#==================================================================

sub write_order {
	($chumon) = @_;

	# カートデータの読み込み(BDY)
	open (DATA,"../cart/b_$id.txt");
	flock (DATA, 2);
	@BDY = <DATA>;
	flock (DATA, 8);
	close(DATA);
	
	# ＤＢ接続
	$dbh = DBI->connect($data_source,$user_name,$user_pass) || die print "CONNECT ERROR $DBI::ERRSTR";

	#-------------------------------------------
	# 注文履歴の更新

	$sql = "INSERT INTO orderhead VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
	$sth = $dbh->prepare($sql);
	$sth->execute( $chumon,$PARA{yubin},$PARA{adrs0},$PARA{adrs1},$PARA{adrs2},$PARA{name},
					$PARA{furigana},$PARA{tel},$PARA{email},$PARA{biko},$PARA{paykind},$PARA{cardno},
					$PARA{kigen1}*100+$PARA{kigen2},$PARA{meigi},$PARA{cardco},$PARA{aeonid} ) || die print $sth->errstr;
	$sth->finish;

	$sql = "INSERT INTO orders VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";

	$i = 1;
	foreach $data_line (@BDY) {
		($su,$komitanka,$komikingaku,$nukitanka,$zei,$todokesaki,$noshi,
			$floor,$corner,$scode,$name,$imgs,$imgl,$maker,$cmnts,$cmntm,$cmntl,
			$yoryo,$sanchi,$genryo,$hyojun,$tujo,$baika,$soryo,
			$sonta1,$sonta2,$sonta3,$sonta4,$sonta5,$sonta6,$sonta7,$sonta8,$sonta9,$sonta10,$CR)
			= split(/,/,$data_line);
		if( $su <= 0 ) { next; }
		if( $todokesaki != 0 ) {
			# 届け先の読み込み
			$sql2 = "SELECT * FROM receiver WHERE SeqNo=?;";
			$sth = $dbh->prepare($sql2);
			$sth->execute( $todokesaki ) || die print $sth->errstr;
			@value = $sth->fetchrow_array;
			$sth->finish;
			# 購入履歴明細登録
			$sth = $dbh->prepare($sql);
			$sth->execute( $chumon,$nowdate,$nowtime,$cc{'LoginID'},$scode,$name,$su,$komitanka,$soryo,$i,$value[2],$value[3],$value[4],$value[5],$value[6],$value[7] ) || die print $sth->errstr;
		}
		if( $todokesaki == 0 ) {
			$sth = $dbh->prepare($sql);
			if( $cc{'LoginID'} ne "" ) {
				# 購入履歴明細登録
				$sth->execute( $chumon,$nowdate,$nowtime,$cc{'LoginID'},$scode,$name,$su,$komitanka,$soryo,$i,$PARA{d_yubin},$PARA{d_adrs0},$PARA{d_adrs1},$PARA{d_adrs2},$PARA{d_name},$PARA{d_tel} ) || die print $sth->errstr;
			} else {
				# 購入履歴明細登録
				$sth->execute( $chumon,$nowdate,$nowtime,"",$scode,$name,$su,$komitanka,$soryo,$i,$PARA{d_yubin},$PARA{d_adrs0},$PARA{d_adrs1},$PARA{d_adrs2},$PARA{d_name},$PARA{d_tel} ) || die print $sth->errstr;
			}
		}
		$sth->finish;
		$i++;
	}

	#-------------------------------------------
	# 届け先別の最終注文の更新

	if( $cc{LoginID} ne "" ) {	# ログインしている時だけ
		$sql = "UPDATE receiver SET LastOrderDate=?,LastOrderGoodsCode=?,LastOrderGoodsName=?,LastOrderQuantity=?,LastOrderAmountOfMoney=? WHERE SeqNo=?;";
		$sth = $dbh->prepare($sql);
	
		foreach $data_line (@BDY) {
			($su,$komitanka,$komikingaku,$nukitanka,$zei,$todokesaki,$noshi,
				$floor,$corner,$scode,$name,$imgs,$imgl,$maker,$cmnts,$cmntm,$cmntl,
				$yoryo,$sanchi,$genryo,$hyojun,$tujo,$baika,$soryo,
				$sonta1,$sonta2,$sonta3,$sonta4,$sonta5,$sonta6,$sonta7,$sonta8,$sonta9,$sonta10,$CR)
				= split(/,/,$data_line);
			if( $su <= 0 ) { next; }
			if( $todokesaki == 0 ) { next; }
			$sth->execute( $nowdate,$scode,$name,$su,$komitanka,$todokesaki ) || die print $sth->errstr;
			$sth->finish;
		}
	}
		
	$dbh->disconnect;
}

#==================================================================
#	数字のカンマ編集
#==================================================================

sub put_comma {
  my $num = $_[0];
  $num = reverse $num;
  $num =~ s/(\d{3})(?=\d)(?!\d*\.)/$1,/g;
  $num = reverse $num;
  return $num
}

#==================================================================
#	クッキーの書き込み
#==================================================================

sub SetCookie {
	($secg,$ming,$hourg,$mdayg,$mong,$yearg,$wdayg)
				= gmtime(time + 60*24*60*365);

	@week = ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
	@mons = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
	$date_g = sprintf("%s, %02d\-%s\-%04d %02d:%02d:%02d GMT",
		$week[$wdayg],$mdayg,$mons[$mong],$yearg+1900,$hourg,$ming,$secg);

	$yubin = $PARA{yubin};
	$adrs0 = $PARA{adrs0};
	$adrs1 = $PARA{adrs1};
	$adrs2 = $PARA{adrs2};
	$name = $PARA{name};
	$furigana = $PARA{furigana};
	$tel = $PARA{tel};
	$email = $PARA{email};
	$nenrei = $PARA{nenrei};
	$pkind = $PARA{paykind};
#	&jcode'convert(*yubin,'jis');
#	&jcode'convert(*adrs0,'jis');
#	&jcode'convert(*adrs1,'jis');
#	&jcode'convert(*adrs2,'jis');
#	&jcode'convert(*name,'jis');
#	&jcode'convert(*tel,'jis');
#	&jcode'convert(*email,'jis');

	$cook="adrs0=$adrs0 yubin=$yubin adrs1=$adrs1 adrs2=$adrs2 name=$name furigana=$furigana tel=$tel email=$email nenrei=$nenrei pay=$pkind";
	print "Set-Cookie: $cook; expires=$date_g\n";
}

#==================================================================
#	代引額の設定
#==================================================================

sub get_daibiki {
	($total_kingaku) = @_;
	
	# 代引設定ファイルの読み込み
	open(DAIBIKI,"$daibiki_file") || die "error!\n";
	flock(DAIBIKI, 2);
	@SETDAIBIKI = <DAIBIKI>;
	flock(DAIBIKI, 8);
	close(DAIBIKI);
	
	$gaku = -1;
	foreach $daibiki_line (@SETDAIBIKI) {
		($maxgaku,$daibikigaku) = split(/,/,$daibiki_line);
		$daibiki_max = $maxgaku;
		if( $maxgaku >= $total_kingaku && $gaku == -1 ) { $gaku = $daibikigaku; last; }
	}
	return $gaku;
}

