<?php
include "../../inc/inc.koneksi.php";
session_start();

$kode	 =$_POST[kode];
$nocab   =$_POST[nocab];
$bulan   =$_POST[bulan];
$tahun   =$_POST[tahun];
$user	 =$_SESSION[namauser];

$kab=$tahun;
while($kab>4){
    $kab=$kab-4;
}

if($bulan==1){
    $bln='01';
    $tgl='31';
}else if($bulan==2){
    $bln='02';
    if($kab==0) $tgl='29';
    else $tgl='28';
}else if($bulan==3){
    $bln='03';
    $tgl='31';
}else if($bulan==4){
    $bln='04';
    $tgl='30';
}else if($bulan==5){
    $bln='05';
    $tgl='31';
}else if($bulan==6){
    $bln='06';
    $tgl='30';
}else if($bulan==7){
    $bln='07';
    $tgl='31';
}else if($bulan==8){
    $bln='08';
    $tgl='31';
}else if($bulan==9){
    $bln='09';
    $tgl='30';
}else if($bulan==10){
    $bln='10';
    $tgl='31';
}else if($bulan==11){
    $bln='11';
    $tgl='30';
}else if($bulan==12){
    $bln='12';
    $tgl='31';
}
$tanggal=$tahun.'-'.$bln.'-'.$tgl;

$sql	= mysql_query("SELECT * FROM interface WHERE nocab='$nocab'");
$r 		= mysql_fetch_assoc($sql);

$akumgd = $r[akum_gdg];
$akumkd = $r[akum_ken];
$akumiv = $r[akum_ivk];
$akumwd = $r[akum_wad];
$akumal = $r[akum_lain];
$deprgd = $r[depres_gdg];
$deprkd = $r[depres_ken];
$depriv = $r[depres_ivk];
$deprwd = $r[depres_wad];
$depral = $r[depres_lain];
$labapb = $r[laba_pb];
$labrug = $r[laba_rugi];
$nosubakg= substr($akumgd,0,4);
$nosubakk= substr($akumkd,0,4);
$nosubaki= substr($akumiv,0,4);
$nosubakw= substr($akumwd,0,4);
$nosubakl= substr($akumal,0,4);
$nosubdpg= substr($deprgd,0,4);
$nosubdpk= substr($deprkd,0,4);
$nosubdpi= substr($depriv,0,4);
$nosubdpw= substr($deprwd,0,4);
$nosubdpl= substr($depral,0,4);
$nosublpb= substr($labapb,0,4);
$nosublr = substr($labrug,0,4);

$sql = mysql_query("SELECT * from aktiva WHERE nocab='$nocab' AND !(kode_bb like '%131%') AND akum_dep<harga_bl GROUP BY kode_bb");

