<?php
include("../../include/db.php");
/*********************************Check item********************************************************/

function check_payments($payment_tems_name)
{
	$Isql=mysql_query("select * from payment_terms where payment_terms_name='$payment_terms_name'");
	$rscount=mysql_num_rows($Isql);
	return $rscount;
}
/*********************************Check item for edit********************************************************/

function check_payment_edit($payment_terms_name)
{
	$sql_query="select * from payment_terms where payment_terms_name='$payment_terms_name'";
$rs=mysql_query($sql_query);
 $rscount1=mysql_num_rows($rs);
 if($rscount1!=0)
 {
 while($rsdata=mysql_fetch_object($rs))
 {
 $id=$rsdata->id;
 } 
	return $id;
 }
 }
 
 /********************************** ADD  QUERY ***************************************/
if(($_POST['action']=="ADD")) 
{
$payment_terms_name=ucfirst($_POST['payment_terms_name']);
$payment_terms_remarks=$_POST['payment_terms_remarks'];
$create_date=date('Y-m-d');
$check_item=check_payments($payment_terms_name);
if($check_item==0)
{
$Insql=mysql_query("insert into payment_terms(payment_terms_name,payment_terms_remarks,create_date)values('$payment_terms_name','$payment_terms_remarks','$create_date')");
echo "Added Successfully!!";
}
else
{
echo "Already Exists!!";
}
}
/********************************** UPDATE QUERY ***************************************/
if(($_POST['action']=="EDIT"))
{
$edit_id = $_POST['edit_id']; 
$payment_terms_name=ucfirst($_POST['payment_terms_name']);
$payment_terms_remarks=$_POST['payment_terms_remarks'];
$create_date=date('Y-m-d');
$check_item=check_payment_edit($payment_terms_name);

if(($check_item==$_POST['edit_id'])||($check_item==''))
{
$Insql=mysql_query("update payment_terms set payment_terms_name='$payment_terms_name',payment_terms_remarks='$payment_terms_remarks',create_date='$create_date' where  id='$_POST[edit_id]'");
echo "Updated Successfully!!";
}
else
{
	echo "Already Exists";
	}
}
/********************************************DELETE*****************************************************************/
if($_POST['action']=="delete_payments")
{ 		 
		   $delete2="delete from payment_terms where id='$_POST[id]'";
			mysql_query($delete2);
			echo "<script>alert('Deleted Succesfully')</script>";
			include("payment_terms_entry_list.php");
}


?>