<?php
include "../../inc/inc.koneksi.php";
$tgl	= $_POST['tanggal'];
$gudang	= $_POST['gudang'];

if($gudang=='Gudang') $gd='GD';
else $gd=$gudang;

$thn	= substr($tgl,0,4);
$thn1	= (int) $thn-2000;
$sql 	= mysql_query("SELECT max(kode_op) as no_akhir FROM st_opname WHERE year(tanggal)='$thn' AND gudang='$gudang' ORDER BY kode_op");
$row	= mysql_num_rows($sql);

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