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

$kode	= $_POST[kode];
$tanggal= $_POST[tanggal];
$limit  = $_POST[limit];

$th_now = (int) substr($tanggal,0,4);
$bl_now	= (int) substr($tanggal,5,2);
$tg_now	= (int) substr($tanggal,8,2);

$data['umurpiut']= '';

$sql1= mysql_query("SELECT * FROM piutang WHERE kode_pl='$kode' AND jumlah>1000");
$row = mysql_num_rows($sql1);
if($row>0){
	while($r1=mysql_fetch_array($sql1)){
		$tglfak	= $r1[tanggal];
		$th_tr	= (int) substr($tglfak,0,4);
		$bl_tr	= (int) substr($tglfak,5,2);
		$tg_tr	= (int) substr($tglfak,8,2);
		$umur   = 0;
		$slsth 	= $th_now-$th_tr;
		$slsbl	= $bl_now-$bl_tr;
		$kb_tr  = $th_tr;
			
		while($kb_tr>4){
			$kb_tr = $kb_tr-4;
		}
		
		if($slsth==0){
			if($slsbl==0) $umur=$tg_now-$tg_tr+1;
			
			if($slsbl==1){
				if($bl_tr==1)
					$umur=31-$tg_tr+$tg_now;
				else if($bl_tr==2){
					if($kb_tr==0)
						$umur=29-$tg_tr+$tg_now;
					else
						$umur=28-$tg_tr+$tg_now;					
				}
				else if($bl_tr==3)
					$umur=31-$tg_tr+$tg_now;
				else if($bl_tr==4)
					$umur=30-$tg_tr+$tg_now;
				else if($bl_tr==5)
					$umur=31-$tg_tr+$tg_now;
				else if($bl_tr==6)
					$umur=30-$tg_tr+$tg_now;
				else if($bl_tr==7)
					$umur=31-$tg_tr+$tg_now;
				else if($bl_tr==8)
					$umur=31-$tg_tr+$tg_now;
				else if($bl_tr==9)
					$umur=30-$tg_tr+$tg_now;				
				else if($bl_tr==10)
					$umur=31-$tg_tr+$tg_now;				
				else if($bl_tr==11)
					$umur=30-$tg_tr+$tg_now;				
				else
					$umur=31-$tg_tr+$tg_now;
			}
			
			if($slsbl>1){
				if($bl_tr==1)
					$umur=31-$tg_tr;
				else if($bl_tr==2){
					if($kb_tr==0)
						$umur=29-$tg_tr;
					else
						$umur=28-$tg_tr;
				}
				else if($bl_tr==3)
					$umur=31-$tg_tr;
				else if($bl_tr==4)
					$umur=30-$tg_tr;
				else if($bl_tr==5)
					$umur=31-$tg_tr;
				else if($bl_tr==6)
					$umur=30-$tg_tr;
				else if($bl_tr==7)
					$umur=31-$tg_tr;
				else if($bl_tr==8)
					$umur=31-$tg_tr;
				else if($bl_tr==9)
					$umur=30-$tg_tr;
				else if($bl_tr==10)
					$umur=31-$tg_tr;
				else if($bl_tr==11)
					$umur=30-$tg_tr;
				else
					$umur=31-$tg_tr;
				
				$bln=$bl_tr+1;
				while($bln<$bl_now){
					if($bln==2){
						if($kb_tr==0)
							$umur=$umur+29;
						else
							$umur=$umur+28;
					}
					else if($bln==3)
						$umur=$umur+31;
					else if($bln==4)
						$umur=$umur+30;
					else if($bln==5)
						$umur=$umur+31;
					else if($bln==6)
						$umur=$umur+30;
					else if($bln==7)
						$umur=$umur+31;
					else if($bln==8)
						$umur=$umur+31;
					else if($bln==9)
						$umur=$umur+30;
					else if($bln==10)
						$umur=$umur+31;
					else if($bln==11)
						$umur=$umur+30;
					else if($bln==12)
						$umur=$umur+31;	
					$bln=$bln+1;
				}
				$umur=$umur+$tg_now;
			}
		}
		
		if($slsth>0){
			if($bl_tr==1)
				$umur=31-$tg_tr;
			else if($bl_tr==2){
				if($kb_tr==0)
					$umur=29-$tg_tr;
				else
					$umur=28-$tg_tr;
			}
			else if($bl_tr==3)
				$umur=31-$tg_tr;
			else if($bl_tr==4)
				$umur=30-$tg_tr;
			else if($bl_tr==5)
				$umur=31-$tg_tr;
			else if($bl_tr==6)
				$umur=30-$tg_tr;
			else if($bl_tr==7)
				$umur=31-$tg_tr;
			else if($bl_tr==8)
				$umur=31-$tg_tr;
			else if($bl_tr==9)
				$umur=30-$tg_tr;
			else if($bl_tr==10)
				$umur=31-$tg_tr;
			else if($bl_tr==11)
				$umur=30-$tg_tr;
			else
				$umur=31-$tg_tr;
				
			$bln=$bl_tr+1;
			while($bln<13){
				if($bln==2){
					if($kb_tr==0)
						$umur=$umur+29;
					else $umur=$umur+28;
				}
				else if($bln==3)
					$umur=$umur+31;
				else if($bln==4)
					$umur=$umur+30;
				else if($bln==5)
					$umur=$umur+31;
				else if($bln==6)
					$umur=$umur+30;
				else if($bln==7)
					$umur=$umur+31;
				else if($bln==8)
					$umur=$umur+31;
				else if($bln==9)
					$umur=$umur+30;
				else if($bln==10)
					$umur=$umur+31;
				else if($bln==11)
					$umur=$umur+30;
				else if($bln==12)
					$umur=$umur+31;				
				$bln=$bln+1;
			}
			
			if($bl_now>1){
				$bln=1;
				while($bln<$bl_now){
					if($bln==2){
						if($kb_tr==0)
							$umur=$umur+29;
						else $umur=$umur+28;
					}
					else if($bln==3)
						$umur=$umur+31;
					else if($bln==4)
						$umur=$umur+30;
					else if($bln==5)
						$umur=$umur+31;
					else if($bln==6)
						$umur=$umur+30;
					else if($bln==7)
						$umur=$umur+31;
					else if($bln==8)
						$umur=$umur+31;
					else if($bln==9)
						$umur=$umur+30;
					else if($bln==10)
						$umur=$umur+31;
					else if($bln==11)
						$umur=$umur+30;
					else if($bln==12)
						$umur=$umur+31;				
					$bln=$bln+1;
				}
				$umur=$umur+$tg_now;
			}
			if($slsth>1){
				$slsth1=$slsth-1;
				$umur=$umur+($slsth1*365);
			}
		}
		if($umur>$limit) $data['umurpiut']= $umur;
	}
}
echo json_encode($data);
?>