<?php include('header.php'); ?>

 <head>
  
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  
  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/js/bootstrap-multiselect.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/css/bootstrap-multiselect.css" />
 </head>

<div class="row">
    <div class="box col-md-12">
        <div class="box-inner">
            <div class="box-header well" data-original-title="">
                <h2><i class="glyphicon glyphicon-edit"></i> Kar Zarar Raporu</h2>

                <div class="box-icon">
                    
                    <a href="#" class="btn btn-minimize btn-round btn-default"><i
                            class="glyphicon glyphicon-chevron-up"></i></a>
                    <a href="#" class="btn btn-close btn-round btn-default"><i
                            class="glyphicon glyphicon-remove"></i></a>
                </div>
            </div>         <?php
$tarih = date("Y-m-d");
$tarih2 = date("Y-m-d");
			if(isset($_POST['date'])){
$tarih = $_POST['date'];
$tarih2 = $_POST['date2'];
}else if (isset($_SESSION['tarih'])){ 
 
 $tarih = $_SESSION['tarih'];
 $tarih2 = $_SESSION['tarih2'];

 }else{
$tarih = date("Y-m-d");
$tarih2 = date("Y-m-d");

 	
 } ?>
            <div class="box-content">

                <form name="form" method="post" role="form">
				
     	
    <div class="form-group">
    



				           <div class="form-group has-success col-md-4">
                        <input type="date" class="form-control" value="<?php echo $tarih ; ?>" name="date" >
                    </div>
				
				          <div class="form-group has-success col-md-4">
                        <input type="date" class="form-control" value="<?php echo $tarih2; ?>" name="date2" >
                    </div>

                   
               
                   &nbsp&nbsp&nbsp <button type="submit" class="btn btn-default"> &nbsp&nbsp&nbsp Raporla &nbsp&nbsp&nbsp</button>
                </form>

            </div>
        </div>
  
    <!--/span-->
