<?php
	//Connects to your Database
	include "../db_connection/db_connection.php";
	include "../config/config.php";		
	$page_id	= base64_decode(base64_decode(base64_decode($_GET['page_id']))); //THIS IS PAGE ID  
	if($page_id==0) //this is for client request	
	{ 		
		$po_no		=	$_POST['po_no'];
		$co_cod		=	base64_decode(base64_decode(base64_decode($_POST['co_cod']))); //THIS IS PAGE ID  
		$pod		=	$_POST['pod']; 
		$pod		= 	str_replace("/", "-", $pod);
		
		$SLCTg = "SELECT * FROM guest_user WHERE party_det_id = '$co_cod'";
		$QURYg = mysqli_query($connect,$SLCTg);
		$FTCHg = mysqli_fetch_object($QURYg);
		
		$INSRT		= "INSERT INTO po_entry(`party_det_id`, `po_no`, `po_date`, `post_dt`) VALUES('$co_cod','$po_no','$pod','$cur_time')";		
		$QURY		= mysqli_query($connect,$INSRT);
		
		$SLCT_pt = "SELECT * FROM party_det WHERE  party_det_id = '$co_cod'";
		$QURY_pt = mysqli_query($connect,$SLCT_pt);
		if(mysqli_num_rows($QURY_pt)>0)
		{
			$FTCH_pt = mysqli_fetch_object($QURY_pt);
			$pt_name	=	$FTCH_pt->cmp_name;
			$sms_code	=	$FTCH_pt->party_code;
			$cli_mail	=	$FTCH_pt->email_id_1;
			$mobile_no	=	$FTCH_pt->con_no_1;
		}
		else
		{
			$pt_name='';
			$cli_mail='';
		}
		
		$SLCT_sk = "SELECT  MAX(po_entry_id) as max_ids FROM po_entry ORDER BY po_entry_id ASC";
		$QURY_sk = mysqli_query($connect,$SLCT_sk);  
		$FTCH_sk = mysqli_fetch_object($QURY_sk);
		$max_id = $FTCH_sk->max_ids;
		
		$lp = 1;
		$SLCT_aj = "SELECT * FROM po_temp_des WHERE party_det_id = '$co_cod'"; 
		$QURY_aj = mysqli_query($connect,$SLCT_aj);
		
		$SLCT_pre_po_ids="SELECT group_concat(`po_entry_id`) as pre_po_id FROM  `po_entry` WHERE  `party_det_id` ='$co_cod'";
		$QURY_pre_po_ids = mysqli_query($connect,$SLCT_pre_po_ids);
		$FTCH_pre_po_ids = mysqli_fetch_object($QURY_pre_po_ids);
			
		while($FTCH_aj = mysqli_fetch_object($QURY_aj))
		{
			$cat_id = $FTCH_aj->design_det_id;
			
			$SLCT_tp = "SELECT * FROM design_det WHERE statu_s ='active' AND design_det_id='$cat_id'";
			$QURY_tp = mysqli_query($connect,$SLCT_tp);
			$FTCH_tp = mysqli_fetch_object($QURY_tp);
			
			$SLCT_pre_des="SELECT * FROM  `po_entry_det` WHERE  `po_entry_id` IN ($FTCH_pre_po_ids->pre_po_id) AND cancelled='no' AND `design_det_id` ='$cat_id'";
			$QURY_pre_des = mysqli_query($connect,$SLCT_pre_des);
			if(mysqli_num_rows($QURY_pre_des)>0)
			{
			}
			else
			{				
				$INSRT_d	= "INSERT INTO po_entry_det(`po_entry_id`,`master_cat_id`,`design_det_id`,`post_dt`) VALUES('$max_id','$FTCH_tp->master_cat_id','$cat_id','$cur_time')";	
				$QURY_d		= mysqli_query($connect,$INSRT_d);
			}
			//THI IS FOR UPDATING THE DESIGN STATUS IN DESIGN SELECTION TABLE
			$UPDATE = "UPDATE guest_user_details SET ord_status = 'yes',update_dt = '$cur_time' WHERE design_det_id = '$cat_id' AND guest_user_id='$FTCHg->guest_id'";
			$QRY    = mysqli_query($connect,$UPDATE); 
			$lp ++;				
		}
		
		$DELT = "DELETE FROM po_temp_des WHERE party_det_id = '$co_cod'";
		$QRY12    = mysqli_query($connect,$DELT); 	
?>
		<script type="text/javascript">
            window.location='../po_entry_list.php';
        </script>
<?php
	}	
	else if($page_id==1) //this is for client order request	
	{ 		
		
		$po_no		=	$_POST['po_no'];
		$co_cod		=	base64_decode(base64_decode(base64_decode($_POST['co_cod']))); //THIS IS PAGE ID  
		$pod		=	$_POST['pod']; 
		$pod		= 	str_replace("/", "-", $pod);
		//echo
		$INSRT		= "INSERT INTO po_entry(`party_det_id`, `po_no`, `po_date`, `post_dt`) VALUES('$co_cod','$po_no','$pod','$cur_time')";		
		//die();
		$QURY		= mysqli_query($connect,$INSRT);
		
		$SLCT_pt = "SELECT * FROM party_det WHERE  party_det_id = '$co_cod'";
		$QURY_pt = mysqli_query($connect,$SLCT_pt);
		if(mysqli_num_rows($QURY_pt)>0)
		{
			$FTCH_pt = mysqli_fetch_object($QURY_pt);
			$pt_name	=	$FTCH_pt->cmp_name;
			$cli_mail	=	$FTCH_pt->email_id_1;
			$mobile_no=$FTCH_pt->con_no_1;
		}
		else
		{
			$pt_name='';
			$cli_mail='';
		}
		
		$SLCT_sk = "SELECT  MAX(po_entry_id) as max_ids FROM po_entry ORDER BY po_entry_id ASC";
		$QURY_sk = mysqli_query($connect,$SLCT_sk);  
		$FTCH_sk = mysqli_fetch_object($QURY_sk);
		$max_id = $FTCH_sk->max_ids;
		
		$lp = 1;
		$SLCT_aj = "SELECT * FROM po_temp_des WHERE party_det_id = '$co_cod'"; 
		$QURY_aj = mysqli_query($connect,$SLCT_aj);
			
		$SLCT_pre_po_ids="SELECT group_concat(`po_entry_id`) as pre_po_id FROM  `po_entry` WHERE  `party_det_id` ='$co_cod'";
		$QURY_pre_po_ids = mysqli_query($connect,$SLCT_pre_po_ids);
		$FTCH_pre_po_ids = mysqli_fetch_object($QURY_pre_po_ids);
		
		while($FTCH_aj = mysqli_fetch_object($QURY_aj))
		{
			$cat_id = $FTCH_aj->design_det_id;
			
			$SLCT_tp = "SELECT * FROM design_det WHERE statu_s ='active' AND design_det_id='$cat_id'";
			$QURY_tp = mysqli_query($connect,$SLCT_tp);
			$FTCH_tp = mysqli_fetch_object($QURY_tp);
			
			$SLCT_pre_des="SELECT * FROM  `po_entry_det` WHERE  `po_entry_id` IN ($FTCH_pre_po_ids->pre_po_id) AND `design_det_id` ='$cat_id'  AND cancelled='no'";
			$QURY_pre_des = mysqli_query($connect,$SLCT_pre_des);
			if(mysqli_num_rows($QURY_pre_des)>0)
			{
			}
			else
			{
				$INSRT_d	= "INSERT INTO po_entry_det(`po_entry_id`,`master_cat_id`,`design_det_id`,`post_dt`) VALUES('$max_id','$FTCH_tp->master_cat_id','$cat_id','$cur_time')";	
				$QURY_d		= mysqli_query($connect,$INSRT_d);
			}
			//THI IS FOR UPDATING THE DESIGN STATUS IN DESIGN SELECTION TABLE
			$UPDATE = "UPDATE design_sel_det SET ord_status = 'yes',update_dt = '$cur_time' WHERE design_det_id = '$cat_id' AND party_det_id='$co_cod'";
			$QRY    = mysqli_query($connect,$UPDATE); 
			$lp ++;				
		}
		
		$DELT = "DELETE FROM po_temp_des WHERE party_det_id = '$co_cod'";
		$QRY12    = mysqli_query($connect,$DELT); 
		
		$tot=0;
		$SLCT = "SELECT `master_cat_id`, count(*) as c FROM `po_entry_det` WHERE `po_entry_id`='$max_id' AND cancelled = 'no' GROUP By `master_cat_id`;";
		$QURY = mysqli_query($connect,$SLCT);
		while($fetch	=	mysqli_fetch_object($QURY))
		{
			$tot = $tot + $fetch->c;
		}
		
		include "../mail/po_order_send_mail.php";
		
		$tto=$cli_mail;
		$subject="Reg:Order Confirmation Details";
		$headers = "MIME-Version: 1.0" . "\r\n";
		$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
		// More headers
		$headers .= 'From: Thahaayaa Sales Team <sales@thahaayaa.com>' . "\r\n";				
		$headers .= 'Bcc:'.$cc_id. "\r\n";	
		//mail("$tto",$subject,$mail_body,$headers);
		
		if($mobile_no!='')
		{
			$tot_qty = $tot;
			$to = $mobile_no;
			$sms = "Thahaayaa thanks to you for new order and Order No:".$po_no.",Qty: ".$tot_qty." We took over it into processing, you will receive the delivery shortly. Further-9894347273";
			$sms_url   	= 	sprintf($sms_domain, $workingkey, $to, $senderid, urlencode($sms));
			//openurl($sms_url);
		}	
?>
		<script type="text/javascript">
            window.location='../po_entry_list.php';
        </script>
<?php
	}
	else if($page_id==2) //this is for basic details add page	
	{			
		$bill_flg = '';
		$flg = $_POST['flg'];
		$po_id		=	base64_decode(base64_decode(base64_decode($_POST['po_id'])));
		$max_id = $po_id;
		$co_cod		=	base64_decode(base64_decode(base64_decode($_POST['co_cod']))); //THIS IS PAGE ID  
		
		if($flg=="add")
		{
			$pod		=	$_POST['pod']; 
			$pod		= 	str_replace("/", "-", $pod);
				
			$lp = 1;
			$SLCT_aj = "SELECT * FROM po_temp_des WHERE party_det_id = '$co_cod'"; 
			$QURY_aj = mysqli_query($connect,$SLCT_aj);
			
			$SLCT_pre_po_ids="SELECT group_concat(`po_entry_id`) as pre_po_id FROM  `po_entry` WHERE  `party_det_id` ='$co_cod'";
			$QURY_pre_po_ids = mysqli_query($connect,$SLCT_pre_po_ids);
			$FTCH_pre_po_ids = mysqli_fetch_object($QURY_pre_po_ids);
				
			while($FTCH_aj = mysqli_fetch_object($QURY_aj))
			{
				$cat_id = $FTCH_aj->design_det_id;
				
				$SLCT_tp = "SELECT * FROM design_det WHERE statu_s ='active' AND design_det_id='$cat_id'";
				$QURY_tp = mysqli_query($connect,$SLCT_tp);
				$FTCH_tp = mysqli_fetch_object($QURY_tp);
				
				$SLCT_pre_des="SELECT * FROM  `po_entry_det` WHERE  `po_entry_id` IN ($FTCH_pre_po_ids->pre_po_id) AND cancelled='no' AND  `design_det_id` ='$cat_id'";
				$QURY_pre_des = mysqli_query($connect,$SLCT_pre_des);
				if(mysqli_num_rows($QURY_pre_des)>0)
				{
				}
				else
				{
						$INSRT_d	= "INSERT INTO po_entry_det(`po_entry_id`,`master_cat_id`,`design_det_id`,`post_dt`) VALUES('$max_id','$FTCH_tp->master_cat_id','$cat_id','$cur_time')";	
						$QURY_d		= mysqli_query($connect,$INSRT_d);
				}
						
				//THI IS FOR UPDATING THE DESIGN STATUS IN DESIGN SELECTION TABLE
				$UPDATE = "UPDATE design_sel_det SET ord_status = 'yes',update_dt = '$cur_time' WHERE design_det_id = '$cat_id' AND party_det_id='$co_cod'";
				$QRY    = mysqli_query($connect,$UPDATE); 
				$lp ++;
					
			}
			$DELT = "DELETE FROM po_temp_des WHERE party_det_id = '$co_cod'";
			$QRY12    = mysqli_query($connect,$DELT); 		
		}
		else if($flg=="can")
		{
			$lp = 1;
			$sum_cn_amt = 0;
			foreach($_POST['category'] as $cat_id)
			{								
				$remak		=	$_POST['remak'][$lp];	
									
				$SLCT_tp = "SELECT * FROM po_entry_det WHERE cancelled ='no' AND design_det_id='$cat_id' AND po_entry_id='$po_id'";
				$QURY_tp = mysqli_query($connect,$SLCT_tp);
				$FTCH_tp = mysqli_fetch_object($QURY_tp);
				
				$UPDATE1 = "UPDATE po_entry_det SET remark_s='$remak',cancelled = 'yes', update_dt = '$cur_time' WHERE design_det_id = '$cat_id' AND po_entry_id='$po_id'";		
				$QRY1    = mysqli_query($connect,$UPDATE1); 
				
				//THIS IS FOR UPDATING PO ENTRY TABLE
				$SLCT_fc = "SELECT * FROM po_entry_det WHERE po_entry_id='$po_id' AND cancelled = 'no'";
				$QURY_fc = mysqli_query($connect,$SLCT_fc);
				if(mysqli_num_rows($QURY_fc)< 1)
				{
					$UPDATE2 = "UPDATE po_entry SET cancelled = 'yes', update_dt = '$cur_time' WHERE po_entry_id='$po_id'";		
					$QRY2    = mysqli_query($connect,$UPDATE2);
				}
			
				$SLCT_PIC = "SELECT * FROM piece_loading WHERE po_entry_id='$po_id' AND design_det_id = '$cat_id' AND piece_status='loading'";
				$QURY_PIC = mysqli_query($connect,$SLCT_PIC);
				if(mysqli_num_rows($QURY_PIC)>0)
				{
					$can_dt  = date("d-m-Y");
					$FTCH_PIC = mysqli_fetch_object($QURY_PIC);
					$UPDATE_PIC = "UPDATE piece_loading SET piece_status = 'cancel', update_dt = '$cur_time' WHERE po_entry_id='$po_id' AND design_det_id = '$cat_id' AND piece_loading_id='$FTCH_PIC->piece_loading_id'";		
					$QRY_PIC    = mysqli_query($connect,$UPDATE_PIC);
					
					$INSRT_HIS	= "UPDATE piece_loading_history SET `cancel_type`='Cancel from PO',`cancel_dt`='$can_dt', `update_dt`='$cur_time', `remarks`='' WHERE `piece_loading_id`='$FTCH_PIC->piece_loading_id' AND `po_entry_id`='$po_id' AND `design_det_id`='$cat_id'";
					$QURY_HIS	= mysqli_query($connect,$INSRT_HIS);
				}
				if($FTCH_tp->bill_status=="yes")
				{
					$UPDATE3 = "UPDATE po_finish SET cancelled = 'yes', update_dt = '$cur_time' WHERE design_det_id = '$cat_id' AND po_entry_det_id='$FTCH_tp->po_entry_det_id'";		
					$QRY3    = mysqli_query($connect,$UPDATE3);
					
					$SLCT_f = "SELECT * FROM po_finish WHERE design_det_id = '$cat_id' AND po_entry_det_id='$FTCH_tp->po_entry_det_id'";
					$QURY_f = mysqli_query($connect,$SLCT_f);
					$FTCH_f = mysqli_fetch_object($QURY_f);
					
					$INSRT_stock	= "INSERT INTO stock_det(`design_det_id`, `pack_emp_id`, `cut_emp_id`, `die_fin_date`, `post_dt`)VALUES('$cat_id','$FTCH_f->pack_emp_id','$FTCH_f->cut_emp_id','$FTCH_f->die_fin_date','$cur_time')";		
					$QURY_stock		= mysqli_query($connect,$INSRT_stock);
					
					$UPDATE4 = "UPDATE po_delivery_det SET cancelled = 'yes', update_dt = '$cur_time' WHERE design_det_id = '$cat_id' AND po_finish_id='$FTCH_f->po_finish_id'";		
					$QRY4    = mysqli_query($connect,$UPDATE4);
					
					$SLCT_fc = "SELECT * FROM po_delivery,po_delivery_det WHERE po_delivery.po_entry_id='$po_id' AND po_delivery_det.po_delivery_id = po_delivery.po_delivery_id AND po_delivery_det.cancelled = 'no'";
					$QURY_fc = mysqli_query($connect,$SLCT_fc);
					if(mysqli_num_rows($QURY_fc)< 1)
					{
						$UPDATE5 = "UPDATE po_delivery SET cancelled = 'yes', update_dt = '$cur_time' WHERE po_entry_id='$po_id'";		
						$QRY5    = mysqli_query($connect,$UPDATE5);
					}
									
					$SLCT_del = "SELECT * FROM po_delivery_det WHERE design_det_id = '$cat_id' AND po_finish_id='$FTCH_f->po_finish_id'";
					$QURY_del = mysqli_query($connect,$SLCT_del);
					$FTCH_del = mysqli_fetch_object($QURY_del);
					
					$sum_cn_amt = $sum_cn_amt + $FTCH_del->cost_val; // THIS IS FOR SUM THE CANCELLED AMOUNT
					
					$SLCT_bill = "SELECT * FROM billing_po_design_det WHERE design_det_id='$cat_id' AND po_entry_id='$po_id'";
					$QURY_bill = mysqli_query($connect,$SLCT_bill);
					$FTCH_bill = mysqli_fetch_object($QURY_bill);
					
					$UPDATE_bill = "UPDATE billing_po_design_det SET cancelled ='yes' WHERE design_det_id='$cat_id' AND po_entry_id='$po_id'";		
					$QRY_bill    = mysqli_query($connect,$UPDATE_bill);
					
					$SLCT_bill_po = "SELECT * FROM billing_po_det WHERE cancelled ='no' AND master_cat_id='$FTCH_bill->master_cat_id' AND po_entry_id='$po_id'";
					$QURY_bill_po = mysqli_query($connect,$SLCT_bill_po);
					$FTCH_bill_po = mysqli_fetch_object($QURY_bill_po);
					$no_des = $FTCH_bill_po->no_of_des - 1;
					$cost_val = $no_des * $FTCH_bill_po->per_uint_pr;
					
					$SLCT_INST = "INSERT INTO cancel_billing_po_det(`billing_po_det_id`, `billing_det_id`, `po_entry_id`, `master_cat_id`, `master_cat_name`, `no_of_des`, `per_uint_pr`, `tot_amt`, `post_dt`)VALUES('$FTCH_bill_po->billing_po_det_id','$FTCH_bill_po->billing_det_id','$FTCH_bill_po->po_entry_id','$FTCH_bill_po->master_cat_id','$FTCH_bill_po->master_cat_name','$FTCH_bill_po->no_of_des','$FTCH_bill_po->per_uint_pr','$FTCH_bill_po->tot_amt','$cur_time')";	
					$QURY_INST    = mysqli_query($connect,$SLCT_INST);
					
					if($no_des<=0)
					{
						$UPDATE_bi = "UPDATE billing_po_det SET cancelled ='yes',no_of_des = '$no_des', tot_amt = '$cost_val', update_dt = '$cur_time' WHERE master_cat_id='$FTCH_bill->master_cat_id' AND po_entry_id='$po_id'";		
						$QRY_bi    = mysqli_query($connect,$UPDATE_bi);
					}
					else
					{		
						$UPDATE_bi = "UPDATE billing_po_det SET no_of_des = '$no_des', tot_amt = '$cost_val', update_dt = '$cur_time' WHERE master_cat_id='$FTCH_bill->master_cat_id' AND po_entry_id='$po_id'";		
						$QRY_bi    = mysqli_query($connect,$UPDATE_bi);
					}
				
					//THIS IS FOR UPDATING THE STAUS IN THE PO ENTY DETAILS TABLE AS BILL STATUS NO 			
					$UPDATE13 = "UPDATE po_entry_det SET update_dt = '$cur_time' WHERE po_entry_id='$po_id' AND design_det_id = '$cat_id'";		
					$QRY13    = mysqli_query($connect,$UPDATE13);	
				}
				/*if($FTCH_tp->bill_status=="yes")
				{
					$UPDATE3 = "UPDATE po_finish SET cancelled = 'yes', update_dt = '$cur_time' WHERE design_det_id = '$cat_id' AND po_entry_det_id='$FTCH_tp->po_entry_det_id'";		
					$QRY3    = mysqli_query($connect,$UPDATE3);
					
					$SLCT_f = "SELECT * FROM po_finish WHERE design_det_id = '$cat_id' AND po_entry_det_id='$FTCH_tp->po_entry_det_id'";
					$QURY_f = mysqli_query($connect,$SLCT_f);
					$FTCH_f = mysqli_fetch_object($QURY_f);
					
					$INSRT_stock	= "INSERT INTO stock_det(`design_det_id`, `pack_emp_id`, `cut_emp_id`, `die_fin_date`, `post_dt`)VALUES('$cat_id','$FTCH_f->pack_emp_id','$FTCH_f->cut_emp_id','$FTCH_f->die_fin_date','$cur_time')";		
					$QURY_stock		= mysqli_query($connect,$INSRT_stock);
					
					$UPDATE4 = "UPDATE po_delivery_det SET cancelled = 'yes', update_dt = '$cur_time' WHERE design_det_id = '$cat_id' AND po_finish_id='$FTCH_f->po_finish_id'";		
					$QRY4    = mysqli_query($connect,$UPDATE4);
					
					$SLCT_fc = "SELECT * FROM po_delivery,po_delivery_det WHERE po_delivery.po_entry_id='$po_id' AND po_delivery_det.po_delivery_id = po_delivery.po_delivery_id AND po_delivery_det.cancelled = 'no'";
					$QURY_fc = mysqli_query($connect,$SLCT_fc);
					if(mysqli_num_rows($QURY_fc)< 1)
					{
						$UPDATE5 = "UPDATE po_delivery SET cancelled = 'yes', update_dt = '$cur_time' WHERE po_entry_id='$po_id'";		
						$QRY5    = mysqli_query($connect,$UPDATE5);
					}
									
					$SLCT_del = "SELECT * FROM po_delivery_det WHERE design_det_id = '$cat_id' AND po_finish_id='$FTCH_f->po_finish_id'";
					$QURY_del = mysqli_query($connect,$SLCT_del);
					$FTCH_del = mysqli_fetch_object($QURY_del);
					
					$sum_cn_amt = $sum_cn_amt + $FTCH_del->cost_val; // THIS IS FOR SUM THE CANCELLED AMOUNT
						
					$SLCT_bill = "SELECT * FROM billing_po_design_det WHERE design_det_id='$cat_id' AND po_entry_id='$po_id'";
					$QURY_bill = mysqli_query($connect,$SLCT_bill);
					$FTCH_bill = mysqli_fetch_object($QURY_bill);
					
					$UPDATE_bill = "UPDATE billing_po_design_det SET cancelled ='yes' WHERE design_det_id='$cat_id' AND po_entry_id='$po_id'";		
					$QRY_bill    = mysqli_query($connect,$UPDATE_bill);
					
					$SLCT_bill_po = "SELECT * FROM billing_po_det WHERE cancelled ='no' AND master_cat_id='$FTCH_bill->master_cat_id' AND po_entry_id='$po_id'";
					$QURY_bill_po = mysqli_query($connect,$SLCT_bill_po);
					$FTCH_bill_po = mysqli_fetch_object($QURY_bill_po);
					$no_des = $FTCH_bill_po->no_of_des - 1;
					$cost_val = $no_des * $FTCH_bill_po->per_uint_pr;
					
					$SLCT_INST = "INSERT INTO cancel_billing_po_det(`billing_po_det_id`, `billing_det_id`, `po_entry_id`, `master_cat_id`, `master_cat_name`, `no_of_des`, `per_uint_pr`, `tot_amt`, `post_dt`)VALUES('$FTCH_bill_po->billing_po_det_id','$FTCH_bill_po->billing_det_id','$FTCH_bill_po->po_entry_id','$FTCH_bill_po->master_cat_id','$FTCH_bill_po->master_cat_name','$FTCH_bill_po->no_of_des','$FTCH_bill_po->per_uint_pr','$FTCH_bill_po->tot_amt','$cur_time')";	
					$QURY_INST    = mysqli_query($connect,$SLCT_INST);
					
					if($no_des<=0)
					{
						$UPDATE_bi = "UPDATE billing_po_det SET cancelled ='yes',no_of_des = '$no_des', tot_amt = '$cost_val', update_dt = '$cur_time' WHERE master_cat_id='$FTCH_bill->master_cat_id' AND po_entry_id='$po_id'";		
						$QRY_bi    = mysqli_query($connect,$UPDATE_bi);
					}
					else
					{		
						$UPDATE_bi = "UPDATE billing_po_det SET no_of_des = '$no_des', tot_amt = '$cost_val', update_dt = '$cur_time' WHERE master_cat_id='$FTCH_bill->master_cat_id' AND po_entry_id='$po_id'";		
						$QRY_bi    = mysqli_query($connect,$UPDATE_bi);
					}
				
					//THIS IS FOR UPDATING THE STAUS IN THE PO ENTY DETAILS TABLE AS BILL STATUS NO 			
					$UPDATE13 = "UPDATE po_entry_det SET update_dt = '$cur_time' WHERE po_entry_id='$po_id' AND design_det_id = '$cat_id'";		
					$QRY13    = mysqli_query($connect,$UPDATE13);	
				}*/
			
				if($FTCH_tp->del_status=="yes")
				{
					$UPDATE6 = "UPDATE po_finish SET cancelled = 'yes', update_dt = '$cur_time' WHERE design_det_id = '$cat_id' AND po_entry_det_id='$FTCH_tp->po_entry_det_id'";		
					$QRY6    = mysqli_query($connect,$UPDATE6);
					
					$SLCT_f = "SELECT * FROM po_finish WHERE design_det_id = '$cat_id' AND po_entry_det_id='$FTCH_tp->po_entry_det_id'";
					$QURY_f = mysqli_query($connect,$SLCT_f);
					$FTCH_f = mysqli_fetch_object($QURY_f);
					
					$INSRT_stock	= "INSERT INTO stock_det(`design_det_id`, `pack_emp_id`, `cut_emp_id`, `die_fin_date`, `post_dt`)VALUES('$cat_id','$FTCH_f->pack_emp_id','$FTCH_f->cut_emp_id','$FTCH_f->die_fin_date','$cur_time')";		
					$QURY_stock		= mysqli_query($connect,$INSRT_stock);
					
					$UPDATE7 = "UPDATE po_delivery_det SET cancelled = 'yes', update_dt = '$cur_time' WHERE design_det_id = '$cat_id' AND po_finish_id='$FTCH_f->po_finish_id'";		
					$QRY7    = mysqli_query($connect,$UPDATE7);
					
					$SLCT_fc = "SELECT * FROM po_delivery,po_delivery_det WHERE po_delivery.po_entry_id='$po_id' AND po_delivery_det.po_delivery_id = po_delivery.po_delivery_id AND po_delivery_det.cancelled = 'no'";
					$QURY_fc = mysqli_query($connect,$SLCT_fc);
					if(mysqli_num_rows($QURY_fc)< 1)
					{
						$UPDATE8 = "UPDATE po_delivery SET cancelled = 'yes', update_dt = '$cur_time' WHERE po_entry_id='$po_id'";		
						$QRY8    = mysqli_query($connect,$UPDATE8);
					}				 
				}
				
				if($FTCH_tp->fin_status=="yes") // THIS IS FOR UPDATING PO FINISH TABLE AND INSERTING STOCK TABLE
				{
					$UPDATE9 = "UPDATE po_finish SET cancelled = 'yes', update_dt = '$cur_time' WHERE design_det_id = '$cat_id' AND po_entry_det_id='$FTCH_tp->po_entry_det_id'";		
					$QRY9    = mysqli_query($connect,$UPDATE9);
					
					$SLCT_f = "SELECT * FROM po_finish WHERE design_det_id = '$cat_id' AND po_entry_det_id='$FTCH_tp->po_entry_det_id'";
					$QURY_f = mysqli_query($connect,$SLCT_f);
					$FTCH_f = mysqli_fetch_object($QURY_f);
					
					$INSRT_stock	= "INSERT INTO stock_det(`design_det_id`, `pack_emp_id`, `cut_emp_id`, `die_fin_date`, `post_dt`)VALUES('$cat_id','$FTCH_f->pack_emp_id','$FTCH_f->cut_emp_id','$FTCH_f->die_fin_date','$cur_time')";		
					$QURY_stock		= mysqli_query($connect,$INSRT_stock);
			
				}			
						
				//THI IS FOR UPDATING THE DESIGN STATUS IN DESIGN SELECTION TABLE
				$UPDATE10 = "UPDATE design_sel_det SET ord_status = 'no',update_dt = '$cur_time' WHERE design_det_id = '$cat_id' AND party_det_id='$co_cod'";
				$QRY10    = mysqli_query($connect,$UPDATE10); 
				$lp ++;						
			}
			
			$SLCT_b = "SELECT COUNT(design_det_id ) AS to_des FROM billing_po_design_det WHERE po_entry_id='$po_id' AND `cancelled` = 'no'";
			$QURY_b = mysqli_query($connect,$SLCT_b);
			$FTCH_b = mysqli_fetch_object($QURY_b);
			
			$SLCT_bls = "SELECT billing_po_det.billing_det_id,bill_amt,bill_tax_amt,bill_tax,bill_tax,courier_amt FROM billing_po_det,billing_det WHERE billing_det.billing_det_id =billing_po_det.billing_det_id AND billing_po_det.po_entry_id='$po_id' GROUP BY billing_po_det.billing_det_id";
			$QURY_bls = mysqli_query($connect,$SLCT_bls);
			if(mysqli_num_rows($QURY_bls)>0)
			{
				$FTCH_bls = mysqli_fetch_object($QURY_bls);
				
				$bl_amt =$FTCH_bls->bill_amt;
				$bl_tx_amt = $FTCH_bls->bill_tax_amt; 
				$act_bl_amt = ($FTCH_bls->bill_amt - $FTCH_bls->bill_tax_amt)-$FTCH_bls->courier_amt;
				$bl_tax = $FTCH_bls->bill_tax;
				
				//THIS IS FOR UPDATING THE ACCOUNTS PARTY TABLE CODE STARTS HERE
				$SLCT_acc = "SELECT * FROM ac_party_det WHERE party_det_id='$co_cod'";
				$QURY_acc = mysqli_query($connect,$SLCT_acc);
				$FTCH_acc = mysqli_fetch_object($QURY_acc);
				
				$tot_cost = $FTCH_acc->total_bill_amt - $bl_amt;
				$tot_rec  = $FTCH_acc->total_rece_amt;
											
				//THIS IS FOR UPDATING THE ACCOUNTS PARTY TABLE CODE ENDS HERE
				
				if($FTCH_b->to_des==0)
				{
					$tx_amt = ($bl_tax * 10) / 100;
					$act_amt = $tx_amt + 10;
							
					$UPDATE11 = "UPDATE billing_det SET bill_amt = '$act_amt', total_bill_amt = '$act_amt', bill_tax_amt='$tx_amt', cancelled = 'yes', update_dt = '$cur_time' WHERE billing_det_id='$FTCH_bls->billing_det_id'";		
					$QRY11    = mysqli_query($connect,$UPDATE11);
					
					//this is for updating the billing and pending amount in acounts party table
					$tot_bil_cc = $tot_cost + $act_amt;
					$tot_pen_cc = $tot_bil_cc - $tot_rec;
					if($tot_bil_cc < $tot_rec){ $ac_cr_blan = abs($tot_pen_cc); }else{ $ac_cr_blan = 0; }
			
					if($tot_pen_cc<0){ $tot_pen_cc = 0; }
									
					$UPDATE12 = "UPDATE ac_party_det SET total_bill_amt='$tot_bil_cc', total_pending='$tot_pen_cc',tot_cre_bln='$ac_cr_blan', update_dt= '$cur_time' WHERE party_det_id='$co_cod'";
					$QRY12    = mysqli_query($connect,$UPDATE12);		
				
				}
				else
				{
					$act_amt1 = $act_bl_amt - $sum_cn_amt; // this is for calculating the actual amount from the cancelled amount
					$tx_amt = ($bl_tax/100) * $act_amt1;
					$act_amt = $tx_amt + $act_amt1 + $FTCH_bls->courier_amt;
					
					$UPDATE11 = "UPDATE billing_det SET bill_amt = '$act_amt', total_bill_amt = '$act_amt', bill_tax_amt='$tx_amt', update_dt = '$cur_time' WHERE billing_det_id='$FTCH_bls->billing_det_id'";		
					$QRY11    = mysqli_query($connect,$UPDATE11);
					
					//this is for updating the billing and pending amount in acounts party table
					$tot_bil_cc = $tot_cost + $act_amt;
					$tot_pen_cc = $tot_bil_cc - $tot_rec;
					if($tot_bil_cc < $tot_rec){ $ac_cr_blan = abs($tot_pen_cc); }else{ $ac_cr_blan = 0; }
					if($tot_pen_cc<0){ $tot_pen_cc = 0; }
					
					$UPDATE12 = "UPDATE ac_party_det SET total_bill_amt='$tot_bil_cc', total_pending='$tot_pen_cc',tot_cre_bln='$ac_cr_blan', update_dt= '$cur_time' WHERE party_det_id='$co_cod'";
					$QRY12    = mysqli_query($connect,$UPDATE12);							
				}
			}
		}	
	?>
	<script type="text/javascript">
		window.location='../po_entry_list.php';
	</script>
	<?php
	}
	else if($page_id==4)		//send the mail to client request from list page when clicking on po_no
	{
		$po_id		=	base64_decode(base64_decode(base64_decode($_GET['po_id'])));
		$UPDATE 	= 	"UPDATE po_entry SET mail_send = 'yes',update_dt = '$cur_time' WHERE po_entry_id = '$po_id'";
		$QRY   	 	= 	mysqli_query($connect,$UPDATE);
		
		
		$SLCT_po = "SELECT * FROM po_entry WHERE  po_entry_id = '$po_id'";
		$QURY_po = mysqli_query($connect,$SLCT_po);
		while($FTCH_po = mysqli_fetch_object($QURY_po))
		{
			$co_cod=$FTCH_po->party_det_id;
			$po_no=$FTCH_po->po_no;
			$SLCT_pt = "SELECT * FROM party_det WHERE  party_det_id = '$co_cod'";
			$QURY_pt = mysqli_query($connect,$SLCT_pt);
			if(mysqli_num_rows($QURY_pt)>0)
			{
				$FTCH_pt = mysqli_fetch_object($QURY_pt);
				$pt_name	=	$FTCH_pt->cmp_name;
				$cli_mail	=	$FTCH_pt->email_id_1;
				$mobile_no=$FTCH_pt->con_no_1;
			}
			else
			{
				$pt_name='';
				$cli_mail='';
			}		
		}
		$max_id = $po_id;
		$tot=0;
		$SLCT = "SELECT `master_cat_id`, count(*) as c FROM `po_entry_det` WHERE `po_entry_id`='$max_id' AND cancelled = 'no' GROUP By `master_cat_id`;";
		$QURY = mysqli_query($connect,$SLCT);
		while($fetch	=	mysqli_fetch_object($QURY))
		{
			$tot = $tot + $fetch->c;
		}
		
		include "../mail/po_order_send_mail.php";
		$tto=$cli_mail;
		$subject="Reg:Order Confirmation Details";
		$headers = "MIME-Version: 1.0" . "\r\n";
		$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
		// More headers
		$headers .= 'From: Thahaayaa Sales Team <sales@thahaayaa.com>' . "\r\n";				
		$headers .= 'Bcc:'.$cc_id. "\r\n";	
		mail("$tto",$subject,$mail_body,$headers);
		
		if($mobile_no!='')
		{
			$tot_qty = $tot;
			$to = $mobile_no;
			$sms = "Thahaayaa thanks to you for new order and Order No:".$po_no.",Qty: ".$tot_qty." We took over it into processing, you will receive the delivery shortly. Further-9894347273";
			$sms_url   	= 	sprintf($sms_domain, $workingkey, $to, $senderid, urlencode($sms));
			openurl($sms_url);
		}
	?>
		<script type="text/javascript">
			window.location='../po_entry_list.php';
		</script>
	<?php
	}
	else if($page_id==5)
	{	
		if(isset($_GET['pg']) && $_GET['pg']=='cli')
		{
			$pg_url = 'cli_ord_req_pend_list.php';
		}
		else
		{
			$pg_url = 'cli_ord_req_list.php';
		}
				
		$pa_id		=	$_POST['party_id'];
		$sel_date	=	$_POST['sel_date'];
			
		$i=1;
		foreach($_POST['category'] as $cat_id)
		{
			$SLCT_tp = "SELECT * FROM design_sel_det WHERE statu_s ='active' AND party_det_id='$pa_id' AND design_det_id='$cat_id' ORDER BY party_det_id ";
			$QURY_tp = mysqli_query($connect,$SLCT_tp);
			$FTCH_tp = mysqli_fetch_object($QURY_tp);
		
			$SLCT_INST = "INSERT INTO des_sel_cancel(`party_det_id`, `design_det_id`, `sel_date`,`post_dt`)VALUES('$pa_id','$cat_id','$FTCH_tp->sel_date','$cur_time')";	
			$QURY_INST    = mysqli_query($connect,$SLCT_INST);
			$i++;
			
			$DELT = "DELETE FROM design_sel_det WHERE party_det_id = '$pa_id' AND design_det_id='$cat_id' ";
			$QRY12    = mysqli_query($connect,$DELT); 
		}
?>
		<script type="text/javascript">
			window.location='../<?php echo $pg_url; ?>';
		</script>
<?php
	}
?>