<?php
	include "../db_connection/db_connection.php";
	include "../config/config.php";

//THIS IS FOR FINDING THE NUMBER OF FINISHED PENDING AMT
	$fin_pen_amt = 0;
	$fin_pen_qty = 1;
	$mast_pice1 = 0;
	$mast_pice2 = 0;
	$mast_pice3 = 0;
	$mast_pice4 = 0;

	$SLCT = "SELECT IFNULL(design_det.master_pieces,'0') AS master_pieces,po_entry_det.* FROM po_entry_det,design_det WHERE design_det.design_det_id=po_entry_det.design_det_id AND fin_status ='no' AND cancelled='no' ORDER BY po_entry_det_id ASC";
	$QURY = mysqli_query($connect,$SLCT);
	while($FTCH = mysqli_fetch_object($QURY))
	{
		$SLCT_E = "SELECT * FROM po_entry,party_det WHERE party_det.party_det_id = po_entry.party_det_id AND po_entry.po_entry_id = '$FTCH->po_entry_id'"; 
		//echo $SLCT_E."<br/>";
		$QURY_E = mysqli_query($connect,$SLCT_E);
		$FTCH_E = mysqli_fetch_object($QURY_E);

		$SLCT_E1 = "SELECT * FROM party_cost_det WHERE party_det_id = '$FTCH_E->party_det_id' AND master_cat_id = '$FTCH->master_cat_id' AND updated = 'no'"; 
		//echo $SLCT_E1."<br>";
		$QURY_E1 = mysqli_query($connect,$SLCT_E1);
		if(mysqli_num_rows($QURY_E1)>0)
		{
			$FTCH_E1 = mysqli_fetch_object($QURY_E1);
			$cost_val = $FTCH_E1->cost_val;
		}
		else
		{
			$cost_val = 0;
		}
		$mast_pice1 = $mast_pice1 + $FTCH->master_pieces;
		$fin_pen_amt = $fin_pen_amt +$cost_val ;
		$fin_pen_qty ++;
	}


