<!DOCTYPE html>

<?php 
	session_start();
	@ob_start();
	include "../../config/koneksi.php";
	include "../../config/fungsi_rupiah.php";
	include "../../config/fungsi_indotgl.php";
	include "../head.php";

	
	if ($_SESSION['nama'] == ''){
		header('Location:index.php');
	}
	else{
?>
<html>

  <body class="hold-transition skin-blue sidebar-mini layout-boxed">
    <div class="wrapper">
		<?php include "../header.php";?>
      
		<?php include "../menu.php";?>
      

      <!-- Content Wrapper. Contains page content -->
      <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
          <h1>
            Laporan
            <small>Laba Rugi</small>
          </h1>
          <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
            <li><a href="labarugi.php">Laporan</a></li>
            <li class="active"><a href="data.php"><u>Laba Rugi</u></a></li>
          </ol>
        </section>

        <!-- Main content -->
        <section class="content">
          <div class="row">
            <div class="col-xs-12">
			<?php		
				$dt = strtotime('today GMT');
				$tglawal = $_GET['tglawal'];
				$tglakhir = $_GET['tglakhir'];
					
				if ($tglawal == ""){
					$tglawal = date('Y-m-01');
				}
				
				if ($tglakhir == ""){
					$tglakhir = date('Y-m-t'); 
				}		


				$sql = "";
				$cek5 = mysql_query("Select * from  qjurnaldetail where (billdate <= '$tglakhir') order by billdate asc");
				while ($r5=mysql_fetch_array($cek5)){
					$kasir = $_SESSION['nama'];
					$sql = $sql."('$kasir','$r5[jenis]','$r5[billdate]','$r5[cust]','$r5[custname]','$r5[dateadded]','$r5[debet]','$r5[kredit]','$r5[akun]'), ";
					
				}
				$sql = "Insert into qqjurnaldetail(kasir,jenis,billdate,cust,custname,dateadded,debet,kredit,akun) values ".substr($sql,0,(strlen($sql) - 2)).";";
				mysql_query("TRUNCATE  qqjurnaldetail");
				mysql_query("$sql");				
			?>
              
              <div class="box">
                <div class="box-header">
                  <h3 class="box-title">Laporan Laba Rugi</h3><br><br>
				  
				  <?php
						
						echo"
						<form method=POST action='aksisales.php?module=lap&act=labarugi'>
								<table>
									<tr>
										<td>
												<div class='input-group'>
													<i class='fa fa-calendar'> From </i> <input value = '$tglawal' name ='awal' type = 'date'>
												</div>
										</td>
										<td> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
										</td>
										<td>
												<div class='input-group'>
													<i class='fa fa-calendar'> To</i> <input value = '$tglakhir' name='akhir' type = 'date'>
												</div>
										</td>
										<td>
												<div class='input-group'>
													<button type='submit'> GO </button></form>
												</div>
										</td>
									</tr>
								</table><br>";

			
			
				?>
                </div><!-- /.box-header -->
                <div class="box-body">
                  <table id="example1" class="table table-bordered table-striped">
                    <thead>
                      <tr>
                        <th></th>
                        <th></th>
						<th></th>
                        <th></th>
                      </tr>
                    </thead>
                    <tbody>
					<?php
					
						echo"
						<tr>
							<td><b>PENDAPATAN</b></td>
							<td></td>
							<td></td>
							<td align='right'>$totaljual2</td>
						</tr>";
						
						
					
						$totaljual = 0;
						$cek2 = mysql_query("Select * from jualcs where jualcs.status = 'N'  and (jualcs.billdate >= '$tglawal' and jualcs.billdate <= '$tglakhir') order by billno desc ");
						while ($r2=mysql_fetch_array($cek2)){	
							$jual = mysql_result(mysql_query("select sum(qty * (harga - (harga * disc /100))) from jualcsdetail where billno = $r2[billno] and ucode=$r2[ucode]"),0);
							$totaljual = $totaljual + $jual;
						}
						
						$cek2 = mysql_query("Select * from jualcstax where jualcstax.status = 'N' and (jualcstax.billdate >= '$tglawal' and jualcstax.billdate <= '$tglakhir') order by billno desc ");
						while ($r2=mysql_fetch_array($cek2)){	
							$jualtax = mysql_result(mysql_query("select sum(qty * (harga - (harga * disc /100))) from jualcstaxdetail where billno = $r2[billno] and ucode=$r2[ucode]"),0);
							$totaljualtax = $totaljualtax + $jualtax;
						}
						$totaljual2 = number_format($totaljual + $totaljualtax,2); 
						
						
						$jualx = mysql_result(mysql_query("select (sum(debet - kredit) * -1) from qqjurnaldetail where (billdate >= '$tglawal' and billdate <= '$tglakhir') and akun = 58"),0);
						$jualx2 = number_format($jualx,2);
						
						echo"
						<tr>
							<td>Penjualan</td>
							<td></td>
							<td></td>
							<td align='right'>$jualx2</td>
						</tr>";
						
					
						echo"
						<tr>
							<td><b>HARGA POKOK PENJUALAN</b></td>
							<td></td>
							<td></td>
							<td align='right'></td>
						</tr>";
						
						
						
						//persediaan Awal
						$awal = $tglawal;
$db = "awal";
$akhir = "N";

	$equal = "";


	mysql_query("TRUNCATE kitchenchef_tax.$db");
	
	$cek = mysql_query("select 'Beli' AS `jenis`,`beli`.`billno` AS `billno`,`beli`.`billdate` AS `billdate`,`beli`.`dateadded` AS `dateadded`,`belidetail`.`produk` AS `produk`,`belidetail`.`qty` AS `qty`,`belidetail`.`harga` AS `harga`,(select sum(`belimasuk`.`qty`) from `belimasuk` where `belimasuk`.`id_detail` = `belidetail`.`id`) AS `reall` from (`beli` join `belidetail` on(`beli`.`billno` = `belidetail`.`billno` and `beli`.`status` = 'V' and `beli`.`isretur` = 'N')) where beli.billno not in (select belino from $db where jenis = 'Non') and beli.billdate <$equal '$awal' order by beli.billdate asc, beli.dateadded asc, belidetail.id asc  ");
	while ($r=mysql_fetch_array($cek)){	
	
		mysql_query("insert into $db(jenis,belino, belidate,beliprice,qtybeli,produk) values('Non','$r[billno]','$r[billdate]','$r[harga]','$r[qty]','$r[produk]')");
	}
	
	
	$cek = mysql_query("select 'BeliTax' AS `jenis`,`buy`.`billno` AS `billno`,`buy`.`billdate` AS `billdate`,`buy`.`dateadded` AS `dateadded`,`buydetail`.`produk` AS `produk`,`buydetail`.`qty` AS `qty`,`buydetail`.`harga` AS `harga`,(select sum(`buymasuk`.`qty`) from `buymasuk` where `buymasuk`.`id_detail` = `buydetail`.`id`) AS `reall` from (`buy` join `buydetail` on(`buy`.`billno` = `buydetail`.`billno` and `buy`.`status` = 'V' and `buy`.`isretur` = 'N')) where buy.billno not in (select belino from $db where jenis = 'Tax') and buy.billdate <$equal '$awal' order by buy.billdate asc, buydetail.id asc ");
	while ($r=mysql_fetch_array($cek)){	
	
		mysql_query("insert into $db(jenis,belino, belidate,beliprice,qtybeli,produk) values('Tax','$r[billno]','$r[billdate]','$r[harga]','$r[qty]','$r[produk]')");
	}
	

	
	$cek = mysql_query("select 'Jual' AS `jenis`,`jualcs`.`billno` AS `billno`,`jualcs`.`ucode` AS `ucode`,`jualcs`.`billdate` AS `billdate`,`jualcs`.`dateadded` AS `dateadded`,`jualcsdetail`.`produk` AS `produk`, `jualcsdetail`.`qty` AS `qty`, (select sum(`jualklr`.`qty`) from `jualklr` where `jualklr`.`id_detail` = `jualcsdetail`.`id`) AS `reall` from (`jualcs` join `jualcsdetail` on(`jualcs`.`billno` = `jualcsdetail`.`billno` and `jualcs`.`ucode` = `jualcsdetail`.`ucode` and `jualcs`.`isretur` = 'N' and `jualcs`.`status` = 'N'  and jualcs.billdate < '$awal'))   ");
	while ($r=mysql_fetch_array($cek)){	
		$qjual = $r['qty'];
		$cek2 = mysql_query("select * from $db where produk = $r[produk] and qtyjual < qtybeli order by belidate asc,id asc");
		while ($r2=mysql_fetch_array($cek2)){	
			$qbeli = $r2['qtybeli'];
			$qty = 0;
			if (($r2['qtyjual']  + $qjual) <= $qbeli){			
				$qty =$r2['qtyjual']  + $qjual;
			}else{
				$qty = $qbeli;
			}
			
			if($qjual > 0 ){
				mysql_query("update $db set qtyjual = '$qty' , jualno = '$r2[billno]' , ucode = '$r2[ucode]' where id = $r2[id]");
			}
			$qjual = $qjual - $qty;
		}
	}
	
	$cek = mysql_query("select 'JualTax' AS `jenis`,`jualcstax`.`billno` AS `billno`,`jualcstax`.`ucode` AS `ucode`,`jualcstax`.`billdate` AS `billdate`,`jualcstax`.`dateadded` AS `dateadded`,`jualcstaxdetail`.`produk` AS `produk`, `jualcstaxdetail`.`qty` AS `qty`, (select sum(`jualklr`.`qty`) from `jualklr` where `jualklr`.`id_detail` = `jualcstaxdetail`.`id`) AS `reall` from (`jualcstax` join `jualcstaxdetail` on(`jualcstax`.`billno` = `jualcstaxdetail`.`billno` and `jualcstax`.`ucode` = `jualcstaxdetail`.`ucode` and `jualcstax`.`isretur` = 'N' and `jualcstax`.`status` = 'N'  and jualcstax.billdate < '$awal'))   ");
	while ($r=mysql_fetch_array($cek)){	
		$qjual = $r['qty'];
		$cek2 = mysql_query("select * from $db where produk = $r[produk] and qtyjual < qtybeli order by belidate asc,id asc");
		while ($r2=mysql_fetch_array($cek2)){	
			$qbeli = $r2['qtybeli'];
			$qty = 0;
			$hitungg = 0;
			if (($r2['qtyjual']  + $qjual) <= $qbeli){			
				$qty = $r2['qtyjual']  + $qjual;
				$hitungg = $qjual;
			}else{
				$qty = $qbeli;
				$hitungg = $qbeli - $r2['qtyjual'];
			}
			
			if($qjual > 0 ){
				mysql_query("update $db set qtyjual = '$qty' , jualno = '$r2[billno]' , ucode = '$r2[ucode]' where id = $r2[id]");
			}
			$qjual = $qjual - $hitungg;
		}
	}

						
						
						
						
							$awall = mysql_result(mysql_query("select sum((qtybeli-qtyjual) * beliprice)  from awal "),0);
			
						$awal2 = number_format($awall,2);
						echo"
						<tr>
							<td><a href = 'awal.php'>Persediaan Awal</p></td>
							<td></td>
							<td align='right'>$awal2</td>
							<td ></td>
						</tr>";
					
					
					
					
					
					
					
						$pembelian = mysql_result(mysql_query("select sum(qty * harga) from beli inner join belidetail on beli.billno = belidetail.billno where beli.status = 'V' and (beli.billdate >= '$tglawal' and beli.billdate <= '$tglakhir')"),0);
												 
												
						$buy = mysql_result(mysql_query("select sum(qty * harga) from buy inner join buydetail on buy.billno = buydetail.billno where buy.status = 'V' and (buy.billdate >= '$tglawal' and buy.billdate <= '$tglakhir')"),0);
												$pembelian2 = format_rupiah($pembelian + $buy);
												
						$belix = mysql_result(mysql_query("select (sum(debet - kredit) ) from qqjurnaldetail where (billdate >= '$tglawal' and billdate <= '$tglakhir') and akun = 61"),0);
						$belix2 = number_format($belix,2);
						
						echo"
						<tr>
							<td>Pembelian</td>
							<td></td>
							<td align='right'>$belix2</td>
							<td ></td>
						</tr>";
						
						//-----
						echo"
						<tr>
							<td>-------------------------------</td>
							<td></td>
							<td align='right'>-------------------------------</td>
							<td ></td>
						</tr>";
						
						//Barang yang tersisa untuk dijual
						$ava = number_format($belix + $awall ,2);
						echo"
						<tr>
							<td>Barang yg tersisa untuk dijual</td>
							<td></td>
							<td align='right'>$ava</td>
							<td ></td>
						</tr>";
						
						
						//Persediaan Akhir
						
						$awal = $tglakhir;
$db = "akhir";
$akhir = "Y";

	$equal = "= ";


	mysql_query("TRUNCATE kitchenchef_tax.$db");
	
	$cek = mysql_query("select 'Beli' AS `jenis`,`beli`.`billno` AS `billno`,`beli`.`billdate` AS `billdate`,`beli`.`dateadded` AS `dateadded`,`belidetail`.`produk` AS `produk`,`belidetail`.`qty` AS `qty`,`belidetail`.`harga` AS `harga`,(select sum(`belimasuk`.`qty`) from `belimasuk` where `belimasuk`.`id_detail` = `belidetail`.`id`) AS `reall` from (`beli` join `belidetail` on(`beli`.`billno` = `belidetail`.`billno` and `beli`.`status` = 'V' and `beli`.`isretur` = 'N')) where beli.billno not in (select belino from $db where jenis = 'Non') and beli.billdate <$equal '$awal' order by beli.billdate asc, beli.dateadded asc, belidetail.id asc  ");
	while ($r=mysql_fetch_array($cek)){	
	
		mysql_query("insert into $db(jenis,belino, belidate,beliprice,qtybeli,produk) values('Non','$r[billno]','$r[billdate]','$r[harga]','$r[qty]','$r[produk]')");
	}
	
	
	$cek = mysql_query("select 'BeliTax' AS `jenis`,`buy`.`billno` AS `billno`,`buy`.`billdate` AS `billdate`,`buy`.`dateadded` AS `dateadded`,`buydetail`.`produk` AS `produk`,`buydetail`.`qty` AS `qty`,`buydetail`.`harga` AS `harga`,(select sum(`buymasuk`.`qty`) from `buymasuk` where `buymasuk`.`id_detail` = `buydetail`.`id`) AS `reall` from (`buy` join `buydetail` on(`buy`.`billno` = `buydetail`.`billno` and `buy`.`status` = 'V' and `buy`.`isretur` = 'N')) where buy.billno not in (select belino from $db where jenis = 'Tax') and buy.billdate <$equal '$awal' order by buy.billdate asc, buydetail.id asc");
	while ($r=mysql_fetch_array($cek)){	
	
		mysql_query("insert into $db(jenis,belino, belidate,beliprice,qtybeli,produk) values('Tax','$r[billno]','$r[billdate]','$r[harga]','$r[qty]','$r[produk]')");
	}
	
	
	$cek = mysql_query("select 'Jual' AS `jenis`,`jualcs`.`billno` AS `billno`,`jualcs`.`ucode` AS `ucode`,`jualcs`.`billdate` AS `billdate`,`jualcs`.`dateadded` AS `dateadded`,`jualcsdetail`.`produk` AS `produk`, `jualcsdetail`.`qty` AS `qty`, (select sum(`jualklr`.`qty`) from `jualklr` where `jualklr`.`id_detail` = `jualcsdetail`.`id`) AS `reall` from (`jualcs` join `jualcsdetail` on(`jualcs`.`billno` = `jualcsdetail`.`billno` and `jualcs`.`ucode` = `jualcsdetail`.`ucode` and `jualcs`.`isretur` = 'N' and `jualcs`.`status` = 'N'  and jualcs.billdate <= '$awal'))   ");
	while ($r=mysql_fetch_array($cek)){	
		$qjual = $r['qty'];
		$cek2 = mysql_query("select * from $db where produk = $r[produk] and qtyjual < qtybeli order by belidate asc,id asc");
		while ($r2=mysql_fetch_array($cek2)){	
			$qbeli = $r2['qtybeli'];
			$qty = 0;
			$hitungg = 0;
			if (($r2['qtyjual']  + $qjual) <= $qbeli){			
				$qty =   $r2['qtyjual']  + $qjual;
			}else{
				$qty = $qbeli;
			}
			
			if($qjual > 0 ){
				mysql_query("update $db set qtyjual = '$qty' , jualno = '$r2[billno]' , ucode = '$r2[ucode]' where id = $r2[id]");
			}
			$qjual = $qjual - $qty;
		}
	}
	
	
	$cek = mysql_query("select 'JualTax' AS `jenis`,`jualcstax`.`billno` AS `billno`,`jualcstax`.`ucode` AS `ucode`,`jualcstax`.`billdate` AS `billdate`,`jualcstax`.`dateadded` AS `dateadded`,`jualcstaxdetail`.`produk` AS `produk`, `jualcstaxdetail`.`qty` AS `qty`, (select sum(`jualklr`.`qty`) from `jualklr` where `jualklr`.`id_detail` = `jualcstaxdetail`.`id`) AS `reall` from (`jualcstax` join `jualcstaxdetail` on(`jualcstax`.`billno` = `jualcstaxdetail`.`billno` and `jualcstax`.`ucode` = `jualcstaxdetail`.`ucode` and `jualcstax`.`isretur` = 'N' and `jualcstax`.`status` = 'N'  and jualcstax.billdate <= '$awal')) order by jualcstax.billdate asc  ");
	while ($r=mysql_fetch_array($cek)){	
		$qjual = $r['qty'];
		$cek2 = mysql_query("select * from $db where produk = $r[produk] and qtyjual < qtybeli order by belidate asc,id asc");
		while ($r2=mysql_fetch_array($cek2)){	
			$qbeli = $r2['qtybeli'];
			$qty = 0;
			if (($r2['qtyjual']  + $qjual) <= $qbeli){			
				$qty = $r2['qtyjual']  +  $qjual;
				$hitungg = $qjual;
			}else{
				$qty = $qbeli;
				$hitungg = $qbeli - $r2['qtyjual'];
			}
			
			if($qjual > 0 ){
				mysql_query("update $db set qtyjual = '$qty' , jualno = '$r2[billno]' , ucode = '$r2[ucode]' where id = $r2[id]");
			}
			$qjual = $qjual - $hitungg;
		}
	}
	
	
	
	
							$akhir = mysql_result(mysql_query("select sum((qtybeli-qtyjual) * beliprice)  from akhir"),0);
						
						$akhir2 = number_format($akhir,2);
						echo"
						<tr>
							<td><a href='akhir.php'>Persediaan Akhir</a></td>
							<td></td>
							<td align='right'>$akhir2</td>
							<td ></td>
						</tr>";
						
						
						//HPP
						$hpp = number_format($belix + $awall - $akhir,2);
						echo"
						<tr>
							<td><b>HPP </b></td>
							<td></td>
							<td ></td>
							<td align='right'>$hpp</td>
						</tr>";
						
						
						//labarugi sebelom biaya
						$labakotor = ($jualx) - ($belix + $awall - $akhir);
						$labakotor2 = number_format($labakotor,2);
						echo"
						<tr>
							<td><b>Laba Kotor</b></td>
							<td></td>
							<td></td>
							<td align='right'>$labakotor2</td>
						</tr>";
						
						//Biaya
						echo"
						<tr>
							<td><b>Biaya</b></td>
							<td></td>
							<td></td>
							<td align='right'></td>
						</tr>";
						
						$totalbiaya = 0;
						$cekBy = mysql_query("Select * from akun_master where del = 'N' and (format in ('12','13','14') or id = 63) order by kode_rek asc ");
						while ($rBy=mysql_fetch_array($cekBy)){	
							$biaya = mysql_result(mysql_query("select sum(debet - kredit) from qqjurnaldetail where (billdate >= '$tglawal' and billdate <= '$tglakhir') and akun = $rBy[id]"),0);
							$biaya2 = number_format($biaya,2);
							$totalbiaya = $totalbiaya + $biaya;
							
							echo"
						<tr>
							<td></td>
							<td>$rBy[kode_rek] - $rBy[nama_rek]</td>
							<td align='right'>$biaya2</td>
							<td align='right'></td>
						</tr>";
						}
						
						//tot biaya
						$totalbiaya2 = number_format($totalbiaya,2);
						echo"
						<tr>
							<td><b>Total Biaya</b></td>
							<td></td>
							<td></td>
							<td align='right'>$totalbiaya2</td>
						</tr>";
						
						//Purchase Domestik
						$belidomestik = mysql_result(mysql_query("select sum(debet - kredit) from qqjurnaldetail where (billdate >= '$tglawal' and billdate <= '$tglakhir') and akun = 62"),0);
						$belidomestik2 = number_format($belidomestik,2);
						echo"
						<tr>
							<td><b>Purchase Domestik</b></td>
							<td></td>
							<td></td>
							<td align='right'>$belidomestik2 </td>
						</tr>";
						
						//tot biaya
						$lababersih = number_format($labakotor - $totalbiaya - $belidomestik,2);
						$lababersih2 = $labakotor - $totalbiaya - $belidomestik;
						echo"
						<tr>
							<td><b>Laba Rugi</b></td>
							<td></td>
							<td></td>
							<td align='right'>$lababersih</td>
						</tr>";
					?>
                        
                     
                    </tbody>
                    
                  </table>
				  <?php
				  echo"
				  <a href='aksisales.php?module=labarugi&act=updateneraca&awal=$_GET[tglawal]&akhir=$_GET[tglakhir]&tot=$lababersih2' class = 'btn btn-primary'>Update To Neraca Laba Rugi</a>";
				  ?>
                </div><!-- /.box-body -->
              </div><!-- /.box -->
            </div><!-- /.col -->
          </div><!-- /.row -->
        </section><!-- /.content -->
		
		<!-- Modal form-->
                <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                  <div class="modal-dialog ">
                    <div class="modal-content">
                      <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                        <h4 class="modal-title" id="myModalLabel"></h4>
                      </div>
                      <div class="modal-body" id="modal-bodyku">
                      </div>
                      <div class="modal-footer" id="modal-footerq">
                      </div>
                    </div>
                  </div>
                </div>
                <!-- end of modal ------------------------------>
		
		
      </div><!-- /.content-wrapper -->
		<?php include "../footer.php";?>
		

      <!-- Control Sidebar -->
      <aside class="control-sidebar control-sidebar-dark">

        <!-- Tab panes -->
        <div class="tab-content">
          <!-- Home tab content -->
          <div class="tab-pane" id="control-sidebar-home-tab">

          </div><!-- /.tab-pane -->

        </div>
      </aside><!-- /.control-sidebar -->
      <!-- Add the sidebar's background. This div must be placed
           immediately after the control sidebar -->
      <div class="control-sidebar-bg"></div>
    </div><!-- ./wrapper -->
	
	

    <!-- jQuery 2.1.4 -->
    <script src="../../plugins/jQuery/jQuery-2.1.4.min.js"></script>
    <!-- Bootstrap 3.3.5 -->
    <script src="../../bootstrap/js/bootstrap.min.js"></script>
    <!-- DataTables -->
    <script src="../../plugins/datatables/jquery.dataTables.min.js"></script>
    <script src="../../plugins/datatables/dataTables.bootstrap.min.js"></script>
    <!-- SlimScroll -->
    <script src="../../plugins/slimScroll/jquery.slimscroll.min.js"></script>
    <!-- FastClick -->
    <script src="../../plugins/fastclick/fastclick.min.js"></script>
    <!-- AdminLTE App -->
    <script src="../../dist/js/app.min.js"></script>
    <!-- AdminLTE for demo purposes -->
    <script src="../../dist/js/demo.js"></script>
    <!-- page script -->
    <script>
      $(function () {
        $("#example2").DataTable();
        $('#example1').DataTable({
          "paging": false,
          "lengthChange": true,
          "searching": true,
          "ordering": false,
		  "order": [[ 0, "desc" ]],
          "info": true,
          "autoWidth": false
        });
      });
    </script>
	
	
  </body>
</html>
<?php 
	}
?>