<?php

error_reporting(0);
include("../../include/db.php");


function get_invoice_function1($invoice_nos,$acc_year)
{
$sql_query="select MAX(invoice_no) AS `invoice_no` from fusing_production_main where billing_type='Estimate' and academic_year='$acc_year'  ";
$rs=mysql_query($sql_query);
 while($rsdata=mysql_fetch_object($rs))
 {
 $invoice_no=$rsdata->invoice_no;
 } 
return $invoice_no;
}

function get_invoice_function2($invoice_nos,$acc_year)
{
$sql_query11="select MAX(invoice_no) AS `invoice_no` from printer_production_main where billing_type='Estimate' and academic_year='$acc_year' ";
$rs11=mysql_query($sql_query11);
while($rsdata11=mysql_fetch_object($rs11))
{
$invoice_no=$rsdata11->invoice_no;
} 
return $invoice_no;
}


function get_invoice_function3($invoice_nos,$acc_year)
{
$sql_query="select MAX(invoice_no) AS `invoice_no` from fusing_production_main where billing_type='Billing' and academic_year='$acc_year'  ";
$rs=mysql_query($sql_query);
while($rsdata=mysql_fetch_object($rs))
{
$invoice_no=$rsdata->invoice_no;
} 
return $invoice_no;
}



function get_invoice_function4($invoice_nos,$acc_year)
{
 $sql_query11="select MAX(invoice_no) AS `invoice_no` from printer_production_main where billing_type='Billing' and academic_year='$acc_year' ";
$rs11=mysql_query($sql_query11);
 while($rsdata11=mysql_fetch_object($rs11))
 {
 $invoice_no=$rsdata11->invoice_no;
 } 
return $invoice_no;
}




function get_invoice_function7($invoice_nos,$acc_year)
{
$sql_query="select MAX(invoice_no) AS `invoice_no` from fusing_production_main where billing_type='Re Production Mistake' and academic_year='$acc_year'  ";
$rs=mysql_query($sql_query);
while($rsdata=mysql_fetch_object($rs))
{
$invoice_no=$rsdata->invoice_no;
} 
return $invoice_no;
}



function get_invoice_function8($invoice_nos,$acc_year)
{
 $sql_query11="select MAX(invoice_no) AS `invoice_no` from printer_production_main where billing_type='Re Production Mistake' and academic_year='$acc_year' ";
$rs11=mysql_query($sql_query11);
 while($rsdata11=mysql_fetch_object($rs11))
 {
 $invoice_no=$rsdata11->invoice_no;
 } 
return $invoice_no;
}


function get_invoice_function5($invoice_nos,$acc_year)
{
$sql_query="select MAX(invoice_no) AS `invoice_no` from fusing_production_main where billing_type='Proforma Invoice' and academic_year='$acc_year'  ";
$rs=mysql_query($sql_query);
while($rsdata=mysql_fetch_object($rs))
{
$invoice_no=$rsdata->invoice_no;
} 
return $invoice_no;
}



function get_invoice_function6($invoice_nos,$acc_year)
{
 $sql_query11="select MAX(invoice_no) AS `invoice_no` from printer_production_main where billing_type='Proforma Invoice' and academic_year='$acc_year' ";
$rs11=mysql_query($sql_query11);
 while($rsdata11=mysql_fetch_object($rs11))
 {
 $invoice_no=$rsdata11->invoice_no;
 } 
return $invoice_no;
}