while($r=mysql_fetch_array($sql)){
    $kodebb = $r[kode_bb];
    $nosubbb=substr($kodebb,0,4);
    
    if($nosubbb==$nosubakg){
        $bbakum =$akumgd;
        $biaya  =$deprgd;
        $nosubak=$nosubakg;
        $nosubbi=$nosubdpg;
    }else if($nosubbb==$nosubakk){
        $bbakum =$akumkd;
        $biaya  =$deprkd;
        $nosubak=$nosubakk;
        $nosubbi=$nosubdpk;
    }else if($nosubbb==$nosubaki){
        $bbakum =$akumiv;
        $biaya  =$depriv;
        $nosubak=$nosubaki;
        $nosubbi=$nosubdpi;
    }else if($nosubbb==$nosubakw){
        $bbakum =$akumwd;
        $biaya  =$deprwd;
        $nosubak=$nosubakw;
        $nosubbi=$nosubdpw;
    }else if($nosubbb==$nosubakl){
        $bbakum =$akumal;
        $biaya  =$depral;
        $nosubak=$nosubakl;
        $nosubbi=$nosubdpl;
    }
    
    $sql1 = mysql_query("SELECT * from akun_bb WHERE noakun='$kodebb'");
    $r1   = mysql_fetch_assoc($sql1);
    $namabb= $r[naakun];
    
    $sql1 = mysql_query("SELECT * from aktiva WHERE kode_bb='$kodebb' AND akum_dep<harga_bl ORDER BY kode_bb");
    $row1 = mysql_num_rows($sql1);
    $jdepres=0;
    
    while($r1=mysql_fetch_array($sql1)){
        $kodeak=$r1[kode_ak];
        $namaak=$r1[nama_ak];
        $tanggalbl=$r1[tanggal_bl];
        $hargabl=$r1[harga_bl];
        $umurek =$r1[umur_ek];
        $nilres =$r1[nilai_res];
        $akumdep=$r1[akum_dep];
        $nobp=substr($kodeak,5,4);
        $bpakum=$bbakum.$nobp;
        $nilbuk =$hargabl-$akumdep;
        $depres =($hargabl-$nilres)/$umurek;
        
        if($nilbuk>0){
            if($depres>$nilbuk) $depres=$nilbuk;
            $akumdep=$akumdep+$depres;
            
            mysql_query("INSERT INTO depres_akt (kode_dp,tanggal,nocab,bulan,tahun,kode_bb,kode_ak,nama_ak,tanggal_bl,harga_bl,umur_ek,depresiasi,akum_dep,user) VALUES('$kode','$tanggal','$nocab','$bulan','$tahun','$kodebb','$kodeak','$namaak','$tanggalbl','$hargabl','$umurek','$depres','$akumdep','$user')");
        
            $sql2  = mysql_query("SELECT noakun,mutasi_kd FROM akun_bb WHERE noakun='$bbakum'");
            $r2    = mysql_fetch_assoc($sql2);
            $kredit= $r2[mutasi_kd]+$depres;
            mysql_query("UPDATE akun_bb SET mutasi_kd='$kredit' WHERE noakun='$bbakum'");
        
            $sql2  = mysql_query("SELECT noakun,mutasi_kd FROM akun_bp WHERE noakun='$bpakum'");
            $r2    = mysql_fetch_assoc($sql2);
            $kredit= $r2[mutasi_kd]+$depres;

            mysql_query("UPDATE akun_bp SET mutasi_kd='$kredit' WHERE noakun='$bpakum'");
            mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,nobp,nocab,kredit,keterangan,user) VALUES('$kode','DEP','$tanggal','$kode','$bbakum','$nosubak','$bpakum','$nocab','$depres','Depresiasi aktiva $bpakum bulan $bulan $tahun','$user')");
        
            $jdepres=$jdepres+$depres;
        }
    }
    
    if(!($jdepres==0)){
        $sql2  = mysql_query("SELECT noakun,mutasi_db FROM akun_bb WHERE noakun='$biaya'");
        $r2    = mysql_fetch_assoc($sql2);
        $debet = $r2[mutasi_db]+$jdepres;

        mysql_query("UPDATE akun_bb set mutasi_db='$debet' WHERE noakun='$biaya'");
        mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,nocab,debet,keterangan,user) VALUES('$kode','DEP','$tanggal','$kode','$biaya','$nosubbi','$nocab','$jdepres','Depresiasi aktiva bulan $bulan $tahun','$user')");

        $sql2 = mysql_query("SELECT noakun,mutasi_db FROM akun_bb WHERE noakun='$labapb'");
        $r2   = mysql_fetch_assoc($sql2);
        $debet= $r2[mutasi_db]+$jdepres;

        mysql_query("UPDATE akun_bb SET mutasi_db='$debet' WHERE noakun='$labapb'");
        mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,nocab,debet,keterangan,user) VALUES('$kode','DEP','$tanggal','$kode','$labapb','$nosublpb','$nocab','$jdepres','Depresiasi $namabb bulan $bulan $tahun','$user')");
    
        $sql2 = mysql_query("SELECT noakun,mutasi_kd FROM akun_bb WHERE noakun='$labrug'");
        $r2   = mysql_fetch_assoc($sql2);
        $kredit= $r2[mutasi_kd]+$jdepres;

        mysql_query("UPDATE akun_bb SET mutasi_kd='$kredit' WHERE noakun='$labrug'");
        mysql_query("INSERT INTO rekapjur (kode_tr,jurnal,tanggal,no_bukti,noakun,nosub,nocab,kredit,keterangan,user) VALUES('$kode','DEP','$tanggal','$kode','$labrug','$nosublr','$nocab','$jdepres','Depresiasi $namabb bulan $bulan $tahun','$user')");
    }
}
?>