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

$tanggal= $_POST['tanggal'];
$user	= $_POST['user'];
$kodecab= $_POST['kodecab'];

$thn	= substr($tanggal,0,4);
$thn	= $thn-2000;
$bln	= substr($tanggal,5,2);
$tgl	= substr($tanggal,8,2);

$sql 	= mysql_query("SELECT max(kode_pj) as no_akhir FROM faktur_pj WHERE tanggal='$tanggal' AND user='$user' ORDER BY kode_pj");
$row	= mysql_num_rows($sql);

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