<?php
error_reporting(0);

include("../../include/db.php");
function get_jobcard_checkitem($jobcard_no)
{
	$Isql=mysql_query("select * from jobcard_main where jobcard_no='$jobcard_no'");
    $rscount=mysql_num_rows($Isql);
	return $rscount;
}

/***************************** Main Add***********************************************************/
if(($_POST['action']=="SUBMIT")) 
{
	
$jobcard_no=$_POST['jobcard_no'];
$entry_time=$_POST['entry_time'];
$customer_name1=$_POST['customer_name'];
$style_no=$_POST['style_no'];
$created_by=$_POST['created_by'];
$style_name=$_POST['style_name'];
$cust_po_no=$_POST['cust_po_no'];
$combo=$_POST['combo'];
$jobtype=$_POST['jobtype'];
$fabric_type=$_POST['fabric_type'];
$hsn_code=$_POST['hsn_code'];

$overal_tot=$_POST['overal_tot'];
$overal_width=$_POST['overal_width'];
$overal_height=$_POST['overal_height'];
$overal_qty=$_POST['overal_qty'];
$tax=$_POST['tax'];
$tax_amt=$_POST['tax_amt'];
$net_amt=$_POST['net_amt'];
$create_date1=date('Y-m-d');
$acadamic_yr=explode("-",$create_date1);
$acdmc_yr_res=$acadamic_yr[0];
$check_item_jobcard = get_jobcard_checkitem($jobcard_no);

$customer_name1 = trim(preg_replace('/\s+/', ' ', $customer_name1));

// Split into array
$a1 = explode(" ", $customer_name1);

// Check if last element is numeric
$last = end($a1);
if (is_numeric($last)) {
    $number = array_pop($a1); // remove numeric part
} else {
    $number = ''; // no number
}

// Join remaining parts as full name
$customer_name = implode(" ", $a1);

if($check_item_jobcard==0)
  
{
$Insql1=mysql_query("insert into jobcard_main(jobcard_no,entry_date,created_by,cust_name,style_no,style_name,cust_po_no,job_type,fabric_type,hsn_code,combo,overall_value,overal_width,overal_height,overal_qty,tax,tax_amt,net_total,academic_yr,createdate)values('$jobcard_no','$entry_time','$created_by','$customer_name','$style_no','$style_name','$cust_po_no','$jobtype','$fabric_type','$hsn_code','$combo','$overal_tot','$overal_width','$overal_height','$overal_qty','$tax','$tax_amt','$net_amt','$acdmc_yr_res','$create_date1')");


   $Upsql100="update jobcard_sublist set entry_time='$entry_time',created_by='$created_by',customer_name='$customer_name',cust_po_no='$cust_po_no',jobtype='$jobtype',fabric_type='$fabric_type',hsn_code='$hsn_code' where jobcard_no='$_POST[jobcard_no]'";
   mysql_query($Upsql100);
}
}
/***************************** Main Update***********************************************************/
if($_POST['action']=="UPDATE")
{
$edit_ids=$_POST['main_edit_id'];
$jobcard_no=$_POST['jobcard_no'];
$entry_time=$_POST['entry_time'];
$customer_name=$_POST['customer_name'];
$style_no=$_POST['style_no'];
$created_by=$_POST['created_by'];
$style_name=$_POST['style_name'];
$cust_po_no=$_POST['cust_po_no'];
$combo=$_POST['combo'];
$jobtype=$_POST['jobtype'];
$fabric_type=$_POST['fabric_type'];
$overal_tot=$_POST['overal_tot'];
$overal_width=$_POST['overal_width'];
$overal_height=$_POST['overal_height'];
$overal_qty=$_POST['overal_qty'];
$tax=$_POST['tax'];
$tax_amt=$_POST['tax_amt'];
$net_amt=$_POST['net_amt'];

$create_date1=date('Y-m-d');
$acadamic_yr=explode("-",$create_date1);
$acdmc_yr_res=$acadamic_yr[0];
  $Upsq12="update jobcard_main set jobcard_no='$jobcard_no',entry_date='$entry_time',created_by='$created_by',cust_name='$customer_name',style_no='$style_no',style_name='$style_name',cust_po_no='$cust_po_no',job_type='$jobtype',fabric_type='$fabric_type',combo='$combo',overall_value='$overal_tot',overal_width='$overal_width',overal_height='$overal_height',overal_qty='$overal_qty',tax='$tax',tax_amt='$tax_amt',net_total='$net_amt',academic_yr='$acdmc_yr_res',createdate='$create_date1' where main_id='$_POST[main_edit_id]'";
 mysql_query($Upsq12) or die(error);
  
  
     $Upsql1002="update jobcard_sublist set entry_time='$entry_time',created_by='$created_by',customer_name='$customer_name',cust_po_no='$cust_po_no',jobtype='$jobtype',fabric_type='$fabric_type' where jobcard_no='$_POST[jobcard_no]'";
   mysql_query($Upsql1002);
}
 /**********************************SUB ADD  QUERY ***************************************/
 if($_POST['action']=="ADD")
	{
	  $edit_id=$_POST['edit_id'];
	  $jobcard_no=$_POST['jobcard_no'];
      $entry_time=$_POST['entry_time'];

      $created_by=$_POST['created_by'];
      $cust_po_no=$_POST['cust_po_no'];
      $customer_name1=$_POST['customer_name'];
      $jobtype=$_POST['jobtype'];
      $fabric_type=$_POST['fabric_type'];
	  $hsn_code=$_POST['hsn_code'];
      $particulars=$_POST['particulars'];
      $width=$_POST['jobcard_width'];
	  $height=$_POST['jobcard_height'];
      $rate=$_POST['rate'];
      $amount=$_POST['amount'];
      $conformed_amt=$_POST['conformed_amt'];
      $conform_rate = $_POST['conform_rate'];
      $qty=$_POST['qty'];
      $values=$_POST['qty']*$_POST['conformed_amt'];
	  $quotation_no=$_POST['quotation_no'];
    $ratetrrrr=$_POST['ratetrrrr'];
      $create_date1=date('Y-m-d');
	  $acadamic_yr=explode("-",$create_date1);
      $acdmc_yr_res=$acadamic_yr[0];
	  
 $customer_name1 = trim(preg_replace('/\s+/', ' ', $customer_name1));

// Split into array
$a1 = explode(" ", $customer_name1);

// Check if last element is numeric
$last = end($a1);
if (is_numeric($last)) {
    $number = array_pop($a1); // remove numeric part
} else {
    $number = ''; // no number
}

// Join remaining parts as full name
$customer_name = implode(" ", $a1);

$Insql1=mysql_query("insert into jobcard_sublist(jobcard_no,entry_time,created_by,cust_po_no,customer_name,jobtype,fabric_type,hsn_code,particulars,width,height,rate,amount,conformed_amt,conform_rate,qty,jobcard_values,academic_yr,createdate,quotation_no,customer_rate)values('$jobcard_no','$entry_time','$created_by','$cust_po_no','$customer_name','$jobtype','$fabric_type','$hsn_code','$particulars','$width','$height','$rate','$amount','$conformed_amt','$conform_rate','$qty','$values','$acdmc_yr_res','$create_date1','$quotation_no','$ratetrrrr')");
include("jobcard_sublist.php");
}
 /**********************************SUB Edit  QUERY ***************************************/
	if($_POST['action']=="EDIT")
    {
      $edit_id=$_POST['edit_id'];
	  $jobcard_no=$_POST['jobcard_no'];
      $entry_time=$_POST['entry_time'];
      $created_by=$_POST['created_by'];
      $cust_po_no=$_POST['cust_po_no'];
      $customer_name1=$_POST['customer_name'];
      $jobtype=$_POST['jobtype'];
      $fabric_type=$_POST['fabric_type'];
      $particulars=$_POST['particulars'];
      $width=$_POST['jobcard_width'];
	  $height=$_POST['jobcard_height'];
      $rate=$_POST['rate'];
      $amount=$_POST['amount'];
      $conformed_amt=$_POST['conformed_amt'];
       $conform_rate=$_POST['conform_rate'];
      $qty=$_POST['qty'];
      $values=$_POST['qty']*$_POST['conformed_amt'];
	   $quotation_no=$_POST['quotation_no'];
     $ratetrrrr=$_POST['ratetrrrr'];
      $create_date1=date('Y-m-d');
	  $acadamic_yr=explode("-",$create_date1);
      $acdmc_yr_res=$acadamic_yr[0];
$customer_name1 = trim(preg_replace('/\s+/', ' ', $customer_name1));

// Split into array
$a1 = explode(" ", $customer_name1);

// Check if last element is numeric
$last = end($a1);
if (is_numeric($last)) {
    $number = array_pop($a1); // remove numeric part
} else {
    $number = ''; // no number
}

// Join remaining parts as full name
$customer_name = implode(" ", $a1);
   $Upsql1="update jobcard_sublist set jobcard_no='$jobcard_no',particulars='$particulars',
width='$width',height='$height',rate='$rate',	amount='$amount',conformed_amt='$conformed_amt',conform_rate='$conform_rate',qty='$qty',jobcard_values='$values',academic_yr='$acdmc_yr_res',createdate='$create_date1',quotation_no='$quotation_no', customer_rate='$ratetrrrr' where 	job_id='$_POST[edit_id]'";
   mysql_query($Upsql1);
	
  
include("jobcard_sublist.php");
}
/******************************************** SUB DELETE*****************************************************************/
if($_POST['action']=="delete_job_sub")
{ 		 
		      $delete1="delete from  jobcard_sublist where 	job_id='$_POST[job_id]'";
			 mysql_query($delete1);
			 
			 include("jobcard_sublist.php");
}
/******************************************** Main DELETE*****************************************************************/
if($_POST['action']=="delete_job_main")
{ 

  $sql="select * from  jobcard_main where main_id='$_POST[job_id]' ";
  $rs=mysql_query($sql);
  while($rsdata=mysql_fetch_array($rs))
  {
   				$style_no=$rsdata['style_no'];
				$style_name=$rsdata['style_name'];
				$jobcard_no=$rsdata['jobcard_no'];
				$created_by=$rsdata['created_by'];
	   			$cust_name=$rsdata['cust_name'];
	   			$entry_date=$rsdata['entry_date'];
				$job_type=$rsdata['job_type'];
	   			$fabric_type=$rsdata['fabric_type'];
	   			$overall_value=$rsdata['overall_value'];
	   			$net_total=$rsdata['net_total'];
				$combo=$rsdata['combo'];
				$tax=$rsdata['tax'];
				$tax_amt=$rsdata['tax_amt'];
				$create_date1=date('Y-m-d');
				$acadamic_yr=explode("-",$create_date1);
				$acdmc_yr_res=$acadamic_yr[0];
				
 $Insql1=mysql_query("insert into deleted_jobcard(jobcard_no,entry_date,created_by,cust_name,style_no,style_name,cust_po_no,job_type,fabric_type,combo,overall_value,tax,tax_amt,net_total,academic_yr,createdate)values('$jobcard_no','$entry_date','$created_by','$cust_name','$style_no','$style_name','$cust_po_no','$job_type','$fabric_type','$combo','$overall_value','$tax','$tax_amt','$net_total','$acdmc_yr_res','$entry_date')");  
 }
	 
 
		 
   $delete2="delete from jobcard_main where main_id='$_POST[job_id]' and jobcard_no='$_POST[jobcard_no]'";
   mysql_query($delete2);

   $Delsql5="delete from jobcard_sublist where  jobcard_no='$_POST[jobcard_no]' ";
   mysql_query($Delsql5);
  
   $delete21="delete from inward_main where  jobcard_no='$_POST[jobcard_no]'";
   mysql_query($delete21);

   $Delsql51="delete from inward_sub_list where  jobcard_no='$_POST[jobcard_no]' ";
   mysql_query($Delsql51);
  
   $delete22="delete from delivery_challan_main where  job_no='$_POST[jobcard_no]'";
   mysql_query($delete22);

   $Delsql52="delete from delivery_challan_sub where  job_no='$_POST[jobcard_no]' ";
   mysql_query($Delsql52);
  
   $delete25="delete from printer_production_main where job_card_no='$_POST[jobcard_no]'";
   mysql_query($delete25);

   $Delsql55="delete from fusing_production_main where  job_card_no='$_POST[jobcard_no]' ";
   mysql_query($Delsql55);
  
   $delete23="delete from fusing_production_sublist where job_card_no='$_POST[jobcard_no]'";
   mysql_query($delete23);

   $Delsql54="delete from printer_production_sublist where  job_card_no='$_POST[jobcard_no]' ";
   mysql_query($Delsql54);
    
   $delete21="delete from file_upload_jobcard where job_card_no='$_POST[job_card_no]'";
   mysql_query($delete21);
  
  include("jobcard_entry_list.php");
  echo $msg;
}
if($_POST['action']=="del_file_upld")
{ 		 
  $delete21="delete from file_upload_jobcard where file_id='$_POST[id]' and (job_card_no='$_POST[job_card_no]' or quotation_no='$_POST[quotation_no]')";
   mysql_query($delete21);
  include("file_upload_subllist.php");

}
?>