<?

include getenv("DOCUMENT_ROOT")."/include/session_include.php";


// Level 1 이하만 사용 가능
$func->checkLevelModal(1);


/*
$fpath = getenv("DOCUMENT_ROOT").'/upload';
$ffilemode = "0755";
chmod_R($fpath, 0755);

function chmod_R($path, $filemode) {
    if (!is_dir($path))
        return chmod($path, $filemode);

    $dh = opendir($path);
    while (($file = readdir($dh)) !== false) {
        if($file != '.' && $file != '..' && $file != 'index.html') {
            $fullpath = $path.'/'.$file;
            echo "[$fullpath]<br>";
            if(is_link($fullpath))
                return FALSE;
            elseif(!is_dir($fullpath))
                if (!chmod($fullpath, $filemode))
                    return FALSE;
            elseif(!chmod_R($fullpath, $filemode))
                return FALSE;
        }
    }

    closedir($dh);

    if(chmod($path, $filemode))
        return TRUE;
    else
        return FALSE;
} 
exit;
*/

/*
$uploaddir = getenv("DOCUMENT_ROOT").'/upload/20120402132706.multi_upload _Status_20120402.csv'; 	
$rt = chmod ($uploaddir, 0755);

$uploaddir = getenv("DOCUMENT_ROOT").'/upload/20120402171526.multi_upload _Status_OOI_20120402_2.csv'; 	
$rt = chmod ($uploaddir, 0755);

exit;
*/




