<?php 
session_start();
require("php_con.php");
require("check_user.php");

if(isset($_POST['upload_btn'])){
	 $filename = explode( ".", $_FILES[ 'file' ][ 'name' ] );
	 if ( $filename[ 1 ] == 'csv' ) {
	 	$handle = fopen( $_FILES[ 'file' ][ 'tmp_name' ], "r" );
	 	while ( $data = fgetcsv( $handle ) ) {
	 		$pd_name = mysqli_real_escape_string( $connation, $data[ 1 ] );
			$pd_name_sinhala = mysqli_real_escape_string( $connation, $data[ 2 ] );
			$pd_category = mysqli_real_escape_string( $connation, $data[ 3 ] );
			$pd_unit = mysqli_real_escape_string( $connation, $data[ 4 ] );
			$pd_sku = mysqli_real_escape_string( $connation, $data[ 5 ] );
			$pd_barcode = mysqli_real_escape_string( $connation, $data[ 6 ] );
			$pd_exp_date = mysqli_real_escape_string( $connation, $data[ 7 ] );
			$pd_buy_price = mysqli_real_escape_string( $connation, $data[ 8 ] );
			$pd_market_price = mysqli_real_escape_string( $connation, $data[ 9 ] );
			$pd_sale_price = mysqli_real_escape_string( $connation, $data[ 10 ] );
			$pd_discount = mysqli_real_escape_string( $connation, $data[ 11 ] );
			$pd_wholesale_price = mysqli_real_escape_string( $connation, $data[ 12 ] );
			$pd_re_order_level = mysqli_real_escape_string( $connation, $data[ 13 ] );
			$pd_description = mysqli_real_escape_string( $connation, $data[ 14 ] );
			$pd_supplier = mysqli_real_escape_string( $connation, $data[ 15 ] );
			$pd_add_date = mysqli_real_escape_string( $connation, $data[ 16 ] );
			$pd_add_time = mysqli_real_escape_string( $connation, $data[ 17 ] );
			$pd_status = mysqli_real_escape_string( $connation, $data[ 18 ] );
			$pd_stock_maintenance = mysqli_real_escape_string( $connation, $data[ 19 ] );
			$sd_qty = mysqli_real_escape_string( $connation, $data[ 20 ] );
			
			
			if($pd_name=="pd_name"){}
			else{
			mysqli_query($connation,"INSERT INTO
			product_details (product_id, product_systemid, product_barcode, product_productid, product_name, product_name_sinhala, product_add_discount, product_alertqty, product_supplier, product_catagory, product_unit, product_stock_maintain, product_addtime)
			VALUES (NULL, '$pd_barcode', '$pd_barcode', '$pd_barcode', '$pd_name', '$pd_name_sinhala', '0', '0', '1', '$pd_category', '1', '1', '$current_date $current_time')");
				
			mysqli_query($connation,"INSERT INTO
			price_details (price_id, price_systemid, price_name, price_buy, price_sale, price_discount, price_addtime)
			VALUES (NULL, '$pd_barcode', 'Opening Badge', '$pd_buy_price', '$pd_sale_price', '0', '$current_date $current_time')");
				
			mysqli_query($connation,"INSERT INTO
			stock_details (stock_id, stock_systemid, stock_qty, stock_type, stock_info, stock_addtime)
			VALUES (NULL, '$pd_barcode', '$sd_qty', '1', 'Manual poduct upload and stock update', '$current_date $current_time')");
				
			}
	 	}
	 }
}

if(isset($_POST['upload_supplier'])){
	 $filename = explode( ".", $_FILES[ 'supplier' ][ 'name' ] );
	 if ( $filename[ 1 ] == 'csv' ) {
	 	$handle = fopen( $_FILES[ 'supplier' ][ 'tmp_name' ], "r" );
	 	while ( $data = fgetcsv( $handle ) ) {
	 		$sd_id = mysqli_real_escape_string( $connation, $data[ 0 ] );
			$sd_name = mysqli_real_escape_string( $connation, $data[ 1 ] );
			$sd_mobile = (int)mysqli_real_escape_string( $connation, $data[ 2 ] );
			$sd_email = mysqli_real_escape_string( $connation, $data[ 3 ] );
			$sd_address = mysqli_real_escape_string( $connation, $data[ 4 ] );
			$sd_company = mysqli_real_escape_string( $connation, $data[ 5 ] );
			$sd_add_date = mysqli_real_escape_string( $connation, $data[ 6 ] );
			$sd_add_time = mysqli_real_escape_string( $connation, $data[ 7 ] );			
			
			if($sd_name=="sd_name"){}
			else{
				mysqli_query($connation,"INSERT INTO
				supplier_details (supplier_id, supplier_name, supplier_mobile, supplier_company, supplier_address)
				VALUES ('$sd_id', '$sd_name', '$sd_mobile', '$sd_company', '$sd_address')");
			}
	 	}
	 }
}

if(isset($_POST['upload_category'])){
	 $filename = explode( ".", $_FILES[ 'category' ][ 'name' ] );
	 if ( $filename[ 1 ] == 'csv' ) {
	 	$handle = fopen( $_FILES[ 'category' ][ 'tmp_name' ], "r" );
	 	while ( $data = fgetcsv( $handle ) ) {
	 		$catogary_id = mysqli_real_escape_string( $connation, $data[ 0 ] );
			$catogary_text = mysqli_real_escape_string( $connation, $data[ 1 ] );
			
			if($catogary_text=="catogary_text"){}
			else{
				mysqli_query($connation,"INSERT INTO catogary_list (catogary_id, catogary_text) VALUES ('$catogary_id', '$catogary_text')");
			}
	 	}
	 }
}

if(isset($_POST['upload_customer'])){
	 $filename = explode( ".", $_FILES[ 'customer' ][ 'name' ] );
	 if ( $filename[ 1 ] == 'csv' ) {
	 	$handle = fopen( $_FILES[ 'customer' ][ 'tmp_name' ], "r" );
	 	while ( $data = fgetcsv( $handle ) ) {
	 		$cd_id = mysqli_real_escape_string( $connation, $data[ 0 ] );
			$cd_name = mysqli_real_escape_string( $connation, $data[ 1 ] );
			$cd_mobile = (int)mysqli_real_escape_string( $connation, $data[ 2 ] );	
			$cd_email = mysqli_real_escape_string( $connation, $data[ 3 ] );	
			$cd_address = mysqli_real_escape_string( $connation, $data[ 4 ] );	
			$cd_credit_limit = mysqli_real_escape_string( $connation, $data[ 5 ] );	
			$cd_add_date = mysqli_real_escape_string( $connation, $data[ 6 ] );	
			$cd_add_time = mysqli_real_escape_string( $connation, $data[ 7 ] );
			$sp_amount = mysqli_real_escape_string( $connation, $data[ 8 ] );			
			
			if($cd_name=="cd_name"){}
			else{
				if(mysqli_query($connation,"INSERT INTO
				customers_details (cd_id, cd_name, cd_mobile, cd_email, cd_address, cd_add_date, cd_add_time)
				VALUES (NULL, '$cd_name', '$cd_mobile', '$cd_email', '$cd_address', '$current_date', '$current_time')")){

				$sp_supplier=mysqli_insert_id($connation);
				mysqli_query($connation,"INSERT INTO
				customers_payment (sp_id, sp_supplier, sp_date, sp_invoice, sp_info, sp_amount, sp_pay_type, sp_cheques_id, sp_add_date, sp_add_time, sp_user)
				VALUES (NULL, '$sp_supplier', '$current_date', '', '', '$sp_amount', 'Cash', NULL, '$current_date', '$current_time', '$_SESSION[ud_id]')");
				}
			}
	 	}
	 }
}

if(isset($_POST['update_price'])){
	 $filename = explode( ".", $_FILES[ 'price_update' ][ 'name' ] );
	 if ( $filename[ 1 ] == 'csv' ) {
	 	$handle = fopen( $_FILES[ 'price_update' ][ 'tmp_name' ], "r" );
	 	while ( $data = fgetcsv( $handle ) ) {
	 		$pd_id = mysqli_real_escape_string( $connation, $data[ 0 ] );
			$pd_name = mysqli_real_escape_string( $connation, $data[ 1 ] );
			$pd_name_sinhala = mysqli_real_escape_string( $connation, $data[ 2 ] );	
			$pd_category = mysqli_real_escape_string( $connation, $data[ 3 ] );	
			$pd_buy_price = mysqli_real_escape_string( $connation, $data[ 4 ] );	
			$pd_market_price = mysqli_real_escape_string( $connation, $data[ 5 ] );	
			$pd_sale_price = mysqli_real_escape_string( $connation, $data[ 6 ] );
			$pd_discount = mysqli_real_escape_string( $connation, $data[ 7 ] );			
			$pd_wholesale_price = mysqli_real_escape_string( $connation, $data[ 8 ] );			
			
			if($pd_name=="pd_name"){}
			else{
				mysqli_query($connation,"UPDATE product_detals SET pd_name='$pd_name',pd_name_sinhala='$pd_name_sinhala',pd_buy_price='$pd_buy_price',pd_market_price='$pd_market_price',pd_sale_price='$pd_sale_price',pd_discount='$pd_discount',pd_wholesale_price='$pd_wholesale_price' WHERE pd_id='$pd_id'");
			}
	 	}
	 }
}
?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Bulk Upload Product</title>
</head>

<body>
	
<em style="color: red;">Upload Only CSV File</em><br><br>
	
<form method="post" enctype="multipart/form-data">
	<strong>Product</strong> <a href="download_file/item_list.csv" download>Download Template</a><br>
	
	<input name="file" type="file" id="file">
	<button name="upload_btn" type="submit">Upload</button>
</form>
	
<hr>
	
<form method="post" enctype="multipart/form-data">
	<strong>Supplier</strong> Download Template<br>
	
	<input name="supplier" type="file" disabled="disabled" id="supplier">
	<button name="upload_supplier" type="submit" disabled>Upload</button>
</form>
	
<hr>
	
<form method="post" enctype="multipart/form-data">
	<strong>Category</strong> <a href="download_file/catogary.csv" download>Download Template</a><br>
	
	<input name="category" type="file" id="category">
	<button name="upload_category" type="submit">Upload</button>
</form>
	
<hr>
	
<form method="post" enctype="multipart/form-data">
	<strong>Customer</strong> Download Template<br>
	
	<input name="customer" type="file" disabled="disabled" id="customer">
	<button name="upload_customer" type="submit" disabled>Upload</button>
</form>
	
<hr>
	
<form method="post" enctype="multipart/form-data">
	<strong>Product Price Update</strong><br>
	
	<input name="price_update" type="file" disabled="disabled" id="price_update">
	<button name="update_price" type="submit" disabled>Upload</button>
</form>
	
<hr>
	
<a href="download/product.php">Download Product List</a><br>
<a href="download/category.php">Download Category List</a>
	
</body>
</html>