<?php
include("../../include/db.php");
/**********************************Functions ***************************************/
function get_particular_name_ret_media($particulars)
{
 $sql="select distinct particulars  from media_pur_sublist where particulars_id='$particulars'";
$rs=mysql_query($sql);
$rscount=mysql_num_rows($rs);
while($rsdata=mysql_fetch_array($rs))
{
	   $particulars12=$rsdata['particulars'];
}
return $particulars12;
}
function get_particular_name_ret_ink($ink_particulars)
{
$sql="select distinct particulars from ink_pur_sublist where particulars_id='$ink_particulars'";
$rs=mysql_query($sql);
$rscount=mysql_num_rows($rs);
while($rsdata=mysql_fetch_array($rs))
{
	   $particulars44=$rsdata['particulars'];
}
return $particulars44;
}
function get_pur_tot_con_ret($job_card_nos)
{
	 $pur_no=explode("-",$job_card_nos);
if($pur_no[0]=='IPR')
{
$sql="select sum(qty) as qty from ink_pur_sublist where ink_pur_no='$job_card_nos'";
}
else
{
$sql="select sum(qty) as qty from media_pur_sublist where media_pur_no='$job_card_nos'";
	
}
$rs=mysql_query($sql);
$rscount=mysql_num_rows($rs);
while($rsdata=mysql_fetch_array($rs))
{
	   $qty11=$rsdata['qty'];
}
return $qty11;
}
function get_pur_return_tot_con_ret($job_card_nos)
{
	 $pur_no1=explode("-",$job_card_nos);
if($pur_no1[0]=='IPR')
{
$sql1="select sum(qty) as qty from return_ink_pur_sublist where ink_pur_no='$job_card_nos'";
}
else
{
$sql1="select sum(qty) as qty from return_media_pur_sublist where media_pur_no='$job_card_nos'";
	
}
$rs1=mysql_query($sql1);
$rscount1=mysql_num_rows($rs1);
while($rsdata1=mysql_fetch_array($rs1))
{
	   $qty22=$rsdata1['qty'];
}
return $qty22;
}
/**********************************Media sub add ***************************************/

if(($_POST['action']=="ADD")) 
{
 $media_pur_no=$_POST['job_card_nos'];
$create_date1=date('Y-m-d');
$acadamic_yr=explode("-",$create_date1);
$acdmc_yr_res=$acadamic_yr[0];
$entrydates=$_POST['entrydates'];
$invoice_no=$_POST['invoice_no'];
$sup_name=$_POST['sup_name'];
$particulars=$_POST['particulars'];
$part_name=get_particular_name_ret_media($particulars);
$qty=$_POST['qty'];
$mtr_roll=$_POST['mtr_roll'];
$tot_mtr=$_POST['tot_mtr'];
$return_no=$_POST['return_no'];
$qty_hid=$_POST['qty_hide'];

$Insql1=mysql_query("insert into return_media_pur_sublist(media_pur_no,entrydates,invoice_no,supplier_name,particulars,qty,mtr_roll,tot_mtr,return_no,particulars_id,qty_hid)values('$media_pur_no','$_POST[entrydates]','$_POST[invoice_no]','$_POST[sup_name]','$part_name','$_POST[qty]','$_POST[mtr_roll]','$_POST[tot_mtr]','$return_no','$particulars','$qty_hid')");

$insert_id=mysql_insert_id();

$ins_qty=mysql_query("insert into stock_table(date,group_id,item_id,item_name,out_qty,voucher_name,ref_id,insert_id,total_mtr)values('$_POST[entrydates]','Media ','$particulars','$part_name','$_POST[tot_mtr]','Consumable Return','$_POST[return_no]','$insert_id','$qty')");

include("consumable_return_sublist.php");
}
/**********************************Ink sub add ***************************************/

if(($_POST['action']=="INK ADD")) 
{
 $ink_pur_no=$_POST['job_card_nos'];
$create_date1=date('Y-m-d');
$acadamic_yr=explode("-",$create_date1);
$acdmc_yr_res=$acadamic_yr[0];
$entrydates=$_POST['ink_entrydates'];
$invoice_no=$_POST['ink_invoice_no'];
$sup_name=$_POST['ink_sup_name'];
$ink_particulars=$_POST['ink_particulars'];
$part_name_ink=get_particular_name_ret_ink($ink_particulars);
$ink_qty=$_POST['ink_qty'];
$return_no=$_POST['return_no'];
$qty_hid=$_POST['qty_hid'];

$ink_Insql1=mysql_query("insert into return_ink_pur_sublist(ink_pur_no,entrydates,invoice_no,supplier_name,particulars,qty,return_no,particulars_id,qty_hid)values('$ink_pur_no','$_POST[ink_entrydates]','$_POST[ink_invoice_no]','$_POST[ink_sup_name]','$part_name_ink','$_POST[ink_qty]','$return_no','$ink_particulars','$qty_hid')");
 
$insert_id=mysql_insert_id();

$ins_qty=mysql_query("insert into stock_table(date,group_id,item_id,item_name,out_qty,voucher_name,ref_id,insert_id,supplier_name)values('$create_date1','Ink','$ink_particulars','$part_name_ink','$ink_qty','Consumable Return','$_POST[return_no]','$insert_id','$_POST[ink_sup_name]')");

include("ink_sublist.php");

}
/**********************************media sub del ***************************************/

