<?php

include_once "../../mainfile.php";

$count = 0;
$sql = "SELECT kcode FROM `受注メイン` WHERE kcode<10000 and kcode>0 group by kcode";
$result = icms::$xoopsDB->query($sql);
	while ($myrow = icms::$xoopsDB->fetchArray($result)) {
/*
		$sql = "select * from tmaster where ID='" . $myrow['kcode'] . "'";
		echo $sql . "<br />";
		$result2 = icms::$xoopsDB->query($sql);
		$myrow2 = icms::$xoopsDB->fetchArray($result2);
		$out_sql .= "insert into `取引先`(`旧売ID`, `社名`, `カナ名`, `郵便番号`, `住所`, `電話番号`, `手数料率`, `総括請求書`, `顧客`, `請求先`, `管理会社`,`備考`, `敬称`) values (";
		$out_sql .= "'" . $myrow2['ID'] . "',";
		$out_sql .= "'" . $myrow2['社名'] . "',";
		$out_sql .= "'" . $myrow2['カナ名'] . "',";
		$out_sql .= "'" . $myrow2['郵便番号'] . "',";
		$out_sql .= "'" . $myrow2['住所'] . "',";
		$out_sql .= "'" . $myrow2['電話番号'] . "',";
		$out_sql .= "'" . $myrow2['手数料率'] . "',";
		$out_sql .= "'" . $myrow2['総括請求書'] . "',";
		$out_sql .= "'" . $myrow2['顧客'] . "',";
		$out_sql .= "'" . $myrow2['請求先'] . "',";
		$out_sql .= "'" . $myrow2['管理会社'] . "',";
		$out_sql .= "'" . $myrow2['備考'] . "',";
		$out_sql .= "'" . $myrow2['敬称'] . "');";
*/

		$sql = "select * from 取引先 where 旧売ID='" . $myrow['kcode'] . "'";
		$result2 = icms::$xoopsDB->query($sql);
		$myrow2 = icms::$xoopsDB->fetchArray($result2);
		$out_sql .= "update 受注メイン set kcode='" . $myrow2['ID'] . "' where kcode='" . $myrow['kcode'] . "';";
//		$result = icms::$xoopsDB->queryF($sql);
		$out_sql .= "update 受注メイン set mcode='" . $myrow2['ID'] . "' where mcode='" . $myrow['kcode'] . "';";
//		$result = icms::$xoopsDB->queryF($sql);
		$out_sql .= "update 受注メイン set scode='" . $myrow2['ID'] . "' where scode='" . $myrow['kcode'] . "';";

		$count++;
	}
	$rrr = icms::$xoopsDB->queryF($out_sql);
//	echo $out_sql;

/*


// 次に取引先データから
$sql = "select * from 取引先 where 旧売ID>0";
$result = icms::$xoopsDB->query($sql);
$rowcount = icms::$xoopsDB->getRowsNum($result);

$sql = "";
$counter = 0;
if($rowcount>0) {
	while ($myrow = icms::$xoopsDB->fetchArray($result)) {
		$old_id = $myrow['旧売ID'];
		$new_id = $myrow['ID'];
		//受注メインの変更
		$sql .= "update 受注メイン set kcode='" . $new_id . "' where kcode='" . $old_id . "';";
//		$result = icms::$xoopsDB->queryF($sql);
		$sql .= "update 受注メイン set mcode='" . $new_id . "' where mcode='" . $old_id . "';";
//		$result = icms::$xoopsDB->queryF($sql);
		$sql .= "update 受注メイン set scode='" . $new_id . "' where scode='" . $old_id . "';";
//		$result = icms::$xoopsDB->queryF($sql);
	}
	$counter++;
	if($counter == 200){
		$counter = 0;
		$result = icms::$xoopsDB->queryF($sql);
		$sql = "";
	}
}
if($counter>0)
		$result = icms::$xoopsDB->queryF($sql);

*/

echo $count . "終わったと思われる";
