<?php
include("../../include/db.php");
error_reporting(0);
/********************************** CHECK ITEM FOR PARTY CREATION***************************************/
function check_party_creat($party_name,$mob_no)
  {   
  $sql_select = "select * from party_creation where party_name='$party_name' and `party_type`='Customer' and delete_status!='1'";
       $rs=mysql_query($sql_select);
       $rs_count=mysql_num_rows($rs);
       return $rs_count; 
}

function get_party_creat_edit($party_name,$mob_no)
{
  $sql_select = "select * from party_creation where party_name='$party_name'  and `party_type`='Customer' and delete_status!='1'";
  $rs=mysql_query($sql_select);
  $rscount1=mysql_num_rows($rs);
    if($rscount1!=0)
    {
      while($rsdata=mysql_fetch_object($rs))
     {
      $id=$rsdata->party_id;
     } 
	return $id;
    }
   }

function get_party_id($party_name,$party_mob)
{
	$sql_user="select * from party_creation where party_name='$party_name' and mobile_no='$party_mob' and delete_status!='1' and `party_type`='Customer'  order by party_name ASC ";
	$rs_user=mysql_query($sql_user);
	while($rsdata_user=mysql_fetch_object($rs_user))
	{ 
	$party_id=$rsdata_user->party_id;
	}
	return $party_id;
} 
 /********************************** ADD  QUERY ***************************************/
if(($_POST['action']=="ADD")) 
{
$party_no=$_POST['party_no'];
$party_name=ucfirst($_POST['party_name']);
$pho_no=$_POST['pho_no'];
$mob_no=$_POST['mob_no'];
$email=$_POST['email'];
$address=$_POST['address'];
$city=$_POST['city'];
$description=$_POST['description'];
$cst_no=$_POST['cst_no'];
$tin_no=$_POST['tin_no'];
$gst_no=$_POST['gst_no'];
$confirmrate=$_POST['confirmrate'];
$party_type=$_POST['party_type'];
$status_type=$_POST['status_type'];
$created_date=date('Y-m-d');
$acad_yr=explode("-",$created_date);
$acad_yr_res=$acad_yr[0];
$check_party=check_party_creat($party_name,$mob_no);

if(($check_party=='0'))
{
$Insql=mysql_query("insert into party_creation(party_no,party_name,mobile_no,phone_no,email_id,address,city,create_date,cst_no,tin_no,gst_no,party_type,status_type,confirmrate)values('$party_no','$party_name','$mob_no','$pho_no','$email','$address','$city','$created_date','$cst_no','$tin_no','$gst_no','$party_type','$status_type','$confirmrate')");
// echo "insert into party_creation(party_no,party_name,mobile_no,phone_no,email_id,address,city,create_date,cst_no,tin_no,gst_no,party_type,status_type)values('$party_no','$party_name','$mob_no','$pho_no','$email','$address','$city','$created_date','$cst_no','$tin_no','$gst_no','$party_type','$status_type')";
 echo "Added Successfully!!";
}
else
{
	echo "Already Exist";
}
}

/********************************** UPDATE QUERY ***************************************/
if(($_POST['action']=="EDIT"))
{
$edit_id = $_POST['edit_id']; 
$party_no=$_POST['party_no'];

$party_name=ucfirst($_POST['party_name']);
$pho_no=$_POST['pho_no'];
$mob_no=$_POST['mob_no'];
$email=$_POST['email'];
$address=$_POST['address'];
$city=$_POST['city'];
$due_days=$_POST['due_days'];
$cst_no=$_POST['cst_no'];
$tin_no=$_POST['tin_no'];
$gst_no=$_POST['gst_no'];
$confirmrate=$_POST['confirmrate'];
$description=$_POST['description'];
$created_date=date('Y-m-d');
$acad_yr=explode("-",$created_date);
$acad_yr_res=$acad_yr[0];
$party_type=$_POST['party_type'];
$status_type=$_POST['status_type'];
$check_par_edit=get_party_creat_edit($party_name,$mob_no);

$Insql=mysql_query("update party_creation set party_no='$party_no',party_name='$party_name',mobile_no='$mob_no',phone_no='$pho_no',email_id='$email',address='$address',city='$city',cst_no='$cst_no',tin_no='$tin_no',create_date='$created_date',party_type='$party_type',status_type='$status_type',gst_no='$gst_no',confirmrate='$confirmrate' where  party_id='$_POST[edit_id]'");
echo "Updated Successfully!!";


   $Upsql1="update delivery_challan_main set customer_name='$party_name' where customer_name LIKE '%$party_name%'  ";
   mysql_query($Upsql1);
   
   $Upsql2="update delivery_challan_sub set cust_name='$party_name' where cust_name LIKE '%$party_name%'  ";
   mysql_query($Upsql2);
   
   $Upsql3="update fusing_production_main set cust_name='$party_name' where cust_name LIKE '%$party_name%' ";
   mysql_query($Upsql3);
   
   $Upsql4="update inward_main cust_name='$party_name' where cust_name LIKE '%$party_name%'  ";
   mysql_query($Upsql4);
   
   $Upsql5="update inward_sub_list set in_cust_name='$party_name' where in_cust_name LIKE '%$party_name%'  ";
   mysql_query($Upsql5);
   
   $Upsql6="update printer_production_main set cust_name='$party_name' where cust_name LIKE '%$party_name%'  ";
   mysql_query($Upsql6);

   $Upsql7="update jobcard_main set cust_name='$party_name' where cust_name LIKE '%$party_name%'  ";
   mysql_query($Upsql7);
   
   $Upsql8="update jobcard_sublist set customer_name='$party_name' where customer_name LIKE '%$party_name%' ";
   mysql_query($Upsql8);
   
}

/********************************************MAIN DELETE*****************************************************************/
if($_POST['action']=="del_party")
{ 		
			$party_no=$_POST['party_no'];
		   $delete2="update party_creation set delete_status='1' where party_id='$_POST[id]'";
			mysql_query($delete2);
			echo "<script>alert('Deleted Succesfully')</script>";
			include("party_entry_list.php");
}

// /******************************************** Auto Active → In-active (no job in 60 days) ***********************************************/

// // 1. Get all active customers
// echo $sql = "SELECT party_name 
//         FROM party_creation 
//         WHERE status_type = 'Active' 
//           AND delete_status = '0' 
//           AND approve_status = '1'";
// $rs = mysql_query($sql);

// while($row = mysql_fetch_assoc($rs)) {
//     $partyName = $row['party_name'];

//     // 2. Check jobcard activity in last 60 days
//     $check = "SELECT COUNT(*) AS cnt 
//               FROM jobcard_sublist 
//               WHERE customer_name = '$partyName'
//                 AND job_date >= DATE_SUB(CURDATE(), INTERVAL 60 DAY)";
//     $rsCheck = mysql_query($check);
//     $result = mysql_fetch_assoc($rsCheck);

//     // 3. If no job work → mark as In-active
//     if ($result['cnt'] == 0) {
//         $update = "UPDATE party_creation 
//                    SET status_type = 'In-active' 
//                    WHERE party_name = '$partyName'";
//         mysql_query($update);
//         echo "Updated: {$partyName} → In-active<br>";
//     }
// }




?>