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

function get_jobcard_check($ink_entry_no)
{
	$Isql=mysql_query("select * from ink_main where ink_entry_no='$ink_entry_no'");
    $rscount=mysql_num_rows($Isql);
	return $rscount;
}
function get_item_id_inkent($particulars)
{
	$Isql=mysql_query("select * from consumable_creation where particulars='$particulars' and item='Ink'");
    $rscount=mysql_num_rows($Isql);
	 while($rsdata=mysql_fetch_array($Isql))
  {
	  
	   $id=$rsdata['id'];
  }
  return $id;
}

/***************************** Main Add***********************************************************/
if(($_POST['action']=="SUBMIT")) 
{
$ink_entry_no=$_POST['ink_entry_no'];
$ink_date=$_POST['ink_date'];
$tot_prod=$_POST['tot_prod'];
$tot_bal=$_POST['tot_bal'];
$cr_date=date('Y-m-d');

$acadamic_yr=explode("-",$create_date1);
$acdmc_yr_res=$acadamic_yr[0];
$check_item=get_jobcard_check($ink_entry_no);

$create_date=date('Y-m-d');
$explodes=explode("-",$create_date);
$year1=$explodes[0];
$year2=$explodes[1];
$year=$year1."-".$year2;

if(($year=='2014-04')||($year=='2014-05')||($year=='2014-06')||($year=='2014-07')||($year=='2014-08')||($year=='2014-09')||($year=='2014-10')||($year=='2014-11')||($year=='2014-12')||($year=='2015-01')||($year=='2015-02')||($year=='2015-03'))
{
$acc_year='2015';
}

if(($year=='2015-04')||($year=='2015-05')||($year=='2015-06')||($year=='2015-07')||($year=='2015-08')||($year=='2015-09')||($year=='2015-10')||($year=='2015-11')||($year=='2015-12')||($year=='2016-01')||($year=='2016-02')||($year=='2016-03'))
{
$acc_year='2016';
}

if(($year=='2016-04')||($year=='2016-05')||($year=='2016-06')||($year=='2016-07')||($year=='2016-08')||($year=='2016-09')||($year=='2016-10')||($year=='2016-11')||($year=='2016-12')||($year=='2017-01')||($year=='2017-02')||($year=='2017-03'))
{
$acc_year='2017';
}

if(($year=='2017-04')||($year=='2017-05')||($year=='2017-06')||($year=='2017-07')||($year=='2017-08')||($year=='2017-09')||($year=='2017-10')||($year=='2017-11')||($year=='2017-12')||($year=='2018-01')||($year=='2018-02')||($year=='2018-03'))
{
$acc_year='2018';
}


if(($year=='2018-04')||($year=='2018-05')||($year=='2018-06')||($year=='2018-07')||($year=='2018-08')||($year=='2018-09')||($year=='2018-10')||($year=='2018-11')||($year=='2018-12')||($year=='2019-01')||($year=='2019-02')||($year=='2019-03'))
{
$acc_year='2019';
}



if(($year=='2019-04')||($year=='2019-05')||($year=='2019-06')||($year=='2019-07')||($year=='2019-08')||($year=='2019-09')||($year=='2019-10')||($year=='2019-11')||($year=='2019-12')||($year=='2020-01')||($year=='2020-02')||($year=='2020-03'))
{
$acc_year='2020';
}

if(($year=='2020-04')||($year=='2020-05')||($year=='2020-06')||($year=='2020-07')||($year=='2020-08')||($year=='2020-09')||($year=='2020-10')||($year=='2020-11')||($year=='2020-12')||($year=='2021-01')||($year=='2021-02')||($year=='2021-03'))
{
$acc_year='2021';
}

if(($year=='2021-04')||($year=='2021-05')||($year=='2021-06')||($year=='2021-07')||($year=='2021-08')||($year=='2021-09')||($year=='2021-10')||($year=='2021-11')||($year=='2021-12')||($year=='2022-01')||($year=='2022-02')||($year=='2022-03'))
{
$acc_year='2022';
}

if(($year=='2022-04')||($year=='2022-05')||($year=='2022-06')||($year=='2022-07')||($year=='2022-08')||($year=='2022-09')||($year=='2022-10')||($year=='2022-11')||($year=='2022-12')||($year=='2023-01')||($year=='2023-02')||($year=='2023-03'))
{
$acc_year='2023';
}

if(($year=='2023-04')||($year=='2023-05')||($year=='2023-06')||($year=='2023-07')||($year=='2023-08')||($year=='2023-09')||($year=='2023-10')||($year=='2023-11')||($year=='2023-12')||($year=='2024-01')||($year=='2024-02')||($year=='2024-03'))
{
$acc_year='2024';
}

if(($year=='2024-04')||($year=='2024-05')||($year=='2024-06')||($year=='2024-07')||($year=='2024-08')||($year=='2024-09')||($year=='2024-10')||($year=='2024-11')||($year=='2024-12')||($year=='2025-01')||($year=='2025-02')||($year=='2025-03'))
{
$acc_year='2025';
}

if($check_item==0)
{
 $Insql1=mysql_query("insert into ink_main(ink_entry_no,ink_date,tot_prod,cr_date,acc_year)values('$ink_entry_no','$ink_date','$tot_prod','$cr_date','$acc_year')");
 echo "Added Successfully";
}
else
{
	echo "Already Exists...";
}
}
/***************************** Main Update***********************************************************/
if($_POST['action']=="UPDATE")
{
$edit_ids=$_POST['main_edit_id'];
$ink_entry_no=$_POST['ink_entry_no'];
$ink_date=$_POST['ink_date'];
$tot_prod=$_POST['tot_prod'];
$tot_bal=$_POST['tot_bal'];
$cr_date=date('Y-m-d');
$acadamic_yr=explode("-",$cr_date);
$acdmc_yr_res=$acadamic_yr[0];
  $Upsq12="update ink_main set ink_entry_no='$ink_entry_no',ink_date='$ink_date',tot_prod='$tot_prod',acc_year='$acdmc_yr_res',cr_date='$cr_date' where ink_id='$_POST[main_edit_id]'";
 mysql_query($Upsq12) or die(error);
 echo "Updated Successfully";
}

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

 if($_POST['action']=="ADD")
	{
	  $ink_no=$_POST['ink_entry_no'];
      
      $ink_date=$_POST['ink_date'];
	  $shift_time=$_POST['shift_time'];
      $machine_no=$_POST['machine_no'];
      $operator_name=$_POST['operator_name'];
      $prod_size=$_POST['prod_size'];
	  $batch_no=$_POST['batch_no'];
	  $total_req=$_POST['total_req'];
	  $entrydates=date('Y-m-d',strtotime($_POST['entrydates']));
      $create_date1=date('Y-m-d');
	  $acadamic_yr=explode("-",$create_date1);
      $acdmc_yr_res=$acadamic_yr[0];
	  $item_id=get_item_id_inkent($prod_size);

$Insql1=mysql_query("insert into ink_sublist(ink_entry_no,ink_date,shift_time,machine_no,operator_name,prod_size,batch_no,total_req,cr_date)values('$ink_no','$ink_date','$shift_time','$machine_no','$operator_name','$prod_size','$batch_no','$total_req','$create_date1')");
$insert_id=mysql_insert_id();

$Insql1=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('$ink_date','$insert_id','$operator_name','Ink','$item_id','$prod_size','','$total_req','$ink_no','Ink Entry')");

$Isql2323=mysql_query("select sum(total_req) as total_req from ink_sublist where prod_size='$prod_size' and batch_no='$batch_no'");
// echo "select sum(total_req) as total_req from ink_sublist where prod_size='$prod_size' and batch_no='$batch_no'";


$rscount=mysql_num_rows($Isql2323);
	while($rsdata_user2323=mysql_fetch_object($Isql2323))
			{ 
			 $totl_metr=$rsdata_user2323->total_req;
			}

$totl_mtr =number_format($totl_metr,2);
// echo $totl_mtr."karthi";
$Upsq122323="update ink_pur_batch_tot_value set total_meter='$totl_mtr' where particulars='$prod_size' and batch_no='$batch_no'";
mysql_query($Upsq122323);

include("ink_sublist.php");
}
/******************************************** SUB EDIT*****************************************************************/

