<?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")) 
{
 
$create_date1=date('Y-m-d');
$acadamic_yr=explode("-",$create_date1);
$acdmc_yr_res=$acadamic_yr[0];
$entrydates=$_POST['entrydates'];
$particulars=$_POST['particulars'];
$batch_no=$_POST['batch_no'];
$uom=$_POST['uom'];
$nos=$_POST['nos'];
$packing=$_POST['packing'];
$qty=$_POST['qty'];
$empty_return_no=$_POST['empty_return_no'];
$qty_hid1=$_POST['qty_hide1'];

$ink_sub_result = mysql_query("SELECT * FROM ink_sublist WHERE prod_size='$particulars' AND batch_no='$batch_no'");
$ink_sub_row = mysql_fetch_array($ink_sub_result);
// echo $ink_sub_row['ink_sub_id'];
$Insql1=mysql_query("insert into empty_can_dc_sublist(entrydates,particulars,batch_no,uom,nos,packing,qty,qty_hid,ink_sub_id,empty_can_return_no)values('$_POST[entrydates]','$_POST[particulars]','$_POST[batch_no]','$_POST[uom]','$_POST[nos]','$_POST[packing]','$_POST[qty]','$_POST[qty_hide1]','$ink_sub_row[ink_sub_id]','$empty_return_no')");

$insert_id=mysql_insert_id();
//echo "insert into empty_can_dc_sublist(entrydates,particulars,batch_no,uom,nos,packing,qty,qty_hid,ink_sub_id,empty_can_return_no)values('$_POST[entrydates]','$_POST[particulars]','$_POST[batch_no]','$_POST[uom]','$_POST[nos]','$_POST[packing]','$_POST[qty]','$_POST[qty_hide1]','$ink_sub_row[ink_sub_id]','$empty_return_no')";
include("empty_can_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_sublist")) 
{ 
$del_Insql=mysql_query("delete from empty_can_dc_sublist where id='$_GET[delete_id]'");
//echo "delete from empty_can_dc_sublist where id='$_GET[delete_id]'";
include("empty_can_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'];
$create_date1=date('Y-m-d');
$acadamic_yr=explode("-",$create_date1);
$acdmc_yr_res=$acadamic_yr[0];
$entrydates=$_POST['entrydates'];
$particulars=$_POST['particulars'];
$batch_no=$_POST['batch_no'];
$uom=$_POST['uom'];
$nos=$_POST['nos'];
$packing=$_POST['packing'];
$qty=$_POST['qty'];
$empty_return_no=$_POST['empty_return_no'];
$qty_hid1=$_POST['qty_hide1'];

$ink_sub_result = mysql_query("SELECT * FROM ink_sublist WHERE prod_size='$particulars' AND batch_no='$batch_no'");
$ink_sub_row = mysql_fetch_array($ink_sub_result);

$update_return=mysql_query("update empty_can_dc_sublist set entrydates='$entrydates',particulars='$_POST[particulars]',ink_sub_id='$ink_sub_row[ink_sub_id]',batch_no='$batch_no',uom='$uom',nos='$nos',packing='$packing',qty='$qty',qty_hid='$qty_hid1',empty_can_return_no='$empty_return_no' where `empty_can_return_no`='$empty_return_no'and `id`='$edit_id'");	

$_GET['edit_id']='';
include("empty_can_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")) 
{
 $empty_return_no=$_POST['empty_return_no'];
 $date_time=$_POST['date_time'];

 $create_date1=date('Y-m-d');
 $date=date("Y-m-d");
 
 $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';
}

$name_ref="Ink Empty Can";
 if($_POST['total']!='')
 {
$total=$_POST['total'];
 }
if($_POST['pac_total']!='')
{
$pac_total=$_POST['pac_total'];
}
$edit_id=$_POST['edit_id'];

 $count_rows=mysql_num_rows(mysql_query("Select * from empty_can_dc where empty_can_return_no='$empty_return_no' "));
 if($count_rows==0)
{
	
$ins_qty55=mysql_query("insert into empty_can_dc(empty_can_return_no,ref_name,packing_total,qty_total,create_date,empty_can_time,acc_year)values('$empty_return_no','$name_ref','$pac_total','$total','$create_date1','$date_time','$acc_year')");
}
else
{
	
	$update_return=mysql_query("update empty_can_dc set empty_can_return_no='$empty_return_no',empty_can_time='$date_time',packing_total='$pac_total',ref_name='$name_ref',qty_total='$total',create_date='$create_date1' where empty_can_return_no='$empty_return_no' ");	

}
//echo "insert into return_media_purchase(empty_can_return_no,ref_name,packing_total,qty_total,create_date,empty_can_time,acc_year)values('$empty_return_no','$name_ref','$pac_total','$total','$create_date1','$date_time','$acc_year')";
}
/**********************************main edit ***************************************/

if(($_POST['action']=="UPDATE")) 
{
 $empty_return_no=$_POST['empty_return_no'];
 $date_time=$_POST['date_time'];
 $create_date1=date('Y-m-d');

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

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

 $edit_id=$_POST['edit_id'];
 $name_ref="Ink Empty Can";
 
 $update_return=mysql_query("update empty_can_dc set empty_can_return_no='$empty_return_no',empty_can_time='$date_time',packing_total='$pac_total',ref_name='$name_ref',qty_total='$total',create_date='$create_date1' where empty_can_return_no='$empty_return_no' ");
echo "update empty_can_dc set empty_can_return_no='$empty_return_no',empty_can_time='$date_time',packing_total='$pac_total',ref_name='$name_ref',qty_total='$total',create_date='$create_date1' where empty_can_return_no='$empty_return_no' ";
//update_return=mysql_query("update empty_can_dc 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' ");
}
/**********************************main del ***************************************/


if($_GET['action']=="delete_empty_main")
{ 

    $delete2="delete from empty_can_dc where id='$_GET[delete_id]'";
    mysql_query($delete2);
   
    $Delsql5="delete from empty_can_dc_sublist where  empty_can_return_no='$_GET[empty_return_noss]' ";
    mysql_query($Delsql5);
  
    $msg="<span class='success'>Successfully Deleted </span>";
    echo $msg;
}
?>