if(($_GET['action']=="delete_trans")) 
{ 
$del_Insql=mysql_query("delete from return_media_pur_sublist where id='$_GET[delete_id]'");

$del_Insql55=mysql_query("delete from stock_table where insert_id='$_GET[delete_id]' and ref_id='$_GET[return_no]' and group_id='Media'");

include("consumable_return_sublist.php");
}
/**********************************Ink sub del ***************************************/

if(($_GET['action']=="delete_ink")) 
{
$del_Insql44=mysql_query("delete from return_ink_pur_sublist where id='$_GET[delete_id]'");
		
$del_Insql3434=mysql_query("delete from stock_table where insert_id='$_GET[delete_id]' and ref_id='$_GET[return_no]' and group_id='Ink'");

include("ink_sublist.php");
}
/**********************************media sub edit ***************************************/


if(($_POST['action']=="EDIT")) 
{
$edit_id=$_GET['edit_id'];
$media_pur_no=$_POST['job_card_nos'];
$create_date1=date('Y-m-d');
$acadamic_yr=explode("-",$create_date1);
$acdmc_yr_res=$acadamic_yr[0];
$entrydates=$_POST['entrydates'];
$invoice_no=$_POST['invoice_no'];
$sup_name=$_POST['sup_name'];
$particulars=$_POST['particulars'];
$part_name=get_particular_name_ret_media($particulars);
$qty=$_POST['qty'];
$mtr_roll=$_POST['mtr_roll'];
$tot_mtr=$_POST['tot_mtr'];
$return_no=$_POST['return_no'];
 $qty_hid=$_POST['qty_hide'];

$update_return=mysql_query("update return_media_pur_sublist set media_pur_no='$media_pur_no',entrydates='$entrydates',return_no='$return_no',invoice_no='$invoice_no',supplier_name='$_POST[sup_name]',particulars='$part_name',particulars_id='$particulars',qty='$qty',mtr_roll='$mtr_roll',tot_mtr='$tot_mtr',qty_hid='$qty_hid' where `media_pur_no`='$media_pur_no'and `id`='$edit_id'");	


$update_return=mysql_query("update stock_table set date='$entrydates',group_id='Media',item_id='$particulars',item_name='$part_name',out_qty='$tot_mtr',total_mtr='$qty',voucher_name='Consumable Return' where ref_id='$_POST[return_no]' and  insert_id='$edit_id' ");

$_GET['edit_id']='';
include("consumable_return_sublist.php");

}
/**********************************Ink sub edit ***************************************/

if(($_POST['action']=="INK EDIT")) 
{ 
$edit_id=$_POST['edit_id'];
$media_pur_no=$_POST['job_card_nos'];
$create_date1=date('Y-m-d');
$acadamic_yr=explode("-",$create_date1);
$acdmc_yr_res=$acadamic_yr[0];
$entrydates=$_POST['ink_entrydates'];
$invoice_no=$_POST['ink_invoice_no'];
$sup_name=$_POST['ink_sup_name'];
$ink_particulars=$_POST['ink_particulars'];
$part_name_ink=get_particular_name_ret_ink($ink_particulars);
$ink_qty=$_POST['ink_qty'];
$return_no=$_POST['return_no'];
$qty_hid=$_POST['qty_hid'];

$update_return=mysql_query("update return_ink_pur_sublist set entrydates='$entrydates',invoice_no='$invoice_no',supplier_name='$_POST[ink_sup_name]',particulars_id='$ink_particulars',return_no='$return_no',qty='$ink_qty',particulars='$part_name_ink',qty_hid='$qty_hid' where `ink_pur_no`='$media_pur_no' and `id`='$edit_id'");	


$update_return=mysql_query("update stock_table set date='$create_date1',group_id='INK',item_id='$ink_particulars',item_name='$part_name_ink',out_qty='$ink_qty',voucher_name='Consumable Return',ref_id='$_POST[return_no]' where  `insert_id`='$edit_id'");	
	

include("ink_sublist.php");
}


/**********************************main add ***************************************/

