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


function get_jobcard_check_ink($ink_pur_no)
{
	$Isql=mysql_query("select * from ink_purchase where ink_pur_no='$ink_pur_no'");
    $rscount=mysql_num_rows($Isql);
	return $rscount;
}
function get_item_id_ink($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_pur_no=$_POST['ink_pur_no'];
$ink_date=$_POST['ink_date'];
$total=$_POST['total'];
$create_date1=date('Y-m-d');
$acadamic_yr=explode("-",$create_date1);
$acdmc_yr_res=$acadamic_yr[0];
$check_item=get_jobcard_check_ink($ink_pur_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_purchase(ink_pur_no,ink_date,total_mtr,academic_yr,createdate)values('$ink_pur_no','$ink_date','$total','$acc_year','$create_date1')");
 echo "Added Successfully";

}
else
{
	echo "Already Exists...";
}
}
/***************************** Main Update***********************************************************/
if($_POST['action']=="UPDATE")
{
$edit_ids=$_POST['main_edit_id'];
$ink_pur_no=$_POST['ink_pur_no'];
$ink_date=$_POST['ink_date'];
$total=$_POST['total'];
$create_date1=date('Y-m-d');
$acadamic_yr=explode("-",$create_date1);
$acdmc_yr_res=$acadamic_yr[0];
echo  $Upsq12="update ink_purchase set ink_pur_no='$ink_pur_no',ink_date='$ink_date',total_mtr='$total',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")
	{
	  $ink_pur_no=$_POST['ink_pur_no'];
      $ink_date=$_POST['ink_date'];
      $entrydates=$_POST['entrydates'];
	  $invoice_no=$_POST['invoice_no'];
      $sup_name=$_POST['sup_name'];
      $particulars=$_POST['particulars'];
      $batch_no=$_POST['batch_no'];
      $uom=$_POST['uom'];
      $nos=$_POST['nos'];
      $qty=$_POST['qty'];
	  $mtr_roll=$_POST['mtr_roll'];
      $tot_mtr=$_POST['tot_mtr'];
      $create_date1=date('Y-m-d');
	  $acadamic_yr=explode("-",$create_date1);
      $acdmc_yr_res=$acadamic_yr[0];
	    $item_id=get_item_id_ink($particulars);

$Insql1=mysql_query("insert into ink_pur_sublist(ink_pur_no,ink_date,entrydates,invoice_no,supplier_name,particulars	 ,batch_no,uom,nos,qty,mtr_roll,tot_mtr,total_meter,particulars_id)values('$ink_pur_no','$ink_date','$entrydates','$invoice_no','$sup_name','$particulars','$batch_no','$uom','$nos','$qty','$mtr_roll','$tot_mtr','$total','$item_id')");

$insert_id=mysql_insert_id();


$Isql23 = mysql_query("SELECT SUM(uom) AS uom, SUM(nos) AS nos, SUM(qty) AS qty 
    FROM ink_pur_batch_tot_value 
    WHERE particulars = '$particulars' AND batch_no = '$batch_no'");
    // echo "SELECT SUM(uom) AS uom, SUM(nos) AS nos, SUM(qty) AS qty 
    // FROM ink_pur_batch_tot_value 
    // WHERE particulars = '$particulars' AND batch_no = '$batch_no'";
    // exit();

$rsdata_user23 = mysql_fetch_object($Isql23);

if ($rsdata_user23->qty === NULL) {
    // No data exists – insert new
    mysql_query("INSERT INTO ink_pur_batch_tot_value (ink_date,entrydates,particulars	,batch_no,uom,nos,qty,mtr_roll,tot_mtr,total_meter,particulars_id
    ) VALUES (
        '$ink_date','$entrydates','$particulars','$batch_no','$uom','$nos','$qty','$mtr_roll','$tot_mtr','$total','$item_id'
    )");
} else {
    // Record exists – update
    $uom1 = $rsdata_user23->uom;
    $nos1 = $rsdata_user23->nos;
    $qty1 = $rsdata_user23->qty;

    $uom2 = $uom + $uom1;
    $nos2 = $nos + $nos1;
    $qty2 = $qty + $qty1;

    $updateQuery25 = "UPDATE ink_pur_batch_tot_value SET 
        uom = '$uom2',
        nos = '$nos2',
        qty = '$qty2',
        total_meter = '$total'
    WHERE 
        particulars = '$particulars' AND batch_no = '$batch_no'";
      
    mysql_query($updateQuery25);
}


$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,create_date)values('$entrydates','$insert_id','$sup_name','Ink','$item_id','$particulars','$qty','','$ink_pur_no','Ink Purchase','$create_date1')");

include("ink_pur_sublist.php");
}

	   /**********************************SUB Edit  QUERY ***************************************/

