<?
include_once('includes/top.php');
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
$fin_year=$_SESSION['fin_year'];
//$br=$_SESSION['branch'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}
?>
<title>Kerala Khadi and Village Industries Board</title>
<link rel="stylesheet" href="style.css" type="text/css" />
<script type="text/javascript" src="scripts/jquery-latest.js"></script>
<link type="text/css" href="includes/style1.css" rel="stylesheet" />
<link rel="stylesheet" href="css/form.css" type="text/css" />
<script type="text/javascript">
        $(function() {
          if ($.browser.msie && $.browser.version.substr(0,1)<7)
          {
			$('li').has('ul').mouseover(function(){
				$(this).children('ul').show();
				}).mouseout(function(){
				$(this).children('ul').hide();
				})
          }
        });
</script>
<style>
#txt{
font-family: Arial, Helvetica;
background-color:#F2F2F2;
}
th{background-color:steelblue;color:white}
table.form{
font-weight: normal;border:1px;
}
</style>
<body>
<font style="font-family: 'Trebuchet MS',Verdana,Helvetica,sans-serif; font-size:15px; color:white;"></font>
<form action='<?print $PHP_SELF?>' name="f1" method='post' >
<center>
<table cellspacing=1 cellpadding=1 bgcolor='steelblue'>
<tr><td bgcolor='white' align=center>
<table class=noprint>
<?
$new_year1 = '20'.(substr($fin_year,2,2)+1).'-20'.(substr($fin_year,-2)+1);
$sv_year='20'.(substr($fin_year,2,2)+1).'-'.(substr($fin_year,-2)+1);
?>
<tr><br><br><br><br><br><br><br><td colspan=2 align=center bgcolor='red'><b>Generate New Financial Year >>> <?print $new_year1?>
<input type='submit' name='proceed' value='Proceed' >
</table>
</td></tr>
</table>
</form>

