<?php
include "koneksi.php";
$bulans = array("Jan", "Feb", "Mar", "Apr", "Mei", "Jun", "Jul", "Agu", "Sep", "Okt", "Nov", "Des");
$response = array();
$response["data"] = array();

$query2 = mysqli_query($koneksi, "SELECT * FROM bahan WHERE id_produk = '".$_GET['id_produk']."' ORDER BY nama_bahan ASC");
while($result2=mysqli_fetch_array($query2)) {
	$analisa = array();
	$analisa['nama_bahan'] = $result2['nama_bahan'];
	$analisa['analisa'] = array();
	$i = 1;
	$totalx = 0;
	$totaly = 0;
	$totalxy = 0;
	$totalx2 = 0;
	$n = 0;
	$query = mysqli_query($koneksi, "SELECT * FROM beli WHERE id_bahan='".$result2['id_bahan']."' ORDER BY tahun, bulan ASC");
	$rows = mysqli_num_rows($query);
	while($result=mysqli_fetch_array($query)) {
		$bln = $result['bulan'];
		$tahun = $result['tahun'];
		$periode = $bulans[$bulan-1]." ".$tahun;
		$total = $result['jumlah'];
		$xy = $i * $total;
		$x2 = pow($i, 2);

		$totalx += $i;
		$totaly += $total;
		$totalxy += $xy;
		$totalx2 += $x2;

		$output = array();
		$output["periode"]=$bulans[($result["bulan"]-1)]." ".$tahun;
		$output["y"]=$result["jumlah"];
		$output["x"]=$i;
		$output["xy"]=$xy;
		$output["xx"]=$x2;

		array_push($analisa["analisa"], $output);

		$i++;
		$n++;
	}

	$output = array();
	$output["periode"]="Total";
	$output["x"]=$totalx;
	$output["y"]=$totaly;
	$output["xy"]=$totalxy;
	$output["xx"]=$totalx2;

	array_push($analisa["analisa"], $output);

	$str_a = "";
	$str_b = "";
	$str_y = "";
	$a = 0.0;
	$b = 0.0;
	$y = 0.0;

	if($rows > 0){
		$kali = $totalx / $n;
		$atas_kiri = $totaly * $kali;
		$atas_kanan = $totalx * $kali;
		$bawah_kiri = $totalxy;
		$bawah_kanan = $totalx2;

		$kurang_kiri = $atas_kiri - $bawah_kiri;
		$kurang_kanan = $atas_kanan - $bawah_kanan;
		$b = $kurang_kiri / -$kurang_kanan;
		$b = round($b, 2);

		$str_b .= "Perhitungan Nilai b\n";
		$str_b .= "∑Y	=	an + b∑X\n";
		$str_b .= "∑XY =	a∑X + b∑X2\n";
		$str_b .= $n."a + ".$totalx."b = ".$totaly."(x".$kali.")\n";
		$str_b .= $totalx."a + ".$totalx2."b = ".$totalxy."(x1)\n";
		$str_b .= "---------------------------------------------------\n";
		$str_b .= "Metode Substitusi dan Eliminasi\n";
		$str_b .= ($n*$kali)."a + ".($totalx*$kali)."b = ".$totaly*$kali."\n";
		$str_b .= $totalx."a + ".$totalx2."b = ".$totalxy."\t\t\t-\n";
		$str_b .= "---------------------------------------------------\n";
		$str_b .= $kurang_kanan."b = ".$kurang_kiri."\n";
		$str_b .= "b = ".$kurang_kiri." / ".-$kurang_kanan."\n";
		$str_b .= "b = ".$b;

		$a = ($totaly-($totalx*$b))/$n;
		$a = round($a, 2);
		$str_a .= "Perhitungan Nilai a\n";
		$str_a .= "∑Y	=	an + b∑X\n";
		$str_a .= $n."a + ".$totalx."b = ".$totaly."\n";
		$str_a .= $n."a + (".$totalx." * ".$b.") = ".$totaly."\n";
		$str_a .= $n."a + ".($totalx*$b)." = ".$totaly."\n";
		$str_a .= $n."a = ".$totaly." + (".-($totalx*$b).")\n";
		$str_a .= $n."a = ".($totaly-($totalx*$b))."\n";
		$str_a .= "a = ".($totaly-($totalx*$b))." / ".$n."\n";
		$str_a .= "a = ".$a."\n";

		$y = $a+($b*$i);
		$y = round($y, 2);
		$str_y .= "Perhitungan Hasil Akhir Nilai Y\n";
		$str_y .= "Y	=	a + b.X\n";
		$str_y .= "Y = ".$a." + (".$b." * ".$i.")\n";
		$str_y .= "Y = ".$a." + ".($b*$i)."\n";
		$str_y .= "Y = ".$y." atau dibulatkan sama dengan ".round($y)."\n";
	}

	if($bln == 12){
	  $bln = 1;
	  $tahun++;
	} else {
	  $bln++;
	}

	$analisa['nama_bahan'] = $result2['nama_bahan'];
	$analisa['bln'] = $bln;
	$analisa['bulan'] = $bulans[$bln-1];
	$analisa['tahun'] = $tahun;
	$analisa['hasil'] = $y;
	$analisa['a'] = $str_a;
	$analisa['b'] = $str_b;
	$analisa['y'] = $str_y;

	array_push($response["data"], $analisa);
}

echo json_encode($response);
?>