// ################# 차후 uptype 에 따른 if 구분이 필요함
// echo "[action=$action]";
// log file upload
if ($mode == "insert") {
	
	$uploaddir = getenv("DOCUMENT_ROOT").'/upload/'; 	
	$upfilename = "Oil_202308.csv";
	
	$existFlag = file_exists($uploaddir.$upfilename);
	
	if (!file_exists($uploaddir.$upfilename)) {
		echo "The file ".$uploaddir.$upfilename." does not exist";
	}

	//$rt = chmod ($uploaddir.$upfilename, 0755);
	
	$lineCnt = 1;
	$successRowCnt = 0;

	$getColName = array();

	echo " ##############################################################<br>";

		
	if (($handle = fopen($uploaddir.$upfilename, "r")) !== FALSE) {
	    while (($getCSV = fgetcsv($handle, 2000, ",")) !== FALSE) {
		        //$num = count($getCSV);
		        //echo "<p> $num fields in line $row: <br /></p>\n";
		        //$row++;
		        //for ($c=0; $c < $num; $c++) {
						//  echo $getCSV[$i]['c']."<br>";
						//	echo "[".utf8_encode($getCSV[$c])."]<br>";
						//}
            //foreach($getCSV as &$field) {
						//		$field = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $field);
            //}

				//while (($getCSVfp = fgets($handle, 2000)) !== FALSE) {
	    		
	    			//$getCSV = explode (",",$getCSVfp);
	    			
						if ($lineCnt == 1) {
								$getColName = $getCSV;

								$cntColName = count($getColName);
								//for($i = 0; $i < $cntColName; $i ++)
								//echo "[getColName = ".utf8_encode($getColName[$i])."]<br>";
								//break;												
						}
						else {
		        		//for ($c=0; $c < count($getCSV); $c++) 
								//	echo "[".utf8_encode($getCSV[$c])."]<br>";
						
								// Get Driver Info
								$qry_driver = "SELECT m_uid FROM tbl_member WHERE m_initial = '".trim($getCSV[1])."' ";
								$rt_driver = $jdb->fQuery($qry_driver, "query error");
								//echo "[$qry_driver]<br>";

								// Get customer Info
								$d_accountno = str_replace(" ", "", trim($getCSV[2])); // d_accountno

								$qry_customer = "SELECT * FROM tbl_customer WHERE c_accountno = '".trim($getCSV[2])."' ";
								$rt_customer = $jdb->fQuery($qry_customer, "query error");
								//echo "[$qry_customer]<br>";
								
								$saveCnt = 1;

	echo " ##############################################################<br>";
	
								for($i = 5; $i < $cntColName; $i ++) {
										if (trim($getCSV[$i]) >= 1) {

												$columns = array();
												$values = array();
						
												$columns[] = "d_orderdate";
												$columns[] = "d_ordertype";
												$columns[] = "d_driveruid";
												$columns[] = "d_customeruid";
												$columns[] = "d_accountno";
												$columns[] = "d_name";
												$columns[] = "d_paymenttype";
												$columns[] = "d_cycle";
												$columns[] = "d_rate";
												$columns[] = "d_maincontainer";
												$columns[] = "d_container";
												$columns[] = "d_location";
												$columns[] = "d_address";
												$columns[] = "d_city";
												$columns[] = "d_postal";
												//$columns[] = "d_area";
												$columns[] = "d_oil_2y";
												$columns[] = "d_oil_1y";
												$columns[] = "d_oil_0y";
												$columns[] = "d_fullcycle";
												$columns[] = "d_lastpickupdate";
												$columns[] = "d_sludge";
												$columns[] = "d_paystatus";
												$columns[] = "d_payamount";
												$columns[] = "d_visit";
												$columns[] = "d_visitdate";
												$columns[] = "d_createruid";
												$columns[] = "d_createddate";
												$columns[] = "d_status";

												$columns[] = "d_inputdate";
												$columns[] = "d_quantity";
												
												
												// Data															

												$values[] = "2023".str_replace("-", "", trim($getColName[$i]));  // d_orderdate == d_inputdate = d_visitdate
												$values[] = "N";  // d_ordertype
												
												$values[] = $rt_driver['m_uid'];  // d_driveruid

												$values[] = $rt_customer['c_uid'];  // d_customeruid
												
												$values[] = $d_accountno;  // d_accountno
												$values[] = $rt_customer['c_name'];  // d_name
												$values[] = $rt_customer['c_paymenttype'];  // d_paymenttype
												
												$d_cycle = str_replace(" ", "", trim($getCSV[4]));
												$values[] = $d_cycle;  // d_cycle
												if ($d_cycle != "") {
													//$jdb->nQuery("UPDATE tbl_customer SET c_paymentcycle='$d_cycle' WHERE c_uid = '".$rt_customer['c_uid']."'", "Update error");
												}		
																						
												$values[] = $rt_customer['c_rate'];  // d_rate
												$values[] = $rt_customer['c_maincontainer'];  // d_maincontainer
												$values[] = $rt_customer['c_container'];  // d_container
												$values[] = $rt_customer['c_location'];  // d_location
												$values[] = $rt_customer['c_address'];  // d_address
												$values[] = $rt_customer['c_city'];  // d_city
												$values[] = $rt_customer['c_postal'];  // d_postal
												//$values[] = $rt_customer['c_area'];  // d_area
												$values[] = 0;  // d_oil_2y
												$values[] = 0;  // d_oil_1y
												$values[] = 0;  // d_oil_0y
												$values[] = 0;  // d_fullcycle
												$values[] = "";  // d_lastpickupdate

												$d_sludge = str_replace(" ", "", trim($getCSV[3]));
												if ($d_sludge <= 5 &&  $d_sludge > 0) $d_sludge = 5;
												else if ($d_sludge == 0) $d_sludge = "";
												$values[] = $d_sludge;  // d_sludge
												if ($d_sludge != "") {
													//$jdb->nQuery("UPDATE tbl_customer SET c_sludge='$d_sludge' WHERE c_uid = '".$rt_customer['c_uid']."'", "Update error");
												}
															
												$values[] = "";  // d_paystatus
												$values[] = "";  // d_payamount
												$values[] = "Y";  // d_visit
												$values[] = "2023".str_replace("-", "", trim($getColName[$i]))."000000";   // d_visitdate
												$values[] = "";  // d_createruid
												$values[] = "20230901000000";  // d_createddate
												$values[] = "F";  // d_status

												$values[] = "2023".str_replace("-", "", trim($getColName[$i]))."000000";  // d_inputdate
												
												$d_quantity = str_replace(" ", "", trim($getCSV[$i]));
												$d_quantity = str_replace(",", "", $d_quantity);
												$values[] = $d_quantity;  // d_quantity

if ($lineCnt >= 2) {
												echo "Line <b>$lineCnt</b>[saveCnt=$saveCnt][$d_accountno][2023-".trim($getColName[$i])."][Qty:".str_replace(" ", "", trim($getCSV[$i]))."][d_sludge=$d_sludge]<br>";
												//for ($ii=0; $ii < count($columns); $ii++)
												//echo "[$columns[$ii]][$values[$ii]]<br>";
		
												$rtvalue = $jdb->iQuery("tbl_daily", $columns, $values);

												$c_form_eu = str_replace("-", "", trim($getCSV[0]));

												$qry_up = "UPDATE tbl_customer 
																			SET c_lastpickupdate='"."2023".str_replace("-", "", trim($getColName[$i]))."', 
																			c_lastpickupquantity='".str_replace(" ", "", trim($getCSV[$i]))."',
																			c_sludge='".$d_sludge."', c_paymentcycle='".$d_cycle."',
																			c_form_eu='".$c_form_eu."' 
																			WHERE c_uid = '".$rt_customer['c_uid']."'";
												$jdb->nQuery($qry_up, "Update error");
												//echo "[$qry_up]<br>";
												
}
												unset ($columns);
												unset ($values);
												
												$saveCnt++;
										}
								}


						}					
						
						//if ($lineCnt == 500) break;
		
						$lineCnt++;

	    }
	    fclose($handle);
	}
	

	
	echo " ##############################################################<br>";
	echo " Total : $lineCnt, Success : $successRowCnt. Fail : ".($lineCnt-$successRowCnt)."<br>";
	echo " ##############################################################<br><br>";
	
}			// End of Modify Inventory status


		
?>