<?php if ($_POST){
    $ara=$_POST["date"];
	$araa=$_POST["date2"];
	$say=$_POST["say"]; 
	
	
	?>
	
</div><!--/row-->

<div class="row">
        <div class="box col-md-12">
            <div class="box-inner">
                <div class="box-header well" data-original-title="">
                    <h2><i class="glyphicon glyphicon-user"></i> Kar Zarar Raporu</h2>

                    <div class="box-icon">
                        <a href="#" class="btn btn-minimize btn-round btn-default"><i
                                class="glyphicon glyphicon-chevron-up"></i></a>
                        <a href="#" class="btn btn-close btn-round btn-default"><i
                                class="glyphicon glyphicon-remove"></i></a>
                    </div>
                </div>
                <div class="box-content">
                    <table class="table table-striped table-bordered responsive">
                        <thead>
                        <tr>
                            <th>Toplam Satış ( Kdv Dahil )</th>
                            <th>Toplam Satış ( Kdv Hariç )</th>
                      
                            
                            <th>Kar Oranı ( % )</th>
                         
                            
                    
                        </tr>
                        </thead>
						

<?php



	if(empty($ara) && empty($araa)) {
      echo '    <div class="alert alert-danger">
                    <button type="button" class="close" data-dismiss="alert">&times;</button>
                    <strong>Hata</strong> Lütfen Boş Bırakmayınız
                </div>';
	 
   } else {
	
	$tarih = date("Y-m-d"); 

function hesapla($x,$y){
    $result=0;
    if($x>$y){
        $result=(($x-$y)/$y)*100;
    }else{
        $result=(($y-$x)/$x)*100;
    }
    return $result;
}

$sql_checks = odbc_exec($conn," SELECT 
      
   sum ([maliyet]) Maliyet
  FROM [online_ciro].[dbo].[belge_detay] where tarih between '$ara' and '$araa'  ");
 while($satirs = odbc_fetch_array($sql_checks)) {

	 $gmaliyetgs += $satirs["Maliyet"];
 }

	$sql_check = odbc_exec($db,"
SELECT * FROM (
SELECT
FL.Id, BelgeTipi, FL.BelgeNo, FL.Tarih, FL.KasaId, KDVDahil, FL.SubeId, CariId, C.Kodu AS CariKodu, C.Adi AS CariAdi, C.ParaBirimi,
Unvani, C.OzelKod1 AS CariOzelKod1, C.OzelKod2 AS CariOzelKod2, C.OzelKod3 AS CariOzelKod3, C.Durum AS CariDurum, UlkeId,
Telefon1, BaslikKur, FL.Aciklama, BaslikIskonto1, BaslikIskonto2, BaslikOzelKod1Id, BaslikOzelKod2Id,
Toplam, ToplamIskonto, ToplamIskontoOrani, AraToplam, KDVToplam, OTVToplam, TevkifatToplam, KDVToplam + OTVToplam AS ToplamVergi,
CASE WHEN AraToplam = 0 THEN 0 ELSE (KDVToplam + OTVToplam) * 100 / AraToplam END AS OrtalamaVergiOrani,
GenelToplam, VadeGun, Vade, FL.KullaniciId, FL.OlusturmaTarihi, FL.ToplamMiktar,
I.FaturaId AS IrsFatId
,(SELECT SUM(AlisFiyati * Miktar * Carpan) FROM StokHareketleriIrs AS SH
INNER JOIN Stoklar AS S ON S.Id = SH.StokId WHERE SH.BaslikId = FL.Id) AS Maliyet
FROM FaturaListesi FL
LEFT JOIN Cariler C ON C.Id = FL.CariId
LEFT JOIN Irsaliyeler AS I ON I.IrsaliyeId = FL.Id AND FL.BelgeTipi IN (32,33, 49,50,56)
WHERE FL.BelgeTipi IN (10,12,14,21,23,25,33,35,36,37,48, 11,13,29,38, 50,52,53,56,59) 
) AS R WHERE 1=1 AND Tarih BETWEEN { ts '$ara 00:00:00.000'} AND { ts '$araa 23:59:59.998'} AND BelgeTipi IN (52,53,56) ORDER BY Tarih, Id
  ");

 while($satir = odbc_fetch_array($sql_check)) { 

if ($satir["BelgeTipi"]=='53'){

$gider += $satir["GenelToplam"];
$agider += $satir["AraToplam"];
$mgider += $satir["Maliyet"];
}	

$adi = $satir['CariAdi'];
$toplam += $satir["AraToplam"];
$geneltoplam += $satir["GenelToplam"];
$maliyet += $satir["Maliyet"];

$geneltop = $geneltoplam - $gider - $gider  ;
$aratoplam = $toplam - $agider - $agider ;
$gmaliyetg = $maliyet - $mgider - $mgider ;

}     

 $gtop = hesapla($aratoplam,$gmaliyetgs);

 $gtop = $gtop ;
 
  if ($ara == '2024-01-01'){ 
 $geneldevir = "275008.74";
 $gdevir = "266995.29";
 $dvirtop = "221995.29";
 $gdevirs = $gdevir;
 
  $gtopa = hesapla($gdevirs,$dvirtop);
 $gtops = $gtopa ;
 
  } else{
	  
	 $gtops =$gtop ;  
  }
  $geneltop =$geneltop + $geneldevir;
  $aratoplam =$gdevir + $aratoplam;
 
  
 // $gider = number_format($gider, 2, '.', ',');
  //$kredi_toplam = number_format($kredi_top, 2, ',', '.');
  $gtopt = number_format($gtops, 2, ',', '.');
  $geneltoplam = number_format($geneltop, 2, ',', '.');
  $gtoplam = number_format($aratoplam, 2, ',', '.'); 
  $gmaliyet = number_format($gmaliyetg, 2, ',', '.'); 
  

?>

                        <tbody>
                        <tr>
                          
                            <td class="center"><b><?php echo $geneltoplam .'  '.'₺'; ?> </td>
                            <td class="center"><b><?php echo $gtoplam .'  '.'₺'; ?> </td>

                            <td class="center"><?php echo '%'.$gtopt ; ?></td>
                       
                            
                        </tr>
					
 <?php } } ?>          
                      
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <!--/span-->


<?php require('footer.php'); ?>