<?php

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

 $jobcard_no     = $_POST['jobcard_no']."<br>";
 $job_card_no    = $_POST['job_card_no']."<br>";
 $entry_date     = $_POST['entry_date']."<br>";
 $refer          = $_POST['refer']."<br>";
 $billing_type   = $_POST['billing_type']."<br>";
 $p_id           = $_POST['p_id']."<br>";          // ✔️ Here is your p_id
 $invoice_no     = $_POST['invoice_no']."<br>";
 $values         = $invoice_no;
 $frieght        = $_POST['frieght']."<br>";
 $payment_terms  = $_POST['payment_terms']."<br>";
 $tax_type       = $_POST['tax_type']."<br>";
 $remarks        = $_POST['remarks']."<br>";
 $hsn_code       = $_POST['hsn_code']."<br>";
 $description    = $_POST['description']."<br>";
 $jobcard_number = $_POST['jobcard_number']."<br>";
 $jobcard_no_billing = $_POST['jobcard_no_billing']."<br>";
 $jobcard_number1 = $_POST['jobcard_number1']."<br>";
// $action         = $_POST['action'];        // always "bill_conform"

// exit();


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;


$jobcard_number = $_POST['jobcard_number1'];

  $sql="select * from fusing_jobcard_sub where 
  jobcard_number='$_POST[jobcard_number1]' 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 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_number1]' where job_card_no='$jobcard_no' and billed_status='1'";

	 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' ";
	 
	 
	   $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 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_number1]' where job_card_no='$jobcard_no' and billed_status='1'";
	  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_1')
{

	$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,invoice_no)values('$_POST[jobcard_no_billing]','$_POST[bill_date]','$_POST[jobcard_number1]','$_POST[acc_year]','$_POST[invoice_no]')");

	// echo "insert into fusing_jobcard_sub(jobcard_no,created_date,jobcard_number,acc_year,invoice_no)values('$_POST[jobcard_no_billing]','$_POST[bill_date]','$_POST[jobcard_number1]','$_POST[acc_year]','$_POST[invoice_no]')";
	}
	include("jobcard_sublist.php");
}

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