<?php
include "../../inc/inc.koneksi.php";

$tanggal= $_POST['tanggal'];
$kodetk = $_POST['kodetk'];

$thn	= substr($tanggal,0,4);
$bln	= substr($tanggal,5,2);
$tgl	= substr($tanggal,8,2);
$thn1	= (int) $thn-2000;
$bln1	= (int) $bln;
$tgl1	= (int) $tgl;
$text	= "SELECT max(kode_ps) as no_akhir FROM jurpsb WHERE tanggal='$tanggal' AND kode_tk='$kodetk' ORDER BY kode_ps";
$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, 9, 4);
	$no_akhir++;
	$kode = 'PSB'.sprintf("%02s",$thn1).sprintf("%02s",$bln1).sprintf("%02s",$tgl1).sprintf("%04s",$no_akhir);
	$data['nomor']= $kode;
	echo json_encode($data);
}else{
	$data['nomor']= 'PSB'.sprintf("%02s",$thn1).sprintf("%02s",$bln1).sprintf("%02s",$tgl1).'0001';	
	echo json_encode($data);
}
?>