<?php 
session_start();
ob_start(); 
include "header.php";
//$objIms->Semifinisheditem_alter();
?>
<script type="text/javascript">

window.addEventListener("keyup", function(e){ if(e.keyCode == 27) history.back(); }, false);

</script>
<style type="text/css">
    
        tr:hover {background-color:#bcd1cd;}

</style>
      <div class="page-wrapper">
            <!-- Container fluid  -->
            <div class="container-fluid">
                <!-- Start Page Content -->
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-body" id="printableArea">
 

<form method="post">
        <div class="panel">
            <div class="panel-heading" style="font-style: bold; font-size: 20px">Ledger Monthly Summary </div>
                <p>&nbsp;</p>
                
                    <table width="100%" class="table table-bordered">
                     <tr height="32" align="left">
                        <th nowrap="nowrap" width="40%" rowspan="2">Particular</th>
                        <th nowrap="nowrap" width="17%" colspan="2" style="text-align: center;">Inwards</th>
                        <th nowrap="nowrap" width="15%" colspan="2" style="text-align: center;">Outwards</th>
                        <th nowrap="nowrap" width="15%" colspan="2" style="text-align: center;">Closing Balance</th>
                    </tr>
                    <tr>
                        
                        <th nowrap="nowrap" width="10%" colspan="1">Qty</th>
                        <th nowrap="nowrap" width="10%" colspan="1">Val</th>
                        <th nowrap="nowrap" width="10%" colspan="1">Qty</th>
                        <th nowrap="nowrap" width="10%" colspan="1">Val</th>
                        <th nowrap="nowrap" width="10%" colspan="1">Qty</th>
                        <th nowrap="nowrap" width="10%" colspan="1">Val</th>
                    </tr>
					 <tr height="32">
                        <td nowrap="nowrap" width="6%">Opening Balance</td>
                        <td nowrap="nowrap" width="6%"></td>
                        <td nowrap="nowrap" width="6%"></td>
                        <td nowrap="nowrap" width="6%"></td>
                        <td nowrap="nowrap" width="6%"></td>
                        <td nowrap="nowrap" width="6%"></td>
                        <td nowrap="nowrap" width="6%"></td>
                    </tr>
<?php
    $cur_fy = '01-04-'.date('Y');
 $next_fy ='31-03-'.(date('Y')+1);
 

$start    = new DateTime($cur_fy);
$start->modify('first day of this month');
$end      = new DateTime($next_fy);
$end->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$period   = new DatePeriod($start, $interval, $end);
$P_tot=0;
$p_T_pric=0;
$tot_sell_q=0;
$s_tot_price_c=0;
$remain_q=0;
$remain_tot_p=0;
foreach ($period as $dt) {
     $dateString= $dt->format("d-m-Y");
	$mn = explode('-',$dateString );
	 $lastDateOfMonth = date("t-m-Y", strtotime($dateString));
	$month_name = date('F', mktime(0, 0, 0, $mn[1], 1)).'<br>';

    $startdate=$dateString;
    $enddate=$lastDateOfMonth;
	
	
  $sdt = date('d-m-Y', strtotime("$startdate"));
  $s_date=$objIms->DateToInt($sdt);
  $edt = date('d-m-Y', strtotime("$enddate"));
  $end_date=$objIms->DateToInt($edt);
                   
 $sql_viewtmp="SELECT * from tbl_purchase_details where session_login_id = '".$_SESSION['login_id']."' and purmas_id in(select id from tbl_purchase_master where stampdate>='$s_date' and stampdate<='$end_date' and session_login_id = '".$_SESSION['login_id']."') group by item_id,pur_price,sell_price ";
 $res_viewtmp=mysql_query($sql_viewtmp);

 $ret_qty=0;
$remaing=0;
$remaing_tot=0;
    while($rows=mysql_fetch_assoc($res_viewtmp))
                    {

                    $item_id=$rows["item_id"];  
                    $unit_id=$rows["unit_id"];  
                    $pur_qty=$rows["pur_qty"]; $P_totqty+=$pur_qty;    
                    $pur_price=$rows["pur_price"]; $p_tot_pri+=$pur_price;  
                    $cgst=$rows["cgst"];
                    $sgst=$rows["sgst"];
                    $igst=$rowss["igst"];
                    $sell_price=$rows["sell_price"]; $s_tot_price+=$sell_price;  

  $sql_viewret="SELECT * , sum(ret_qty) as ret_qty from view_purchasereturn_det where session_login_id = '".$_SESSION['login_id']."' and item_id='$item_id' and pur_price='$pur_price' and sell_price='$sell_price' and purretmas_id in(select purretmas_id from view_purret_mstrdet where purchasemstr_id in(select purmas_id from view_purchase_det where session_login_id = '".$_SESSION['login_id']."')) group by item_id,pur_price,sell_price ";
 $res_viewret=mysql_query($sql_viewret);  
 $ret_view=mysql_fetch_assoc($res_viewret);
                    $ret_qty=$ret_view["ret_qty"];     
                    $pur_remain=$pur_qty-$ret_qty;
                    $ret_qty;

 $sql_sl="SELECT * from view_sales_detail where session_login_id = '".$_SESSION['login_id']."' and item_id='$item_id' and unit_price ='$sell_price' and salesmas_id in (select id from tbl_sales where session_login_id = '".$_SESSION['login_id']."') group by item_id,unit_price";
                $res_sl=mysql_query($sql_sl);
                $row_count=mysql_fetch_assoc($res_sl);
                   
                 $sel_qty=$row_count["qty"]; $tot_sell+=$sel_qty;

 $sql_sl_ret="SELECT * , sum(ret_qty) as ret_qty from tbl_sales_return_details where session_login_id = '".$_SESSION['login_id']."' and item_id='$item_id' and sell_price ='$sell_price' and salesretmas_id in (select id from tbl_sales_return_mstr  where salesmas_id in(select id from tbl_sales where session_login_id = '".$_SESSION['login_id']."')) group by item_id,sell_price";
                $res_sl_ret=mysql_query($sql_sl_ret);
                $row_sl_ret=mysql_fetch_assoc($res_sl_ret);
                   
                 $sel_ret_qty=$row_sl_ret["ret_qty"];  

    $cgst_t=($pur_price*$cgst)/100;$c_tot+=$cgst_t;
    $sgst_t=($pur_price*$sgst)/100;$s_tot+=$sgst_t;
    $igst_t=($pur_price*$igst)/100;$i_tot+=$igst_t;
    $p_tot_gst=$pur_price+$c_tot+$s_tot+$i_tot; $p_r_tot+=$p_tot_gst;
                 $remaing=$pur_remain-$sel_qty;$remain+=$remaing;    
                 $remaing_tot=$remaing*$p_r_tot;$remain_tot+=$remaing_tot;  

  $sql_item="SELECT * from tbl_item where id='$item_id' and session_login_id = '".$_SESSION['login_id']."'";
  $res_item=mysql_query($sql_item);
  $row_item=mysql_fetch_assoc($res_item);
  $item_name1=$row_item["item_name"];             
         


 ?>


                   
                <tr height="32">
                        <td nowrap="nowrap" width="6%"><?php echo $month_name; ?></td>
                        <td nowrap="nowrap" width="6%"><?php echo $P_totqty; ?></td>
                        <td nowrap="nowrap" width="6%"><?php echo $p_tot_pri; ?></td>
                        <td nowrap="nowrap" width="6%"><?php echo $tot_sell; ?></td>
                        <td nowrap="nowrap" width="6%"><?php echo $s_tot_price; ?></td>
                        <td nowrap="nowrap" width="6%"><?php echo $remain; ?></td>
                        <td nowrap="nowrap" width="6%"><?php echo $remain_tot; ?></td>
                    </tr>
           
<?php

$P_tot+=$P_totqty;
$p_T_pric+=$p_tot_pri;
$tot_sell_q+=$tot_sell;
$s_tot_price_c+=$s_tot_price;
$remain_q+=$remain;
$remain_tot_p+=$remain_tot;
}}?>

      


</table>
<br /><br /><br /><br />
            <table class="table">
                    <tr height="32">
                        <td nowrap="nowrap" width="30%" style="font-weight: bold">Grand Total</td>
                        <td nowrap="nowrap" width="8%"><?php  echo $P_tot; ?></td>

                        <th nowrap="nowrap" width="9%"><?php echo $p_T_pric; ?></th>
                        <td nowrap="nowrap" width="6%"><?php  echo $tot_sell_q; ?></td>
                        <th nowrap="nowrap" width="9%"><?php  echo $s_tot_price_c; ?></th>
                        <td nowrap="nowrap" width="6%"><?php  echo $remain_q; ?></td>
                        <td nowrap="nowrap" width="6%"><?php  echo $remain_tot_p; ?></td>
                    </tr>
                </table
                ></div>
</form>
<?php
 
$dataPoints1 = array(
  array("label"=> "Apr", "y"=> $p_tot_pri),
  array("label"=> "May", "y"=> $p_tot_pri1),
  array("label"=> "Jun", "y"=> $p_tot_pri2),
  array("label"=> "Jul", "y"=> $p_tot_pri3),
  array("label"=> "Aug", "y"=> $p_tot_pri4),
  array("label"=> "Sep", "y"=> $p_tot_pri5),
  array("label"=> "Oct", "y"=> $p_tot_pri6),
  array("label"=> "Nov", "y"=> $p_tot_pri7),
  array("label"=> "Dec", "y"=> $p_tot_pri8),
  array("label"=> "Jan", "y"=> $p_tot_pri9),
  array("label"=> "Feb", "y"=> $p_tot_pri10),
  array("label"=> "Mar", "y"=> $p_tot_pri11)
);
$dataPoints2 = array(
  array("label"=> "Apr", "y"=> $s_tot_price),
  array("label"=> "May", "y"=> $s_tot_price1),
  array("label"=> "Jun", "y"=> $s_tot_price2),
  array("label"=> "Jul", "y"=> $s_tot_price3),
  array("label"=> "Aug", "y"=> $s_tot_price4),
  array("label"=> "Sep", "y"=> $s_tot_price5),
  array("label"=> "Oct", "y"=> $s_tot_price6),
  array("label"=> "Nov", "y"=> $s_tot_price7),
  array("label"=> "Dec", "y"=> $s_tot_price8),
  array("label"=> "Jan", "y"=> $s_tot_price9),
  array("label"=> "Feb", "y"=> $s_tot_price10),
  array("label"=> "Mar", "y"=> $s_tot_price11)
);
  
?>

<!--<div id="chartContainer" style="height: 370px; width: 100%;"></div>-->


                                         </div>
                        </div>
                    </div>
                </div>
                <!-- End PAge Content -->
            </div>
            <!-- End Container fluid  -->
      </div>
      
 <?php include 'footer.php' ?>

    
    <script src="../js/canvasjs.min.js"></script>
<script>
window.onload = function () {
 
var chart = new CanvasJS.Chart("chartContainer", {
  animationEnabled: true,
  theme: "light2",
  title:{
    text: "Stocks Item Monthly Summary"
  },
  legend:{
    cursor: "pointer",
    verticalAlign: "center",
    horizontalAlign: "right",
    itemclick: toggleDataSeries
  },
  data: [{
    type: "column",
    name: "Real Trees",
    indexLabel: "{y}",
    yValueFormatString: "#0.##",
    showInLegend: true,
    dataPoints: <?php echo json_encode($dataPoints1, JSON_NUMERIC_CHECK); ?>
  },{
    type: "column",
    name: "Artificial Trees",
    indexLabel: "{y}",
    yValueFormatString: "#0.##",
    showInLegend: true,
    dataPoints: <?php echo json_encode($dataPoints2, JSON_NUMERIC_CHECK); ?>
  }]
});
chart.render();
 
function toggleDataSeries(e){
  if (typeof(e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
    e.dataSeries.visible = false;
  }
  else{
    e.dataSeries.visible = true;
  }
  chart.render();
}
 
}
</script>