<?php
	//Connects to your Database
	include "../db_connection/db_connection.php";
	include "../config/config.php";	
	ini_set('max_execution_time', 800);		
	$page_id	=	base64_decode(base64_decode(base64_decode($_GET['page_id']))); //THIS IS PAGE ID  
	$po_id	=	base64_decode(base64_decode(base64_decode($_GET['po_en_id'])));                  
	
	//echo $po_id;
	//po_no,co_cod,pod,remak
if($page_id==1) //this is for basic details add page	
{ 		
		$SLCT_po = "SELECT * FROM po_entry WHERE po_entry_id='$po_id'";
		//echo $SLCT_po;
		$QURY_po = mysqli_query($connect,$SLCT_po);
		$FTCH_po = mysqli_fetch_object($QURY_po);		
		
		$SLCT_tp = "SELECT * FROM party_det WHERE party_det_id = '$FTCH_po->party_det_id'";
		//echo $SLCT_tp;
		$QURY_tp = mysqli_query($connect,$SLCT_tp);
		$FTCH_tp = mysqli_fetch_object($QURY_tp);
		
		$cli_emailid = $FTCH_tp->email_id_1;
		$com_name  	 = $FTCH_tp->cmp_name;	
		$party_det_id = $FTCH_tp->party_det_id;
		
		$po_dd		=	$_POST['pdd'];
		$po_dd		= 	str_replace("/", "-", $po_dd);
		//$co_cod		=	base64_decode(base64_decode(base64_decode($_POST['co_cod']))); //THIS IS PAGE ID  
		//$pod		=	$_POST['pod']; 
		
		$SLCT_PO = "SELECT po_delivery_id FROM po_delivery WHERE po_entry_id='$po_id' AND STR_TO_DATE(delivery_date,'%d-%m-%Y') = STR_TO_DATE('$po_dd','%d-%m-%Y')";
		$QURY_PO = mysqli_query($connect,$SLCT_PO);  
		if(mysqli_num_rows($QURY_PO)>0)
		{
			$FTCH_PO = mysqli_fetch_object($QURY_PO);
			$max_id = $FTCH_PO->po_delivery_id;
			$po_flagg = 1;
		}
		else
		{		
			$INSRT		= "INSERT INTO po_delivery(`po_entry_id`, `delivery_date`, `post_dt`) VALUES('$po_id','$po_dd','$cur_time')";		
			//echo $INSRT;
			$QURY		= mysqli_query($connect,$INSRT);
			
			$SLCT_sk = "SELECT  MAX(po_delivery_id) as max_ids FROM po_delivery ORDER BY po_delivery_id ASC";
			$QURY_sk = mysqli_query($connect,$SLCT_sk);  
			$FTCH_sk = mysqli_fetch_object($QURY_sk);
			$max_id = $FTCH_sk->max_ids;
			$po_flagg = 2;
		}
				
		
		
		$lp = 1;
		$lp_cnt=1;
		
		foreach($_POST['category'] as $cat_id)
		{
			$spl_id=explode(",",$cat_id);
			//echo $cat_id;
			//echo "po_finish_id=".$spl_id[1];
			//echo "design_det_id=".$spl_id[0];	
			//$remak		=	$_POST['remak'][$lp]; 	
				
			$INSRT_d	= "INSERT INTO po_delivery_det(`po_delivery_id`, `po_finish_id`, `master_cat_id`, `cost_val`, `design_det_id`, `post_dt`) VALUES('$max_id','$spl_id[1]','$spl_id[3]','$spl_id[4]','$spl_id[0]','$cur_time')";		
			//echo $INSRT_d;
			$QURY_d		= mysqli_query($connect,$INSRT_d);
			
			$SLCT_po = "SELECT * FROM po_finish WHERE po_finish_id='$spl_id[1]'";
			//echo $SLCT_po;
			$QURY_po = mysqli_query($connect,$SLCT_po);
			$FTCH_po = mysqli_fetch_object($QURY_po);
					
			//THI IS FOR UPDATING THE DESIGN STATUS
			$UPDATE = "UPDATE po_entry_det SET del_status='yes',update_dt = '$cur_time' WHERE po_entry_det_id = '$spl_id[2]'";
			//echo $UPDATE;
			$QRY    = mysqli_query($connect,$UPDATE); 
			$UPDATE_fin = "UPDATE po_finish SET del_status='yes', update_dt = '$cur_time' WHERE po_finish_id = '$spl_id[1]'";
			//echo $UPDATE_fin;
			$QRY_fin   = mysqli_query($connect,$UPDATE_fin); 
			$lp_cnt++;				
		}
		if($po_flagg == 1)
		{
			$del_qty = $lp_cnt-1;
			$INSRT_DEL = "UPDATE `delivery_reminder` SET `delivery_qty`=`delivery_qty`+$del_qty WHERE po_delivery_id='$max_id'";
			$QURY_DEL   = mysqli_query($connect,$INSRT_DEL);
		}
		else if($po_flagg == 2)
		{
			$del_qty = $lp_cnt-1;
			$date   = new DateTime($po_dd);
			$date->modify('+4 days');
			$SLCT_REMM = "SELECT emp_det_id FROM exist_party_det WHERE party_det_id='$party_det_id' AND des_regard!='0'";
			$QURY_REMM = mysqli_query($connect,$SLCT_REMM); 
			if(mysqli_num_rows($QURY_REMM)>0)
			{
				$FTCH_REMM   = mysqli_query($connect,$QURY_REMM); 
				$emp_id = $FTCH_REMM->emp_det_id;
			}
			else
			{		
				$emp_id = 0;
			}
			$rem_date = $date->format('d-m-Y');
			$INSRT_DEL = "INSERT INTO `delivery_reminder`(`emp_det_id`,`party_det_id`, `po_entry_id`, `po_delivery_id`, `delivery_dt`, `delivery_qty`, `reminder_date`, `post_dt`) VALUES('$emp_id','$party_det_id','$po_id','$max_id','$po_dd','$del_qty','$rem_date','$cur_time');";
			$QURY_DEL   = mysqli_query($connect,$INSRT_DEL);
		}
		//$email_ida="sales@thahaayaa.com";
		//$subject ="Reg:Po Delivery Details"; 
		
	/*	include "../mail/po_delivery_mail.php";
		//echo $mail_body;
			
		//if(mail($cli_emailid, $subject,$mail_body, "From:" . $email_ida ));
		
		$tto=$cli_emailid;
		$subject="Reg:Po Delivery Details";
		
		$headers = "MIME-Version: 1.0" . "\r\n";
		$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
		
		// More headers
		$headers .= 'From: <sales@thahaayaa.com>' . "\r\n";
		mail("$tto",$subject,$mail_body,$headers);*/
		
?>
		<script type="text/javascript">
            //alert("Data inserted successfully");
            //history.back();
            window.location='../po_delivery_list.php';
        </script>
<?php
}
else if($page_id==2)
{
	$cost_val=0;
	$to_des=0;
	$bill_flg = '';
	$po_id		=	$_POST['po_id'];
	$po_del_id	=	$_POST['po_del_id'];
	
	$SLCT_po  = "SELECT * FROM po_entry WHERE po_entry_id='$po_id'";
	$QURY_po  = mysqli_query($connect,$SLCT_po);
	$FTCH_po  = mysqli_fetch_object($QURY_po);
	$party_id = $FTCH_po->party_det_id;	
	
	$sum_cn_amt = 0;
	foreach($_POST['category'] as $cat_id)
	{	
				
			
		$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);
		
		if($FTCH_tp->bill_status=="yes")
		{
			$UPDATE1 = "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'";		
			//echo $UPDATE1."<br />";
			$QRY1    = mysqli_query($connect,$UPDATE1);
			
			$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')";		
			//echo $INSRT_stock."<br />";
			$QURY_stock		= mysqli_query($connect,$INSRT_stock);
			
			$UPDATE2 = "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'";		
			//echo $UPDATE2."<br />";
			$QRY2    = mysqli_query($connect,$UPDATE2);
			
			$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)
			{
				$UPDATE3 = "UPDATE po_delivery SET cancelled = 'yes', update_dt = '$cur_time' WHERE po_entry_id='$po_id'";		
				//echo $UPDATE3."<br />";
				$QRY3    = mysqli_query($connect,$UPDATE3);
			}
			//$bill_flg = 1; //this is for billing updagte flage	
					
							
			$SLCT_del = "SELECT * FROM po_delivery_det WHERE design_det_id='$cat_id' AND po_delivery_id='$po_del_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'";		
			//echo $UPDATE_bill."<br /><br />";
			$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'";
			//echo $SLCT_bill_po."<br /><br />";
			$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')";	
			//echo $SLCT_INST."<br />";
			$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'";		
				//echo $UPDATE_bi."<br />";
				$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'";		
				//echo $UPDATE_bi."<br />";
				$QRY_bi    = mysqli_query($connect,$UPDATE_bi);
			}
			
			//THIS IS FOR UPDATING THE STAUS IN THE PO ENTY DETAILS TANLE AS BILL STATUS NO 			
				$UPDATE13 = "UPDATE po_entry_det SET bill_status = 'no', update_dt = '$cur_time' WHERE po_entry_id='$po_id' AND design_det_id = '$cat_id'";		
				//echo $UPDATE13."<br/>";		
				$QRY13    = mysqli_query($connect,$UPDATE13);	
				
		}
		else if($FTCH_tp->del_status=="yes")
		{
			$UPDATE4 = "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'";		
			//echo $UPDATE4."<br />";
			$QRY4    = mysqli_query($connect,$UPDATE4);
			
			$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')";		
			//echo $INSRT_stock."<br />";
			$QURY_stock		= mysqli_query($connect,$INSRT_stock);
			
			$UPDATE5 = "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'";		
			//echo $UPDATE5."<br />";
			$QRY5    = mysqli_query($connect,$UPDATE5);
			
			$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)
			{
				$UPDATE6 = "UPDATE po_delivery SET cancelled = 'yes', update_dt = '$cur_time' WHERE po_entry_id='$po_id'";		
				//echo $UPDATE6."<br />";
				$QRY6    = mysqli_query($connect,$UPDATE6);
			}
			//$bill_flg = 1; //this is for billing updagte flage				
			 
		}
		//echo $cat_id."<br/>";
		//THIS IS FOR UPDATING THE DELIVERY STATUS AND BILL STATUS IN THE TABLE
		$UPDATE7 = "UPDATE po_entry_det SET fin_status = 'no',del_status = 'no', update_dt = '$cur_time' WHERE design_det_id = '$cat_id' AND po_entry_id='$po_id'";		
		//echo $UPDATE7."<br />";
		$QRY7    = mysqli_query($connect,$UPDATE7);
	}
	
	$SLCT_b = "SELECT COUNT(design_det_id ) AS to_des FROM billing_po_design_det WHERE po_entry_id='$po_id' AND `cancelled` = 'no'";
	//echo $SLCT_b."<br/>";
	$QURY_b = mysqli_query($connect,$SLCT_b);
	$FTCH_b = mysqli_fetch_object($QURY_b);
	//echo $FTCH_b->billing_po_det_id;
	
	//$SLCT_bls = "SELECT * 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'";
	$SLCT_bls = "SELECT billing_po_det.billing_det_id,bill_amt,bill_tax_amt,bill_tax 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";
	//echo $SLCT_bls."<br/>";
	$QURY_bls = mysqli_query($connect,$SLCT_bls);
	$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;
	$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='$party_id'";
	$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'";		
		//echo $UPDATE11."<br />";
		$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='$party_id'";
		//echo $UPDATE12."<br />";
		$QRY12    = mysqli_query($connect,$UPDATE12);		
	}
	else
	{
		$act_amt = $act_bl_amt - $sum_cn_amt; // this is for calculating the actual amount from the cancelled amount
		$tx_amt = ($bl_tax * $act_amt) / 100;
		$act_amt = $tx_amt + $act_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'";		
		//echo $UPDATE11."<br />";
		$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='$party_id'";
		//echo $UPDATE12."<br />";
		$QRY12    = mysqli_query($connect,$UPDATE12);	
		
	}
		
	
?>
	<script type="text/javascript">
		//alert("Data inserted successfully");
		//history.back();
		window.location='../po_delivery_list.php';
	</script>
<?php
}
?>
	