if($_POST['action']=="EDIT")
	{
	  $ink_pur_no=$_POST['ink_pur_no'];
      $ink_date=$_POST['ink_date'];
      $entrydates=$_POST['entrydates'];
	  $invoice_no=$_POST['invoice_no'];
      $sup_name=$_POST['sup_name'];
      $particulars=$_POST['particulars'];
      $batch_no=$_POST['batch_no'];
	   $item_id=get_item_id_ink($particulars);
      $qty=$_POST['qty'];
	  $mtr_roll=$_POST['mtr_roll'];
      $tot_mtr=$_POST['tot_mtr'];
      $create_date1=date('Y-m-d');
	  $acadamic_yr=explode("-",$create_date1);
      $acdmc_yr_res=$acadamic_yr[0];
	  
$Insql1=mysql_query("update ink_pur_sublist set ink_pur_no='$ink_pur_no',ink_date='$ink_date',entrydates='$entrydates',invoice_no='$invoice_no',supplier_name='$sup_name',particulars='$particulars',batch_no='$batch_no',qty='$qty',mtr_roll='$mtr_roll',tot_mtr='$tot_mtr',total_meter='$total',particulars_id='$item_id' where id='$_POST[edit_id]'");

$Insql1=mysql_query("update stock_table set date='$entrydates',supplier_name='$sup_name',group_id='Ink',item_id='$item_id',item_name='$particulars',in_qty='$qty' where insert_id='$_POST[edit_id]' and voucher_name='Ink Purchase' and ref_id='$ink_pur_no'");

include("ink_pur_sublist.php");
}

