<?php
include "../../inc/inc.koneksi.php";
$tanggal= $_POST['tanggal'];
$thn	= substr($tanggal,0,4);
$bln	= substr($tanggal,5,2);
$tgl	= substr($tanggal,8,2);
$thn1	= (int) $thn-2000;
$text	= "SELECT max(kode_km) as no_akhir FROM jurkm WHERE tanggal='$tanggal' ORDER BY kode_km";
$sql 	= mysql_query($text);
$row	= mysql_num_rows($sql);

if ($row>0){	
	$data=mysql_fetch_array($sql);	
	$no = $data['no_akhir'];
	$no_akhir = (int) substr($no, 8, 3);
	$no_akhir++;
	$kode = 'KM'.sprintf("%02s",$thn1).$bln.$tgl.sprintf("%03s",$no_akhir);
	$data['nomor'] = $kode;
	$data['tanggal'] = $tgl;
	echo json_encode($data);
}else{
	$data['nomor'] = 'KM'.sprintf("%02s",$thn1).$bln.$tgl.'001';
	$data['tanggal'] = $tgl;
	echo json_encode($data);
}
?>