if($_POST['action']=="EDIT")
	{
	$ink_no=$_POST['ink_entry_no'];
      
      $ink_date=$_POST['ink_date'];
	  $shift_time=$_POST['shift_time'];
      $machine_no=$_POST['machine_no'];
      $operator_name=$_POST['operator_name'];
      $prod_size=$_POST['prod_size'];
	  $batch_no=$_POST['batch_no'];
	  $total_req=$_POST['total_req'];
	  $nos=$_POST['nos'];
	  $total_qty=$_POST['total_qty'];
	  $entrydates=date('Y-m-d',strtotime($_POST['entrydates']));
      $create_date1=date('Y-m-d');
	  $acadamic_yr=explode("-",$create_date1);
      $acdmc_yr_res=$acadamic_yr[0];
	  $item_id=get_item_id_inkent($prod_size);
	   $balance=$_POST['balance'];
$Insql1=mysql_query("update ink_sublist set ink_entry_no='$ink_no',ink_date='$ink_date',shift_time='$shift_time',machine_no='$machine_no',operator_name='$operator_name',prod_size='$prod_size',batch_no='$batch_no',total_req='$total_req',nos='$nos',total_qty='$total_qty',cr_date='$create_date1',balance_qty='$balance' where ink_sub_id='$_POST[edit_id]'");

$Insql1=mysql_query("update stock_table set date='$ink_date',supplier_name='$operator_name',group_id='Ink',item_id='$item_id',item_name='$prod_size',out_qty='$total_req' where insert_id='$_POST[edit_id]' and voucher_name='Ink Entry' and ref_id='$ink_no'");



$Isql2323=mysql_query("select sum(total_req) as total_req from ink_sublist where prod_size='$prod_size' and batch_no='$batch_no'");
// echo "select sum(total_req) as total_req from ink_sublist where prod_size='$prod_size' and batch_no='$batch_no'";


$rscount=mysql_num_rows($Isql2323);
	while($rsdata_user2323=mysql_fetch_object($Isql2323))
			{ 
			 $totl_metr=$rsdata_user2323->total_req;
			}

$totl_mtr =number_format($totl_metr,2);
// echo $totl_mtr."karthi";
$Upsq122323="update ink_pur_batch_tot_value set total_meter='$totl_mtr' where particulars='$prod_size' and batch_no='$batch_no'";
mysql_query($Upsq122323);





include("ink_sublist.php");
}

