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


function get_jobcard_check($jobcard_no)
{
	$Isql=mysql_query("select * from media_main where jobcard_no='$jobcard_no'");
    $rscount=mysql_num_rows($Isql);
	return $rscount;
}
function get_roll_name($roll_name)
{
	$Isql=mysql_query("select * from consumable_creation where particulars='$roll_name'");
    $rscount=mysql_num_rows($Isql);
	while($rsdata_user=mysql_fetch_object($Isql))
			{ 
			 $id=$rsdata_user->id;
			}
			return $id;
}

/***************************** Main Add***********************************************************/
if(($_POST['action']=="SUBMIT")) 
{
$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'];
$create_date1=date('Y-m-d');
$acadamic_yr=explode("-",$create_date1);
$acdmc_yr_res=$acadamic_yr[0];
$check_item=get_jobcard_check($jobcard_no);
if($check_item==0)
{
 $Insql1=mysql_query("insert into media_main(jobcard_no,entry_date,created_by,cust_name,style_no,style_name,cust_po_no,job_type,fabric_type,combo,academic_yr,createdate)values('$jobcard_no','$entry_time','$created_by','$customer_name','$style_no','$style_name','$cust_po_no','$jobtype','$fabric_type','$combo','$acdmc_yr_res','$create_date1')");
 echo "Added Successfully";
 $insert_id = mysql_insert_id();
 $Upsq1211="update media_sub_list set insert_id='$insert_id' where jobcard_no='$jobcard_no'";
 mysql_query($Upsq1211) or die(error);
}
else
{
	echo "Already Exists...";
}
}
/***************************** 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'];
$create_date1=date('Y-m-d');
$acadamic_yr=explode("-",$create_date1);
$acdmc_yr_res=$acadamic_yr[0];
  $Upsq12="update media_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',academic_yr='$acdmc_yr_res',createdate='$create_date1', where main_id='$_POST[main_edit_id]'";
 mysql_query($Upsq12) or die(error);
 echo "Updated Successfully";
}


 /**********************************SUB ADD  QUERY ***************************************/


 if($_POST['action']=="ADD")
	{
	  $jobcard_no=$_POST['jobcard_no'];
      $in_cust_name=$_POST['in_cust_name'];
      $media_date=$_POST['media_date'];
	  $roll_name=$_POST['roll_name'];
	  $roll_id=get_roll_name($roll_name);
	  $batch_no=$_POST['batch_no'];
      $media_particular=$_POST['media_particular'];
      $order_qty=$_POST['order_qty'];
      $plan_qty=$_POST['plan_qty'];
	  $steps=$_POST['steps'];
	  $gap_mtr=$_POST['gap_mtr'];
	  $steps_used=$_POST['steps_used'];
	  $gap = $_POST['gap'];
      $total_steps=$_POST['total_steps'];
	  $req_by=$_POST['req_by'];
	  $total_req=$_POST['total_req'];
	  $width=$_POST['width'];
	   $height=$_POST['height'];
	    $width_hid=$_POST['width_hid'];
		 $height_hid=$_POST['height_hid'];
	  $created_by=$_POST['created_by'];
	  $job_types=$_POST['job_types'];
	  $entrydates=$_POST['entrydates'];
      $create_date1=date('Y-m-d');
	  $acadamic_yr=explode("-",$create_date1);
      $acdmc_yr_res=$acadamic_yr[0];
	
$Insql1=mysql_query("insert into media_sub_list(jobcard_no,in_cust_name,media_date,media_particular,acad_yr,cr_date,fabric_type,created_by,job_types,entrydates,roll_name,batch_no,order_qty,plan_qty,steps,gap,total_steps,req_by,tot_req_by,width,height,width_hid,height_hid,steps_used,gap_mtr)values('$jobcard_no','$in_cust_name','$media_date','$media_particular','$acdmc_yr_res','$create_date1','$fabric_type','$created_by','$job_types','$entrydates','$roll_name','$batch_no','$order_qty','$plan_qty','$steps','$gap','$total_steps','$req_by','$total_req','$width','$height','$width_hid','$height_hid','$steps_used','$gap_mtr')");

$insert_id=mysql_insert_id();

$Insql21=mysql_query("insert into stock_table(date,insert_id,supplier_name,group_id,item_id,item_name,in_qty,out_qty,ref_id,voucher_name)values('$entrydates','$insert_id','$in_cust_name','Media','$roll_id','$roll_name','','$total_req','$jobcard_no','Media Entry')");

$Isql23=mysql_query("select sum(tot_req_by) as totl_metr from media_sub_list where roll_name='$roll_name' and batch_no='$batch_no'");

$rscount=mysql_num_rows($Isql23);
	while($rsdata_user23=mysql_fetch_object($Isql23))
			{ 
			 $totl_metr=$rsdata_user23->totl_metr;
			}

$totl_mtr =number_format($totl_metr,2);
// echo $totl_mtr."karthi";

$Upsq1223="update media_pur_batch_tot_value set total_meter='$totl_mtr' where particulars='$roll_name' and batch_no='$batch_no'";
mysql_query($Upsq1223);
// echo "Updated Successfully";




include("media_sublist.php");
}
 if($_POST['action']=="EDIT")
	{
	  $jobcard_no=$_POST['jobcard_no'];
      $in_cust_name=$_POST['in_cust_name'];
      $media_date=$_POST['media_date'];
	  $roll_name=$_POST['roll_name'];
      $media_particular=$_POST['media_particular'];
      $order_qty=$_POST['order_qty'];
      $plan_qty=$_POST['plan_qty'];
	  $steps=$_POST['steps'];
	  $gap_mtr=$_POST['gap_mtr'];
	  $steps_used=$_POST['steps_used'];
	  $gap = $_POST['gap'];
      $total_steps=$_POST['total_steps'];
	  $req_by=$_POST['req_by'];
	  $total_req=$_POST['total_req'];
	  $roll_size=$_POST['roll_size'];
	  $batch_no=$_POST['batch_no'];
	  $width=$_POST['width'];
	   $height=$_POST['height'];
	    $width_hid=$_POST['width_hid'];
		 $height_hid=$_POST['height_hid'];
	  $created_by=$_POST['created_by'];
	  $job_types=$_POST['job_types'];
	  $entrydates=$_POST['entrydates'];
	   $roll_id=get_roll_name($roll_name);
      $create_date1=date('Y-m-d');
	  $acadamic_yr=explode("-",$create_date1);
      $acdmc_yr_res=$acadamic_yr[0];
	
$Insql1=mysql_query("update media_sub_list set jobcard_no='$jobcard_no',in_cust_name='$in_cust_name',media_date='$media_date',media_particular='$media_particular',acad_yr='$acdmc_yr_res',cr_date='$create_date1' ,fabric_type='$fabric_type',created_by='$created_by',job_types='$job_types',entrydates='$entrydates',roll_name='$roll_name',batch_no='$batch_no',order_qty='$order_qty',plan_qty='$plan_qty',steps='$steps',gap_mtr='$gap_mtr',steps_used='$steps_used',gap='$gap',total_steps='$total_steps',req_by='$req_by',tot_req_by='$total_req',width='$width',height='$height',width_hid='$width_hid',height_hid='$height_hid' where id='$_POST[edit_id]'");

$Insql1=mysql_query("update stock_table set date='$media_date',supplier_name='$in_cust_name',group_id='Media',item_id='$roll_id',item_name='$roll_name',out_qty='$total_req' where insert_id='$_POST[edit_id]' and voucher_name='Media Entry' and ref_id='$jobcard_no'");


$Isql23=mysql_query("select sum(tot_req_by) as totl_metr from media_sub_list where roll_name='$roll_name' and batch_no='$batch_no'");

$rscount=mysql_num_rows($Isql23);
	while($rsdata_user23=mysql_fetch_object($Isql23))
			{ 
			 $totl_metr=$rsdata_user23->totl_metr;
			}

$totl_mtr =number_format($totl_metr,2);
// echo $totl_mtr."karthi";

$Upsq1223="update media_pur_batch_tot_value set total_meter='$totl_mtr' where particulars='$roll_name' and batch_no='$batch_no'";
mysql_query($Upsq1223);
// echo "Updated Successfully";





include("media_sublist.php");
}
/******************************************** SUB DELETE*****************************************************************/
if($_POST['action']=="del_media_sublist")
{ 		
	
	$Isq9195=mysql_query("select sum(tot_req_by) as totl_metr,roll_name,batch_no from media_sub_list where id='$_POST[id]'");

	$rscount=mysql_num_rows($Isq9195);
	while($rsdata_user9195=mysql_fetch_object($Isq9195))
	{ 
	$totl_metr23=$rsdata_user9195->totl_metr;
	$roll_name23=$rsdata_user9195->roll_name;
	$batch_no23=$rsdata_user9195->batch_no;

	}

	// echo $totl_metr23;

	$Isq915=mysql_query("select sum(total_meter) as total_meter,particulars,batch_no from media_pur_batch_tot_value where particulars='$roll_name23' and batch_no='$batch_no23'");

	$rscount=mysql_num_rows($Isq915);
	while($rsdata_user915=mysql_fetch_object($Isq915))
	{ 
	$totl_metr2356=$rsdata_user915->total_meter;
	}
	// echo $totl_metr2356;
	$delete_totl_mtr = $totl_metr2356-$totl_metr23;
//  echo $delete_totl_mtr;
// exit();

		     $delete1="delete from media_sub_list where id='$_POST[id]'";
			 mysql_query($delete1);
			   $delete1="delete from stock_table where insert_id='$_POST[id]' and voucher_name='Media Entry'";
			 mysql_query($delete1);

			 $Upsq9195="update media_pur_batch_tot_value set total_meter='$delete_totl_mtr' where particulars='$roll_name23' and batch_no='$batch_no23'";
			 mysql_query($Upsq9195);




			 include("media_sublist.php");
}
/******************************************** Main DELETE*****************************************************************/
if ($_POST['action'] == "delete_media") {
// echo "karthi";
// exit();
    $Isq25 = mysql_query("SELECT roll_name, batch_no FROM media_sub_list WHERE jobcard_no='{$_POST['jobcard_no']}'");
	

    if (!$Isq25) {
        die("Select Query Error: " . mysql_error());
    }

    while ($rsdata_user25 = mysql_fetch_object($Isq25)) {
        $particulars = $rsdata_user25->roll_name;
        $batch_no = $rsdata_user25->batch_no;

        // First SUM from sublist
        $Isql26 = mysql_query("SELECT SUM(tot_req_by) AS tot_req_by 
            FROM media_sub_list 
            WHERE roll_name = '$particulars' AND batch_no = '$batch_no' AND jobcard_no='{$_POST['jobcard_no']}'");
			

        if (!$Isql26) {
            die("SUM Query Error (sublist): " . mysql_error());
        }

        $rsdata_user26 = mysql_fetch_object($Isql26);
        $tot_req_by21 = isset($rsdata_user26->tot_req_by) ? $rsdata_user26->tot_req_by : 0;

        // Second SUM from batch_tot_value
        $Isql27 = mysql_query("SELECT SUM(total_meter) AS total_meter 
            FROM media_pur_batch_tot_value 
            WHERE particulars = '$particulars' AND batch_no = '$batch_no'");

        if (!$Isql27) {
            die("SUM Query Error (batch_tot_value): " . mysql_error());
        }

        $rsdata_user27 = mysql_fetch_object($Isql27);
        $total_meter22 = isset($rsdata_user27->total_meter) ? $rsdata_user27->total_meter : 0;

        // Difference calculation
        $total_meter23 = $total_meter22 - $tot_req_by21;

        // Update Query
        $updateQuery12 = "UPDATE media_pur_batch_tot_value SET 
            total_meter = '$total_meter23'
            WHERE particulars = '$particulars' AND batch_no = '$batch_no'";

        if (!mysql_query($updateQuery12)) {
            die("Update Query Error: " . mysql_error());
        }
    }

    // Delete related entries
    mysql_query("DELETE FROM media_main WHERE jobcard_no='{$_POST['jobcard_no']}'");
    mysql_query("DELETE FROM media_sub_list WHERE jobcard_no='{$_POST['jobcard_no']}'");
    mysql_query("DELETE FROM stock_table WHERE ref_id='{$_POST['jobcard_no']}' AND voucher_name='Media Entry'");

    include("media_entry_list.php");

    echo "<span class='success'>Successfully Deleted</span>";
}

?>