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

function get_jobcard_check_media($jobcard_no)
{
	$Isql=mysql_query("select * from media_purchase where media_pur_no='$media_pur_no'");
    $rscount=mysql_num_rows($Isql);
	return $rscount;
}
function get_item_id_media($particulars)
{
	$Isql=mysql_query("select * from consumable_creation where id='$particulars' and item='Media'");
    $rscount=mysql_num_rows($Isql);
	 while($rsdata=mysql_fetch_array($Isql))
  {
	  
	   $particulars=$rsdata['particulars'];
  }
  return $particulars;
}

/***************************** Main Add***********************************************************/
if(($_POST['action']=="SUBMIT")) 
{
$media_pur_no=$_POST['media_pur_no'];
$media_date=$_POST['media_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_media($media_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 media_purchase(media_pur_no,media_date,total_mtr,academic_yr,createdate)values('$media_pur_no','$media_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'];
$media_pur_no=$_POST['media_pur_no'];
$media_date=$_POST['media_date'];
$total=$_POST['total'];
$create_date1=date('Y-m-d');
$acadamic_yr=explode("-",$create_date1);
$acdmc_yr_res=$acadamic_yr[0];
  $Upsq12="update media_purchase set media_pur_no='$media_pur_no',media_date='$media_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") {
  $media_pur_no = $_POST['media_pur_no'];
  $media_date = $_POST['media_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_media($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];

  // You may need to define total if it's missing
//   $total = $tot_mtr; // or whatever calculation you need

  // Insert into media_pur_sublist
  $Insql1 = mysql_query("INSERT INTO media_pur_sublist(
      media_pur_no, media_date, entrydates, invoice_no, supplier_name, particulars, qty, mtr_roll, tot_mtr, total_meter, particulars_id, batch_no, status_type
  ) VALUES (
      '$media_pur_no', '$media_date', '$entrydates', '$invoice_no', '$sup_name', '$item_id', '$qty', '$mtr_roll', '$tot_mtr', '$total', '$particulars', '$batch_no', 'Active'
  )");

  $insert_id = mysql_insert_id();

  // Get existing batch data
  $Isql23 = mysql_query("SELECT SUM(qty) AS qty, SUM(mtr_roll) AS mtr_roll, SUM(tot_mtr) AS tot_mtr 
    FROM media_pur_batch_tot_value 
    WHERE particulars = '$item_id' AND batch_no = '$batch_no'");

$rsdata_user23 = mysql_fetch_object($Isql23);

if ($rsdata_user23->qty === NULL) {
    // No data exists – insert new
    mysql_query("INSERT INTO media_pur_batch_tot_value (
        media_date, entrydates, particulars, qty, mtr_roll, tot_mtr, total_meter, particulars_id, batch_no, status_type
    ) VALUES (
        '$media_date', '$entrydates', '$item_id', '$qty', '$mtr_roll', '$tot_mtr', '$total', '$particulars', '$batch_no', 'Active'
    )");
} else {
    // Record exists – update
    $qty1 = $rsdata_user23->qty;
    $mtr_roll1 = $rsdata_user23->mtr_roll;
    $tot_mtr1 = $rsdata_user23->tot_mtr;

    $qty2 = $qty + $qty1;
    $mtr_roll2 = $mtr_roll + $mtr_roll1;
    $tot_mtr2 = $tot_mtr + $tot_mtr1;

    $updateQuery = "UPDATE media_pur_batch_tot_value SET 
        qty = '$qty2',
        mtr_roll = '$mtr_roll2',
        tot_mtr = '$tot_mtr2',
        total_meter = '$total',
        status_type = 'Active'
    WHERE 
        particulars = '$item_id' AND batch_no = '$batch_no'";

    mysql_query($updateQuery);
}
 

  // Insert into stock table
  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, in_net
  ) VALUES (
      '$media_date', '$insert_id', '$sup_name', 'Media', '$particulars', '$item_id', '$tot_mtr', '', '$media_pur_no', 'Media Purchase', '$tot_mtr'
  )");

  include("media_pur_sublist.php");
}


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

if($_POST['action']=="EDIT")
	{
	    
	    
	  $media_pur_no=$_POST['media_pur_no'];
      $media_date=$_POST['media_date'];
      $entrydates=$_POST['entrydates'];
	  $invoice_no=$_POST['invoice_no'];
      $sup_name=$_POST['sup_name'];
      $particulars=$_POST['particulars'];
      
	  $item_id=get_item_id_media($particulars);
	  
	  $batch_no=$_POST['batch_no'];

      $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 media_pur_sublist set media_pur_no='$media_pur_no',media_date='$media_date',entrydates='$entrydates',invoice_no='$invoice_no',supplier_name='$sup_name',particulars='$item_id',batch_no='$batch_no',qty='$qty',mtr_roll='$mtr_roll',tot_mtr='$tot_mtr',total_meter='$total' where id='$_POST[edit_id]'");

$Isql233 = mysql_query("SELECT SUM(qty) AS qty, SUM(mtr_roll) AS mtr_roll, SUM(tot_mtr) AS tot_mtr 
    FROM media_pur_batch_tot_value 
    WHERE particulars = '$item_id' AND batch_no = '$batch_no'");

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

$rsdata_user233 = mysql_fetch_object($Isql233);

$qty11 = isset($rsdata_user233->qty) ? $rsdata_user233->qty : 0;
$mtr_roll11 = isset($rsdata_user233->mtr_roll) ? $rsdata_user233->mtr_roll : 0;
$tot_mtr11 = isset($rsdata_user233->tot_mtr) ? $rsdata_user233->tot_mtr : 0;

$qty23 = $qty + $qty11;
$mtr_roll23 = $mtr_roll + $mtr_roll11;
$tot_mtr23 = $tot_mtr + $tot_mtr11;

$updateQuery = "UPDATE media_pur_batch_tot_value SET 
    qty = '$qty23',
    mtr_roll = '$mtr_roll23',
    tot_mtr = '$tot_mtr23',
    total_meter = '$tot_mtr23',
    status_type = 'Active'
WHERE 
    particulars = '$item_id' AND batch_no = '$batch_no'";

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




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


include("media_pur_sublist.php");
}

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

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

// echo "SELECT COUNT(*) as total FROM media_sub_list 
//     WHERE roll_name = '$_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 media entry is already in use, so it cannot be deleted.');</script>";
    // echo "karthi";
} else {
    echo "<script>alert('This media entry is not currently in use and can be safely deleted.');</script>";
    $delete_date1=date('Y-m-d');
        $ip_address = $_SERVER['REMOTE_ADDR'];

    $Isql2333 = mysql_query("SELECT SUM(qty) AS qty, SUM(mtr_roll) AS mtr_roll, SUM(tot_mtr) AS tot_mtr 
    FROM media_pur_sublist 
    WHERE particulars = '$_POST[particulars]' AND batch_no = '$_POST[batch_no]' and  id='$_POST[id]' and media_pur_no='$_POST[media_pur_no]'");

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

    $rsdata_user2333 = mysql_fetch_object($Isql2333);

    $qty111 = isset($rsdata_user2333->qty) ? $rsdata_user2333->qty : 0;
    $mtr_roll11 = isset($rsdata_user2333->mtr_roll) ? $rsdata_user2333->mtr_roll : 0;
    $tot_mtr111 = isset($rsdata_user2333->tot_mtr) ? $rsdata_user2333->tot_mtr : 0;

    $Isql23333 = mysql_query("SELECT SUM(qty) AS qty, SUM(mtr_roll) AS mtr_roll, SUM(tot_mtr) AS tot_mtr 
    FROM media_pur_batch_tot_value 
    WHERE particulars = '$_POST[particulars]' AND batch_no = '$_POST[batch_no]' ");

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

    $rsdata_user23333 = mysql_fetch_object($Isql23333);

    $qty1111 = isset($rsdata_user23333->qty) ? $rsdata_user23333->qty : 0;
    $mtr_roll111 = isset($rsdata_user23333->mtr_roll) ? $rsdata_user23333->mtr_roll : 0;
    $tot_mtr1111 = isset($rsdata_user23333->tot_mtr) ? $rsdata_user23333->tot_mtr : 0;

    $qty233 = $qty1111 - $qty111;
    $mtr_roll233 = $mtr_roll111 - $mtr_roll11;
    $tot_mtr233 = $tot_mtr1111 - $tot_mtr111;


    $updateQuery1 = "UPDATE media_pur_batch_tot_value SET 
    qty = '$qty233',
    mtr_roll = '$mtr_roll233',
    tot_mtr = '$tot_mtr233',
    total_meter = '',
    status_type = 'Active',
    delete_date = '$delete_date1',
    last_delete_value = '$tot_mtr111',
    ip_address = '$ip_address'
WHERE 
    particulars = '$_POST[particulars]' AND batch_no = '$_POST[batch_no]'";

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


    $Isql33 = mysql_query("SELECT SUM(total_mtr) AS total_mtr 
    FROM media_purchase 
    WHERE media_pur_no='$_POST[media_pur_no]'");

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

    $rsdata_user33 = mysql_fetch_object($Isql33);

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

    $tot_mtr33 = $tot_mtr1133 - $tot_mtr111;


    $updateQuery13 = "UPDATE media_purchase SET 
      total_mtr = '$tot_mtr33'
    WHERE media_pur_no='$_POST[media_pur_no]'";

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



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



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

   
    include("media_pur_sublist.php");
    }
/******************************************** Main DELETE*****************************************************************/

if ($_POST['action'] == "delete_media") {

    $media_pur_no = $_POST['media_pur_no'];
    $jobcard_no = $_POST['jobcard_no'];

    $Isq25 = mysql_query("SELECT particulars, batch_no FROM media_pur_sublist WHERE media_pur_no='$media_pur_no'");
    if (!$Isq25) {
        die("Select Query Error: " . mysql_error());
    }

    // Loop through all unique (particulars, batch_no) pairs
    while ($rsdata_user25 = mysql_fetch_object($Isq25)) {
        $particulars = $rsdata_user25->particulars;
        $batch_no = $rsdata_user25->batch_no;

        // First SUM from sublist
        $Isql26 = mysql_query("SELECT SUM(qty) AS qty, SUM(mtr_roll) AS mtr_roll, SUM(tot_mtr) AS tot_mtr 
            FROM media_pur_sublist 
            WHERE particulars = '$particulars' AND batch_no = '$batch_no' AND media_pur_no='$media_pur_no'");
        if (!$Isql26) {
            die("SUM Query Error (sublist): " . mysql_error());
        }

        $rsdata_user26 = mysql_fetch_object($Isql26);
        $qty21 = isset($rsdata_user26->qty) ? $rsdata_user26->qty : 0;
        $mtr_ro21 = isset($rsdata_user26->mtr_roll) ? $rsdata_user26->mtr_roll : 0;
        $tot_mtr21 = isset($rsdata_user26->tot_mtr) ? $rsdata_user26->tot_mtr : 0;

        // Second SUM from batch_tot_value
        $Isql27 = mysql_query("SELECT SUM(qty) AS qty, SUM(mtr_roll) AS mtr_roll, SUM(tot_mtr) AS tot_mtr 
            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);
        $qty22 = isset($rsdata_user27->qty) ? $rsdata_user27->qty : 0;
        $mtr_ro22 = isset($rsdata_user27->mtr_roll) ? $rsdata_user27->mtr_roll : 0;
        $tot_mtr22 = isset($rsdata_user27->tot_mtr) ? $rsdata_user27->tot_mtr : 0;

        // Difference calculations
        $qty23 = $qty22 - $qty21;
        $mtr_roll23 = $mtr_ro22 - $mtr_ro21;
        $tot_mtr23 = $tot_mtr22 - $tot_mtr21;

        // Update query
        $updateQuery12 = "UPDATE media_pur_batch_tot_value SET 
            qty = '$qty23',
            mtr_roll = '$mtr_roll23',
            tot_mtr = '$tot_mtr23',
            total_meter = '',
            status_type = 'Active'
            WHERE particulars = '$particulars' AND batch_no = '$batch_no'";

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

    // Now Delete entries
    mysql_query("DELETE FROM media_purchase WHERE media_pur_no='$media_pur_no'");
    mysql_query("DELETE FROM media_pur_sublist WHERE media_pur_no='$media_pur_no'");
    mysql_query("DELETE FROM stock_table WHERE ref_id='$media_pur_no'");
    mysql_query("DELETE FROM media_sub_list WHERE jobcard_no='$jobcard_no'");

    // Redirect after all operations
    header("Location: media_pur_list.php");
    exit();
}
?>