<?php
$targetpage = "eoy.php";
$date=date('Y-m-d');
//$proceed = $_REQUEST['proceed'];
//print "aaaaaxx".$proceed."asdasdas";
if(isset($_POST['proceed']))
{
    $new_year = 'khadidb'.(substr($fin_year,2,2)+1).(substr($fin_year,-2)+1);
    if($db=mysqli_select_db("$new_year"))
    {
        print "<script> alert('Financial Year ($new_year1) Already Generated !!!'); document.location='eoy.php' </script>";
        return;
    }
    if($fin_year=='2015-16' || strlen($fin_year)<=0)
        $fin_year1 = "khadidb";
    else
        $fin_year1 = "khadidb".substr($fin_year,2,2).substr($fin_year,-2);

    $sql1 ="create database ".$new_year;
    mysqli_query($db,$sql1)or die(mysqli_error($db));
    print "00==".$sql1.".....Successful....!!!<br>";
    $sql[1]="create table $new_year.acperiod select * from ".$fin_year1.".acperiod";
    $sql[2]="create table $new_year.acgroups select * from ".$fin_year1.".acgroups";
    $sql[3]="create table $new_year.adjstock select * from ".$fin_year1.".adjstock limit 0";
    $sql[4]="create table $new_year.bankmaster select * from ".$fin_year1.".bankmaster";
    $sql[5]="create table $new_year.bnkpay select * from ".$fin_year1.".bnkpay limit 0";
    $sql[6]="create table $new_year.bnkrec select * from ".$fin_year1.".bnkrec limit 0";
    $sql[7]="create table $new_year.branch select * from ".$fin_year1.".branch";
    $sql[8]="create table $new_year.breco select * from ".$fin_year1.".breco limit 0";
    $sql[9]="create table $new_year.brtransferin select * from ".$fin_year1.".brtransferin limit 0";
    $sql[10]="create table $new_year.brtransferout select * from ".$fin_year1.".brtransferout limit 0";
    $sql[11]="create table $new_year.bruser select * from ".$fin_year1.".bruser";
    $sql[12]="create table $new_year.chequecollection select * from ".$fin_year1.".chequecollection limit 0";    
    $sql[13]="create table $new_year.corp_master select * from ".$fin_year1.".corp_master";
    $sql[14]="create table $new_year.cshpay select * from ".$fin_year1.".cshpay limit 0";
    $sql[15]="create table $new_year.subgroupmaster select * from ".$fin_year1.".subgroupmaster";
    $sql[16]="create table $new_year.sup_master select * from ".$fin_year1.".sup_master";
    $sql[17]="create table $new_year.cshrec select * from ".$fin_year1.".cshrec limit 0";
    $sql[18]="create table $new_year.customer select * from ".$fin_year1.".customer";
    $sql[19]="create table $new_year.dbnote select * from ".$fin_year1.".dbnote  limit 0";
    $sql[20]="create table $new_year.deliverychallanin select * from ".$fin_year1.".deliverychallanin limit 0";
    $sql[21]="create table $new_year.deliverychallanout select * from ".$fin_year1.".deliverychallanout  limit 0";
    $sql[22]="create table $new_year.desigmast select * from ".$fin_year1.".desigmast";
    $sql[23]="create table $new_year.down_payment select * from ".$fin_year1.".down_payment";
    $sql[24]="create table $new_year.empmaster select * from ".$fin_year1.".empmaster";
    $sql[25]="create table $new_year.groupmaster select * from ".$fin_year1.".groupmaster";
    $sql[26]="create table $new_year.itempurchase select * from ".$fin_year1.".itempurchase  limit 0";
    $sql[27]="create table $new_year.journal2 select * from ".$fin_year1.".journal2  limit 0";
    $sql[28]="create table $new_year.khadistaff select * from ".$fin_year1.".khadistaff";
    $sql[29]="create table $new_year.ledger select * from ".$fin_year1.".ledger";
    $sql[30]="create table $new_year.log select * from ".$fin_year1.".log  limit 0";
    $sql[31]="create table $new_year.partmaster select * from ".$fin_year1.".partmaster";
    $sql[32]="create table $new_year.payment_mode select * from ".$fin_year1.".payment_mode";
    $sql[33]="create table $new_year.prcsunit select * from ".$fin_year1.".prcsunit";
    $sql[34]="create table $new_year.prodinvoicewholesale select * from ".$fin_year1.".prodinvoicewholesale  limit 0";
    $sql[35]="create table $new_year.prod_invoice select * from ".$fin_year1.".prod_invoice  limit 0";
    $sql[36]="create table $new_year.purchretn select * from ".$fin_year1.".purchretn  limit 0";
    $sql[37]="create table $new_year.qtytransf select * from ".$fin_year1.".qtytransf  limit 0";
    $sql[38]="create table $new_year.salesretn select * from ".$fin_year1.".salesretn  limit 0";
    $sql[39]="create table $new_year.salesrtnwhole select * from ".$fin_year1.".salesrtnwhole  limit 0";
    $sql[40]="create table $new_year.spareprmast select * from ".$fin_year1.".spareprmast";
    $sql[41]="create table $new_year.sparestock select * from ".$fin_year1.".sparestock";  
    $sql[42]="create table $new_year.taxmaster select * from ".$fin_year1.".taxmaster";
    $sql[43]="create table $new_year.unit select * from ".$fin_year1.".unit";
    $macfrom = '20'.(substr($fin_year,2,2)+1).'-04-01';
    $macto = '20'.(substr($fin_year,2,2)+2).'-03-31';
    $sql[44]="update $new_year.acperiod set acfrom = '$macfrom',acto = '$macto'";
    for ($i = 1; $i <= 44; $i++)
    {
    	mysqli_query($db,$sql[$i])or die(mysqli_error($db));
    	print $i."==".$sql[$i].".....Successful....!!!<br>";
    }
    echo "Completed Successfully!";
 mysqli_query($db,"update $new_year.sparestock set opstk = 0");
$res=mysqli_query($db,"select * from $fin_year1.stockverification order by itemcode");

while($res1=mysqli_fetch_array($res))
{
       $mopstk = $res1[5];
       $itemcode = $res1[1];
       $gname=$res1[7];
       $sql = "update $new_year.sparestock set opstk = $mopstk,item_grp='$gname' where itemcode = '".$itemcode."'";
       mysqli_query($db,$sql)or die(mysqli_error($db));
       print $sql."<br>";
}
    print "Stock updated successfully!";
    $re=mysqli_query($db,"select op,drcr,led from $fin_year1.ledger where brcode = '$brcode'");
    while($row=mysqli_fetch_array($re))
    {
        $re1=mysqli_query($db,"select sum(dramt),sum(cramt) from $fin_year1.journal2 where partic = '$row[2]' and brcode = '$brcode'")or die(mysqli_error($db));
        $row1=mysqli_fetch_array($re1);
        if($row[1]=='Dr')
            $op_amt = $row[0]+($row1[0]-$row1[1]);
        else
            $op_amt = (-1*$row[0])+($row1[0]-$row1[1]);
	    mysqli_query($db,"update $new_year.ledger set op=$op_amt where $new_year.led ='$row[2]' and brcode='$brcode'");
        print "update $new_year.ledger set op=$op_amt where led ='$row[2]' and brcode='$brcode'";
    }
    $re=mysqli_query($db,"select acgroups.gname,ledger.led,ledger.code from $fin_year1.ledger,$fin_year1.acgroups where acgroups.gname = ledger.grp and (acgroups.nature = 'Expenses' or acgroups.nature = 'Income' ) and brcode = '$brcode'");
    while($row=mysqli_fetch_array($re))
    {
        mysqli_query($db,"update $new_year.ledger set op=0 where led ='$row[1]' and brcode='$brcode'");
        print "update $new_year.ledger set op=0 where led ='$row[1]' and brcode='$brcode'";
        }
    print "Ledgers updated successfully!";
$query="update khadidb.branch set brpass='$sv_year'";
mysqli_query($db,$query);
    print "<script> alert('New Financial Year ($new_year1) Generated Successfully !!!'); document.location='eoy.php' </script>";
}
?>