/******************************************** SUB DELETE*****************************************************************/
if($_POST['action']=="del_media_sublist")
{ 		 

  $countQuery = mysql_query("SELECT COUNT(*) as total FROM ink_sublist 
    WHERE prod_size = '$_POST[particulars]' 
    AND batch_no = '$_POST[batch_no]'");

if (!$countQuery) {
    die("Count Query Error: " . mysql_error());
}

$countResult = mysql_fetch_object($countQuery);
$total = $countResult->total;

if ($total != 0) {
    echo "<script>alert('This Ink entry is already in use, so it cannot be deleted.');</script>";
    // echo "karthi";
}
else {
  echo "<script>alert('This Ink entry is not currently in use and can be safely deleted.');</script>";
  $delete_date1=date('Y-m-d');
      $ip_address = $_SERVER['REMOTE_ADDR'];

  $Isql26 = mysql_query("SELECT SUM(uom) AS uom, SUM(nos) AS nos, SUM(qty) AS qty 
  FROM ink_pur_sublist 
  WHERE particulars = '$_POST[particulars]' AND batch_no = '$_POST[batch_no]' and  id='$_POST[id]' and ink_pur_no='$_POST[ink_pur_no]'");

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

  $rsdata_user26 = mysql_fetch_object($Isql26);

  $uom111 = isset($rsdata_user26->uom) ? $rsdata_user26->uom : 0;
  $nos111 = isset($rsdata_user26->nos) ? $rsdata_user26->nos : 0;
  $qty111 = isset($rsdata_user26->qty) ? $rsdata_user26->qty : 0;

  $Isql227 = mysql_query("SELECT SUM(uom) AS uom, SUM(nos) AS nos, SUM(qty) AS qty 
  FROM ink_pur_batch_tot_value 
  WHERE particulars = '$_POST[particulars]' AND batch_no = '$_POST[batch_no]' ");

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

  $rsdata_user27 = mysql_fetch_object($Isql227);

  $uom1111 = isset($rsdata_user27->uom) ? $rsdata_user27->uom : 0;
  $nos1111 = isset($rsdata_user27->nos) ? $rsdata_user27->nos : 0;
  $qty1111 = isset($rsdata_user27->qty) ? $rsdata_user27->qty : 0;

  $qty28 = $qty1111 - $qty111;
  $uom28 = $uom1111 - $uom111;
  $nos28 = $nos1111 - $nos111;


  $updateQuery29 = "UPDATE ink_pur_batch_tot_value SET 
  qty = '$qty28',
  uom = '$uom28',
  nos = '$nos28',
  total_meter = '',
  delete_date = '$delete_date1',
  last_delete_value = '$qty111',
  ip_address = '$ip_address'
WHERE 
  particulars = '$_POST[particulars]' AND batch_no = '$_POST[batch_no]'";

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


  $Isql30 = mysql_query("SELECT SUM(total_mtr) AS total_mtr 
  FROM ink_purchase 
  WHERE ink_pur_no='$_POST[ink_pur_no]'");

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

  $rsdata_user33 = mysql_fetch_object($Isql30);

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

  $tot_mtr33 = $tot_mtr1133 - $qty111;


  $updateQuery13 = "UPDATE ink_purchase SET 
    total_mtr = '$tot_mtr33'
  WHERE ink_pur_no='$_POST[ink_pur_no]'";

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



  $delete1="delete from ink_pur_sublist where id='$_POST[id]' and ink_pur_no='$_POST[ink_pur_no]'";
			 mysql_query($delete1);


			   $delete21="delete from stock_table where insert_id='$_POST[id]' and ref_id='$_POST[ink_pur_no]' and voucher_name='Ink Purchase'";
			 mysql_query($delete21);
}

		     
			 include("ink_pur_sublist.php");
}
/******************************************** Main DELETE*****************************************************************/
if($_POST['action']=="delete_media")
{ 		 
  $delete_date1=date('Y-m-d');
  $ip_address = $_SERVER['REMOTE_ADDR'];

  
  $Isq35 = mysql_query("SELECT particulars, batch_no FROM ink_pur_sublist WHERE ink_pur_no='$_POST[ink_pur_no]'");

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

// Loop through all unique (particulars, batch_no) pairs
while ($rsdata_user35 = mysql_fetch_object($Isq35)) {
    $particulars1 = $rsdata_user35->particulars;
    $batch_no1 = $rsdata_user35->batch_no;

    // First SUM from sublist
    $Isql36 = mysql_query("SELECT SUM(uom) AS uom, SUM(nos) AS nos, SUM(qty) AS qty 
        FROM ink_pur_sublist 
        WHERE particulars = '$particulars1' AND batch_no = '$batch_no1' and ink_pur_no='$_POST[ink_pur_no]'");

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

    $rsdata_user36 = mysql_fetch_object($Isql36);

    $uom21 = isset($rsdata_user36->uom) ? $rsdata_user36->uom : 0;
    $nos21 = isset($rsdata_user36->nos) ? $rsdata_user36->nos : 0;
    $qty21 = isset($rsdata_user36->qty) ? $rsdata_user36->qty : 0;

    // Second SUM from batch_tot_value
    $Isql37 = mysql_query("SELECT SUM(uom) AS uom, SUM(nos) AS nos, SUM(qty) AS qty 
        FROM ink_pur_batch_tot_value 
        WHERE particulars = '$particulars1' AND batch_no = '$batch_no1'");

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

    $rsdata_user37 = mysql_fetch_object($Isql37);

    $uom22 = isset($rsdata_user37->uom) ? $rsdata_user37->uom : 0;
    $nos22 = isset($rsdata_user37->nos) ? $rsdata_user37->nos : 0;
    $qty22 = isset($rsdata_user37->qty) ? $rsdata_user37->qty : 0;

    // Difference calculations
    $uom23 = $uom22 - $uom21;
    $nos23 = $nos22 - $nos21;
    $qty23 = $qty22 - $qty21;

    // ✅ UPDATE QUERY INSIDE LOOP
    $updateQuery126 = "UPDATE ink_pur_batch_tot_value SET 
      uom = '$uom23',
      nos = '$nos23',
      qty = '$qty23',
      total_meter = '',
      delete_date = '$delete_date1',
      last_delete_value = '$qty21',
      ip_address = '$ip_address'
        WHERE particulars = '$particulars1' AND batch_no = '$batch_no1'";

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


  $delete2="delete from ink_purchase where ink_pur_no='$_POST[ink_pur_no]'";
   mysql_query($delete2);
  $Delsql5="delete from ink_pur_sublist where  ink_pur_no='$_POST[ink_pur_no]' ";
  mysql_query($Delsql5);
    $Delsql5="delete from stock_table where  ref_id='$_POST[ink_pur_no]' ";
  mysql_query($Delsql5);
  include("ink_pur_list.php");
  $msg="<span class='success'>Successfully Deleted </span>";
  echo $msg;
}
?>