/******************************************** SUB DELETE*****************************************************************/
if($_POST['action']=="del_ink_sublist")
{ 	
	
	$Isq9191=mysql_query("select ink_entry_no, sum(total_req) as totl_metr,prod_size,batch_no from ink_sublist where ink_sub_id='$_POST[id]'");
	
	$rscount=mysql_num_rows($Isq9191);
	while($rsdata_user9191=mysql_fetch_object($Isq9191))
	{ 
	$totl_metr2325=$rsdata_user9191->totl_metr;
	$roll_name2325=$rsdata_user9191->prod_size;
	$batch_no2325=$rsdata_user9191->batch_no;
	$ink_entry_no=$rsdata_user9191->ink_entry_no;

	}
// echo $ink_entry_no;
	$Isq916=mysql_query("select sum(total_meter) as total_meter,particulars,batch_no from ink_pur_batch_tot_value where particulars='$roll_name2325' and batch_no='$batch_no2325'");


	$rscount=mysql_num_rows($Isq916);
	while($rsdata_user916=mysql_fetch_object($Isq916))
	{ 
	$totl_metr2359=$rsdata_user916->total_meter;
	}
// echo $totl_metr2325;
	$delete_totl_mtr5 = $totl_metr2359-$totl_metr2325;
// echo $delete_totl_mtr5;
// exit();

$Upsq91999="update ink_pur_batch_tot_value set total_meter='$delete_totl_mtr5' where particulars='$roll_name2325' and batch_no='$batch_no2325'";

mysql_query($Upsq91999);



$Isql30 = mysql_query("SELECT SUM(tot_prod) AS tot_prod 
  FROM ink_main 
  WHERE ink_entry_no='$ink_entry_no'");

//   echo "SELECT SUM(tot_prod) AS tot_prod 
//   FROM ink_main 
//   WHERE ink_entry_no='$ink_entry_no'";
//   exit();

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

  $rsdata_user33 = mysql_fetch_object($Isql30);

  $tot_mtr1133 = isset($rsdata_user33->tot_prod) ? $rsdata_user33->tot_prod : 0;

  $tot_mtr33 = $tot_mtr1133 - $totl_metr2325;


  $updateQuery13 = "UPDATE ink_main SET 
    tot_prod = '$tot_mtr33'
  WHERE ink_entry_no='$_POST[ink_entry_no]'";

  if (!mysql_query($updateQuery13)) {
  die("Update Query Error: " . mysql_error());
  }
		     $delete1="delete from ink_sublist where ink_sub_id='$_POST[id]'";
			 mysql_query($delete1);
			 
			 $delete12="delete from stock_table where insert_id='$_POST[id]' and voucher_name='Ink Entry'";
			 mysql_query($delete12);

			 include("ink_sublist.php");
}
/******************************************** Main DELETE*****************************************************************/
if($_POST['action']=="del_main")

if (isset($_POST['ink_entry_no'])) {
    $delete_date1 = date('Y-m-d');
    $ip_address = $_SERVER['REMOTE_ADDR'];
    $ink_entry_no = $_POST['ink_entry_no'];

    // Fetch unique (prod_size, batch_no) pairs
    $Isq40 = mysql_query("SELECT DISTINCT prod_size, batch_no FROM ink_sublist WHERE ink_entry_no='$ink_entry_no'");

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

    // Loop through each (prod_size, batch_no)
    while ($rsdata_user40 = mysql_fetch_object($Isq40)) {
        $particulars2 = $rsdata_user40->prod_size;
        $batch_no2 = $rsdata_user40->batch_no;

        // Get SUM of total_req from ink_sublist
        $Isql41 = mysql_query("SELECT SUM(total_req) AS total_req 
            FROM ink_sublist 
            WHERE prod_size = '$particulars2' AND batch_no = '$batch_no2' AND ink_entry_no = '$ink_entry_no'");

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

        $rsdata_user41 = mysql_fetch_object($Isql41);
        $total_req = isset($rsdata_user41->total_req) ? $rsdata_user41->total_req : 0;

        // Get SUM of total_meter from ink_pur_batch_tot_value
        $Isql42 = mysql_query("SELECT SUM(total_meter) AS total_meter 
            FROM ink_pur_batch_tot_value 
            WHERE particulars = '$particulars2' AND batch_no = '$batch_no2'");

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

        $rsdata_user42 = mysql_fetch_object($Isql42);
        $total_meter = isset($rsdata_user42->total_meter) ? $rsdata_user42->total_meter : 0;

        // Difference calculation
        $new_total_meter = $total_meter - $total_req;

        // Update ink_pur_batch_tot_value
        $updateQuery = "UPDATE ink_pur_batch_tot_value SET 
            total_meter = '$new_total_meter',
            delete_date = '$delete_date1',
            last_delete_value = '$total_req',
            ip_address = '$ip_address'
            WHERE particulars = '$particulars2' AND batch_no = '$batch_no2'";

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

    // Delete records after loop
    $delete2 = "DELETE FROM ink_main WHERE ink_entry_no='$ink_entry_no'";
    mysql_query($delete2);

    $Delsql5 = "DELETE FROM ink_sublist WHERE ink_entry_no='$ink_entry_no'";
    mysql_query($Delsql5);

    $delete12 = "DELETE FROM stock_table WHERE ref_id='$ink_entry_no' AND voucher_name='Ink Entry'";
    mysql_query($delete12);

    include("ink_entry_list.php");

    $msg = "<span class='success'>Successfully Deleted</span>";
    echo $msg;
} else {
    echo "Invalid request.";
}
?>