//THIS IS FOR FINDING THE NUMBER OF DELIVERD PENDING AMT
	$del_pen_amt = 0;
	$del_pen_qty = 1;
	$SLCT = "SELECT IFNULL(design_det.master_pieces,'0') AS master_pieces,po_entry_det.* FROM po_entry_det,design_det WHERE design_det.design_det_id=po_entry_det.design_det_id AND fin_status ='yes' AND del_status= 'no' AND cancelled='no' ORDER BY po_entry_det_id ASC";
	$QURY = mysqli_query($connect,$SLCT);
	while($FTCH = mysqli_fetch_object($QURY)){
	$SLCT_E = "SELECT * FROM po_entry,party_det WHERE party_det.party_det_id = po_entry.party_det_id AND po_entry.po_entry_id = '$FTCH->po_entry_id'"; 
	$QURY_E = mysqli_query($connect,$SLCT_E);
	$FTCH_E = mysqli_fetch_object($QURY_E);

	$SLCT_E1 = "SELECT * FROM party_cost_det WHERE party_det_id = '$FTCH_E->party_det_id' AND master_cat_id = '$FTCH->master_cat_id' AND updated = 'no'"; 
	$QURY_E1 = mysqli_query($connect,$SLCT_E1);
	if(mysqli_num_rows($QURY_E1)>0)
	{
		$FTCH_E1 = mysqli_fetch_object($QURY_E1);
		$mast_pice2 = $mast_pice2 + $FTCH->master_pieces;
		$del_pen_amt = $del_pen_amt +$FTCH_E1->cost_val;
		$del_pen_qty ++;
	}
}

		//THIS IS FOR FINDING THE NUMBER OF STOCKS AND STOCK AMT
		$st_amt = 0;
		$st_qty = 1;
		$SLCT = "SELECT IFNULL(design_det.master_pieces,'0') AS master_pieces,stock_det.* FROM stock_det,design_det WHERE design_det.design_det_id=stock_det.design_det_id AND st_fin_status ='no' ORDER BY stock_det_id ASC";
		$QURY = mysqli_query($connect,$SLCT);
		while($FTCH = mysqli_fetch_object($QURY))
		{
			$SLCT_E = "SELECT * FROM design_det WHERE design_det_id = '$FTCH->design_det_id'"; 
			//echo $SLCT_E;
			$QURY_E = mysqli_query($connect,$SLCT_E);
			$FTCH_E = mysqli_fetch_object($QURY_E);

			$SLCT_E1 = "SELECT * FROM master_cat WHERE master_cat_id = '$FTCH_E->master_cat_id'";
			//echo $SLCT_E1;
			$QURY_E1 = mysqli_query($connect,$SLCT_E1);
			$FTCH_E1 = mysqli_fetch_object($QURY_E1);

			$mast_pice3 = $mast_pice3 + $FTCH->master_pieces;
			$st_amt = $st_amt + $FTCH_E1->cost_vl;
			$st_qty ++;
		}

		//THIS IS FOR FINDING THE NUMBER OF BILL PENDING PART
		$bil_pen_amt = 0;
		$bil_pen_qty = 1;

		$SLCT_ppo = "SELECT GROUP_CONCAT(DISTINCT po_entry.po_entry_id) AS po_entry_id, GROUP_CONCAT(DISTINCT po_entry_det.master_cat_id) AS mas_cat_id, party_det.party_det_id, po_entry_det.master_cat_id,party_code,cmp_name, COUNT(po_entry_det_id) AS cont,IFNULL(SUM(design_det.master_pieces),'0') AS master_pieces FROM po_entry, po_entry_det,party_det,design_det WHERE design_det.design_det_id=po_entry_det.design_det_id AND po_entry.party_det_id = party_det.party_det_id AND po_entry_det.po_entry_id = po_entry.po_entry_id AND po_entry_det.del_status = 'yes' AND po_entry_det.bill_status = 'no' AND po_entry_det.cancelled='no' GROUP BY party_det.party_det_id";
		$QURY_ppo = mysqli_query($connect,$SLCT_ppo);
		while($FTCH_ppo = mysqli_fetch_object($QURY_ppo))
		{
			$SLCT_pr = "SELECT cost_val FROM po_delivery,po_delivery_det WHERE po_delivery.po_entry_id IN($FTCH_ppo->po_entry_id) AND po_delivery_det.po_delivery_id = po_delivery.po_delivery_id AND po_delivery_det.master_cat_id IN($FTCH_ppo->mas_cat_id)";
			$QURY_pr = mysqli_query($connect,$SLCT_pr);
			while($FTCH_pr = mysqli_fetch_array($QURY_pr))
			{
				$bil_pen_amt = $bil_pen_amt + $FTCH_pr['cost_val'];
			}
			$mast_pice4 = $mast_pice4 + $FTCH_ppo->master_pieces;
			$bil_pen_qty = $bil_pen_qty +$FTCH_ppo->cont;
		}

/*$fin_pen_qty = $fin_pen_qty;
$del_pen_qty = $del_pen_qty;
$bil_pen_amt;
$bil_pen_qty;
$st_amt;
$st_qty;
$mast_pice1;
$mast_pice2;
$mast_pice3;
$mast_pice4;*/

$clean_cron = mysqli_query($connect,"TRUNCATE cron_topmenu");
$insert_cron = mysqli_query($connect,"insert into cron_topmenu(fin_pen_amt,fin_pen_qty,del_pen_qty,del_pen_amt,bil_pen_amt,bil_pen_qty,st_amt,st_qty,mast_pice1,mast_pice2,mast_pice3,mast_pice4) values('".$fin_pen_amt."','".$fin_pen_qty."','".$del_pen_qty."','".$del_pen_amt."','".$bil_pen_amt."','".$bil_pen_qty."','".$st_amt."','".$st_qty."', '".$mast_pice1."','".$mast_pice2."','".$mast_pice3."','".$mast_pice4."')");

echo number_format($fin_pen_amt,2)."**".($fin_pen_qty-1)."**".number_format($del_pen_amt,2)."**".($del_pen_qty-1)."**".number_format($bil_pen_amt,2)."**".($bil_pen_qty-1)."**".$mast_pice1."**". $mast_pice2."**".$mast_pice3."**".$mast_pice4."**".number_format($st_amt,2)."**".($st_qty-1);

?>