if($_POST['action']=="bill_conform")
{
	$remarks=$_POST['remarks'];
	$description=$_POST['description'];
    $accounting_year = date('Y');
	
$create_date=date('Y-m-d');
$explodes=explode("-",$create_date);
$year1=$explodes[0];
$year2=$explodes[1];
$year=$year1."-".$year2;


$year = date('Y', strtotime($create_date));
$month = date('m', strtotime($create_date));

// If the month is April or later, use the current year +1 for the accounting year
// Otherwise, use the current year
$acc_year = ($month >= 4) ? ($year + 1) : $year;








if($_POST['billing_type']=='Estimate')
{
	 $a=get_invoice_function1($invoice_nos,$acc_year);	
	 $b=get_invoice_function2($invoice_nos,$acc_year);
	 
	if($a>$b)
	{ 
		 if($a==0)
		 {
		 $values=1; 
		 }
		 if($a!=0)
		 {
		 $values=$a+1;
		 }
	}
	else
	{
		 if($b==0)
		 {
		 $values=1; 
		 }
		 if($b!=0)
		 {
		 $values=$b+1;
		 }
	}		
}


if($_POST['billing_type']=='Billing')
{
	 $c=get_invoice_function3($invoice_nos,$acc_year);	
	 $d=get_invoice_function4($invoice_nos,$acc_year);
	 
	if($c>$d)
	{ 
		 if($c==0)
		 {
		 $values=1; 
		 }
		 if($c!=0)
		 {
		 $values=$c+1;
		 }
	}
	else
	{
		 if($d==0)
		 {
		 $values=1; 
		 }
		 if($d!=0)
		 {
		 $values=$d+1;
		 }
	}		
}


if($_POST['billing_type']=='Proforma Invoice')
{
	 $e=get_invoice_function5($invoice_nos,$acc_year);	
	 $f=get_invoice_function6($invoice_nos,$acc_year);
	 
	if($e>$f)
	{ 
		 if($e==0)
		 {
		 $values=1; 
		 }
		 if($e!=0)
		 {
		 $values=$e+1;
		 }
	}
	else
	{
		 if($f==0)
		 {
		 $values=1; 
		 }
		 if($f!=0)
		 {
		 $values=$f+1;
		 }
	}		
}


if($_POST['billing_type']=='Re Production Mistake')
{
	 $g=get_invoice_function7($invoice_nos,$acc_year);	
	 $h=get_invoice_function8($invoice_nos,$acc_year);
	 
	if($g>$h)
	{ 
		 if($g==0)
		 {
		 $values=1; 
		 }
		 if($g!=0)
		 {
		 $values=$g+1;
		 }
	}
	else
	{
		 if($h==0)
		 {
		 $values=1; 
		 }
		 if($h!=0)
		 {
		 $values=$h+1;
		 }
	}		
}


$jobcard_number = $_POST['jobcard_number'];

  $sql="select * from fusing_jobcard_sub where jobcard_number='$_POST[jobcard_number]' and acc_year='$acc_year' order by id DESC ";
  $rs=mysql_query($sql);
  $rscount=mysql_num_rows($rs);
  $s_no=0;
  while($rsdata=mysql_fetch_array($rs))
  {
	   $jobcard_no=$rsdata['jobcard_no'];
       $id=$rsdata['id'];
	   $jobcard_number=$rsdata['jobcard_number'];
	   
 if($_POST['refer']=='Fusing')
	 {
 $update12="update fusing_production_main set billed_status='1',bill_date='$_POST[entry_date]',academic_year='$acc_year',billing_type='$_POST[billing_type]',frieght_charges='$_POST[frieght]',tax_type='$_POST[tax_type]',payment_terms='$_POST[payment_terms]',remarks='$_POST[remarks]',hsn_code='$_POST[hsn_code]',description='$_POST[description]',jobcard_number='$_POST[jobcard_number]' where job_card_no='$jobcard_no'";
	 mysql_query($update12);
	 
	   $update_fusing=" update fusing_jobcard_sub set invoice_no='$values',billing_type='$_POST[billing_type]' where jobcard_no='$jobcard_no' and acc_year='$acc_year' ";
	   mysql_query($update_fusing);
	 
	 
	   $update121="update fusing_production_main set invoice_no='$values' where job_card_no='$jobcard_no' and academic_year='$acc_year' and billing_type='$_POST[billing_type]'";
	   mysql_query($update121);
	 


	 }
	 else
	 {  
  $update1="update printer_production_main set billed_status='1',bill_date='$_POST[entry_date]',academic_year='$acc_year',billing_type='$_POST[billing_type]',frieght_charges='$_POST[frieght]',tax_type='$_POST[tax_type]',payment_terms='$_POST[payment_terms]',remarks='$_POST[remarks]',hsn_code='$_POST[hsn_code]',description='$_POST[description]',jobcard_number='$_POST[jobcard_number]' where job_card_no='$jobcard_no'";
	  mysql_query($update1); 
	  
	    $update_fusing=" update fusing_jobcard_sub set invoice_no='$values',billing_type='$_POST[billing_type]' where jobcard_no='$jobcard_no' and acc_year='$acc_year' ";
	   mysql_query($update_fusing);
	 
	   $update121="update printer_production_main set invoice_no='$values' where job_card_no='$jobcard_no' and academic_year='$acc_year' and billing_type='$_POST[billing_type]'";
	   mysql_query($update121);
	 } 
}	
}


function get_check_item_jobcard_sub($jobcard_number2,$acc_year)
{
  $sql_query="select * from fusing_jobcard_sub where jobcard_no='$jobcard_number2' and acc_year='$acc_year' ";
  $rs=mysql_query($sql_query);
  $rscount=mysql_num_rows($rs);
	return $rscount;
 }
 
if($_POST['action']=='add_jobcard_sub')
{
$create_date=date('Y-m-d');
$explodes=explode("-",$create_date);
$year1=$explodes[0];
$year2=$explodes[1];
$year=$year1."-".$year2;

$year = date('Y', strtotime($create_date));
$month = date('m', strtotime($create_date));


$acc_year = ($month >= 4) ? ($year + 1) : $year;

	$check_item_jobcard = get_check_item_jobcard_sub($_POST['jobcard_no_billing'],$acc_year);
	if($check_item_jobcard==0)
	{
	$create_date = date('Y-m-d');
	$Insql2=mysql_query("insert into fusing_jobcard_sub(jobcard_no,created_date,jobcard_number,acc_year)values('$_POST[jobcard_no_billing]','$create_date','$_POST[jobcard_number]','$acc_year')");
	}
	include("jobcard_sublist.php");
}

if($_POST['action']=='delete_jobcard_sub')
{
$delete3="delete from fusing_jobcard_sub  where id='$_POST[id]' and jobcard_no='$_POST[jobcard_no]'";
mysql_query($delete3);
include("jobcard_sublist.php");
}
?>