<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/session.php");  

header("Content-Type: application/json");  //json을 사용하기 위해 필요한 구문

$tablename ='instock';  

function conv_num($num) {
$number = (int)str_replace(',', '', $num);
return $number;
}

function pipetocomma($str) {
$strtmp = str_replace('|', ',', $str);
return $strtmp;
}

isset($_REQUEST["col1"])  ? $col1=$_REQUEST["col1"] :   $col1=''; 
isset($_REQUEST["col2"])  ? $col2=$_REQUEST["col2"] :   $col2=''; 
isset($_REQUEST["col3"])  ? $col3=$_REQUEST["col3"] :   $col3=''; 
isset($_REQUEST["col4"])  ? $col4=$_REQUEST["col4"] :   $col4=''; 
isset($_REQUEST["col5"])  ? $col5=$_REQUEST["col5"] :   $col5=''; 
isset($_REQUEST["col6"])  ? $col6=$_REQUEST["col6"] :   $col6=''; 
isset($_REQUEST["col7"])  ? $col7=$_REQUEST["col7"] :   $col7=''; 
isset($_REQUEST["col8"])  ? $col8=$_REQUEST["col8"] :   $col8=''; 
isset($_REQUEST["col9"])  ? $col9=$_REQUEST["col9"] :   $col9=''; 
isset($_REQUEST["col10"])  ? $col10=$_REQUEST["col10"] :   $col10=''; 
isset($_REQUEST["col11"])  ? $col11=$_REQUEST["col11"] :   $col11=''; 
isset($_REQUEST["col12"])  ? $col12=$_REQUEST["col12"] :   $col12=''; 
isset($_REQUEST["col13"])  ? $col13=$_REQUEST["col13"] :   $col13=''; 

$colarr1 = explode(",",$col1[0]);
$colarr2 = explode(",",$col2[0]);
$colarr3 = explode(",",$col3[0]);
$colarr4 = explode(",",$col4[0]);
$colarr5 = explode(",",$col5[0]);
$colarr6 = explode(",",$col6[0]);
$colarr7 = explode(",",$col7[0]);
$colarr8 = explode(",",$col8[0]);
$colarr9 = explode(",",$col9[0]);
$colarr10 = explode(",",$col10[0]);
$colarr11 = explode(",",$col11[0]);
$colarr12 = explode(",",$col12[0]);
$colarr13 = explode(",",$col13[0]);

$orderday = date("Y-m-d"); // 현재날짜 2022-01-20 형태로 지정	

require_once($_SERVER['DOCUMENT_ROOT'] . "/lib/mydb.php");
$pdo = db_connect();

for($i=0;$i<count($colarr1);$i++) {	
if($colarr1[$i]!='')
{
	 // | -> , 로 변환함  
     $colarr1[$i] = pipetocomma($colarr1[$i]);
     $colarr2[$i] = pipetocomma($colarr2[$i]);
     $colarr3[$i] = pipetocomma($colarr3[$i]);
     $colarr4[$i] = pipetocomma($colarr4[$i]);
     $colarr5[$i] = pipetocomma($colarr5[$i]);
     $colarr6[$i] = pipetocomma($colarr6[$i]);
     $colarr7[$i] = pipetocomma($colarr7[$i]);
     $colarr8[$i] = pipetocomma($colarr8[$i]);
     $colarr9[$i] = pipetocomma($colarr9[$i]);
     $colarr10[$i] = pipetocomma($colarr10[$i]);
     $colarr11[$i] = pipetocomma($colarr11[$i]);
     $colarr12[$i] = pipetocomma($colarr12[$i]);
     $colarr13[$i] = pipetocomma($colarr13[$i]);

		
	try {
		$pdo->beginTransaction();

		// 입력 데이터 수집
		$lot_no = $colarr1[$i];
		$inspection_date = $colarr2[$i];
		$supplier = $colarr3[$i];
		$item_name = $colarr4[$i];
		$specification = $colarr5[$i];
		$unit = $colarr6[$i];
		$received_qty = $colarr7[$i];		
		$material_no = $colarr8[$i];
		$manufacturer = $colarr9[$i];
		$remarks = $colarr10[$i];
		$purchase_price_excl_vat = $colarr11[$i];
		$weight_kg = $colarr12[$i];

		// searchtag 생성
		$searchtag = $lot_no . ' ' . $inspection_date . ' ' . $supplier . ' ' . $item_name . ' ' . 
					 $specification . ' ' . $unit . ' ' . $received_qty . ' ' . $material_no . ' ' . 
					 $manufacturer . ' ' . $remarks . ' ' . $purchase_price_excl_vat . ' ' . $weight_kg;



        // SQL 쿼리 생성 (삽입)
        $sql = "INSERT INTO " . $DB . "." . $tablename . " (";
        $sql .= "lot_no, inspection_date, supplier, item_name, specification, unit, received_qty, material_no, manufacturer, remarks, purchase_price_excl_vat, weight_kg, searchtag";
        $sql .= ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        $stmh = $pdo->prepare($sql);

        // 변수 바인딩
        $stmh->bindValue(1, $lot_no, PDO::PARAM_STR);
        $stmh->bindValue(2, $inspection_date, PDO::PARAM_STR);
        $stmh->bindValue(3, $supplier, PDO::PARAM_STR);
        $stmh->bindValue(4, $item_name, PDO::PARAM_STR);
        $stmh->bindValue(5, $specification, PDO::PARAM_STR);
        $stmh->bindValue(6, $unit, PDO::PARAM_STR);
        $stmh->bindValue(7, str_replace(',', '', $received_qty), PDO::PARAM_STR);
        $stmh->bindValue(8, $material_no, PDO::PARAM_STR);
        $stmh->bindValue(9, $manufacturer, PDO::PARAM_STR);
        $stmh->bindValue(10, $remarks, PDO::PARAM_STR);
        $stmh->bindValue(11, str_replace(',', '', $purchase_price_excl_vat), PDO::PARAM_STR);
        $stmh->bindValue(12, str_replace(',', '', $weight_kg), PDO::PARAM_STR);
        $stmh->bindValue(13, $searchtag, PDO::PARAM_STR);

		// Execute the statement
		$stmh->execute();
		$pdo->commit();
	} catch (PDOException $Exception) {
		$pdo->rollBack();
		print "오류: " . $Exception->getMessage();
	}
	
	}
}
//각각의 정보를 하나의 배열 변수에 넣어준다.
$data = array(
		"colarr1" => $colarr1
	);

//json 출력
echo(json_encode($data, JSON_UNESCAPED_UNICODE));

?>