<?php 
include('sys/phpcore.inc');
include('sys/master.php');

/*
$i = 501;
$sql = "SELECT *, x.name as judul FROM oc_product_description x, oc_product a,  oc_manufacturer b
		WHERE 
		x.product_id = a.product_id 
		AND
		a.manufacturer_id = b.manufacturer_id AND b.active = 1";
$dcon = $conn->query($sql);
while($rs = $dcon->fetch_assoc()){
	$judul = $conn->real_escape_string($rs['judul']);
	$kode = $rs['model'];
	$penerbit = $rs['manufacturer_id'];
	$id = $rs['product_id'];
	$isbn = $rs['isbn'];
	
	$sqlin = "insert into dl_product 
			(product_code,product_name,penerbit,ref_autoid,product_id,isbn) 
			values 
			('$kode','$judul','$penerbit','$id','$i','$isbn')";
	$conn->query($sqlin) or die($sqlin);
	
	
	echo $sqlin."<br>";
	
$i++;
	}

	
$sql = "SELECT * FROM oc_attribute_description a, oc_product_attribute b WHERE a.attribute_id = b.attribute_id AND a.language_id = 1";
$dcon = $conn->query($sql);
while($rs = $dcon->fetch_assoc()){
	
	$prod_id = $rs['product_id'];
	$attr_id = $rs['attribute_id'];
	$text[$attr_id][$prod_id] = $rs['text'];
	
}

$xsql = "select * from oc_product";
$xdcon = $conn->query($xsql);
while($xrs = $xdcon->fetch_assoc()){
	$id 	= $xrs['product_id'];
	$gambar = $conn->real_escape_string($xrs['image']);


	
	$sqln = "UPDATE dl_product set 
					image_url = '$gambar'
					WHERE ref_autoid = '$id'";
					$conn->query($sqln) or die($sqln);
	
	//echo $sqln."<br>";
	
}

$xsql = "select * from tempitem";
$xdcon = $conn->query($xsql);
while($xrs = $xdcon->fetch_assoc()){
	$nama = $conn->real_escape_string($xrs['nama_item']);
	$kode = $conn->real_escape_string($xrs['kode_produk']);
	$pt_autoid = $conn->real_escape_string($xrs['pt_autoid']);
	$umb 	= $conn->real_escape_string($xrs['satuan_besar']);
	$ums 	= $conn->real_escape_string($xrs['satuan_kecil']);
	$isi 	= $conn->real_escape_string($xrs['konversi_satuan']);
	$kategori 	= $conn->real_escape_string($xrs['kategori']);

	$sub 	= $conn->real_escape_string($xrs['sub_kategori']);
	$cost 	= $conn->real_escape_string($xrs['cost']);
	
	/*
	$update = "INSERT INTO mst_product 
				(pm_code,pm_name,pt_autoid,pm_ums,pm_umb,pm_ums_convert,kategori,sub_kategori) 
				VALUES 
				('$kode','$nama','$pt_autoid','$ums','$umb','$isi','$kategori','$sub')";
	//$conn->query($update);

	$update = "update mst_product set pm_bprice = '$cost' where pm_code = '$kode'";
	echo $update.";<br>";
	
}



$xsql = "select * from tempstok WHERE outlet = 2";
$xdcon = $conn->query($xsql);
while($xrs = $xdcon->fetch_assoc()){
	$name = $conn->real_escape_string($xrs['pm_name']);
	$kode = $conn->real_escape_string($xrs['pm_code']);
	$id	=	$xrs['pm_autoid'];
	

	$ums = strtoupper($xrs['satuan']);
	$price = $xrs['harga_beli'];
	$qty = $xrs['qty'];

	$pm_ums = $_pm_ums[$id];
	$convert = $_pm_convert[$id];
	
	$subtotal = $price * $qty;
	//$update = "update tempstok set satuan = '$pm_ums', pm_autoid = '$id' where pm_code = '$kode'";
	if(!$convert){$convert = 1;}
	$umb_avg_price = $price * $convert;
	$outlet = 36;
	$insert = "INSERT INTO invlog 
				(log_refdate,log_refautoid,log_refno,
				pm_autoid,pm_code,pm_name,curr_qty,log_inqty,log_price,log_ums,log_avg_price,outlet_id,
				itt_desc,itt_code,itt_autoid) 
				VALUES 
				('2020-05-05','1','SA',
				'$id','$kode','$name','$qty','$qty','$price','$pm_ums','$umb_avg_price','$outlet',
				'Stok Awal - Outlet','+','4')";

	//$update = "update mst_product SET pm_avg_price = '$umb_avg_price' WHERE pm_autoid = '$id'";
	//echo $insert.";<br>";
	
	
}

/*
$xsql = "select * from mst_product where pm_avg_price > 0";
$xdcon = $conn->query($xsql);
while($xrs = $xdcon->fetch_assoc()){
	$name = $conn->real_escape_string($xrs['nama_produk']);
	$kode = $conn->real_escape_string($xrs['kode_produk']);
	$id	=	$xrs['pm_autoid'];
	

	$ums = strtoupper($xrs['satuan']);
	$price = $xrs['pm_avg_price'];
	$qty = $xrs['qty'];

	$pm_ums = $_pm_ums[$id];

	$subtotal = $price * $qty;
	$update = "update mst_product set pm_avg_price = '$price' where pm_autoid = '$id'";
	
	$insert = "INSERT INTO podetail 
				(po_autoid,pm_autoid,pm_code,pm_name,pod_qty,pod_ums,
				pod_price,pod_nett_price,pod_subtotal) 
				VALUES ('125','$id','$kode','$name','$qty','$ums',
				'$price','$price','$subtotal')";
	echo $update.";<br>";
	
}*/
//echo toDecimal("11.31",2);
$i = 1;
$sql = "SELECT * FROM sheet1";
$xdcon = $conn->query($sql);
while($xrs = $xdcon->fetch_assoc()){

	$nama = $xrs['nama'];
	$price = $xrs['price'];
	
	$update = "INSERT INTO mst_product (pm_autoid,pm_name,pm_sprice,pm_bprice,pm_ums) 
				VALUES ('$id','$nama','$price','$price','KG')";
		$conn->query($update);
}
?>