if(($_POST['action']=="SAVE")) 
{
 $return_no=$_POST['return_no'];
 $date_time=$_POST['date_time'];
 $job_card_nos=$_POST['job_card_nos'];
 $get_pur_tot=get_pur_tot_con_ret($job_card_nos);
  $get_pur_tot_ret=get_pur_return_tot_con_ret($job_card_nos);

 $create_date1=date('Y-m-d');
 $date=date("Y-m-d");
 $cards=explode("-",$job_card_nos);
 
 $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($_POST['total']!='')
 {
$res_total=$_POST['total'];
 }
if($_POST['ink_total_res']!='')
{
$res_total=$_POST['ink_total_res'];
}
$edit_id=$_POST['edit_id'];
 
if($cards[0]=="MPR")
 {
	 $name_ref="Media Ref";

 }
elseif($cards[0]=="IPR")
 {
	$name_ref="Ink Ref";
 }
 $count_rows=mysql_num_rows(mysql_query("Select * from return_media_purchase where return_no='$return_no' "));
 if($count_rows==0)
{
	
$ins_qty55=mysql_query("insert into return_media_purchase(return_no,media_ink_time,purchase_no,total,ref_name,create_date,acc_year)values('$return_no','$date_time','$job_card_nos','$res_total','$name_ref','$create_date1','$acc_year')");
}
else
{
	
	$update_return=mysql_query("update return_media_purchase set return_no='$return_no',media_ink_time='$date_time',purchase_no='$job_card_nos',ref_name='$name_ref',total='$res_total',create_date='$create_date1' where return_no='$return_no' ");	

}
if($get_pur_tot==$get_pur_tot_ret)
{
	 if($cards[0]=='IPR')
	 {
		 $update_return=mysql_query("update ink_purchase set return_status='1' where ink_pur_no='$job_card_nos'");
	 }
	 else
	 {echo "update media_purchase set return_status='1' where media_pur_no='$job_card_nos'";
		  $update_return=mysql_query("update media_purchase set return_status='1' where media_pur_no='$job_card_nos'");

	 }

	
}

}
/**********************************main edit ***************************************/

if(($_POST['action']=="UPDATE")) 
{
 $return_no=$_POST['return_no'];
 $date_time=$_POST['date_time'];
 $job_card_nos=$_POST['job_card_nos'];
 $create_date1=date('Y-m-d');
 $cards=explode("-",$job_card_nos);
  $get_pur_tot=get_pur_tot_con_ret($job_card_nos);
  $get_pur_tot_ret=get_pur_return_tot_con_ret($job_card_nos);

 if($_POST['total']!='')
 {
	$res_total=$_POST['total'];

 }
  if($_POST['ink_total_res']!='')
  {
	$res_total=$_POST['ink_total_res'];
  }

 $edit_id=$_POST['edit_id'];
 $cards[0];
 if($cards[0]=="MPR")
 {
	$name_ref="Media Ref";
 }
 elseif($cards[0]=="IPR")
 {
	$name_ref="Ink Ref";
	 
 }
 

	$update_return=mysql_query("update return_media_purchase set media_ink_time='$date_time',purchase_no='$job_card_nos',ref_name='$name_ref',total='$res_total',create_date='$create_date1' where return_no='$return_no' ");	
if($get_pur_tot==$get_pur_tot_ret)
{
	 if($cards[0]=='IPR')
	 {
		 $update_return=mysql_query("update ink_purchase set return_status='1' where ink_pur_no='$job_card_nos'");
	 }
	 else
	 {
		  $update_return=mysql_query("update media_purchase set return_status='1' where media_pur_no='$job_card_nos'");

	 }

	
}

}
/**********************************main del ***************************************/


if($_GET['action']=="delete_media_ink")
{ 
   $purchase=$_GET['purchase_noss'];
   $get_pur_tot=get_pur_tot_con_ret($_GET['purchase_noss']);

   $delete2="delete from return_media_purchase where id='$_GET[delete_id]'";
   mysql_query($delete2);
   
   $Delsql5="delete from return_media_pur_sublist where  return_no='$_GET[return_noss]' ";
   mysql_query($Delsql5);
  
   $Delsql5="delete from return_ink_pur_sublist where  return_no='$_GET[return_noss]' ";
   mysql_query($Delsql5);
  
  
    $Delsql66="delete from stock_table where  ref_id='$_GET[return_noss]' ";
    mysql_query($Delsql66);
	
	
      $update_return=mysql_query("update ink_purchase set return_status='0' where ink_pur_no='$_GET[purchase_noss]'");
   	  $update_return=mysql_query("update media_purchase set return_status='0' where media_pur_no='$_GET[purchase_noss]'");
      $msg="<span class='success'>Successfully Deleted </span>";
      echo $msg;
}
?>